-
Notifications
You must be signed in to change notification settings - Fork 0
/
prf.c
117 lines (94 loc) · 1.57 KB
/
prf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* PIECES COMMENTED OUT TO SAVE SPACE */
#include "compat.h"
/*VARARGS1*/
printf(fmt, x1)
char *fmt;
unsigned x1;
{
extern int showchar();
doprf(fmt, &x1, showchar);
}
/*
* Scaled down version of C Library printf.
*/
doprf(fmt, adx, pfunc)
register char *fmt;
register int *adx;
int (*pfunc)();
{
register int b, c, i, len;
char *s;
int zfill;
int any;
long number;
loop:
while ((c = *fmt++) != '%') {
if(c == '\0')
return;
(*pfunc)(p1(c));
}
len = 0;
zfill = 0;
again:
c = *fmt++;
switch (c) {
case 'r':
adx = *(int **)(adx);
fmt = *(char **)(adx)++;
break;
case 'D':
case 'd':
if (c == 'D')
number = *(long *)(adx)++;
else
number = *(int *)(adx)++;
b = 10;
printn(number, b, len, zfill, pfunc);
break;
case 's':
s = *(char **)(adx)++;
while (c = *s++)
(*pfunc)(p1(c));
break;
case '0':
if ( len==0 ) zfill = 1;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
len = len*10 + (c-'0');
goto again;
case '%':
(*pfunc)(p1('%'));
break;
}
goto loop;
}
/*
* Printn prints a number n in base b.
*/
printn(n, b, len, zfill, pfunc)
register unsigned long n;
register int len;
int (*pfunc)();
{
char prbuf[11];
register char *cp;
int ch;
if (b == 10 && (int)n < 0) {
(*pfunc)(p1('-'));
n = (unsigned)(-(int)n);
}
cp = prbuf;
do {
*cp++ = "0123456789abcdef"[n%b];
n /= b;
} while (n);
while ( len-- > cp-prbuf )
if ( zfill )
(*pfunc)(p1('0'));
else
(*pfunc)(p1(' '));
do {
ch = *--cp;
(*pfunc)(p1(ch));
} while (cp > prbuf);
}