blob: 5b326c643d3c863ac14ccbcfa6af86381952b4a6 [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
Rob Landley251161f2006-01-06 20:28:05 +00007 Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
8*/
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) {
69 bb_error_msg("%s: invalid number", arg);
70 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
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000155 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000156 case 'c':
157 printf(format, *argument);
158 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000159 case 'd':
160 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000161 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000162 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000163 if (!have_width) {
164 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000165 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000166 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000167 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000168 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000169 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000170 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000171 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000172 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000173 }
174 break;
175 case 'o':
176 case 'u':
177 case 'x':
178 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000179 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000180 /* cheat: unsigned long and long have same width, so... */
181 goto print_long;
182 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000183 /* Are char* and long long the same? */
184 if (sizeof(argument) == sizeof(llv)) {
185 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000186 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000187 } else {
188 /* Hope compiler will optimize it out by moving call
189 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000190 if (!have_width) {
191 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000192 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000193 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000194 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000195 } else {
196 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000197 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000198 else
199 printf(format, field_width, precision, argument);
200 }
201 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000202 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000203 case 'f':
204 case 'e':
205 case 'E':
206 case 'g':
207 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000208 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000209 if (!have_width) {
210 if (!have_prec)
211 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000212 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000213 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000214 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000215 if (!have_prec)
216 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000217 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000218 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000219 }
220 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000221 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000222
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000223 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000224}
225
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000226/* Handle params for "%*.*f". Negative numbers are ok (compat). */
227static int get_width_prec(const char *str)
228{
229 int v = bb_strtoi(str, NULL, 10);
230 if (errno) {
231 bb_error_msg("%s: invalid number", str);
232 v = 0;
233 }
234 return v;
235}
236
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000237/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
238 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200239static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000240{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000241 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000242 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000243 int field_width; /* Arg to first '*' */
244 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000245 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000246
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000247 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 switch (*f) {
249 case '%':
250 direc_start = f++;
251 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000252 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000254 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 break;
256 }
257 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000258 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 print_esc_string(*argv);
260 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 }
262 break;
263 }
264 if (strchr("-+ #", *f)) {
265 ++f;
266 ++direc_length;
267 }
268 if (*f == '*') {
269 ++f;
270 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000271 if (*argv)
272 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000273 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000274 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 ++f;
276 ++direc_length;
277 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000278 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 if (*f == '.') {
280 ++f;
281 ++direc_length;
282 if (*f == '*') {
283 ++f;
284 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000285 if (*argv)
286 precision = get_width_prec(*argv++);
287 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000288 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289 ++f;
290 ++direc_length;
291 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000292 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000294
295 /* Remove "lLhz" size modifiers, repeatedly.
296 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200297 * happily takes even "%Llllhhzhhzd"!
298 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000299 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000300 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000302 /* Add "ll" if integer modifier, then print */
303 {
304 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
305 char *p = strchr(format_chars, *f);
306 /* needed - try "printf %" without it */
307 if (p == NULL) {
308 bb_error_msg("%s: invalid format", direc_start);
309 /* causes main() to exit with error */
310 return saved_argv - 1;
311 }
312 ++direc_length;
313 if (p - format_chars <= 5) {
314 /* it is one of "diouxX" */
315 p = xmalloc(direc_length + 3);
316 memcpy(p, direc_start, direc_length);
317 p[direc_length + 1] = p[direc_length - 1];
318 p[direc_length - 1] = 'l';
319 p[direc_length] = 'l';
320 //bb_error_msg("<%s>", p);
321 direc_length += 2;
322 direc_start = p;
323 } else {
324 p = NULL;
325 }
326 if (*argv) {
327 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200328 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000329 } else {
330 print_direc(direc_start, direc_length, field_width,
331 precision, "");
332 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200333 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000334 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000335 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000338 if (*++f == 'c') {
339 return saved_argv; /* causes main() to exit */
340 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000341 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000342 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000345 bb_putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000347 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000348
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000349 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000350}
351
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000352int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000353{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200354 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000355 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000356 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000357
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000358 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000359 * The reason for this is highly non-obvious.
360 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000361 * Shell must correctly handle 'printf "%s" foo'
362 * if stdout is closed. With stdio, output gets shoveled into
363 * stdout buffer, and even fflush cannot clear it out. It seems that
364 * even if libc receives EBADF on write attempts, it feels determined
365 * to output data no matter what. So it will try later,
366 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000367// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
368 if (fcntl(1, F_GETFL) == -1)
369 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
370 //if (dup2(1, 1) != 1) - old way
371 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000372
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000373 /* bash builtin errors out on "printf '-%s-\n' foo",
374 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
375 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000376 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000377 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000378 if (!argv[1]) {
379 if (ENABLE_ASH_BUILTIN_PRINTF
380 && applet_name[0] != 'p'
381 ) {
382 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
383 return 2; /* bash compat */
384 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000385 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000386 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000388 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000389 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000390
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200391 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000392 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000393 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200394 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000395 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000396
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000397 /* coreutils compat (bash doesn't do this):
398 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000399 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000400 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000401
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200402 return (argv2 < argv) /* if true, print_formatted errored out */
403 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000404}