Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* printf - format and print data |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 4 | Copyright 1999 Dave Cinege |
| 5 | Portions copyright (C) 1990-1996 Free Software Foundation, Inc. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 7 | Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | |
| 10 | /* Usage: printf format [argument...] |
| 11 | |
| 12 | A front end to the printf function that lets it be used from the shell. |
| 13 | |
| 14 | Backslash escapes: |
| 15 | |
| 16 | \" = double quote |
| 17 | \\ = backslash |
| 18 | \a = alert (bell) |
| 19 | \b = backspace |
| 20 | \c = produce no further output |
| 21 | \f = form feed |
| 22 | \n = new line |
| 23 | \r = carriage return |
| 24 | \t = horizontal tab |
| 25 | \v = vertical tab |
| 26 | \0ooo = octal number (ooo is 0 to 3 digits) |
| 27 | \xhhh = hexadecimal number (hhh is 1 to 3 digits) |
| 28 | |
| 29 | Additional directive: |
| 30 | |
| 31 | %b = print an argument string, interpreting backslash escapes |
| 32 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 33 | The 'format' argument is re-used as many times as necessary |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | to convert all of the given arguments. |
| 35 | |
| 36 | David MacKenzie <djm@gnu.ai.mit.edu> */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
| 39 | // 19990508 Busy Boxed! Dave Cinege |
| 40 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 41 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 43 | typedef void (*converter)(const char *arg, void *result); |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 45 | static void multiconvert(const char *arg, void *result, converter convert) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 46 | { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 47 | char s[sizeof(int)*3 + 2]; |
| 48 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 49 | if (*arg == '"' || *arg == '\'') { |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 50 | sprintf(s, "%d", (unsigned char)arg[1]); |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 51 | arg = s; |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 52 | } |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 53 | convert(arg, result); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 54 | /* if there was conversion error, print unconverted string */ |
| 55 | if (errno) |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 56 | fputs(arg, stderr); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 57 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 59 | static void conv_strtoul(const char *arg, void *result) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 60 | { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 61 | *(unsigned long*)result = bb_strtoul(arg, NULL, 0); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 62 | } |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 63 | static void conv_strtol(const char *arg, void *result) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 64 | { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 65 | *(long*)result = bb_strtol(arg, NULL, 0); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 66 | } |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 67 | static void conv_strtod(const char *arg, void *result) |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 68 | { |
| 69 | char *end; |
| 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; |
| 74 | } |
| 75 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 76 | static unsigned long my_xstrtoul(const char *arg) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 77 | { |
| 78 | unsigned long result; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 79 | multiconvert(arg, &result, conv_strtoul); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 80 | return result; |
| 81 | } |
| 82 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 83 | static long my_xstrtol(const char *arg) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 84 | { |
| 85 | long result; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 86 | multiconvert(arg, &result, conv_strtol); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 87 | return result; |
| 88 | } |
| 89 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 90 | static double my_xstrtod(const char *arg) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 91 | { |
| 92 | double result; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 93 | multiconvert(arg, &result, conv_strtod); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 94 | return result; |
| 95 | } |
| 96 | |
| 97 | static void print_esc_string(char *str) |
| 98 | { |
| 99 | for (; *str; str++) { |
| 100 | if (*str == '\\') { |
| 101 | str++; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 102 | bb_putchar(bb_process_escape_sequence((const char **)&str)); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 103 | } else { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 104 | bb_putchar(*str); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } |
| 108 | } |
| 109 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 110 | static void print_direc(char *format, unsigned fmt_length, |
| 111 | int field_width, int precision, |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 112 | const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 113 | { |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 114 | char saved; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 116 | saved = format[fmt_length]; |
| 117 | format[fmt_length] = '\0'; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | a76669c | 2008-05-31 18:32:56 +0000 | [diff] [blame] | 119 | switch (format[fmt_length - 1]) { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 120 | case 'd': |
| 121 | case 'i': |
| 122 | if (field_width < 0) { |
| 123 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 124 | printf(format, my_xstrtol(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 125 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 126 | printf(format, precision, my_xstrtol(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 127 | } else { |
| 128 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 129 | printf(format, field_width, my_xstrtol(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 130 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 131 | printf(format, field_width, precision, my_xstrtol(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 132 | } |
| 133 | break; |
| 134 | case 'o': |
| 135 | case 'u': |
| 136 | case 'x': |
| 137 | case 'X': |
| 138 | if (field_width < 0) { |
| 139 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 140 | printf(format, my_xstrtoul(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 141 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 142 | printf(format, precision, my_xstrtoul(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 143 | } else { |
| 144 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 145 | printf(format, field_width, my_xstrtoul(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 146 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 147 | printf(format, field_width, precision, my_xstrtoul(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 148 | } |
| 149 | break; |
| 150 | case 'f': |
| 151 | case 'e': |
| 152 | case 'E': |
| 153 | case 'g': |
| 154 | case 'G': |
| 155 | if (field_width < 0) { |
| 156 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 157 | printf(format, my_xstrtod(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 158 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 159 | printf(format, precision, my_xstrtod(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 160 | } else { |
| 161 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 162 | printf(format, field_width, my_xstrtod(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 163 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 164 | printf(format, field_width, precision, my_xstrtod(argument)); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 165 | } |
| 166 | break; |
| 167 | case 'c': |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 168 | printf(format, *argument); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 169 | break; |
| 170 | case 's': |
| 171 | if (field_width < 0) { |
| 172 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 173 | printf(format, argument); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 174 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 175 | printf(format, precision, argument); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 176 | } else { |
| 177 | if (precision < 0) |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 178 | printf(format, field_width, argument); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 179 | else |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 180 | printf(format, field_width, precision, argument); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 181 | } |
| 182 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 183 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 185 | format[fmt_length] = saved; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 188 | /* Print the text in FORMAT, using ARGV for arguments to any '%' directives. |
| 189 | Return advanced ARGV. */ |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 190 | static char **print_formatted(char *f, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 191 | { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 192 | char *direc_start; /* Start of % directive. */ |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 193 | unsigned direc_length; /* Length of % directive. */ |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 194 | int field_width; /* Arg to first '*', or -1 if none. */ |
| 195 | int precision; /* Arg to second '*', or -1 if none. */ |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 196 | char **saved_argv = argv; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 197 | |
Denis Vlasenko | 1d1bba4 | 2008-05-31 11:41:50 +0000 | [diff] [blame] | 198 | for (; *f; ++f) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | switch (*f) { |
| 200 | case '%': |
| 201 | direc_start = f++; |
| 202 | direc_length = 1; |
| 203 | field_width = precision = -1; |
| 204 | if (*f == '%') { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 205 | bb_putchar('%'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | if (*f == 'b') { |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 209 | if (*argv) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | print_esc_string(*argv); |
| 211 | ++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 212 | } |
| 213 | break; |
| 214 | } |
| 215 | if (strchr("-+ #", *f)) { |
| 216 | ++f; |
| 217 | ++direc_length; |
| 218 | } |
| 219 | if (*f == '*') { |
| 220 | ++f; |
| 221 | ++direc_length; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 222 | if (*argv) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 223 | field_width = my_xstrtoul(*argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | ++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | } else |
| 226 | field_width = 0; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 227 | } else { |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 228 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | ++f; |
| 230 | ++direc_length; |
| 231 | } |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 232 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | if (*f == '.') { |
| 234 | ++f; |
| 235 | ++direc_length; |
| 236 | if (*f == '*') { |
| 237 | ++f; |
| 238 | ++direc_length; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 239 | if (*argv) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 240 | precision = my_xstrtoul(*argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | ++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 242 | } else |
| 243 | precision = 0; |
| 244 | } else |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 245 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 246 | ++f; |
| 247 | ++direc_length; |
| 248 | } |
| 249 | } |
| 250 | if (*f == 'l' || *f == 'L' || *f == 'h') { |
| 251 | ++f; |
| 252 | ++direc_length; |
| 253 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 254 | /* |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 255 | if (!strchr ("diouxXfeEgGcs", *f)) |
| 256 | fprintf(stderr, "%%%c: invalid directive", *f); |
| 257 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 258 | ++direc_length; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 259 | if (*argv) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 260 | print_direc(direc_start, direc_length, field_width, |
| 261 | precision, *argv); |
| 262 | ++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | } else |
| 264 | print_direc(direc_start, direc_length, field_width, |
| 265 | precision, ""); |
| 266 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 267 | case '\\': |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 268 | if (*++f == 'c') { |
| 269 | return saved_argv; /* causes main() to exit */ |
| 270 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 271 | bb_putchar(bb_process_escape_sequence((const char **)&f)); |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 272 | f--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 273 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 275 | bb_putchar(*f); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 276 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 278 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 279 | return argv; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 282 | int printf_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 283 | { |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 284 | char *format; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 285 | char **argv2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 286 | |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 287 | /* We must check that stdout is not closed. |
| 288 | * The reason for this is highly non-obvious. printf_main is used from shell. |
| 289 | * Shell must correctly handle 'printf "%s" foo' |
| 290 | * if stdout is closed. With stdio, output gets shoveled into |
| 291 | * stdout buffer, and even fflush cannot clear it out. It seems that |
| 292 | * even if libc receives EBADF on write attempts, it feels determined |
| 293 | * to output data no matter what. So it will try later, |
| 294 | * and possibly will clobber future output. Not good. */ |
| 295 | if (dup2(1, 1) != 1) |
| 296 | return -1; |
| 297 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 298 | /* bash builtin errors out on "printf '-%s-\n' foo", |
| 299 | * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo". |
| 300 | * We will mimic coreutils. */ |
| 301 | if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && argv[1][2] == '\0') |
| 302 | argv++; |
| 303 | if (!argv[1]) |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 304 | bb_show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 305 | |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 306 | format = argv[1]; |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 307 | argv2 = argv + 2; |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 308 | |
| 309 | do { |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 310 | argv = argv2; |
| 311 | argv2 = print_formatted(format, argv); |
| 312 | } while (argv2 != argv && *argv2); |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 313 | |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 314 | /* coreutils compat (bash doesn't do this): |
| 315 | if (*argv) |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 316 | fprintf(stderr, "excess args ignored"); |
Denis Vlasenko | b6c4855 | 2008-05-18 14:28:40 +0000 | [diff] [blame] | 317 | */ |
Denis Vlasenko | 339f5eb | 2007-03-09 16:43:01 +0000 | [diff] [blame] | 318 | |
| 319 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 320 | } |