blob: 6c8e115d85b36e91e89a32a633ec3b5b2d0ac884 [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
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010039/* 19990508 Busy Boxed! Dave Cinege */
Eric Andersencc8ed391999-10-05 16:24:54 +000040
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010041//config:config PRINTF
42//config: bool "printf"
43//config: default y
44//config: help
45//config: printf is used to format and print specified strings.
46//config: It's similar to `echo' except it has more options.
47
48//applet:IF_PRINTF(APPLET_NOFORK(printf, printf, BB_DIR_USR_BIN, BB_SUID_DROP, printf))
49
50//kbuild:lib-$(CONFIG_PRINTF) += printf.o
51
Pere Orga34425382011-03-31 14:43:25 +020052//usage:#define printf_trivial_usage
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010053//usage: "FORMAT [ARG]..."
Pere Orga34425382011-03-31 14:43:25 +020054//usage:#define printf_full_usage "\n\n"
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010055//usage: "Format and print ARG(s) according to FORMAT (a-la C printf)"
Pere Orga34425382011-03-31 14:43:25 +020056//usage:
57//usage:#define printf_example_usage
58//usage: "$ printf \"Val=%d\\n\" 5\n"
59//usage: "Val=5\n"
60
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000061#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000062
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000063/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
64 * They report it:
65 * bash: printf: XXX: invalid number
66 * printf: XXX: expected a numeric value
67 * bash: printf: 123XXX: invalid number
68 * printf: 123XXX: value not completely converted
69 * but then they use 0 (or partially converted numeric prefix) as a value
70 * and continue. They exit with 1 in this case.
71 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
72 * Both print error message and assume 0 if %*.*f width/precision is "bad"
73 * (but negative numbers are not "bad").
74 * Both accept negative numbers for %u specifier.
75 *
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +020076 * We try to be compatible.
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000077 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000078
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000079typedef void FAST_FUNC (*converter)(const char *arg, void *result);
80
81static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000082{
Rob Landley251161f2006-01-06 20:28:05 +000083 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000084 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000085 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000086 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000087 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000088 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +020089 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000090 return 1;
91 }
92 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000093}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000094
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000095static void FAST_FUNC conv_strtoull(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000096{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000097 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +020098 /* both coreutils 6.10 and bash 3.2:
99 * $ printf '%x\n' -2
100 * fffffffffffffffe
101 * Mimic that:
102 */
103 if (errno) {
104 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
105 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000106}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000107static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000108{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000109 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000110}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000111static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000112{
113 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000114 /* Well, this one allows leading whitespace... so what? */
115 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +0000116 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000117 if (end[0]) {
118 errno = ERANGE;
119 *(double*)result = 0;
120 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000121}
122
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000123/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000124static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000125{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000126 unsigned long long result;
127 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000128 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000129 return result;
130}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000131static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000132{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000133 long long result;
134 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000135 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000136 return result;
137}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000138static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000139{
140 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000141 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000142 return result;
143}
144
Ron Yorston4a792242016-01-31 22:22:25 +0100145/* Handles %b; return 1 if output is to be short-circuited by \c */
146static int print_esc_string(const char *str)
Rob Landley251161f2006-01-06 20:28:05 +0000147{
Denys Vlasenko53600592010-10-23 21:06:06 +0200148 char c;
149 while ((c = *str) != '\0') {
150 str++;
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100151 if (c == '\\') {
152 /* %b also accepts 4-digit octals of the form \0### */
153 if (*str == '0') {
154 if ((unsigned char)(str[1] - '0') < 8) {
155 /* 2nd char is 0..7: skip leading '0' */
156 str++;
157 }
158 }
Ron Yorston4a792242016-01-31 22:22:25 +0100159 else if (*str == 'c') {
160 return 1;
161 }
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100162 {
163 /* optimization: don't force arg to be on-stack,
164 * use another variable for that. */
165 const char *z = str;
166 c = bb_process_escape_sequence(&z);
167 str = z;
168 }
169 }
Denys Vlasenko53600592010-10-23 21:06:06 +0200170 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000171 }
Ron Yorston4a792242016-01-31 22:22:25 +0100172
173 return 0;
Rob Landley251161f2006-01-06 20:28:05 +0000174}
175
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000176static void print_direc(char *format, unsigned fmt_length,
177 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000178 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000179{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000180 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000181 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000182 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000183 char *have_prec, *have_width;
184
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000185 saved = format[fmt_length];
186 format[fmt_length] = '\0';
187
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000188 have_prec = strstr(format, ".*");
189 have_width = strchr(format, '*');
190 if (have_width - 1 == have_prec)
191 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000192
Denys Vlasenko7e603432009-06-27 00:07:23 +0200193 errno = 0;
194
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000195 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000196 case 'c':
197 printf(format, *argument);
198 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000199 case 'd':
200 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000201 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000202 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000203 if (!have_width) {
204 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000205 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000206 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000207 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000208 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000209 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000210 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000211 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000212 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000213 }
214 break;
215 case 'o':
216 case 'u':
217 case 'x':
218 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000219 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000220 /* cheat: unsigned long and long have same width, so... */
221 goto print_long;
222 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000223 /* Are char* and long long the same? */
224 if (sizeof(argument) == sizeof(llv)) {
225 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000226 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000227 } else {
228 /* Hope compiler will optimize it out by moving call
229 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000230 if (!have_width) {
231 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000232 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000233 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000234 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000235 } else {
236 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000237 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000238 else
239 printf(format, field_width, precision, argument);
240 }
241 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000242 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000243 case 'f':
244 case 'e':
245 case 'E':
246 case 'g':
247 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000248 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000249 if (!have_width) {
250 if (!have_prec)
251 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000252 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000253 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000254 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000255 if (!have_prec)
256 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000257 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000258 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000259 }
260 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000261 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000262
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000263 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000264}
265
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000266/* Handle params for "%*.*f". Negative numbers are ok (compat). */
267static int get_width_prec(const char *str)
268{
269 int v = bb_strtoi(str, NULL, 10);
270 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200271 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000272 v = 0;
273 }
274 return v;
275}
276
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000277/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
278 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200279static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000280{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000281 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000282 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000283 int field_width; /* Arg to first '*' */
284 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000285 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000286
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000287 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 switch (*f) {
289 case '%':
290 direc_start = f++;
291 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000292 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000294 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295 break;
296 }
297 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000298 if (*argv) {
Ron Yorston4a792242016-01-31 22:22:25 +0100299 if (print_esc_string(*argv))
300 return saved_argv; /* causes main() to exit */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 }
303 break;
304 }
305 if (strchr("-+ #", *f)) {
306 ++f;
307 ++direc_length;
308 }
309 if (*f == '*') {
310 ++f;
311 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000312 if (*argv)
313 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000314 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000315 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 ++f;
317 ++direc_length;
318 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000319 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 if (*f == '.') {
321 ++f;
322 ++direc_length;
323 if (*f == '*') {
324 ++f;
325 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000326 if (*argv)
327 precision = get_width_prec(*argv++);
328 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000329 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 ++f;
331 ++direc_length;
332 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000333 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000335
336 /* Remove "lLhz" size modifiers, repeatedly.
337 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200338 * happily takes even "%Llllhhzhhzd"!
339 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000340 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000341 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000343 /* Add "ll" if integer modifier, then print */
344 {
345 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
346 char *p = strchr(format_chars, *f);
347 /* needed - try "printf %" without it */
348 if (p == NULL) {
349 bb_error_msg("%s: invalid format", direc_start);
350 /* causes main() to exit with error */
351 return saved_argv - 1;
352 }
353 ++direc_length;
354 if (p - format_chars <= 5) {
355 /* it is one of "diouxX" */
356 p = xmalloc(direc_length + 3);
357 memcpy(p, direc_start, direc_length);
358 p[direc_length + 1] = p[direc_length - 1];
359 p[direc_length - 1] = 'l';
360 p[direc_length] = 'l';
361 //bb_error_msg("<%s>", p);
362 direc_length += 2;
363 direc_start = p;
364 } else {
365 p = NULL;
366 }
367 if (*argv) {
368 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200369 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000370 } else {
371 print_direc(direc_start, direc_length, field_width,
372 precision, "");
373 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200374 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000375 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000376 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000379 if (*++f == 'c') {
380 return saved_argv; /* causes main() to exit */
381 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000382 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000383 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200386 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000388 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000389
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000390 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000391}
392
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000393int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000394{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200395 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000396 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000397 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000398
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000399 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000400 * The reason for this is highly non-obvious.
401 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000402 * Shell must correctly handle 'printf "%s" foo'
403 * if stdout is closed. With stdio, output gets shoveled into
404 * stdout buffer, and even fflush cannot clear it out. It seems that
405 * even if libc receives EBADF on write attempts, it feels determined
406 * to output data no matter what. So it will try later,
407 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000408// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
409 if (fcntl(1, F_GETFL) == -1)
410 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
411 //if (dup2(1, 1) != 1) - old way
412 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000413
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000414 /* bash builtin errors out on "printf '-%s-\n' foo",
415 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
416 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000417 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000418 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000419 if (!argv[1]) {
420 if (ENABLE_ASH_BUILTIN_PRINTF
421 && applet_name[0] != 'p'
422 ) {
423 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
424 return 2; /* bash compat */
425 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000426 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000427 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000428
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000429 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000430 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000431
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200432 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000433 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000434 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200435 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000436 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000437
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000438 /* coreutils compat (bash doesn't do this):
439 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000440 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000441 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000442
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200443 return (argv2 < argv) /* if true, print_formatted errored out */
444 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000445}