2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if !defined(BUILTIN) && !defined(SHELL)
36 static char const copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93";
44 static const char rcsid[] =
48 #include <sys/types.h>
59 #define main printfcmd
60 #include "bltin/bltin.h"
62 #define warnx1(a, b, c) warnx(a)
63 #define warnx2(a, b, c) warnx(a, b)
64 #define warnx3(a, b, c) warnx(a, b, c)
67 #define PF(f, func) { \
71 (void)asprintf(&b, f, fieldwidth, precision, func); \
73 (void)asprintf(&b, f, fieldwidth, func); \
75 (void)asprintf(&b, f, precision, func); \
77 (void)asprintf(&b, f, func); \
79 (void)fputs(b, stdout); \
84 static int asciicode __P((void));
85 static void escape __P((char *));
86 static int getchr __P((void));
87 static double getdouble __P((void));
88 static int getint __P((int *));
89 static int getquad __P((quad_t *));
90 static char *getstr __P((void));
91 static char *mklong __P((char *, int));
92 static void usage __P((void));
98 progprintf(argc, argv)
105 static char *skip1, *skip2;
106 int ch, end, fieldwidth, precision;
107 char convch, nextch, *format, *fmt, *start;
109 while ((ch = getopt(argc, argv, "")) != -1)
125 * Basic algorithm is to scan the format string for conversion
126 * specifications -- once one is found, find out if the field
127 * width or precision is a '*'; if it is, gather up value. Note,
128 * format strings are reused as necessary to use up the provided
129 * arguments, arguments of zero/null string are provided to use
130 * up the format string.
133 skip2 = "0123456789";
135 escape(fmt = format = *argv); /* backslash interpretation */
139 /* find next format specification */
140 next: for (start = fmt;; ++fmt) {
142 /* avoid infinite loop */
144 warnx1("missing format character",
150 (void)printf("%s", start);
161 (void)printf("%s", start);
166 /* skip to field width */
167 for (; strchr(skip1, *fmt); ++fmt);
169 if (getint(&fieldwidth))
175 /* skip to possible '.', get following precision */
176 for (; strchr(skip2, *fmt); ++fmt);
179 /* precision present? */
182 if (getint(&precision))
188 /* skip to conversion char */
189 for (; strchr(skip2, *fmt); ++fmt);
194 warnx1("missing format character", NULL, NULL);
216 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
220 if ((f = mklong(start, convch)) == NULL)
227 case 'e': case 'E': case 'f': case 'g': case 'G': {
235 warnx2("illegal format character %c", convch, NULL);
248 static char copy[64];
251 len = strlen(str) + 2;
252 if (len > sizeof copy)
255 memmove(copy, str, len - 3);
258 copy[len - 1] = '\0';
266 register char *store;
267 register int value, c;
269 for (store = fmt; (c = *fmt); ++fmt, ++store) {
275 case '\0': /* EOS, user error */
279 case '\\': /* backslash */
280 case '\'': /* single quote */
283 case 'a': /* bell/alert */
286 case 'b': /* backspace */
289 case 'f': /* form-feed */
292 case 'n': /* newline */
295 case 'r': /* carriage-return */
298 case 't': /* horizontal tab */
301 case 'v': /* vertical tab */
305 case '0': case '1': case '2': case '3':
306 case '4': case '5': case '6': case '7':
307 for (c = 3, value = 0;
308 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
328 return ((int)**gargv++);
339 static char *Number = "+-.0123456789";
348 if (val < INT_MIN || val > INT_MAX) {
349 warnx3("%s: %s", *gargv, strerror(ERANGE));
367 if (strchr(Number, **gargv)) {
369 val = strtoq(*gargv, &ep, 0);
371 warnx2("%s: illegal number", *gargv, NULL);
375 if (val == QUAD_MAX) {
376 warnx3("%s: %s", *gargv, strerror(ERANGE));
379 if (val == QUAD_MIN) {
380 warnx3("%s: %s", *gargv, strerror(ERANGE));
388 *lp = (long)asciicode();
397 if (strchr(Number, **gargv))
398 return (atof(*gargv++));
399 return ((double)asciicode());
408 if (ch == '\'' || ch == '"')
417 (void)fprintf(stderr, "usage: printf format [arg ...]\n");