blob: acaf076ff971329726d6cd99353d5f29a2ed4f9b [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020042//config: bool "printf (3.3 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010043//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
Denys Vlasenko265062d2017-01-10 15:13:30 +010052//kbuild:lib-$(CONFIG_ASH_PRINTF) += printf.o
53//kbuild:lib-$(CONFIG_HUSH_PRINTF) += printf.o
54
Pere Orga34425382011-03-31 14:43:25 +020055//usage:#define printf_trivial_usage
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010056//usage: "FORMAT [ARG]..."
Pere Orga34425382011-03-31 14:43:25 +020057//usage:#define printf_full_usage "\n\n"
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010058//usage: "Format and print ARG(s) according to FORMAT (a-la C printf)"
Pere Orga34425382011-03-31 14:43:25 +020059//usage:
60//usage:#define printf_example_usage
61//usage: "$ printf \"Val=%d\\n\" 5\n"
62//usage: "Val=5\n"
63
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000064#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000065
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000066/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
67 * They report it:
68 * bash: printf: XXX: invalid number
69 * printf: XXX: expected a numeric value
70 * bash: printf: 123XXX: invalid number
71 * printf: 123XXX: value not completely converted
72 * but then they use 0 (or partially converted numeric prefix) as a value
73 * and continue. They exit with 1 in this case.
74 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
75 * Both print error message and assume 0 if %*.*f width/precision is "bad"
76 * (but negative numbers are not "bad").
77 * Both accept negative numbers for %u specifier.
78 *
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +020079 * We try to be compatible.
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000080 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000081
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000082typedef void FAST_FUNC (*converter)(const char *arg, void *result);
83
84static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000085{
Rob Landley251161f2006-01-06 20:28:05 +000086 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000087 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000088 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000089 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000090 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000091 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +020092 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000093 return 1;
94 }
95 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000096}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000097
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000098static void FAST_FUNC conv_strtoull(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000099{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000100 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +0200101 /* both coreutils 6.10 and bash 3.2:
102 * $ printf '%x\n' -2
103 * fffffffffffffffe
104 * Mimic that:
105 */
106 if (errno) {
107 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
108 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000109}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000110static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000111{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000112 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000113}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000114static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000115{
116 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000117 /* Well, this one allows leading whitespace... so what? */
118 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +0000119 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000120 if (end[0]) {
121 errno = ERANGE;
122 *(double*)result = 0;
123 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000124}
125
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000126/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000127static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000128{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000129 unsigned long long result;
130 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000131 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000132 return result;
133}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000134static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000135{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000136 long long result;
137 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000138 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000139 return result;
140}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000141static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000142{
143 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000144 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000145 return result;
146}
147
Ron Yorston4a792242016-01-31 22:22:25 +0100148/* Handles %b; return 1 if output is to be short-circuited by \c */
149static int print_esc_string(const char *str)
Rob Landley251161f2006-01-06 20:28:05 +0000150{
Denys Vlasenko53600592010-10-23 21:06:06 +0200151 char c;
152 while ((c = *str) != '\0') {
153 str++;
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100154 if (c == '\\') {
155 /* %b also accepts 4-digit octals of the form \0### */
156 if (*str == '0') {
157 if ((unsigned char)(str[1] - '0') < 8) {
158 /* 2nd char is 0..7: skip leading '0' */
159 str++;
160 }
161 }
Ron Yorston4a792242016-01-31 22:22:25 +0100162 else if (*str == 'c') {
163 return 1;
164 }
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100165 {
166 /* optimization: don't force arg to be on-stack,
167 * use another variable for that. */
168 const char *z = str;
169 c = bb_process_escape_sequence(&z);
170 str = z;
171 }
172 }
Denys Vlasenko53600592010-10-23 21:06:06 +0200173 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000174 }
Ron Yorston4a792242016-01-31 22:22:25 +0100175
176 return 0;
Rob Landley251161f2006-01-06 20:28:05 +0000177}
178
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000179static void print_direc(char *format, unsigned fmt_length,
180 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000181 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000182{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000183 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000184 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000185 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000186 char *have_prec, *have_width;
187
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000188 saved = format[fmt_length];
189 format[fmt_length] = '\0';
190
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000191 have_prec = strstr(format, ".*");
192 have_width = strchr(format, '*');
193 if (have_width - 1 == have_prec)
194 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000195
Denys Vlasenko7e603432009-06-27 00:07:23 +0200196 errno = 0;
197
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000198 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000199 case 'c':
200 printf(format, *argument);
201 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000202 case 'd':
203 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000204 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000205 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000206 if (!have_width) {
207 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000208 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000209 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000210 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000211 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000212 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000213 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000214 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000215 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000216 }
217 break;
218 case 'o':
219 case 'u':
220 case 'x':
221 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000222 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000223 /* cheat: unsigned long and long have same width, so... */
224 goto print_long;
225 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000226 /* Are char* and long long the same? */
227 if (sizeof(argument) == sizeof(llv)) {
228 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000229 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000230 } else {
231 /* Hope compiler will optimize it out by moving call
232 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000233 if (!have_width) {
234 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000235 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000236 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000237 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000238 } else {
239 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000240 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000241 else
242 printf(format, field_width, precision, argument);
243 }
244 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000245 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000246 case 'f':
247 case 'e':
248 case 'E':
249 case 'g':
250 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000251 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000252 if (!have_width) {
253 if (!have_prec)
254 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000255 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000256 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000257 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000258 if (!have_prec)
259 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000260 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000261 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000262 }
263 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000264 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000265
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000266 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000267}
268
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000269/* Handle params for "%*.*f". Negative numbers are ok (compat). */
270static int get_width_prec(const char *str)
271{
272 int v = bb_strtoi(str, NULL, 10);
273 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200274 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000275 v = 0;
276 }
277 return v;
278}
279
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000280/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
281 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200282static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000283{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000284 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000285 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000286 int field_width; /* Arg to first '*' */
287 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000288 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000289
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000290 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 switch (*f) {
292 case '%':
293 direc_start = f++;
294 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000295 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000297 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 break;
299 }
300 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000301 if (*argv) {
Ron Yorston4a792242016-01-31 22:22:25 +0100302 if (print_esc_string(*argv))
303 return saved_argv; /* causes main() to exit */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 }
306 break;
307 }
Ron Yorston513a2452017-07-18 09:33:22 +0100308 if (*f && strchr("-+ #", *f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 ++f;
310 ++direc_length;
311 }
312 if (*f == '*') {
313 ++f;
314 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000315 if (*argv)
316 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000317 } 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 Vlasenko339f5eb2007-03-09 16:43:01 +0000322 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 if (*f == '.') {
324 ++f;
325 ++direc_length;
326 if (*f == '*') {
327 ++f;
328 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000329 if (*argv)
330 precision = get_width_prec(*argv++);
331 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000332 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 ++f;
334 ++direc_length;
335 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000336 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000338
339 /* Remove "lLhz" size modifiers, repeatedly.
340 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200341 * happily takes even "%Llllhhzhhzd"!
342 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000343 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000344 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000346 /* Add "ll" if integer modifier, then print */
347 {
348 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
349 char *p = strchr(format_chars, *f);
350 /* needed - try "printf %" without it */
Ron Yorston513a2452017-07-18 09:33:22 +0100351 if (p == NULL || *f == '\0') {
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000352 bb_error_msg("%s: invalid format", direc_start);
353 /* causes main() to exit with error */
354 return saved_argv - 1;
355 }
356 ++direc_length;
357 if (p - format_chars <= 5) {
358 /* it is one of "diouxX" */
359 p = xmalloc(direc_length + 3);
360 memcpy(p, direc_start, direc_length);
361 p[direc_length + 1] = p[direc_length - 1];
362 p[direc_length - 1] = 'l';
363 p[direc_length] = 'l';
364 //bb_error_msg("<%s>", p);
365 direc_length += 2;
366 direc_start = p;
367 } else {
368 p = NULL;
369 }
370 if (*argv) {
371 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200372 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000373 } else {
374 print_direc(direc_start, direc_length, field_width,
375 precision, "");
376 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200377 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000378 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000379 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000382 if (*++f == 'c') {
383 return saved_argv; /* causes main() to exit */
384 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000385 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000386 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200389 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000391 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000392
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000393 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000394}
395
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000396int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000397{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200398 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000399 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000400 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000401
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000402 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000403 * The reason for this is highly non-obvious.
404 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000405 * Shell must correctly handle 'printf "%s" foo'
406 * if stdout is closed. With stdio, output gets shoveled into
407 * stdout buffer, and even fflush cannot clear it out. It seems that
408 * even if libc receives EBADF on write attempts, it feels determined
409 * to output data no matter what. So it will try later,
410 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000411// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
412 if (fcntl(1, F_GETFL) == -1)
413 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
414 //if (dup2(1, 1) != 1) - old way
415 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000416
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000417 /* bash builtin errors out on "printf '-%s-\n' foo",
418 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
419 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000420 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000421 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000422 if (!argv[1]) {
Denys Vlasenko265062d2017-01-10 15:13:30 +0100423 if (ENABLE_ASH_PRINTF
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000424 && applet_name[0] != 'p'
425 ) {
426 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
427 return 2; /* bash compat */
428 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000429 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000430 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000431
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000432 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000433 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000434
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200435 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000436 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000437 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200438 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000439 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000440
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000441 /* coreutils compat (bash doesn't do this):
442 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000443 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000444 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000445
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200446 return (argv2 < argv) /* if true, print_formatted errored out */
447 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000448}