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