blob: 2cc238439177598b0c1f4018672414b9ad6485b2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/* printf - format and print data
Eric Andersencc8ed391999-10-05 16:24:54 +00003
Rob Landley251161f2006-01-06 20:28:05 +00004 Copyright 1999 Dave Cinege
5 Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
Eric Andersencc8ed391999-10-05 16:24:54 +00006
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley251161f2006-01-06 20:28:05 +00008*/
Eric Andersencc8ed391999-10-05 16:24:54 +00009
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 Vlasenko13858992006-10-08 12:49:22 +000033 The 'format' argument is re-used as many times as necessary
Eric Andersencc8ed391999-10-05 16:24:54 +000034 to convert all of the given arguments.
35
Denis Vlasenko0f683f82008-07-17 09:17:51 +000036 David MacKenzie <djm@gnu.ai.mit.edu>
37*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000038
Eric Andersencc8ed391999-10-05 16:24:54 +000039// 19990508 Busy Boxed! Dave Cinege
40
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000043/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
44 * They report it:
45 * bash: printf: XXX: invalid number
46 * printf: XXX: expected a numeric value
47 * bash: printf: 123XXX: invalid number
48 * printf: 123XXX: value not completely converted
49 * but then they use 0 (or partially converted numeric prefix) as a value
50 * and continue. They exit with 1 in this case.
51 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
52 * Both print error message and assume 0 if %*.*f width/precision is "bad"
53 * (but negative numbers are not "bad").
54 * Both accept negative numbers for %u specifier.
55 *
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +020056 * We try to be compatible.
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000057 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000058
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000059typedef void FAST_FUNC (*converter)(const char *arg, void *result);
60
61static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000062{
Rob Landley251161f2006-01-06 20:28:05 +000063 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000064 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000065 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000066 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000067 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000068 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +020069 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000070 return 1;
71 }
72 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000073}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000074
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000075static void FAST_FUNC conv_strtoull(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000076{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000077 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +020078 /* both coreutils 6.10 and bash 3.2:
79 * $ printf '%x\n' -2
80 * fffffffffffffffe
81 * Mimic that:
82 */
83 if (errno) {
84 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
85 }
Denis Vlasenkod686a042006-11-27 14:43:21 +000086}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000087static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000088{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000089 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +000090}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000091static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000092{
93 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000094 /* Well, this one allows leading whitespace... so what? */
95 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +000096 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000097 if (end[0]) {
98 errno = ERANGE;
99 *(double*)result = 0;
100 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000101}
102
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000103/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000104static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000105{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000106 unsigned long long result;
107 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000108 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000109 return result;
110}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000111static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000112{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000113 long long result;
114 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000115 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000116 return result;
117}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000118static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000119{
120 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000121 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000122 return result;
123}
124
125static void print_esc_string(char *str)
126{
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000127 while (*str) {
Rob Landley251161f2006-01-06 20:28:05 +0000128 if (*str == '\\') {
129 str++;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000130 bb_putchar(bb_process_escape_sequence((const char **)&str));
Rob Landley251161f2006-01-06 20:28:05 +0000131 } else {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000132 bb_putchar(*str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000133 str++;
Rob Landley251161f2006-01-06 20:28:05 +0000134 }
Rob Landley251161f2006-01-06 20:28:05 +0000135 }
136}
137
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000138static void print_direc(char *format, unsigned fmt_length,
139 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000140 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000141{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000142 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000143 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000144 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000145 char *have_prec, *have_width;
146
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000147 saved = format[fmt_length];
148 format[fmt_length] = '\0';
149
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000150 have_prec = strstr(format, ".*");
151 have_width = strchr(format, '*');
152 if (have_width - 1 == have_prec)
153 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000154
Denys Vlasenko7e603432009-06-27 00:07:23 +0200155 errno = 0;
156
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000157 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000158 case 'c':
159 printf(format, *argument);
160 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000161 case 'd':
162 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000163 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000164 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000165 if (!have_width) {
166 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000167 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000168 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000169 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000170 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000171 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000172 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000173 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000174 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000175 }
176 break;
177 case 'o':
178 case 'u':
179 case 'x':
180 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000181 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000182 /* cheat: unsigned long and long have same width, so... */
183 goto print_long;
184 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000185 /* Are char* and long long the same? */
186 if (sizeof(argument) == sizeof(llv)) {
187 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000188 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000189 } else {
190 /* Hope compiler will optimize it out by moving call
191 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000192 if (!have_width) {
193 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000194 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000195 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000196 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000197 } else {
198 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000199 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000200 else
201 printf(format, field_width, precision, argument);
202 }
203 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000204 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000205 case 'f':
206 case 'e':
207 case 'E':
208 case 'g':
209 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000210 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000211 if (!have_width) {
212 if (!have_prec)
213 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000214 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000215 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000216 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000217 if (!have_prec)
218 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000219 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000220 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000221 }
222 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000223 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000224
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000225 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000226}
227
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000228/* Handle params for "%*.*f". Negative numbers are ok (compat). */
229static int get_width_prec(const char *str)
230{
231 int v = bb_strtoi(str, NULL, 10);
232 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200233 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000234 v = 0;
235 }
236 return v;
237}
238
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000239/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
240 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200241static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000242{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000243 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000244 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000245 int field_width; /* Arg to first '*' */
246 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000247 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000248
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000249 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 switch (*f) {
251 case '%':
252 direc_start = f++;
253 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000254 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000256 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 break;
258 }
259 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000260 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 print_esc_string(*argv);
262 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 }
264 break;
265 }
266 if (strchr("-+ #", *f)) {
267 ++f;
268 ++direc_length;
269 }
270 if (*f == '*') {
271 ++f;
272 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000273 if (*argv)
274 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000275 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000276 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 ++f;
278 ++direc_length;
279 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000280 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 if (*f == '.') {
282 ++f;
283 ++direc_length;
284 if (*f == '*') {
285 ++f;
286 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000287 if (*argv)
288 precision = get_width_prec(*argv++);
289 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000290 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 ++f;
292 ++direc_length;
293 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000294 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000296
297 /* Remove "lLhz" size modifiers, repeatedly.
298 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200299 * happily takes even "%Llllhhzhhzd"!
300 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000301 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000302 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000304 /* Add "ll" if integer modifier, then print */
305 {
306 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
307 char *p = strchr(format_chars, *f);
308 /* needed - try "printf %" without it */
309 if (p == NULL) {
310 bb_error_msg("%s: invalid format", direc_start);
311 /* causes main() to exit with error */
312 return saved_argv - 1;
313 }
314 ++direc_length;
315 if (p - format_chars <= 5) {
316 /* it is one of "diouxX" */
317 p = xmalloc(direc_length + 3);
318 memcpy(p, direc_start, direc_length);
319 p[direc_length + 1] = p[direc_length - 1];
320 p[direc_length - 1] = 'l';
321 p[direc_length] = 'l';
322 //bb_error_msg("<%s>", p);
323 direc_length += 2;
324 direc_start = p;
325 } else {
326 p = NULL;
327 }
328 if (*argv) {
329 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200330 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000331 } else {
332 print_direc(direc_start, direc_length, field_width,
333 precision, "");
334 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200335 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000336 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000337 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000340 if (*++f == 'c') {
341 return saved_argv; /* causes main() to exit */
342 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000343 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000344 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000347 bb_putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000348 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000349 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000350
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000351 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000352}
353
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000354int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000355{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200356 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000357 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000358 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000359
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000360 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000361 * The reason for this is highly non-obvious.
362 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000363 * Shell must correctly handle 'printf "%s" foo'
364 * if stdout is closed. With stdio, output gets shoveled into
365 * stdout buffer, and even fflush cannot clear it out. It seems that
366 * even if libc receives EBADF on write attempts, it feels determined
367 * to output data no matter what. So it will try later,
368 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000369// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
370 if (fcntl(1, F_GETFL) == -1)
371 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
372 //if (dup2(1, 1) != 1) - old way
373 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000374
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000375 /* bash builtin errors out on "printf '-%s-\n' foo",
376 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
377 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000378 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000379 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000380 if (!argv[1]) {
381 if (ENABLE_ASH_BUILTIN_PRINTF
382 && applet_name[0] != 'p'
383 ) {
384 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
385 return 2; /* bash compat */
386 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000387 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000388 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000389
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000390 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000391 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000392
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200393 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000394 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000395 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200396 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000397 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000398
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000399 /* coreutils compat (bash doesn't do this):
400 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000401 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000402 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000403
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200404 return (argv2 < argv) /* if true, print_formatted errored out */
405 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000406}