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
104 #if defined(__hpux) && !defined(__GNUC__) && !defined(__STDC__)
111 #endif /* People who don't like const sys_error */
114 #if defined(__hpux) && !defined(__GNUC__) || defined(__DECC)
118 #if !defined(__CYGWIN32__) && defined(__hpux) && !defined(__GNUC__)
126 #if SIZEOF_LONG > SIZEOF_INT
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.
138 unsigned char *_base
;
144 * stdio state variables.
146 * The following always hold:
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
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.)
158 * _lbfsize is used only to make the inline line-buffered output stream
159 * code as compact as possible.
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 if and only if
165 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
167 * NB: see WARNING above before changing the layout of this structure!
171 typedef struct __sFILE
{
172 unsigned char *_p
; /* current position in (some) buffer */
174 size_t _r
; /* read space left for getc() */
176 size_t _w
; /* write space left for putc() */
177 short _flags
; /* flags, below; this FILE is free if 0 */
178 short _file
; /* fileno, if Unix descriptor, else -1 */
179 struct __sbuf _bf
; /* the buffer (at least 1 byte, if !NULL) */
181 size_t _lbfsize
; /* 0 or -_bf._size, for inline putc */
183 int (*vwrite
)(struct __sFILE
*, struct __suio
*);
184 const char *(*vextra
)(struct __sFILE
*, size_t, void*, long*, int);
188 #define __SLBF 0x0001 /* line buffered */
189 #define __SNBF 0x0002 /* unbuffered */
190 #define __SRD 0x0004 /* OK to read */
191 #define __SWR 0x0008 /* OK to write */
192 /* RD and WR are never simultaneously asserted */
193 #define __SRW 0x0010 /* open for reading & writing */
194 #define __SEOF 0x0020 /* found EOF */
195 #define __SERR 0x0040 /* found error */
196 #define __SMBF 0x0080 /* _buf is from malloc */
197 #define __SAPP 0x0100 /* fdopen()ed in append mode */
198 #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
199 #define __SOPT 0x0400 /* do fseek() optimisation */
200 #define __SNPT 0x0800 /* do not do fseek() optimisation */
201 #define __SOFF 0x1000 /* set if and only if _offset is in fact correct */
202 #define __SMOD 0x2000 /* true => fgetln modified _p text */
208 #define BSD__sfeof(p) (((p)->_flags & __SEOF) != 0)
209 #define BSD__sferror(p) (((p)->_flags & __SERR) != 0)
210 #define BSD__sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
211 #define BSD__sfileno(p) ((p)->_file)
216 #define feof(p) BSD__sfeof(p)
217 #define ferror(p) BSD__sferror(p)
218 #define clearerr(p) BSD__sclearerr(p)
221 #define fileno(p) BSD__sfileno(p)
226 * I/O descriptors for __sfvwrite().
229 const void *iov_base
;
233 struct __siov
*uio_iov
;
239 * Write some memory regions. Return zero on success, EOF on error.
241 * This routine is large and unsightly, but most of the ugliness due
242 * to the three different kinds of output buffering is handled here.
245 BSD__sfvwrite(register FILE *fp
, register struct __suio
*uio
)
248 register const char *p
;
249 register struct __siov
*iov
;
252 if ((len
= uio
->uio_resid
) == 0)
255 #define MIN(a, b) ((a) < (b) ? (a) : (b))
257 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
263 #define GETIOV(extra_work) \
267 len = iov->iov_len; \
270 if (fp
->_flags
& __SNBF
) {
271 /* fjc 7-31-97 Will never happen. We are working with
274 } else if ((fp
->_flags
& __SLBF
) == 0) {
276 * Fully buffered: fill partially full buffer, if any,
277 * and then flush. If there is no partial buffer, write
278 * one _bf._size byte chunk directly (without copying).
280 * String output is a special case: write as many bytes
281 * as fit, but pretend we wrote everything. This makes
282 * snprintf() return the number of bytes needed, rather
283 * than the number used, and avoids its write function
284 * (so that the write function can be invalid).
289 if (fp
->_flags
& __SSTR
) {
292 COPY(w
); /* copy MIN(fp->_w,len), */
295 w
= len
; /* but pretend copied all */
297 /* fjc 7-31-97 Will never happen. We are working with
303 } while ((uio
->uio_resid
-= w
) != 0);
305 /* fjc 7-31-97 Will never happen. We are working with
313 * Actual printf innards.
315 * This code is large and complicated...
319 * Flush out all the vectors defined by the given uio,
320 * then reset it so that it can be reused.
323 BSD__sprint(FILE *fp
, register struct __suio
*uio
)
327 if (uio
->uio_resid
== 0) {
331 err
= (*fp
->vwrite
)(fp
, uio
);
339 * Helper function for `fprintf to unbuffered unix file': creates a
340 * temporary buffer. We only work on write-only files; this avoids
341 * worries about ungetc buffers and so forth.
344 BSD__sbprintf(register FILE *fp
, const char *fmt
, va_list ap
)
346 /* We don't support files. */
352 * Macros for converting digits to letters and vice versa
354 #define to_digit(c) ((c) - '0')
355 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
356 #define to_char(n) (char)((n) + '0')
358 #ifdef _HAVE_SANE_QUAD_
360 * Convert an unsigned long long to ASCII for printf purposes, returning
361 * a pointer to the first character of the string representation.
362 * Octal numbers can be forced to have a leading zero; hex numbers
363 * use the given digits.
366 BSD__uqtoa(register u_quad_t val
, char *endp
, int base
, int octzero
, const char *xdigs
)
368 register char *cp
= endp
;
369 register quad_t sval
;
372 * Handle the three cases separately, in the hope of getting
373 * better/faster code.
377 if (val
< 10) { /* many numbers are 1 digit */
378 *--cp
= to_char(val
);
382 * On many machines, unsigned arithmetic is harder than
383 * signed arithmetic, so we do at most one unsigned mod and
384 * divide; this is sufficient to reduce the range of
385 * the incoming value to where signed arithmetic works.
387 if (val
> LLONG_MAX
) {
388 *--cp
= to_char(val
% 10);
393 *--cp
= to_char(sval
% 10);
400 *--cp
= to_char(val
& 7);
403 if (octzero
&& *cp
!= '0')
409 *--cp
= xdigs
[val
& 15];
418 break; /* fjc 7-31-97. Don't reference abort() here */
422 #endif /* _HAVE_SANE_QUAD_ */
425 * Convert an unsigned long to ASCII for printf purposes, returning
426 * a pointer to the first character of the string representation.
427 * Octal numbers can be forced to have a leading zero; hex numbers
428 * use the given digits.
431 BSD__ultoa(register u_long val
, char *endp
, int base
, int octzero
, const char *xdigs
)
433 register char *cp
= endp
;
437 * Handle the three cases separately, in the hope of getting
438 * better/faster code.
442 if (val
< 10) { /* many numbers are 1 digit */
443 *--cp
= to_char(val
);
447 * On many machines, unsigned arithmetic is harder than
448 * signed arithmetic, so we do at most one unsigned mod and
449 * divide; this is sufficient to reduce the range of
450 * the incoming value to where signed arithmetic works.
452 if (val
> LONG_MAX
) {
453 *--cp
= to_char(val
% 10);
458 *--cp
= to_char(sval
% 10);
465 *--cp
= to_char(val
& 7);
468 if (octzero
&& *cp
!= '0')
474 *--cp
= xdigs
[val
& 15];
483 break; /* fjc 7-31-97. Don't reference abort() here */
488 #ifdef FLOATING_POINT
491 /* #include "floatio.h" */
494 # if DBL_MAX_10_EXP > -DBL_MIN_10_EXP
495 # define MAXEXP (DBL_MAX_10_EXP)
497 # define MAXEXP (-DBL_MIN_10_EXP)
502 # define MAXFRACT (MAXEXP*10/3)
505 #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
508 static char *cvt(double, int, int, char *, int *, int, int *, char *);
509 static int exponent(char *, int, int);
511 #else /* no FLOATING_POINT */
515 #endif /* FLOATING_POINT */
517 #ifndef lower_hexdigits
518 # define lower_hexdigits "0123456789abcdef"
520 #ifndef upper_hexdigits
521 # define upper_hexdigits "0123456789ABCDEF"
525 * Flags used during conversion.
527 #define ALT 0x001 /* alternate form */
528 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
529 #define LADJUST 0x004 /* left adjustment */
530 #define LONGDBL 0x008 /* long double; unimplemented */
531 #define LONGINT 0x010 /* long integer */
533 #ifdef _HAVE_SANE_QUAD_
534 #define QUADINT 0x020 /* quad integer */
535 #endif /* _HAVE_SANE_QUAD_ */
537 #define SHORTINT 0x040 /* short integer */
538 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
539 #define FPT 0x100 /* Floating point number */
540 ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static ssize_t
BSD_vfprintf(FILE *fp
, const char *fmt0
, va_list ap
));
542 BSD_vfprintf(FILE *fp
, const char *fmt0
, va_list ap
)
544 #ifdef PRI_EXTRA_MARK
545 const int PRI_EXTRA_MARK_LEN
= rb_strlen_lit(PRI_EXTRA_MARK
);
547 register const char *fmt
; /* format string */
548 register int ch
; /* character from fmt */
549 register int n
; /* handy integer (short term usage) */
550 register const char *cp
;/* handy char pointer (short term usage) */
551 register struct __siov
*iovp
;/* for PRINT macro */
552 register int flags
; /* flags as above */
553 ssize_t ret
; /* return value accumulator */
554 int width
; /* width from format (%8d), or 0 */
555 int prec
; /* precision from format (%.3d), or -1 */
556 char sign
; /* sign prefix (' ', '+', '-', or \0) */
557 #ifdef FLOATING_POINT
558 char softsign
; /* temporary negative sign for floats */
559 double _double
= 0; /* double precision arguments %[eEfgG] */
560 int expt
; /* integer value of exponent */
561 int expsize
= 0; /* character count for expstr */
562 int ndig
= 0; /* actual number of digits returned by cvt */
563 int fprec
= 0; /* floating point precision */
564 char expstr
[7]; /* buffer for exponent string */
566 u_long
MAYBE_UNUSED(ulval
) = 0; /* integer arguments %[diouxX] */
567 #ifdef _HAVE_SANE_QUAD_
568 u_quad_t
MAYBE_UNUSED(uqval
); /* %q integers */
569 #endif /* _HAVE_SANE_QUAD_ */
570 int base
; /* base for [diouxX] conversion */
571 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
572 long fieldsz
; /* field size expanded by sign, etc */
573 long realsz
; /* field size expanded by dprec */
574 int size
; /* size of converted field or string */
575 const char *xdigs
= 0; /* digits for [xX] conversion */
577 struct __suio uio
; /* output information: summary */
578 struct __siov iov
[NIOV
];/* ... and individual io vectors */
579 char buf
[BUF
]; /* space for %c, %[diouxX], %[eEfgG] */
580 char ox
[4]; /* space for 0x hex-prefix, hexadecimal's 1. */
581 char *const ebuf
= buf
+ sizeof(buf
);
582 #if SIZEOF_LONG > SIZEOF_INT
587 * Choose PADSIZE to trade efficiency vs. size. If larger printf
588 * fields occur frequently, increase PADSIZE and make the initializers
591 #define PADSIZE 16 /* pad chunk size */
592 static const char blanks
[PADSIZE
] =
593 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
594 static const char zeroes
[PADSIZE
] =
595 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
598 * BEWARE, these `goto error' on error, and PAD uses `n'.
600 #define PRINT(ptr, len) { \
601 iovp->iov_base = (ptr); \
602 iovp->iov_len = (len); \
603 uio.uio_resid += (len); \
605 if (++uio.uio_iovcnt >= NIOV) { \
606 if (BSD__sprint(fp, &uio)) \
611 #define PAD(howmany, with) { \
612 if ((n = (howmany)) > 0) { \
613 while (n > PADSIZE) { \
614 PRINT((with), PADSIZE); \
620 #if SIZEOF_LONG > SIZEOF_INT
621 /* abandon if too larger padding */
622 #define PAD_L(howmany, with) { \
624 if ((long)((int)ln) != ln) { \
628 if (ln > 0) PAD((int)ln, (with)); \
631 #define PAD_L(howmany, with) PAD((howmany), (with))
634 if (uio.uio_resid && BSD__sprint(fp, &uio)) \
636 uio.uio_iovcnt = 0; \
641 * To extend shorts properly, we need both signed and unsigned
642 * argument extraction methods.
645 (flags&LONGINT ? va_arg(ap, long) : \
646 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
647 (long)va_arg(ap, int))
649 (flags&LONGINT ? va_arg(ap, u_long) : \
650 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
651 (u_long)va_arg(ap, u_int))
653 /* optimize fprintf(stderr) (and other unbuffered Unix files) */
654 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
656 return (BSD__sbprintf(fp
, fmt0
, ap
));
659 uio
.uio_iov
= iovp
= iov
;
666 * Scan the format for conversions (`%' character).
670 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
672 if ((nc
= fmt
- cp
) != 0) {
678 fmt
++; /* skip over '%' */
687 reswitch
: switch (ch
) {
690 * ``If the space and + flags both appear, the space
691 * flag will be ignored.''
702 * ``A negative field width argument is taken as a
703 * - flag followed by a positive field width.''
705 * They don't exclude field widths read from args.
707 if ((width
= va_arg(ap
, int)) >= 0)
718 if ((ch
= *fmt
++) == '*') {
720 prec
= n
< 0 ? -1 : n
;
724 while (is_digit(ch
)) {
725 n
= 10 * n
+ to_digit(ch
);
728 prec
= n
< 0 ? -1 : n
;
732 * ``Note that 0 is taken as a flag, not as the
733 * beginning of a field width.''
738 case '1': case '2': case '3': case '4':
739 case '5': case '6': case '7': case '8': case '9':
742 n
= 10 * n
+ to_digit(ch
);
744 } while (is_digit(ch
));
747 #ifdef FLOATING_POINT
755 #if SIZEOF_PTRDIFF_T == SIZEOF_LONG
758 #if SIZEOF_SIZE_T == SIZEOF_LONG
762 #ifdef _HAVE_SANE_QUAD_
773 #ifdef _HAVE_SANE_QUAD_
774 #if SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
777 #if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
783 #endif /* _HAVE_SANE_QUAD_ */
786 if (*fmt
== '3' && *(fmt
+ 1) == '2') {
790 #ifdef _HAVE_SANE_QUAD_
791 else if (*fmt
== '6' && *(fmt
+ 1) == '4') {
797 #if defined(_HAVE_SANE_QUAD_) && SIZEOF_SIZE_T == SIZEOF_LONG_LONG
806 *buf
= (char)va_arg(ap
, int);
811 #ifdef _HAVE_SANE_QUAD_
812 # define INTPTR_MASK (QUADINT|LONGINT|SHORTINT)
814 # define INTPTR_MASK (LONGINT|SHORTINT)
816 #if defined _HAVE_SANE_QUAD_ && SIZEOF_VOIDP == SIZEOF_LONG_LONG
817 # define INTPTR_FLAG QUADINT
818 #elif SIZEOF_VOIDP == SIZEOF_LONG
819 # define INTPTR_FLAG LONGINT
821 # define INTPTR_FLAG 0
823 #ifdef PRI_EXTRA_MARK
824 # define IS_PRI_EXTRA_MARK(s) \
825 (PRI_EXTRA_MARK_LEN < 1 || \
826 (*(s) == PRI_EXTRA_MARK[0] && \
827 (PRI_EXTRA_MARK_LEN == 1 || \
828 strncmp((s)+1, &PRI_EXTRA_MARK[1], \
829 PRI_EXTRA_MARK_LEN-1) == 0)))
831 # define PRI_EXTRA_MARK_LEN 0
832 # define IS_PRI_EXTRA_MARK(s) 1
834 if (fp
->vextra
&& (flags
& INTPTR_MASK
) == INTPTR_FLAG
&&
835 IS_PRI_EXTRA_MARK(fmt
)) {
836 fmt
+= PRI_EXTRA_MARK_LEN
;
838 #if defined _HAVE_SANE_QUAD_ && SIZEOF_VOIDP == SIZEOF_LONG_LONG
839 uqval
= va_arg(ap
, u_quad_t
);
840 cp
= (*fp
->vextra
)(fp
, sizeof(uqval
), &uqval
, &fieldsz
, sign
);
842 ulval
= va_arg(ap
, u_long
);
843 cp
= (*fp
->vextra
)(fp
, sizeof(ulval
), &ulval
, &fieldsz
, sign
);
847 if (prec
< 0) goto long_len
;
848 size
= fieldsz
< prec
? (int)fieldsz
: prec
;
857 #ifdef _HAVE_SANE_QUAD_
858 if (flags
& QUADINT
) {
859 uqval
= va_arg(ap
, quad_t
);
860 if ((quad_t
)uqval
< 0) {
861 uqval
= -(quad_t
)uqval
;
865 #endif /* _HAVE_SANE_QUAD_ */
868 if ((long)ulval
< 0) {
869 ulval
= (u_long
)(-(long)ulval
);
875 #ifdef FLOATING_POINT
884 case 'e': /* anomalous precision */
888 prec
= (prec
== -1) ?
889 DEFPREC
+ 1 : (fprec
= prec
+ 1);
892 case 'f': /* always print trailing zeroes */
901 fp_begin
: _double
= va_arg(ap
, double);
902 /* do this before tricky precision changes */
903 if (isinf(_double
)) {
910 if (isnan(_double
)) {
916 cp
= cvt(_double
, (prec
< MAXFRACT
? prec
: MAXFRACT
), flags
, &softsign
,
917 &expt
, ch
, &ndig
, buf
);
918 if (ch
== 'g' || ch
== 'G') {
919 if (expt
<= -4 || (expt
> prec
&& expt
> 1))
920 ch
= (ch
== 'g') ? 'e' : 'E';
924 if (ch
== 'a' || ch
== 'A') {
927 expsize
= exponent(expstr
, expt
, ch
+ 'p' - 'a');
929 size
= expsize
+ ndig
;
930 if (ndig
> 1 || flags
& ALT
)
931 ++size
; /* floating point */
933 else if (ch
<= 'e') { /* 'e' or 'E' fmt */
935 expsize
= exponent(expstr
, expt
, ch
);
936 size
= expsize
+ ndig
;
937 if (ndig
> 1 || flags
& ALT
)
939 } else if (ch
== 'f') { /* f fmt */
942 if (prec
|| flags
& ALT
)
944 } else if (!prec
) { /* "0" */
950 } else if (expt
>= ndig
) { /* fixed g fmt */
955 size
= ndig
+ (expt
> 0 ?
961 #endif /* FLOATING_POINT */
963 #ifdef _HAVE_SANE_QUAD_
965 *va_arg(ap
, quad_t
*) = ret
;
966 else if (flags
& LONGINT
)
967 #else /* _HAVE_SANE_QUAD_ */
969 #endif /* _HAVE_SANE_QUAD_ */
970 *va_arg(ap
, long *) = ret
;
971 else if (flags
& SHORTINT
)
972 *va_arg(ap
, short *) = (short)ret
;
974 *va_arg(ap
, int *) = (int)ret
;
975 continue; /* no output */
980 #ifdef _HAVE_SANE_QUAD_
982 uqval
= va_arg(ap
, u_quad_t
);
984 #endif /* _HAVE_SANE_QUAD_ */
990 * ``The argument shall be a pointer to void. The
991 * value of the pointer is converted to a sequence
992 * of printable characters, in an implementation-
996 prec
= (int)(sizeof(void*)*CHAR_BIT
/4);
998 uqval
= (u_quad_t
)va_arg(ap
, void *);
999 flags
= (flags
) | QUADINT
| HEXPREFIX
;
1001 ulval
= (u_long
)va_arg(ap
, void *);
1002 #ifdef _HAVE_SANE_QUAD_
1003 flags
= (flags
& ~QUADINT
) | HEXPREFIX
;
1004 #else /* _HAVE_SANE_QUAD_ */
1005 flags
= (flags
) | HEXPREFIX
;
1006 #endif /* _HAVE_SANE_QUAD_ */
1009 xdigs
= lower_hexdigits
;
1013 if ((cp
= va_arg(ap
, char *)) == NULL
)
1017 * can't use strlen; can only look for the
1018 * NUL in the first `prec' characters, and
1019 * strlen() will go further.
1021 const char *p
= (char *)memchr(cp
, 0, prec
);
1023 if (p
!= NULL
&& (p
- cp
) < prec
)
1024 size
= (int)(p
- cp
);
1029 fieldsz
= strlen(cp
);
1038 #ifdef _HAVE_SANE_QUAD_
1039 if (flags
& QUADINT
)
1040 uqval
= va_arg(ap
, u_quad_t
);
1042 #endif /* _HAVE_SANE_QUAD_ */
1047 xdigs
= upper_hexdigits
;
1050 xdigs
= lower_hexdigits
;
1052 #ifdef _HAVE_SANE_QUAD_
1053 if (flags
& QUADINT
)
1054 uqval
= va_arg(ap
, u_quad_t
);
1056 #endif /* _HAVE_SANE_QUAD_ */
1059 /* leading 0x/X only if non-zero */
1061 #ifdef _HAVE_SANE_QUAD_
1062 (flags
& QUADINT
? uqval
!= 0 : ulval
!= 0)
1063 #else /* _HAVE_SANE_QUAD_ */
1065 #endif /* _HAVE_SANE_QUAD_ */
1069 /* unsigned conversions */
1070 nosign
: sign
= '\0';
1072 * ``... diouXx conversions ... if a precision is
1073 * specified, the 0 flag will be ignored.''
1076 number
: if ((dprec
= prec
) >= 0)
1080 * ``The result of converting a zero value with an
1081 * explicit precision of zero is no characters.''
1085 #ifdef _HAVE_SANE_QUAD_
1086 if (flags
& QUADINT
) {
1087 if (uqval
!= 0 || prec
!= 0)
1088 cp
= BSD__uqtoa(uqval
, ebuf
, base
,
1089 flags
& ALT
, xdigs
);
1091 #else /* _HAVE_SANE_QUAD_ */
1092 #endif /* _HAVE_SANE_QUAD_ */
1094 if (ulval
!= 0 || prec
!= 0)
1095 cp
= BSD__ultoa(ulval
, ebuf
, base
,
1096 flags
& ALT
, xdigs
);
1098 size
= (int)(ebuf
- cp
);
1100 default: /* "%?" prints ?, unless ? is NUL */
1103 /* pretend it was %c with argument ch */
1112 * All reasonable formats wind up here. At this point, `cp'
1113 * points to a string which (if not flags&LADJUST) should be
1114 * padded out to `width' places. If flags&ZEROPAD, it should
1115 * first be prefixed by any sign or other prefix; otherwise,
1116 * it should be blank padded before the prefix is emitted.
1117 * After any left-hand padding and prefixing, emit zeroes
1118 * required by a decimal [diouxX] precision, then print the
1119 * string proper, then emit zeroes required by any leftover
1120 * floating precision; finally, if LADJUST, pad with blanks.
1122 * Compute actual size, so we know how much to pad.
1123 * fieldsz excludes decimal prec; realsz includes it.
1127 realsz
= dprec
> fieldsz
? dprec
: fieldsz
;
1130 if (flags
& HEXPREFIX
)
1133 /* right-adjusting blank padding */
1134 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1135 PAD_L(width
- realsz
, blanks
);
1141 if (flags
& HEXPREFIX
) {
1147 /* right-adjusting zero padding */
1148 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1149 PAD_L(width
- realsz
, zeroes
);
1151 /* leading zeroes from decimal precision */
1152 PAD_L(dprec
- fieldsz
, zeroes
);
1154 /* the string or number proper */
1155 #ifdef FLOATING_POINT
1156 if ((flags
& FPT
) == 0) {
1158 } else { /* glue together f_p fragments */
1159 if (flags
& HEXPREFIX
) {
1160 if (ndig
> 1 || flags
& ALT
) {
1164 if (ndig
> 0) PRINT(cp
, ndig
-1);
1167 PAD(fprec
-ndig
, zeroes
);
1168 PRINT(expstr
, expsize
);
1170 else if (ch
>= 'f') { /* 'f' or 'g' */
1172 /* kludge for __dtoa irregularity */
1174 (flags
& ALT
) == 0) {
1178 PAD((ndig
>= fprec
? ndig
- 1 : fprec
- (ch
!= 'f')),
1181 } else if (expt
== 0 && ndig
== 0 && (flags
& ALT
) == 0) {
1183 } else if (expt
<= 0) {
1188 PAD(fprec
- ndig
+ (ch
== 'f' ? expt
: 0), zeroes
);
1189 } else if (expt
>= ndig
) {
1191 PAD(expt
- ndig
, zeroes
);
1198 PRINT(cp
, ndig
-expt
);
1200 PAD(fprec
- ndig
+ (ch
== 'f' ? expt
: 0), zeroes
);
1202 } else { /* 'e' or 'E' */
1203 if (ndig
> 1 || flags
& ALT
) {
1207 if (_double
/*|| flags & ALT == 0*/) {
1209 } else /* 0.[0..] */
1210 /* __dtoa irregularity */
1211 PAD(ndig
- 1, zeroes
);
1212 if (flags
& ALT
) PAD(fprec
- ndig
- 1, zeroes
);
1215 PRINT(expstr
, expsize
);
1221 /* left-adjusting padding (always blank) */
1222 if (flags
& LADJUST
)
1223 PAD_L(width
- realsz
, blanks
);
1225 /* finally, adjust ret */
1226 ret
+= width
> realsz
? width
: realsz
;
1228 FLUSH(); /* copy out the I/O vectors */
1233 return (BSD__sferror(fp
) ? EOF
: ret
);
1237 #ifdef FLOATING_POINT
1239 extern char *BSD__dtoa(double, int, int, int *, int *, char **);
1240 extern char *BSD__hdtoa(double, const char *, int, int *, int *, char **);
1243 cvt(double value
, int ndigits
, int flags
, char *sign
, int *decpt
, int ch
, int *length
, char *buf
)
1246 char *digits
, *bp
, *rve
;
1256 } else if (value
== 0.0 && signbit(value
)) {
1261 if (ch
== 'a' || ch
=='A') {
1262 digits
= BSD__hdtoa(value
,
1263 ch
== 'a' ? lower_hexdigits
: upper_hexdigits
,
1264 ndigits
, decpt
, &dsgn
, &rve
);
1267 digits
= BSD__dtoa(value
, mode
, ndigits
, decpt
, &dsgn
, &rve
);
1269 buf
[0] = 0; /* rve - digits may be 0 */
1270 memcpy(buf
, digits
, rve
- digits
);
1272 rve
= buf
+ (rve
- digits
);
1274 if (flags
& ALT
) { /* Print trailing zeros */
1275 bp
= digits
+ ndigits
;
1277 if (*digits
== '0' && value
)
1278 *decpt
= -ndigits
+ 1;
1284 *length
= (int)(rve
- digits
);
1289 exponent(char *p0
, int exp
, int fmtch
)
1291 register char *p
, *t
;
1292 char expbuf
[2 + (MAXEXP
< 1000 ? 3 : MAXEXP
< 10000 ? 4 : 5)]; /* >= 2 + ceil(log10(MAXEXP)) */
1302 t
= expbuf
+ sizeof(expbuf
);
1305 *--t
= to_char(exp
% 10);
1306 } while ((exp
/= 10) > 9);
1307 *--t
= to_char(exp
);
1308 for (; t
< expbuf
+ sizeof(expbuf
); *p
++ = *t
++);
1311 if (fmtch
& 15) *p
++ = '0'; /* other than p or P */
1312 *p
++ = to_char(exp
);
1314 return (int)(p
- p0
);
1316 #endif /* FLOATING_POINT */