1 /* snprintf.c - Formatted, length-limited print to a string */
13 #include <netinet/in.h>
15 #if defined(AFS_AIX32_ENV) || defined(AFS_SUN_ENV) || defined(AFS_XBSD_ENV)
16 #include <sys/socket.h>
28 /* Generate an ASCII representation of an integer <val>, as follows:
29 * <base> indicates the base to be used (2-36)
30 * <uc> is nonzero if letter digits should be uppercase
31 * <prec> is the minimum number of digits
32 * The resulting number is stored in <buf>, which must be long enough
33 * to receive it. The minimum length is <prec> or ceil(log{base}(val)),
34 * whichever is larger, plus room for a trailing NUL.
37 mkint(char *buf, afs_uintmax_t val, int base, int uc, unsigned prec)
43 val = (val - dig) / base;
54 for (i = 0; i < (len + 1) / 2; i++) {
56 buf[i] = buf[len - i - 1];
57 buf[len - i - 1] = dig;
63 /* This function is a mostly-complete implementation of snprintf,
64 * with the following features:
66 * - Actually obeys the length limit, which (unfortunately) many
67 * implementations of snprintf do not.
69 * - Supports all the standard format specifiers for integers
70 * (d, i, o, u, x, X), floating-point values (f, e, E, g, G),
71 * and strings and characters (c, s, %), plus a few unusual
72 * but useful ones described below.
74 * - Supports all the standard flags (-, 0, +, space, #). These
75 * flags are ignored if used when they are not appropriate.
77 * - Supports the standard size modifiers for short (h), long (h),
78 * and double (L) arguments. These modifiers are ignored if used
79 * when they are not appropriate.
81 * - Supports minimum field width and precision, where appropriate,
82 * including the use of '*' to specify a value given as an argument
83 * instead of in the format string. There is a maximum precision
86 * - At present, the 'p' specifier for printing pointers is not
87 * implemented, because it is inherently non-portable and thus
88 * can be implemented correctly only by the compiler's run-time
91 * - Floating-point specifier (%e, %f, %g) are implemented by
92 * calling the standard sprintf, and thus may be unsafe.
94 * - The '%...$' notation is used primarily when the format string
95 * is specified by the user, who knows but cannot change the order
96 * of the arguments. Such usage is inherently dangerous and
97 * insecure; thus, it is not supported.
99 * The custom format specifier '%I' is supported. This specifier
100 * takes as its argument an unsigned long integer containing an
101 * IPv4 address in network byte order. The address is rendered
102 * either as a hostname or as a dotted quad, as follows:
104 * - If precision is nonzero or unspecified, a hostname lookup
105 * is attempted; if it is successful, the hostname is printed.
106 * If the hostname lookup fails, the address is printed in
107 * dotted-quad notation.
109 * - If precision is explicitly specified as 0, then the hostname
110 * lookup is skipped, and dotted-quad notation is always used.
112 * - If a hostname is to be printed:
113 * + The precision controls the maximum number of characters
114 * printed, as with %s.
115 * + If the '#' flag is specified, any letters in the hostname
116 * will be forced to lower case before printing.
117 * + If the '+' flag is specified, any letters in the hostname
118 * will be forced to upper case before printing. If both
119 * '#' and '+' are given, the '+' flag will be ignored.
120 * + The '0' and ' ' flags have no effect.
122 * - If a dotted quad is to be printed:
123 * + The precision has no effect; dotted quads are always
124 * 7 to 12 characters in length, depending on the value
125 * to be printed and the format flags used.
126 * + If the '0' flag is given, each field (byte) of the address
127 * will be padded with '0' on the left to three digits.
128 * + If the ' ' flag is given, each field (byte) of the address
129 * will be padded with spaces on the left to three digits. If
130 * both '0' and ' ' are given, the ' ' flag will be ignored.
131 * + The '#' and '+' flags have no effect.
134 afs_vsnprintf(char *p, size_t avail, const char *fmt, va_list ap)
136 unsigned int width, precision, haveprec, len;
137 int ljust, plsign, spsign, altform, zfill;
138 int hflag, lflag, count, *countp, j;
139 char *x, *y, xbuf[MAXPREC + 21], fbuf[20];
144 afs_intmax_t *llcountp;
151 while (*fmt && avail) {
159 /** Found a format specifier **/
160 ljust = plsign = spsign = altform = zfill = 0;
161 width = precision = haveprec = 0;
165 /* parse format flags */
171 continue; /* left justify */
175 continue; /* use + or - */
179 continue; /* use space or - */
183 continue; /* alternate form */
187 continue; /* pad with 0 */
194 /* parse minimum width */
196 width = va_arg(ap, int);
199 while (isdigit(*fmt)) {
200 width = (width * 10) + (*fmt - '0');
204 /* parse precision */
209 precision = va_arg(ap, int);
212 while (isdigit(*fmt)) {
213 precision = (precision * 10) + (*fmt - '0');
218 /* parse size flags */
224 continue; /* short argument */
228 continue; /* long argument */
232 continue; /* long long argument */
239 /* parse format specifier */
248 FVAL = va_arg(ap, double);
249 sprintf(fbuf, "%%%s%s.*L%c", plsign ? "+" : (spsign ? " " : ""),
250 altform ? "#" : "", fmt[-1]);
253 if (precision > MAXPREC)
255 sprintf(xbuf, fbuf, precision, FVAL);
261 case 'd': /* signed decimal integer */
263 SVAL = va_arg(ap, afs_intmax_t);
265 SVAL = va_arg(ap, long);
267 SVAL = va_arg(ap, int);
269 SVAL = va_arg(ap, int);
270 UVAL = (SVAL < 0) ? -SVAL : SVAL;
283 precision = width - !!xbuf[0];
286 if (precision < 1 + !!xbuf[0])
287 precision = 1 + !!xbuf[0];
289 if (precision > MAXPREC)
292 mkint(xbuf + 1, UVAL, 10, 0, precision);
298 case 'o': /* unsigned octal integer */
300 UVAL = va_arg(ap, afs_uintmax_t);
302 UVAL = va_arg(ap, unsigned long);
304 UVAL = va_arg(ap, unsigned int);
306 UVAL = va_arg(ap, unsigned int);
316 if (precision > MAXPREC)
319 mkint(xbuf + 1, UVAL, 8, 0, precision);
320 x = xbuf + (xbuf[1] == '0' || !altform);
324 case 'u': /* unsigned decimal integer */
326 UVAL = va_arg(ap, afs_uintmax_t);
328 UVAL = va_arg(ap, unsigned long);
330 UVAL = va_arg(ap, unsigned int);
332 UVAL = va_arg(ap, unsigned int);
340 if (precision > MAXPREC)
343 mkint(xbuf, UVAL, 10, 0, precision);
349 case 'X': /* unsigned hexadecimal integer */
351 UVAL = va_arg(ap, afs_uintmax_t);
353 UVAL = va_arg(ap, unsigned long);
355 UVAL = va_arg(ap, unsigned int);
357 UVAL = va_arg(ap, unsigned int);
368 if (precision > MAXPREC)
371 mkint(xbuf + 2, UVAL, 16, 0, precision);
372 x = xbuf + ((altform && UVAL) ? 0 : 2);
376 case '%': /* literal % */
383 case 'c': /* character */
384 xbuf[0] = va_arg(ap, int);
390 case 's': /* string */
391 x = va_arg(ap, char *);
395 if (haveprec && precision < len)
399 case 'I': /* IP address:
400 * value is provided as a network-order unsigned long integer
401 * precision specifies max hostname length, as for %s
402 * if precision is explicitly 0, no hostname lookup is done
403 * if 0fill specified, IPaddr fields are 0-filled to 3 digits
404 * if spsign specified, IPaddr fields are space-filled to 3 digits
406 UVAL = va_arg(ap, unsigned long);
408 if (haveprec && !precision)
411 he = gethostbyaddr((char *)&ia, 4, AF_INET);
415 if (haveprec && precision < len)
429 x = "%03u.%03u.%03u.%03u";
431 x = "%3u.%3u.%3u.%3u";
435 sprintf(xbuf, x, (UVAL & 0xff000000) >> 24,
436 (UVAL & 0x00ff0000) >> 16, (UVAL & 0x0000ff00) >> 8,
437 (UVAL & 0x000000ff));
443 case 'n': /* report count so far */
445 llcountp = va_arg(ap, afs_intmax_t *);
446 *llcountp = (long long)count;
448 lcountp = va_arg(ap, long *);
449 *lcountp = (long)count;
451 hcountp = va_arg(ap, short *);
452 *hcountp = (short)count;
454 countp = va_arg(ap, int *);
459 default: /* unknown specifier */
463 /* render the results */
494 afs_snprintf(char *p, size_t avail, const char *fmt, ...)
500 result = afs_vsnprintf(p, avail, fmt, ap);
505 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV)) || !defined(HAVE_VSNPRINTF)
509 vsnprintf(char *p, size_t avail, const char *fmt, va_list ap)
512 vsnprintf(char *p, unsigned int avail, char *fmt, va_list ap)
516 result = afs_vsnprintf(p, avail, fmt, ap);
521 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */
523 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV)) || !defined(HAVE_SNPRINTF)
527 snprintf(char *p, size_t avail, const char *fmt, ...)
530 snprintf(char *p, unsigned int avail, char *fmt, ...)
537 result = afs_vsnprintf(p, avail, fmt, ap);
543 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */