1 /* snprintf.c - Formatted, length-limited print to a string */
8 #if defined(AFS_OSF20_ENV) && !defined(AFS_DUX50_ENV) || defined(AFS_AIX32_ENV) || (defined(AFS_SUN55_ENV) && !defined(AFS_SUN56_ENV))
13 #include <netinet/in.h>
15 #if defined(AFS_AIX32_ENV) || defined(AFS_SUN55_ENV)
16 #include <sys/socket.h>
21 /* Generate an ASCII representation of an integer <val>, as follows:
22 * <base> indicates the base to be used (2-36)
23 * <uc> is nonzero if letter digits should be uppercase
24 * <prec> is the minimum number of digits
25 * The resulting number is stored in <buf>, which must be long enough
26 * to receive it. The minimum length is <prec> or ceil(log{base}(val)),
27 * whichever is larger, plus room for a trailing NUL.
29 static void mkint(char *buf, unsigned long val, int base, int uc, int prec)
35 val = (val - dig) / base;
36 if (dig < 10) dig = dig + '0';
37 else if (uc) dig = dig + 'A' - 10;
38 else dig = dig + 'a' - 10;
41 while (len < prec) buf[len++] = '0';
42 for (i = 0; i < (len + 1) / 2; i++) {
44 buf[i] = buf[len - i - 1];
45 buf[len - i - 1] = dig;
51 /* This function is a mostly-complete implementation of snprintf,
52 * with the following features:
54 * - Actually obeys the length limit, which (unfortunately) many
55 * implementations of snprintf do not.
57 * - Supports all the standard format specifiers for integers
58 * (d, i, o, u, x, X), floating-point values (f, e, E, g, G),
59 * and strings and characters (c, s, %), plus a few unusual
60 * but useful ones described below.
62 * - Supports all the standard flags (-, 0, +, space, #). These
63 * flags are ignored if used when they are not appropriate.
65 * - Supports the standard size modifiers for short (h), long (h),
66 * and double (L) arguments. These modifiers are ignored if used
67 * when they are not appropriate.
69 * - Supports minimum field width and precision, where appropriate,
70 * including the use of '*' to specify a value given as an argument
71 * instead of in the format string. There is a maximum precision
74 * - At present, the 'p' specifier for printing pointers is not
75 * implemented, because it is inherently non-portable and thus
76 * can be implemented correctly only by the compiler's run-time
79 * - Floating-point specifier (%e, %f, %g) are implemented by
80 * calling the standard sprintf, and thus may be unsafe.
82 * - The '%...$' notation is used primarily when the format string
83 * is specified by the user, who knows but cannot change the order
84 * of the arguments. Such usage is inherently dangerous and
85 * insecure; thus, it is not supported.
87 * The custom format specifier '%I' is supported. This specifier
88 * takes as its argument an unsigned long integer containing an
89 * IPv4 address in network byte order. The address is rendered
90 * either as a hostname or as a dotted quad, as follows:
92 * - If precision is nonzero or unspecified, a hostname lookup
93 * is attempted; if it is successful, the hostname is printed.
94 * If the hostname lookup fails, the address is printed in
95 * dotted-quad notation.
97 * - If precision is explicitly specified as 0, then the hostname
98 * lookup is skipped, and dotted-quad notation is always used.
100 * - If a hostname is to be printed:
101 * + The precision controls the maximum number of characters
102 * printed, as with %s.
103 * + If the '#' flag is specified, any letters in the hostname
104 * will be forced to lower case before printing.
105 * + If the '+' flag is specified, any letters in the hostname
106 * will be forced to upper case before printing. If both
107 * '#' and '+' are given, the '+' flag will be ignored.
108 * + The '0' and ' ' flags have no effect.
110 * - If a dotted quad is to be printed:
111 * + The precision has no effect; dotted quads are always
112 * 7 to 12 characters in length, depending on the value
113 * to be printed and the format flags used.
114 * + If the '0' flag is given, each field (byte) of the address
115 * will be padded with '0' on the left to three digits.
116 * + If the ' ' flag is given, each field (byte) of the address
117 * will be padded with spaces on the left to three digits. If
118 * both '0' and ' ' are given, the ' ' flag will be ignored.
119 * + The '#' and '+' flags have no effect.
121 static void vsnprintf(char *p, unsigned int avail, char *fmt, va_list ap)
123 unsigned int width, precision, haveprec, len;
124 int ljust, plsign, spsign, altform, zfill;
125 int hflag, lflag, count, *countp, j;
126 char *x, *y, xbuf[MAXPREC + 21], fbuf[20];
136 while (*fmt && avail) {
144 /** Found a format specifier **/
145 ljust = plsign = spsign = altform = zfill = 0;
146 width = precision = haveprec = 0;
150 /* parse format flags */
153 case '-': ljust = 1; fmt++; continue; /* left justify */
154 case '+': plsign = 1; fmt++; continue; /* use + or - */
155 case ' ': spsign = 1; fmt++; continue; /* use space or - */
156 case '#': altform = 1; fmt++; continue; /* alternate form */
157 case '0': zfill = 1; fmt++; continue; /* pad with 0 */
163 /* parse minimum width */
165 width = va_arg(ap, int);
167 } else while (isdigit(*fmt)) {
168 width = (width * 10) + (*fmt - '0');
172 /* parse precision */
177 precision = va_arg(ap, int);
179 } else while (isdigit(*fmt)) {
180 precision = (precision * 10) + (*fmt - '0');
185 /* parse size flags */
188 case 'h': hflag = 1; fmt++; continue; /* short argument */
189 case 'l': lflag = 1; fmt++; continue; /* long argument */
195 /* parse format specifier */
203 FVAL = va_arg(ap, double);
204 sprintf(fbuf, "%%%s%s.*L%c", plsign ? "+" : (spsign ? " " : ""),
205 altform ? "#" : "", fmt[-1]);
206 if (!haveprec) precision = 6;
207 if (precision > MAXPREC) precision = MAXPREC;
208 sprintf(xbuf, fbuf, precision, FVAL);
214 case 'd': /* signed decimal integer */
215 if (lflag) SVAL = va_arg(ap, long);
216 else if (hflag) SVAL = va_arg(ap, short);
217 else SVAL = va_arg(ap, int);
218 UVAL = (SVAL < 0) ? -SVAL : SVAL;
220 if (SVAL < 0) xbuf[0] = '-';
221 else if (plsign) xbuf[0] = '+';
222 else if (spsign) xbuf[0] = ' ';
226 if (zfill && !ljust) precision = width - !!xbuf[0];
228 if (precision < 1 + !!xbuf[0]) precision = 1 + !!xbuf[0];
230 if (precision > MAXPREC) precision = MAXPREC;
232 mkint(xbuf + 1, UVAL, 10, 0, precision);
238 case 'o': /* unsigned octal integer */
239 if (lflag) UVAL = va_arg(ap, unsigned long);
240 else if (hflag) UVAL = va_arg(ap, unsigned short);
241 else UVAL = va_arg(ap, unsigned int);
246 if (zfill && !ljust) precision = width;
249 if (precision > MAXPREC) precision = MAXPREC;
251 mkint(xbuf + 1, UVAL, 8, 0, precision);
252 x = xbuf + (xbuf[1] == '0' || !altform);
256 case 'u': /* unsigned decimal integer */
257 if (lflag) UVAL = va_arg(ap, unsigned long);
258 else if (hflag) UVAL = va_arg(ap, unsigned short);
259 else UVAL = va_arg(ap, unsigned int);
262 if (zfill && !ljust) precision = width;
265 if (precision > MAXPREC) precision = MAXPREC;
267 mkint(xbuf, UVAL, 10, 0, precision);
273 case 'X': /* unsigned hexadecimal integer */
274 if (lflag) UVAL = va_arg(ap, unsigned long);
275 else if (hflag) UVAL = va_arg(ap, unsigned short);
276 else UVAL = va_arg(ap, unsigned int);
282 if (zfill && !ljust) precision = width;
285 if (precision > MAXPREC) precision = MAXPREC;
287 mkint(xbuf + 2, UVAL, 16, 0, precision);
288 x = xbuf + ((altform && UVAL) ? 0 : 2);
292 case '%': /* literal % */
299 case 'c': /* character */
300 xbuf[0] = va_arg(ap, int);
306 case 's': /* string */
307 x = va_arg(ap, char *);
308 if (!x) x = "<null>";
310 if (haveprec && precision < len) len = precision;
313 case 'I': /* IP address:
314 * value is provided as a network-order unsigned long integer
315 * precision specifies max hostname length, as for %s
316 * if precision is explicitly 0, no hostname lookup is done
317 * if 0fill specified, IPaddr fields are 0-filled to 3 digits
318 * if spsign specified, IPaddr fields are space-filled to 3 digits
320 UVAL = va_arg(ap, unsigned long);
322 if (haveprec && !precision) he = 0;
323 else he = gethostbyaddr((char *)&ia, 4, AF_INET);
327 if (haveprec && precision < len) len = precision;
329 for (y = x; *y; y++) if (isupper(*y)) *y = tolower(*y);
331 for (y = x; *y; y++) if (islower(*y)) *y = toupper(*y);
334 if (zfill) x = "%03u.%03u.%03u.%03u";
335 else if (spsign) x = "%3u.%3u.%3u.%3u";
336 else x = "%u.%u.%u.%u";
338 (UVAL & 0xff000000) >> 24, (UVAL & 0x00ff0000) >> 16,
339 (UVAL & 0x0000ff00) >> 8, (UVAL & 0x000000ff));
345 case 'n': /* report count so far */
347 lcountp = va_arg(ap, long *);
350 hcountp = va_arg(ap, short *);
353 countp = va_arg(ap, int *);
358 default: /* unknown specifier */
362 /* render the results */
363 if (len > avail) len = avail;
364 if (!width) width = len;
365 if (width > avail) width = avail;
372 if (!ljust) while (j-- > 0) *p++ = ' ';
379 if (ljust) while (j-- > 0) *p++ = ' ';
385 void snprintf(char *p, unsigned int avail, char *fmt, ...)
390 vsnprintf(p, avail, fmt, ap);
393 #endif /* AFS_OSF20_ENV || AFS_AIX32_ENV */