1 /* snprintf.c - Formatted, length-limited print to a string */
15 #include <netinet/in.h>
20 #if defined(AFS_AIX32_ENV) || defined(AFS_SUN_ENV) || defined(AFS_XBSD_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SGI65_ENV)
21 #include <sys/socket.h>
26 /* Generate an ASCII representation of an integer <val>, as follows:
27 * <base> indicates the base to be used (2-36)
28 * <uc> is nonzero if letter digits should be uppercase
29 * <prec> is the minimum number of digits
30 * The resulting number is stored in <buf>, which must be long enough
31 * to receive it. The minimum length is <prec> or ceil(log{base}(val)),
32 * whichever is larger, plus room for a trailing NUL.
35 mkint(char *buf, afs_uintmax_t val, int base, int uc, unsigned prec)
40 dig = (int) (val % base);
41 val = (val - dig) / base;
52 for (i = 0; i < (len + 1) / 2; i++) {
54 buf[i] = buf[len - i - 1];
55 buf[len - i - 1] = dig;
61 /* This function is a mostly-complete implementation of snprintf,
62 * with the following features:
64 * - Actually obeys the length limit, which (unfortunately) many
65 * implementations of snprintf do not.
67 * - Supports all the standard format specifiers for integers
68 * (d, i, o, u, x, X), floating-point values (f, e, E, g, G),
69 * and strings and characters (c, s, %), plus a few unusual
70 * but useful ones described below.
72 * - Supports all the standard flags (-, 0, +, space, #). These
73 * flags are ignored if used when they are not appropriate.
75 * - Supports the standard size modifiers for short (h), long (h),
76 * and double (L) arguments. These modifiers are ignored if used
77 * when they are not appropriate.
79 * - Supports minimum field width and precision, where appropriate,
80 * including the use of '*' to specify a value given as an argument
81 * instead of in the format string. There is a maximum precision
84 * - At present, the 'p' specifier for printing pointers is not
85 * implemented, because it is inherently non-portable and thus
86 * can be implemented correctly only by the compiler's run-time
89 * - Floating-point specifier (%e, %f, %g) are implemented by
90 * calling the standard sprintf, and thus may be unsafe.
92 * - The '%...$' notation is used primarily when the format string
93 * is specified by the user, who knows but cannot change the order
94 * of the arguments. Such usage is inherently dangerous and
95 * insecure; thus, it is not supported.
97 * The custom format specifier '%I' is supported. This specifier
98 * takes as its argument an unsigned long integer containing an
99 * IPv4 address in network byte order. The address is rendered
100 * either as a hostname or as a dotted quad, as follows:
102 * - If precision is nonzero or unspecified, a hostname lookup
103 * is attempted; if it is successful, the hostname is printed.
104 * If the hostname lookup fails, the address is printed in
105 * dotted-quad notation.
107 * - If precision is explicitly specified as 0, then the hostname
108 * lookup is skipped, and dotted-quad notation is always used.
110 * - If a hostname is to be printed:
111 * + The precision controls the maximum number of characters
112 * printed, as with %s.
113 * + If the '#' flag is specified, any letters in the hostname
114 * will be forced to lower case before printing.
115 * + If the '+' flag is specified, any letters in the hostname
116 * will be forced to upper case before printing. If both
117 * '#' and '+' are given, the '+' flag will be ignored.
118 * + The '0' and ' ' flags have no effect.
120 * - If a dotted quad is to be printed:
121 * + The precision has no effect; dotted quads are always
122 * 7 to 12 characters in length, depending on the value
123 * to be printed and the format flags used.
124 * + If the '0' flag is given, each field (byte) of the address
125 * will be padded with '0' on the left to three digits.
126 * + If the ' ' flag is given, each field (byte) of the address
127 * will be padded with spaces on the left to three digits. If
128 * both '0' and ' ' are given, the ' ' flag will be ignored.
129 * + The '#' and '+' flags have no effect.
132 afs_vsnprintf(char *p, size_t avail, const char *fmt, va_list ap)
134 unsigned int width, precision, haveprec;
136 int ljust, plsign, spsign, altform, zfill;
137 int hflag, lflag, count, *countp, j;
138 char *x, *y, xbuf[MAXPREC + 21], fbuf[20];
143 afs_intmax_t *llcountp;
150 while (*fmt && avail) {
158 /** Found a format specifier **/
159 ljust = plsign = spsign = altform = zfill = 0;
160 width = precision = haveprec = 0;
164 /* parse format flags */
170 continue; /* left justify */
174 continue; /* use + or - */
178 continue; /* use space or - */
182 continue; /* alternate form */
186 continue; /* pad with 0 */
193 /* parse minimum width */
195 width = va_arg(ap, int);
198 while (isdigit(*fmt)) {
199 width = (width * 10) + (*fmt - '0');
203 /* parse precision */
208 precision = va_arg(ap, int);
211 while (isdigit(*fmt)) {
212 precision = (precision * 10) + (*fmt - '0');
217 /* parse size flags */
223 continue; /* short argument */
227 continue; /* long argument */
231 continue; /* long long argument */
238 /* parse format specifier */
247 FVAL = va_arg(ap, double);
248 sprintf(fbuf, "%%%s%s.*L%c", plsign ? "+" : (spsign ? " " : ""),
249 altform ? "#" : "", fmt[-1]);
252 if (precision > MAXPREC)
254 sprintf(xbuf, fbuf, precision, FVAL);
260 case 'd': /* signed decimal integer */
262 SVAL = va_arg(ap, afs_intmax_t);
264 SVAL = va_arg(ap, long);
266 SVAL = va_arg(ap, int);
268 SVAL = va_arg(ap, int);
269 UVAL = (SVAL < 0) ? -SVAL : SVAL;
282 precision = width - !!xbuf[0];
285 if (precision < 1 + !!xbuf[0])
286 precision = 1 + !!xbuf[0];
288 if (precision > MAXPREC)
291 mkint(xbuf + 1, UVAL, 10, 0, precision);
297 case 'o': /* unsigned octal integer */
299 UVAL = va_arg(ap, afs_uintmax_t);
301 UVAL = va_arg(ap, unsigned long);
303 UVAL = va_arg(ap, unsigned int);
305 UVAL = va_arg(ap, unsigned int);
315 if (precision > MAXPREC)
318 mkint(xbuf + 1, UVAL, 8, 0, precision);
319 x = xbuf + (xbuf[1] == '0' || !altform);
323 case 'u': /* unsigned decimal integer */
325 UVAL = va_arg(ap, afs_uintmax_t);
327 UVAL = va_arg(ap, unsigned long);
329 UVAL = va_arg(ap, unsigned int);
331 UVAL = va_arg(ap, unsigned int);
339 if (precision > MAXPREC)
342 mkint(xbuf, UVAL, 10, 0, precision);
348 case 'X': /* unsigned hexadecimal integer */
350 UVAL = va_arg(ap, afs_uintmax_t);
352 UVAL = va_arg(ap, unsigned long);
354 UVAL = va_arg(ap, unsigned int);
356 UVAL = va_arg(ap, unsigned int);
367 if (precision > MAXPREC)
370 mkint(xbuf + 2, UVAL, 16, 0, precision);
371 x = xbuf + ((altform && UVAL) ? 0 : 2);
375 case '%': /* literal % */
382 case 'c': /* character */
383 xbuf[0] = va_arg(ap, int);
389 case 's': /* string */
390 x = va_arg(ap, char *);
394 if (haveprec && precision < len)
398 case 'I': /* IP address:
399 * value is provided as a network-order unsigned long integer
400 * precision specifies max hostname length, as for %s
401 * if precision is explicitly 0, no hostname lookup is done
402 * if 0fill specified, IPaddr fields are 0-filled to 3 digits
403 * if spsign specified, IPaddr fields are space-filled to 3 digits
405 UVAL = va_arg(ap, unsigned long);
406 ia.s_addr = (unsigned long)UVAL;
407 if (haveprec && !precision)
410 he = gethostbyaddr((char *)&ia, 4, AF_INET);
414 if (haveprec && precision < len)
426 UVAL = ntohl((unsigned long)UVAL);
428 x = "%03u.%03u.%03u.%03u";
430 x = "%3u.%3u.%3u.%3u";
434 /* typecast to whatever '%u' is! */
435 sprintf(xbuf, x, (unsigned int)((UVAL & 0xff000000) >> 24),
436 (unsigned int)((UVAL & 0x00ff0000) >> 16),
437 (unsigned int)((UVAL & 0x0000ff00) >> 8),
438 (unsigned int)(UVAL & 0x000000ff));
444 case 'n': /* report count so far */
446 llcountp = va_arg(ap, afs_intmax_t *);
447 *llcountp = (afs_intmax_t)count;
449 lcountp = va_arg(ap, long *);
450 *lcountp = (long)count;
452 hcountp = va_arg(ap, short *);
453 *hcountp = (short)count;
455 countp = va_arg(ap, int *);
460 default: /* unknown specifier */
464 /* render the results */
495 afs_snprintf(char *p, size_t avail, const char *fmt, ...)
501 result = afs_vsnprintf(p, avail, fmt, ap);
506 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV)) || !defined(HAVE_VSNPRINTF)
508 #if defined(AFS_AIX51_ENV) || defined(AFS_NT40_ENV)
510 vsnprintf(char *p, size_t avail, const char *fmt, va_list ap)
513 vsnprintf(char *p, unsigned int avail, char *fmt, va_list ap)
517 result = afs_vsnprintf(p, avail, fmt, ap);
518 #if defined(AFS_AIX51_ENV) || defined(AFS_NT40_ENV)
522 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */
525 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV)) || !defined(HAVE_SNPRINTF)
529 snprintf(char *p, size_t avail, const char *fmt, ...)
532 snprintf(char *p, unsigned int avail, char *fmt, ...)
539 result = afs_vsnprintf(p, avail, fmt, ap);
545 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */
546 #endif /* AFS_NT40_ENV */