-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdoprnt.c
167 lines (153 loc) · 2.58 KB
/
fdoprnt.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <ctype.h>
/*
* doprnt for 8086
*/
static uchar ival;
static char * x;
static FILE * ffile;
extern int atoi(char *);
extern int strlen(char *);
static
pputc(c)
int c;
{
putc(c, ffile);
}
static char *
icvt(cp)
register char * cp;
{
ival = atoi(cp);
while(isdigit((unsigned)*cp))
cp++;
return cp;
}
_doprnt(file, f, a)
FILE * file;
register char * f;
int * a;
{
char c, prec;
uchar fill, left;
unsigned int i, len;
uchar base, width, sign;
uchar ftype;
extern short _pnum(), _fnum();
ffile = file;
while(c = *f++)
if(c != '%')
pputc(c);
else {
base = 10;
width = 0;
sign = 0;
left = 0;
ftype = 0;
len = sizeof(int)/sizeof *a;
if(*f == '-') {
f++;
left++;
}
fill = *f == '0';
if(isdigit((unsigned)*f)) {
f = icvt(f);
width = ival;
} else if(*f == '*') {
width = *a++;
f++;
}
if(*f == '.')
if(*++f == '*') {
prec = *a++;
f++;
} else {
f = icvt(f);
prec = ival;
}
else
prec = fill ? width : -1;
if(*f == 'l') {
f++;
len = sizeof(long)/sizeof *a;
}
switch(c = *f++) {
case 0:
return;
case 'o':
case 'O':
base = 8;
break;
case 'd':
case 'D':
sign = 1;
break;
case 'x':
case 'X':
base = 16;
break;
case 's':
x = *(char **)a;
a += sizeof(char *)/sizeof *a;
if(!x)
x = "(null)";
i = strlen(x);
dostring:
if(prec < 0)
prec = 0;
if(prec && prec < i)
i = prec;
if(width > i)
width -= i;
else
width = 0;
if(!left)
while(width--)
pputc(' ');
while(i--)
pputc(*x++);
if(left)
while(width--)
pputc(' ');
continue;
case 'c':
c = *a++;
default:
x = &c;
i = 1;
goto dostring;
case 'u':
case 'U':
break;
case 'e':
case 'E':
sign++;
case 'g':
case 'G':
sign++;
case 'f':
case 'F':
if(prec < 0)
prec = 6;
ftype = 1;
break;
}
if(left) {
left = width;
width = 0;
}
if(isupper(c))
len = sizeof(long)/sizeof *a;
if(prec < 0)
prec = 0;
if(ftype) {
width = _fnum(*(double *)a, prec, width, sign, pputc);
a += sizeof(double)/sizeof(*a);
} else {
width = _pnum((len == sizeof(int)/sizeof *a ? (sign ? (long)*a : (unsigned long)*a) : *(long *)a), prec, width, sign, base, pputc);
a += len;
}
while(left-- > width)
pputc(' ');
}
}