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