1 /* $NetBSD: vsnprintf_ss.c,v 1.13 2014/09/29 14:58:33 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.13 2014/09/29 14:58:33 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
, 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 */
141 static const char xdigs_lower
[16] = "0123456789abcdef";
142 static const char xdigs_upper
[16] = "0123456789ABCDEF";
144 _DIAGASSERT(slen
== 0 || sbuf
!= NULL
);
145 _DIAGASSERT(fmt0
!= NULL
);
147 if (slen
> INT_MAX
) {
154 cp
= NULL
; /* XXX: shutup gcc */
155 size
= 0; /* XXX: shutup gcc */
160 xdigs
= NULL
; /* XXX: shut up gcc warning */
163 * Scan the format for conversions (`%' character).
166 while (*fmt
!= '%' && *fmt
) {
174 fmt
++; /* skip over '%' */
183 reswitch
: switch (ch
) {
186 * ``If the space and + flags both appear, the space
187 * flag will be ignored.''
198 * ``A negative field width argument is taken as a
199 * - flag followed by a positive field width.''
201 * They don't exclude field widths read from args.
203 if ((width
= va_arg(ap
, int)) >= 0)
214 if ((ch
= *fmt
++) == '*') {
216 prec
= n
< 0 ? -1 : n
;
220 while (is_digit(ch
)) {
221 n
= 10 * n
+ to_digit(ch
);
224 prec
= n
< 0 ? -1 : n
;
228 * ``Note that 0 is taken as a flag, not as the
229 * beginning of a field width.''
234 case '1': case '2': case '3': case '4':
235 case '5': case '6': case '7': case '8': case '9':
238 n
= 10 * n
+ to_digit(ch
);
240 } while (is_digit(ch
));
267 *(cp
= bf
) = va_arg(ap
, int);
277 if ((quad_t
)_uquad
< 0) {
285 *va_arg(ap
, intmax_t *) = ret
;
286 else if (flags
& PTRINT
)
287 *va_arg(ap
, intptr_t *) = ret
;
288 else if (flags
& SIZEINT
)
289 *va_arg(ap
, ssize_t
*) = ret
;
290 else if (flags
& QUADINT
)
291 *va_arg(ap
, quad_t
*) = ret
;
292 else if (flags
& LONGINT
)
293 *va_arg(ap
, long *) = ret
;
294 else if (flags
& SHORTINT
)
295 *va_arg(ap
, short *) = ret
;
297 *va_arg(ap
, int *) = ret
;
298 continue; /* no output */
308 * ``The argument shall be a pointer to void. The
309 * value of the pointer is converted to a sequence
310 * of printable characters, in an implementation-
315 _uquad
= (u_long
)va_arg(ap
, void *);
322 if ((cp
= va_arg(ap
, char *)) == NULL
)
324 cp
= __UNCONST("(null)");
327 * can't use strlen; can only look for the
328 * NUL in the first `prec' characters, and
329 * strlen() will go further.
331 char *p
= memchr(cp
, 0, (size_t)prec
);
334 _DIAGASSERT(__type_fit(int, p
- cp
));
335 size
= (int)(p
- cp
);
342 _DIAGASSERT(__type_fit(int, len
));
359 hex
: _uquad
= UARG();
361 /* leading 0x/X only if non-zero */
362 if (flags
& ALT
&& _uquad
!= 0)
365 /* unsigned conversions */
368 * ``... diouXx conversions ... if a precision is
369 * specified, the 0 flag will be ignored.''
372 number
: if ((dprec
= prec
) >= 0)
376 * ``The result of converting a zero value with an
377 * explicit precision of zero is no characters.''
380 cp
= bf
+ sizeof(bf
);
381 if (_uquad
!= 0 || prec
!= 0) {
383 * Unsigned mod is hard, and unsigned mod
384 * by a constant is easier than that by
385 * a variable; hence this switch.
390 *--cp
= to_char(_uquad
& 7);
393 /* handle octal leading 0 */
394 if (flags
& ALT
&& *cp
!= '0')
399 /* many numbers are 1 digit */
400 while (_uquad
>= 10) {
401 *--cp
= to_char(_uquad
% 10);
404 *--cp
= to_char(_uquad
);
409 *--cp
= xdigs
[(size_t)_uquad
& 15];
416 cp
= __UNCONST("bug bad base");
418 _DIAGASSERT(__type_fit(int, len
));
423 _DIAGASSERT(__type_fit(int, bf
+ sizeof(bf
) - cp
));
424 size
= (int)(bf
+ sizeof(bf
) - cp
);
427 default: /* "%?" prints ?, unless ? is NUL */
430 /* pretend it was %c with argument ch */
439 * All reasonable formats wind up here. At this point, `cp'
440 * points to a string which (if not flags&LADJUST) should be
441 * padded out to `width' places. If flags&ZEROPAD, it should
442 * first be prefixed by any sign or other prefix; otherwise,
443 * it should be blank padded before the prefix is emitted.
444 * After any left-hand padding and prefixing, emit zeroes
445 * required by a decimal [diouxX] precision, then print the
446 * string proper, then emit zeroes required by any leftover
447 * floating precision; finally, if LADJUST, pad with blanks.
449 * Compute actual size, so we know how much to pad.
450 * size excludes decimal prec; realsz includes it.
452 realsz
= dprec
> size
? dprec
: size
;
455 else if (flags
& HEXPREFIX
)
459 ret
+= width
> realsz
? width
: realsz
;
461 /* right-adjusting blank padding */
462 if ((flags
& (LADJUST
|ZEROPAD
)) == 0) {
471 } else if (flags
& HEXPREFIX
) {
476 /* right-adjusting zero padding */
477 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
) {
483 /* leading zeroes from decimal precision */
488 /* the string or number proper */
491 /* left-adjusting padding (always blank) */
492 if (flags
& LADJUST
) {