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, char *argument);
47 typedef int (*converter)(char *arg, void *result);
49 static void multiconvert(char *arg, void *result, converter convert)
52 if (*arg == '"' || *arg == '\'') {
53 sprintf(s, "%d", (unsigned)arg[1]);
56 if (convert(arg, result))
60 static unsigned long my_xstrtoul(char *arg)
64 multiconvert(arg, &result, (converter)safe_strtoul);
68 static long my_xstrtol(char *arg)
71 multiconvert(arg, &result, (converter)safe_strtol);
75 static double my_xstrtod(char *arg)
78 multiconvert(arg, &result, (converter)safe_strtod);
82 static void print_esc_string(char *str)
87 putchar(bb_process_escape_sequence((const char **)&str));
95 int printf_main(int argc, char **argv)
100 if (argc <= 1 || argv[1][0] == '-') {
109 args_used = print_formatted(format, argc, argv);
113 while (args_used > 0 && argc > 0);
116 fprintf(stderr, "excess args ignored");
122 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
123 arguments to any '%' directives.
124 Return the number of elements of ARGV used. */
126 static int print_formatted(char *format, int argc, char **argv)
128 int save_argc = argc; /* Preserve original value. */
129 char *f; /* Pointer into 'format'. */
130 char *direc_start; /* Start of % directive. */
131 size_t direc_length; /* Length of % directive. */
132 int field_width; /* Arg to first '*', or -1 if none. */
133 int precision; /* Arg to second '*', or -1 if none. */
135 for (f = format; *f; ++f) {
140 field_width = precision = -1;
147 print_esc_string(*argv);
153 if (strchr("-+ #", *f)) {
161 field_width = my_xstrtoul(*argv);
167 while (isdigit(*f)) {
178 precision = my_xstrtoul(*argv);
184 while (isdigit(*f)) {
189 if (*f == 'l' || *f == 'L' || *f == 'h') {
194 if (!strchr ("diouxXfeEgGcs", *f))
195 fprintf(stderr, "%%%c: invalid directive", *f);
199 print_direc(direc_start, direc_length, field_width,
204 print_direc(direc_start, direc_length, field_width,
211 putchar(bb_process_escape_sequence((const char **)&f));
220 return save_argc - argc;
224 print_direc(char *start, size_t length, int field_width, int precision,
227 char *p; /* Null-terminated copy of % directive. */
229 p = xmalloc((unsigned) (length + 1));
230 strncpy(p, start, length);
233 switch (p[length - 1]) {
236 if (field_width < 0) {
238 printf(p, my_xstrtol(argument));
240 printf(p, precision, my_xstrtol(argument));
243 printf(p, field_width, my_xstrtol(argument));
245 printf(p, field_width, precision, my_xstrtol(argument));
253 if (field_width < 0) {
255 printf(p, my_xstrtoul(argument));
257 printf(p, precision, my_xstrtoul(argument));
260 printf(p, field_width, my_xstrtoul(argument));
262 printf(p, field_width, precision, my_xstrtoul(argument));
271 if (field_width < 0) {
273 printf(p, my_xstrtod(argument));
275 printf(p, precision, my_xstrtod(argument));
278 printf(p, field_width, my_xstrtod(argument));
280 printf(p, field_width, precision, my_xstrtod(argument));
285 printf(p, *argument);
289 if (field_width < 0) {
293 printf(p, precision, argument);
296 printf(p, field_width, argument);
298 printf(p, field_width, precision, argument);