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: /usr/local/www/cvsroot/FreeBSD/src/usr.bin/printf/printf.c,v 1.27 2004/03/07 22:22:13 cperciva Exp $
36 * $DragonFly: src/usr.bin/printf/printf.c,v 1.7 2007/01/21 00:24:55 swildner 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 asprintf(&b, f, fieldwidth, precision, func); \
69 asprintf(&b, f, fieldwidth, func); \
71 asprintf(&b, f, precision, func); \
73 asprintf(&b, f, func); \
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 getlonglongs(long long *, unsigned long long *, int);
88 static char *mkquad(char *, int);
89 static void usage(void);
95 progprintf(int argc
, char **argv
)
97 main(int argc
, char **argv
)
100 static const char *skip1
, *skip2
;
101 int chopped
, end
, fieldwidth
, precision
, rval
;
102 char convch
, nextch
, *format
, *fmt
, *start
;
105 setlocale(LC_NUMERIC
, "");
109 * We may not use getopt(3) because calling
110 * "printf -f%s oo" may not result in an invalid
112 * However common usage and other implementations seem
113 * to indicate that we need to allow -- as a discardable
116 if (argc
> 1 && strcmp(argv
[1], "--") == 0) {
129 * Basic algorithm is to scan the format string for conversion
130 * specifications -- once one is found, find out if the field
131 * width or precision is a '*'; if it is, gather up value. Note,
132 * format strings are reused as necessary to use up the provided
133 * arguments, arguments of zero/null string are provided to use
134 * up the format string.
137 skip2
= "0123456789";
139 chopped
= escape(fmt
= format
= *argv
); /* backslash interpretation */
144 /* find next format specification */
145 next
: for (start
= fmt
;; ++fmt
) {
147 /* avoid infinite loop */
153 warnx1("missing format character",
175 /* skip to field width */
176 for (; strchr(skip1
, *fmt
); ++fmt
);
178 if (getint(&fieldwidth
))
184 /* skip to possible '.', get following precision */
185 for (; strchr(skip2
, *fmt
); ++fmt
);
188 /* precision present? */
191 if (getint(&precision
))
197 /* skip to conversion char */
198 for (; strchr(skip2
, *fmt
); ++fmt
);
203 warnx1("missing format character", NULL
, NULL
);
216 p
= savestr(getstr());
218 p
= strdup(getstr());
221 warnx2("%s", strerror(ENOMEM
), NULL
);
251 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
254 unsigned long long uval
;
257 signedconv
= (convch
== 'd' || convch
== 'i');
258 if ((f
= mkquad(start
, convch
)) == NULL
)
260 if (getlonglongs(&val
, &uval
, signedconv
))
268 case 'e': case 'E': case 'f': case 'g': case 'G': {
277 warnx2("illegal format character %c", convch
, NULL
);
286 mkquad(char *str
, int ch
)
289 static size_t copy_size
;
293 len
= strlen(str
) + 2;
294 if (len
> copy_size
) {
295 newlen
= ((len
+ 1023) >> 10) << 10;
297 if ((newcopy
= ckrealloc(copy
, newlen
)) == NULL
)
299 if ((newcopy
= realloc(copy
, newlen
)) == NULL
)
302 warnx2("%s", strerror(ENOMEM
), NULL
);
309 memmove(copy
, str
, len
- 3);
312 copy
[len
- 1] = '\0';
322 for (store
= fmt
; (c
= *fmt
); ++fmt
, ++store
) {
328 case '\0': /* EOS, user error */
332 case '\\': /* backslash */
333 case '\'': /* single quote */
336 case 'a': /* bell/alert */
339 case 'b': /* backspace */
345 case 'f': /* form-feed */
348 case 'n': /* newline */
351 case 'r': /* carriage-return */
354 case 't': /* horizontal tab */
357 case 'v': /* vertical tab */
361 case '0': case '1': case '2': case '3':
362 case '4': case '5': case '6': case '7':
363 for (c
= *fmt
== '0' ? 4 : 3, value
= 0;
364 c
-- && *fmt
>= '0' && *fmt
<= '7'; ++fmt
) {
385 return ((int)**gargv
++);
400 unsigned long long uval
;
403 if (getlonglongs(&val
, &uval
, 1))
406 if (val
< INT_MIN
|| val
> INT_MAX
) {
407 warnx3("%s: %s", *gargv
, strerror(ERANGE
));
415 getlonglongs(long long *qp
, unsigned long long *uqp
, int signedconv
)
424 if (**gargv
== '"' || **gargv
== '\'') {
434 *qp
= strtoll(*gargv
, &ep
, 0);
436 *uqp
= strtoull(*gargv
, &ep
, 0);
438 warnx2("%s: expected numeric value", *gargv
, NULL
);
441 else if (*ep
!= '\0') {
442 warnx2("%s: not completely converted", *gargv
, NULL
);
445 if (errno
== ERANGE
) {
446 warnx3("%s: %s", *gargv
, strerror(ERANGE
));
454 getdouble(double *dp
)
462 if (**gargv
== '"' || **gargv
== '\'') {
468 *dp
= strtod(*gargv
, &ep
);
470 warnx2("%s: expected numeric value", *gargv
, NULL
);
472 } else if (*ep
!= '\0') {
473 warnx2("%s: not completely converted", *gargv
, NULL
);
476 if (errno
== ERANGE
) {
477 warnx3("%s: %s", *gargv
, strerror(ERANGE
));
490 if (ch
== '\'' || ch
== '"')
499 fprintf(stderr
, "usage: printf format [arg ...]\n");