1 /* $Header: /src/pub/tcsh/tc.printf.c,v 3.21 2001/06/21 23:26:54 kim Exp $ */
3 * tc.printf.c: A public-domain, minimal printf/sprintf routine that prints
4 * through the putchar() routine. Feel free to use for
5 * anything... -- 7/17/87 Paul Placeway
8 * Copyright (c) 1980, 1991 The Regents of the University of California.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 RCSID("$Id: tc.printf.c,v 3.21 2001/06/21 23:26:54 kim Exp $")
45 #define va_arg(a, b) (a ? (b) 0 : (b) 0)
48 #define INF 32766 /* should be bigger than any field to print */
52 static void xaddchar __P((int));
53 static void doprnt __P((void (*) __P((int)), const char *, va_list));
56 doprnt(addchar, sfmt, ap)
57 void (*addchar) __P((int));
62 register const char *f;
65 #endif /* SHORT_STRINGS */
67 register unsigned long u;
70 register unsigned char pad = ' ';
71 int flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
78 if (*f != '%') { /* then just out the char */
79 (*addchar) ((int) (((unsigned char)*f) | attributes));
84 if (*f == '-') { /* minus: flush left */
89 if (*f == '0' || *f == '.') {
90 /* padding with 0 rather than blank */
94 if (*f == '*') { /* field width */
95 f_width = va_arg(ap, int);
98 else if (Isdigit((unsigned char) *f)) {
100 while (Isdigit((unsigned char) *f))
101 f++; /* skip the digits */
104 if (*f == '.') { /* precision */
107 prec = va_arg(ap, int);
110 else if (Isdigit((unsigned char) *f)) {
111 prec = atoi((char *) f);
112 while (Isdigit((unsigned char) *f))
113 f++; /* skip the digits */
117 if (*f == '#') { /* alternate form */
122 if (*f == 'l') { /* long format */
127 fmt = (unsigned char) *f;
128 if (fmt != 'S' && fmt != 'Q' && Isupper(fmt)) {
133 switch (fmt) { /* do the format */
136 l = va_arg(ap, long);
138 l = (long) (va_arg(ap, int));
144 *bp++ = (char) (l % 10) + '0';
145 } while ((l /= 10) > 0);
148 f_width = f_width - (int) (bp - buf);
150 while (f_width-- > 0)
151 (*addchar) ((int) (pad | attributes));
152 for (bp--; bp >= buf; bp--)
153 (*addchar) ((int) (((unsigned char) *bp) | attributes));
155 while (f_width-- > 0)
156 (*addchar) ((int) (' ' | attributes));
163 u = va_arg(ap, unsigned long);
165 u = (unsigned long) (va_arg(ap, unsigned int));
166 if (fmt == 'u') { /* unsigned decimal */
168 *bp++ = (char) (u % 10) + '0';
169 } while ((u /= 10) > 0);
171 else if (fmt == 'o') { /* octal */
173 *bp++ = (char) (u % 8) + '0';
174 } while ((u /= 8) > 0);
178 else if (fmt == 'x') { /* hex */
184 *bp++ = i - 10 + 'a';
185 } while ((u /= 16) > 0);
191 i = f_width - (int) (bp - buf);
194 (*addchar) ((int) (pad | attributes));
195 for (bp--; bp >= buf; bp--)
196 (*addchar) ((int) (((unsigned char) *bp) | attributes));
199 (*addchar) ((int) (' ' | attributes));
205 (*addchar) ((int) (i | attributes));
211 Bp = va_arg(ap, Char *);
216 f_width = f_width - Strlen(Bp);
218 while (f_width-- > 0)
219 (*addchar) ((int) (pad | attributes));
220 for (i = 0; *Bp && i < prec; i++) {
221 if (fmt == 'Q' && *Bp & QUOTE)
222 (*addchar) ((int) ('\\' | attributes));
223 (*addchar) ((int) ((*Bp & TRIM) | attributes));
227 while (f_width-- > 0)
228 (*addchar) ((int) (' ' | attributes));
230 #endif /* SHORT_STRINGS */
234 bp = va_arg(ap, char *);
238 f_width = f_width - strlen((char *) bp);
240 while (f_width-- > 0)
241 (*addchar) ((int) (pad | attributes));
242 for (i = 0; *bp && i < prec; i++) {
243 if (fmt == 'q' && *bp & QUOTE)
244 (*addchar) ((int) ('\\' | attributes));
245 (*addchar) ((int) (((unsigned char) *bp & TRIM) |
250 while (f_width-- > 0)
251 (*addchar) ((int) (' ' | attributes));
255 attributes = va_arg(ap, int);
259 (*addchar) ((int) ('%' | attributes));
265 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
273 static char *xstring, *xestring;
278 if (xestring == xstring)
281 *xstring++ = (char) c;
288 xsnprintf(char *str, size_t size, const char *fmt, ...)
302 str = va_arg(va, char *);
303 size = va_arg(va, size_t);
304 fmt = va_arg(va, char *);
308 xestring = str + size - 1;
309 doprnt(xaddchar, fmt, va);
320 xprintf(const char *fmt, ...)
333 fmt = va_arg(va, char *);
335 doprnt(xputchar, fmt, va);
348 doprnt(xputchar, fmt, va);
355 xvsnprintf(str, size, fmt, va)
362 xestring = str + size - 1;
363 doprnt(xaddchar, fmt, va);
373 /* Purify uses (some of..) the following functions to output memory-use
374 * debugging info. Given all the messing with file descriptors that
375 * tcsh does, the easiest way I could think of to get it (Purify) to
376 * print anything was by replacing some standard functions with
377 * ones that do tcsh output directly - see dumb hook in doreaddirs()
385 fprintf(FILE *fp, const char* fmt, ...)
399 fp = va_arg(va, FILE *);
400 fmt = va_arg(va, const char *);
402 doprnt(xputchar, fmt, va);
408 vfprintf(fp, fmt, va)
413 doprnt(xputchar, fmt, va);