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>
62 #define main printfcmd
63 #include "bltin/bltin.h"
66 #define warnx1(a, b, c) warnx(a)
67 #define warnx2(a, b, c) warnx(a, b)
68 #define warnx3(a, b, c) warnx(a, b, c)
75 #define PF(f, func) do { \
79 (void)asprintf(&b, f, fieldwidth, precision, func); \
81 (void)asprintf(&b, f, fieldwidth, func); \
83 (void)asprintf(&b, f, precision, func); \
85 (void)asprintf(&b, f, func); \
87 (void)fputs(b, stdout); \
92 static int asciicode(void);
93 static char *doformat(char *, int *);
94 static int escape(char *, int, size_t *);
95 static int getchr(void);
96 static int getfloating(long double *, int);
97 static int getint(int *);
98 static int getnum(intmax_t *, uintmax_t *, int);
101 static char *mknum(char *, int);
102 static void usage(void);
108 progprintf(int argc, char *argv[])
110 main(int argc, char *argv[])
114 int ch, chopped, end, rval;
115 char *format, *fmt, *start;
118 (void) setlocale(LC_NUMERIC, "");
120 while ((ch = getopt(argc, argv, "")) != -1)
136 * Basic algorithm is to scan the format string for conversion
137 * specifications -- once one is found, find out if the field
138 * width or precision is a '*'; if it is, gather up value. Note,
139 * format strings are reused as necessary to use up the provided
140 * arguments, arguments of zero/null string are provided to use
141 * up the format string.
143 fmt = format = *argv;
144 chopped = escape(fmt, 1, &len); /* backslash interpretation */
149 while (fmt < format + len) {
151 fwrite(start, 1, fmt - start, stdout);
157 fmt = doformat(fmt, &rval);
168 warnx1("missing format character", NULL, NULL);
171 fwrite(start, 1, fmt - start, stdout);
172 if (chopped || !*gargv)
174 /* Restart at the beginning of the format string. */
183 doformat(char *start, int *rval)
185 static const char skip1[] = "#'-+ 0";
186 static const char skip2[] = "0123456789";
188 int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
192 /* skip to field width */
193 fmt += strspn(fmt, skip1);
195 if (getint(&fieldwidth))
202 /* skip to possible '.', get following precision */
203 fmt += strspn(fmt, skip2);
206 /* precision present? */
209 if (getint(&precision))
216 /* skip to conversion char */
217 fmt += strspn(fmt, skip2);
222 warnx1("missing format character", NULL, NULL);
227 * Look for a length modifier. POSIX doesn't have these, so
228 * we only support them for floating-point conversions, which
229 * are extensions. This is useful because the L modifier can
230 * be used to gain extra range and precision, while omitting
231 * it is more likely to produce consistent results on different
232 * architectures. This is not so important for integers
233 * because overflow is the only bad thing that can happen to
234 * them, but consider the command printf %a 1.1
239 if (!strchr("aAeEfFgG", *fmt)) {
240 warnx2("bad modifier L for %%%c", *fmt, NULL);
257 p = savestr(getstr());
259 p = strdup(getstr());
262 warnx2("%s", strerror(ENOMEM), NULL);
265 getout = escape(p, 0, &len);
292 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
298 signedconv = (convch == 'd' || convch == 'i');
299 if ((f = mknum(start, convch)) == NULL)
301 if (getnum(&val, &uval, signedconv))
312 case 'a': case 'A': {
315 if (getfloating(&p, mod_ldbl))
320 PF(start, (double)p);
324 warnx2("illegal format character %c", convch, NULL);
332 mknum(char *str, int ch)
335 static size_t copy_size;
339 len = strlen(str) + 2;
340 if (len > copy_size) {
341 newlen = ((len + 1023) >> 10) << 10;
343 if ((newcopy = ckrealloc(copy, newlen)) == NULL)
345 if ((newcopy = realloc(copy, newlen)) == NULL)
348 warnx2("%s", strerror(ENOMEM), NULL);
355 memmove(copy, str, len - 3);
358 copy[len - 1] = '\0';
363 escape(char *fmt, int percent, size_t *len)
368 for (save = store = fmt; (c = *fmt); ++fmt, ++store) {
374 case '\0': /* EOS, user error */
379 case '\\': /* backslash */
380 case '\'': /* single quote */
383 case 'a': /* bell/alert */
386 case 'b': /* backspace */
393 case 'f': /* form-feed */
396 case 'n': /* newline */
399 case 'r': /* carriage-return */
402 case 't': /* horizontal tab */
405 case 'v': /* vertical tab */
409 case '0': case '1': case '2': case '3':
410 case '4': case '5': case '6': case '7':
411 c = (!percent && *fmt == '0') ? 4 : 3;
413 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
418 if (percent && value == '%') {
439 return ((int)**gargv++);
457 if (getnum(&val, &uval, 1))
460 if (val < INT_MIN || val > INT_MAX) {
461 warnx3("%s: %s", *gargv, strerror(ERANGE));
469 getnum(intmax_t *ip, uintmax_t *uip, int signedconv)
478 if (**gargv == '"' || **gargv == '\'') {
488 *ip = strtoimax(*gargv, &ep, 0);
490 *uip = strtoumax(*gargv, &ep, 0);
492 warnx2("%s: expected numeric value", *gargv, NULL);
495 else if (*ep != '\0') {
496 warnx2("%s: not completely converted", *gargv, NULL);
499 if (errno == ERANGE) {
500 warnx3("%s: %s", *gargv, strerror(ERANGE));
508 getfloating(long double *dp, int mod_ldbl)
517 if (**gargv == '"' || **gargv == '\'') {
524 *dp = strtold(*gargv, &ep);
526 *dp = strtod(*gargv, &ep);
528 warnx2("%s: expected numeric value", *gargv, NULL);
530 } else if (*ep != '\0') {
531 warnx2("%s: not completely converted", *gargv, NULL);
534 if (errno == ERANGE) {
535 warnx3("%s: %s", *gargv, strerror(ERANGE));
548 if (ch == '\'' || ch == '"')
557 (void)fprintf(stderr, "usage: printf format [arguments ...]\n");