1 /* snprintf.c - Formatted, length-limited print to a string */
4 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV)
10 #include <netinet/in.h>
15 /* Generate an ASCII representation of an integer <val>, as follows:
16 * <base> indicates the base to be used (2-36)
17 * <uc> is nonzero if letter digits should be uppercase
18 * <prec> is the minimum number of digits
19 * The resulting number is stored in <buf>, which must be long enough
20 * to receive it. The minimum length is <prec> or ceil(log{base}(val)),
21 * whichever is larger, plus room for a trailing NUL.
23 static void mkint(char *buf, unsigned long val, int base, int uc, int prec)
29 val = (val - dig) / base;
30 if (dig < 10) dig = dig + '0';
31 else if (uc) dig = dig + 'A' - 10;
32 else dig = dig + 'a' - 10;
35 while (len < prec) buf[len++] = '0';
36 for (i = 0; i < (len + 1) / 2; i++) {
38 buf[i] = buf[len - i - 1];
39 buf[len - i - 1] = dig;
45 /* This function is a mostly-complete implementation of snprintf,
46 * with the following features:
48 * - Actually obeys the length limit, which (unfortunately) many
49 * implementations of snprintf do not.
51 * - Supports all the standard format specifiers for integers
52 * (d, i, o, u, x, X), floating-point values (f, e, E, g, G),
53 * and strings and characters (c, s, %), plus a few unusual
54 * but useful ones described below.
56 * - Supports all the standard flags (-, 0, +, space, #). These
57 * flags are ignored if used when they are not appropriate.
59 * - Supports the standard size modifiers for short (h), long (h),
60 * and double (L) arguments. These modifiers are ignored if used
61 * when they are not appropriate.
63 * - Supports minimum field width and precision, where appropriate,
64 * including the use of '*' to specify a value given as an argument
65 * instead of in the format string. There is a maximum precision
68 * - At present, the 'p' specifier for printing pointers is not
69 * implemented, because it is inherently non-portable and thus
70 * can be implemented correctly only by the compiler's run-time
73 * - Floating-point specifier (%e, %f, %g) are implemented by
74 * calling the standard sprintf, and thus may be unsafe.
76 * - The '%...$' notation is used primarily when the format string
77 * is specified by the user, who knows but cannot change the order
78 * of the arguments. Such usage is inherently dangerous and
79 * insecure; thus, it is not supported.
81 * The custom format specifier '%I' is supported. This specifier
82 * takes as its argument an unsigned long integer containing an
83 * IPv4 address in network byte order. The address is rendered
84 * either as a hostname or as a dotted quad, as follows:
86 * - If precision is nonzero or unspecified, a hostname lookup
87 * is attempted; if it is successful, the hostname is printed.
88 * If the hostname lookup fails, the address is printed in
89 * dotted-quad notation.
91 * - If precision is explicitly specified as 0, then the hostname
92 * lookup is skipped, and dotted-quad notation is always used.
94 * - If a hostname is to be printed:
95 * + The precision controls the maximum number of characters
96 * printed, as with %s.
97 * + If the '#' flag is specified, any letters in the hostname
98 * will be forced to lower case before printing.
99 * + If the '+' flag is specified, any letters in the hostname
100 * will be forced to upper case before printing. If both
101 * '#' and '+' are given, the '+' flag will be ignored.
102 * + The '0' and ' ' flags have no effect.
104 * - If a dotted quad is to be printed:
105 * + The precision has no effect; dotted quads are always
106 * 7 to 12 characters in length, depending on the value
107 * to be printed and the format flags used.
108 * + If the '0' flag is given, each field (byte) of the address
109 * will be padded with '0' on the left to three digits.
110 * + If the ' ' flag is given, each field (byte) of the address
111 * will be padded with spaces on the left to three digits. If
112 * both '0' and ' ' are given, the ' ' flag will be ignored.
113 * + The '#' and '+' flags have no effect.
115 static void vsnprintf(char *p, unsigned int avail, char *fmt, va_list ap)
117 unsigned int width, precision, haveprec, len;
118 int ljust, plsign, spsign, altform, zfill;
119 int hflag, lflag, count, *countp, j;
120 char *x, *y, xbuf[MAXPREC + 21], fbuf[20];
130 while (*fmt && avail) {
138 /** Found a format specifier **/
139 ljust = plsign = spsign = altform = zfill = 0;
140 width = precision = haveprec = 0;
144 /* parse format flags */
147 case '-': ljust = 1; fmt++; continue; /* left justify */
148 case '+': plsign = 1; fmt++; continue; /* use + or - */
149 case ' ': spsign = 1; fmt++; continue; /* use space or - */
150 case '#': altform = 1; fmt++; continue; /* alternate form */
151 case '0': zfill = 1; fmt++; continue; /* pad with 0 */
157 /* parse minimum width */
159 width = va_arg(ap, int);
161 } else while (isdigit(*fmt)) {
162 width = (width * 10) + (*fmt - '0');
166 /* parse precision */
171 precision = va_arg(ap, int);
173 } else while (isdigit(*fmt)) {
174 precision = (precision * 10) + (*fmt - '0');
179 /* parse size flags */
182 case 'h': hflag = 1; fmt++; continue; /* short argument */
183 case 'l': lflag = 1; fmt++; continue; /* long argument */
189 /* parse format specifier */
197 FVAL = va_arg(ap, double);
198 sprintf(fbuf, "%%%s%s.*L%c", plsign ? "+" : (spsign ? " " : ""),
199 altform ? "#" : "", fmt[-1]);
200 if (!haveprec) precision = 6;
201 if (precision > MAXPREC) precision = MAXPREC;
202 sprintf(xbuf, fbuf, precision, FVAL);
208 case 'd': /* signed decimal integer */
209 if (lflag) SVAL = va_arg(ap, long);
210 else if (hflag) SVAL = va_arg(ap, short);
211 else SVAL = va_arg(ap, int);
212 UVAL = (SVAL < 0) ? -SVAL : SVAL;
214 if (SVAL < 0) xbuf[0] = '-';
215 else if (plsign) xbuf[0] = '+';
216 else if (spsign) xbuf[0] = ' ';
220 if (zfill && !ljust) precision = width - !!xbuf[0];
222 if (precision < 1 + !!xbuf[0]) precision = 1 + !!xbuf[0];
224 if (precision > MAXPREC) precision = MAXPREC;
226 mkint(xbuf + 1, UVAL, 10, 0, precision);
232 case 'o': /* unsigned octal integer */
233 if (lflag) UVAL = va_arg(ap, unsigned long);
234 else if (hflag) UVAL = va_arg(ap, unsigned short);
235 else UVAL = va_arg(ap, unsigned int);
240 if (zfill && !ljust) precision = width;
243 if (precision > MAXPREC) precision = MAXPREC;
245 mkint(xbuf + 1, UVAL, 8, 0, precision);
246 x = xbuf + (xbuf[1] == '0' || !altform);
250 case 'u': /* unsigned decimal integer */
251 if (lflag) UVAL = va_arg(ap, unsigned long);
252 else if (hflag) UVAL = va_arg(ap, unsigned short);
253 else UVAL = va_arg(ap, unsigned int);
256 if (zfill && !ljust) precision = width;
259 if (precision > MAXPREC) precision = MAXPREC;
261 mkint(xbuf, UVAL, 10, 0, precision);
267 case 'X': /* unsigned hexadecimal integer */
268 if (lflag) UVAL = va_arg(ap, unsigned long);
269 else if (hflag) UVAL = va_arg(ap, unsigned short);
270 else UVAL = va_arg(ap, unsigned int);
276 if (zfill && !ljust) precision = width;
279 if (precision > MAXPREC) precision = MAXPREC;
281 mkint(xbuf + 2, UVAL, 16, 0, precision);
282 x = xbuf + ((altform && UVAL) ? 0 : 2);
286 case '%': /* literal % */
293 case 'c': /* character */
294 xbuf[0] = va_arg(ap, int);
300 case 's': /* string */
301 x = va_arg(ap, char *);
302 if (!x) x = "<null>";
304 if (haveprec && precision < len) len = precision;
307 case 'I': /* IP address:
308 * value is provided as a network-order unsigned long integer
309 * precision specifies max hostname length, as for %s
310 * if precision is explicitly 0, no hostname lookup is done
311 * if 0fill specified, IPaddr fields are 0-filled to 3 digits
312 * if spsign specified, IPaddr fields are space-filled to 3 digits
314 UVAL = va_arg(ap, unsigned long);
316 if (haveprec && !precision) he = 0;
317 else he = gethostbyaddr((char *)&ia, 4, AF_INET);
321 if (haveprec && precision < len) len = precision;
323 for (y = x; *y; y++) if (isupper(*y)) *y = tolower(*y);
325 for (y = x; *y; y++) if (islower(*y)) *y = toupper(*y);
328 if (zfill) x = "%03u.%03u.%03u.%03u";
329 else if (spsign) x = "%3u.%3u.%3u.%3u";
330 else x = "%u.%u.%u.%u";
332 (UVAL & 0xff000000) >> 24, (UVAL & 0x00ff0000) >> 16,
333 (UVAL & 0x0000ff00) >> 8, (UVAL & 0x000000ff));
339 case 'n': /* report count so far */
341 lcountp = va_arg(ap, long *);
344 hcountp = va_arg(ap, short *);
347 countp = va_arg(ap, int *);
352 default: /* unknown specifier */
356 /* render the results */
357 if (len > avail) len = avail;
358 if (!width) width = len;
359 if (width > avail) width = avail;
366 if (!ljust) while (j-- > 0) *p++ = ' ';
373 if (ljust) while (j-- > 0) *p++ = ' ';
379 void snprintf(char *p, unsigned int avail, char *fmt, ...)
384 vsnprintf(p, avail, fmt, ap);
387 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */