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