blob: a666ff7acf43ef770a8218a510c267826259febc [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoa02a4e92017-10-05 15:19:25 +02002/*
3 * printf - format and print data
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02004 *
5 * Copyright 1999 Dave Cinege
6 * Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 */
Eric Andersencc8ed391999-10-05 16:24:54 +000010/* Usage: printf format [argument...]
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +020011 *
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 *
33 * The 'format' argument is re-used as many times as necessary
34 * to convert all of the given arguments.
35 *
36 * David MacKenzie <djm@gnu.ai.mit.edu>
37 */
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010038/* 19990508 Busy Boxed! Dave Cinege */
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010040//config:config PRINTF
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020041//config: bool "printf (3.3 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010042//config: default y
43//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020044//config: printf is used to format and print specified strings.
Denys Vlasenko18e781d2017-08-02 14:12:48 +020045//config: It's similar to 'echo' except it has more options.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010046
47//applet:IF_PRINTF(APPLET_NOFORK(printf, printf, BB_DIR_USR_BIN, BB_SUID_DROP, printf))
48
49//kbuild:lib-$(CONFIG_PRINTF) += printf.o
Denys Vlasenko265062d2017-01-10 15:13:30 +010050//kbuild:lib-$(CONFIG_ASH_PRINTF) += printf.o
51//kbuild:lib-$(CONFIG_HUSH_PRINTF) += printf.o
52
Pere Orga34425382011-03-31 14:43:25 +020053//usage:#define printf_trivial_usage
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010054//usage: "FORMAT [ARG]..."
Pere Orga34425382011-03-31 14:43:25 +020055//usage:#define printf_full_usage "\n\n"
Denys Vlasenkod4acaf72012-03-07 11:44:15 +010056//usage: "Format and print ARG(s) according to FORMAT (a-la C printf)"
Pere Orga34425382011-03-31 14:43:25 +020057//usage:
58//usage:#define printf_example_usage
59//usage: "$ printf \"Val=%d\\n\" 5\n"
60//usage: "Val=5\n"
61
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000062#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000063
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000064/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
65 * They report it:
66 * bash: printf: XXX: invalid number
67 * printf: XXX: expected a numeric value
68 * bash: printf: 123XXX: invalid number
69 * printf: 123XXX: value not completely converted
70 * but then they use 0 (or partially converted numeric prefix) as a value
71 * and continue. They exit with 1 in this case.
72 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
73 * Both print error message and assume 0 if %*.*f width/precision is "bad"
74 * (but negative numbers are not "bad").
75 * Both accept negative numbers for %u specifier.
76 *
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +020077 * We try to be compatible.
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000078 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000079
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000080typedef void FAST_FUNC (*converter)(const char *arg, void *result);
81
82static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000083{
Rob Landley251161f2006-01-06 20:28:05 +000084 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000085 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000086 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000087 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000088 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000089 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +020090 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000091 return 1;
92 }
93 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000094}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000096static void FAST_FUNC conv_strtoull(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000097{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +000098 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +020099 /* both coreutils 6.10 and bash 3.2:
100 * $ printf '%x\n' -2
101 * fffffffffffffffe
102 * Mimic that:
103 */
104 if (errno) {
105 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
106 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000107}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000108static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000109{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000110 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000111}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000112static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000113{
114 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000115 /* Well, this one allows leading whitespace... so what? */
116 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +0000117 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000118 if (end[0]) {
119 errno = ERANGE;
120 *(double*)result = 0;
121 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000122}
123
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000124/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000125static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000126{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000127 unsigned long long result;
128 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000129 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000130 return result;
131}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000132static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000133{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000134 long long result;
135 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000136 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000137 return result;
138}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000139static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000140{
141 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000142 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000143 return result;
144}
145
Ron Yorston4a792242016-01-31 22:22:25 +0100146/* Handles %b; return 1 if output is to be short-circuited by \c */
147static int print_esc_string(const char *str)
Rob Landley251161f2006-01-06 20:28:05 +0000148{
Denys Vlasenko53600592010-10-23 21:06:06 +0200149 char c;
150 while ((c = *str) != '\0') {
151 str++;
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100152 if (c == '\\') {
153 /* %b also accepts 4-digit octals of the form \0### */
154 if (*str == '0') {
155 if ((unsigned char)(str[1] - '0') < 8) {
156 /* 2nd char is 0..7: skip leading '0' */
157 str++;
158 }
159 }
Ron Yorston4a792242016-01-31 22:22:25 +0100160 else if (*str == 'c') {
161 return 1;
162 }
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100163 {
164 /* optimization: don't force arg to be on-stack,
165 * use another variable for that. */
166 const char *z = str;
167 c = bb_process_escape_sequence(&z);
168 str = z;
169 }
170 }
Denys Vlasenko53600592010-10-23 21:06:06 +0200171 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000172 }
Ron Yorston4a792242016-01-31 22:22:25 +0100173
174 return 0;
Rob Landley251161f2006-01-06 20:28:05 +0000175}
176
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000177static void print_direc(char *format, unsigned fmt_length,
178 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000179 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000180{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000181 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000182 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000183 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000184 char *have_prec, *have_width;
185
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000186 saved = format[fmt_length];
187 format[fmt_length] = '\0';
188
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000189 have_prec = strstr(format, ".*");
190 have_width = strchr(format, '*');
191 if (have_width - 1 == have_prec)
192 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000193
Denys Vlasenko7e603432009-06-27 00:07:23 +0200194 errno = 0;
195
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000196 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000197 case 'c':
198 printf(format, *argument);
199 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000200 case 'd':
201 case 'i':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000202 llv = my_xstrtoll(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000203 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000204 if (!have_width) {
205 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000206 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000207 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000208 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000209 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000210 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000211 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000212 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000213 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000214 }
215 break;
216 case 'o':
217 case 'u':
218 case 'x':
219 case 'X':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000220 llv = my_xstrtoull(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000221 /* cheat: unsigned long and long have same width, so... */
222 goto print_long;
223 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000224 /* Are char* and long long the same? */
225 if (sizeof(argument) == sizeof(llv)) {
226 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000227 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000228 } else {
229 /* Hope compiler will optimize it out by moving call
230 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000231 if (!have_width) {
232 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000233 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000234 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000235 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000236 } else {
237 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000238 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000239 else
240 printf(format, field_width, precision, argument);
241 }
242 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000243 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000244 case 'f':
245 case 'e':
246 case 'E':
247 case 'g':
248 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000249 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000250 if (!have_width) {
251 if (!have_prec)
252 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000253 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000254 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000255 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000256 if (!have_prec)
257 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000258 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000259 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000260 }
261 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000262 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000263
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000264 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000265}
266
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000267/* Handle params for "%*.*f". Negative numbers are ok (compat). */
268static int get_width_prec(const char *str)
269{
270 int v = bb_strtoi(str, NULL, 10);
271 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200272 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000273 v = 0;
274 }
275 return v;
276}
277
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000278/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
279 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200280static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000281{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000282 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000283 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000284 int field_width; /* Arg to first '*' */
285 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000286 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000287
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000288 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289 switch (*f) {
290 case '%':
291 direc_start = f++;
292 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000293 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000295 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 break;
297 }
298 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000299 if (*argv) {
Ron Yorston4a792242016-01-31 22:22:25 +0100300 if (print_esc_string(*argv))
301 return saved_argv; /* causes main() to exit */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 }
304 break;
305 }
Ron Yorston513a2452017-07-18 09:33:22 +0100306 if (*f && strchr("-+ #", *f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 ++f;
308 ++direc_length;
309 }
310 if (*f == '*') {
311 ++f;
312 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000313 if (*argv)
314 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000315 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000316 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 ++f;
318 ++direc_length;
319 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000320 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 if (*f == '.') {
322 ++f;
323 ++direc_length;
324 if (*f == '*') {
325 ++f;
326 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000327 if (*argv)
328 precision = get_width_prec(*argv++);
329 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000330 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 ++f;
332 ++direc_length;
333 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000334 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000336
337 /* Remove "lLhz" size modifiers, repeatedly.
338 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200339 * happily takes even "%Llllhhzhhzd"!
340 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000341 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000342 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000344 /* Add "ll" if integer modifier, then print */
345 {
346 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
347 char *p = strchr(format_chars, *f);
348 /* needed - try "printf %" without it */
Ron Yorston513a2452017-07-18 09:33:22 +0100349 if (p == NULL || *f == '\0') {
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000350 bb_error_msg("%s: invalid format", direc_start);
351 /* causes main() to exit with error */
352 return saved_argv - 1;
353 }
354 ++direc_length;
355 if (p - format_chars <= 5) {
356 /* it is one of "diouxX" */
357 p = xmalloc(direc_length + 3);
358 memcpy(p, direc_start, direc_length);
359 p[direc_length + 1] = p[direc_length - 1];
360 p[direc_length - 1] = 'l';
361 p[direc_length] = 'l';
362 //bb_error_msg("<%s>", p);
363 direc_length += 2;
364 direc_start = p;
365 } else {
366 p = NULL;
367 }
368 if (*argv) {
369 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200370 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000371 } else {
372 print_direc(direc_start, direc_length, field_width,
373 precision, "");
374 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200375 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000376 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000377 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000380 if (*++f == 'c') {
381 return saved_argv; /* causes main() to exit */
382 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000383 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000384 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200387 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000389 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000390
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000391 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000392}
393
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000394int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000395{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200396 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000397 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000398 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000400 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000401 * The reason for this is highly non-obvious.
402 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000403 * Shell must correctly handle 'printf "%s" foo'
404 * if stdout is closed. With stdio, output gets shoveled into
405 * stdout buffer, and even fflush cannot clear it out. It seems that
406 * even if libc receives EBADF on write attempts, it feels determined
407 * to output data no matter what. So it will try later,
408 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000409// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
410 if (fcntl(1, F_GETFL) == -1)
411 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
412 //if (dup2(1, 1) != 1) - old way
413 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000414
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000415 /* bash builtin errors out on "printf '-%s-\n' foo",
416 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
417 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000418 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000419 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000420 if (!argv[1]) {
Denys Vlasenko265062d2017-01-10 15:13:30 +0100421 if (ENABLE_ASH_PRINTF
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000422 && applet_name[0] != 'p'
423 ) {
424 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
425 return 2; /* bash compat */
426 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000427 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000428 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000429
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000430 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000431 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000432
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200433 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000434 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000435 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200436 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000437 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000438
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000439 /* coreutils compat (bash doesn't do this):
440 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000441 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000442 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000443
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200444 return (argv2 < argv) /* if true, print_formatted errored out */
445 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000446}