|
|
1.1 root 1: /*
2: * Coherent.
3: * Print formatted.
4: *
5: * $Log: printf.c,v $
6: * Revision 1.1 92/07/17 15:18:10 bin
7: * Initial revision
8: *
9: * Revision 1.1 88/03/24 16:14:13 src
10: * Initial revision
11: *
12: * 87/09/20 Allan Cornish /usr/src/sys/coh/printf.c
13: * %U now correctly displays in base 10 rather than base 16.
14: *
15: * 86/12/16 Allan Cornish /usr/src/sys/coh/printf.c
16: * Added '%D' and '%X options to printf().
17: */
18: #include <sys/coherent.h>
19:
20: /*
21: * For indirecting and incrementing argument pointer.
22: */
23: #define ind(p, t) (*((t *) p))
24: #define inc(t1, t2) ((sizeof(t2 *)+sizeof(t1)-1) / sizeof(t1))
25:
26: /*
27: * Table for printing out digits.
28: */
29: char digtab[] ={
30: '0', '1', '2', '3', '4', '5', '6', '7',
31: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
32: };
33:
34: /*
35: * A simple printf.
36: */
37: printf(fp, a1)
38: register char *fp;
39: {
40: char * cp;
41: register int c;
42: register unsigned *ap;
43: int lflag;
44:
45: ap = (char *)&a1;
46: for (;;) {
47: while ((c=*fp++) != '%') {
48: if (c == '\0')
49: return;
50: putchar(c);
51: }
52:
53: lflag = 0;
54: if ( *fp == 'l' ) {
55: lflag = 1;
56: fp++;
57: }
58:
59: switch ( c = *fp++ ) {
60:
61: case 'c':
62: putchar(*ap++);
63: continue;
64:
65: case 'd':
66: if ( lflag == 0 ) {
67: if ( ((int)(*ap)) < 0 ) {
68: putchar('-');
69: printn( -((long) ((int)(*ap))), 10 );
70: }
71: else
72: printn( ((long)(*ap)), 10 );
73: ap++;
74: continue;
75: }
76: /* fall through */
77: case 'D':
78: if ( *((long *)(ap)) < 0 ) {
79: putchar('-');
80: printn( - *((long *)(ap)), 10 );
81: }
82: else
83: printn( *((long *)(ap)), 10 );
84:
85: ((long *)(ap))++;
86: continue;
87:
88: case 'o':
89: if ( lflag == 0 ) {
90: printn( ((long)(*ap)), 8);
91: ap++;
92: continue;
93: }
94: /* fall through */
95: case 'O':
96: printf( *((long *)(ap)), 8 );
97: ((long *)(ap))++;
98: continue;
99:
100: case 'r':
101: ap = *((int **) ap);
102: fp = ind(ap, char *);
103: ap += inc(int, char *);
104: continue;
105:
106: case 's':
107: cp = ind(ap, char *);
108: ap += inc(int, char *);
109: while ((c=*cp++) != '\0')
110: putchar(c);
111: continue;
112:
113: case 'x':
114: if ( lflag == 0 ) {
115: printn( ((long)(*ap)), 16 );
116: ap++;
117: continue;
118: }
119: /* fall through */
120: case 'X':
121: printn( *((long *)(ap)), 16 );
122: ((long *)(ap))++;
123: continue;
124:
125: case 'u':
126: if ( lflag == 0 ) {
127: printn( ((long)(*ap)), 10);
128: ap++;
129: continue;
130: }
131: /* fall through */
132: case 'U':
133: printn( *((long *)(ap)), 10 );
134: ((long *)(ap))++;
135: continue;
136:
137: case 'p':
138: if (sizeof(char *) > sizeof(int)) {
139: printn( ((long)(*ap)), 16);
140: putchar(':');
141: ap++;
142: }
143: printn( ((long)(*ap)), 16);
144: ap++;
145: continue;
146:
147: default:
148: putchar(c);
149: continue;
150: }
151: }
152: }
153:
154: /*
155: * Print out the unsigned long `v' in the base `b'.
156: */
157: printn( v, b )
158: unsigned long v;
159: {
160: unsigned long n;
161:
162: if ((n=v/b) != 0)
163: printn(n, b);
164:
165: putchar(digtab[v%b]);
166: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.