2 * vfprintf() and vprintf() clones. They will produce unexpected results
3 * when excessive dynamic ("*") field widths are specified. To be used for
4 * testing purposes only.
6 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
10 static char sccsid
[] = "@(#) vfprintf.c 1.2 94/03/23 17:44:46";
21 /* vfprintf - print variable-length argument list to stream */
23 int vfprintf(fp
, format
, ap
)
28 char fmt
[BUFSIZ
]; /* format specifier */
34 * Iterate over characters in the format string, picking up arguments
35 * when format specifiers are found.
38 for (cp
= format
; *cp
; cp
++) {
40 putc(*cp
, fp
); /* ordinary character */
45 * Format specifiers are handled one at a time, since we can only
46 * deal with arguments one at a time. Try to determine the end of
47 * the format specifier. We do not attempt to fully parse format
48 * strings, since we are ging to let fprintf() do the hard work.
49 * In regular expression notation, we recognize:
51 * %-?0?([0-9]+|\*)?\.?([0-9]+|\*)?l?[a-z]
53 * which includes some combinations that do not make sense.
58 if (*cp
== '-') /* left-adjusted field? */
60 if (*cp
== '0') /* zero-padded field? */
62 if (*cp
== '*') { /* dynamic field witdh */
63 sprintf(fmtp
, "%d", va_arg(ap
, int));
67 while (isdigit(*cp
)) /* hard-coded field width */
70 if (*cp
== '.') /* width/precision separator */
72 if (*cp
== '*') { /* dynamic precision */
73 sprintf(fmtp
, "%d", va_arg(ap
, int));
77 while (isdigit(*cp
)) /* hard-coded precision */
80 if (*cp
== 'l') /* long whatever */
82 if (*cp
== 0) /* premature end, punt */
84 *fmtp
++ = *cp
; /* type (checked below) */
87 /* Execute the format string - let fprintf() do the hard work. */
90 case 's': /* string-valued argument */
91 count
+= fprintf(fp
, fmt
, va_arg(ap
, char *));
93 case 'c': /* integral-valued argument */
99 count
+= fprintf(fp
, fmt
, va_arg(ap
, long));
101 count
+= fprintf(fp
, fmt
, va_arg(ap
, int));
103 case 'e': /* float-valued argument */
106 count
+= fprintf(fp
, fmt
, va_arg(ap
, double));
108 default: /* anything else */
118 /* vprintf - print variable-length argument list to stdout */
124 return (vfprintf(stdout
, format
, ap
));