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.6 2004/12/31 20:32:59 cpressey 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 ch
, chopped
, end
, fieldwidth
, precision
, rval
;
102 char convch
, nextch
, *format
, *fmt
, *start
;
105 setlocale(LC_NUMERIC
, "");
107 while ((ch
= getopt(argc
, argv
, "")) != -1)
123 * Basic algorithm is to scan the format string for conversion
124 * specifications -- once one is found, find out if the field
125 * width or precision is a '*'; if it is, gather up value. Note,
126 * format strings are reused as necessary to use up the provided
127 * arguments, arguments of zero/null string are provided to use
128 * up the format string.
131 skip2
= "0123456789";
133 chopped
= escape(fmt
= format
= *argv
); /* backslash interpretation */
138 /* find next format specification */
139 next
: for (start
= fmt
;; ++fmt
) {
141 /* avoid infinite loop */
147 warnx1("missing format character",
169 /* skip to field width */
170 for (; strchr(skip1
, *fmt
); ++fmt
);
172 if (getint(&fieldwidth
))
178 /* skip to possible '.', get following precision */
179 for (; strchr(skip2
, *fmt
); ++fmt
);
182 /* precision present? */
185 if (getint(&precision
))
191 /* skip to conversion char */
192 for (; strchr(skip2
, *fmt
); ++fmt
);
197 warnx1("missing format character", NULL
, NULL
);
210 p
= savestr(getstr());
212 p
= strdup(getstr());
215 warnx2("%s", strerror(ENOMEM
), NULL
);
245 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
248 unsigned long long uval
;
251 signedconv
= (convch
== 'd' || convch
== 'i');
252 if ((f
= mkquad(start
, convch
)) == NULL
)
254 if (getlonglongs(&val
, &uval
, signedconv
))
262 case 'e': case 'E': case 'f': case 'g': case 'G': {
271 warnx2("illegal format character %c", convch
, NULL
);
280 mkquad(char *str
, int ch
)
283 static size_t copy_size
;
287 len
= strlen(str
) + 2;
288 if (len
> copy_size
) {
289 newlen
= ((len
+ 1023) >> 10) << 10;
291 if ((newcopy
= ckrealloc(copy
, newlen
)) == NULL
)
293 if ((newcopy
= realloc(copy
, newlen
)) == NULL
)
296 warnx2("%s", strerror(ENOMEM
), NULL
);
303 memmove(copy
, str
, len
- 3);
306 copy
[len
- 1] = '\0';
316 for (store
= fmt
; (c
= *fmt
); ++fmt
, ++store
) {
322 case '\0': /* EOS, user error */
326 case '\\': /* backslash */
327 case '\'': /* single quote */
330 case 'a': /* bell/alert */
333 case 'b': /* backspace */
339 case 'f': /* form-feed */
342 case 'n': /* newline */
345 case 'r': /* carriage-return */
348 case 't': /* horizontal tab */
351 case 'v': /* vertical tab */
355 case '0': case '1': case '2': case '3':
356 case '4': case '5': case '6': case '7':
357 for (c
= *fmt
== '0' ? 4 : 3, value
= 0;
358 c
-- && *fmt
>= '0' && *fmt
<= '7'; ++fmt
) {
379 return ((int)**gargv
++);
394 unsigned long long uval
;
397 if (getlonglongs(&val
, &uval
, 1))
400 if (val
< INT_MIN
|| val
> INT_MAX
) {
401 warnx3("%s: %s", *gargv
, strerror(ERANGE
));
409 getlonglongs(long long *qp
, unsigned long long *uqp
, int signedconv
)
418 if (**gargv
== '"' || **gargv
== '\'') {
428 *qp
= strtoll(*gargv
, &ep
, 0);
430 *uqp
= strtoull(*gargv
, &ep
, 0);
432 warnx2("%s: expected numeric value", *gargv
, NULL
);
435 else if (*ep
!= '\0') {
436 warnx2("%s: not completely converted", *gargv
, NULL
);
439 if (errno
== ERANGE
) {
440 warnx3("%s: %s", *gargv
, strerror(ERANGE
));
448 getdouble(double *dp
)
455 if (**gargv
== '"' || **gargv
== '\'') {
461 *dp
= strtod(*gargv
, &ep
);
463 warnx2("%s: expected numeric value", *gargv
, NULL
);
465 } else if (*ep
!= '\0') {
466 warnx2("%s: not completely converted", *gargv
, NULL
);
469 if (errno
== ERANGE
) {
470 warnx3("%s: %s", *gargv
, strerror(ERANGE
));
483 if (ch
== '\'' || ch
== '"')
492 fprintf(stderr
, "usage: printf format [arg ...]\n");