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 static int print_formatted(char *format, int argc, char **argv);
44 static void print_direc(char *start, size_t length,
45 int field_width, int precision, const char *argument);
47 typedef void (*converter)(const char *arg, void *result);
49 static void multiconvert(const char *arg, void *result, converter convert)
52 if (*arg == '"' || *arg == '\'') {
53 sprintf(s, "%d", (unsigned char)arg[1]);
57 if (errno) /* Huh, looks strange... bug? */
61 static void conv_strtoul(const char *arg, void *result)
63 *(unsigned long*)result = bb_strtoul(arg, NULL, 10);
65 static void conv_strtol(const char *arg, void *result)
67 *(long*)result = bb_strtol(arg, NULL, 10);
69 static void conv_strtod(const char *arg, void *result)
72 /* Well, this one allows leading whitespace... so what */
73 /* What I like much less is that "-" is accepted too! :( */
74 *(double*)result = strtod(arg, &end);
75 if (end[0]) errno = ERANGE;
78 static unsigned long my_xstrtoul(const char *arg)
81 multiconvert(arg, &result, conv_strtoul);
85 static long my_xstrtol(const char *arg)
88 multiconvert(arg, &result, conv_strtol);
92 static double my_xstrtod(const char *arg)
95 multiconvert(arg, &result, conv_strtod);
99 static void print_esc_string(char *str)
101 for (; *str; str++) {
104 putchar(bb_process_escape_sequence((const char **)&str));
112 int printf_main(int argc, char **argv)
117 if (argc <= 1 || argv[1][0] == '-') {
126 args_used = print_formatted(format, argc, argv);
130 while (args_used > 0 && argc > 0);
133 fprintf(stderr, "excess args ignored");
139 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
140 arguments to any '%' directives.
141 Return the number of elements of ARGV used. */
143 static int print_formatted(char *format, int argc, char **argv)
145 int save_argc = argc; /* Preserve original value. */
146 char *f; /* Pointer into 'format'. */
147 char *direc_start; /* Start of % directive. */
148 size_t direc_length; /* Length of % directive. */
149 int field_width; /* Arg to first '*', or -1 if none. */
150 int precision; /* Arg to second '*', or -1 if none. */
152 for (f = format; *f; ++f) {
157 field_width = precision = -1;
164 print_esc_string(*argv);
170 if (strchr("-+ #", *f)) {
178 field_width = my_xstrtoul(*argv);
184 while (isdigit(*f)) {
195 precision = my_xstrtoul(*argv);
201 while (isdigit(*f)) {
206 if (*f == 'l' || *f == 'L' || *f == 'h') {
211 if (!strchr ("diouxXfeEgGcs", *f))
212 fprintf(stderr, "%%%c: invalid directive", *f);
216 print_direc(direc_start, direc_length, field_width,
221 print_direc(direc_start, direc_length, field_width,
228 putchar(bb_process_escape_sequence((const char **)&f));
237 return save_argc - argc;
241 print_direc(char *start, size_t length, int field_width, int precision,
242 const char *argument)
244 char *p; /* Null-terminated copy of % directive. */
246 p = xmalloc((unsigned) (length + 1));
247 strncpy(p, start, length);
250 switch (p[length - 1]) {
253 if (field_width < 0) {
255 printf(p, my_xstrtol(argument));
257 printf(p, precision, my_xstrtol(argument));
260 printf(p, field_width, my_xstrtol(argument));
262 printf(p, field_width, precision, my_xstrtol(argument));
270 if (field_width < 0) {
272 printf(p, my_xstrtoul(argument));
274 printf(p, precision, my_xstrtoul(argument));
277 printf(p, field_width, my_xstrtoul(argument));
279 printf(p, field_width, precision, my_xstrtoul(argument));
288 if (field_width < 0) {
290 printf(p, my_xstrtod(argument));
292 printf(p, precision, my_xstrtod(argument));
295 printf(p, field_width, my_xstrtod(argument));
297 printf(p, field_width, precision, my_xstrtod(argument));
302 printf(p, *argument);
306 if (field_width < 0) {
310 printf(p, precision, argument);
313 printf(p, field_width, argument);
315 printf(p, field_width, precision, argument);