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