2 * tc.printf.c: A public-domain, minimal printf/sprintf routine that prints
3 * through the putchar() routine. Feel free to use for
4 * anything... -- 7/17/87 Paul Placeway
7 * Copyright (c) 1980, 1991 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #define va_arg(a, b) (a ? (b) 0 : (b) 0)
41 #define INF INT_MAX /* should be bigger than any field to print */
43 static char snil[] = "(nil)";
45 static void xaddchar (int);
46 static int doprnt (void (*) (int), const char *, va_list);
49 doprnt(void (*addchar) (int), const char *sfmt, va_list ap)
55 #endif /* SHORT_STRINGS */
63 char buf[(CHAR_BIT * sizeof (l) + 2) / 3 + 1]; /* Octal: 3 bits per char */
66 unsigned char pad = ' ';
67 int flush_left = 0, f_width = 0, prec = INF, hash = 0;
68 int do_long = 0, do_size_t = 0, do_ptrdiff_t = 0;
69 int sign = 0, count = 0;
75 if (*f != '%') { /* then just out the char */
76 (*addchar) (((unsigned char)*f) | attributes);
82 if (*f == '-') { /* minus: flush left */
87 if (*f == '0' || *f == '.') {
88 /* padding with 0 rather than blank */
92 if (*f == '*') { /* field width */
93 f_width = va_arg(ap, int);
96 else if (isdigit((unsigned char) *f)) {
98 while (isdigit((unsigned char) *f))
99 f++; /* skip the digits */
102 if (*f == '.') { /* precision */
105 prec = va_arg(ap, int);
108 else if (isdigit((unsigned char) *f)) {
110 while (isdigit((unsigned char) *f))
111 f++; /* skip the digits */
115 if (*f == '#') { /* alternate form */
120 if (*f == 'l') { /* long format */
128 if (*f == 'z') { /* size_t format */
132 if (*f == 't') { /* ptrdiff_t format */
137 fmt = (unsigned char) *f;
138 if (fmt != 'S' && fmt != 'Q' && isupper(fmt)) {
143 switch (fmt) { /* do the format */
148 l = (long) (va_arg(ap, size_t));
150 l = (long) (va_arg(ap, int));
153 #ifndef HAVE_LONG_LONG
156 l = va_arg(ap, long);
158 #ifdef HAVE_LONG_LONG
160 l = va_arg(ap, long long);
170 *bp++ = (char) (l % 10) + '0';
171 } while ((l /= 10) > 0);
174 f_width = f_width - (int) (bp - buf);
176 while (f_width-- > 0) {
177 (*addchar) (pad | attributes);
180 for (bp--; bp >= buf; bp--) {
181 (*addchar) (((unsigned char) *bp) | attributes);
185 while (f_width-- > 0) {
186 (*addchar) (' ' | attributes);
202 u = va_arg(ap, size_t);
203 else if (do_ptrdiff_t)
204 u = va_arg(ap, ptrdiff_t);
206 u = va_arg(ap, unsigned int);
209 #ifndef HAVE_LONG_LONG
212 u = va_arg(ap, unsigned long);
214 #ifdef HAVE_LONG_LONG
216 u = va_arg(ap, unsigned long long);
220 if (fmt == 'u') { /* unsigned decimal */
222 *bp++ = (char) (u % 10) + '0';
223 } while ((u /= 10) > 0);
225 else if (fmt == 'o') { /* octal */
227 *bp++ = (char) (u % 8) + '0';
228 } while ((u /= 8) > 0);
232 else if (fmt == 'x') { /* hex */
238 *bp++ = i - 10 + 'a';
239 } while ((u /= 16) > 0);
245 i = f_width - (int) (bp - buf);
248 (*addchar) (pad | attributes);
251 for (bp--; bp >= buf; bp--)
252 (*addchar) (((unsigned char) *bp) | attributes);
255 (*addchar) (' ' | attributes);
263 (*addchar) (i | attributes);
270 Bp = va_arg(ap, Char *);
275 f_width = f_width - Strlen(Bp);
277 while (f_width-- > 0) {
278 (*addchar) ((int) (pad | attributes));
281 for (i = 0; *Bp && i < prec; i++) {
282 char cbuf[MB_LEN_MAX];
285 if (fmt == 'Q' && *Bp & QUOTE) {
286 (*addchar) ('\\' | attributes);
289 len = one_wctomb(cbuf, *Bp);
290 for (pos = 0; pos < len; pos++) {
291 (*addchar) ((unsigned char)cbuf[pos] | attributes
292 | (*Bp & ATTRIBUTES));
298 while (f_width-- > 0) {
299 (*addchar) (' ' | attributes);
303 #endif /* SHORT_STRINGS */
307 bp = va_arg(ap, char *);
311 f_width = f_width - strlen(bp);
313 while (f_width-- > 0) {
314 (*addchar) (pad | attributes);
317 for (i = 0; *bp && i < prec; i++) {
318 if (fmt == 'q' && *bp & QUOTE) {
319 (*addchar) ('\\' | attributes);
322 (*addchar) (((unsigned char) *bp & TRIM) | attributes);
327 while (f_width-- > 0) {
328 (*addchar) (' ' | attributes);
334 attributes = va_arg(ap, int);
338 (*addchar) ('%' | attributes);
345 flush_left = 0, f_width = 0, prec = INF, hash = 0;
346 do_ptrdiff_t = 0, do_size_t = 0, do_long = 0;
355 static char *xstring, *xestring;
359 if (xestring == xstring)
362 *xstring++ = (char) c;
368 xsnprintf(char *str, size_t size, const char *fmt, ...)
375 xestring = str + size - 1;
376 count = doprnt(xaddchar, fmt, va);
384 xprintf(const char *fmt, ...)
389 count = doprnt(xputchar, fmt, va);
395 xvprintf(const char *fmt, va_list va)
397 return doprnt(xputchar, fmt, va);
401 xvsnprintf(char *str, size_t size, const char *fmt, va_list va)
405 xestring = str + size - 1;
406 count = doprnt(xaddchar, fmt, va);
412 xvasprintf(const char *fmt, va_list va)
418 size = 2048; /* Arbitrary */
422 buf = xrealloc(buf, size);
424 xestring = buf + size - 1;
426 doprnt(xaddchar, fmt, copy);
428 if (xstring < xestring)
433 return xrealloc(buf, xstring - buf);
437 xasprintf(const char *fmt, ...)
443 ret = xvasprintf(fmt, va);
450 /* Purify uses (some of..) the following functions to output memory-use
451 * debugging info. Given all the messing with file descriptors that
452 * tcsh does, the easiest way I could think of to get it (Purify) to
453 * print anything was by replacing some standard functions with
454 * ones that do tcsh output directly - see dumb hook in doreaddirs()
461 fprintf(FILE *fp, const char* fmt, ...)
466 count = doprnt(xputchar, fmt, va);
472 vfprintf(FILE *fp, const char *fmt, va_list va)
474 return doprnt(xputchar, fmt, va);