printf.c revision 1.20 1 1.20 wsanchez /* $NetBSD: printf.c,v 1.20 1998/10/14 00:56:00 wsanchez Exp $ */
2 1.14 tls
3 1.1 cgd /*
4 1.17 mrg * Copyright (c) 1989, 1993
5 1.17 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.16 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.17 mrg #if !defined(BUILTIN) && !defined(SHELL)
39 1.17 mrg __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
40 1.17 mrg The Regents of the University of California. All rights reserved.\n");
41 1.17 mrg #endif
42 1.5 jtc #endif
43 1.1 cgd
44 1.1 cgd #ifndef lint
45 1.16 christos #if 0
46 1.17 mrg static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
47 1.16 christos #else
48 1.20 wsanchez __RCSID("$NetBSD: printf.c,v 1.20 1998/10/14 00:56:00 wsanchez Exp $");
49 1.16 christos #endif
50 1.1 cgd #endif /* not lint */
51 1.1 cgd
52 1.17 mrg #include <sys/types.h>
53 1.17 mrg
54 1.4 jtc #include <ctype.h>
55 1.19 perry #include <err.h>
56 1.19 perry #include <errno.h>
57 1.19 perry #include <limits.h>
58 1.19 perry #include <locale.h>
59 1.1 cgd #include <stdio.h>
60 1.4 jtc #include <stdlib.h>
61 1.20 wsanchez #include <unistd.h>
62 1.2 mycroft #include <string.h>
63 1.19 perry #include <unistd.h>
64 1.4 jtc
65 1.7 jtc static int print_escape_str __P((const char *));
66 1.15 cgd static size_t print_escape __P((const char *));
67 1.5 jtc
68 1.5 jtc static int getchr __P((void));
69 1.5 jtc static double getdouble __P((void));
70 1.5 jtc static int getint __P((void));
71 1.5 jtc static long getlong __P((void));
72 1.11 jtc static unsigned long getulong __P ((void));
73 1.5 jtc static char *getstr __P((void));
74 1.12 jtc static char *mklong __P((const char *, int));
75 1.12 jtc static void check_conversion __P((const char *, const char *));
76 1.5 jtc static void usage __P((void));
77 1.4 jtc
78 1.5 jtc static int rval;
79 1.5 jtc static char **gargv;
80 1.4 jtc
81 1.16 christos #ifdef BUILTIN
82 1.16 christos int progprintf __P((int, char **));
83 1.16 christos #else
84 1.16 christos int main __P((int, char **));
85 1.16 christos #endif
86 1.16 christos
87 1.4 jtc #define isodigit(c) ((c) >= '0' && (c) <= '7')
88 1.4 jtc #define octtobin(c) ((c) - '0')
89 1.9 jtc #define hextobin(c) ((c) >= 'A' && (c) <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0')
90 1.1 cgd
91 1.5 jtc #ifdef SHELL
92 1.5 jtc #define main printfcmd
93 1.5 jtc #include "../../bin/sh/bltin/bltin.h"
94 1.5 jtc
95 1.5 jtc #ifdef __STDC__
96 1.5 jtc #include <stdarg.h>
97 1.5 jtc #else
98 1.5 jtc #include <vararg.h>
99 1.5 jtc #endif
100 1.16 christos
101 1.16 christos static void warnx __P((const char *fmt, ...));
102 1.5 jtc
103 1.5 jtc static void
104 1.5 jtc #ifdef __STDC__
105 1.5 jtc warnx(const char *fmt, ...)
106 1.5 jtc #else
107 1.5 jtc warnx(fmt, va_alist)
108 1.5 jtc const char *fmt;
109 1.5 jtc va_dcl
110 1.5 jtc #endif
111 1.5 jtc {
112 1.5 jtc
113 1.5 jtc char buf[64];
114 1.5 jtc va_list ap;
115 1.5 jtc
116 1.5 jtc #ifdef __STDC__
117 1.5 jtc va_start(ap, fmt);
118 1.5 jtc #else
119 1.5 jtc va_start(ap);
120 1.5 jtc #endif
121 1.5 jtc vsprintf(buf, fmt, ap);
122 1.5 jtc va_end(ap);
123 1.5 jtc
124 1.5 jtc error(buf);
125 1.5 jtc }
126 1.5 jtc #endif /* SHELL */
127 1.5 jtc
128 1.1 cgd #define PF(f, func) { \
129 1.1 cgd if (fieldwidth) \
130 1.1 cgd if (precision) \
131 1.1 cgd (void)printf(f, fieldwidth, precision, func); \
132 1.1 cgd else \
133 1.1 cgd (void)printf(f, fieldwidth, func); \
134 1.1 cgd else if (precision) \
135 1.1 cgd (void)printf(f, precision, func); \
136 1.1 cgd else \
137 1.1 cgd (void)printf(f, func); \
138 1.1 cgd }
139 1.1 cgd
140 1.4 jtc int
141 1.5 jtc #ifdef BUILTIN
142 1.5 jtc progprintf(argc, argv)
143 1.5 jtc #else
144 1.1 cgd main(argc, argv)
145 1.5 jtc #endif
146 1.1 cgd int argc;
147 1.17 mrg char *argv[];
148 1.1 cgd {
149 1.18 lukem char *fmt, *start;
150 1.18 lukem int fieldwidth, precision;
151 1.6 jtc char convch, nextch;
152 1.4 jtc char *format;
153 1.5 jtc int ch;
154 1.4 jtc
155 1.5 jtc #if !defined(SHELL) && !defined(BUILTIN)
156 1.15 cgd (void)setlocale (LC_ALL, "");
157 1.5 jtc #endif
158 1.1 cgd
159 1.5 jtc while ((ch = getopt(argc, argv, "")) != -1) {
160 1.5 jtc switch (ch) {
161 1.5 jtc case '?':
162 1.5 jtc default:
163 1.5 jtc usage();
164 1.5 jtc return (1);
165 1.5 jtc }
166 1.1 cgd }
167 1.5 jtc argc -= optind;
168 1.5 jtc argv += optind;
169 1.1 cgd
170 1.5 jtc if (argc < 1) {
171 1.5 jtc usage();
172 1.5 jtc return (1);
173 1.5 jtc }
174 1.5 jtc
175 1.5 jtc format = *argv;
176 1.5 jtc gargv = ++argv;
177 1.4 jtc
178 1.7 jtc #define SKIP1 "#-+ 0"
179 1.7 jtc #define SKIP2 "*0123456789"
180 1.4 jtc do {
181 1.6 jtc /*
182 1.6 jtc * Basic algorithm is to scan the format string for conversion
183 1.6 jtc * specifications -- once one is found, find out if the field
184 1.6 jtc * width or precision is a '*'; if it is, gather up value.
185 1.6 jtc * Note, format strings are reused as necessary to use up the
186 1.6 jtc * provided arguments, arguments of zero/null string are
187 1.6 jtc * provided to use up the format string.
188 1.6 jtc */
189 1.6 jtc
190 1.6 jtc /* find next format specification */
191 1.6 jtc for (fmt = format; *fmt; fmt++) {
192 1.6 jtc switch (*fmt) {
193 1.6 jtc case '%':
194 1.6 jtc start = fmt++;
195 1.6 jtc
196 1.6 jtc if (*fmt == '%') {
197 1.15 cgd (void)putchar('%');
198 1.6 jtc break;
199 1.6 jtc } else if (*fmt == 'b') {
200 1.6 jtc char *p = getstr();
201 1.7 jtc if (print_escape_str(p)) {
202 1.7 jtc return (rval);
203 1.7 jtc }
204 1.6 jtc break;
205 1.6 jtc }
206 1.6 jtc
207 1.6 jtc /* skip to field width */
208 1.18 lukem for (; strchr(SKIP1, *fmt); ++fmt) ;
209 1.6 jtc fieldwidth = *fmt == '*' ? getint() : 0;
210 1.6 jtc
211 1.6 jtc /* skip to possible '.', get following precision */
212 1.18 lukem for (; strchr(SKIP2, *fmt); ++fmt) ;
213 1.6 jtc if (*fmt == '.')
214 1.6 jtc ++fmt;
215 1.6 jtc precision = *fmt == '*' ? getint() : 0;
216 1.6 jtc
217 1.18 lukem for (; strchr(SKIP2, *fmt); ++fmt) ;
218 1.6 jtc if (!*fmt) {
219 1.6 jtc warnx ("missing format character");
220 1.10 jtc return(1);
221 1.6 jtc }
222 1.6 jtc
223 1.6 jtc convch = *fmt;
224 1.6 jtc nextch = *(fmt + 1);
225 1.6 jtc *(fmt + 1) = '\0';
226 1.6 jtc switch(convch) {
227 1.6 jtc case 'c': {
228 1.6 jtc char p = getchr();
229 1.6 jtc PF(start, p);
230 1.6 jtc break;
231 1.6 jtc }
232 1.6 jtc case 's': {
233 1.6 jtc char *p = getstr();
234 1.6 jtc PF(start, p);
235 1.6 jtc break;
236 1.6 jtc }
237 1.7 jtc case 'd':
238 1.11 jtc case 'i': {
239 1.11 jtc char *f = mklong(start, convch);
240 1.11 jtc long p = getlong();
241 1.11 jtc PF(f, p);
242 1.11 jtc break;
243 1.11 jtc }
244 1.7 jtc case 'o':
245 1.7 jtc case 'u':
246 1.7 jtc case 'x':
247 1.7 jtc case 'X': {
248 1.6 jtc char *f = mklong(start, convch);
249 1.11 jtc unsigned long p = getulong();
250 1.6 jtc PF(f, p);
251 1.6 jtc break;
252 1.6 jtc }
253 1.7 jtc case 'e':
254 1.7 jtc case 'E':
255 1.7 jtc case 'f':
256 1.7 jtc case 'g':
257 1.7 jtc case 'G': {
258 1.6 jtc double p = getdouble();
259 1.6 jtc PF(start, p);
260 1.6 jtc break;
261 1.6 jtc }
262 1.6 jtc default:
263 1.6 jtc warnx ("%s: invalid directive", start);
264 1.10 jtc return(1);
265 1.6 jtc }
266 1.6 jtc *(fmt + 1) = nextch;
267 1.6 jtc break;
268 1.4 jtc
269 1.6 jtc case '\\':
270 1.6 jtc fmt += print_escape(fmt);
271 1.4 jtc break;
272 1.4 jtc
273 1.6 jtc default:
274 1.15 cgd (void)putchar(*fmt);
275 1.4 jtc break;
276 1.4 jtc }
277 1.6 jtc }
278 1.6 jtc } while (gargv > argv && *gargv);
279 1.1 cgd
280 1.6 jtc return (rval);
281 1.4 jtc }
282 1.4 jtc
283 1.4 jtc
284 1.4 jtc /*
285 1.4 jtc * Print SysV echo(1) style escape string
286 1.7 jtc * Halts processing string and returns 1 if a \c escape is encountered.
287 1.4 jtc */
288 1.7 jtc static int
289 1.4 jtc print_escape_str(str)
290 1.18 lukem const char *str;
291 1.4 jtc {
292 1.4 jtc int value;
293 1.4 jtc int c;
294 1.4 jtc
295 1.4 jtc while (*str) {
296 1.4 jtc if (*str == '\\') {
297 1.4 jtc str++;
298 1.4 jtc /*
299 1.4 jtc * %b string octal constants are not like those in C.
300 1.4 jtc * They start with a \0, and are followed by 0, 1, 2,
301 1.4 jtc * or 3 octal digits.
302 1.4 jtc */
303 1.4 jtc if (*str == '0') {
304 1.4 jtc str++;
305 1.4 jtc for (c = 3, value = 0; c-- && isodigit(*str); str++) {
306 1.4 jtc value <<= 3;
307 1.4 jtc value += octtobin(*str);
308 1.4 jtc }
309 1.15 cgd (void)putchar(value);
310 1.4 jtc str--;
311 1.4 jtc } else if (*str == 'c') {
312 1.7 jtc return 1;
313 1.4 jtc } else {
314 1.4 jtc str--;
315 1.4 jtc str += print_escape(str);
316 1.4 jtc }
317 1.4 jtc } else {
318 1.15 cgd (void)putchar(*str);
319 1.1 cgd }
320 1.4 jtc str++;
321 1.4 jtc }
322 1.7 jtc
323 1.7 jtc return 0;
324 1.4 jtc }
325 1.4 jtc
326 1.4 jtc /*
327 1.4 jtc * Print "standard" escape characters
328 1.4 jtc */
329 1.15 cgd static size_t
330 1.4 jtc print_escape(str)
331 1.18 lukem const char *str;
332 1.4 jtc {
333 1.7 jtc const char *start = str;
334 1.9 jtc int value;
335 1.4 jtc int c;
336 1.4 jtc
337 1.4 jtc str++;
338 1.4 jtc
339 1.4 jtc switch (*str) {
340 1.4 jtc case '0': case '1': case '2': case '3':
341 1.4 jtc case '4': case '5': case '6': case '7':
342 1.4 jtc for (c = 3, value = 0; c-- && isodigit(*str); str++) {
343 1.4 jtc value <<= 3;
344 1.4 jtc value += octtobin(*str);
345 1.1 cgd }
346 1.15 cgd (void)putchar(value);
347 1.4 jtc return str - start - 1;
348 1.4 jtc /* NOTREACHED */
349 1.4 jtc
350 1.4 jtc case 'x':
351 1.4 jtc str++;
352 1.4 jtc for (value = 0; isxdigit(*str); str++) {
353 1.4 jtc value <<= 4;
354 1.4 jtc value += hextobin(*str);
355 1.1 cgd }
356 1.4 jtc if (value > UCHAR_MAX) {
357 1.4 jtc warnx ("escape sequence out of range for character");
358 1.4 jtc rval = 1;
359 1.1 cgd }
360 1.15 cgd (void)putchar (value);
361 1.4 jtc return str - start - 1;
362 1.4 jtc /* NOTREACHED */
363 1.4 jtc
364 1.4 jtc case '\\': /* backslash */
365 1.15 cgd (void)putchar('\\');
366 1.4 jtc break;
367 1.4 jtc
368 1.4 jtc case '\'': /* single quote */
369 1.15 cgd (void)putchar('\'');
370 1.4 jtc break;
371 1.4 jtc
372 1.4 jtc case '"': /* double quote */
373 1.15 cgd (void)putchar('"');
374 1.4 jtc break;
375 1.4 jtc
376 1.4 jtc case 'a': /* alert */
377 1.4 jtc #ifdef __STDC__
378 1.15 cgd (void)putchar('\a');
379 1.4 jtc #else
380 1.15 cgd (void)putchar(007);
381 1.4 jtc #endif
382 1.4 jtc break;
383 1.4 jtc
384 1.4 jtc case 'b': /* backspace */
385 1.15 cgd (void)putchar('\b');
386 1.4 jtc break;
387 1.4 jtc
388 1.4 jtc case 'e': /* escape */
389 1.4 jtc #ifdef __GNUC__
390 1.15 cgd (void)putchar('\e');
391 1.4 jtc #else
392 1.15 cgd (void)putchar(033);
393 1.4 jtc #endif
394 1.4 jtc break;
395 1.4 jtc
396 1.4 jtc case 'f': /* form-feed */
397 1.15 cgd (void)putchar('\f');
398 1.4 jtc break;
399 1.4 jtc
400 1.4 jtc case 'n': /* newline */
401 1.15 cgd (void)putchar('\n');
402 1.4 jtc break;
403 1.4 jtc
404 1.4 jtc case 'r': /* carriage-return */
405 1.15 cgd (void)putchar('\r');
406 1.4 jtc break;
407 1.4 jtc
408 1.4 jtc case 't': /* tab */
409 1.15 cgd (void)putchar('\t');
410 1.4 jtc break;
411 1.4 jtc
412 1.4 jtc case 'v': /* vertical-tab */
413 1.15 cgd (void)putchar('\v');
414 1.4 jtc break;
415 1.4 jtc
416 1.4 jtc default:
417 1.15 cgd (void)putchar(*str);
418 1.4 jtc warnx("unknown escape sequence `\\%c'", *str);
419 1.4 jtc rval = 1;
420 1.15 cgd break;
421 1.1 cgd }
422 1.4 jtc
423 1.4 jtc return 1;
424 1.1 cgd }
425 1.1 cgd
426 1.5 jtc static char *
427 1.1 cgd mklong(str, ch)
428 1.12 jtc const char *str;
429 1.12 jtc char ch;
430 1.1 cgd {
431 1.5 jtc static char copy[64];
432 1.15 cgd size_t len;
433 1.1 cgd
434 1.1 cgd len = strlen(str) + 2;
435 1.15 cgd (void)memmove(copy, str, len - 3);
436 1.1 cgd copy[len - 3] = 'l';
437 1.1 cgd copy[len - 2] = ch;
438 1.1 cgd copy[len - 1] = '\0';
439 1.5 jtc return (copy);
440 1.1 cgd }
441 1.1 cgd
442 1.5 jtc static int
443 1.1 cgd getchr()
444 1.1 cgd {
445 1.1 cgd if (!*gargv)
446 1.17 mrg return ('\0');
447 1.17 mrg return ((int)**gargv++);
448 1.1 cgd }
449 1.1 cgd
450 1.5 jtc static char *
451 1.1 cgd getstr()
452 1.1 cgd {
453 1.1 cgd if (!*gargv)
454 1.17 mrg return ("");
455 1.17 mrg return (*gargv++);
456 1.1 cgd }
457 1.1 cgd
458 1.17 mrg static char *Number = "+-.0123456789";
459 1.5 jtc static int
460 1.1 cgd getint()
461 1.1 cgd {
462 1.1 cgd if (!*gargv)
463 1.1 cgd return(0);
464 1.4 jtc
465 1.18 lukem if (strchr(Number, **gargv))
466 1.1 cgd return(atoi(*gargv++));
467 1.4 jtc
468 1.4 jtc return 0;
469 1.1 cgd }
470 1.1 cgd
471 1.5 jtc static long
472 1.1 cgd getlong()
473 1.1 cgd {
474 1.4 jtc long val;
475 1.4 jtc char *ep;
476 1.1 cgd
477 1.1 cgd if (!*gargv)
478 1.4 jtc return(0L);
479 1.4 jtc
480 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
481 1.13 jtc return (long) *((*gargv++)+1);
482 1.4 jtc
483 1.11 jtc errno = 0;
484 1.4 jtc val = strtol (*gargv, &ep, 0);
485 1.12 jtc check_conversion(*gargv++, ep);
486 1.11 jtc return val;
487 1.11 jtc }
488 1.11 jtc
489 1.11 jtc static unsigned long
490 1.11 jtc getulong()
491 1.11 jtc {
492 1.11 jtc unsigned long val;
493 1.11 jtc char *ep;
494 1.11 jtc
495 1.11 jtc if (!*gargv)
496 1.13 jtc return(0UL);
497 1.11 jtc
498 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
499 1.13 jtc return (unsigned long) *((*gargv++)+1);
500 1.11 jtc
501 1.11 jtc errno = 0;
502 1.11 jtc val = strtoul (*gargv, &ep, 0);
503 1.12 jtc check_conversion(*gargv++, ep);
504 1.4 jtc return val;
505 1.1 cgd }
506 1.1 cgd
507 1.5 jtc static double
508 1.1 cgd getdouble()
509 1.1 cgd {
510 1.4 jtc double val;
511 1.4 jtc char *ep;
512 1.1 cgd
513 1.1 cgd if (!*gargv)
514 1.4 jtc return(0.0);
515 1.4 jtc
516 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
517 1.13 jtc return (double) *((*gargv++)+1);
518 1.1 cgd
519 1.11 jtc errno = 0;
520 1.4 jtc val = strtod (*gargv, &ep);
521 1.12 jtc check_conversion(*gargv++, ep);
522 1.12 jtc return val;
523 1.12 jtc }
524 1.12 jtc
525 1.12 jtc static void
526 1.12 jtc check_conversion(s, ep)
527 1.12 jtc const char *s;
528 1.12 jtc const char *ep;
529 1.12 jtc {
530 1.4 jtc if (*ep) {
531 1.12 jtc if (ep == s)
532 1.12 jtc warnx ("%s: expected numeric value", s);
533 1.11 jtc else
534 1.12 jtc warnx ("%s: not completely converted", s);
535 1.11 jtc rval = 1;
536 1.11 jtc } else if (errno == ERANGE) {
537 1.12 jtc warnx ("%s: %s", s, strerror(ERANGE));
538 1.4 jtc rval = 1;
539 1.4 jtc }
540 1.5 jtc }
541 1.5 jtc
542 1.5 jtc static void
543 1.5 jtc usage()
544 1.5 jtc {
545 1.5 jtc (void)fprintf(stderr, "usage: printf format [arg ...]\n");
546 1.1 cgd }
547