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>
64 # define S_IFMT 0170000
66 #if !defined(S_ISBLK) && defined(S_IFBLK)
67 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
69 #if !defined(S_ISCHR) && defined(S_IFCHR)
70 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
72 #if !defined(S_ISDIR) && defined(S_IFDIR)
73 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
75 #if !defined(S_ISREG) && defined(S_IFREG)
76 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
78 #if !defined(S_ISFIFO) && defined(S_IFIFO)
79 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
81 #if !defined(S_ISLNK) && defined(S_IFLNK)
82 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
84 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
85 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
87 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
88 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
89 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
91 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
92 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
95 #define IN_CTYPE_DOMAIN(c) 1
98 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
100 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
103 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
105 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
108 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
109 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
110 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
111 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
112 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
113 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
114 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
115 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
116 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
117 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
118 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
120 #define isodigit(c) ((c) >= '0' && (c) <= '7')
121 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
122 #define octtobin(c) ((c) - '0')
124 static double xstrtod __P((char *s));
125 static int print_esc __P((char *escstart));
126 static int print_formatted __P((char *format, int argc, char **argv));
127 static long xstrtol __P((char *s));
128 static unsigned long xstrtoul __P((char *s));
129 static void print_direc
132 (char *start, size_t length, int field_width, int precision,
134 static void print_esc_char __P((int c));
135 static void print_esc_string __P((char *str));
136 static void verify __P((char *s, char *end));
138 /* The value to return to the calling program. */
139 static int exit_status;
141 static const char printf_usage[] = "printf FORMAT [ARGUMENT...]\n"
142 #ifndef BB_FEATURE_TRIVIAL_HELP
143 "\nFormats and prints ARGUMENT(s) according to FORMAT,\n"
144 "Where FORMAT controls the output exactly as in C printf.\n"
148 int printf_main(int argc, char **argv)
154 if (argc <= 1 || **(argv + 1) == '-') {
163 args_used = print_formatted(format, argc, argv);
167 while (args_used > 0 && argc > 0);
171 fprintf(stderr, "excess args ignored");
177 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
178 arguments to any `%' directives.
179 Return the number of elements of ARGV used. */
181 static int print_formatted(char *format, int argc, char **argv)
183 int save_argc = argc; /* Preserve original value. */
184 char *f; /* Pointer into `format'. */
185 char *direc_start; /* Start of % directive. */
186 size_t direc_length; /* Length of % directive. */
187 int field_width; /* Arg to first '*', or -1 if none. */
188 int precision; /* Arg to second '*', or -1 if none. */
190 for (f = format; *f; ++f) {
195 field_width = precision = -1;
202 print_esc_string(*argv);
208 if (strchr("-+ #", *f)) {
216 field_width = xstrtoul(*argv);
222 while (ISDIGIT(*f)) {
233 precision = xstrtoul(*argv);
239 while (ISDIGIT(*f)) {
244 if (*f == 'l' || *f == 'L' || *f == 'h') {
249 if (!strchr ("diouxXfeEgGcs", *f))
250 fprintf(stderr, "%%%c: invalid directive", *f);
254 print_direc(direc_start, direc_length, field_width,
259 print_direc(direc_start, direc_length, field_width,
272 return save_argc - argc;
275 /* Print a \ escape sequence starting at ESCSTART.
276 Return the number of characters in the escape sequence
277 besides the backslash. */
279 static int print_esc(char *escstart)
281 register char *p = escstart + 1;
282 int esc_value = 0; /* Value of \nnn escape. */
283 int esc_length; /* Length of \nnn escape. */
285 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
287 for (esc_length = 0, ++p;
288 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
289 esc_value = esc_value * 16 + hextobin(*p);
290 /* if (esc_length == 0)
291 fprintf(stderr, "missing hex in esc");
294 } else if (*p == '0') {
295 for (esc_length = 0, ++p;
296 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
297 esc_value = esc_value * 8 + octtobin(*p);
299 } else if (strchr("\"\\abcfnrtv", *p))
300 print_esc_char(*p++);
302 fprintf(stderr, "\\%c: invalid esc", *p);
304 return p - escstart - 1;
307 /* Output a single-character \ escape. */
309 static void print_esc_char(int c)
312 case 'a': /* Alert. */
315 case 'b': /* Backspace. */
318 case 'c': /* Cancel the rest of the output. */
321 case 'f': /* Form feed. */
324 case 'n': /* New line. */
327 case 'r': /* Carriage return. */
330 case 't': /* Horizontal tab. */
333 case 'v': /* Vertical tab. */
342 /* Print string STR, evaluating \ escapes. */
344 static void print_esc_string(char *str)
348 str += print_esc(str);
354 print_direc(char *start, size_t length, int field_width, int precision,
357 char *p; /* Null-terminated copy of % directive. */
359 p = xmalloc((unsigned) (length + 1));
360 strncpy(p, start, length);
363 switch (p[length - 1]) {
366 if (field_width < 0) {
368 printf(p, xstrtol(argument));
370 printf(p, precision, xstrtol(argument));
373 printf(p, field_width, xstrtol(argument));
375 printf(p, field_width, precision, xstrtol(argument));
383 if (field_width < 0) {
385 printf(p, xstrtoul(argument));
387 printf(p, precision, xstrtoul(argument));
390 printf(p, field_width, xstrtoul(argument));
392 printf(p, field_width, precision, xstrtoul(argument));
401 if (field_width < 0) {
403 printf(p, xstrtod(argument));
405 printf(p, precision, xstrtod(argument));
408 printf(p, field_width, xstrtod(argument));
410 printf(p, field_width, precision, xstrtod(argument));
415 printf(p, *argument);
419 if (field_width < 0) {
423 printf(p, precision, argument);
426 printf(p, field_width, argument);
428 printf(p, field_width, precision, argument);
436 static unsigned long xstrtoul(char *s)
442 val = strtoul(s, &end, 0);
447 static long xstrtol(char *s)
453 val = strtol(s, &end, 0);
458 static double xstrtod(char *s)
464 val = strtod(s, &end);
469 static void verify(char *s, char *end)
472 fprintf(stderr, "%s", s);
477 fprintf(stderr, "%s: expected numeric", s);
479 fprintf(stderr, "%s: not completely converted", s);