vsnprintf_ss.c revision 1.8 1 1.7 dsl /* $NetBSD: vsnprintf_ss.c,v 1.8 2009/10/25 20:44:13 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1990, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to Berkeley by
8 1.1 christos * Chris Torek.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. Neither the name of the University nor the names of its contributors
19 1.1 christos * may be used to endorse or promote products derived from this software
20 1.1 christos * without specific prior written permission.
21 1.1 christos *
22 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 christos * SUCH DAMAGE.
33 1.1 christos */
34 1.1 christos
35 1.1 christos #include <sys/cdefs.h>
36 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
37 1.1 christos #if 0
38 1.1 christos static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
39 1.1 christos #else
40 1.7 dsl __RCSID("$NetBSD: vsnprintf_ss.c,v 1.8 2009/10/25 20:44:13 christos Exp $");
41 1.1 christos #endif
42 1.1 christos #endif /* LIBC_SCCS and not lint */
43 1.1 christos
44 1.1 christos #include "namespace.h"
45 1.1 christos
46 1.3 christos #include <sys/types.h>
47 1.3 christos #include <inttypes.h>
48 1.1 christos #include <assert.h>
49 1.3 christos #include <stdio.h>
50 1.1 christos #include <errno.h>
51 1.3 christos #include <stdarg.h>
52 1.3 christos #include <string.h>
53 1.1 christos #include "reentrant.h"
54 1.2 christos #include "extern.h"
55 1.1 christos #include "local.h"
56 1.1 christos
57 1.1 christos #ifdef __weak_alias
58 1.1 christos __weak_alias(vsnprintf_ss,_vsnprintf_ss)
59 1.1 christos #endif
60 1.1 christos
61 1.3 christos /*
62 1.3 christos * vsnprintf_ss: scaled down version of printf(3).
63 1.3 christos *
64 1.3 christos * this version based on vfprintf() from libc which was derived from
65 1.3 christos * software contributed to Berkeley by Chris Torek.
66 1.3 christos *
67 1.3 christos */
68 1.3 christos
69 1.3 christos /*
70 1.3 christos * macros for converting digits to letters and vice versa
71 1.3 christos */
72 1.3 christos #define to_digit(c) ((c) - '0')
73 1.3 christos #define is_digit(c) ((unsigned)to_digit(c) <= 9)
74 1.3 christos #define to_char(n) (char)((n) + '0')
75 1.3 christos
76 1.3 christos /*
77 1.3 christos * flags used during conversion.
78 1.3 christos */
79 1.3 christos #define ALT 0x001 /* alternate form */
80 1.3 christos #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
81 1.3 christos #define LADJUST 0x004 /* left adjustment */
82 1.3 christos #define LONGDBL 0x008 /* long double; unimplemented */
83 1.3 christos #define LONGINT 0x010 /* long integer */
84 1.3 christos #define QUADINT 0x020 /* quad integer */
85 1.3 christos #define SHORTINT 0x040 /* short integer */
86 1.3 christos #define MAXINT 0x080 /* intmax_t */
87 1.3 christos #define PTRINT 0x100 /* intptr_t */
88 1.3 christos #define SIZEINT 0x200 /* size_t */
89 1.3 christos #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
90 1.3 christos #define FPT 0x800 /* Floating point number */
91 1.3 christos
92 1.3 christos /*
93 1.3 christos * To extend shorts properly, we need both signed and unsigned
94 1.3 christos * argument extraction methods.
95 1.3 christos */
96 1.3 christos #define SARG() \
97 1.3 christos (flags&MAXINT ? va_arg(ap, intmax_t) : \
98 1.3 christos flags&PTRINT ? va_arg(ap, intptr_t) : \
99 1.3 christos flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
100 1.3 christos flags&QUADINT ? va_arg(ap, quad_t) : \
101 1.3 christos flags&LONGINT ? va_arg(ap, long) : \
102 1.3 christos flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
103 1.3 christos (long)va_arg(ap, int))
104 1.3 christos #define UARG() \
105 1.3 christos (flags&MAXINT ? va_arg(ap, uintmax_t) : \
106 1.3 christos flags&PTRINT ? va_arg(ap, uintptr_t) : \
107 1.3 christos flags&SIZEINT ? va_arg(ap, size_t) : \
108 1.3 christos flags&QUADINT ? va_arg(ap, u_quad_t) : \
109 1.3 christos flags&LONGINT ? va_arg(ap, u_long) : \
110 1.3 christos flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
111 1.3 christos (u_long)va_arg(ap, u_int))
112 1.3 christos
113 1.3 christos #define PUTCHAR(C) do { \
114 1.3 christos if (sbuf < tailp) \
115 1.3 christos *sbuf++ = (C); \
116 1.3 christos } while (/*CONSTCOND*/0)
117 1.3 christos
118 1.1 christos int
119 1.3 christos vsnprintf_ss(char *sbuf, size_t slen, const char *fmt0, _BSD_VA_LIST_ ap)
120 1.1 christos {
121 1.3 christos const char *fmt; /* format string */
122 1.3 christos int ch; /* character from fmt */
123 1.3 christos int n; /* handy integer (short term usage) */
124 1.3 christos char *cp; /* handy char pointer (short term usage) */
125 1.3 christos int flags; /* flags as above */
126 1.3 christos int ret; /* return value accumulator */
127 1.3 christos int width; /* width from format (%8d), or 0 */
128 1.3 christos int prec; /* precision from format (%.3d), or -1 */
129 1.3 christos char sign; /* sign prefix (' ', '+', '-', or \0) */
130 1.3 christos
131 1.3 christos u_quad_t _uquad; /* integer arguments %[diouxX] */
132 1.3 christos enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
133 1.3 christos int dprec; /* a copy of prec if [diouxX], 0 otherwise */
134 1.3 christos int realsz; /* field size expanded by dprec */
135 1.3 christos int size; /* size of converted field or string */
136 1.3 christos const char *xdigs; /* digits for [xX] conversion */
137 1.3 christos char bf[128]; /* space for %c, %[diouxX] */
138 1.3 christos char *tailp; /* tail pointer for snprintf */
139 1.3 christos
140 1.3 christos static const char xdigs_lower[16] = "0123456789abcdef";
141 1.3 christos static const char xdigs_upper[16] = "0123456789ABCDEF";
142 1.1 christos
143 1.5 christos _DIAGASSERT(slen == 0 || sbuf != NULL);
144 1.6 christos _DIAGASSERT(fmt0 != NULL);
145 1.1 christos
146 1.5 christos if ((int)slen < 0) {
147 1.4 christos errno = EINVAL;
148 1.4 christos return (-1);
149 1.4 christos }
150 1.4 christos
151 1.3 christos tailp = sbuf + slen;
152 1.3 christos
153 1.3 christos cp = NULL; /* XXX: shutup gcc */
154 1.3 christos size = 0; /* XXX: shutup gcc */
155 1.3 christos
156 1.3 christos fmt = fmt0;
157 1.3 christos ret = 0;
158 1.3 christos
159 1.3 christos xdigs = NULL; /* XXX: shut up gcc warning */
160 1.3 christos
161 1.3 christos /*
162 1.3 christos * Scan the format for conversions (`%' character).
163 1.3 christos */
164 1.3 christos for (;;) {
165 1.3 christos while (*fmt != '%' && *fmt) {
166 1.3 christos ret++;
167 1.3 christos PUTCHAR(*fmt++);
168 1.3 christos }
169 1.3 christos if (*fmt == 0)
170 1.3 christos goto done;
171 1.3 christos
172 1.3 christos fmt++; /* skip over '%' */
173 1.3 christos
174 1.3 christos flags = 0;
175 1.3 christos dprec = 0;
176 1.3 christos width = 0;
177 1.3 christos prec = -1;
178 1.3 christos sign = '\0';
179 1.3 christos
180 1.3 christos rflag: ch = *fmt++;
181 1.3 christos reswitch: switch (ch) {
182 1.3 christos case ' ':
183 1.3 christos /*
184 1.3 christos * ``If the space and + flags both appear, the space
185 1.3 christos * flag will be ignored.''
186 1.3 christos * -- ANSI X3J11
187 1.3 christos */
188 1.3 christos if (!sign)
189 1.3 christos sign = ' ';
190 1.3 christos goto rflag;
191 1.3 christos case '#':
192 1.3 christos flags |= ALT;
193 1.3 christos goto rflag;
194 1.3 christos case '*':
195 1.3 christos /*
196 1.3 christos * ``A negative field width argument is taken as a
197 1.3 christos * - flag followed by a positive field width.''
198 1.3 christos * -- ANSI X3J11
199 1.3 christos * They don't exclude field widths read from args.
200 1.3 christos */
201 1.3 christos if ((width = va_arg(ap, int)) >= 0)
202 1.3 christos goto rflag;
203 1.3 christos width = -width;
204 1.3 christos /* FALLTHROUGH */
205 1.3 christos case '-':
206 1.3 christos flags |= LADJUST;
207 1.3 christos goto rflag;
208 1.3 christos case '+':
209 1.3 christos sign = '+';
210 1.3 christos goto rflag;
211 1.3 christos case '.':
212 1.3 christos if ((ch = *fmt++) == '*') {
213 1.3 christos n = va_arg(ap, int);
214 1.3 christos prec = n < 0 ? -1 : n;
215 1.3 christos goto rflag;
216 1.3 christos }
217 1.3 christos n = 0;
218 1.3 christos while (is_digit(ch)) {
219 1.3 christos n = 10 * n + to_digit(ch);
220 1.3 christos ch = *fmt++;
221 1.3 christos }
222 1.3 christos prec = n < 0 ? -1 : n;
223 1.3 christos goto reswitch;
224 1.3 christos case '0':
225 1.3 christos /*
226 1.3 christos * ``Note that 0 is taken as a flag, not as the
227 1.3 christos * beginning of a field width.''
228 1.3 christos * -- ANSI X3J11
229 1.3 christos */
230 1.3 christos flags |= ZEROPAD;
231 1.3 christos goto rflag;
232 1.3 christos case '1': case '2': case '3': case '4':
233 1.3 christos case '5': case '6': case '7': case '8': case '9':
234 1.3 christos n = 0;
235 1.3 christos do {
236 1.3 christos n = 10 * n + to_digit(ch);
237 1.3 christos ch = *fmt++;
238 1.3 christos } while (is_digit(ch));
239 1.3 christos width = n;
240 1.3 christos goto reswitch;
241 1.3 christos case 'h':
242 1.3 christos flags |= SHORTINT;
243 1.3 christos goto rflag;
244 1.3 christos case 'j':
245 1.3 christos flags |= MAXINT;
246 1.3 christos goto rflag;
247 1.3 christos case 'l':
248 1.3 christos if (*fmt == 'l') {
249 1.3 christos fmt++;
250 1.3 christos flags |= QUADINT;
251 1.3 christos } else {
252 1.3 christos flags |= LONGINT;
253 1.3 christos }
254 1.3 christos goto rflag;
255 1.3 christos case 'q':
256 1.3 christos flags |= QUADINT;
257 1.3 christos goto rflag;
258 1.3 christos case 't':
259 1.3 christos flags |= PTRINT;
260 1.3 christos goto rflag;
261 1.3 christos case 'z':
262 1.3 christos flags |= SIZEINT;
263 1.3 christos goto rflag;
264 1.3 christos case 'c':
265 1.3 christos *(cp = bf) = va_arg(ap, int);
266 1.3 christos size = 1;
267 1.3 christos sign = '\0';
268 1.3 christos break;
269 1.3 christos case 'D':
270 1.3 christos flags |= LONGINT;
271 1.3 christos /*FALLTHROUGH*/
272 1.3 christos case 'd':
273 1.3 christos case 'i':
274 1.3 christos _uquad = SARG();
275 1.3 christos if ((quad_t)_uquad < 0) {
276 1.3 christos _uquad = -_uquad;
277 1.3 christos sign = '-';
278 1.3 christos }
279 1.3 christos base = DEC;
280 1.3 christos goto number;
281 1.3 christos case 'n':
282 1.3 christos if (flags & MAXINT)
283 1.3 christos *va_arg(ap, intmax_t *) = ret;
284 1.3 christos else if (flags & PTRINT)
285 1.3 christos *va_arg(ap, intptr_t *) = ret;
286 1.3 christos else if (flags & SIZEINT)
287 1.3 christos *va_arg(ap, ssize_t *) = ret;
288 1.3 christos else if (flags & QUADINT)
289 1.3 christos *va_arg(ap, quad_t *) = ret;
290 1.3 christos else if (flags & LONGINT)
291 1.3 christos *va_arg(ap, long *) = ret;
292 1.3 christos else if (flags & SHORTINT)
293 1.3 christos *va_arg(ap, short *) = ret;
294 1.3 christos else
295 1.3 christos *va_arg(ap, int *) = ret;
296 1.3 christos continue; /* no output */
297 1.3 christos case 'O':
298 1.3 christos flags |= LONGINT;
299 1.3 christos /*FALLTHROUGH*/
300 1.3 christos case 'o':
301 1.3 christos _uquad = UARG();
302 1.3 christos base = OCT;
303 1.3 christos goto nosign;
304 1.3 christos case 'p':
305 1.3 christos /*
306 1.3 christos * ``The argument shall be a pointer to void. The
307 1.3 christos * value of the pointer is converted to a sequence
308 1.3 christos * of printable characters, in an implementation-
309 1.3 christos * defined manner.''
310 1.3 christos * -- ANSI X3J11
311 1.3 christos */
312 1.3 christos /* NOSTRICT */
313 1.3 christos _uquad = (u_long)va_arg(ap, void *);
314 1.3 christos base = HEX;
315 1.3 christos xdigs = xdigs_lower;
316 1.3 christos flags |= HEXPREFIX;
317 1.3 christos ch = 'x';
318 1.3 christos goto nosign;
319 1.3 christos case 's':
320 1.3 christos if ((cp = va_arg(ap, char *)) == NULL)
321 1.3 christos /*XXXUNCONST*/
322 1.3 christos cp = __UNCONST("(null)");
323 1.3 christos if (prec >= 0) {
324 1.3 christos /*
325 1.3 christos * can't use strlen; can only look for the
326 1.3 christos * NUL in the first `prec' characters, and
327 1.3 christos * strlen() will go further.
328 1.3 christos */
329 1.8 christos char *p = memchr(cp, 0, (size_t)prec);
330 1.3 christos
331 1.3 christos if (p != NULL) {
332 1.3 christos size = p - cp;
333 1.3 christos if (size > prec)
334 1.3 christos size = prec;
335 1.3 christos } else
336 1.3 christos size = prec;
337 1.3 christos } else
338 1.3 christos size = strlen(cp);
339 1.3 christos sign = '\0';
340 1.3 christos break;
341 1.3 christos case 'U':
342 1.3 christos flags |= LONGINT;
343 1.3 christos /*FALLTHROUGH*/
344 1.3 christos case 'u':
345 1.3 christos _uquad = UARG();
346 1.3 christos base = DEC;
347 1.3 christos goto nosign;
348 1.3 christos case 'X':
349 1.3 christos xdigs = xdigs_upper;
350 1.3 christos goto hex;
351 1.3 christos case 'x':
352 1.3 christos xdigs = xdigs_lower;
353 1.3 christos hex: _uquad = UARG();
354 1.3 christos base = HEX;
355 1.3 christos /* leading 0x/X only if non-zero */
356 1.3 christos if (flags & ALT && _uquad != 0)
357 1.3 christos flags |= HEXPREFIX;
358 1.3 christos
359 1.3 christos /* unsigned conversions */
360 1.3 christos nosign: sign = '\0';
361 1.3 christos /*
362 1.3 christos * ``... diouXx conversions ... if a precision is
363 1.3 christos * specified, the 0 flag will be ignored.''
364 1.3 christos * -- ANSI X3J11
365 1.3 christos */
366 1.3 christos number: if ((dprec = prec) >= 0)
367 1.3 christos flags &= ~ZEROPAD;
368 1.3 christos
369 1.3 christos /*
370 1.3 christos * ``The result of converting a zero value with an
371 1.3 christos * explicit precision of zero is no characters.''
372 1.3 christos * -- ANSI X3J11
373 1.3 christos */
374 1.3 christos cp = bf + sizeof(bf);
375 1.3 christos if (_uquad != 0 || prec != 0) {
376 1.3 christos /*
377 1.3 christos * Unsigned mod is hard, and unsigned mod
378 1.3 christos * by a constant is easier than that by
379 1.3 christos * a variable; hence this switch.
380 1.3 christos */
381 1.3 christos switch (base) {
382 1.3 christos case OCT:
383 1.3 christos do {
384 1.3 christos *--cp = to_char(_uquad & 7);
385 1.3 christos _uquad >>= 3;
386 1.3 christos } while (_uquad);
387 1.3 christos /* handle octal leading 0 */
388 1.3 christos if (flags & ALT && *cp != '0')
389 1.3 christos *--cp = '0';
390 1.3 christos break;
391 1.3 christos
392 1.3 christos case DEC:
393 1.3 christos /* many numbers are 1 digit */
394 1.3 christos while (_uquad >= 10) {
395 1.3 christos *--cp = to_char(_uquad % 10);
396 1.3 christos _uquad /= 10;
397 1.3 christos }
398 1.3 christos *--cp = to_char(_uquad);
399 1.3 christos break;
400 1.3 christos
401 1.3 christos case HEX:
402 1.3 christos do {
403 1.8 christos *--cp = xdigs[(size_t)_uquad & 15];
404 1.3 christos _uquad >>= 4;
405 1.3 christos } while (_uquad);
406 1.3 christos break;
407 1.3 christos
408 1.3 christos default:
409 1.3 christos /*XXXUNCONST*/
410 1.3 christos cp = __UNCONST("bug bad base");
411 1.3 christos size = strlen(cp);
412 1.3 christos goto skipsize;
413 1.3 christos }
414 1.3 christos }
415 1.3 christos size = bf + sizeof(bf) - cp;
416 1.3 christos skipsize:
417 1.3 christos break;
418 1.3 christos default: /* "%?" prints ?, unless ? is NUL */
419 1.3 christos if (ch == '\0')
420 1.3 christos goto done;
421 1.3 christos /* pretend it was %c with argument ch */
422 1.3 christos cp = bf;
423 1.3 christos *cp = ch;
424 1.3 christos size = 1;
425 1.3 christos sign = '\0';
426 1.3 christos break;
427 1.3 christos }
428 1.3 christos
429 1.3 christos /*
430 1.3 christos * All reasonable formats wind up here. At this point, `cp'
431 1.3 christos * points to a string which (if not flags&LADJUST) should be
432 1.3 christos * padded out to `width' places. If flags&ZEROPAD, it should
433 1.3 christos * first be prefixed by any sign or other prefix; otherwise,
434 1.3 christos * it should be blank padded before the prefix is emitted.
435 1.3 christos * After any left-hand padding and prefixing, emit zeroes
436 1.3 christos * required by a decimal [diouxX] precision, then print the
437 1.3 christos * string proper, then emit zeroes required by any leftover
438 1.3 christos * floating precision; finally, if LADJUST, pad with blanks.
439 1.3 christos *
440 1.3 christos * Compute actual size, so we know how much to pad.
441 1.3 christos * size excludes decimal prec; realsz includes it.
442 1.3 christos */
443 1.3 christos realsz = dprec > size ? dprec : size;
444 1.3 christos if (sign)
445 1.3 christos realsz++;
446 1.3 christos else if (flags & HEXPREFIX)
447 1.3 christos realsz+= 2;
448 1.3 christos
449 1.3 christos /* adjust ret */
450 1.3 christos ret += width > realsz ? width : realsz;
451 1.3 christos
452 1.3 christos /* right-adjusting blank padding */
453 1.3 christos if ((flags & (LADJUST|ZEROPAD)) == 0) {
454 1.3 christos n = width - realsz;
455 1.3 christos while (n-- > 0)
456 1.3 christos PUTCHAR(' ');
457 1.3 christos }
458 1.3 christos
459 1.3 christos /* prefix */
460 1.3 christos if (sign) {
461 1.3 christos PUTCHAR(sign);
462 1.3 christos } else if (flags & HEXPREFIX) {
463 1.3 christos PUTCHAR('0');
464 1.3 christos PUTCHAR(ch);
465 1.3 christos }
466 1.3 christos
467 1.3 christos /* right-adjusting zero padding */
468 1.3 christos if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
469 1.3 christos n = width - realsz;
470 1.3 christos while (n-- > 0)
471 1.3 christos PUTCHAR('0');
472 1.3 christos }
473 1.3 christos
474 1.3 christos /* leading zeroes from decimal precision */
475 1.3 christos n = dprec - size;
476 1.3 christos while (n-- > 0)
477 1.3 christos PUTCHAR('0');
478 1.3 christos
479 1.3 christos /* the string or number proper */
480 1.3 christos while (size--)
481 1.3 christos PUTCHAR(*cp++);
482 1.3 christos /* left-adjusting padding (always blank) */
483 1.3 christos if (flags & LADJUST) {
484 1.3 christos n = width - realsz;
485 1.3 christos while (n-- > 0)
486 1.3 christos PUTCHAR(' ');
487 1.3 christos }
488 1.1 christos }
489 1.1 christos
490 1.3 christos done:
491 1.3 christos if (sbuf == tailp)
492 1.3 christos sbuf[-1] = '\0';
493 1.3 christos else
494 1.3 christos *sbuf = '\0';
495 1.1 christos return (ret);
496 1.3 christos /* NOTREACHED */
497 1.1 christos }
498