blob: 3dd43a978205f2ebcbfc3c3f49d7e5e0130264d2 [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
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100134/* Handles %b */
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++;
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 }
148 {
149 /* optimization: don't force arg to be on-stack,
150 * use another variable for that. */
151 const char *z = str;
152 c = bb_process_escape_sequence(&z);
153 str = z;
154 }
155 }
Denys Vlasenko53600592010-10-23 21:06:06 +0200156 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000157 }
158}
159
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000160static void print_direc(char *format, unsigned fmt_length,
161 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000162 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000163{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000164 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000165 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000166 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000167 char *have_prec, *have_width;
168
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000169 saved = format[fmt_length];
170 format[fmt_length] = '\0';
171
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000172 have_prec = strstr(format, ".*");
173 have_width = strchr(format, '*');
174 if (have_width - 1 == have_prec)
175 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000176
Denys Vlasenko7e603432009-06-27 00:07:23 +0200177 errno = 0;
178
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000179 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000180 case 'c':
181 printf(format, *argument);
182 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000183 case 'd':
184 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000185 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000186 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000187 if (!have_width) {
188 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000189 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000190 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000191 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000192 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000193 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000194 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000195 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000196 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000197 }
198 break;
199 case 'o':
200 case 'u':
201 case 'x':
202 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000203 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000204 /* cheat: unsigned long and long have same width, so... */
205 goto print_long;
206 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000207 /* Are char* and long long the same? */
208 if (sizeof(argument) == sizeof(llv)) {
209 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000210 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000211 } else {
212 /* Hope compiler will optimize it out by moving call
213 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000214 if (!have_width) {
215 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000216 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000217 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000218 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000219 } else {
220 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000221 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000222 else
223 printf(format, field_width, precision, argument);
224 }
225 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000226 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000227 case 'f':
228 case 'e':
229 case 'E':
230 case 'g':
231 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000232 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000233 if (!have_width) {
234 if (!have_prec)
235 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000236 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000237 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000238 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000239 if (!have_prec)
240 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000241 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000242 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000243 }
244 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000245 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000246
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000247 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000248}
249
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000250/* Handle params for "%*.*f". Negative numbers are ok (compat). */
251static int get_width_prec(const char *str)
252{
253 int v = bb_strtoi(str, NULL, 10);
254 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200255 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000256 v = 0;
257 }
258 return v;
259}
260
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000261/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
262 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200263static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000264{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000265 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000266 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000267 int field_width; /* Arg to first '*' */
268 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000269 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000270
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000271 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 switch (*f) {
273 case '%':
274 direc_start = f++;
275 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000276 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000278 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 break;
280 }
281 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000282 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 print_esc_string(*argv);
284 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 }
286 break;
287 }
288 if (strchr("-+ #", *f)) {
289 ++f;
290 ++direc_length;
291 }
292 if (*f == '*') {
293 ++f;
294 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000295 if (*argv)
296 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000297 } 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 Vlasenko339f5eb2007-03-09 16:43:01 +0000302 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 if (*f == '.') {
304 ++f;
305 ++direc_length;
306 if (*f == '*') {
307 ++f;
308 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000309 if (*argv)
310 precision = get_width_prec(*argv++);
311 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000312 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 ++f;
314 ++direc_length;
315 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000316 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000318
319 /* Remove "lLhz" size modifiers, repeatedly.
320 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200321 * happily takes even "%Llllhhzhhzd"!
322 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000323 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000324 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000326 /* Add "ll" if integer modifier, then print */
327 {
328 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
329 char *p = strchr(format_chars, *f);
330 /* needed - try "printf %" without it */
331 if (p == NULL) {
332 bb_error_msg("%s: invalid format", direc_start);
333 /* causes main() to exit with error */
334 return saved_argv - 1;
335 }
336 ++direc_length;
337 if (p - format_chars <= 5) {
338 /* it is one of "diouxX" */
339 p = xmalloc(direc_length + 3);
340 memcpy(p, direc_start, direc_length);
341 p[direc_length + 1] = p[direc_length - 1];
342 p[direc_length - 1] = 'l';
343 p[direc_length] = 'l';
344 //bb_error_msg("<%s>", p);
345 direc_length += 2;
346 direc_start = p;
347 } else {
348 p = NULL;
349 }
350 if (*argv) {
351 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200352 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000353 } else {
354 print_direc(direc_start, direc_length, field_width,
355 precision, "");
356 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200357 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000358 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000359 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000362 if (*++f == 'c') {
363 return saved_argv; /* causes main() to exit */
364 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000365 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000366 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200369 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000371 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000372
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000373 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000374}
375
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000376int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000377{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200378 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000379 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000380 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000381
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000382 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000383 * The reason for this is highly non-obvious.
384 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000385 * Shell must correctly handle 'printf "%s" foo'
386 * if stdout is closed. With stdio, output gets shoveled into
387 * stdout buffer, and even fflush cannot clear it out. It seems that
388 * even if libc receives EBADF on write attempts, it feels determined
389 * to output data no matter what. So it will try later,
390 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000391// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
392 if (fcntl(1, F_GETFL) == -1)
393 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
394 //if (dup2(1, 1) != 1) - old way
395 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000396
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000397 /* bash builtin errors out on "printf '-%s-\n' foo",
398 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
399 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000400 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000401 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000402 if (!argv[1]) {
403 if (ENABLE_ASH_BUILTIN_PRINTF
404 && applet_name[0] != 'p'
405 ) {
406 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
407 return 2; /* bash compat */
408 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000409 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000410 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000411
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000412 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000413 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000414
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200415 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000416 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000417 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200418 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000419 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000420
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000421 /* coreutils compat (bash doesn't do this):
422 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000423 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000424 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000425
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200426 return (argv2 < argv) /* if true, print_formatted errored out */
427 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000428}