1 /* $Header: /src/pub/tcsh/tc.printf.c,v 3.27 2005/01/05 16:06:15 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 RCSID("$Id: tc.printf.c,v 3.27 2005/01/05 16:06:15 christos Exp $")
41 #define va_arg(a, b) (a ? (b) 0 : (b) 0)
44 #define INF 32766 /* should be bigger than any field to print */
47 static char snil[] = "(nil)";
49 static void xaddchar __P((int));
50 static void doprnt __P((void (*) __P((int)), const char *, va_list));
53 doprnt(addchar, sfmt, ap)
54 void (*addchar) __P((int));
62 #endif /* SHORT_STRINGS */
72 unsigned char pad = ' ';
73 int flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
80 if (*f != '%') { /* then just out the char */
81 (*addchar) ((int) (((unsigned char)*f) | attributes));
86 if (*f == '-') { /* minus: flush left */
91 if (*f == '0' || *f == '.') {
92 /* padding with 0 rather than blank */
96 if (*f == '*') { /* field width */
97 f_width = va_arg(ap, int);
100 else if (isdigit((unsigned char) *f)) {
102 while (isdigit((unsigned char) *f))
103 f++; /* skip the digits */
106 if (*f == '.') { /* precision */
109 prec = va_arg(ap, int);
112 else if (isdigit((unsigned char) *f)) {
114 while (isdigit((unsigned char) *f))
115 f++; /* skip the digits */
119 if (*f == '#') { /* alternate form */
124 if (*f == 'l') { /* long format */
133 fmt = (unsigned char) *f;
134 if (fmt != 'S' && fmt != 'Q' && isupper(fmt)) {
139 switch (fmt) { /* do the format */
143 l = (long) (va_arg(ap, int));
146 #ifndef HAVE_LONG_LONG
149 l = va_arg(ap, long);
151 #ifdef HAVE_LONG_LONG
153 l = va_arg(ap, long long);
163 *bp++ = (char) (l % 10) + '0';
164 } while ((l /= 10) > 0);
167 f_width = f_width - (int) (bp - buf);
169 while (f_width-- > 0)
170 (*addchar) ((int) (pad | attributes));
171 for (bp--; bp >= buf; bp--)
172 (*addchar) ((int) (((unsigned char) *bp) | attributes));
174 while (f_width-- > 0)
175 (*addchar) ((int) (' ' | attributes));
188 u = (unsigned long) (va_arg(ap, unsigned int));
191 #ifndef HAVE_LONG_LONG
194 u = va_arg(ap, unsigned long);
196 #ifdef HAVE_LONG_LONG
198 u = va_arg(ap, unsigned long long);
202 if (fmt == 'u') { /* unsigned decimal */
204 *bp++ = (char) (u % 10) + '0';
205 } while ((u /= 10) > 0);
207 else if (fmt == 'o') { /* octal */
209 *bp++ = (char) (u % 8) + '0';
210 } while ((u /= 8) > 0);
214 else if (fmt == 'x') { /* hex */
220 *bp++ = i - 10 + 'a';
221 } while ((u /= 16) > 0);
227 i = f_width - (int) (bp - buf);
230 (*addchar) ((int) (pad | attributes));
231 for (bp--; bp >= buf; bp--)
232 (*addchar) ((int) (((unsigned char) *bp) | attributes));
235 (*addchar) ((int) (' ' | attributes));
241 (*addchar) ((int) (i | attributes));
247 Bp = va_arg(ap, Char *);
252 f_width = f_width - Strlen(Bp);
254 while (f_width-- > 0)
255 (*addchar) ((int) (pad | attributes));
256 for (i = 0; *Bp && i < prec; i++) {
257 char cbuf[MB_LEN_MAX];
260 if (fmt == 'Q' && *Bp & QUOTE)
261 (*addchar) ((int) ('\\' | attributes));
262 len = one_wctomb(cbuf, *Bp & CHAR);
263 for (pos = 0; pos < len; pos++)
264 (*addchar) ((int) ((unsigned char)cbuf[pos]
265 | attributes | (*Bp & ATTRIBUTES)));
269 while (f_width-- > 0)
270 (*addchar) ((int) (' ' | attributes));
272 #endif /* SHORT_STRINGS */
276 bp = va_arg(ap, char *);
280 f_width = f_width - strlen((char *) bp);
282 while (f_width-- > 0)
283 (*addchar) ((int) (pad | attributes));
284 for (i = 0; *bp && i < prec; i++) {
285 if (fmt == 'q' && *bp & QUOTE)
286 (*addchar) ((int) ('\\' | attributes));
287 (*addchar) ((int) (((unsigned char) *bp & TRIM) |
292 while (f_width-- > 0)
293 (*addchar) ((int) (' ' | attributes));
297 attributes = va_arg(ap, int);
301 (*addchar) ((int) ('%' | attributes));
307 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
315 static char *xstring, *xestring;
320 if (xestring == xstring)
323 *xstring++ = (char) c;
330 xsnprintf(char *str, size_t size, const char *fmt, ...)
344 str = va_arg(va, char *);
345 size = va_arg(va, size_t);
346 fmt = va_arg(va, char *);
350 xestring = str + size - 1;
351 doprnt(xaddchar, fmt, va);
362 xprintf(const char *fmt, ...)
375 fmt = va_arg(va, char *);
377 doprnt(xputchar, fmt, va);
390 doprnt(xputchar, fmt, va);
397 xvsnprintf(str, size, fmt, va)
404 xestring = str + size - 1;
405 doprnt(xaddchar, fmt, va);
415 /* Purify uses (some of..) the following functions to output memory-use
416 * debugging info. Given all the messing with file descriptors that
417 * tcsh does, the easiest way I could think of to get it (Purify) to
418 * print anything was by replacing some standard functions with
419 * ones that do tcsh output directly - see dumb hook in doreaddirs()
427 fprintf(FILE *fp, const char* fmt, ...)
441 fp = va_arg(va, FILE *);
442 fmt = va_arg(va, const char *);
444 doprnt(xputchar, fmt, va);
450 vfprintf(fp, fmt, va)
455 doprnt(xputchar, fmt, va);