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 (p[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. */
202 field_width = precision = -1;
209 print_esc_string(*argv);
214 if (strchr("-+ #", *f)) {
222 field_width = my_xstrtoul(*argv);
227 while (isdigit(*f)) {
239 precision = my_xstrtoul(*argv);
244 while (isdigit(*f)) {
249 if (*f == 'l' || *f == 'L' || *f == 'h') {
254 if (!strchr ("diouxXfeEgGcs", *f))
255 fprintf(stderr, "%%%c: invalid directive", *f);
259 print_direc(direc_start, direc_length, field_width,
263 print_direc(direc_start, direc_length, field_width,
269 bb_putchar(bb_process_escape_sequence((const char **)&f));
280 int printf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
281 int printf_main(int argc ATTRIBUTE_UNUSED, char **argv)
286 /* bash builtin errors out on "printf '-%s-\n' foo",
287 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
288 * We will mimic coreutils. */
289 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && argv[1][2] == '\0')
299 argv2 = print_formatted(format, argv);
300 } while (argv2 != argv && *argv2);
302 /* coreutils compat (bash doesn't do this):
304 fprintf(stderr, "excess args ignored");