Chameleon

Chameleon Commit Details

Date:2016-05-19 19:14:41 (3 years 27 days ago)
Author:zenith432
Commit:2820
Parents: 2819
Message:Add unsigned, long and long long support to prf()
Changes:
M/branches/zenith432/i386/libsa/prf.c

File differences

branches/zenith432/i386/libsa/prf.c
3838
3939
4040
41
41
4242
4343
4444
45
45
46
4647
4748
4849
......
5657
5758
5859
59
60
61
62
63
60
61
62
63
64
65
66
67
6468
65
69
6670
6771
6872
69
73
74
7075
71
76
7277
7378
74
75
79
80
81
7682
7783
7884
79
80
81
85
86
87
88
89
90
91
8292
8393
84
85
86
94
95
96
97
98
99
100
87101
88
102
89103
104
90105
91106
92107
93
94
95
108
109
110
96111
97112
98113
99
100
101
114
115
116
102117
103118
104119
105
120
121
106122
107123
108124
......
110126
111127
112128
113
129
114130
115131
116132
117133
134
135
118136
119137
120
138
139
121140
141
142
143
144
122145
123146
124147
......
149172
150173
151174
175
176
177
152178
153179
154180
155181
156182
157
183
158184
159
185
186
187
188
189
190
191
192
193
160194
161
162195
163196
164
165
197
198
199
200
166201
167202
168
203
169204
170205
171206
......
174209
175210
176211
177
212
178213
179214
180215
......
182217
183218
184219
185
220
186221
187222
188223
189224
190225
191226
192
193227
194228
*@(#)prf.c7.1 (Berkeley) 6/5/86
*/
#include <sys/param.h>
#include <stdarg.h>
#define SPACE1
#define ZERO2
#define UCASE 16
#define UCASE16
#define SIGNED32
/*
* Scaled down version of C Library printf.
* Printn prints a number n in base b.
* We don't use recursion to avoid deep kernel stacks.
*/
static void printn(n, b, flag, minwidth, putfn_p, putfn_arg)
u_long n;
int b, flag, minwidth;
void (*putfn_p)();
void *putfn_arg;
static int printn(
unsigned long long n,
int b,
int flag,
int minwidth,
int (*putfn_p)(),
void* putfn_arg
)
{
char prbuf[11];
char prbuf[22];
register char *cp;
int width = 0, neg = 0;
if (b == 10 && (int)n < 0) {
if ((flag & SIGNED) && (long long)n < 0)
{
neg = 1;
n = (unsigned)(-(int)n);
n = (unsigned long long)(-(long long)n);
}
cp = prbuf;
do {
*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + n%b];
do
{
*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + (int) (n%b)];
n /= b;
width++;
} while (n);
if (neg) {
(*putfn_p)('-', putfn_arg);
if (neg)
{
if (putfn_p)
{
(void)(*putfn_p)('-', putfn_arg);
}
width++;
}
while (width++ < minwidth)
(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);
if (!putfn_p)
{
return (width < minwidth) ? minwidth : width;
}
for (;width < minwidth; width++)
(void)(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);
do
(*putfn_p)(*--cp, putfn_arg);
(void)(*putfn_p)(*--cp, putfn_arg);
while (cp > prbuf);
return width;
}
int prf(
char *fmt,
unsigned int *adx,
void (*putfn_p)(),
const char *fmt,
va_list ap,
int (*putfn_p)(),
void *putfn_arg
)
{
int b, c, len =0;
char *s;
int flag = 0, width = 0;
int b, c, len = 0;
const char *s;
int flag, width, ells;
int minwidth;
loop:
while ((c = *fmt++) != '%') {
while ((c = *fmt++) != '%')
{
if(c == '\0')
{
return len;
if (putfn_p)
{
(*putfn_p)(c, putfn_arg);
(void)(*putfn_p)(c, putfn_arg);
}
len++;
}
minwidth = 0;
flag = 0;
ells = 0;
again:
c = *fmt++;
switch (c) {
switch (c)
{
case 'l':
if (ells < 2)
{
++ells;
}
goto again;
case ' ':
flag |= SPACE;
b = 16;
goto number;
case 'd':
flag |= SIGNED;
/* fall through */
case 'u':
b = 10;
goto number;
case 'o': case 'O':
b = 8;
number:
if (putfn_p)
switch (ells)
{
printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg);
case 2:
len += printn(va_arg(ap, unsigned long long), b, flag, minwidth, putfn_p, putfn_arg);
break;
case 1:
len += printn(va_arg(ap, unsigned long), b, flag, minwidth, putfn_p, putfn_arg);
break;
default:
len += printn(va_arg(ap, unsigned int), b, flag, minwidth, putfn_p, putfn_arg);
break;
}
len++;
break;
case 's':
s = (char *)*adx;
while ((c = *s++)) {
s = va_arg(ap, const char*);
width = 0;
while (s && (c = *s++))
{
if (putfn_p)
{
(*putfn_p)(c, putfn_arg);
(void)(*putfn_p)(c, putfn_arg);
}
len++;
width++;
{
if (putfn_p)
{
(*putfn_p)(' ', putfn_arg);
(void)(*putfn_p)(' ', putfn_arg);
}
len++;
}
case 'c':
if (putfn_p)
{
(*putfn_p)((char)*adx, putfn_arg);
(void)(*putfn_p)((char) va_arg(ap, int), putfn_arg);
}
len++;
break;
default:
break;
}
adx++;
goto loop;
}

Archive Download the corresponding diff file

Revision: 2820