blob: 9ee7350d01d0989bfa0add727e8b5eaaf18e7132 [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
Pere Orga34425382011-03-31 14:43:25 +020041//usage:#define printf_trivial_usage
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010042//usage: "FORMAT [ARG]..."
Pere Orga34425382011-03-31 14:43:25 +020043//usage:#define printf_full_usage "\n\n"
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010044//usage: "Format and print ARG(s) according to FORMAT (a-la C printf)"
Pere Orga34425382011-03-31 14:43:25 +020045//usage:
46//usage:#define printf_example_usage
47//usage: "$ printf \"Val=%d\\n\" 5\n"
48//usage: "Val=5\n"
49
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000050#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000052/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
53 * They report it:
54 * bash: printf: XXX: invalid number
55 * printf: XXX: expected a numeric value
56 * bash: printf: 123XXX: invalid number
57 * printf: 123XXX: value not completely converted
58 * but then they use 0 (or partially converted numeric prefix) as a value
59 * and continue. They exit with 1 in this case.
60 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
61 * Both print error message and assume 0 if %*.*f width/precision is "bad"
62 * (but negative numbers are not "bad").
63 * Both accept negative numbers for %u specifier.
64 *
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +020065 * We try to be compatible.
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000066 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000067
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000068typedef void FAST_FUNC (*converter)(const char *arg, void *result);
69
70static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000071{
Rob Landley251161f2006-01-06 20:28:05 +000072 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000073 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000074 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000075 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000076 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000077 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +020078 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000079 return 1;
80 }
81 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000082}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000084static void FAST_FUNC conv_strtoull(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000085{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000086 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +020087 /* both coreutils 6.10 and bash 3.2:
88 * $ printf '%x\n' -2
89 * fffffffffffffffe
90 * Mimic that:
91 */
92 if (errno) {
93 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
94 }
Denis Vlasenkod686a042006-11-27 14:43:21 +000095}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000096static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000097{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000098 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +000099}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000100static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000101{
102 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000103 /* Well, this one allows leading whitespace... so what? */
104 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +0000105 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000106 if (end[0]) {
107 errno = ERANGE;
108 *(double*)result = 0;
109 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000110}
111
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000112/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000113static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000114{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000115 unsigned long long result;
116 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000117 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000118 return result;
119}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000120static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000121{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000122 long long result;
123 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000124 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000125 return result;
126}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000127static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000128{
129 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000130 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000131 return result;
132}
133
Ron Yorston4a792242016-01-31 22:22:25 +0100134/* Handles %b; return 1 if output is to be short-circuited by \c */
135static int 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++;
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100140 if (c == '\\') {
141 /* %b also accepts 4-digit octals of the form \0### */
142 if (*str == '0') {
143 if ((unsigned char)(str[1] - '0') < 8) {
144 /* 2nd char is 0..7: skip leading '0' */
145 str++;
146 }
147 }
Ron Yorston4a792242016-01-31 22:22:25 +0100148 else if (*str == 'c') {
149 return 1;
150 }
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100151 {
152 /* optimization: don't force arg to be on-stack,
153 * use another variable for that. */
154 const char *z = str;
155 c = bb_process_escape_sequence(&z);
156 str = z;
157 }
158 }
Denys Vlasenko53600592010-10-23 21:06:06 +0200159 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000160 }
Ron Yorston4a792242016-01-31 22:22:25 +0100161
162 return 0;
Rob Landley251161f2006-01-06 20:28:05 +0000163}
164
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000165static void print_direc(char *format, unsigned fmt_length,
166 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000167 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000168{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000169 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000170 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000171 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000172 char *have_prec, *have_width;
173
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000174 saved = format[fmt_length];
175 format[fmt_length] = '\0';
176
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000177 have_prec = strstr(format, ".*");
178 have_width = strchr(format, '*');
179 if (have_width - 1 == have_prec)
180 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000181
Denys Vlasenko7e603432009-06-27 00:07:23 +0200182 errno = 0;
183
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000184 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000185 case 'c':
186 printf(format, *argument);
187 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000188 case 'd':
189 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000190 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000191 print_long:
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, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000195 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000196 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000197 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000198 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000199 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000200 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000201 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000202 }
203 break;
204 case 'o':
205 case 'u':
206 case 'x':
207 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000208 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000209 /* cheat: unsigned long and long have same width, so... */
210 goto print_long;
211 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000212 /* Are char* and long long the same? */
213 if (sizeof(argument) == sizeof(llv)) {
214 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000215 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000216 } else {
217 /* Hope compiler will optimize it out by moving call
218 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000219 if (!have_width) {
220 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000221 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000222 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000223 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000224 } else {
225 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000226 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000227 else
228 printf(format, field_width, precision, argument);
229 }
230 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000231 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000232 case 'f':
233 case 'e':
234 case 'E':
235 case 'g':
236 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000237 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000238 if (!have_width) {
239 if (!have_prec)
240 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000241 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000242 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000243 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000244 if (!have_prec)
245 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000246 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000247 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000248 }
249 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000250 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000251
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000252 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000253}
254
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000255/* Handle params for "%*.*f". Negative numbers are ok (compat). */
256static int get_width_prec(const char *str)
257{
258 int v = bb_strtoi(str, NULL, 10);
259 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200260 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000261 v = 0;
262 }
263 return v;
264}
265
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000266/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
267 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200268static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000269{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000270 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000271 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000272 int field_width; /* Arg to first '*' */
273 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000274 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000275
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000276 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 switch (*f) {
278 case '%':
279 direc_start = f++;
280 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000281 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000283 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 break;
285 }
286 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000287 if (*argv) {
Ron Yorston4a792242016-01-31 22:22:25 +0100288 if (print_esc_string(*argv))
289 return saved_argv; /* causes main() to exit */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 }
292 break;
293 }
294 if (strchr("-+ #", *f)) {
295 ++f;
296 ++direc_length;
297 }
298 if (*f == '*') {
299 ++f;
300 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000301 if (*argv)
302 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000303 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000304 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 ++f;
306 ++direc_length;
307 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000308 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 if (*f == '.') {
310 ++f;
311 ++direc_length;
312 if (*f == '*') {
313 ++f;
314 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000315 if (*argv)
316 precision = get_width_prec(*argv++);
317 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000318 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 ++f;
320 ++direc_length;
321 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000322 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000324
325 /* Remove "lLhz" size modifiers, repeatedly.
326 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200327 * happily takes even "%Llllhhzhhzd"!
328 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000329 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000330 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000332 /* Add "ll" if integer modifier, then print */
333 {
334 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
335 char *p = strchr(format_chars, *f);
336 /* needed - try "printf %" without it */
337 if (p == NULL) {
338 bb_error_msg("%s: invalid format", direc_start);
339 /* causes main() to exit with error */
340 return saved_argv - 1;
341 }
342 ++direc_length;
343 if (p - format_chars <= 5) {
344 /* it is one of "diouxX" */
345 p = xmalloc(direc_length + 3);
346 memcpy(p, direc_start, direc_length);
347 p[direc_length + 1] = p[direc_length - 1];
348 p[direc_length - 1] = 'l';
349 p[direc_length] = 'l';
350 //bb_error_msg("<%s>", p);
351 direc_length += 2;
352 direc_start = p;
353 } else {
354 p = NULL;
355 }
356 if (*argv) {
357 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200358 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000359 } else {
360 print_direc(direc_start, direc_length, field_width,
361 precision, "");
362 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200363 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000364 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000365 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000368 if (*++f == 'c') {
369 return saved_argv; /* causes main() to exit */
370 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000371 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000372 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200375 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000377 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000378
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000379 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000380}
381
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000382int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000383{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200384 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000385 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000386 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000388 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000389 * The reason for this is highly non-obvious.
390 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000391 * Shell must correctly handle 'printf "%s" foo'
392 * if stdout is closed. With stdio, output gets shoveled into
393 * stdout buffer, and even fflush cannot clear it out. It seems that
394 * even if libc receives EBADF on write attempts, it feels determined
395 * to output data no matter what. So it will try later,
396 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000397// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
398 if (fcntl(1, F_GETFL) == -1)
399 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
400 //if (dup2(1, 1) != 1) - old way
401 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000402
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000403 /* bash builtin errors out on "printf '-%s-\n' foo",
404 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
405 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000406 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000407 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000408 if (!argv[1]) {
409 if (ENABLE_ASH_BUILTIN_PRINTF
410 && applet_name[0] != 'p'
411 ) {
412 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
413 return 2; /* bash compat */
414 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000415 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000416 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000417
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000418 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000419 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000420
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200421 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000422 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000423 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200424 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000425 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000426
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000427 /* coreutils compat (bash doesn't do this):
428 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000429 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000430 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000431
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200432 return (argv2 < argv) /* if true, print_formatted errored out */
433 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000434}