Annotation of src/usr.bin/printf/printf.c, Revision 1.15
1.15 ! martynas 1: /* $OpenBSD: printf.c,v 1.14 2008/09/08 17:04:20 martynas 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.15 ! martynas 42: static char rcsid[] = "$OpenBSD: printf.c,v 1.14 2008/09/08 17:04:20 martynas 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
1.13 millert 121:
122: /* Need to accept/ignore "--" option. */
123: if (argc > 1 && strcmp(argv[1], "--") == 0) {
124: argc--;
125: argv++;
126: }
1.1 deraadt 127:
1.11 millert 128: if (argc < 2) {
1.1 deraadt 129: usage();
130: return (1);
131: }
132:
1.11 millert 133: format = *++argv;
1.1 deraadt 134: gargv = ++argv;
135:
136: #define SKIP1 "#-+ 0"
1.15 ! martynas 137: #define SKIP2 "0123456789"
1.1 deraadt 138: do {
139: /*
140: * Basic algorithm is to scan the format string for conversion
141: * specifications -- once one is found, find out if the field
142: * width or precision is a '*'; if it is, gather up value.
143: * Note, format strings are reused as necessary to use up the
144: * provided arguments, arguments of zero/null string are
145: * provided to use up the format string.
146: */
147:
148: /* find next format specification */
149: for (fmt = format; *fmt; fmt++) {
150: switch (*fmt) {
151: case '%':
152: start = fmt++;
153:
154: if (*fmt == '%') {
155: putchar ('%');
156: break;
157: } else if (*fmt == 'b') {
158: char *p = getstr();
159: if (print_escape_str(p)) {
160: return (rval);
161: }
162: break;
163: }
164:
165: /* skip to field width */
1.15 ! martynas 166: for (; strchr(SKIP1, *fmt); ++fmt)
! 167: ;
! 168: if (*fmt == '*') {
! 169: ++fmt;
! 170: fieldwidth = getint();
! 171: } else
! 172: fieldwidth = 0;
! 173:
! 174: /* skip to field precision */
! 175: for (; strchr(SKIP2, *fmt); ++fmt)
! 176: ;
! 177: precision = 0;
! 178: if (*fmt == '.') {
1.1 deraadt 179: ++fmt;
1.15 ! martynas 180: if (*fmt == '*') {
! 181: ++fmt;
! 182: precision = getint();
! 183: }
! 184: for (; strchr(SKIP2, *fmt); ++fmt)
! 185: ;
! 186: }
1.1 deraadt 187:
188: if (!*fmt) {
189: warnx ("missing format character");
190: return(1);
191: }
192:
193: convch = *fmt;
194: nextch = *(fmt + 1);
195: *(fmt + 1) = '\0';
196: switch(convch) {
197: case 'c': {
198: char p = getchr();
199: PF(start, p);
200: break;
201: }
202: case 's': {
203: char *p = getstr();
204: PF(start, p);
205: break;
206: }
207: case 'd':
208: case 'i': {
1.4 deraadt 209: long p;
1.1 deraadt 210: char *f = mklong(start, convch);
1.4 deraadt 211: if (!f) {
212: warnx("out of memory");
213: return (1);
214: }
215: p = getlong();
1.1 deraadt 216: PF(f, p);
217: break;
218: }
219: case 'o':
220: case 'u':
221: case 'x':
222: case 'X': {
1.4 deraadt 223: unsigned long p;
1.1 deraadt 224: char *f = mklong(start, convch);
1.4 deraadt 225: if (!f) {
226: warnx("out of memory");
227: return (1);
228: }
229: p = getulong();
1.1 deraadt 230: PF(f, p);
231: break;
232: }
1.14 martynas 233: case 'a':
234: case 'A':
1.1 deraadt 235: case 'e':
236: case 'E':
237: case 'f':
1.14 martynas 238: case 'F':
1.1 deraadt 239: case 'g':
240: case 'G': {
241: double p = getdouble();
242: PF(start, p);
243: break;
244: }
245: default:
246: warnx ("%s: invalid directive", start);
247: return(1);
248: }
249: *(fmt + 1) = nextch;
250: break;
251:
252: case '\\':
253: fmt += print_escape(fmt);
254: break;
255:
256: default:
257: putchar (*fmt);
258: break;
259: }
260: }
261: } while (gargv > argv && *gargv);
262:
263: return (rval);
264: }
265:
266:
267: /*
268: * Print SysV echo(1) style escape string
269: * Halts processing string and returns 1 if a \c escape is encountered.
270: */
271: static int
1.10 deraadt 272: print_escape_str(const char *str)
1.1 deraadt 273: {
274: int value;
275: int c;
276:
277: while (*str) {
278: if (*str == '\\') {
279: str++;
280: /*
281: * %b string octal constants are not like those in C.
282: * They start with a \0, and are followed by 0, 1, 2,
283: * or 3 octal digits.
284: */
285: if (*str == '0') {
286: str++;
287: for (c = 3, value = 0; c-- && isodigit(*str); str++) {
288: value <<= 3;
289: value += octtobin(*str);
290: }
291: putchar (value);
292: str--;
293: } else if (*str == 'c') {
294: return 1;
295: } else {
296: str--;
297: str += print_escape(str);
298: }
299: } else {
300: putchar (*str);
301: }
302: str++;
303: }
304:
305: return 0;
306: }
307:
308: /*
309: * Print "standard" escape characters
310: */
311: static int
1.10 deraadt 312: print_escape(const char *str)
1.1 deraadt 313: {
314: const char *start = str;
315: int value;
316: int c;
317:
318: str++;
319:
320: switch (*str) {
321: case '0': case '1': case '2': case '3':
322: case '4': case '5': case '6': case '7':
323: for (c = 3, value = 0; c-- && isodigit(*str); str++) {
324: value <<= 3;
325: value += octtobin(*str);
326: }
327: putchar(value);
328: return str - start - 1;
329: /* NOTREACHED */
330:
331: case 'x':
332: str++;
333: for (value = 0; isxdigit(*str); str++) {
334: value <<= 4;
335: value += hextobin(*str);
336: }
337: if (value > UCHAR_MAX) {
338: warnx ("escape sequence out of range for character");
339: rval = 1;
340: }
341: putchar (value);
342: return str - start - 1;
343: /* NOTREACHED */
344:
345: case '\\': /* backslash */
346: putchar('\\');
347: break;
348:
349: case '\'': /* single quote */
350: putchar('\'');
351: break;
352:
353: case '"': /* double quote */
354: putchar('"');
355: break;
356:
357: case 'a': /* alert */
358: putchar('\a');
359: break;
360:
361: case 'b': /* backspace */
362: putchar('\b');
363: break;
364:
365: case 'e': /* escape */
366: #ifdef __GNUC__
367: putchar('\e');
368: #else
369: putchar(033);
370: #endif
371: break;
372:
373: case 'f': /* form-feed */
374: putchar('\f');
375: break;
376:
377: case 'n': /* newline */
378: putchar('\n');
379: break;
380:
381: case 'r': /* carriage-return */
382: putchar('\r');
383: break;
384:
385: case 't': /* tab */
386: putchar('\t');
387: break;
388:
389: case 'v': /* vertical-tab */
390: putchar('\v');
391: break;
392:
393: default:
394: putchar(*str);
395: warnx("unknown escape sequence `\\%c'", *str);
396: rval = 1;
397: }
398:
399: return 1;
400: }
401:
402: static char *
1.10 deraadt 403: mklong(const char *str, int ch)
1.1 deraadt 404: {
1.4 deraadt 405: static char *copy;
406: static int copysize;
1.1 deraadt 407: int len;
408:
409: len = strlen(str) + 2;
1.4 deraadt 410: if (copysize < len) {
411: char *newcopy;
412: copysize = len + 256;
413:
414: newcopy = realloc(copy, copysize);
415: if (newcopy == NULL) {
416: copysize = 0;
417: free(copy);
418: copy = NULL;
419: return (NULL);
420: }
421: copy = newcopy;
422: }
1.1 deraadt 423: (void) memmove(copy, str, len - 3);
424: copy[len - 3] = 'l';
425: copy[len - 2] = ch;
426: copy[len - 1] = '\0';
427: return (copy);
428: }
429:
430: static int
1.10 deraadt 431: getchr(void)
1.1 deraadt 432: {
433: if (!*gargv)
434: return((int)'\0');
435: return((int)**gargv++);
436: }
437:
438: static char *
1.10 deraadt 439: getstr(void)
1.1 deraadt 440: {
441: if (!*gargv)
442: return("");
443: return(*gargv++);
444: }
445:
446: static char *number = "+-.0123456789";
447: static int
1.10 deraadt 448: getint(void)
1.1 deraadt 449: {
450: if (!*gargv)
451: return(0);
452:
1.3 millert 453: if (strchr(number, **gargv))
1.1 deraadt 454: return(atoi(*gargv++));
455:
456: return 0;
457: }
458:
459: static long
1.10 deraadt 460: getlong(void)
1.1 deraadt 461: {
462: long val;
463: char *ep;
464:
465: if (!*gargv)
466: return(0L);
467:
468: if (**gargv == '\"' || **gargv == '\'')
469: return (long) *((*gargv++)+1);
470:
471: errno = 0;
472: val = strtol (*gargv, &ep, 0);
473: check_conversion(*gargv++, ep);
474: return val;
475: }
476:
477: static unsigned long
1.10 deraadt 478: getulong(void)
1.1 deraadt 479: {
480: unsigned long val;
481: char *ep;
482:
483: if (!*gargv)
484: return(0UL);
485:
486: if (**gargv == '\"' || **gargv == '\'')
487: return (unsigned long) *((*gargv++)+1);
488:
489: errno = 0;
490: val = strtoul (*gargv, &ep, 0);
491: check_conversion(*gargv++, ep);
492: return val;
493: }
494:
495: static double
1.10 deraadt 496: getdouble(void)
1.1 deraadt 497: {
498: double val;
499: char *ep;
500:
501: if (!*gargv)
502: return(0.0);
503:
504: if (**gargv == '\"' || **gargv == '\'')
505: return (double) *((*gargv++)+1);
506:
507: errno = 0;
508: val = strtod (*gargv, &ep);
509: check_conversion(*gargv++, ep);
510: return val;
511: }
512:
513: static void
1.10 deraadt 514: check_conversion(const char *s, const char *ep)
1.1 deraadt 515: {
516: if (*ep) {
517: if (ep == s)
518: warnx ("%s: expected numeric value", s);
519: else
520: warnx ("%s: not completely converted", s);
521: rval = 1;
522: } else if (errno == ERANGE) {
523: warnx ("%s: %s", s, strerror(ERANGE));
524: rval = 1;
525: }
526: }
527:
528: static void
1.10 deraadt 529: usage(void)
1.1 deraadt 530: {
531: (void)fprintf(stderr, "usage: printf format [arg ...]\n");
532: }