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>
40 // 19990508 Busy Boxed! Dave Cinege
44 typedef void (*converter)(const char *arg, void *result);
46 static void multiconvert(const char *arg, void *result, converter convert)
48 char s[sizeof(int)*3 + 2];
50 if (*arg == '"' || *arg == '\'') {
51 sprintf(s, "%d", (unsigned char)arg[1]);
55 /* if there was conversion error, print unconverted string */
60 static void conv_strtoul(const char *arg, void *result)
62 *(unsigned long*)result = bb_strtoul(arg, NULL, 0);
64 static void conv_strtol(const char *arg, void *result)
66 *(long*)result = bb_strtol(arg, NULL, 0);
68 static void conv_strtod(const char *arg, void *result)
71 /* Well, this one allows leading whitespace... so what */
72 /* What I like much less is that "-" is accepted too! :( */
73 *(double*)result = strtod(arg, &end);
74 if (end[0]) errno = ERANGE;
77 static unsigned long my_xstrtoul(const char *arg)
80 multiconvert(arg, &result, conv_strtoul);
84 static long my_xstrtol(const char *arg)
87 multiconvert(arg, &result, conv_strtol);
91 static double my_xstrtod(const char *arg)
94 multiconvert(arg, &result, conv_strtod);
98 static void print_esc_string(char *str)
100 for (; *str; str++) {
103 bb_putchar(bb_process_escape_sequence((const char **)&str));
111 static void print_direc(char *format, unsigned fmt_length,
112 int field_width, int precision,
113 const char *argument)
117 saved = format[fmt_length];
118 format[fmt_length] = '\0';
120 switch (format[fmt_length - 1]) {
123 if (field_width < 0) {
125 printf(format, my_xstrtol(argument));
127 printf(format, precision, my_xstrtol(argument));
130 printf(format, field_width, my_xstrtol(argument));
132 printf(format, field_width, precision, my_xstrtol(argument));
139 if (field_width < 0) {
141 printf(format, my_xstrtoul(argument));
143 printf(format, precision, my_xstrtoul(argument));
146 printf(format, field_width, my_xstrtoul(argument));
148 printf(format, field_width, precision, my_xstrtoul(argument));
156 if (field_width < 0) {
158 printf(format, my_xstrtod(argument));
160 printf(format, precision, my_xstrtod(argument));
163 printf(format, field_width, my_xstrtod(argument));
165 printf(format, field_width, precision, my_xstrtod(argument));
169 printf(format, *argument);
172 if (field_width < 0) {
174 printf(format, argument);
176 printf(format, precision, argument);
179 printf(format, field_width, argument);
181 printf(format, field_width, precision, argument);
186 format[fmt_length] = saved;
189 /* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
190 Return advanced ARGV. */
191 static char **print_formatted(char *f, char **argv)
193 char *direc_start; /* Start of % directive. */
194 unsigned direc_length; /* Length of % directive. */
195 int field_width; /* Arg to first '*', or -1 if none. */
196 int precision; /* Arg to second '*', or -1 if none. */
197 char **saved_argv = argv;
204 field_width = precision = -1;
211 print_esc_string(*argv);
216 if (strchr("-+ #", *f)) {
224 field_width = my_xstrtoul(*argv);
229 while (isdigit(*f)) {
241 precision = my_xstrtoul(*argv);
246 while (isdigit(*f)) {
251 if (*f == 'l' || *f == 'L' || *f == 'h') {
255 /* needed - try "printf %" without it */
256 if (!strchr("diouxXfeEgGcs", *f)) {
257 bb_error_msg("invalid directive '%s'", direc_start);
258 /* causes main() to exit with error */
259 return saved_argv - 1;
263 print_direc(direc_start, direc_length, field_width,
267 print_direc(direc_start, direc_length, field_width,
272 return saved_argv; /* causes main() to exit */
274 bb_putchar(bb_process_escape_sequence((const char **)&f));
285 int printf_main(int argc UNUSED_PARAM, char **argv)
290 /* We must check that stdout is not closed.
291 * The reason for this is highly non-obvious.
292 * printf_main is used from shell.
293 * Shell must correctly handle 'printf "%s" foo'
294 * if stdout is closed. With stdio, output gets shoveled into
295 * stdout buffer, and even fflush cannot clear it out. It seems that
296 * even if libc receives EBADF on write attempts, it feels determined
297 * to output data no matter what. So it will try later,
298 * and possibly will clobber future output. Not good. */
302 /* bash builtin errors out on "printf '-%s-\n' foo",
303 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
304 * We will mimic coreutils. */
305 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
315 argv2 = print_formatted(format, argv);
316 } while (argv2 > argv && *argv2);
318 /* coreutils compat (bash doesn't do this):
320 fprintf(stderr, "excess args ignored");
323 return (argv2 < argv); /* if true, print_formatted errored out */