1 /* $Header: /src/pub/tcsh/tc.printf.c,v 3.19 1998/10/25 15:10:37 christos 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.19 1998/10/25 15:10:37 christos 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));
210 Bp = va_arg(ap, Char *);
215 f_width = f_width - Strlen(Bp);
217 while (f_width-- > 0)
218 (*addchar) ((int) (pad | attributes));
219 for (i = 0; *Bp && i < prec; i++) {
220 if (fmt == 'Q' && *Bp & QUOTE)
221 (*addchar) ((int) ('\\' | attributes));
222 (*addchar) ((int) ((*Bp & TRIM) | attributes));
226 while (f_width-- > 0)
227 (*addchar) ((int) (' ' | attributes));
232 bp = va_arg(ap, char *);
236 f_width = f_width - strlen((char *) bp);
238 while (f_width-- > 0)
239 (*addchar) ((int) (pad | attributes));
240 for (i = 0; *bp && i < prec; i++) {
241 if (fmt == 'q' && *bp & QUOTE)
242 (*addchar) ((int) ('\\' | attributes));
243 (*addchar) ((int) (((unsigned char) *bp & TRIM) |
248 while (f_width-- > 0)
249 (*addchar) ((int) (' ' | attributes));
253 attributes = va_arg(ap, int);
257 (*addchar) ((int) ('%' | attributes));
263 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
271 static char *xstring, *xestring;
276 if (xestring == xstring)
279 *xstring++ = (char) c;
286 xsnprintf(char *str, size_t size, const char *fmt, ...)
300 str = va_arg(va, char *);
301 size = va_arg(va, size_t);
302 fmt = va_arg(va, char *);
306 xestring = str + size - 1;
307 doprnt(xaddchar, fmt, va);
318 xprintf(const char *fmt, ...)
331 fmt = va_arg(va, char *);
333 doprnt(xputchar, fmt, va);
346 doprnt(xputchar, fmt, va);
353 xvsnprintf(str, size, fmt, va)
360 xestring = str + size - 1;
361 doprnt(xaddchar, fmt, va);
371 /* Purify uses (some of..) the following functions to output memory-use
372 * debugging info. Given all the messing with file descriptors that
373 * tcsh does, the easiest way I could think of to get it (Purify) to
374 * print anything was by replacing some standard functions with
375 * ones that do tcsh output directly - see dumb hook in doreaddirs()
383 fprintf(FILE *fp, const char* fmt, ...)
397 fp = va_arg(va, FILE *);
398 fmt = va_arg(va, const char *);
400 doprnt(xputchar, fmt, va);
406 vfprintf(fp, fmt, va)
411 doprnt(xputchar, fmt, va);