1 /* $NetBSD: vsnprintf_ss.c,v 1.8 2009/10/25 20:44:13 christos Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: vsnprintf_ss.c,v 1.8 2009/10/25 20:44:13 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
46 #include <sys/types.h>
53 #include "reentrant.h"
58 __weak_alias(vsnprintf_ss
,_vsnprintf_ss
)
62 * vsnprintf_ss: scaled down version of printf(3).
64 * this version based on vfprintf() from libc which was derived from
65 * software contributed to Berkeley by Chris Torek.
70 * macros for converting digits to letters and vice versa
72 #define to_digit(c) ((c) - '0')
73 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
74 #define to_char(n) (char)((n) + '0')
77 * flags used during conversion.
79 #define ALT 0x001 /* alternate form */
80 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
81 #define LADJUST 0x004 /* left adjustment */
82 #define LONGDBL 0x008 /* long double; unimplemented */
83 #define LONGINT 0x010 /* long integer */
84 #define QUADINT 0x020 /* quad integer */
85 #define SHORTINT 0x040 /* short integer */
86 #define MAXINT 0x080 /* intmax_t */
87 #define PTRINT 0x100 /* intptr_t */
88 #define SIZEINT 0x200 /* size_t */
89 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
90 #define FPT 0x800 /* Floating point number */
93 * To extend shorts properly, we need both signed and unsigned
94 * argument extraction methods.
97 (flags&MAXINT ? va_arg(ap, intmax_t) : \
98 flags&PTRINT ? va_arg(ap, intptr_t) : \
99 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
100 flags&QUADINT ? va_arg(ap, quad_t) : \
101 flags&LONGINT ? va_arg(ap, long) : \
102 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
103 (long)va_arg(ap, int))
105 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
106 flags&PTRINT ? va_arg(ap, uintptr_t) : \
107 flags&SIZEINT ? va_arg(ap, size_t) : \
108 flags&QUADINT ? va_arg(ap, u_quad_t) : \
109 flags&LONGINT ? va_arg(ap, u_long) : \
110 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
111 (u_long)va_arg(ap, u_int))
113 #define PUTCHAR(C) do { \
116 } while (/*CONSTCOND*/0)
119 vsnprintf_ss(char *sbuf
, size_t slen
, const char *fmt0
, _BSD_VA_LIST_ ap
)
121 const char *fmt
; /* format string */
122 int ch
; /* character from fmt */
123 int n
; /* handy integer (short term usage) */
124 char *cp
; /* handy char pointer (short term usage) */
125 int flags
; /* flags as above */
126 int ret
; /* return value accumulator */
127 int width
; /* width from format (%8d), or 0 */
128 int prec
; /* precision from format (%.3d), or -1 */
129 char sign
; /* sign prefix (' ', '+', '-', or \0) */
131 u_quad_t _uquad
; /* integer arguments %[diouxX] */
132 enum { OCT
, DEC
, HEX
} base
;/* base for [diouxX] conversion */
133 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
134 int realsz
; /* field size expanded by dprec */
135 int size
; /* size of converted field or string */
136 const char *xdigs
; /* digits for [xX] conversion */
137 char bf
[128]; /* space for %c, %[diouxX] */
138 char *tailp
; /* tail pointer for snprintf */
140 static const char xdigs_lower
[16] = "0123456789abcdef";
141 static const char xdigs_upper
[16] = "0123456789ABCDEF";
143 _DIAGASSERT(slen
== 0 || sbuf
!= NULL
);
144 _DIAGASSERT(fmt0
!= NULL
);
153 cp
= NULL
; /* XXX: shutup gcc */
154 size
= 0; /* XXX: shutup gcc */
159 xdigs
= NULL
; /* XXX: shut up gcc warning */
162 * Scan the format for conversions (`%' character).
165 while (*fmt
!= '%' && *fmt
) {
173 fmt
++; /* skip over '%' */
182 reswitch
: switch (ch
) {
185 * ``If the space and + flags both appear, the space
186 * flag will be ignored.''
197 * ``A negative field width argument is taken as a
198 * - flag followed by a positive field width.''
200 * They don't exclude field widths read from args.
202 if ((width
= va_arg(ap
, int)) >= 0)
213 if ((ch
= *fmt
++) == '*') {
215 prec
= n
< 0 ? -1 : n
;
219 while (is_digit(ch
)) {
220 n
= 10 * n
+ to_digit(ch
);
223 prec
= n
< 0 ? -1 : n
;
227 * ``Note that 0 is taken as a flag, not as the
228 * beginning of a field width.''
233 case '1': case '2': case '3': case '4':
234 case '5': case '6': case '7': case '8': case '9':
237 n
= 10 * n
+ to_digit(ch
);
239 } while (is_digit(ch
));
266 *(cp
= bf
) = va_arg(ap
, int);
276 if ((quad_t
)_uquad
< 0) {
284 *va_arg(ap
, intmax_t *) = ret
;
285 else if (flags
& PTRINT
)
286 *va_arg(ap
, intptr_t *) = ret
;
287 else if (flags
& SIZEINT
)
288 *va_arg(ap
, ssize_t
*) = ret
;
289 else if (flags
& QUADINT
)
290 *va_arg(ap
, quad_t
*) = ret
;
291 else if (flags
& LONGINT
)
292 *va_arg(ap
, long *) = ret
;
293 else if (flags
& SHORTINT
)
294 *va_arg(ap
, short *) = ret
;
296 *va_arg(ap
, int *) = ret
;
297 continue; /* no output */
307 * ``The argument shall be a pointer to void. The
308 * value of the pointer is converted to a sequence
309 * of printable characters, in an implementation-
314 _uquad
= (u_long
)va_arg(ap
, void *);
321 if ((cp
= va_arg(ap
, char *)) == NULL
)
323 cp
= __UNCONST("(null)");
326 * can't use strlen; can only look for the
327 * NUL in the first `prec' characters, and
328 * strlen() will go further.
330 char *p
= memchr(cp
, 0, (size_t)prec
);
354 hex
: _uquad
= UARG();
356 /* leading 0x/X only if non-zero */
357 if (flags
& ALT
&& _uquad
!= 0)
360 /* unsigned conversions */
363 * ``... diouXx conversions ... if a precision is
364 * specified, the 0 flag will be ignored.''
367 number
: if ((dprec
= prec
) >= 0)
371 * ``The result of converting a zero value with an
372 * explicit precision of zero is no characters.''
375 cp
= bf
+ sizeof(bf
);
376 if (_uquad
!= 0 || prec
!= 0) {
378 * Unsigned mod is hard, and unsigned mod
379 * by a constant is easier than that by
380 * a variable; hence this switch.
385 *--cp
= to_char(_uquad
& 7);
388 /* handle octal leading 0 */
389 if (flags
& ALT
&& *cp
!= '0')
394 /* many numbers are 1 digit */
395 while (_uquad
>= 10) {
396 *--cp
= to_char(_uquad
% 10);
399 *--cp
= to_char(_uquad
);
404 *--cp
= xdigs
[(size_t)_uquad
& 15];
411 cp
= __UNCONST("bug bad base");
416 size
= bf
+ sizeof(bf
) - cp
;
419 default: /* "%?" prints ?, unless ? is NUL */
422 /* pretend it was %c with argument ch */
431 * All reasonable formats wind up here. At this point, `cp'
432 * points to a string which (if not flags&LADJUST) should be
433 * padded out to `width' places. If flags&ZEROPAD, it should
434 * first be prefixed by any sign or other prefix; otherwise,
435 * it should be blank padded before the prefix is emitted.
436 * After any left-hand padding and prefixing, emit zeroes
437 * required by a decimal [diouxX] precision, then print the
438 * string proper, then emit zeroes required by any leftover
439 * floating precision; finally, if LADJUST, pad with blanks.
441 * Compute actual size, so we know how much to pad.
442 * size excludes decimal prec; realsz includes it.
444 realsz
= dprec
> size
? dprec
: size
;
447 else if (flags
& HEXPREFIX
)
451 ret
+= width
> realsz
? width
: realsz
;
453 /* right-adjusting blank padding */
454 if ((flags
& (LADJUST
|ZEROPAD
)) == 0) {
463 } else if (flags
& HEXPREFIX
) {
468 /* right-adjusting zero padding */
469 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
) {
475 /* leading zeroes from decimal precision */
480 /* the string or number proper */
483 /* left-adjusting padding (always blank) */
484 if (flags
& LADJUST
) {