Ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a25176072e02db9254f0e0c84c805cd)
vsnprintf.c
Go to the documentation of this file.
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * IMPORTANT NOTE:
35 * --------------
36 * From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
37 * paragraph 3 above is now null and void.
38 */
39
40/* SNPRINTF.C
41 * fjc 7-31-97 Modified by Mib Software to be a standalone snprintf.c module.
42 * http://www.mibsoftware.com
43 * Mib Software does not warrant this software any differently than the
44 * University of California, Berkeley as described above. All warranties
45 * are disclaimed. Use this software at your own risk.
46 *
47 * All code referencing FILE * functions was eliminated, since it could
48 * never be called. All header files and necessary files are collapsed
49 * into one file, internal functions are declared static. This should
50 * allow inclusion into libraries with less chance of namespace collisions.
51 *
52 * snprintf should be the only externally visible item.
53 *
54 * As of 7-31-97 FLOATING_POINT is NOT provided. The code is somewhat
55 * non-portable, so it is disabled.
56 */
57
58/* Define FLOATING_POINT to get floating point. */
59/*
60#define FLOATING_POINT
61*/
62
63#include <sys/types.h>
64#define u_long unsigned long
65#define u_short unsigned short
66#define u_int unsigned int
67
68#if !defined(HAVE_STDARG_PROTOTYPES)
69#if defined(__STDC__)
70#define HAVE_STDARG_PROTOTYPES 1
71#endif
72#endif
73
74#undef __P
75#if defined(HAVE_STDARG_PROTOTYPES)
76# include <stdarg.h>
77# if !defined(__P)
78# define __P(x) x
79# endif
80#else
81# define __P(x) ()
82# if !defined(const)
83# define const
84# endif
85# include <varargs.h>
86#endif
87#ifndef _BSD_VA_LIST_
88#define _BSD_VA_LIST_ va_list
89#endif
90
91#ifdef __STDC__
92# include <limits.h>
93#else
94# ifndef LONG_MAX
95# ifdef HAVE_LIMITS_H
96# include <limits.h>
97# else
98 /* assuming 32bit(2's complement) long */
99# define LONG_MAX 2147483647
100# endif
101# endif
102#endif
103
104#if defined(__hpux) && !defined(__GNUC__) && !defined(__STDC__)
105#define const
106#endif
107
108#if defined(sgi)
109#undef __const
110#define __const
111#endif /* People who don't like const sys_error */
112
113#include <stddef.h>
114#if defined(__hpux) && !defined(__GNUC__) || defined(__DECC)
115#include <string.h>
116#endif
117
118#if !defined(__CYGWIN32__) && defined(__hpux) && !defined(__GNUC__)
119#include <stdlib.h>
120#endif
121
122#ifndef NULL
123#define NULL 0
124#endif
125
126#if SIZEOF_LONG > SIZEOF_INT
127# include <errno.h>
128#endif
129
130/*
131 * NB: to fit things in six character monocase externals, the stdio
132 * code uses the prefix `__s' for stdio objects, typically followed
133 * by a three-character attempt at a mnemonic.
134 */
135
136/* stdio buffers */
137struct __sbuf {
138 unsigned char *_base;
139 size_t _size;
140};
141
142
143/*
144 * stdio state variables.
145 *
146 * The following always hold:
147 *
148 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
149 * _lbfsize is -_bf._size, else _lbfsize is 0
150 * if _flags&__SRD, _w is 0
151 * if _flags&__SWR, _r is 0
152 *
153 * This ensures that the getc and putc macros (or inline functions) never
154 * try to write or read from a file that is in `read' or `write' mode.
155 * (Moreover, they can, and do, automatically switch from read mode to
156 * write mode, and back, on "r+" and "w+" files.)
157 *
158 * _lbfsize is used only to make the inline line-buffered output stream
159 * code as compact as possible.
160 *
161 * _ub, _up, and _ur are used when ungetc() pushes back more characters
162 * than fit in the current _bf, or when ungetc() pushes back a character
163 * that does not match the previous one in _bf. When this happens,
164 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
165 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
166 *
167 * NB: see WARNING above before changing the layout of this structure!
168 */
169typedef struct __sFILE {
170 unsigned char *_p; /* current position in (some) buffer */
171#if 0
172 size_t _r; /* read space left for getc() */
173#endif
174 size_t _w; /* write space left for putc() */
175 short _flags; /* flags, below; this FILE is free if 0 */
176 short _file; /* fileno, if Unix descriptor, else -1 */
177 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
178#if 0
179 size_t _lbfsize; /* 0 or -_bf._size, for inline putc */
180#endif
181 int (*vwrite)(/* struct __sFILE*, struct __suio * */);
182 const char *(*vextra)(/* struct __sFILE*, size_t, void*, long*, int */);
184
185
186#define __SLBF 0x0001 /* line buffered */
187#define __SNBF 0x0002 /* unbuffered */
188#define __SRD 0x0004 /* OK to read */
189#define __SWR 0x0008 /* OK to write */
190 /* RD and WR are never simultaneously asserted */
191#define __SRW 0x0010 /* open for reading & writing */
192#define __SEOF 0x0020 /* found EOF */
193#define __SERR 0x0040 /* found error */
194#define __SMBF 0x0080 /* _buf is from malloc */
195#define __SAPP 0x0100 /* fdopen()ed in append mode */
196#define __SSTR 0x0200 /* this is an sprintf/snprintf string */
197#define __SOPT 0x0400 /* do fseek() optimisation */
198#define __SNPT 0x0800 /* do not do fseek() optimisation */
199#define __SOFF 0x1000 /* set iff _offset is in fact correct */
200#define __SMOD 0x2000 /* true => fgetln modified _p text */
201
202
203#define EOF (-1)
204
205
206#define BSD__sfeof(p) (((p)->_flags & __SEOF) != 0)
207#define BSD__sferror(p) (((p)->_flags & __SERR) != 0)
208#define BSD__sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
209#define BSD__sfileno(p) ((p)->_file)
210
211#undef feof
212#undef ferror
213#undef clearerr
214#define feof(p) BSD__sfeof(p)
215#define ferror(p) BSD__sferror(p)
216#define clearerr(p) BSD__sclearerr(p)
217
218#ifndef _ANSI_SOURCE
219#define fileno(p) BSD__sfileno(p)
220#endif
221
222
223/*
224 * I/O descriptors for __sfvwrite().
225 */
226struct __siov {
227 const void *iov_base;
228 size_t iov_len;
229};
230struct __suio {
233 size_t uio_resid;
234};
235
236/*
237 * Write some memory regions. Return zero on success, EOF on error.
238 *
239 * This routine is large and unsightly, but most of the ugliness due
240 * to the three different kinds of output buffering is handled here.
241 */
242static int
243BSD__sfvwrite(register FILE *fp, register struct __suio *uio)
244{
245 register size_t len;
246 register const char *p;
247 register struct __siov *iov;
248 register size_t w;
249
250 if ((len = uio->uio_resid) == 0)
251 return (0);
252#ifndef __hpux
253#define MIN(a, b) ((a) < (b) ? (a) : (b))
254#endif
255#define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
256
257 iov = uio->uio_iov;
258 p = iov->iov_base;
259 len = iov->iov_len;
260 iov++;
261#define GETIOV(extra_work) \
262 while (len == 0) { \
263 extra_work; \
264 p = iov->iov_base; \
265 len = iov->iov_len; \
266 iov++; \
267 }
268 if (fp->_flags & __SNBF) {
269 /* fjc 7-31-97 Will never happen. We are working with
270 strings only
271 */
272 } else if ((fp->_flags & __SLBF) == 0) {
273 /*
274 * Fully buffered: fill partially full buffer, if any,
275 * and then flush. If there is no partial buffer, write
276 * one _bf._size byte chunk directly (without copying).
277 *
278 * String output is a special case: write as many bytes
279 * as fit, but pretend we wrote everything. This makes
280 * snprintf() return the number of bytes needed, rather
281 * than the number used, and avoids its write function
282 * (so that the write function can be invalid).
283 */
284 do {
285 GETIOV(;);
286 w = fp->_w;
287 if (fp->_flags & __SSTR) {
288 if (len < w)
289 w = len;
290 COPY(w); /* copy MIN(fp->_w,len), */
291 fp->_w -= w;
292 fp->_p += w;
293 w = len; /* but pretend copied all */
294 } else {
295 /* fjc 7-31-97 Will never happen. We are working with
296 strings only
297 */
298 }
299 p += w;
300 len -= w;
301 } while ((uio->uio_resid -= w) != 0);
302 } else {
303 /* fjc 7-31-97 Will never happen. We are working with
304 strings only
305 */
306 }
307 return (0);
308}
309
310/*
311 * Actual printf innards.
312 *
313 * This code is large and complicated...
314 */
315
316/*
317 * Flush out all the vectors defined by the given uio,
318 * then reset it so that it can be reused.
319 */
320static int
321BSD__sprint(FILE *fp, register struct __suio *uio)
322{
323 register int err;
324
325 if (uio->uio_resid == 0) {
326 uio->uio_iovcnt = 0;
327 return (0);
328 }
329 err = (*fp->vwrite)(fp, uio);
330 uio->uio_resid = 0;
331 uio->uio_iovcnt = 0;
332 return (err);
333}
334
335
336/*
337 * Helper function for `fprintf to unbuffered unix file': creates a
338 * temporary buffer. We only work on write-only files; this avoids
339 * worries about ungetc buffers and so forth.
340 */
341static int
342BSD__sbprintf(register FILE *fp, const char *fmt, va_list ap)
343{
344/* We don't support files. */
345 return 0;
346}
347
348
349/*
350 * Macros for converting digits to letters and vice versa
351 */
352#define to_digit(c) ((c) - '0')
353#define is_digit(c) ((unsigned)to_digit(c) <= 9)
354#define to_char(n) (char)((n) + '0')
355
356#ifdef _HAVE_SANE_QUAD_
357/*
358 * Convert an unsigned long long to ASCII for printf purposes, returning
359 * a pointer to the first character of the string representation.
360 * Octal numbers can be forced to have a leading zero; hex numbers
361 * use the given digits.
362 */
363static char *
364BSD__uqtoa(register u_quad_t val, char *endp, int base, int octzero, const char *xdigs)
365{
366 register char *cp = endp;
367 register quad_t sval;
368
369 /*
370 * Handle the three cases separately, in the hope of getting
371 * better/faster code.
372 */
373 switch (base) {
374 case 10:
375 if (val < 10) { /* many numbers are 1 digit */
376 *--cp = to_char(val);
377 return (cp);
378 }
379 /*
380 * On many machines, unsigned arithmetic is harder than
381 * signed arithmetic, so we do at most one unsigned mod and
382 * divide; this is sufficient to reduce the range of
383 * the incoming value to where signed arithmetic works.
384 */
385 if (val > LLONG_MAX) {
386 *--cp = to_char(val % 10);
387 sval = val / 10;
388 } else
389 sval = val;
390 do {
391 *--cp = to_char(sval % 10);
392 sval /= 10;
393 } while (sval != 0);
394 break;
395
396 case 8:
397 do {
398 *--cp = to_char(val & 7);
399 val >>= 3;
400 } while (val);
401 if (octzero && *cp != '0')
402 *--cp = '0';
403 break;
404
405 case 16:
406 do {
407 *--cp = xdigs[val & 15];
408 val >>= 4;
409 } while (val);
410 break;
411
412 default: /* oops */
413 /*
414 abort();
415 */
416 break; /* fjc 7-31-97. Don't reference abort() here */
417 }
418 return (cp);
419}
420#endif /* _HAVE_SANE_QUAD_ */
421
422/*
423 * Convert an unsigned long to ASCII for printf purposes, returning
424 * a pointer to the first character of the string representation.
425 * Octal numbers can be forced to have a leading zero; hex numbers
426 * use the given digits.
427 */
428static char *
429BSD__ultoa(register u_long val, char *endp, int base, int octzero, const char *xdigs)
430{
431 register char *cp = endp;
432 register long sval;
433
434 /*
435 * Handle the three cases separately, in the hope of getting
436 * better/faster code.
437 */
438 switch (base) {
439 case 10:
440 if (val < 10) { /* many numbers are 1 digit */
441 *--cp = to_char(val);
442 return (cp);
443 }
444 /*
445 * On many machines, unsigned arithmetic is harder than
446 * signed arithmetic, so we do at most one unsigned mod and
447 * divide; this is sufficient to reduce the range of
448 * the incoming value to where signed arithmetic works.
449 */
450 if (val > LONG_MAX) {
451 *--cp = to_char(val % 10);
452 sval = val / 10;
453 } else
454 sval = val;
455 do {
456 *--cp = to_char(sval % 10);
457 sval /= 10;
458 } while (sval != 0);
459 break;
460
461 case 8:
462 do {
463 *--cp = to_char(val & 7);
464 val >>= 3;
465 } while (val);
466 if (octzero && *cp != '0')
467 *--cp = '0';
468 break;
469
470 case 16:
471 do {
472 *--cp = xdigs[val & 15];
473 val >>= 4;
474 } while (val);
475 break;
476
477 default: /* oops */
478 /*
479 abort();
480 */
481 break; /* fjc 7-31-97. Don't reference abort() here */
482 }
483 return (cp);
484}
485
486#ifdef FLOATING_POINT
487#include <math.h>
488#include <float.h>
489/* #include "floatio.h" */
490
491#ifndef MAXEXP
492# if DBL_MAX_10_EXP > -DBL_MIN_10_EXP
493# define MAXEXP (DBL_MAX_10_EXP)
494# else
495# define MAXEXP (-DBL_MIN_10_EXP)
496# endif
497#endif
498
499#ifndef MAXFRACT
500# define MAXFRACT (MAXEXP*10/3)
501#endif
502
503#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
504#define DEFPREC 6
505
506static char *cvt(double, int, int, char *, int *, int, int *, char *);
507static int exponent(char *, int, int);
508
509#else /* no FLOATING_POINT */
510
511#define BUF 68
512
513#endif /* FLOATING_POINT */
514
515#ifndef lower_hexdigits
516# define lower_hexdigits "0123456789abcdef"
517#endif
518#ifndef upper_hexdigits
519# define upper_hexdigits "0123456789ABCDEF"
520#endif
521
522/*
523 * Flags used during conversion.
524 */
525#define ALT 0x001 /* alternate form */
526#define HEXPREFIX 0x002 /* add 0x or 0X prefix */
527#define LADJUST 0x004 /* left adjustment */
528#define LONGDBL 0x008 /* long double; unimplemented */
529#define LONGINT 0x010 /* long integer */
530
531#ifdef _HAVE_SANE_QUAD_
532#define QUADINT 0x020 /* quad integer */
533#endif /* _HAVE_SANE_QUAD_ */
534
535#define SHORTINT 0x040 /* short integer */
536#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
537#define FPT 0x100 /* Floating point number */
538ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static ssize_t BSD_vfprintf(FILE *fp, const char *fmt0, va_list ap));
539static ssize_t
540BSD_vfprintf(FILE *fp, const char *fmt0, va_list ap)
541{
542#ifdef PRI_EXTRA_MARK
544#endif
545 register const char *fmt; /* format string */
546 register int ch; /* character from fmt */
547 register int n; /* handy integer (short term usage) */
548 register const char *cp;/* handy char pointer (short term usage) */
549 register struct __siov *iovp;/* for PRINT macro */
550 register int flags; /* flags as above */
551 ssize_t ret; /* return value accumulator */
552 int width; /* width from format (%8d), or 0 */
553 int prec; /* precision from format (%.3d), or -1 */
554 char sign; /* sign prefix (' ', '+', '-', or \0) */
555#ifdef FLOATING_POINT
556 char softsign; /* temporary negative sign for floats */
557 double _double = 0; /* double precision arguments %[eEfgG] */
558 int expt; /* integer value of exponent */
559 int expsize = 0; /* character count for expstr */
560 int ndig = 0; /* actual number of digits returned by cvt */
561 int fprec = 0; /* floating point precision */
562 char expstr[7]; /* buffer for exponent string */
563#endif
564 u_long MAYBE_UNUSED(ulval); /* integer arguments %[diouxX] */
565#ifdef _HAVE_SANE_QUAD_
566 u_quad_t MAYBE_UNUSED(uqval); /* %q integers */
567#endif /* _HAVE_SANE_QUAD_ */
568 int base; /* base for [diouxX] conversion */
569 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
570 long fieldsz; /* field size expanded by sign, etc */
571 long realsz; /* field size expanded by dprec */
572 int size; /* size of converted field or string */
573 const char *xdigs = 0; /* digits for [xX] conversion */
574#define NIOV 8
575 struct __suio uio; /* output information: summary */
576 struct __siov iov[NIOV];/* ... and individual io vectors */
577 char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */
578 char ox[4]; /* space for 0x hex-prefix, hexadecimal's 1. */
579 char *const ebuf = buf + sizeof(buf);
580#if SIZEOF_LONG > SIZEOF_INT
581 long ln;
582#endif
583
584 /*
585 * Choose PADSIZE to trade efficiency vs. size. If larger printf
586 * fields occur frequently, increase PADSIZE and make the initializers
587 * below longer.
588 */
589#define PADSIZE 16 /* pad chunk size */
590 static const char blanks[PADSIZE] =
591 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
592 static const char zeroes[PADSIZE] =
593 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
594
595 /*
596 * BEWARE, these `goto error' on error, and PAD uses `n'.
597 */
598#define PRINT(ptr, len) { \
599 iovp->iov_base = (ptr); \
600 iovp->iov_len = (len); \
601 uio.uio_resid += (len); \
602 iovp++; \
603 if (++uio.uio_iovcnt >= NIOV) { \
604 if (BSD__sprint(fp, &uio)) \
605 goto error; \
606 iovp = iov; \
607 } \
608}
609#define PAD(howmany, with) { \
610 if ((n = (howmany)) > 0) { \
611 while (n > PADSIZE) { \
612 PRINT((with), PADSIZE); \
613 n -= PADSIZE; \
614 } \
615 PRINT((with), n); \
616 } \
617}
618#if SIZEOF_LONG > SIZEOF_INT
619 /* abandon if too larger padding */
620#define PAD_L(howmany, with) { \
621 ln = (howmany); \
622 if ((long)((int)ln) != ln) { \
623 errno = ENOMEM; \
624 goto error; \
625 } \
626 if (ln > 0) PAD((int)ln, (with)); \
627}
628#else
629#define PAD_L(howmany, with) PAD((howmany), (with))
630#endif
631#define FLUSH() { \
632 if (uio.uio_resid && BSD__sprint(fp, &uio)) \
633 goto error; \
634 uio.uio_iovcnt = 0; \
635 iovp = iov; \
636}
637
638 /*
639 * To extend shorts properly, we need both signed and unsigned
640 * argument extraction methods.
641 */
642#define SARG() \
643 (flags&LONGINT ? va_arg(ap, long) : \
644 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
645 (long)va_arg(ap, int))
646#define UARG() \
647 (flags&LONGINT ? va_arg(ap, u_long) : \
648 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
649 (u_long)va_arg(ap, u_int))
650
651 /* optimize fprintf(stderr) (and other unbuffered Unix files) */
652 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
653 fp->_file >= 0)
654 return (BSD__sbprintf(fp, fmt0, ap));
655
656 fmt = fmt0;
657 uio.uio_iov = iovp = iov;
658 uio.uio_resid = 0;
659 uio.uio_iovcnt = 0;
660 ret = 0;
661 xdigs = 0;
662
663 /*
664 * Scan the format for conversions (`%' character).
665 */
666 for (;;) {
667 size_t nc;
668 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
669 /* void */;
670 if ((nc = fmt - cp) != 0) {
671 PRINT(cp, nc);
672 ret += nc;
673 }
674 if (ch == '\0')
675 goto done;
676 fmt++; /* skip over '%' */
677
678 flags = 0;
679 dprec = 0;
680 width = 0;
681 prec = -1;
682 sign = '\0';
683
684rflag: ch = *fmt++;
685reswitch: switch (ch) {
686 case ' ':
687 /*
688 * ``If the space and + flags both appear, the space
689 * flag will be ignored.''
690 * -- ANSI X3J11
691 */
692 if (!sign)
693 sign = ' ';
694 goto rflag;
695 case '#':
696 flags |= ALT;
697 goto rflag;
698 case '*':
699 /*
700 * ``A negative field width argument is taken as a
701 * - flag followed by a positive field width.''
702 * -- ANSI X3J11
703 * They don't exclude field widths read from args.
704 */
705 if ((width = va_arg(ap, int)) >= 0)
706 goto rflag;
707 width = -width;
708 /* FALLTHROUGH */
709 case '-':
710 flags |= LADJUST;
711 goto rflag;
712 case '+':
713 sign = '+';
714 goto rflag;
715 case '.':
716 if ((ch = *fmt++) == '*') {
717 n = va_arg(ap, int);
718 prec = n < 0 ? -1 : n;
719 goto rflag;
720 }
721 n = 0;
722 while (is_digit(ch)) {
723 n = 10 * n + to_digit(ch);
724 ch = *fmt++;
725 }
726 prec = n < 0 ? -1 : n;
727 goto reswitch;
728 case '0':
729 /*
730 * ``Note that 0 is taken as a flag, not as the
731 * beginning of a field width.''
732 * -- ANSI X3J11
733 */
734 flags |= ZEROPAD;
735 goto rflag;
736 case '1': case '2': case '3': case '4':
737 case '5': case '6': case '7': case '8': case '9':
738 n = 0;
739 do {
740 n = 10 * n + to_digit(ch);
741 ch = *fmt++;
742 } while (is_digit(ch));
743 width = n;
744 goto reswitch;
745#ifdef FLOATING_POINT
746 case 'L':
747 flags |= LONGDBL;
748 goto rflag;
749#endif
750 case 'h':
751 flags |= SHORTINT;
752 goto rflag;
753#if SIZEOF_PTRDIFF_T == SIZEOF_LONG
754 case 't':
755#endif
756#if SIZEOF_SIZE_T == SIZEOF_LONG
757 case 'z':
758#endif
759 case 'l':
760#ifdef _HAVE_SANE_QUAD_
761 if (*fmt == 'l') {
762 fmt++;
763 flags |= QUADINT;
764 } else {
765 flags |= LONGINT;
766 }
767#else
768 flags |= LONGINT;
769#endif
770 goto rflag;
771#ifdef _HAVE_SANE_QUAD_
772#if SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
773 case 't':
774#endif
775#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
776 case 'z':
777#endif
778 case 'q':
779 flags |= QUADINT;
780 goto rflag;
781#endif /* _HAVE_SANE_QUAD_ */
782#ifdef _WIN32
783 case 'I':
784 if (*fmt == '3' && *(fmt + 1) == '2') {
785 fmt += 2;
786 flags |= LONGINT;
787 }
788#ifdef _HAVE_SANE_QUAD_
789 else if (*fmt == '6' && *(fmt + 1) == '4') {
790 fmt += 2;
791 flags |= QUADINT;
792 }
793#endif
794 else
795#if defined(_HAVE_SANE_QUAD_) && SIZEOF_SIZE_T == SIZEOF_LONG_LONG
796 flags |= QUADINT;
797#else
798 flags |= LONGINT;
799#endif
800 goto rflag;
801#endif
802 case 'c':
803 cp = buf;
804 *buf = (char)va_arg(ap, int);
805 size = 1;
806 sign = '\0';
807 break;
808 case 'i':
809#ifdef _HAVE_SANE_QUAD_
810# define INTPTR_MASK (QUADINT|LONGINT|SHORTINT)
811#else
812# define INTPTR_MASK (LONGINT|SHORTINT)
813#endif
814#if defined _HAVE_SANE_QUAD_ && SIZEOF_VOIDP == SIZEOF_LONG_LONG
815# define INTPTR_FLAG QUADINT
816#elif SIZEOF_VOIDP == SIZEOF_LONG
817# define INTPTR_FLAG LONGINT
818#else
819# define INTPTR_FLAG 0
820#endif
821#ifdef PRI_EXTRA_MARK
822# define IS_PRI_EXTRA_MARK(s) \
823 (PRI_EXTRA_MARK_LEN < 1 || \
824 (*(s) == PRI_EXTRA_MARK[0] && \
825 (PRI_EXTRA_MARK_LEN == 1 || \
826 strncmp((s)+1, &PRI_EXTRA_MARK[1], \
827 PRI_EXTRA_MARK_LEN-1) == 0)))
828#else
829# define PRI_EXTRA_MARK_LEN 0
830# define IS_PRI_EXTRA_MARK(s) 1
831#endif
832 if (fp->vextra && (flags & INTPTR_MASK) == INTPTR_FLAG &&
835 FLUSH();
836#if defined _HAVE_SANE_QUAD_ && SIZEOF_VOIDP == SIZEOF_LONG_LONG
837 uqval = va_arg(ap, u_quad_t);
838 cp = (*fp->vextra)(fp, sizeof(uqval), &uqval, &fieldsz, sign);
839#else
840 ulval = va_arg(ap, u_long);
841 cp = (*fp->vextra)(fp, sizeof(ulval), &ulval, &fieldsz, sign);
842#endif
843 sign = '\0';
844 if (!cp) goto error;
845 if (prec < 0) goto long_len;
846 size = fieldsz < prec ? (int)fieldsz : prec;
847 break;
848 }
849 goto decimal;
850 case 'D':
851 flags |= LONGINT;
852 /*FALLTHROUGH*/
853 case 'd':
854 decimal:
855#ifdef _HAVE_SANE_QUAD_
856 if (flags & QUADINT) {
857 uqval = va_arg(ap, quad_t);
858 if ((quad_t)uqval < 0) {
859 uqval = -(quad_t)uqval;
860 sign = '-';
861 }
862 } else
863#endif /* _HAVE_SANE_QUAD_ */
864 {
865 ulval = SARG();
866 if ((long)ulval < 0) {
867 ulval = (u_long)(-(long)ulval);
868 sign = '-';
869 }
870 }
871 base = 10;
872 goto number;
873#ifdef FLOATING_POINT
874 case 'a':
875 case 'A':
876 if (prec > 0) {
877 flags |= ALT;
878 prec++;
879 fprec = prec;
880 }
881 goto fp_begin;
882 case 'e': /* anomalous precision */
883 case 'E':
884 if (prec != 0)
885 flags |= ALT;
886 prec = (prec == -1) ?
887 DEFPREC + 1 : (fprec = prec + 1);
888 /* FALLTHROUGH */
889 goto fp_begin;
890 case 'f': /* always print trailing zeroes */
891 if (prec != 0)
892 flags |= ALT;
893 case 'g':
894 case 'G':
895 if (prec == -1)
896 prec = DEFPREC;
897 else
898 fprec = prec;
899fp_begin: _double = va_arg(ap, double);
900 /* do this before tricky precision changes */
901 if (isinf(_double)) {
902 if (_double < 0)
903 sign = '-';
904 cp = "Inf";
905 size = 3;
906 break;
907 }
908 if (isnan(_double)) {
909 cp = "NaN";
910 size = 3;
911 break;
912 }
913 flags |= FPT;
914 cp = cvt(_double, (prec < MAXFRACT ? prec : MAXFRACT), flags, &softsign,
915 &expt, ch, &ndig, buf);
916 if (ch == 'g' || ch == 'G') {
917 if (expt <= -4 || (expt > prec && expt > 1))
918 ch = (ch == 'g') ? 'e' : 'E';
919 else
920 ch = 'g';
921 }
922 if (ch == 'a' || ch == 'A') {
923 flags |= HEXPREFIX;
924 --expt;
925 expsize = exponent(expstr, expt, ch + 'p' - 'a');
926 ch += 'x' - 'a';
927 size = expsize + ndig;
928 if (ndig > 1 || flags & ALT)
929 ++size; /* floating point */
930 }
931 else if (ch <= 'e') { /* 'e' or 'E' fmt */
932 --expt;
933 expsize = exponent(expstr, expt, ch);
934 size = expsize + ndig;
935 if (ndig > 1 || flags & ALT)
936 ++fprec, ++size;
937 } else if (ch == 'f') { /* f fmt */
938 if (expt > 0) {
939 size = expt;
940 if (prec || flags & ALT)
941 size += prec + 1;
942 } else if (!prec) { /* "0" */
943 size = 1;
944 if (flags & ALT)
945 size += 1;
946 } else /* "0.X" */
947 size = prec + 2;
948 } else if (expt >= ndig) { /* fixed g fmt */
949 size = expt;
950 if (flags & ALT)
951 ++size;
952 } else
953 size = ndig + (expt > 0 ?
954 1 : 2 - expt);
955
956 if (softsign)
957 sign = '-';
958 break;
959#endif /* FLOATING_POINT */
960 case 'n':
961#ifdef _HAVE_SANE_QUAD_
962 if (flags & QUADINT)
963 *va_arg(ap, quad_t *) = ret;
964 else if (flags & LONGINT)
965#else /* _HAVE_SANE_QUAD_ */
966 if (flags & LONGINT)
967#endif /* _HAVE_SANE_QUAD_ */
968 *va_arg(ap, long *) = ret;
969 else if (flags & SHORTINT)
970 *va_arg(ap, short *) = (short)ret;
971 else
972 *va_arg(ap, int *) = (int)ret;
973 continue; /* no output */
974 case 'O':
975 flags |= LONGINT;
976 /*FALLTHROUGH*/
977 case 'o':
978#ifdef _HAVE_SANE_QUAD_
979 if (flags & QUADINT)
980 uqval = va_arg(ap, u_quad_t);
981 else
982#endif /* _HAVE_SANE_QUAD_ */
983 ulval = UARG();
984 base = 8;
985 goto nosign;
986 case 'p':
987 /*
988 * ``The argument shall be a pointer to void. The
989 * value of the pointer is converted to a sequence
990 * of printable characters, in an implementation-
991 * defined manner.''
992 * -- ANSI X3J11
993 */
994 prec = (int)(sizeof(void*)*CHAR_BIT/4);
995#ifdef _HAVE_LLP64_
996 uqval = (u_quad_t)va_arg(ap, void *);
997 flags = (flags) | QUADINT | HEXPREFIX;
998#else
999 ulval = (u_long)va_arg(ap, void *);
1000#ifdef _HAVE_SANE_QUAD_
1001 flags = (flags & ~QUADINT) | HEXPREFIX;
1002#else /* _HAVE_SANE_QUAD_ */
1003 flags = (flags) | HEXPREFIX;
1004#endif /* _HAVE_SANE_QUAD_ */
1005#endif
1006 base = 16;
1007 xdigs = lower_hexdigits;
1008 ch = 'x';
1009 goto nosign;
1010 case 's':
1011 if ((cp = va_arg(ap, char *)) == NULL)
1012 cp = "(null)";
1013 if (prec >= 0) {
1014 /*
1015 * can't use strlen; can only look for the
1016 * NUL in the first `prec' characters, and
1017 * strlen() will go further.
1018 */
1019 const char *p = (char *)memchr(cp, 0, prec);
1020
1021 if (p != NULL && (p - cp) < prec)
1022 size = (int)(p - cp);
1023 else
1024 size = prec;
1025 }
1026 else {
1027 fieldsz = strlen(cp);
1028 goto long_len;
1029 }
1030 sign = '\0';
1031 break;
1032 case 'U':
1033 flags |= LONGINT;
1034 /*FALLTHROUGH*/
1035 case 'u':
1036#ifdef _HAVE_SANE_QUAD_
1037 if (flags & QUADINT)
1038 uqval = va_arg(ap, u_quad_t);
1039 else
1040#endif /* _HAVE_SANE_QUAD_ */
1041 ulval = UARG();
1042 base = 10;
1043 goto nosign;
1044 case 'X':
1045 xdigs = upper_hexdigits;
1046 goto hex;
1047 case 'x':
1048 xdigs = lower_hexdigits;
1049hex:
1050#ifdef _HAVE_SANE_QUAD_
1051 if (flags & QUADINT)
1052 uqval = va_arg(ap, u_quad_t);
1053 else
1054#endif /* _HAVE_SANE_QUAD_ */
1055 ulval = UARG();
1056 base = 16;
1057 /* leading 0x/X only if non-zero */
1058 if (flags & ALT &&
1059#ifdef _HAVE_SANE_QUAD_
1060 (flags & QUADINT ? uqval != 0 : ulval != 0)
1061#else /* _HAVE_SANE_QUAD_ */
1062 ulval != 0
1063#endif /* _HAVE_SANE_QUAD_ */
1064 )
1065 flags |= HEXPREFIX;
1066
1067 /* unsigned conversions */
1068nosign: sign = '\0';
1069 /*
1070 * ``... diouXx conversions ... if a precision is
1071 * specified, the 0 flag will be ignored.''
1072 * -- ANSI X3J11
1073 */
1074number: if ((dprec = prec) >= 0)
1075 flags &= ~ZEROPAD;
1076
1077 /*
1078 * ``The result of converting a zero value with an
1079 * explicit precision of zero is no characters.''
1080 * -- ANSI X3J11
1081 */
1082 cp = ebuf;
1083#ifdef _HAVE_SANE_QUAD_
1084 if (flags & QUADINT) {
1085 if (uqval != 0 || prec != 0)
1086 cp = BSD__uqtoa(uqval, ebuf, base,
1087 flags & ALT, xdigs);
1088 } else
1089#else /* _HAVE_SANE_QUAD_ */
1090#endif /* _HAVE_SANE_QUAD_ */
1091 {
1092 if (ulval != 0 || prec != 0)
1093 cp = BSD__ultoa(ulval, ebuf, base,
1094 flags & ALT, xdigs);
1095 }
1096 size = (int)(ebuf - cp);
1097 break;
1098 default: /* "%?" prints ?, unless ? is NUL */
1099 if (ch == '\0')
1100 goto done;
1101 /* pretend it was %c with argument ch */
1102 cp = buf;
1103 *buf = ch;
1104 size = 1;
1105 sign = '\0';
1106 break;
1107 }
1108
1109 /*
1110 * All reasonable formats wind up here. At this point, `cp'
1111 * points to a string which (if not flags&LADJUST) should be
1112 * padded out to `width' places. If flags&ZEROPAD, it should
1113 * first be prefixed by any sign or other prefix; otherwise,
1114 * it should be blank padded before the prefix is emitted.
1115 * After any left-hand padding and prefixing, emit zeroes
1116 * required by a decimal [diouxX] precision, then print the
1117 * string proper, then emit zeroes required by any leftover
1118 * floating precision; finally, if LADJUST, pad with blanks.
1119 *
1120 * Compute actual size, so we know how much to pad.
1121 * fieldsz excludes decimal prec; realsz includes it.
1122 */
1123 fieldsz = size;
1124long_len:
1125 realsz = dprec > fieldsz ? dprec : fieldsz;
1126 if (sign)
1127 realsz++;
1128 if (flags & HEXPREFIX)
1129 realsz += 2;
1130
1131 /* right-adjusting blank padding */
1132 if ((flags & (LADJUST|ZEROPAD)) == 0)
1133 PAD_L(width - realsz, blanks);
1134
1135 /* prefix */
1136 if (sign) {
1137 PRINT(&sign, 1);
1138 }
1139 if (flags & HEXPREFIX) {
1140 ox[0] = '0';
1141 ox[1] = ch;
1142 PRINT(ox, 2);
1143 }
1144
1145 /* right-adjusting zero padding */
1146 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1147 PAD_L(width - realsz, zeroes);
1148
1149 /* leading zeroes from decimal precision */
1150 PAD_L(dprec - fieldsz, zeroes);
1151
1152 /* the string or number proper */
1153#ifdef FLOATING_POINT
1154 if ((flags & FPT) == 0) {
1155 PRINT(cp, fieldsz);
1156 } else { /* glue together f_p fragments */
1157 if (flags & HEXPREFIX) {
1158 if (ndig > 1 || flags & ALT) {
1159 ox[2] = *cp++;
1160 ox[3] = '.';
1161 PRINT(ox+2, 2);
1162 if (ndig > 0) PRINT(cp, ndig-1);
1163 } else /* XpYYY */
1164 PRINT(cp, 1);
1165 PAD(fprec-ndig, zeroes);
1166 PRINT(expstr, expsize);
1167 }
1168 else if (ch >= 'f') { /* 'f' or 'g' */
1169 if (_double == 0) {
1170 /* kludge for __dtoa irregularity */
1171 if (ndig <= 1 &&
1172 (flags & ALT) == 0) {
1173 PRINT("0", 1);
1174 } else {
1175 PRINT("0.", 2);
1176 PAD((ndig >= fprec ? ndig - 1 : fprec - (ch != 'f')),
1177 zeroes);
1178 }
1179 } else if (expt == 0 && ndig == 0 && (flags & ALT) == 0) {
1180 PRINT("0", 1);
1181 } else if (expt <= 0) {
1182 PRINT("0.", 2);
1183 PAD(-expt, zeroes);
1184 PRINT(cp, ndig);
1185 if (flags & ALT)
1186 PAD(fprec - ndig + (ch == 'f' ? expt : 0), zeroes);
1187 } else if (expt >= ndig) {
1188 PRINT(cp, ndig);
1189 PAD(expt - ndig, zeroes);
1190 if (flags & ALT)
1191 PRINT(".", 1);
1192 } else {
1193 PRINT(cp, expt);
1194 cp += expt;
1195 PRINT(".", 1);
1196 PRINT(cp, ndig-expt);
1197 if (flags & ALT)
1198 PAD(fprec - ndig + (ch == 'f' ? expt : 0), zeroes);
1199 }
1200 } else { /* 'e' or 'E' */
1201 if (ndig > 1 || flags & ALT) {
1202 ox[0] = *cp++;
1203 ox[1] = '.';
1204 PRINT(ox, 2);
1205 if (_double /*|| flags & ALT == 0*/) {
1206 PRINT(cp, ndig-1);
1207 } else /* 0.[0..] */
1208 /* __dtoa irregularity */
1209 PAD(ndig - 1, zeroes);
1210 if (flags & ALT) PAD(fprec - ndig - 1, zeroes);
1211 } else /* XeYYY */
1212 PRINT(cp, 1);
1213 PRINT(expstr, expsize);
1214 }
1215 }
1216#else
1217 PRINT(cp, fieldsz);
1218#endif
1219 /* left-adjusting padding (always blank) */
1220 if (flags & LADJUST)
1221 PAD_L(width - realsz, blanks);
1222
1223 /* finally, adjust ret */
1224 ret += width > realsz ? width : realsz;
1225
1226 FLUSH(); /* copy out the I/O vectors */
1227 }
1228done:
1229 FLUSH();
1230error:
1231 return (BSD__sferror(fp) ? EOF : ret);
1232 /* NOTREACHED */
1233}
1234
1235#ifdef FLOATING_POINT
1236
1237extern char *BSD__dtoa(double, int, int, int *, int *, char **);
1238extern char *BSD__hdtoa(double, const char *, int, int *, int *, char **);
1239
1240static char *
1241cvt(double value, int ndigits, int flags, char *sign, int *decpt, int ch, int *length, char *buf)
1242{
1243 int mode, dsgn;
1244 char *digits, *bp, *rve;
1245
1246 if (ch == 'f')
1247 mode = 3;
1248 else {
1249 mode = 2;
1250 }
1251 if (value < 0) {
1252 value = -value;
1253 *sign = '-';
1254 } else if (value == 0.0 && signbit(value)) {
1255 *sign = '-';
1256 } else {
1257 *sign = '\000';
1258 }
1259 if (ch == 'a' || ch =='A') {
1260 digits = BSD__hdtoa(value,
1261 ch == 'a' ? lower_hexdigits : upper_hexdigits,
1262 ndigits, decpt, &dsgn, &rve);
1263 }
1264 else {
1265 digits = BSD__dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
1266 }
1267 buf[0] = 0; /* rve - digits may be 0 */
1268 memcpy(buf, digits, rve - digits);
1269 xfree(digits);
1270 rve = buf + (rve - digits);
1271 digits = buf;
1272 if (flags & ALT) { /* Print trailing zeros */
1273 bp = digits + ndigits;
1274 if (ch == 'f') {
1275 if (*digits == '0' && value)
1276 *decpt = -ndigits + 1;
1277 bp += *decpt;
1278 }
1279 while (rve < bp)
1280 *rve++ = '0';
1281 }
1282 *length = (int)(rve - digits);
1283 return (digits);
1284}
1285
1286static int
1287exponent(char *p0, int exp, int fmtch)
1288{
1289 register char *p, *t;
1290 char expbuf[2 + (MAXEXP < 1000 ? 3 : MAXEXP < 10000 ? 4 : 5)]; /* >= 2 + ceil(log10(MAXEXP)) */
1291
1292 p = p0;
1293 *p++ = fmtch;
1294 if (exp < 0) {
1295 exp = -exp;
1296 *p++ = '-';
1297 }
1298 else
1299 *p++ = '+';
1300 t = expbuf + sizeof(expbuf);
1301 if (exp > 9) {
1302 do {
1303 *--t = to_char(exp % 10);
1304 } while ((exp /= 10) > 9);
1305 *--t = to_char(exp);
1306 for (; t < expbuf + sizeof(expbuf); *p++ = *t++);
1307 }
1308 else {
1309 if (fmtch & 15) *p++ = '0'; /* other than p or P */
1310 *p++ = to_char(exp);
1311 }
1312 return (int)(p - p0);
1313}
1314#endif /* FLOATING_POINT */
void * memchr(const void *, int, size_t)
#define bp()
size_t strlen(const char *)
#define LLONG_MAX
#define xfree
#define CHAR_BIT
#define MAYBE_UNUSED(x)
const rb_iseq_t const char * error
#define signbit(__x)
const char size_t n
#define isinf(__x)
#define char
#define isnan(__x)
__inline__ const void *__restrict__ size_t len
const VALUE int int int int int int VALUE char * fmt
__gnuc_va_list va_list
unsigned long u_long
#define short
#define va_arg(v, l)
unsigned int size
#define rb_strlen_lit(str)
struct rb_call_cache buf
void * memcpy(void *__restrict__, const void *__restrict__, size_t)
_ssize_t ssize_t
__inline__ int
double exp(double)
#define PRI_EXTRA_MARK
Definition: sprintf.c:983
#define BSD__dtoa
Definition: sprintf.c:980
#define BSD__hdtoa
Definition: sprintf.c:981
unsigned char * _p
unsigned char * _p
size_t _w
Definition: vsnprintf.c:174
struct __sbuf _bf
int(* vwrite)()
Definition: vsnprintf.c:181
unsigned char * _base
size_t _size
Definition: vsnprintf.c:139
size_t iov_len
Definition: vsnprintf.c:228
const void * iov_base
Definition: vsnprintf.c:227
struct __siov * uio_iov
Definition: vsnprintf.c:231
int uio_iovcnt
Definition: vsnprintf.c:232
size_t uio_resid
Definition: vsnprintf.c:233
#define NULL
Definition: vsnprintf.c:123
#define __SRW
Definition: vsnprintf.c:191
#define __SNBF
Definition: vsnprintf.c:187
#define GETIOV(extra_work)
#define NIOV
#define SARG()
#define BUF
Definition: vsnprintf.c:511
#define is_digit(c)
Definition: vsnprintf.c:353
#define ZEROPAD
Definition: vsnprintf.c:536
#define to_char(n)
Definition: vsnprintf.c:354
#define FPT
Definition: vsnprintf.c:537
#define LONGINT
Definition: vsnprintf.c:529
#define HEXPREFIX
Definition: vsnprintf.c:526
#define LONG_MAX
Definition: vsnprintf.c:99
#define PRI_EXTRA_MARK_LEN
#define LONGDBL
Definition: vsnprintf.c:528
#define EOF
Definition: vsnprintf.c:203
#define PAD_L(howmany, with)
#define INTPTR_MASK
ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static ssize_t BSD_vfprintf(FILE *fp, const char *fmt0, va_list ap))
#define PADSIZE
#define PRINT(ptr, len)
#define __SSTR
Definition: vsnprintf.c:196
#define COPY(n)
#define PAD(howmany, with)
#define IS_PRI_EXTRA_MARK(s)
#define ALT
Definition: vsnprintf.c:525
#define u_long
Definition: vsnprintf.c:64
#define BSD__sferror(p)
Definition: vsnprintf.c:207
#define upper_hexdigits
Definition: vsnprintf.c:519
#define FLUSH()
#define to_digit(c)
Definition: vsnprintf.c:352
#define __SLBF
Definition: vsnprintf.c:186
#define UARG()
#define LADJUST
Definition: vsnprintf.c:527
struct __sFILE FILE
#define SHORTINT
Definition: vsnprintf.c:535
#define __SWR
Definition: vsnprintf.c:189
#define INTPTR_FLAG
#define lower_hexdigits
Definition: vsnprintf.c:516