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 #include <sys/types.h>
52 static int print_formatted (char *format, int argc, char **argv);
53 static void print_direc (char *start, size_t length,
54 int field_width, int precision, char *argument);
56 typedef int (*converter)(char *arg, void *result);
57 static void multiconvert(char *arg, void *result, converter convert)
60 if (*arg == '"' || *arg == '\'') {
61 sprintf(s,"%d",(unsigned)*(++arg));
64 if(convert(arg,result)) fprintf(stderr, "%s", arg);
67 static unsigned long xstrtoul(char *arg)
71 multiconvert(arg,&result, (converter)safe_strtoul);
75 static long xstrtol(char *arg)
78 multiconvert(arg, &result, (converter)safe_strtol);
82 static double xstrtod(char *arg)
85 multiconvert(arg, &result, (converter)safe_strtod);
89 static void print_esc_string(char *str)
94 putchar(bb_process_escape_sequence((const char **)&str));
102 int printf_main(int argc, char **argv)
107 if (argc <= 1 || **(argv + 1) == '-') {
116 args_used = print_formatted(format, argc, argv);
120 while (args_used > 0 && argc > 0);
124 fprintf(stderr, "excess args ignored");
130 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
131 arguments to any `%' directives.
132 Return the number of elements of ARGV used. */
134 static int print_formatted(char *format, int argc, char **argv)
136 int save_argc = argc; /* Preserve original value. */
137 char *f; /* Pointer into `format'. */
138 char *direc_start; /* Start of % directive. */
139 size_t direc_length; /* Length of % directive. */
140 int field_width; /* Arg to first '*', or -1 if none. */
141 int precision; /* Arg to second '*', or -1 if none. */
143 for (f = format; *f; ++f) {
148 field_width = precision = -1;
155 print_esc_string(*argv);
161 if (strchr("-+ #", *f)) {
169 field_width = xstrtoul(*argv);
175 while (isdigit(*f)) {
186 precision = xstrtoul(*argv);
192 while (isdigit(*f)) {
197 if (*f == 'l' || *f == 'L' || *f == 'h') {
202 if (!strchr ("diouxXfeEgGcs", *f))
203 fprintf(stderr, "%%%c: invalid directive", *f);
207 print_direc(direc_start, direc_length, field_width,
212 print_direc(direc_start, direc_length, field_width,
219 putchar(bb_process_escape_sequence((const char **)&f));
228 return save_argc - argc;
232 print_direc(char *start, size_t length, int field_width, int precision,
235 char *p; /* Null-terminated copy of % directive. */
237 p = xmalloc((unsigned) (length + 1));
238 strncpy(p, start, length);
241 switch (p[length - 1]) {
244 if (field_width < 0) {
246 printf(p, xstrtol(argument));
248 printf(p, precision, xstrtol(argument));
251 printf(p, field_width, xstrtol(argument));
253 printf(p, field_width, precision, xstrtol(argument));
261 if (field_width < 0) {
263 printf(p, xstrtoul(argument));
265 printf(p, precision, xstrtoul(argument));
268 printf(p, field_width, xstrtoul(argument));
270 printf(p, field_width, precision, xstrtoul(argument));
279 if (field_width < 0) {
281 printf(p, xstrtod(argument));
283 printf(p, precision, xstrtod(argument));
286 printf(p, field_width, xstrtod(argument));
288 printf(p, field_width, precision, xstrtod(argument));
293 printf(p, *argument);
297 if (field_width < 0) {
301 printf(p, precision, argument);
304 printf(p, field_width, argument);
306 printf(p, field_width, precision, argument);