1 /* vi: set sw=4 ts=4: */
2 /* printf - format and print data
4 Copyright 1999 Dave Cinege
5 Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
7 Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
10 /* Usage: printf format [argument...]
12 A front end to the printf function that lets it be used from the shell.
20 \c = produce no further output
26 \0ooo = octal number (ooo is 0 to 3 digits)
27 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
31 %b = print an argument string, interpreting backslash escapes
33 The 'format' argument is re-used as many times as necessary
34 to convert all of the given arguments.
36 David MacKenzie <djm@gnu.ai.mit.edu> */
39 // 19990508 Busy Boxed! Dave Cinege
43 typedef void (*converter)(const char *arg, void *result);
45 static void multiconvert(const char *arg, void *result, converter convert)
47 char s[sizeof(int)*3 + 2];
49 if (*arg == '"' || *arg == '\'') {
50 sprintf(s, "%d", (unsigned char)arg[1]);
54 /* if there was conversion error, print unconverted string */
59 static void conv_strtoul(const char *arg, void *result)
61 *(unsigned long*)result = bb_strtoul(arg, NULL, 0);
63 static void conv_strtol(const char *arg, void *result)
65 *(long*)result = bb_strtol(arg, NULL, 0);
67 static void conv_strtod(const char *arg, void *result)
70 /* Well, this one allows leading whitespace... so what */
71 /* What I like much less is that "-" is accepted too! :( */
72 *(double*)result = strtod(arg, &end);
73 if (end[0]) errno = ERANGE;
76 static unsigned long my_xstrtoul(const char *arg)
79 multiconvert(arg, &result, conv_strtoul);
83 static long my_xstrtol(const char *arg)
86 multiconvert(arg, &result, conv_strtol);
90 static double my_xstrtod(const char *arg)
93 multiconvert(arg, &result, conv_strtod);
97 static void print_esc_string(char *str)
102 bb_putchar(bb_process_escape_sequence((const char **)&str));
110 static void print_direc(char *format, unsigned fmt_length,
111 int field_width, int precision,
112 const char *argument)
116 saved = format[fmt_length];
117 format[fmt_length] = '\0';
119 switch (format[fmt_length - 1]) {
122 if (field_width < 0) {
124 printf(format, my_xstrtol(argument));
126 printf(format, precision, my_xstrtol(argument));
129 printf(format, field_width, my_xstrtol(argument));
131 printf(format, field_width, precision, my_xstrtol(argument));
138 if (field_width < 0) {
140 printf(format, my_xstrtoul(argument));
142 printf(format, precision, my_xstrtoul(argument));
145 printf(format, field_width, my_xstrtoul(argument));
147 printf(format, field_width, precision, my_xstrtoul(argument));
155 if (field_width < 0) {
157 printf(format, my_xstrtod(argument));
159 printf(format, precision, my_xstrtod(argument));
162 printf(format, field_width, my_xstrtod(argument));
164 printf(format, field_width, precision, my_xstrtod(argument));
168 printf(format, *argument);
171 if (field_width < 0) {
173 printf(format, argument);
175 printf(format, precision, argument);
178 printf(format, field_width, argument);
180 printf(format, field_width, precision, argument);
185 format[fmt_length] = saved;
188 /* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
189 Return advanced ARGV. */
190 static char **print_formatted(char *f, char **argv)
192 char *direc_start; /* Start of % directive. */
193 unsigned direc_length; /* Length of % directive. */
194 int field_width; /* Arg to first '*', or -1 if none. */
195 int precision; /* Arg to second '*', or -1 if none. */
196 char **saved_argv = argv;
203 field_width = precision = -1;
210 print_esc_string(*argv);
215 if (strchr("-+ #", *f)) {
223 field_width = my_xstrtoul(*argv);
228 while (isdigit(*f)) {
240 precision = my_xstrtoul(*argv);
245 while (isdigit(*f)) {
250 if (*f == 'l' || *f == 'L' || *f == 'h') {
255 if (!strchr ("diouxXfeEgGcs", *f))
256 fprintf(stderr, "%%%c: invalid directive", *f);
260 print_direc(direc_start, direc_length, field_width,
264 print_direc(direc_start, direc_length, field_width,
269 return saved_argv; /* causes main() to exit */
271 bb_putchar(bb_process_escape_sequence((const char **)&f));
282 int printf_main(int argc UNUSED_PARAM, char **argv)
287 /* We must check that stdout is not closed.
288 * The reason for this is highly non-obvious. printf_main is used from shell.
289 * Shell must correctly handle 'printf "%s" foo'
290 * if stdout is closed. With stdio, output gets shoveled into
291 * stdout buffer, and even fflush cannot clear it out. It seems that
292 * even if libc receives EBADF on write attempts, it feels determined
293 * to output data no matter what. So it will try later,
294 * and possibly will clobber future output. Not good. */
298 /* bash builtin errors out on "printf '-%s-\n' foo",
299 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
300 * We will mimic coreutils. */
301 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && argv[1][2] == '\0')
311 argv2 = print_formatted(format, argv);
312 } while (argv2 != argv && *argv2);
314 /* coreutils compat (bash doesn't do this):
316 fprintf(stderr, "excess args ignored");