blob: b7752369c56089c7b286b8c7951ef24fa949f1c9 [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
36 David MacKenzie <djm@gnu.ai.mit.edu> */
Erik Andersene49d5ec2000-02-08 19:58:47 +000037
Eric Andersencc8ed391999-10-05 16:24:54 +000038
39// 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 Vlasenkoa41fdf32007-01-29 22:51:00 +000043typedef void (*converter)(const char *arg, void *result);
Denis Vlasenkoc2905632006-09-23 16:01:09 +000044
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000045static void multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000046{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +000047 char s[sizeof(int)*3 + 2];
48
Rob Landley251161f2006-01-06 20:28:05 +000049 if (*arg == '"' || *arg == '\'') {
Denis Vlasenkod686a042006-11-27 14:43:21 +000050 sprintf(s, "%d", (unsigned char)arg[1]);
Denis Vlasenkoc2905632006-09-23 16:01:09 +000051 arg = s;
Rob Landley251161f2006-01-06 20:28:05 +000052 }
Denis Vlasenkod686a042006-11-27 14:43:21 +000053 convert(arg, result);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +000054 /* if there was conversion error, print unconverted string */
55 if (errno)
Denis Vlasenkoc2905632006-09-23 16:01:09 +000056 fputs(arg, stderr);
Rob Landley251161f2006-01-06 20:28:05 +000057}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000058
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000059static void conv_strtoul(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000060{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +000061 *(unsigned long*)result = bb_strtoul(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +000062}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000063static void conv_strtol(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000064{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +000065 *(long*)result = bb_strtol(arg, NULL, 0);
Denis Vlasenkod686a042006-11-27 14:43:21 +000066}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000067static void conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000068{
69 char *end;
70 /* Well, this one allows leading whitespace... so what */
71 /* What I like much less is that "-" is accepted too! :( */
72 *(double*)result = strtod(arg, &end);
73 if (end[0]) errno = ERANGE;
74}
75
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000076static unsigned long my_xstrtoul(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +000077{
78 unsigned long result;
Denis Vlasenkod686a042006-11-27 14:43:21 +000079 multiconvert(arg, &result, conv_strtoul);
Rob Landley251161f2006-01-06 20:28:05 +000080 return result;
81}
82
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000083static long my_xstrtol(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +000084{
85 long result;
Denis Vlasenkod686a042006-11-27 14:43:21 +000086 multiconvert(arg, &result, conv_strtol);
Rob Landley251161f2006-01-06 20:28:05 +000087 return result;
88}
89
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000090static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +000091{
92 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +000093 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +000094 return result;
95}
96
97static void print_esc_string(char *str)
98{
99 for (; *str; str++) {
100 if (*str == '\\') {
101 str++;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000102 bb_putchar(bb_process_escape_sequence((const char **)&str));
Rob Landley251161f2006-01-06 20:28:05 +0000103 } else {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000104 bb_putchar(*str);
Rob Landley251161f2006-01-06 20:28:05 +0000105 }
106
107 }
108}
109
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000110static void print_direc(char *format, unsigned fmt_length,
111 int field_width, int precision,
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000112 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000113{
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000114 char saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000115
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000116 saved = format[fmt_length];
117 format[fmt_length] = '\0';
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000118
Denis Vlasenkoa76669c2008-05-31 18:32:56 +0000119 switch (format[fmt_length - 1]) {
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000120 case 'd':
121 case 'i':
122 if (field_width < 0) {
123 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000124 printf(format, my_xstrtol(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000125 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000126 printf(format, precision, my_xstrtol(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000127 } else {
128 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000129 printf(format, field_width, my_xstrtol(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000130 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000131 printf(format, field_width, precision, my_xstrtol(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000132 }
133 break;
134 case 'o':
135 case 'u':
136 case 'x':
137 case 'X':
138 if (field_width < 0) {
139 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000140 printf(format, my_xstrtoul(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000141 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000142 printf(format, precision, my_xstrtoul(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000143 } else {
144 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000145 printf(format, field_width, my_xstrtoul(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000146 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000147 printf(format, field_width, precision, my_xstrtoul(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000148 }
149 break;
150 case 'f':
151 case 'e':
152 case 'E':
153 case 'g':
154 case 'G':
155 if (field_width < 0) {
156 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000157 printf(format, my_xstrtod(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000158 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000159 printf(format, precision, my_xstrtod(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000160 } else {
161 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000162 printf(format, field_width, my_xstrtod(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000163 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000164 printf(format, field_width, precision, my_xstrtod(argument));
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000165 }
166 break;
167 case 'c':
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000168 printf(format, *argument);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000169 break;
170 case 's':
171 if (field_width < 0) {
172 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000173 printf(format, argument);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000174 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000175 printf(format, precision, argument);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000176 } else {
177 if (precision < 0)
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000178 printf(format, field_width, argument);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000179 else
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000180 printf(format, field_width, precision, argument);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000181 }
182 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000184
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000185 format[fmt_length] = saved;
Eric Andersencc8ed391999-10-05 16:24:54 +0000186}
187
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000188/* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
189 Return advanced ARGV. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000190static char **print_formatted(char *f, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000191{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000192 char *direc_start; /* Start of % directive. */
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000193 unsigned direc_length; /* Length of % directive. */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000194 int field_width; /* Arg to first '*', or -1 if none. */
195 int precision; /* Arg to second '*', or -1 if none. */
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000196 char **saved_argv = argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000197
Denis Vlasenko1d1bba42008-05-31 11:41:50 +0000198 for (; *f; ++f) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 switch (*f) {
200 case '%':
201 direc_start = f++;
202 direc_length = 1;
203 field_width = precision = -1;
204 if (*f == '%') {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000205 bb_putchar('%');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 break;
207 }
208 if (*f == 'b') {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000209 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 print_esc_string(*argv);
211 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 }
213 break;
214 }
215 if (strchr("-+ #", *f)) {
216 ++f;
217 ++direc_length;
218 }
219 if (*f == '*') {
220 ++f;
221 ++direc_length;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000222 if (*argv) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000223 field_width = my_xstrtoul(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 } else
226 field_width = 0;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000227 } else {
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000228 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 ++f;
230 ++direc_length;
231 }
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000232 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 if (*f == '.') {
234 ++f;
235 ++direc_length;
236 if (*f == '*') {
237 ++f;
238 ++direc_length;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000239 if (*argv) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000240 precision = my_xstrtoul(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 } else
243 precision = 0;
244 } else
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000245 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 ++f;
247 ++direc_length;
248 }
249 }
250 if (*f == 'l' || *f == 'L' || *f == 'h') {
251 ++f;
252 ++direc_length;
253 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000254 /*
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000255 if (!strchr ("diouxXfeEgGcs", *f))
256 fprintf(stderr, "%%%c: invalid directive", *f);
257 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 ++direc_length;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000259 if (*argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260 print_direc(direc_start, direc_length, field_width,
261 precision, *argv);
262 ++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 } else
264 print_direc(direc_start, direc_length, field_width,
265 precision, "");
266 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267 case '\\':
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000268 if (*++f == 'c') {
269 return saved_argv; /* causes main() to exit */
270 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000271 bb_putchar(bb_process_escape_sequence((const char **)&f));
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000272 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000275 bb_putchar(*f);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000277 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000278
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000279 return argv;
Eric Andersencc8ed391999-10-05 16:24:54 +0000280}
281
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000282int printf_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000283{
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000284 char *format;
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000285 char **argv2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000286
Denis Vlasenkocd2663f2008-06-01 22:36:39 +0000287 /* We must check that stdout is not closed.
288 * The reason for this is highly non-obvious. printf_main is used from shell.
289 * Shell must correctly handle 'printf "%s" foo'
290 * if stdout is closed. With stdio, output gets shoveled into
291 * stdout buffer, and even fflush cannot clear it out. It seems that
292 * even if libc receives EBADF on write attempts, it feels determined
293 * to output data no matter what. So it will try later,
294 * and possibly will clobber future output. Not good. */
295 if (dup2(1, 1) != 1)
296 return -1;
297
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000298 /* bash builtin errors out on "printf '-%s-\n' foo",
299 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
300 * We will mimic coreutils. */
301 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && argv[1][2] == '\0')
302 argv++;
303 if (!argv[1])
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000304 bb_show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +0000305
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000306 format = argv[1];
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000307 argv2 = argv + 2;
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000308
309 do {
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000310 argv = argv2;
311 argv2 = print_formatted(format, argv);
312 } while (argv2 != argv && *argv2);
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000313
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000314 /* coreutils compat (bash doesn't do this):
315 if (*argv)
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000316 fprintf(stderr, "excess args ignored");
Denis Vlasenkob6c48552008-05-18 14:28:40 +0000317 */
Denis Vlasenko339f5eb2007-03-09 16:43:01 +0000318
319 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000320}