1 /* snprintf.c - Formatted, length-limited print to a string */
14 #include <netinet/in.h>
19 #if defined(AFS_AIX32_ENV) || defined(AFS_SUN_ENV) || defined(AFS_XBSD_ENV) || defined(AFS_HPUX_ENV)
20 #include <sys/socket.h>
32 /* Generate an ASCII representation of an integer <val>, as follows:
33 * <base> indicates the base to be used (2-36)
34 * <uc> is nonzero if letter digits should be uppercase
35 * <prec> is the minimum number of digits
36 * The resulting number is stored in <buf>, which must be long enough
37 * to receive it. The minimum length is <prec> or ceil(log{base}(val)),
38 * whichever is larger, plus room for a trailing NUL.
41 mkint(char *buf, afs_uintmax_t val, int base, int uc, unsigned prec)
47 val = (val - dig) / base;
58 for (i = 0; i < (len + 1) / 2; i++) {
60 buf[i] = buf[len - i - 1];
61 buf[len - i - 1] = dig;
67 /* This function is a mostly-complete implementation of snprintf,
68 * with the following features:
70 * - Actually obeys the length limit, which (unfortunately) many
71 * implementations of snprintf do not.
73 * - Supports all the standard format specifiers for integers
74 * (d, i, o, u, x, X), floating-point values (f, e, E, g, G),
75 * and strings and characters (c, s, %), plus a few unusual
76 * but useful ones described below.
78 * - Supports all the standard flags (-, 0, +, space, #). These
79 * flags are ignored if used when they are not appropriate.
81 * - Supports the standard size modifiers for short (h), long (h),
82 * and double (L) arguments. These modifiers are ignored if used
83 * when they are not appropriate.
85 * - Supports minimum field width and precision, where appropriate,
86 * including the use of '*' to specify a value given as an argument
87 * instead of in the format string. There is a maximum precision
90 * - At present, the 'p' specifier for printing pointers is not
91 * implemented, because it is inherently non-portable and thus
92 * can be implemented correctly only by the compiler's run-time
95 * - Floating-point specifier (%e, %f, %g) are implemented by
96 * calling the standard sprintf, and thus may be unsafe.
98 * - The '%...$' notation is used primarily when the format string
99 * is specified by the user, who knows but cannot change the order
100 * of the arguments. Such usage is inherently dangerous and
101 * insecure; thus, it is not supported.
103 * The custom format specifier '%I' is supported. This specifier
104 * takes as its argument an unsigned long integer containing an
105 * IPv4 address in network byte order. The address is rendered
106 * either as a hostname or as a dotted quad, as follows:
108 * - If precision is nonzero or unspecified, a hostname lookup
109 * is attempted; if it is successful, the hostname is printed.
110 * If the hostname lookup fails, the address is printed in
111 * dotted-quad notation.
113 * - If precision is explicitly specified as 0, then the hostname
114 * lookup is skipped, and dotted-quad notation is always used.
116 * - If a hostname is to be printed:
117 * + The precision controls the maximum number of characters
118 * printed, as with %s.
119 * + If the '#' flag is specified, any letters in the hostname
120 * will be forced to lower case before printing.
121 * + If the '+' flag is specified, any letters in the hostname
122 * will be forced to upper case before printing. If both
123 * '#' and '+' are given, the '+' flag will be ignored.
124 * + The '0' and ' ' flags have no effect.
126 * - If a dotted quad is to be printed:
127 * + The precision has no effect; dotted quads are always
128 * 7 to 12 characters in length, depending on the value
129 * to be printed and the format flags used.
130 * + If the '0' flag is given, each field (byte) of the address
131 * will be padded with '0' on the left to three digits.
132 * + If the ' ' flag is given, each field (byte) of the address
133 * will be padded with spaces on the left to three digits. If
134 * both '0' and ' ' are given, the ' ' flag will be ignored.
135 * + The '#' and '+' flags have no effect.
138 afs_vsnprintf(char *p, size_t avail, const char *fmt, va_list ap)
140 unsigned int width, precision, haveprec, len;
141 int ljust, plsign, spsign, altform, zfill;
142 int hflag, lflag, count, *countp, j;
143 char *x, *y, xbuf[MAXPREC + 21], fbuf[20];
148 afs_intmax_t *llcountp;
155 while (*fmt && avail) {
163 /** Found a format specifier **/
164 ljust = plsign = spsign = altform = zfill = 0;
165 width = precision = haveprec = 0;
169 /* parse format flags */
175 continue; /* left justify */
179 continue; /* use + or - */
183 continue; /* use space or - */
187 continue; /* alternate form */
191 continue; /* pad with 0 */
198 /* parse minimum width */
200 width = va_arg(ap, int);
203 while (isdigit(*fmt)) {
204 width = (width * 10) + (*fmt - '0');
208 /* parse precision */
213 precision = va_arg(ap, int);
216 while (isdigit(*fmt)) {
217 precision = (precision * 10) + (*fmt - '0');
222 /* parse size flags */
228 continue; /* short argument */
232 continue; /* long argument */
236 continue; /* long long argument */
243 /* parse format specifier */
252 FVAL = va_arg(ap, double);
253 sprintf(fbuf, "%%%s%s.*L%c", plsign ? "+" : (spsign ? " " : ""),
254 altform ? "#" : "", fmt[-1]);
257 if (precision > MAXPREC)
259 sprintf(xbuf, fbuf, precision, FVAL);
265 case 'd': /* signed decimal integer */
267 SVAL = va_arg(ap, afs_intmax_t);
269 SVAL = va_arg(ap, long);
271 SVAL = va_arg(ap, int);
273 SVAL = va_arg(ap, int);
274 UVAL = (SVAL < 0) ? -SVAL : SVAL;
287 precision = width - !!xbuf[0];
290 if (precision < 1 + !!xbuf[0])
291 precision = 1 + !!xbuf[0];
293 if (precision > MAXPREC)
296 mkint(xbuf + 1, UVAL, 10, 0, precision);
302 case 'o': /* unsigned octal integer */
304 UVAL = va_arg(ap, afs_uintmax_t);
306 UVAL = va_arg(ap, unsigned long);
308 UVAL = va_arg(ap, unsigned int);
310 UVAL = va_arg(ap, unsigned int);
320 if (precision > MAXPREC)
323 mkint(xbuf + 1, UVAL, 8, 0, precision);
324 x = xbuf + (xbuf[1] == '0' || !altform);
328 case 'u': /* unsigned decimal integer */
330 UVAL = va_arg(ap, afs_uintmax_t);
332 UVAL = va_arg(ap, unsigned long);
334 UVAL = va_arg(ap, unsigned int);
336 UVAL = va_arg(ap, unsigned int);
344 if (precision > MAXPREC)
347 mkint(xbuf, UVAL, 10, 0, precision);
353 case 'X': /* unsigned hexadecimal integer */
355 UVAL = va_arg(ap, afs_uintmax_t);
357 UVAL = va_arg(ap, unsigned long);
359 UVAL = va_arg(ap, unsigned int);
361 UVAL = va_arg(ap, unsigned int);
372 if (precision > MAXPREC)
375 mkint(xbuf + 2, UVAL, 16, 0, precision);
376 x = xbuf + ((altform && UVAL) ? 0 : 2);
380 case '%': /* literal % */
387 case 'c': /* character */
388 xbuf[0] = va_arg(ap, int);
394 case 's': /* string */
395 x = va_arg(ap, char *);
399 if (haveprec && precision < len)
403 case 'I': /* IP address:
404 * value is provided as a network-order unsigned long integer
405 * precision specifies max hostname length, as for %s
406 * if precision is explicitly 0, no hostname lookup is done
407 * if 0fill specified, IPaddr fields are 0-filled to 3 digits
408 * if spsign specified, IPaddr fields are space-filled to 3 digits
410 UVAL = va_arg(ap, unsigned long);
412 if (haveprec && !precision)
415 he = gethostbyaddr((char *)&ia, 4, AF_INET);
419 if (haveprec && precision < len)
433 x = "%03u.%03u.%03u.%03u";
435 x = "%3u.%3u.%3u.%3u";
439 sprintf(xbuf, x, (UVAL & 0xff000000) >> 24,
440 (UVAL & 0x00ff0000) >> 16, (UVAL & 0x0000ff00) >> 8,
441 (UVAL & 0x000000ff));
447 case 'n': /* report count so far */
449 llcountp = va_arg(ap, afs_intmax_t *);
450 *llcountp = (afs_intmax_t)count;
452 lcountp = va_arg(ap, long *);
453 *lcountp = (long)count;
455 hcountp = va_arg(ap, short *);
456 *hcountp = (short)count;
458 countp = va_arg(ap, int *);
463 default: /* unknown specifier */
467 /* render the results */
498 afs_snprintf(char *p, size_t avail, const char *fmt, ...)
504 result = afs_vsnprintf(p, avail, fmt, ap);
509 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV)) || !defined(HAVE_VSNPRINTF)
513 vsnprintf(char *p, size_t avail, const char *fmt, va_list ap)
516 vsnprintf(char *p, unsigned int avail, char *fmt, va_list ap)
520 result = afs_vsnprintf(p, avail, fmt, ap);
525 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */
528 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV)) || !defined(HAVE_SNPRINTF)
532 snprintf(char *p, size_t avail, const char *fmt, ...)
535 snprintf(char *p, unsigned int avail, char *fmt, ...)
542 result = afs_vsnprintf(p, avail, fmt, ap);
548 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */
549 #endif /* AFS_NT40_ENV */