2 * Copyright (c) 1983, 1995, 1996 Eric P. Allman
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/ioctl.h>
39 #include <sys/param.h>
47 * SNPRINTF, VSNPRINTF and friends
49 * These versions have been grabbed off the net. They have been
50 * cleaned up to compile properly and support for most of the Single Unix
51 * Specification has been added. Remaining unimplemented features are:
53 * 1. No locale support: the radix character is always '.' and the '
54 * (single quote) format flag is ignored.
56 * 2. No support for the "%n" format specification.
58 * 3. No support for wide characters ("lc" and "ls" formats).
60 * 4. No support for "long double" ("Lf" and related formats).
62 * 5. Space and '#' flags are not implemented.
65 * The result values of these functions are not the same across different
66 * platforms. This implementation is compatible with the Single Unix Spec:
68 * 1. -1 is returned only if processing is abandoned due to an invalid
69 * parameter, such as incorrect format string. (Although not required by
70 * the spec, this happens only when no characters have yet been transmitted
71 * to the destination.)
73 * 2. For snprintf and sprintf, 0 is returned if str == NULL or count == 0;
74 * no data has been stored.
76 * 3. Otherwise, the number of bytes actually transmitted to the destination
77 * is returned (excluding the trailing '\0' for snprintf and sprintf).
79 * For snprintf with nonzero count, the result cannot be more than count-1
80 * (a trailing '\0' is always stored); it is not possible to distinguish
81 * buffer overrun from exact fit. This is unlike some implementations that
82 * return the number of bytes that would have been needed for the complete
86 /**************************************************************
88 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
89 * A bombproof version of doprnt (dopr) included.
90 * Sigh. This sort of thing is always nasty do deal with. Note that
91 * the version here does not include floating point. (now it does ... tgl)
92 **************************************************************/
94 /* Prevent recursion */
102 /* Info about where the formatted output is going */
105 char *bufptr; /* next buffer output position */
106 char *bufstart; /* first buffer element */
107 char *bufend; /* last buffer element, or NULL */
108 /* bufend == NULL is for sprintf, where we assume buf is big enough */
109 FILE *stream; /* eventual output destination, or NULL */
110 int nchars; /* # chars already sent to stream */
114 * Info about the type and value of a formatting parameter. Note that we
115 * don't currently support "long double", "wint_t", or "wchar_t *" data,
116 * nor the '%n' formatting code; else we'd need more types. Also, at this
117 * level we need not worry about signed vs unsigned values.
139 static void flushbuffer(PrintfTarget *target);
140 static int dopr(PrintfTarget *target, const char *format, va_list args);
144 pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
148 if (str == NULL || count == 0)
150 target.bufstart = target.bufptr = str;
151 target.bufend = str + count - 1;
152 target.stream = NULL;
153 /* target.nchars is unused in this case */
154 if (dopr(&target, fmt, args))
156 *(target.bufptr) = '\0';
157 errno = EINVAL; /* bad format */
160 *(target.bufptr) = '\0';
161 return target.bufptr - target.bufstart;
165 pg_snprintf(char *str, size_t count, const char *fmt,...)
171 len = pg_vsnprintf(str, count, fmt, args);
177 pg_vsprintf(char *str, const char *fmt, va_list args)
183 target.bufstart = target.bufptr = str;
184 target.bufend = NULL;
185 target.stream = NULL;
186 /* target.nchars is unused in this case */
187 if (dopr(&target, fmt, args))
189 *(target.bufptr) = '\0';
190 errno = EINVAL; /* bad format */
193 *(target.bufptr) = '\0';
194 return target.bufptr - target.bufstart;
198 pg_sprintf(char *str, const char *fmt,...)
204 len = pg_vsprintf(str, fmt, args);
210 pg_vfprintf(FILE *stream, const char *fmt, va_list args)
213 char buffer[1024]; /* size is arbitrary */
220 target.bufstart = target.bufptr = buffer;
221 target.bufend = buffer + sizeof(buffer) - 1;
222 target.stream = stream;
224 if (dopr(&target, fmt, args))
226 errno = EINVAL; /* bad format */
229 /* dump any remaining buffer contents */
230 flushbuffer(&target);
231 return target.nchars;
235 pg_fprintf(FILE *stream, const char *fmt,...)
241 len = pg_vfprintf(stream, fmt, args);
247 pg_printf(const char *fmt,...)
253 len = pg_vfprintf(stdout, fmt, args);
258 /* call this only when stream is defined */
260 flushbuffer(PrintfTarget *target)
262 size_t nc = target->bufptr - target->bufstart;
265 target->nchars += fwrite(target->bufstart, 1, nc, target->stream);
266 target->bufptr = target->bufstart;
270 static void fmtstr(char *value, int leftjust, int minlen, int maxwidth,
271 int pointflag, PrintfTarget *target);
272 static void fmtptr(void *value, PrintfTarget *target);
273 static void fmtint(int64 value, char type, int forcesign,
274 int leftjust, int minlen, int zpad, int precision, int pointflag,
275 PrintfTarget *target);
276 static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target);
277 static void fmtfloat(double value, char type, int forcesign,
278 int leftjust, int minlen, int zpad, int precision, int pointflag,
279 PrintfTarget *target);
280 static void dostr(const char *str, int slen, PrintfTarget *target);
281 static void dopr_outch(int c, PrintfTarget *target);
282 static int adjust_sign(int is_negative, int forcesign, int *signvalue);
283 static void adjust_padlen(int minlen, int vallen, int leftjust, int *padlen);
284 static void leading_pad(int zpad, int *signvalue, int *padlen,
285 PrintfTarget *target);
286 static void trailing_pad(int *padlen, PrintfTarget *target);
290 * dopr(): poor man's version of doprintf
293 dopr(PrintfTarget *target, const char *format, va_list args)
295 const char *format_start = format;
298 bool have_non_dollar;
317 PrintfArgType argtypes[NL_ARGMAX + 1];
318 PrintfArgValue argvalues[NL_ARGMAX + 1];
321 * Parse the format string to determine whether there are %n$ format
322 * specs, and identify the types and order of the format parameters.
324 have_dollar = have_non_dollar = false;
326 MemSet(argtypes, 0, sizeof(argtypes));
328 while ((ch = *format++) != '\0')
332 longflag = longlongflag = pointflag = 0;
338 break; /* illegal, but we don't complain */
354 accum = accum * 10 + (ch - '0');
362 have_non_dollar = true; /* multiple stars */
368 if (accum <= 0 || accum > NL_ARGMAX)
372 if (argtypes[accum] &&
373 argtypes[accum] != ATYPE_INT)
375 argtypes[accum] = ATYPE_INT;
376 last_dollar = Max(last_dollar, accum);
390 #if SIZEOF_SIZE_T == 8
391 #ifdef HAVE_LONG_INT_64
393 #elif defined(HAVE_LONG_LONG_INT_64)
396 #error "Don't know how to print 64bit integers"
399 /* assume size_t is same size as int */
417 atype = ATYPE_LONGLONG;
422 if (argtypes[fmtpos] &&
423 argtypes[fmtpos] != atype)
425 argtypes[fmtpos] = atype;
426 last_dollar = Max(last_dollar, fmtpos);
429 have_non_dollar = true;
434 if (argtypes[fmtpos] &&
435 argtypes[fmtpos] != ATYPE_INT)
437 argtypes[fmtpos] = ATYPE_INT;
438 last_dollar = Max(last_dollar, fmtpos);
441 have_non_dollar = true;
447 if (argtypes[fmtpos] &&
448 argtypes[fmtpos] != ATYPE_CHARPTR)
450 argtypes[fmtpos] = ATYPE_CHARPTR;
451 last_dollar = Max(last_dollar, fmtpos);
454 have_non_dollar = true;
463 if (argtypes[fmtpos] &&
464 argtypes[fmtpos] != ATYPE_DOUBLE)
466 argtypes[fmtpos] = ATYPE_DOUBLE;
467 last_dollar = Max(last_dollar, fmtpos);
470 have_non_dollar = true;
477 * If we finish the spec with afterstar still set, there's a
478 * non-dollar star in there.
481 have_non_dollar = true;
484 /* Per spec, you use either all dollar or all not. */
485 if (have_dollar && have_non_dollar)
489 * In dollar mode, collect the arguments in physical order.
491 for (i = 1; i <= last_dollar; i++)
496 return -1; /* invalid format */
498 argvalues[i].i = va_arg(args, int);
501 argvalues[i].l = va_arg(args, long);
504 argvalues[i].ll = va_arg(args, int64);
507 argvalues[i].d = va_arg(args, double);
510 argvalues[i].cptr = va_arg(args, char *);
516 * At last we can parse the format for real.
518 format = format_start;
519 while ((ch = *format++) != '\0')
523 dopr_outch(ch, target);
526 fieldwidth = precision = zpad = leftjust = forcesign = 0;
527 longflag = longlongflag = pointflag = 0;
529 have_star = afterstar = false;
533 break; /* illegal, but we don't complain */
543 /* set zero padding if no nonzero digits yet */
544 if (accum == 0 && !pointflag)
556 accum = accum * 10 + (ch - '0');
569 /* process value after reading n$ */
574 /* fetch and process value now */
575 int starval = va_arg(args, int);
588 fieldwidth = starval;
592 fieldwidth = -fieldwidth;
602 /* fetch and process star value */
603 int starval = argvalues[accum].i;
616 fieldwidth = starval;
620 fieldwidth = -fieldwidth;
636 #if SIZEOF_SIZE_T == 8
637 #ifdef HAVE_LONG_INT_64
639 #elif defined(HAVE_LONG_LONG_INT_64)
642 #error "Don't know how to print 64bit integers"
645 /* assume size_t is same size as int */
664 numvalue = argvalues[fmtpos].ll;
666 numvalue = argvalues[fmtpos].l;
668 numvalue = argvalues[fmtpos].i;
673 numvalue = va_arg(args, int64);
675 numvalue = va_arg(args, long);
677 numvalue = va_arg(args, int);
679 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
680 precision, pointflag, target);
696 numvalue = (uint64) argvalues[fmtpos].ll;
698 numvalue = (unsigned long) argvalues[fmtpos].l;
700 numvalue = (unsigned int) argvalues[fmtpos].i;
705 numvalue = (uint64) va_arg(args, int64);
707 numvalue = (unsigned long) va_arg(args, long);
709 numvalue = (unsigned int) va_arg(args, int);
711 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
712 precision, pointflag, target);
723 cvalue = (unsigned char) argvalues[fmtpos].i;
725 cvalue = (unsigned char) va_arg(args, int);
726 fmtchar(cvalue, leftjust, fieldwidth, target);
737 strvalue = argvalues[fmtpos].cptr;
739 strvalue = va_arg(args, char *);
740 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
744 /* fieldwidth/leftjust are ignored ... */
746 strvalue = argvalues[fmtpos].cptr;
748 strvalue = va_arg(args, char *);
749 fmtptr((void *) strvalue, target);
764 fvalue = argvalues[fmtpos].d;
766 fvalue = va_arg(args, double);
767 fmtfloat(fvalue, ch, forcesign, leftjust,
769 precision, pointflag,
773 dopr_outch('%', target);
782 pg_strnlen(const char *str, size_t maxlen)
786 while (maxlen-- > 0 && *p)
792 fmtstr(char *value, int leftjust, int minlen, int maxwidth,
793 int pointflag, PrintfTarget *target)
796 vallen; /* amount to pad */
799 * If a maxwidth (precision) is specified, we must not fetch more bytes
803 vallen = pg_strnlen(value, maxwidth);
805 vallen = strlen(value);
807 adjust_padlen(minlen, vallen, leftjust, &padlen);
811 dopr_outch(' ', target);
815 dostr(value, vallen, target);
817 trailing_pad(&padlen, target);
821 fmtptr(void *value, PrintfTarget *target)
826 /* we rely on regular C library's sprintf to do the basic conversion */
827 vallen = sprintf(convert, "%p", value);
829 dostr(convert, vallen, target);
833 fmtint(int64 value, char type, int forcesign, int leftjust,
834 int minlen, int zpad, int precision, int pointflag,
835 PrintfTarget *target)
839 const char *cvt = "0123456789abcdef";
843 int padlen = 0; /* amount to pad */
844 int zeropad; /* extra leading zeroes */
866 cvt = "0123456789ABCDEF";
871 return; /* keep compiler quiet */
875 if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
879 * SUS: the result of converting 0 with an explicit precision of 0 is no
882 if (value == 0 && pointflag && precision == 0)
886 /* make integer string */
887 uint64 uvalue = (uint64) value;
891 convert[vallen++] = cvt[uvalue % base];
892 uvalue = uvalue / base;
896 zeropad = Max(0, precision - vallen);
898 adjust_padlen(minlen, vallen + zeropad, leftjust, &padlen);
900 leading_pad(zpad, &signvalue, &padlen, target);
902 while (zeropad-- > 0)
903 dopr_outch('0', target);
906 dopr_outch(convert[--vallen], target);
908 trailing_pad(&padlen, target);
912 fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
914 int padlen = 0; /* amount to pad */
916 adjust_padlen(minlen, 1, leftjust, &padlen);
920 dopr_outch(' ', target);
924 dopr_outch(value, target);
926 trailing_pad(&padlen, target);
930 fmtfloat(double value, char type, int forcesign, int leftjust,
931 int minlen, int zpad, int precision, int pointflag,
932 PrintfTarget *target)
938 int padlen = 0; /* amount to pad */
940 /* we rely on regular C library's sprintf to do the basic conversion */
942 sprintf(fmt, "%%.%d%c", precision, type);
944 sprintf(fmt, "%%%c", type);
946 if (adjust_sign((value < 0), forcesign, &signvalue))
949 vallen = sprintf(convert, fmt, value);
951 adjust_padlen(minlen, vallen, leftjust, &padlen);
953 leading_pad(zpad, &signvalue, &padlen, target);
955 dostr(convert, vallen, target);
957 trailing_pad(&padlen, target);
961 dostr(const char *str, int slen, PrintfTarget *target)
967 if (target->bufend != NULL)
968 avail = target->bufend - target->bufptr;
973 /* buffer full, can we dump to stream? */
974 if (target->stream == NULL)
975 return; /* no, lose the data */
979 avail = Min(avail, slen);
980 memmove(target->bufptr, str, avail);
981 target->bufptr += avail;
988 dopr_outch(int c, PrintfTarget *target)
990 if (target->bufend != NULL && target->bufptr >= target->bufend)
992 /* buffer full, can we dump to stream? */
993 if (target->stream == NULL)
994 return; /* no, lose the data */
997 *(target->bufptr++) = c;
1002 adjust_sign(int is_negative, int forcesign, int *signvalue)
1016 adjust_padlen(int minlen, int vallen, int leftjust, int *padlen)
1018 *padlen = minlen - vallen;
1022 *padlen = -(*padlen);
1027 leading_pad(int zpad, int *signvalue, int *padlen, PrintfTarget *target)
1029 if (*padlen > 0 && zpad)
1033 dopr_outch(*signvalue, target);
1039 dopr_outch(zpad, target);
1043 while (*padlen > (*signvalue != 0))
1045 dopr_outch(' ', target);
1050 dopr_outch(*signvalue, target);
1053 else if (*padlen < 0)
1060 trailing_pad(int *padlen, PrintfTarget *target)
1064 dopr_outch(' ', target);