]>
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 | */ | |
36 | ||
f7e3ed82 | 37 | #include <stddef.h> |
38 | #include <stdarg.h> | |
39 | #include "printf.h" | |
40 | #include "util.h" | |
9ab7a6c7 | 41 | #include "string.h" |
f7e3ed82 | 42 | |
6f5cb60c | 43 | typedef unsigned char u_char; |
44 | typedef unsigned int u_int; | |
45 | typedef unsigned long u_long; | |
46 | typedef unsigned short u_short; | |
47 | typedef unsigned long long u_quad_t; | |
48 | typedef long long quad_t; | |
f7e3ed82 | 49 | |
50 | typedef int ssize_t; | |
51 | ||
6f5cb60c | 52 | #define NBBY 8 /* number of bits in a byte */ |
f7e3ed82 | 53 | |
6f5cb60c | 54 | char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
55 | #define hex2ascii(hex) (hex2ascii_data[hex]) | |
6f5cb60c | 56 | #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z'))) |
6f5cb60c | 57 | |
58 | /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ | |
59 | #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) | |
60 | ||
61 | /* | |
62 | * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse | |
63 | * order; return an optional length and a pointer to the last character | |
64 | * written in the buffer (i.e., the first character of the string). | |
65 | * The buffer pointed to by `nbuf' must have length >= MAXNBUF. | |
66 | */ | |
67 | static char * | |
68 | ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) | |
69 | { | |
70 | char *p, c; | |
71 | ||
72 | p = nbuf; | |
73 | *p = '\0'; | |
74 | do { | |
75 | c = hex2ascii(num % base); | |
76 | *++p = upper ? toupper(c) : c; | |
77 | } while (num /= base); | |
78 | if (lenp) | |
79 | *lenp = p - nbuf; | |
80 | return (p); | |
81 | } | |
82 | ||
83 | /* | |
84 | * Scaled down version of printf(3). | |
85 | * | |
86 | * Two additional formats: | |
87 | * | |
88 | * The format %b is supported to decode error registers. | |
89 | * Its usage is: | |
90 | * | |
91 | * printf("reg=%b\n", regval, "*"); | |
92 | * | |
93 | * where is the output base expressed as a control character, e.g. | |
94 | * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, | |
95 | * the first of which gives the bit number to be inspected (origin 1), and | |
96 | * the next characters (up to a control character, i.e. a character <= 32), | |
97 | * give the name of the register. Thus: | |
98 | * | |
99 | * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); | |
100 | * | |
101 | * would produce output: | |
102 | * | |
103 | * reg=3 | |
104 | * | |
105 | * XXX: %D -- Hexdump, takes pointer and separator string: | |
106 | * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX | |
107 | * ("%*D", len, ptr, " " -> XX XX XX XX ... | |
108 | */ | |
109 | int | |
110 | kvsprintf(char const *fmt, void *arg, int radix, va_list ap) | |
111 | { | |
112 | #define PCHAR(c) {int cc=(c); *d++ = cc; retval++; } | |
113 | char nbuf[MAXNBUF]; | |
114 | char *d; | |
115 | const char *p, *percent, *q; | |
116 | u_char *up; | |
117 | int ch, n; | |
118 | uintmax_t num; | |
119 | int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; | |
120 | int cflag, hflag, jflag, tflag, zflag; | |
121 | int dwidth, upper; | |
122 | char padc; | |
123 | int stop = 0, retval = 0; | |
124 | ||
125 | num = 0; | |
126 | d = (char *) arg; | |
127 | ||
128 | if (fmt == NULL) | |
129 | fmt = "(fmt null)\n"; | |
130 | ||
131 | if (radix < 2 || radix > 36) | |
132 | radix = 10; | |
133 | ||
134 | for (;;) { | |
135 | padc = ' '; | |
136 | width = 0; | |
137 | while ((ch = (u_char)*fmt++) != '%' || stop) { | |
138 | PCHAR(ch); | |
139 | if (ch == '\0') | |
140 | return (retval); | |
141 | } | |
142 | percent = fmt - 1; | |
143 | qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; | |
144 | sign = 0; dot = 0; dwidth = 0; upper = 0; | |
145 | cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0; | |
146 | reswitch: switch (ch = (u_char)*fmt++) { | |
147 | case '.': | |
148 | dot = 1; | |
149 | goto reswitch; | |
150 | case '#': | |
151 | sharpflag = 1; | |
152 | goto reswitch; | |
153 | case '+': | |
154 | sign = 1; | |
155 | goto reswitch; | |
156 | case '-': | |
157 | ladjust = 1; | |
158 | goto reswitch; | |
159 | case '%': | |
160 | PCHAR(ch); | |
161 | break; | |
162 | case '*': | |
163 | if (!dot) { | |
164 | width = va_arg(ap, int); | |
165 | if (width < 0) { | |
166 | ladjust = !ladjust; | |
167 | width = -width; | |
168 | } | |
169 | } else { | |
170 | dwidth = va_arg(ap, int); | |
171 | } | |
172 | goto reswitch; | |
173 | case '0': | |
174 | if (!dot) { | |
175 | padc = '0'; | |
176 | goto reswitch; | |
177 | } | |
44964fd1 | 178 | // intentionally fall through to next case |
6f5cb60c | 179 | case '1': case '2': case '3': case '4': |
180 | case '5': case '6': case '7': case '8': case '9': | |
181 | for (n = 0;; ++fmt) { | |
182 | n = n * 10 + ch - '0'; | |
183 | ch = *fmt; | |
184 | if (ch < '0' || ch > '9') | |
185 | break; | |
186 | } | |
187 | if (dot) | |
188 | dwidth = n; | |
189 | else | |
190 | width = n; | |
191 | goto reswitch; | |
192 | case 'b': | |
193 | num = (u_int)va_arg(ap, int); | |
194 | p = va_arg(ap, char *); | |
195 | for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) | |
196 | PCHAR(*q--); | |
197 | ||
198 | if (num == 0) | |
199 | break; | |
200 | ||
201 | for (tmp = 0; *p;) { | |
202 | n = *p++; | |
203 | if (num & (1 << (n - 1))) { | |
204 | PCHAR(tmp ? ',' : '<'); | |
205 | for (; (n = *p) > ' '; ++p) | |
206 | PCHAR(n); | |
207 | tmp = 1; | |
208 | } else | |
209 | for (; *p > ' '; ++p) | |
210 | continue; | |
211 | } | |
212 | if (tmp) | |
213 | PCHAR('>'); | |
214 | break; | |
215 | case 'c': | |
216 | PCHAR(va_arg(ap, int)); | |
217 | break; | |
218 | case 'D': | |
219 | up = va_arg(ap, u_char *); | |
220 | p = va_arg(ap, char *); | |
221 | if (!width) | |
222 | width = 16; | |
223 | while(width--) { | |
224 | PCHAR(hex2ascii(*up >> 4)); | |
225 | PCHAR(hex2ascii(*up & 0x0f)); | |
226 | up++; | |
227 | if (width) | |
228 | for (q=p;*q;q++) | |
229 | PCHAR(*q); | |
230 | } | |
231 | break; | |
232 | case 'd': | |
233 | case 'i': | |
234 | base = 10; | |
235 | sign = 1; | |
236 | goto handle_sign; | |
237 | case 'h': | |
238 | if (hflag) { | |
239 | hflag = 0; | |
240 | cflag = 1; | |
241 | } else | |
242 | hflag = 1; | |
243 | goto reswitch; | |
244 | case 'j': | |
245 | jflag = 1; | |
246 | goto reswitch; | |
247 | case 'l': | |
248 | if (lflag) { | |
249 | lflag = 0; | |
250 | qflag = 1; | |
251 | } else | |
252 | lflag = 1; | |
253 | goto reswitch; | |
254 | case 'n': | |
255 | if (jflag) | |
256 | *(va_arg(ap, intmax_t *)) = retval; | |
257 | else if (qflag) | |
258 | *(va_arg(ap, quad_t *)) = retval; | |
259 | else if (lflag) | |
260 | *(va_arg(ap, long *)) = retval; | |
261 | else if (zflag) | |
262 | *(va_arg(ap, size_t *)) = retval; | |
263 | else if (hflag) | |
264 | *(va_arg(ap, short *)) = retval; | |
265 | else if (cflag) | |
266 | *(va_arg(ap, char *)) = retval; | |
267 | else | |
268 | *(va_arg(ap, int *)) = retval; | |
269 | break; | |
270 | case 'o': | |
271 | base = 8; | |
272 | goto handle_nosign; | |
273 | case 'p': | |
274 | base = 16; | |
275 | sharpflag = (width == 0); | |
276 | sign = 0; | |
277 | num = (uintptr_t)va_arg(ap, void *); | |
278 | goto number; | |
279 | case 'q': | |
280 | qflag = 1; | |
281 | goto reswitch; | |
282 | case 'r': | |
283 | base = radix; | |
284 | if (sign) | |
285 | goto handle_sign; | |
286 | goto handle_nosign; | |
287 | case 's': | |
288 | p = va_arg(ap, char *); | |
289 | if (p == NULL) | |
290 | p = "(null)"; | |
291 | if (!dot) | |
292 | n = strlen (p); | |
293 | else | |
294 | for (n = 0; n < dwidth && p[n]; n++) | |
295 | continue; | |
296 | ||
297 | width -= n; | |
298 | ||
299 | if (!ladjust && width > 0) | |
300 | while (width--) | |
301 | PCHAR(padc); | |
302 | while (n--) | |
303 | PCHAR(*p++); | |
304 | if (ladjust && width > 0) | |
305 | while (width--) | |
306 | PCHAR(padc); | |
307 | break; | |
308 | case 't': | |
309 | tflag = 1; | |
310 | goto reswitch; | |
311 | case 'u': | |
312 | base = 10; | |
313 | goto handle_nosign; | |
314 | case 'X': | |
315 | upper = 1; | |
316 | case 'x': | |
317 | base = 16; | |
318 | goto handle_nosign; | |
319 | case 'y': | |
320 | base = 16; | |
321 | sign = 1; | |
322 | goto handle_sign; | |
323 | case 'z': | |
324 | zflag = 1; | |
325 | goto reswitch; | |
326 | handle_nosign: | |
327 | sign = 0; | |
328 | if (jflag) | |
329 | num = va_arg(ap, uintmax_t); | |
330 | else if (qflag) | |
331 | num = va_arg(ap, u_quad_t); | |
332 | else if (tflag) | |
333 | num = va_arg(ap, ptrdiff_t); | |
334 | else if (lflag) | |
335 | num = va_arg(ap, u_long); | |
336 | else if (zflag) | |
337 | num = va_arg(ap, size_t); | |
338 | else if (hflag) | |
339 | num = (u_short)va_arg(ap, int); | |
340 | else if (cflag) | |
341 | num = (u_char)va_arg(ap, int); | |
342 | else | |
343 | num = va_arg(ap, u_int); | |
344 | goto number; | |
345 | handle_sign: | |
346 | if (jflag) | |
347 | num = va_arg(ap, intmax_t); | |
348 | else if (qflag) | |
349 | num = va_arg(ap, quad_t); | |
350 | else if (tflag) | |
351 | num = va_arg(ap, ptrdiff_t); | |
352 | else if (lflag) | |
353 | num = va_arg(ap, long); | |
354 | else if (zflag) | |
355 | num = va_arg(ap, ssize_t); | |
356 | else if (hflag) | |
357 | num = (short)va_arg(ap, int); | |
358 | else if (cflag) | |
359 | num = (char)va_arg(ap, int); | |
360 | else | |
361 | num = va_arg(ap, int); | |
362 | number: | |
363 | if (sign && (intmax_t)num < 0) { | |
364 | neg = 1; | |
365 | num = -(intmax_t)num; | |
366 | } | |
367 | p = ksprintn(nbuf, num, base, &tmp, upper); | |
368 | if (sharpflag && num != 0) { | |
369 | if (base == 8) | |
370 | tmp++; | |
371 | else if (base == 16) | |
372 | tmp += 2; | |
373 | } | |
374 | if (neg) | |
375 | tmp++; | |
376 | ||
377 | if (!ladjust && padc != '0' && width | |
378 | && (width -= tmp) > 0) | |
379 | while (width--) | |
380 | PCHAR(padc); | |
381 | if (neg) | |
382 | PCHAR('-'); | |
383 | if (sharpflag && num != 0) { | |
384 | if (base == 8) { | |
385 | PCHAR('0'); | |
386 | } else if (base == 16) { | |
387 | PCHAR('0'); | |
388 | PCHAR('x'); | |
389 | } | |
390 | } | |
391 | if (!ladjust && width && (width -= tmp) > 0) | |
392 | while (width--) | |
393 | PCHAR(padc); | |
394 | ||
395 | while (*p) | |
396 | PCHAR(*p--); | |
397 | ||
398 | if (ladjust && width && (width -= tmp) > 0) | |
399 | while (width--) | |
400 | PCHAR(padc); | |
401 | ||
402 | break; | |
403 | default: | |
404 | while (percent < fmt) | |
405 | PCHAR(*percent++); | |
406 | /* | |
407 | * Since we ignore an formatting argument it is no | |
408 | * longer safe to obey the remaining formatting | |
409 | * arguments as the arguments will no longer match | |
410 | * the format specs. | |
411 | */ | |
412 | stop = 1; | |
413 | break; | |
414 | } | |
415 | } | |
416 | PCHAR(0); | |
417 | return retval; | |
418 | #undef PCHAR | |
419 | } | |
420 | ||
f7e3ed82 | 421 | int vsprintf(char *dest, const char *fmt, va_list ap) |
422 | { | |
423 | return kvsprintf(fmt, dest, 10, ap); | |
424 | } | |
425 | ||
6f5cb60c | 426 | int |
427 | sprintf(char *dest, const char *fmt, ...) | |
428 | { | |
429 | /* http://www.pagetable.com/?p=298 */ | |
430 | int retval; | |
431 | va_list ap; | |
432 | ||
433 | va_start(ap, fmt); | |
434 | retval = kvsprintf(fmt, dest, 10, ap); | |
435 | va_end(ap); | |
436 | return retval; | |
437 | } |