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";
44 static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93";
46 static const char rcsid[] =
50 #include <sys/types.h>
61 #define main printfcmd
62 #include "bltin/bltin.h"
65 #define warnx1(a, b, c) warnx(a)
66 #define warnx2(a, b, c) warnx(a, b)
67 #define warnx3(a, b, c) warnx(a, b, c)
74 #define PF(f, func) do { \
78 (void)asprintf(&b, f, fieldwidth, precision, func); \
80 (void)asprintf(&b, f, fieldwidth, func); \
82 (void)asprintf(&b, f, precision, func); \
84 (void)asprintf(&b, f, func); \
86 (void)fputs(b, stdout); \
91 static int asciicode(void);
92 static int escape(char *, int);
93 static int getchr(void);
94 static int getdouble(double *);
95 static int getint(int *);
96 static int getquads(quad_t *, u_quad_t *, int);
99 static char *mkquad(char *, int);
100 static void usage(void);
106 progprintf(int argc, char *argv[])
108 main(int argc, char *argv[])
111 static const char *skip1, *skip2;
112 int ch, chopped, end, fieldwidth, haveprec, havewidth, precision, rval;
113 char convch, nextch, *format, *fmt, *start;
116 (void) setlocale(LC_NUMERIC, "");
118 while ((ch = getopt(argc, argv, "")) != -1)
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. Note,
137 * format strings are reused as necessary to use up the provided
138 * arguments, arguments of zero/null string are provided to use
139 * up the format string.
142 skip2 = "0123456789";
144 chopped = escape(fmt = format = *argv, 1);/* backslash interpretation */
149 /* find next format specification */
150 next: for (start = fmt;; ++fmt) {
152 /* avoid infinite loop */
154 (void)printf("%s", start);
158 warnx1("missing format character",
164 (void)printf("%s", start);
174 (void)printf("%.*s", (int)(fmt - start), start);
180 /* skip to field width */
181 for (; strchr(skip1, *fmt); ++fmt);
183 if (getint(&fieldwidth))
190 /* skip to possible '.', get following precision */
191 for (; strchr(skip2, *fmt); ++fmt);
194 /* precision present? */
197 if (getint(&precision))
204 /* skip to conversion char */
205 for (; strchr(skip2, *fmt); ++fmt);
210 warnx1("missing format character", NULL, NULL);
223 p = savestr(getstr());
225 p = strdup(getstr());
228 warnx2("%s", strerror(ENOMEM), NULL);
231 getout = escape(p, 0);
258 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
264 signedconv = (convch == 'd' || convch == 'i');
265 if ((f = mkquad(start, convch)) == NULL)
267 if (getquads(&val, &uval, signedconv))
278 case 'a': case 'A': {
287 warnx2("illegal format character %c", convch, NULL);
296 mkquad(char *str, int ch)
299 static size_t copy_size;
303 len = strlen(str) + 2;
304 if (len > copy_size) {
305 newlen = ((len + 1023) >> 10) << 10;
307 if ((newcopy = ckrealloc(copy, newlen)) == NULL)
309 if ((newcopy = realloc(copy, newlen)) == NULL)
312 warnx2("%s", strerror(ENOMEM), NULL);
319 memmove(copy, str, len - 3);
322 copy[len - 1] = '\0';
327 escape(char *fmt, int percent)
332 for (store = fmt; (c = *fmt); ++fmt, ++store) {
338 case '\0': /* EOS, user error */
342 case '\\': /* backslash */
343 case '\'': /* single quote */
346 case 'a': /* bell/alert */
349 case 'b': /* backspace */
355 case 'f': /* form-feed */
358 case 'n': /* newline */
361 case 'r': /* carriage-return */
364 case 't': /* horizontal tab */
367 case 'v': /* vertical tab */
371 case '0': case '1': case '2': case '3':
372 case '4': case '5': case '6': case '7':
373 for (c = *fmt == '0' ? 4 : 3, value = 0;
374 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
379 if (percent && value == '%') {
399 return ((int)**gargv++);
417 if (getquads(&val, &uval, 1))
420 if (val < INT_MIN || val > INT_MAX) {
421 warnx3("%s: %s", *gargv, strerror(ERANGE));
429 getquads(quad_t *qp, u_quad_t *uqp, int signedconv)
438 if (**gargv == '"' || **gargv == '\'') {
448 *qp = strtoq(*gargv, &ep, 0);
450 *uqp = strtouq(*gargv, &ep, 0);
452 warnx2("%s: expected numeric value", *gargv, NULL);
455 else if (*ep != '\0') {
456 warnx2("%s: not completely converted", *gargv, NULL);
459 if (errno == ERANGE) {
460 warnx3("%s: %s", *gargv, strerror(ERANGE));
468 getdouble(double *dp)
475 if (**gargv == '"' || **gargv == '\'') {
481 *dp = strtod(*gargv, &ep);
483 warnx2("%s: expected numeric value", *gargv, NULL);
485 } else if (*ep != '\0') {
486 warnx2("%s: not completely converted", *gargv, NULL);
489 if (errno == ERANGE) {
490 warnx3("%s: %s", *gargv, strerror(ERANGE));
503 if (ch == '\'' || ch == '"')
512 (void)fprintf(stderr, "usage: printf format [arg ...]\n");