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 putchar(bb_process_escape_sequence((const char **)&str));
110 static void print_direc(char *start, size_t length, int field_width, int precision,
111 const char *argument)
113 char *p; /* Null-terminated copy of % directive. */
115 p = xmalloc((unsigned) (length + 1));
116 strncpy(p, start, length);
119 switch (p[length - 1]) {
122 if (field_width < 0) {
124 printf(p, my_xstrtol(argument));
126 printf(p, precision, my_xstrtol(argument));
129 printf(p, field_width, my_xstrtol(argument));
131 printf(p, field_width, precision, my_xstrtol(argument));
138 if (field_width < 0) {
140 printf(p, my_xstrtoul(argument));
142 printf(p, precision, my_xstrtoul(argument));
145 printf(p, field_width, my_xstrtoul(argument));
147 printf(p, field_width, precision, my_xstrtoul(argument));
155 if (field_width < 0) {
157 printf(p, my_xstrtod(argument));
159 printf(p, precision, my_xstrtod(argument));
162 printf(p, field_width, my_xstrtod(argument));
164 printf(p, field_width, precision, my_xstrtod(argument));
168 printf(p, *argument);
171 if (field_width < 0) {
175 printf(p, precision, argument);
178 printf(p, field_width, argument);
180 printf(p, field_width, precision, argument);
188 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
189 arguments to any '%' directives.
190 Return the number of elements of ARGV used. */
192 static int print_formatted(char *format, int argc, char **argv)
194 int save_argc = argc; /* Preserve original value. */
195 char *f; /* Pointer into 'format'. */
196 char *direc_start; /* Start of % directive. */
197 size_t direc_length; /* Length of % directive. */
198 int field_width; /* Arg to first '*', or -1 if none. */
199 int precision; /* Arg to second '*', or -1 if none. */
201 for (f = format; *f; ++f) {
206 field_width = precision = -1;
213 print_esc_string(*argv);
219 if (strchr("-+ #", *f)) {
227 field_width = my_xstrtoul(*argv);
233 while (isdigit(*f)) {
245 precision = my_xstrtoul(*argv);
251 while (isdigit(*f)) {
256 if (*f == 'l' || *f == 'L' || *f == 'h') {
261 if (!strchr ("diouxXfeEgGcs", *f))
262 fprintf(stderr, "%%%c: invalid directive", *f);
266 print_direc(direc_start, direc_length, field_width,
271 print_direc(direc_start, direc_length, field_width,
277 putchar(bb_process_escape_sequence((const char **)&f));
285 return save_argc - argc;
288 int printf_main(int argc, char **argv);
289 int printf_main(int argc, char **argv)
294 if (argc <= 1 || argv[1][0] == '-') {
303 args_used = print_formatted(format, argc, argv);
306 } while (args_used > 0 && argc > 0);
309 fprintf(stderr, "excess args ignored");