]>
Commit | Line | Data |
---|---|---|
6f5cb60c | 1 | /*- |
2 | * Copyright (c) 1986, 1988, 1991, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * (c) UNIX System Laboratories, Inc. | |
5 | * All or some portions of this file are derived from material licensed | |
6 | * to the University of California by American Telephone and Telegraph | |
7 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
8 | * the permission of UNIX System Laboratories, Inc. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * 4. Neither the name of the University nor the names of its contributors | |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | * | |
34 | * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 | |
35 | */ | |
f7e3ed82 | 36 | #include "printf.h" |
f7e3ed82 | 37 | |
6f5cb60c | 38 | typedef unsigned char u_char; |
39 | typedef unsigned int u_int; | |
6f5cb60c | 40 | typedef unsigned long long u_quad_t; |
41 | typedef long long quad_t; | |
07bc72b8 | 42 | typedef unsigned long u_long; |
43 | typedef unsigned short u_short; | |
f7e3ed82 | 44 | typedef int ssize_t; |
07bc72b8 | 45 | /* |
46 | typedef uint32_t uintmax_t; | |
47 | typedef int32_t intmax_t; | |
48 | */ | |
f7e3ed82 | 49 | |
6f5cb60c | 50 | #define NBBY 8 /* number of bits in a byte */ |
f7e3ed82 | 51 | |
6f5cb60c | 52 | char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
53 | #define hex2ascii(hex) (hex2ascii_data[hex]) | |
6f5cb60c | 54 | #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z'))) |
6f5cb60c | 55 | |
56 | /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ | |
57 | #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) | |
58 | ||
59 | /* | |
60 | * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse | |
61 | * order; return an optional length and a pointer to the last character | |
62 | * written in the buffer (i.e., the first character of the string). | |
63 | * The buffer pointed to by `nbuf' must have length >= MAXNBUF. | |
64 | */ | |
65 | static char * | |
66 | ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) | |
67 | { | |
68 | char *p, c; | |
69 | ||
70 | p = nbuf; | |
71 | *p = '\0'; | |
72 | do { | |
73 | c = hex2ascii(num % base); | |
74 | *++p = upper ? toupper(c) : c; | |
75 | } while (num /= base); | |
76 | if (lenp) | |
77 | *lenp = p - nbuf; | |
78 | return (p); | |
79 | } | |
80 | ||
81 | /* | |
82 | * Scaled down version of printf(3). | |
83 | * | |
84 | * Two additional formats: | |
85 | * | |
86 | * The format %b is supported to decode error registers. | |
87 | * Its usage is: | |
88 | * | |
89 | * printf("reg=%b\n", regval, "*"); | |
90 | * | |
91 | * where is the output base expressed as a control character, e.g. | |
92 | * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, | |
93 | * the first of which gives the bit number to be inspected (origin 1), and | |
94 | * the next characters (up to a control character, i.e. a character <= 32), | |
95 | * give the name of the register. Thus: | |
96 | * | |
97 | * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); | |
98 | * | |
99 | * would produce output: | |
100 | * | |
101 | * reg=3 | |
102 | * | |
103 | * XXX: %D -- Hexdump, takes pointer and separator string: | |
104 | * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX | |
105 | * ("%*D", len, ptr, " " -> XX XX XX XX ... | |
106 | */ | |
107 | int | |
108 | kvsprintf(char const *fmt, void *arg, int radix, va_list ap) | |
109 | { | |
110 | #define PCHAR(c) {int cc=(c); *d++ = cc; retval++; } | |
111 | char nbuf[MAXNBUF]; | |
112 | char *d; | |
113 | const char *p, *percent, *q; | |
114 | u_char *up; | |
115 | int ch, n; | |
116 | uintmax_t num; | |
117 | int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; | |
118 | int cflag, hflag, jflag, tflag, zflag; | |
119 | int dwidth, upper; | |
120 | char padc; | |
121 | int stop = 0, retval = 0; | |
122 | ||
123 | num = 0; | |
124 | d = (char *) arg; | |
125 | ||
126 | if (fmt == NULL) | |
127 | fmt = "(fmt null)\n"; | |
128 | ||
129 | if (radix < 2 || radix > 36) | |
130 | radix = 10; | |
131 | ||
132 | for (;;) { | |
133 | padc = ' '; | |
134 | width = 0; | |
135 | while ((ch = (u_char)*fmt++) != '%' || stop) { | |
136 | PCHAR(ch); | |
137 | if (ch == '\0') | |
138 | return (retval); | |
139 | } | |
140 | percent = fmt - 1; | |
141 | qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; | |
142 | sign = 0; dot = 0; dwidth = 0; upper = 0; | |
143 | cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0; | |
144 | reswitch: switch (ch = (u_char)*fmt++) { | |
145 | case '.': | |
146 | dot = 1; | |
147 | goto reswitch; | |
148 | case '#': | |
149 | sharpflag = 1; | |
150 | goto reswitch; | |
151 | case '+': | |
152 | sign = 1; | |
153 | goto reswitch; | |
154 | case '-': | |
155 | ladjust = 1; | |
156 | goto reswitch; | |
157 | case '%': | |
158 | PCHAR(ch); | |
159 | break; | |
160 | case '*': | |
161 | if (!dot) { | |
162 | width = va_arg(ap, int); | |
163 | if (width < 0) { | |
164 | ladjust = !ladjust; | |
165 | width = -width; | |
166 | } | |
167 | } else { | |
168 | dwidth = va_arg(ap, int); | |
169 | } | |
170 | goto reswitch; | |
171 | case '0': | |
172 | if (!dot) { | |
173 | padc = '0'; | |
174 | goto reswitch; | |
175 | } | |
176 | case '1': case '2': case '3': case '4': | |
177 | case '5': case '6': case '7': case '8': case '9': | |
178 | for (n = 0;; ++fmt) { | |
179 | n = n * 10 + ch - '0'; | |
180 | ch = *fmt; | |
181 | if (ch < '0' || ch > '9') | |
182 | break; | |
183 | } | |
184 | if (dot) | |
185 | dwidth = n; | |
186 | else | |
187 | width = n; | |
188 | goto reswitch; | |
189 | case 'b': | |
190 | num = (u_int)va_arg(ap, int); | |
191 | p = va_arg(ap, char *); | |
192 | for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) | |
193 | PCHAR(*q--); | |
194 | ||
195 | if (num == 0) | |
196 | break; | |
197 | ||
198 | for (tmp = 0; *p;) { | |
199 | n = *p++; | |
200 | if (num & (1 << (n - 1))) { | |
201 | PCHAR(tmp ? ',' : '<'); | |
202 | for (; (n = *p) > ' '; ++p) | |
203 | PCHAR(n); | |
204 | tmp = 1; | |
205 | } else | |
206 | for (; *p > ' '; ++p) | |
207 | continue; | |
208 | } | |
209 | if (tmp) | |
210 | PCHAR('>'); | |
211 | break; | |
212 | case 'c': | |
213 | PCHAR(va_arg(ap, int)); | |
214 | break; | |
215 | case 'D': | |
216 | up = va_arg(ap, u_char *); | |
217 | p = va_arg(ap, char *); | |
218 | if (!width) | |
219 | width = 16; | |
220 | while(width--) { | |
221 | PCHAR(hex2ascii(*up >> 4)); | |
222 | PCHAR(hex2ascii(*up & 0x0f)); | |
223 | up++; | |
224 | if (width) | |
225 | for (q=p;*q;q++) | |
226 | PCHAR(*q); | |
227 | } | |
228 | break; | |
229 | case 'd': | |
230 | case 'i': | |
231 | base = 10; | |
232 | sign = 1; | |
233 | goto handle_sign; | |
234 | case 'h': | |
235 | if (hflag) { | |
236 | hflag = 0; | |
237 | cflag = 1; | |
238 | } else | |
239 | hflag = 1; | |
240 | goto reswitch; | |
241 | case 'j': | |
242 | jflag = 1; | |
243 | goto reswitch; | |
244 | case 'l': | |
245 | if (lflag) { | |
246 | lflag = 0; | |
247 | qflag = 1; | |
248 | } else | |
249 | lflag = 1; | |
250 | goto reswitch; | |
251 | case 'n': | |
252 | if (jflag) | |
253 | *(va_arg(ap, intmax_t *)) = retval; | |
254 | else if (qflag) | |
255 | *(va_arg(ap, quad_t *)) = retval; | |
256 | else if (lflag) | |
257 | *(va_arg(ap, long *)) = retval; | |
258 | else if (zflag) | |
259 | *(va_arg(ap, size_t *)) = retval; | |
260 | else if (hflag) | |
261 | *(va_arg(ap, short *)) = retval; | |
262 | else if (cflag) | |
263 | *(va_arg(ap, char *)) = retval; | |
264 | else | |
265 | *(va_arg(ap, int *)) = retval; | |
266 | break; | |
267 | case 'o': | |
268 | base = 8; | |
269 | goto handle_nosign; | |
270 | case 'p': | |
271 | base = 16; | |
272 | sharpflag = (width == 0); | |
273 | sign = 0; | |
274 | num = (uintptr_t)va_arg(ap, void *); | |
275 | goto number; | |
276 | case 'q': | |
277 | qflag = 1; | |
278 | goto reswitch; | |
279 | case 'r': | |
280 | base = radix; | |
281 | if (sign) | |
282 | goto handle_sign; | |
283 | goto handle_nosign; | |
284 | case 's': | |
285 | p = va_arg(ap, char *); | |
286 | if (p == NULL) | |
287 | p = "(null)"; | |
288 | if (!dot) | |
289 | n = strlen (p); | |
290 | else | |
291 | for (n = 0; n < dwidth && p[n]; n++) | |
292 | continue; | |
293 | ||
294 | width -= n; | |
295 | ||
296 | if (!ladjust && width > 0) | |
297 | while (width--) | |
298 | PCHAR(padc); | |
299 | while (n--) | |
300 | PCHAR(*p++); | |
301 | if (ladjust && width > 0) | |
302 | while (width--) | |
303 | PCHAR(padc); | |
304 | break; | |
305 | case 't': | |
306 | tflag = 1; | |
307 | goto reswitch; | |
308 | case 'u': | |
309 | base = 10; | |
310 | goto handle_nosign; | |
311 | case 'X': | |
312 | upper = 1; | |
313 | case 'x': | |
314 | base = 16; | |
315 | goto handle_nosign; | |
316 | case 'y': | |
317 | base = 16; | |
318 | sign = 1; | |
319 | goto handle_sign; | |
320 | case 'z': | |
321 | zflag = 1; | |
322 | goto reswitch; | |
323 | handle_nosign: | |
324 | sign = 0; | |
325 | if (jflag) | |
326 | num = va_arg(ap, uintmax_t); | |
327 | else if (qflag) | |
328 | num = va_arg(ap, u_quad_t); | |
329 | else if (tflag) | |
330 | num = va_arg(ap, ptrdiff_t); | |
331 | else if (lflag) | |
332 | num = va_arg(ap, u_long); | |
333 | else if (zflag) | |
334 | num = va_arg(ap, size_t); | |
335 | else if (hflag) | |
336 | num = (u_short)va_arg(ap, int); | |
337 | else if (cflag) | |
338 | num = (u_char)va_arg(ap, int); | |
339 | else | |
340 | num = va_arg(ap, u_int); | |
341 | goto number; | |
342 | handle_sign: | |
343 | if (jflag) | |
344 | num = va_arg(ap, intmax_t); | |
345 | else if (qflag) | |
346 | num = va_arg(ap, quad_t); | |
347 | else if (tflag) | |
348 | num = va_arg(ap, ptrdiff_t); | |
349 | else if (lflag) | |
350 | num = va_arg(ap, long); | |
351 | else if (zflag) | |
352 | num = va_arg(ap, ssize_t); | |
353 | else if (hflag) | |
354 | num = (short)va_arg(ap, int); | |
355 | else if (cflag) | |
356 | num = (char)va_arg(ap, int); | |
357 | else | |
358 | num = va_arg(ap, int); | |
359 | number: | |
360 | if (sign && (intmax_t)num < 0) { | |
361 | neg = 1; | |
362 | num = -(intmax_t)num; | |
363 | } | |
364 | p = ksprintn(nbuf, num, base, &tmp, upper); | |
365 | if (sharpflag && num != 0) { | |
366 | if (base == 8) | |
367 | tmp++; | |
368 | else if (base == 16) | |
369 | tmp += 2; | |
370 | } | |
371 | if (neg) | |
372 | tmp++; | |
373 | ||
374 | if (!ladjust && padc != '0' && width | |
375 | && (width -= tmp) > 0) | |
376 | while (width--) | |
377 | PCHAR(padc); | |
378 | if (neg) | |
379 | PCHAR('-'); | |
380 | if (sharpflag && num != 0) { | |
381 | if (base == 8) { | |
382 | PCHAR('0'); | |
383 | } else if (base == 16) { | |
384 | PCHAR('0'); | |
385 | PCHAR('x'); | |
386 | } | |
387 | } | |
388 | if (!ladjust && width && (width -= tmp) > 0) | |
389 | while (width--) | |
390 | PCHAR(padc); | |
391 | ||
392 | while (*p) | |
393 | PCHAR(*p--); | |
394 | ||
395 | if (ladjust && width && (width -= tmp) > 0) | |
396 | while (width--) | |
397 | PCHAR(padc); | |
398 | ||
399 | break; | |
400 | default: | |
401 | while (percent < fmt) | |
402 | PCHAR(*percent++); | |
403 | /* | |
404 | * Since we ignore an formatting argument it is no | |
405 | * longer safe to obey the remaining formatting | |
406 | * arguments as the arguments will no longer match | |
407 | * the format specs. | |
408 | */ | |
409 | stop = 1; | |
410 | break; | |
411 | } | |
412 | } | |
413 | PCHAR(0); | |
414 | return retval; | |
415 | #undef PCHAR | |
416 | } | |
417 | ||
f7e3ed82 | 418 | int vsprintf(char *dest, const char *fmt, va_list ap) |
419 | { | |
420 | return kvsprintf(fmt, dest, 10, ap); | |
421 | } | |
422 | ||
6f5cb60c | 423 | int |
424 | sprintf(char *dest, const char *fmt, ...) | |
425 | { | |
426 | /* http://www.pagetable.com/?p=298 */ | |
427 | int retval; | |
428 | va_list ap; | |
6f5cb60c | 429 | va_start(ap, fmt); |
430 | retval = kvsprintf(fmt, dest, 10, ap); | |
431 | va_end(ap); | |
432 | return retval; | |
433 | } |