blob: 76524f70648e6d1d66ffa47c81d5f97facb8e4a9 [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
Rob Landley251161f2006-01-06 20:28:05 +00007 Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
8*/
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
Eric Andersencc8ed391999-10-05 16:24:54 +000039// 19990508 Busy Boxed! Dave Cinege
40
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000043/* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
44 * They report it:
45 * bash: printf: XXX: invalid number
46 * printf: XXX: expected a numeric value
47 * bash: printf: 123XXX: invalid number
48 * printf: 123XXX: value not completely converted
49 * but then they use 0 (or partially converted numeric prefix) as a value
50 * and continue. They exit with 1 in this case.
51 * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
52 * Both print error message and assume 0 if %*.*f width/precision is "bad"
53 * (but negative numbers are not "bad").
54 * Both accept negative numbers for %u specifier.
55 *
56 * We try to be compatible. We are not compatible here:
57 * - we do not accept -NUM for %u
58 * - exit code is 0 even if "invalid number" was seen (FIXME)
59 * See "if (errno)" checks in the code below.
60 */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000061
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000062typedef void FAST_FUNC (*converter)(const char *arg, void *result);
63
64static int multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000065{
Rob Landley251161f2006-01-06 20:28:05 +000066 if (*arg == '"' || *arg == '\'') {
Denis Vlasenko5f116622008-07-18 18:41:55 +000067 arg = utoa((unsigned char)arg[1]);
Rob Landley251161f2006-01-06 20:28:05 +000068 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000069 errno = 0;
Denis Vlasenkod686a042006-11-27 14:43:21 +000070 convert(arg, result);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000071 if (errno) {
72 bb_error_msg("%s: invalid number", arg);
73 return 1;
74 }
75 return 0;
Rob Landley251161f2006-01-06 20:28:05 +000076}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000077
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000078static void FAST_FUNC conv_strtoul(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000079{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +000080 *(unsigned long*)result = bb_strtoul(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +000081}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000082static void FAST_FUNC conv_strtol(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000083{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +000084 *(long*)result = bb_strtol(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +000085}
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000086static void FAST_FUNC conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000087{
88 char *end;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000089 /* Well, this one allows leading whitespace... so what? */
90 /* What I like much less is that "-" accepted too! :( */
Denis Vlasenkod686a042006-11-27 14:43:21 +000091 *(double*)result = strtod(arg, &end);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000092 if (end[0]) {
93 errno = ERANGE;
94 *(double*)result = 0;
95 }
Denis Vlasenkod686a042006-11-27 14:43:21 +000096}
97
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000098/* Callers should check errno to detect errors */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000099static unsigned long my_xstrtoul(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000100{
101 unsigned long result;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000102 if (multiconvert(arg, &result, conv_strtoul))
103 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000104 return result;
105}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000106static long my_xstrtol(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000107{
108 long result;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000109 if (multiconvert(arg, &result, conv_strtol))
110 result = 0;
Rob Landley251161f2006-01-06 20:28:05 +0000111 return result;
112}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000113static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +0000114{
115 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000116 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +0000117 return result;
118}
119
120static void print_esc_string(char *str)
121{
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000122 while (*str) {
Rob Landley251161f2006-01-06 20:28:05 +0000123 if (*str == '\\') {
124 str++;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000125 bb_putchar(bb_process_escape_sequence((const char **)&str));
Rob Landley251161f2006-01-06 20:28:05 +0000126 } else {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000127 bb_putchar(*str);
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000128 str++;
Rob Landley251161f2006-01-06 20:28:05 +0000129 }
Rob Landley251161f2006-01-06 20:28:05 +0000130 }
131}
132
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000133static void print_direc(char *format, unsigned fmt_length,
134 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000135 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000136{
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000137 long lv;
138 double dv;
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000139 char saved;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000140 char *have_prec, *have_width;
141
142 have_prec = strstr(format, ".*");
143 have_width = strchr(format, '*');
144 if (have_width - 1 == have_prec)
145 have_width = NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000146
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000147 saved = format[fmt_length];
148 format[fmt_length] = '\0';
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000149
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000150 switch (format[fmt_length - 1]) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000151 case 'c':
152 printf(format, *argument);
153 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000154 case 'd':
155 case 'i':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000156 lv = my_xstrtol(argument);
157 print_long:
158 /* if (errno) return; - see comment at the top */
159 if (!have_width) {
160 if (!have_prec)
161 printf(format, lv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000162 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000163 printf(format, precision, lv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000164 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000165 if (!have_prec)
166 printf(format, field_width, lv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000167 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000168 printf(format, field_width, precision, lv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000169 }
170 break;
171 case 'o':
172 case 'u':
173 case 'x':
174 case 'X':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000175 lv = my_xstrtoul(argument);
176 /* cheat: unsigned long and long have same width, so... */
177 goto print_long;
178 case 's':
179 /* Are char* and long the same? (true for most arches) */
180 if (sizeof(argument) == sizeof(lv)) {
181 lv = (long)(ptrdiff_t)argument;
182 goto print_long;
183 } else { /* Hope compiler will optimize it out */
184 if (!have_width) {
185 if (!have_prec)
186 printf(format, argument);
187 else
188 printf(format, precision, argument);
189 } else {
190 if (!have_prec)
191 printf(format, field_width, argument);
192 else
193 printf(format, field_width, precision, argument);
194 }
195 break;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000196 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000197 case 'f':
198 case 'e':
199 case 'E':
200 case 'g':
201 case 'G':
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000202 dv = my_xstrtod(argument);
203 /* if (errno) return; */
204 if (!have_width) {
205 if (!have_prec)
206 printf(format, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000207 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000208 printf(format, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000209 } else {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000210 if (!have_prec)
211 printf(format, field_width, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000212 else
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000213 printf(format, field_width, precision, dv);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000214 }
215 break;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000216 } /* switch */
Eric Andersencc8ed391999-10-05 16:24:54 +0000217
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000218 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000219}
220
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000221/* Handle params for "%*.*f". Negative numbers are ok (compat). */
222static int get_width_prec(const char *str)
223{
224 int v = bb_strtoi(str, NULL, 10);
225 if (errno) {
226 bb_error_msg("%s: invalid number", str);
227 v = 0;
228 }
229 return v;
230}
231
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000232/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
233 Return advanced ARGV. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000234static char **print_formatted(char *f, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000235{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000236 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000237 unsigned direc_length; /* Length of % directive. */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000238 int field_width; /* Arg to first '*' */
239 int precision; /* Arg to second '*' */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000240 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000241
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000242 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 switch (*f) {
244 case '%':
245 direc_start = f++;
246 direc_length = 1;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000247 field_width = precision = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000249 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 break;
251 }
252 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000253 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254 print_esc_string(*argv);
255 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 }
257 break;
258 }
259 if (strchr("-+ #", *f)) {
260 ++f;
261 ++direc_length;
262 }
263 if (*f == '*') {
264 ++f;
265 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000266 if (*argv)
267 field_width = get_width_prec(*argv++);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000268 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000269 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 ++f;
271 ++direc_length;
272 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000273 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 if (*f == '.') {
275 ++f;
276 ++direc_length;
277 if (*f == '*') {
278 ++f;
279 ++direc_length;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000280 if (*argv)
281 precision = get_width_prec(*argv++);
282 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000283 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 ++f;
285 ++direc_length;
286 }
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000287 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 }
Denis Vlasenko5f116622008-07-18 18:41:55 +0000289 /* Remove size modifiers - "%Ld" would try to printf
290 * long long, we pass long, and it spews garbage */
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000291 if ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000292 overlapping_strcpy(f, f + 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 }
Denis Vlasenko5f116622008-07-18 18:41:55 +0000294//FIXME: actually, the same happens with bare "%d":
295//it printfs an int, but we pass long!
296//What saves us is that on most arches stack slot
297//is pointer-sized -> long-sized -> ints are promoted to longs
298// for variadic functions -> printf("%d", int_v) is in reality
299// indistinqushable from printf("%d", long_v) ->
300// since printf("%d", int_v) works, printf("%d", long_v) has to work.
301//But "clean" solution would be to add "l" to d,i,o,x,X.
302//Probably makes sense to go all the way to "ll" then.
303//Coreutils support long long-sized arguments.
304
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000305 /* needed - try "printf %" without it */
306 if (!strchr("diouxXfeEgGcs", *f)) {
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000307 bb_error_msg("%s: invalid format", direc_start);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000308 /* causes main() to exit with error */
309 return saved_argv - 1;
310 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 ++direc_length;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000312 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 print_direc(direc_start, direc_length, field_width,
314 precision, *argv);
315 ++argv;
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000316 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 print_direc(direc_start, direc_length, field_width,
318 precision, "");
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000319 }
320 /* if (errno) return saved_argv - 1; */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000323 if (*++f == 'c') {
324 return saved_argv; /* causes main() to exit */
325 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000326 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000327 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000330 bb_putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000332 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000333
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000334 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000335}
336
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000337int printf_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000338{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000339 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000340 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000341
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000342 /* We must check that stdout is not closed.
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000343 * The reason for this is highly non-obvious.
344 * printf_main is used from shell.
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000345 * Shell must correctly handle 'printf "%s" foo'
346 * if stdout is closed. With stdio, output gets shoveled into
347 * stdout buffer, and even fflush cannot clear it out. It seems that
348 * even if libc receives EBADF on write attempts, it feels determined
349 * to output data no matter what. So it will try later,
350 * and possibly will clobber future output. Not good. */
Denis Vlasenko6a0ad252008-07-25 13:34:05 +0000351// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
352 if (fcntl(1, F_GETFL) == -1)
353 return 1; /* match coreutils 6.10 (sans error msg to stderr) */
354 //if (dup2(1, 1) != 1) - old way
355 // return 1;
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000356
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000357 /* bash builtin errors out on "printf '-%s-\n' foo",
358 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
359 * We will mimic coreutils. */
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000360 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000361 argv++;
362 if (!argv[1])
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000363 bb_show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +0000364
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000365 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000366 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000367
368 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000369 argv = argv2;
370 argv2 = print_formatted(format, argv);
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000371 } while (argv2 > argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000372
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000373 /* coreutils compat (bash doesn't do this):
374 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000375 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000376 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000377
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000378 return (argv2 < argv); /* if true, print_formatted errored out */
Eric Andersencc8ed391999-10-05 16:24:54 +0000379}