2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
37 * paragraph 3 above is now null and void.
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.
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.
52 * snprintf should be the only externally visible item.
54 * As of 7-31-97 FLOATING_POINT is NOT provided. The code is somewhat
55 * non-portable, so it is disabled.
58 /* Define FLOATING_POINT to get floating point. */
60 #define FLOATING_POINT
63 #include <sys/types.h>
64 #define u_long unsigned long
65 #define u_short unsigned short
66 #define u_int unsigned int
68 #if !defined(HAVE_STDARG_PROTOTYPES)
70 #define HAVE_STDARG_PROTOTYPES 1
75 #if defined(HAVE_STDARG_PROTOTYPES)
88 #define _BSD_VA_LIST_ va_list
98 /* assuming 32bit(2's complement) long */
99 # define LONG_MAX 2147483647
107 #endif /* People who don't like const sys_error */
115 #if SIZEOF_LONG > SIZEOF_INT
120 * NB: to fit things in six character monocase externals, the stdio
121 * code uses the prefix `__s' for stdio objects, typically followed
122 * by a three-character attempt at a mnemonic.
127 unsigned char *_base
;
133 * stdio state variables.
135 * The following always hold:
137 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
138 * _lbfsize is -_bf._size, else _lbfsize is 0
139 * if _flags&__SRD, _w is 0
140 * if _flags&__SWR, _r is 0
142 * This ensures that the getc and putc macros (or inline functions) never
143 * try to write or read from a file that is in `read' or `write' mode.
144 * (Moreover, they can, and do, automatically switch from read mode to
145 * write mode, and back, on "r+" and "w+" files.)
147 * _lbfsize is used only to make the inline line-buffered output stream
148 * code as compact as possible.
150 * _ub, _up, and _ur are used when ungetc() pushes back more characters
151 * than fit in the current _bf, or when ungetc() pushes back a character
152 * that does not match the previous one in _bf. When this happens,
153 * _ub._base becomes non-nil (i.e., a stream has ungetc() data if and only if
154 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
156 * NB: see WARNING above before changing the layout of this structure!
160 typedef struct __sFILE
{
161 unsigned char *_p
; /* current position in (some) buffer */
163 size_t _r
; /* read space left for getc() */
165 size_t _w
; /* write space left for putc() */
166 short _flags
; /* flags, below; this FILE is free if 0 */
167 short _file
; /* fileno, if Unix descriptor, else -1 */
168 struct __sbuf _bf
; /* the buffer (at least 1 byte, if !NULL) */
170 size_t _lbfsize
; /* 0 or -_bf._size, for inline putc */
172 int (*vwrite
)(struct __sFILE
*, struct __suio
*);
173 const char *(*vextra
)(struct __sFILE
*, size_t, void*, long*, int);
177 #define __SLBF 0x0001 /* line buffered */
178 #define __SNBF 0x0002 /* unbuffered */
179 #define __SRD 0x0004 /* OK to read */
180 #define __SWR 0x0008 /* OK to write */
181 /* RD and WR are never simultaneously asserted */
182 #define __SRW 0x0010 /* open for reading & writing */
183 #define __SEOF 0x0020 /* found EOF */
184 #define __SERR 0x0040 /* found error */
185 #define __SMBF 0x0080 /* _buf is from malloc */
186 #define __SAPP 0x0100 /* fdopen()ed in append mode */
187 #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
188 #define __SOPT 0x0400 /* do fseek() optimisation */
189 #define __SNPT 0x0800 /* do not do fseek() optimisation */
190 #define __SOFF 0x1000 /* set if and only if _offset is in fact correct */
191 #define __SMOD 0x2000 /* true => fgetln modified _p text */
197 #define BSD__sfeof(p) (((p)->_flags & __SEOF) != 0)
198 #define BSD__sferror(p) (((p)->_flags & __SERR) != 0)
199 #define BSD__sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
200 #define BSD__sfileno(p) ((p)->_file)
205 #define feof(p) BSD__sfeof(p)
206 #define ferror(p) BSD__sferror(p)
207 #define clearerr(p) BSD__sclearerr(p)
210 #define fileno(p) BSD__sfileno(p)
215 * I/O descriptors for __sfvwrite().
218 const void *iov_base
;
222 struct __siov
*uio_iov
;
228 * Write some memory regions. Return zero on success, EOF on error.
230 * This routine is large and unsightly, but most of the ugliness due
231 * to the three different kinds of output buffering is handled here.
234 BSD__sfvwrite(register FILE *fp
, register struct __suio
*uio
)
237 register const char *p
;
238 register struct __siov
*iov
;
241 if ((len
= uio
->uio_resid
) == 0)
243 #define MIN(a, b) ((a) < (b) ? (a) : (b))
244 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
250 #define GETIOV(extra_work) \
254 len = iov->iov_len; \
257 if (fp
->_flags
& __SNBF
) {
258 /* fjc 7-31-97 Will never happen. We are working with
261 } else if ((fp
->_flags
& __SLBF
) == 0) {
263 * Fully buffered: fill partially full buffer, if any,
264 * and then flush. If there is no partial buffer, write
265 * one _bf._size byte chunk directly (without copying).
267 * String output is a special case: write as many bytes
268 * as fit, but pretend we wrote everything. This makes
269 * snprintf() return the number of bytes needed, rather
270 * than the number used, and avoids its write function
271 * (so that the write function can be invalid).
276 if (fp
->_flags
& __SSTR
) {
279 COPY(w
); /* copy MIN(fp->_w,len), */
282 w
= len
; /* but pretend copied all */
284 /* fjc 7-31-97 Will never happen. We are working with
290 } while ((uio
->uio_resid
-= w
) != 0);
292 /* fjc 7-31-97 Will never happen. We are working with
300 * Actual printf innards.
302 * This code is large and complicated...
306 * Flush out all the vectors defined by the given uio,
307 * then reset it so that it can be reused.
310 BSD__sprint(FILE *fp
, register struct __suio
*uio
)
314 if (uio
->uio_resid
== 0) {
318 err
= (*fp
->vwrite
)(fp
, uio
);
326 * Helper function for `fprintf to unbuffered unix file': creates a
327 * temporary buffer. We only work on write-only files; this avoids
328 * worries about ungetc buffers and so forth.
331 BSD__sbprintf(register FILE *fp
, const char *fmt
, va_list ap
)
333 /* We don't support files. */
339 * Macros for converting digits to letters and vice versa
341 #define to_digit(c) ((c) - '0')
342 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
343 #define to_char(n) (char)((n) + '0')
345 #ifdef _HAVE_SANE_QUAD_
347 * Convert an unsigned long long to ASCII for printf purposes, returning
348 * a pointer to the first character of the string representation.
349 * Octal numbers can be forced to have a leading zero; hex numbers
350 * use the given digits.
353 BSD__uqtoa(register u_quad_t val
, char *endp
, int base
, int octzero
, const char *xdigs
)
355 register char *cp
= endp
;
356 register quad_t sval
;
359 * Handle the three cases separately, in the hope of getting
360 * better/faster code.
364 if (val
< 10) { /* many numbers are 1 digit */
365 *--cp
= to_char(val
);
369 * On many machines, unsigned arithmetic is harder than
370 * signed arithmetic, so we do at most one unsigned mod and
371 * divide; this is sufficient to reduce the range of
372 * the incoming value to where signed arithmetic works.
374 if (val
> LLONG_MAX
) {
375 *--cp
= to_char(val
% 10);
380 *--cp
= to_char(sval
% 10);
387 *--cp
= to_char(val
& 7);
390 if (octzero
&& *cp
!= '0')
396 *--cp
= xdigs
[val
& 15];
405 break; /* fjc 7-31-97. Don't reference abort() here */
409 #endif /* _HAVE_SANE_QUAD_ */
412 * Convert an unsigned long to ASCII for printf purposes, returning
413 * a pointer to the first character of the string representation.
414 * Octal numbers can be forced to have a leading zero; hex numbers
415 * use the given digits.
418 BSD__ultoa(register u_long val
, char *endp
, int base
, int octzero
, const char *xdigs
)
420 register char *cp
= endp
;
424 * Handle the three cases separately, in the hope of getting
425 * better/faster code.
429 if (val
< 10) { /* many numbers are 1 digit */
430 *--cp
= to_char(val
);
434 * On many machines, unsigned arithmetic is harder than
435 * signed arithmetic, so we do at most one unsigned mod and
436 * divide; this is sufficient to reduce the range of
437 * the incoming value to where signed arithmetic works.
439 if (val
> LONG_MAX
) {
440 *--cp
= to_char(val
% 10);
445 *--cp
= to_char(sval
% 10);
452 *--cp
= to_char(val
& 7);
455 if (octzero
&& *cp
!= '0')
461 *--cp
= xdigs
[val
& 15];
470 break; /* fjc 7-31-97. Don't reference abort() here */
475 #ifdef FLOATING_POINT
478 /* #include "floatio.h" */
481 # if DBL_MAX_10_EXP > -DBL_MIN_10_EXP
482 # define MAXEXP (DBL_MAX_10_EXP)
484 # define MAXEXP (-DBL_MIN_10_EXP)
489 # define MAXFRACT (MAXEXP*10/3)
492 #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
495 static char *cvt(double, int, int, char *, int *, int, int *, char *);
496 static int exponent(char *, int, int);
498 #else /* no FLOATING_POINT */
502 #endif /* FLOATING_POINT */
504 #ifndef lower_hexdigits
505 # define lower_hexdigits "0123456789abcdef"
507 #ifndef upper_hexdigits
508 # define upper_hexdigits "0123456789ABCDEF"
512 * Flags used during conversion.
514 #define ALT 0x001 /* alternate form */
515 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
516 #define LADJUST 0x004 /* left adjustment */
517 #define LONGDBL 0x008 /* long double; unimplemented */
518 #define LONGINT 0x010 /* long integer */
520 #ifdef _HAVE_SANE_QUAD_
521 #define QUADINT 0x020 /* quad integer */
522 #endif /* _HAVE_SANE_QUAD_ */
524 #define SHORTINT 0x040 /* short integer */
525 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
526 #define FPT 0x100 /* Floating point number */
527 ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static ssize_t
BSD_vfprintf(FILE *fp
, const char *fmt0
, va_list ap
));
529 BSD_vfprintf(FILE *fp
, const char *fmt0
, va_list ap
)
531 #ifdef PRI_EXTRA_MARK
532 const int PRI_EXTRA_MARK_LEN
= rb_strlen_lit(PRI_EXTRA_MARK
);
534 register const char *fmt
; /* format string */
535 register int ch
; /* character from fmt */
536 register int n
; /* handy integer (short term usage) */
537 register const char *cp
;/* handy char pointer (short term usage) */
538 register struct __siov
*iovp
;/* for PRINT macro */
539 register int flags
; /* flags as above */
540 ssize_t ret
; /* return value accumulator */
541 int width
; /* width from format (%8d), or 0 */
542 int prec
; /* precision from format (%.3d), or -1 */
543 char sign
; /* sign prefix (' ', '+', '-', or \0) */
544 #ifdef FLOATING_POINT
545 char softsign
; /* temporary negative sign for floats */
546 double _double
= 0; /* double precision arguments %[eEfgG] */
547 int expt
; /* integer value of exponent */
548 int expsize
= 0; /* character count for expstr */
549 int ndig
= 0; /* actual number of digits returned by cvt */
550 int fprec
= 0; /* floating point precision */
551 char expstr
[7]; /* buffer for exponent string */
553 u_long
MAYBE_UNUSED(ulval
) = 0; /* integer arguments %[diouxX] */
554 #ifdef _HAVE_SANE_QUAD_
555 u_quad_t
MAYBE_UNUSED(uqval
) = 0; /* %q integers */
556 #endif /* _HAVE_SANE_QUAD_ */
557 int base
; /* base for [diouxX] conversion */
558 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
559 long fieldsz
; /* field size expanded by sign, etc */
560 long realsz
; /* field size expanded by dprec */
561 int size
; /* size of converted field or string */
562 const char *xdigs
= 0; /* digits for [xX] conversion */
564 struct __suio uio
; /* output information: summary */
565 struct __siov iov
[NIOV
];/* ... and individual io vectors */
566 char buf
[BUF
]; /* space for %c, %[diouxX], %[eEfgG] */
567 char ox
[4]; /* space for 0x hex-prefix, hexadecimal's 1. */
568 char *const ebuf
= buf
+ sizeof(buf
);
569 #if SIZEOF_LONG > SIZEOF_INT
574 * Choose PADSIZE to trade efficiency vs. size. If larger printf
575 * fields occur frequently, increase PADSIZE and make the initializers
578 #define PADSIZE 16 /* pad chunk size */
579 static const char blanks
[PADSIZE
] =
580 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
581 static const char zeroes
[PADSIZE
] =
582 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
585 * BEWARE, these `goto error' on error, and PAD uses `n'.
587 #define PRINT(ptr, len) { \
588 iovp->iov_base = (ptr); \
589 iovp->iov_len = (len); \
590 uio.uio_resid += (len); \
592 if (++uio.uio_iovcnt >= NIOV) { \
593 if (BSD__sprint(fp, &uio)) \
598 #define PAD(howmany, with) { \
599 if ((n = (howmany)) > 0) { \
600 while (n > PADSIZE) { \
601 PRINT((with), PADSIZE); \
607 #if SIZEOF_LONG > SIZEOF_INT
608 /* abandon if too larger padding */
609 #define PAD_L(howmany, with) { \
611 if ((long)((int)ln) != ln) { \
615 if (ln > 0) PAD((int)ln, (with)); \
618 #define PAD_L(howmany, with) PAD((howmany), (with))
621 if (uio.uio_resid && BSD__sprint(fp, &uio)) \
623 uio.uio_iovcnt = 0; \
628 * To extend shorts properly, we need both signed and unsigned
629 * argument extraction methods.
632 (flags&LONGINT ? va_arg(ap, long) : \
633 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
634 (long)va_arg(ap, int))
636 (flags&LONGINT ? va_arg(ap, u_long) : \
637 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
638 (u_long)va_arg(ap, u_int))
640 /* optimize fprintf(stderr) (and other unbuffered Unix files) */
641 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
643 return (BSD__sbprintf(fp
, fmt0
, ap
));
646 uio
.uio_iov
= iovp
= iov
;
653 * Scan the format for conversions (`%' character).
657 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
659 if ((nc
= fmt
- cp
) != 0) {
665 fmt
++; /* skip over '%' */
674 reswitch
: switch (ch
) {
677 * ``If the space and + flags both appear, the space
678 * flag will be ignored.''
689 * ``A negative field width argument is taken as a
690 * - flag followed by a positive field width.''
692 * They don't exclude field widths read from args.
694 if ((width
= va_arg(ap
, int)) >= 0)
705 if ((ch
= *fmt
++) == '*') {
707 prec
= n
< 0 ? -1 : n
;
711 while (is_digit(ch
)) {
712 n
= 10 * n
+ to_digit(ch
);
715 prec
= n
< 0 ? -1 : n
;
719 * ``Note that 0 is taken as a flag, not as the
720 * beginning of a field width.''
725 case '1': case '2': case '3': case '4':
726 case '5': case '6': case '7': case '8': case '9':
729 n
= 10 * n
+ to_digit(ch
);
731 } while (is_digit(ch
));
734 #ifdef FLOATING_POINT
742 #if SIZEOF_PTRDIFF_T == SIZEOF_LONG
745 #if SIZEOF_SIZE_T == SIZEOF_LONG
749 #ifdef _HAVE_SANE_QUAD_
760 #ifdef _HAVE_SANE_QUAD_
761 #if SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
764 #if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
770 #endif /* _HAVE_SANE_QUAD_ */
773 if (*fmt
== '3' && *(fmt
+ 1) == '2') {
777 #ifdef _HAVE_SANE_QUAD_
778 else if (*fmt
== '6' && *(fmt
+ 1) == '4') {
784 #if defined(_HAVE_SANE_QUAD_) && SIZEOF_SIZE_T == SIZEOF_LONG_LONG
793 *buf
= (char)va_arg(ap
, int);
798 #ifdef _HAVE_SANE_QUAD_
799 # define INTPTR_MASK (QUADINT|LONGINT|SHORTINT)
801 # define INTPTR_MASK (LONGINT|SHORTINT)
803 #if defined _HAVE_SANE_QUAD_ && SIZEOF_VOIDP == SIZEOF_LONG_LONG
804 # define INTPTR_FLAG QUADINT
805 #elif SIZEOF_VOIDP == SIZEOF_LONG
806 # define INTPTR_FLAG LONGINT
808 # define INTPTR_FLAG 0
810 #ifdef PRI_EXTRA_MARK
811 # define IS_PRI_EXTRA_MARK(s) \
812 (PRI_EXTRA_MARK_LEN < 1 || \
813 (*(s) == PRI_EXTRA_MARK[0] && \
814 (PRI_EXTRA_MARK_LEN == 1 || \
815 strncmp((s)+1, &PRI_EXTRA_MARK[1], \
816 PRI_EXTRA_MARK_LEN-1) == 0)))
818 # define PRI_EXTRA_MARK_LEN 0
819 # define IS_PRI_EXTRA_MARK(s) 1
821 if (fp
->vextra
&& (flags
& INTPTR_MASK
) == INTPTR_FLAG
&&
822 IS_PRI_EXTRA_MARK(fmt
)) {
823 fmt
+= PRI_EXTRA_MARK_LEN
;
825 #if defined _HAVE_SANE_QUAD_ && SIZEOF_VOIDP == SIZEOF_LONG_LONG
826 uqval
= va_arg(ap
, u_quad_t
);
827 cp
= (*fp
->vextra
)(fp
, sizeof(uqval
), &uqval
, &fieldsz
, sign
);
829 ulval
= va_arg(ap
, u_long
);
830 cp
= (*fp
->vextra
)(fp
, sizeof(ulval
), &ulval
, &fieldsz
, sign
);
834 if (prec
< 0) goto long_len
;
835 size
= fieldsz
< prec
? (int)fieldsz
: prec
;
844 #ifdef _HAVE_SANE_QUAD_
845 if (flags
& QUADINT
) {
846 uqval
= va_arg(ap
, quad_t
);
847 if ((quad_t
)uqval
< 0) {
848 uqval
= -(quad_t
)uqval
;
852 #endif /* _HAVE_SANE_QUAD_ */
855 if ((long)ulval
< 0) {
856 ulval
= (u_long
)(-(long)ulval
);
862 #ifdef FLOATING_POINT
871 case 'e': /* anomalous precision */
875 prec
= (prec
== -1) ?
876 DEFPREC
+ 1 : (fprec
= prec
+ 1);
879 case 'f': /* always print trailing zeroes */
888 fp_begin
: _double
= va_arg(ap
, double);
889 /* do this before tricky precision changes */
890 if (isinf(_double
)) {
897 if (isnan(_double
)) {
903 cp
= cvt(_double
, (prec
< MAXFRACT
? prec
: MAXFRACT
), flags
, &softsign
,
904 &expt
, ch
, &ndig
, buf
);
905 if (ch
== 'g' || ch
== 'G') {
906 if (expt
<= -4 || (expt
> prec
&& expt
> 1))
907 ch
= (ch
== 'g') ? 'e' : 'E';
911 if (ch
== 'a' || ch
== 'A') {
914 expsize
= exponent(expstr
, expt
, ch
+ 'p' - 'a');
916 size
= expsize
+ ndig
;
917 if (ndig
> 1 || flags
& ALT
)
918 ++size
; /* floating point */
920 else if (ch
<= 'e') { /* 'e' or 'E' fmt */
922 expsize
= exponent(expstr
, expt
, ch
);
923 size
= expsize
+ ndig
;
924 if (ndig
> 1 || flags
& ALT
)
926 } else if (ch
== 'f') { /* f fmt */
929 if (prec
|| flags
& ALT
)
931 } else if (!prec
) { /* "0" */
937 } else if (expt
>= ndig
) { /* fixed g fmt */
942 size
= ndig
+ (expt
> 0 ?
948 #endif /* FLOATING_POINT */
950 #ifdef _HAVE_SANE_QUAD_
952 *va_arg(ap
, quad_t
*) = ret
;
953 else if (flags
& LONGINT
)
954 #else /* _HAVE_SANE_QUAD_ */
956 #endif /* _HAVE_SANE_QUAD_ */
957 *va_arg(ap
, long *) = ret
;
958 else if (flags
& SHORTINT
)
959 *va_arg(ap
, short *) = (short)ret
;
961 *va_arg(ap
, int *) = (int)ret
;
962 continue; /* no output */
967 #ifdef _HAVE_SANE_QUAD_
969 uqval
= va_arg(ap
, u_quad_t
);
971 #endif /* _HAVE_SANE_QUAD_ */
977 * ``The argument shall be a pointer to void. The
978 * value of the pointer is converted to a sequence
979 * of printable characters, in an implementation-
983 prec
= (int)(sizeof(void*)*CHAR_BIT
/4);
985 uqval
= (u_quad_t
)va_arg(ap
, void *);
986 flags
= (flags
) | QUADINT
| HEXPREFIX
;
988 ulval
= (u_long
)va_arg(ap
, void *);
989 #ifdef _HAVE_SANE_QUAD_
990 flags
= (flags
& ~QUADINT
) | HEXPREFIX
;
991 #else /* _HAVE_SANE_QUAD_ */
992 flags
= (flags
) | HEXPREFIX
;
993 #endif /* _HAVE_SANE_QUAD_ */
996 xdigs
= lower_hexdigits
;
1000 if ((cp
= va_arg(ap
, char *)) == NULL
)
1004 * can't use strlen; can only look for the
1005 * NUL in the first `prec' characters, and
1006 * strlen() will go further.
1008 const char *p
= (char *)memchr(cp
, 0, prec
);
1010 if (p
!= NULL
&& (p
- cp
) < prec
)
1011 size
= (int)(p
- cp
);
1016 fieldsz
= strlen(cp
);
1025 #ifdef _HAVE_SANE_QUAD_
1026 if (flags
& QUADINT
)
1027 uqval
= va_arg(ap
, u_quad_t
);
1029 #endif /* _HAVE_SANE_QUAD_ */
1034 xdigs
= upper_hexdigits
;
1037 xdigs
= lower_hexdigits
;
1039 #ifdef _HAVE_SANE_QUAD_
1040 if (flags
& QUADINT
)
1041 uqval
= va_arg(ap
, u_quad_t
);
1043 #endif /* _HAVE_SANE_QUAD_ */
1046 /* leading 0x/X only if non-zero */
1048 #ifdef _HAVE_SANE_QUAD_
1049 (flags
& QUADINT
? uqval
!= 0 : ulval
!= 0)
1050 #else /* _HAVE_SANE_QUAD_ */
1052 #endif /* _HAVE_SANE_QUAD_ */
1056 /* unsigned conversions */
1057 nosign
: sign
= '\0';
1059 * ``... diouXx conversions ... if a precision is
1060 * specified, the 0 flag will be ignored.''
1063 number
: if ((dprec
= prec
) >= 0)
1067 * ``The result of converting a zero value with an
1068 * explicit precision of zero is no characters.''
1072 #ifdef _HAVE_SANE_QUAD_
1073 if (flags
& QUADINT
) {
1074 if (uqval
!= 0 || prec
!= 0)
1075 cp
= BSD__uqtoa(uqval
, ebuf
, base
,
1076 flags
& ALT
, xdigs
);
1078 #else /* _HAVE_SANE_QUAD_ */
1079 #endif /* _HAVE_SANE_QUAD_ */
1081 if (ulval
!= 0 || prec
!= 0)
1082 cp
= BSD__ultoa(ulval
, ebuf
, base
,
1083 flags
& ALT
, xdigs
);
1085 size
= (int)(ebuf
- cp
);
1087 default: /* "%?" prints ?, unless ? is NUL */
1090 /* pretend it was %c with argument ch */
1099 * All reasonable formats wind up here. At this point, `cp'
1100 * points to a string which (if not flags&LADJUST) should be
1101 * padded out to `width' places. If flags&ZEROPAD, it should
1102 * first be prefixed by any sign or other prefix; otherwise,
1103 * it should be blank padded before the prefix is emitted.
1104 * After any left-hand padding and prefixing, emit zeroes
1105 * required by a decimal [diouxX] precision, then print the
1106 * string proper, then emit zeroes required by any leftover
1107 * floating precision; finally, if LADJUST, pad with blanks.
1109 * Compute actual size, so we know how much to pad.
1110 * fieldsz excludes decimal prec; realsz includes it.
1114 realsz
= dprec
> fieldsz
? dprec
: fieldsz
;
1117 if (flags
& HEXPREFIX
)
1120 /* right-adjusting blank padding */
1121 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1122 PAD_L(width
- realsz
, blanks
);
1128 if (flags
& HEXPREFIX
) {
1134 /* right-adjusting zero padding */
1135 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1136 PAD_L(width
- realsz
, zeroes
);
1138 /* leading zeroes from decimal precision */
1139 PAD_L(dprec
- fieldsz
, zeroes
);
1141 /* the string or number proper */
1142 #ifdef FLOATING_POINT
1143 if ((flags
& FPT
) == 0) {
1145 } else { /* glue together f_p fragments */
1146 if (flags
& HEXPREFIX
) {
1147 if (ndig
> 1 || flags
& ALT
) {
1151 if (ndig
> 0) PRINT(cp
, ndig
-1);
1154 PAD(fprec
-ndig
, zeroes
);
1155 PRINT(expstr
, expsize
);
1157 else if (ch
>= 'f') { /* 'f' or 'g' */
1159 /* kludge for __dtoa irregularity */
1161 (flags
& ALT
) == 0) {
1165 PAD((ndig
>= fprec
? ndig
- 1 : fprec
- (ch
!= 'f')),
1168 } else if (expt
== 0 && ndig
== 0 && (flags
& ALT
) == 0) {
1170 } else if (expt
<= 0) {
1175 PAD(fprec
- ndig
+ (ch
== 'f' ? expt
: 0), zeroes
);
1176 } else if (expt
>= ndig
) {
1178 PAD(expt
- ndig
, zeroes
);
1185 PRINT(cp
, ndig
-expt
);
1187 PAD(fprec
- ndig
+ (ch
== 'f' ? expt
: 0), zeroes
);
1189 } else { /* 'e' or 'E' */
1190 if (ndig
> 1 || flags
& ALT
) {
1194 if (_double
/*|| flags & ALT == 0*/) {
1196 } else /* 0.[0..] */
1197 /* __dtoa irregularity */
1198 PAD(ndig
- 1, zeroes
);
1199 if (flags
& ALT
) PAD(fprec
- ndig
- 1, zeroes
);
1202 PRINT(expstr
, expsize
);
1208 /* left-adjusting padding (always blank) */
1209 if (flags
& LADJUST
)
1210 PAD_L(width
- realsz
, blanks
);
1212 /* finally, adjust ret */
1213 ret
+= width
> realsz
? width
: realsz
;
1215 FLUSH(); /* copy out the I/O vectors */
1220 return (BSD__sferror(fp
) ? EOF
: ret
);
1224 #ifdef FLOATING_POINT
1226 extern char *BSD__dtoa(double, int, int, int *, int *, char **);
1227 extern char *BSD__hdtoa(double, const char *, int, int *, int *, char **);
1230 cvt(double value
, int ndigits
, int flags
, char *sign
, int *decpt
, int ch
, int *length
, char *buf
)
1233 char *digits
, *bp
, *rve
;
1243 } else if (value
== 0.0 && signbit(value
)) {
1248 if (ch
== 'a' || ch
=='A') {
1249 digits
= BSD__hdtoa(value
,
1250 ch
== 'a' ? lower_hexdigits
: upper_hexdigits
,
1251 ndigits
, decpt
, &dsgn
, &rve
);
1254 digits
= BSD__dtoa(value
, mode
, ndigits
, decpt
, &dsgn
, &rve
);
1256 buf
[0] = 0; /* rve - digits may be 0 */
1257 memcpy(buf
, digits
, rve
- digits
);
1258 rve
= buf
+ (rve
- digits
);
1261 if (flags
& ALT
) { /* Print trailing zeros */
1262 bp
= digits
+ ndigits
;
1264 if (*digits
== '0' && value
)
1265 *decpt
= -ndigits
+ 1;
1271 *length
= (int)(rve
- digits
);
1276 exponent(char *p0
, int exp
, int fmtch
)
1278 register char *p
, *t
;
1279 char expbuf
[2 + (MAXEXP
< 1000 ? 3 : MAXEXP
< 10000 ? 4 : 5)]; /* >= 2 + ceil(log10(MAXEXP)) */
1289 t
= expbuf
+ sizeof(expbuf
);
1292 *--t
= to_char(exp
% 10);
1293 } while ((exp
/= 10) > 9);
1294 *--t
= to_char(exp
);
1295 for (; t
< expbuf
+ sizeof(expbuf
); *p
++ = *t
++);
1298 if (fmtch
& 15) *p
++ = '0'; /* other than p or P */
1299 *p
++ = to_char(exp
);
1301 return (int)(p
- p0
);
1303 #endif /* FLOATING_POINT */