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
33 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)printf.c 8.1 (Berkeley) 7/20/93
35 * $FreeBSD: src/usr.bin/printf/printf.c,v 1.12.6.6 2002/04/29 16:45:16 jmallett Exp $
36 * $DragonFly: src/usr.bin/printf/printf.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
39 #include <sys/types.h>
50 #define main printfcmd
51 #include "bltin/bltin.h"
54 #define warnx1(a, b, c) warnx(a)
55 #define warnx2(a, b, c) warnx(a, b)
56 #define warnx3(a, b, c) warnx(a, b, c)
63 #define PF(f, func) do { \
67 (void)asprintf(&b, f, fieldwidth, precision, func); \
69 (void)asprintf(&b, f, fieldwidth, func); \
71 (void)asprintf(&b, f, precision, func); \
73 (void)asprintf(&b, f, func); \
75 (void)fputs(b, stdout); \
80 static int asciicode(void);
81 static int escape(char *);
82 static int getchr(void);
83 static int getdouble(double *);
84 static int getint(int *);
85 static int getquads(quad_t *, u_quad_t *, int);
88 static char *mkquad(char *, int);
89 static void usage(void);
95 progprintf(argc, argv)
102 static const char *skip1, *skip2;
103 int ch, chopped, end, fieldwidth, precision, rval;
104 char convch, nextch, *format, *fmt, *start;
107 (void) setlocale(LC_NUMERIC, "");
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 chopped = escape(fmt = format = *argv); /* backslash interpretation */
140 /* find next format specification */
141 next: for (start = fmt;; ++fmt) {
143 /* avoid infinite loop */
145 (void)printf("%s", start);
149 warnx1("missing format character",
155 (void)printf("%s", start);
166 (void)printf("%s", start);
171 /* skip to field width */
172 for (; strchr(skip1, *fmt); ++fmt);
174 if (getint(&fieldwidth))
180 /* skip to possible '.', get following precision */
181 for (; strchr(skip2, *fmt); ++fmt);
184 /* precision present? */
187 if (getint(&precision))
193 /* skip to conversion char */
194 for (; strchr(skip2, *fmt); ++fmt);
199 warnx1("missing format character", NULL, NULL);
212 p = savestr(getstr());
214 p = strdup(getstr());
217 warnx2("%s", strerror(ENOMEM), NULL);
247 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
253 signedconv = (convch == 'd' || convch == 'i');
254 if ((f = mkquad(start, convch)) == NULL)
256 if (getquads(&val, &uval, signedconv))
264 case 'e': case 'E': case 'f': case 'g': case 'G': {
273 warnx2("illegal format character %c", convch, NULL);
287 static size_t copy_size;
291 len = strlen(str) + 2;
292 if (len > copy_size) {
293 newlen = ((len + 1023) >> 10) << 10;
295 if ((newcopy = ckrealloc(copy, newlen)) == NULL)
297 if ((newcopy = realloc(copy, newlen)) == NULL)
300 warnx2("%s", strerror(ENOMEM), NULL);
307 memmove(copy, str, len - 3);
310 copy[len - 1] = '\0';
318 register char *store;
319 register int value, c;
321 for (store = fmt; (c = *fmt); ++fmt, ++store) {
327 case '\0': /* EOS, user error */
331 case '\\': /* backslash */
332 case '\'': /* single quote */
335 case 'a': /* bell/alert */
338 case 'b': /* backspace */
344 case 'f': /* form-feed */
347 case 'n': /* newline */
350 case 'r': /* carriage-return */
353 case 't': /* horizontal tab */
356 case 'v': /* vertical tab */
360 case '0': case '1': case '2': case '3':
361 case '4': case '5': case '6': case '7':
362 for (c = *fmt == '0' ? 4 : 3, value = 0;
363 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
384 return ((int)**gargv++);
403 if (getquads(&val, &uval, 1))
406 if (val < INT_MIN || val > INT_MAX) {
407 warnx3("%s: %s", *gargv, strerror(ERANGE));
415 getquads(qp, uqp, signedconv)
427 if (**gargv == '"' || **gargv == '\'') {
437 *qp = strtoq(*gargv, &ep, 0);
439 *uqp = strtouq(*gargv, &ep, 0);
441 warnx2("%s: expected numeric value", *gargv, NULL);
444 else if (*ep != '\0') {
445 warnx2("%s: not completely converted", *gargv, NULL);
448 if (errno == ERANGE) {
449 warnx3("%s: %s", *gargv, strerror(ERANGE));
465 if (**gargv == '"' || **gargv == '\'') {
471 *dp = strtod(*gargv, &ep);
473 warnx2("%s: expected numeric value", *gargv, NULL);
475 } else if (*ep != '\0') {
476 warnx2("%s: not completely converted", *gargv, NULL);
479 if (errno == ERANGE) {
480 warnx3("%s: %s", *gargv, strerror(ERANGE));
493 if (ch == '\'' || ch == '"')
502 (void)fprintf(stderr, "usage: printf format [arg ...]\n");