blob: a20fc33013b849414c8c0c95aaa2da009001972e [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 Vlasenkob097a842018-12-28 03:20:17 +010041//config: bool "printf (3.8 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{
Denys Vlasenko93ef5dd2018-10-30 23:24:18 +010098 /* Allow leading '+' - bb_strtoull() by itself does not allow it,
99 * and probably shouldn't (other callers might require purely numeric
100 * inputs to be allowed.
101 */
102 if (arg[0] == '+')
103 arg++;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000104 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
Denys Vlasenko71016ba2009-06-05 16:24:29 +0200105 /* both coreutils 6.10 and bash 3.2:
106 * $ printf '%x\n' -2
107 * fffffffffffffffe
108 * Mimic that:
109 */
110 if (errno) {
111 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
112 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000113}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000114static void FAST_FUNC conv_strtoll(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000115{
Denys Vlasenko93ef5dd2018-10-30 23:24:18 +0100116 if (arg[0] == '+')
117 arg++;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000118 *(long long*)result = bb_strtoll(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000119}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000120static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000121{
122 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000123 /* Well, this one allows leading whitespace... so what? */
124 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +0000125 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000126 if (end[0]) {
127 errno = ERANGE;
128 *(double*)result = 0;
129 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000130}
131
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000132/* Callers should check errno to detect errors */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000133static unsigned long long my_xstrtoull(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000134{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000135 unsigned long long result;
136 if (multiconvert(arg, &result, conv_strtoull))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000137 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000138 return result;
139}
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000140static long long my_xstrtoll(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000141{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000142 long long result;
143 if (multiconvert(arg, &result, conv_strtoll))
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000144 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000145 return result;
146}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000147static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000148{
149 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000150 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000151 return result;
152}
153
Ron Yorston4a792242016-01-31 22:22:25 +0100154/* Handles %b; return 1 if output is to be short-circuited by \c */
155static int print_esc_string(const char *str)
Rob Landley251161f2006-01-06 20:28:05 +0000156{
Denys Vlasenko53600592010-10-23 21:06:06 +0200157 char c;
158 while ((c = *str) != '\0') {
159 str++;
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100160 if (c == '\\') {
161 /* %b also accepts 4-digit octals of the form \0### */
162 if (*str == '0') {
163 if ((unsigned char)(str[1] - '0') < 8) {
164 /* 2nd char is 0..7: skip leading '0' */
165 str++;
166 }
167 }
Ron Yorston4a792242016-01-31 22:22:25 +0100168 else if (*str == 'c') {
169 return 1;
170 }
Denys Vlasenko69d81a12012-03-07 11:57:47 +0100171 {
172 /* optimization: don't force arg to be on-stack,
173 * use another variable for that. */
174 const char *z = str;
175 c = bb_process_escape_sequence(&z);
176 str = z;
177 }
178 }
Denys Vlasenko53600592010-10-23 21:06:06 +0200179 putchar(c);
Rob Landley251161f2006-01-06 20:28:05 +0000180 }
Ron Yorston4a792242016-01-31 22:22:25 +0100181
182 return 0;
Rob Landley251161f2006-01-06 20:28:05 +0000183}
184
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000185static void print_direc(char *format, unsigned fmt_length,
186 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000187 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000188{
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000189 long long llv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000190 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000191 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000192 char *have_prec, *have_width;
193
Denis Vlasenko6852eff2009-03-03 14:14:44 +0000194 saved = format[fmt_length];
195 format[fmt_length] = '\0';
196
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000197 have_prec = strstr(format, ".*");
198 have_width = strchr(format, '*');
199 if (have_width - 1 == have_prec)
200 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Bernhard Reutner-Fischer3db4e7f2018-10-19 15:25:41 +0200202 /* multiconvert sets errno = 0, but %s needs it cleared */
Denys Vlasenko7e603432009-06-27 00:07:23 +0200203 errno = 0;
204
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000205 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000206 case 'c':
207 printf(format, *argument);
208 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000209 case 'd':
210 case 'i':
Bernhard Reutner-Fischer3db4e7f2018-10-19 15:25:41 +0200211 llv = my_xstrtoll(skip_whitespace(argument));
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000212 print_long:
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000213 if (!have_width) {
214 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000215 printf(format, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000216 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000217 printf(format, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000218 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000219 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000220 printf(format, field_width, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000221 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000222 printf(format, field_width, precision, llv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000223 }
224 break;
225 case 'o':
226 case 'u':
227 case 'x':
228 case 'X':
Bernhard Reutner-Fischer3db4e7f2018-10-19 15:25:41 +0200229 llv = my_xstrtoull(skip_whitespace(argument));
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000230 /* cheat: unsigned long and long have same width, so... */
231 goto print_long;
232 case 's':
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000233 /* Are char* and long long the same? */
234 if (sizeof(argument) == sizeof(llv)) {
235 llv = (long long)(ptrdiff_t)argument;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000236 goto print_long;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000237 } else {
238 /* Hope compiler will optimize it out by moving call
239 * instruction after the ifs... */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000240 if (!have_width) {
241 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000242 printf(format, argument, /*unused:*/ argument, argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000243 else
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000244 printf(format, precision, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000245 } else {
246 if (!have_prec)
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000247 printf(format, field_width, argument, /*unused:*/ argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000248 else
249 printf(format, field_width, precision, argument);
250 }
251 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000252 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000253 case 'f':
254 case 'e':
255 case 'E':
256 case 'g':
257 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000258 dv = my_xstrtod(argument);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000259 if (!have_width) {
260 if (!have_prec)
261 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000262 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000263 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000264 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000265 if (!have_prec)
266 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000267 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000268 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000269 }
270 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000271 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000272
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000273 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000274}
275
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000276/* Handle params for "%*.*f". Negative numbers are ok (compat). */
277static int get_width_prec(const char *str)
278{
279 int v = bb_strtoi(str, NULL, 10);
280 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200281 bb_error_msg("invalid number '%s'", str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000282 v = 0;
283 }
284 return v;
285}
286
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000287/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
288 Return advanced ARGV. */
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200289static char **print_formatted(char *f, char **argv, int *conv_err)
Eric Andersencc8ed391999-10-05 16:24:54 +0000290{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000291 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000292 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000293 int field_width; /* Arg to first '*' */
294 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000295 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000296
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000297 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 switch (*f) {
299 case '%':
300 direc_start = f++;
301 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000302 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000304 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 break;
306 }
307 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000308 if (*argv) {
Ron Yorston4a792242016-01-31 22:22:25 +0100309 if (print_esc_string(*argv))
310 return saved_argv; /* causes main() to exit */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 }
313 break;
314 }
Ron Yorston513a2452017-07-18 09:33:22 +0100315 if (*f && strchr("-+ #", *f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 ++f;
317 ++direc_length;
318 }
319 if (*f == '*') {
320 ++f;
321 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000322 if (*argv)
323 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000324 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000325 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 ++f;
327 ++direc_length;
328 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000329 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 if (*f == '.') {
331 ++f;
332 ++direc_length;
333 if (*f == '*') {
334 ++f;
335 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000336 if (*argv)
337 precision = get_width_prec(*argv++);
338 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000339 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 ++f;
341 ++direc_length;
342 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000343 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000345
346 /* Remove "lLhz" size modifiers, repeatedly.
347 * bash does not like "%lld", but coreutils
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200348 * happily takes even "%Llllhhzhhzd"!
349 * We are permissive like coreutils */
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000350 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000351 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 }
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000353 /* Add "ll" if integer modifier, then print */
354 {
355 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
356 char *p = strchr(format_chars, *f);
357 /* needed - try "printf %" without it */
Ron Yorston513a2452017-07-18 09:33:22 +0100358 if (p == NULL || *f == '\0') {
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000359 bb_error_msg("%s: invalid format", direc_start);
360 /* causes main() to exit with error */
361 return saved_argv - 1;
362 }
363 ++direc_length;
364 if (p - format_chars <= 5) {
365 /* it is one of "diouxX" */
366 p = xmalloc(direc_length + 3);
367 memcpy(p, direc_start, direc_length);
368 p[direc_length + 1] = p[direc_length - 1];
369 p[direc_length - 1] = 'l';
370 p[direc_length] = 'l';
371 //bb_error_msg("<%s>", p);
372 direc_length += 2;
373 direc_start = p;
374 } else {
375 p = NULL;
376 }
377 if (*argv) {
378 print_direc(direc_start, direc_length, field_width,
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200379 precision, *argv++);
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000380 } else {
381 print_direc(direc_start, direc_length, field_width,
382 precision, "");
383 }
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200384 *conv_err |= errno;
Denis Vlasenkocb39a7c2009-01-04 02:58:58 +0000385 free(p);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000386 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000389 if (*++f == 'c') {
390 return saved_argv; /* causes main() to exit */
391 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000392 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000393 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000395 default:
Denys Vlasenko53600592010-10-23 21:06:06 +0200396 putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000398 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000400 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000401}
402
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000403int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000404{
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200405 int conv_err;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000406 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000407 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000408
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000409 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000410 * The reason for this is highly non-obvious.
411 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000412 * Shell must correctly handle 'printf "%s" foo'
413 * if stdout is closed. With stdio, output gets shoveled into
414 * stdout buffer, and even fflush cannot clear it out. It seems that
415 * even if libc receives EBADF on write attempts, it feels determined
416 * to output data no matter what. So it will try later,
417 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000418// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
419 if (fcntl(1, F_GETFL) == -1)
420 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
421 //if (dup2(1, 1) != 1) - old way
422 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000423
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000424 /* bash builtin errors out on "printf '-%s-\n' foo",
425 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
426 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000427 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000428 argv++;
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000429 if (!argv[1]) {
Denys Vlasenko265062d2017-01-10 15:13:30 +0100430 if (ENABLE_ASH_PRINTF
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000431 && applet_name[0] != 'p'
432 ) {
James Byrne69374872019-07-02 11:35:03 +0200433 bb_simple_error_msg("usage: printf FORMAT [ARGUMENT...]");
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000434 return 2; /* bash compat */
435 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000436 bb_show_usage();
Denis Vlasenko311c19a2008-12-10 11:51:45 +0000437 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000438
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000439 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000440 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000441
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200442 conv_err = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000443 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000444 argv = argv2;
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200445 argv2 = print_formatted(format, argv, &conv_err);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000446 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000447
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000448 /* coreutils compat (bash doesn't do this):
449 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000450 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000451 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000452
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200453 return (argv2 < argv) /* if true, print_formatted errored out */
454 || conv_err; /* print_formatted saw invalid number */
Eric Andersencc8ed391999-10-05 16:24:54 +0000455}