1 /* vi: set sw=4 ts=4: */
2 /* printf - format and print data
3 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Usage: printf format [argument...]
21 A front end to the printf function that lets it be used from the shell.
29 \c = produce no further output
35 \0ooo = octal number (ooo is 0 to 3 digits)
36 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
40 %b = print an argument string, interpreting backslash escapes
42 The `format' argument is re-used as many times as necessary
43 to convert all of the given arguments.
45 David MacKenzie <djm@gnu.ai.mit.edu> */
48 // 19990508 Busy Boxed! Dave Cinege
53 #include <sys/types.h>
63 # define S_IFMT 0170000
65 #if !defined(S_ISBLK) && defined(S_IFBLK)
66 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
68 #if !defined(S_ISCHR) && defined(S_IFCHR)
69 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
71 #if !defined(S_ISDIR) && defined(S_IFDIR)
72 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
74 #if !defined(S_ISREG) && defined(S_IFREG)
75 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
77 #if !defined(S_ISFIFO) && defined(S_IFIFO)
78 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
80 #if !defined(S_ISLNK) && defined(S_IFLNK)
81 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
83 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
84 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
86 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
87 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
88 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
90 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
91 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
94 #define IN_CTYPE_DOMAIN(c) 1
97 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
99 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
102 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
104 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
107 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
108 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
109 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
110 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
111 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
112 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
113 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
114 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
115 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
116 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
117 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
119 #define isodigit(c) ((c) >= '0' && (c) <= '7')
120 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
121 #define octtobin(c) ((c) - '0')
123 static double xstrtod __P((char *s));
124 static int print_esc __P((char *escstart));
125 static int print_formatted __P((char *format, int argc, char **argv));
126 static long xstrtol __P((char *s));
127 static unsigned long xstrtoul __P((char *s));
128 static void print_direc
131 (char *start, size_t length, int field_width, int precision,
133 static void print_esc_char __P((int c));
134 static void print_esc_string __P((char *str));
135 static void verify __P((char *s, char *end));
137 /* The value to return to the calling program. */
138 static int exit_status;
140 static const char printf_usage[] = "printf FORMAT [ARGUMENT...]\n"
141 #ifndef BB_FEATURE_TRIVIAL_HELP
142 "\nFormats and prints ARGUMENT(s) according to FORMAT,\n"
143 "Where FORMAT controls the output exactly as in C printf.\n"
147 int printf_main(int argc, char **argv)
153 if (argc <= 1 || **(argv + 1) == '-') {
162 args_used = print_formatted(format, argc, argv);
166 while (args_used > 0 && argc > 0);
170 fprintf(stderr, "excess args ignored");
176 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
177 arguments to any `%' directives.
178 Return the number of elements of ARGV used. */
180 static int print_formatted(char *format, int argc, char **argv)
182 int save_argc = argc; /* Preserve original value. */
183 char *f; /* Pointer into `format'. */
184 char *direc_start; /* Start of % directive. */
185 size_t direc_length; /* Length of % directive. */
186 int field_width; /* Arg to first '*', or -1 if none. */
187 int precision; /* Arg to second '*', or -1 if none. */
189 for (f = format; *f; ++f) {
194 field_width = precision = -1;
201 print_esc_string(*argv);
207 if (strchr("-+ #", *f)) {
215 field_width = xstrtoul(*argv);
221 while (ISDIGIT(*f)) {
232 precision = xstrtoul(*argv);
238 while (ISDIGIT(*f)) {
243 if (*f == 'l' || *f == 'L' || *f == 'h') {
248 if (!strchr ("diouxXfeEgGcs", *f))
249 fprintf(stderr, "%%%c: invalid directive", *f);
253 print_direc(direc_start, direc_length, field_width,
258 print_direc(direc_start, direc_length, field_width,
271 return save_argc - argc;
274 /* Print a \ escape sequence starting at ESCSTART.
275 Return the number of characters in the escape sequence
276 besides the backslash. */
278 static int print_esc(char *escstart)
280 register char *p = escstart + 1;
281 int esc_value = 0; /* Value of \nnn escape. */
282 int esc_length; /* Length of \nnn escape. */
284 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
286 for (esc_length = 0, ++p;
287 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
288 esc_value = esc_value * 16 + hextobin(*p);
289 /* if (esc_length == 0)
290 fprintf(stderr, "missing hex in esc");
293 } else if (*p == '0') {
294 for (esc_length = 0, ++p;
295 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
296 esc_value = esc_value * 8 + octtobin(*p);
298 } else if (strchr("\"\\abcfnrtv", *p))
299 print_esc_char(*p++);
301 fprintf(stderr, "\\%c: invalid esc", *p);
303 return p - escstart - 1;
306 /* Output a single-character \ escape. */
308 static void print_esc_char(int c)
311 case 'a': /* Alert. */
314 case 'b': /* Backspace. */
317 case 'c': /* Cancel the rest of the output. */
320 case 'f': /* Form feed. */
323 case 'n': /* New line. */
326 case 'r': /* Carriage return. */
329 case 't': /* Horizontal tab. */
332 case 'v': /* Vertical tab. */
341 /* Print string STR, evaluating \ escapes. */
343 static void print_esc_string(char *str)
347 str += print_esc(str);
353 print_direc(char *start, size_t length, int field_width, int precision,
356 char *p; /* Null-terminated copy of % directive. */
358 p = xmalloc((unsigned) (length + 1));
359 strncpy(p, start, length);
362 switch (p[length - 1]) {
365 if (field_width < 0) {
367 printf(p, xstrtol(argument));
369 printf(p, precision, xstrtol(argument));
372 printf(p, field_width, xstrtol(argument));
374 printf(p, field_width, precision, xstrtol(argument));
382 if (field_width < 0) {
384 printf(p, xstrtoul(argument));
386 printf(p, precision, xstrtoul(argument));
389 printf(p, field_width, xstrtoul(argument));
391 printf(p, field_width, precision, xstrtoul(argument));
400 if (field_width < 0) {
402 printf(p, xstrtod(argument));
404 printf(p, precision, xstrtod(argument));
407 printf(p, field_width, xstrtod(argument));
409 printf(p, field_width, precision, xstrtod(argument));
414 printf(p, *argument);
418 if (field_width < 0) {
422 printf(p, precision, argument);
425 printf(p, field_width, argument);
427 printf(p, field_width, precision, argument);
435 static unsigned long xstrtoul(char *s)
441 val = strtoul(s, &end, 0);
446 static long xstrtol(char *s)
452 val = strtol(s, &end, 0);
457 static double xstrtod(char *s)
463 val = strtod(s, &end);
468 static void verify(char *s, char *end)
471 fprintf(stderr, "%s", s);
476 fprintf(stderr, "%s: expected numeric", s);
478 fprintf(stderr, "%s: not completely converted", s);