blob: f53aa4787ac78421e56e8a2cb8174118174ccf6f [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
Pere Orga34425382011-03-31 14:43:25 +020041//usage:#define printf_trivial_usage
42//usage: "FORMAT [ARGUMENT]..."
43//usage:#define printf_full_usage "\n\n"
44//usage: "Format and print ARGUMENT(s) according to FORMAT,\n"
45//usage: "where FORMAT controls the output exactly as in C printf"
46//usage:
47//usage:#define printf_example_usage
48//usage: "$ printf \"Val=%d\\n\" 5\n"
49//usage: "Val=5\n"
50
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000051#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000053/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
54 * They report it:
55 * bash: printf: XXX: invalid number
56 * printf: XXX: expected a numeric value
57 * bash: printf: 123XXX: invalid number
58 * printf: 123XXX: value not completely converted
59 * but then they use 0 (or partially converted numeric prefix) as a value
60 * and continue. They exit with 1 in this case.
61 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
62 * Both print error message and assume 0 if %*.*f width/precision is "bad"
63 * (but negative numbers are not "bad").
64 * Both accept negative numbers for %u specifier.
65 *
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +020066 * We try to be compatible.
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000067 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000068
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000069typedef void FAST_FUNC (*converter)(const char *arg, void *result);
70
71static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000072{
Rob Landley251161f2006-01-06 20:28:05 +000073 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000074 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000075 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000076 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000077 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000078 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +020079 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000080 return 1;
81 }
82 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000083}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000084
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000085static void FAST_FUNC conv_strtoull(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000086{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000087 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +020088 /* both coreutils 6.10 and bash 3.2:
89 * $ printf '%x\n' -2
90 * fffffffffffffffe
91 * Mimic that:
92 */
93 if (errno) {
94 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
95 }
Denis Vlasenkod686a042006-11-27 14:43:21 +000096}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000097static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000098{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000099 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000100}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000101static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000102{
103 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000104 /* Well, this one allows leading whitespace... so what? */
105 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +0000106 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000107 if (end[0]) {
108 errno = ERANGE;
109 *(double*)result = 0;
110 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000111}
112
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000113/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000114static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000115{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000116 unsigned long long result;
117 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000118 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000119 return result;
120}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000121static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000122{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000123 long long result;
124 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000125 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000126 return result;
127}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000128static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000129{
130 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000131 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000132 return result;
133}
134
Denys Vlasenko53600592010-10-23 21:06:06 +0200135static void print_esc_string(const char *str)
Rob Landley251161f2006-01-06 20:28:05 +0000136{
Denys Vlasenko53600592010-10-23 21:06:06 +0200137 char c;
138 while ((c = *str) != '\0') {
139 str++;
140 if (c == '\\')
141 c = bb_process_escape_sequence(&str);
142 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000143 }
144}
145
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000146static void print_direc(char *format, unsigned fmt_length,
147 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000148 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000149{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000150 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000151 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000152 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000153 char *have_prec, *have_width;
154
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000155 saved = format[fmt_length];
156 format[fmt_length] = '\0';
157
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000158 have_prec = strstr(format, ".*");
159 have_width = strchr(format, '*');
160 if (have_width - 1 == have_prec)
161 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Denys Vlasenko7e603432009-06-27 00:07:23 +0200163 errno = 0;
164
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000165 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000166 case 'c':
167 printf(format, *argument);
168 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000169 case 'd':
170 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000171 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000172 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000173 if (!have_width) {
174 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000175 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000176 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000177 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000178 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000179 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000180 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000181 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000182 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000183 }
184 break;
185 case 'o':
186 case 'u':
187 case 'x':
188 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000189 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000190 /* cheat: unsigned long and long have same width, so... */
191 goto print_long;
192 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000193 /* Are char* and long long the same? */
194 if (sizeof(argument) == sizeof(llv)) {
195 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000196 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000197 } else {
198 /* Hope compiler will optimize it out by moving call
199 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000200 if (!have_width) {
201 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000202 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000203 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000204 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000205 } else {
206 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000207 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000208 else
209 printf(format, field_width, precision, argument);
210 }
211 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000212 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000213 case 'f':
214 case 'e':
215 case 'E':
216 case 'g':
217 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000218 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000219 if (!have_width) {
220 if (!have_prec)
221 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000222 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000223 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000224 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000225 if (!have_prec)
226 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000227 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000228 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000229 }
230 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000231 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000232
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000233 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000234}
235
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000236/* Handle params for "%*.*f". Negative numbers are ok (compat). */
237static int get_width_prec(const char *str)
238{
239 int v = bb_strtoi(str, NULL, 10);
240 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200241 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000242 v = 0;
243 }
244 return v;
245}
246
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000247/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
248 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200249static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000250{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000251 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000252 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000253 int field_width; /* Arg to first '*' */
254 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000255 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000256
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000257 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 switch (*f) {
259 case '%':
260 direc_start = f++;
261 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000262 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000264 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 break;
266 }
267 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000268 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 print_esc_string(*argv);
270 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 }
272 break;
273 }
274 if (strchr("-+ #", *f)) {
275 ++f;
276 ++direc_length;
277 }
278 if (*f == '*') {
279 ++f;
280 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000281 if (*argv)
282 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000283 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000284 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 ++f;
286 ++direc_length;
287 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000288 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289 if (*f == '.') {
290 ++f;
291 ++direc_length;
292 if (*f == '*') {
293 ++f;
294 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000295 if (*argv)
296 precision = get_width_prec(*argv++);
297 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000298 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 ++f;
300 ++direc_length;
301 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000302 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000304
305 /* Remove "lLhz" size modifiers, repeatedly.
306 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200307 * happily takes even "%Llllhhzhhzd"!
308 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000309 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000310 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000312 /* Add "ll" if integer modifier, then print */
313 {
314 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
315 char *p = strchr(format_chars, *f);
316 /* needed - try "printf %" without it */
317 if (p == NULL) {
318 bb_error_msg("%s: invalid format", direc_start);
319 /* causes main() to exit with error */
320 return saved_argv - 1;
321 }
322 ++direc_length;
323 if (p - format_chars <= 5) {
324 /* it is one of "diouxX" */
325 p = xmalloc(direc_length + 3);
326 memcpy(p, direc_start, direc_length);
327 p[direc_length + 1] = p[direc_length - 1];
328 p[direc_length - 1] = 'l';
329 p[direc_length] = 'l';
330 //bb_error_msg("<%s>", p);
331 direc_length += 2;
332 direc_start = p;
333 } else {
334 p = NULL;
335 }
336 if (*argv) {
337 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200338 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000339 } else {
340 print_direc(direc_start, direc_length, field_width,
341 precision, "");
342 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200343 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000344 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000345 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000348 if (*++f == 'c') {
349 return saved_argv; /* causes main() to exit */
350 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000351 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000352 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200355 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000357 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000358
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000359 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000360}
361
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000362int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000363{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200364 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000365 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000366 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000367
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000368 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000369 * The reason for this is highly non-obvious.
370 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000371 * Shell must correctly handle 'printf "%s" foo'
372 * if stdout is closed. With stdio, output gets shoveled into
373 * stdout buffer, and even fflush cannot clear it out. It seems that
374 * even if libc receives EBADF on write attempts, it feels determined
375 * to output data no matter what. So it will try later,
376 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000377// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
378 if (fcntl(1, F_GETFL) == -1)
379 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
380 //if (dup2(1, 1) != 1) - old way
381 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000382
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000383 /* bash builtin errors out on "printf '-%s-\n' foo",
384 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
385 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000386 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000387 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000388 if (!argv[1]) {
389 if (ENABLE_ASH_BUILTIN_PRINTF
390 && applet_name[0] != 'p'
391 ) {
392 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
393 return 2; /* bash compat */
394 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000395 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000396 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000397
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000398 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000399 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000400
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200401 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000402 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000403 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200404 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000405 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000406
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000407 /* coreutils compat (bash doesn't do this):
408 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000409 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000410 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000411
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200412 return (argv2 < argv) /* if true, print_formatted errored out */
413 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000414}