1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include "libbb.h"
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68typedef void FAST_FUNC (*converter)(const char *arg, void *result);
69
70static int multiconvert(const char *arg, void *result, converter convert)
71{
72 if (*arg == '"' || *arg == '\'') {
73 arg = utoa((unsigned char)arg[1]);
74 }
75 errno = 0;
76 convert(arg, result);
77 if (errno) {
78 bb_error_msg("invalid number '%s'", arg);
79 return 1;
80 }
81 return 0;
82}
83
84static void FAST_FUNC conv_strtoull(const char *arg, void *result)
85{
86 *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
87
88
89
90
91
92 if (errno) {
93 *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
94 }
95}
96static void FAST_FUNC conv_strtoll(const char *arg, void *result)
97{
98 *(long long*)result = bb_strtoll(arg, NULL, 0);
99}
100static void FAST_FUNC conv_strtod(const char *arg, void *result)
101{
102 char *end;
103
104
105 *(double*)result = strtod(arg, &end);
106 if (end[0]) {
107 errno = ERANGE;
108 *(double*)result = 0;
109 }
110}
111
112
113static unsigned long long my_xstrtoull(const char *arg)
114{
115 unsigned long long result;
116 if (multiconvert(arg, &result, conv_strtoull))
117 result = 0;
118 return result;
119}
120static long long my_xstrtoll(const char *arg)
121{
122 long long result;
123 if (multiconvert(arg, &result, conv_strtoll))
124 result = 0;
125 return result;
126}
127static double my_xstrtod(const char *arg)
128{
129 double result;
130 multiconvert(arg, &result, conv_strtod);
131 return result;
132}
133
134
135static int print_esc_string(const char *str)
136{
137 char c;
138 while ((c = *str) != '\0') {
139 str++;
140 if (c == '\\') {
141
142 if (*str == '0') {
143 if ((unsigned char)(str[1] - '0') < 8) {
144
145 str++;
146 }
147 }
148 else if (*str == 'c') {
149 return 1;
150 }
151 {
152
153
154 const char *z = str;
155 c = bb_process_escape_sequence(&z);
156 str = z;
157 }
158 }
159 putchar(c);
160 }
161
162 return 0;
163}
164
165static void print_direc(char *format, unsigned fmt_length,
166 int field_width, int precision,
167 const char *argument)
168{
169 long long llv;
170 double dv;
171 char saved;
172 char *have_prec, *have_width;
173
174 saved = format[fmt_length];
175 format[fmt_length] = '\0';
176
177 have_prec = strstr(format, ".*");
178 have_width = strchr(format, '*');
179 if (have_width - 1 == have_prec)
180 have_width = NULL;
181
182 errno = 0;
183
184 switch (format[fmt_length - 1]) {
185 case 'c':
186 printf(format, *argument);
187 break;
188 case 'd':
189 case 'i':
190 llv = my_xstrtoll(argument);
191 print_long:
192 if (!have_width) {
193 if (!have_prec)
194 printf(format, llv);
195 else
196 printf(format, precision, llv);
197 } else {
198 if (!have_prec)
199 printf(format, field_width, llv);
200 else
201 printf(format, field_width, precision, llv);
202 }
203 break;
204 case 'o':
205 case 'u':
206 case 'x':
207 case 'X':
208 llv = my_xstrtoull(argument);
209
210 goto print_long;
211 case 's':
212
213 if (sizeof(argument) == sizeof(llv)) {
214 llv = (long long)(ptrdiff_t)argument;
215 goto print_long;
216 } else {
217
218
219 if (!have_width) {
220 if (!have_prec)
221 printf(format, argument, argument, argument);
222 else
223 printf(format, precision, argument, argument);
224 } else {
225 if (!have_prec)
226 printf(format, field_width, argument, argument);
227 else
228 printf(format, field_width, precision, argument);
229 }
230 break;
231 }
232 case 'f':
233 case 'e':
234 case 'E':
235 case 'g':
236 case 'G':
237 dv = my_xstrtod(argument);
238 if (!have_width) {
239 if (!have_prec)
240 printf(format, dv);
241 else
242 printf(format, precision, dv);
243 } else {
244 if (!have_prec)
245 printf(format, field_width, dv);
246 else
247 printf(format, field_width, precision, dv);
248 }
249 break;
250 }
251
252 format[fmt_length] = saved;
253}
254
255
256static int get_width_prec(const char *str)
257{
258 int v = bb_strtoi(str, NULL, 10);
259 if (errno) {
260 bb_error_msg("invalid number '%s'", str);
261 v = 0;
262 }
263 return v;
264}
265
266
267
268static char **print_formatted(char *f, char **argv, int *conv_err)
269{
270 char *direc_start;
271 unsigned direc_length;
272 int field_width;
273 int precision;
274 char **saved_argv = argv;
275
276 for (; *f; ++f) {
277 switch (*f) {
278 case '%':
279 direc_start = f++;
280 direc_length = 1;
281 field_width = precision = 0;
282 if (*f == '%') {
283 bb_putchar('%');
284 break;
285 }
286 if (*f == 'b') {
287 if (*argv) {
288 if (print_esc_string(*argv))
289 return saved_argv;
290 ++argv;
291 }
292 break;
293 }
294 if (strchr("-+ #", *f)) {
295 ++f;
296 ++direc_length;
297 }
298 if (*f == '*') {
299 ++f;
300 ++direc_length;
301 if (*argv)
302 field_width = get_width_prec(*argv++);
303 } else {
304 while (isdigit(*f)) {
305 ++f;
306 ++direc_length;
307 }
308 }
309 if (*f == '.') {
310 ++f;
311 ++direc_length;
312 if (*f == '*') {
313 ++f;
314 ++direc_length;
315 if (*argv)
316 precision = get_width_prec(*argv++);
317 } else {
318 while (isdigit(*f)) {
319 ++f;
320 ++direc_length;
321 }
322 }
323 }
324
325
326
327
328
329 while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
330 overlapping_strcpy(f, f + 1);
331 }
332
333 {
334 static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
335 char *p = strchr(format_chars, *f);
336
337 if (p == NULL) {
338 bb_error_msg("%s: invalid format", direc_start);
339
340 return saved_argv - 1;
341 }
342 ++direc_length;
343 if (p - format_chars <= 5) {
344
345 p = xmalloc(direc_length + 3);
346 memcpy(p, direc_start, direc_length);
347 p[direc_length + 1] = p[direc_length - 1];
348 p[direc_length - 1] = 'l';
349 p[direc_length] = 'l';
350
351 direc_length += 2;
352 direc_start = p;
353 } else {
354 p = NULL;
355 }
356 if (*argv) {
357 print_direc(direc_start, direc_length, field_width,
358 precision, *argv++);
359 } else {
360 print_direc(direc_start, direc_length, field_width,
361 precision, "");
362 }
363 *conv_err |= errno;
364 free(p);
365 }
366 break;
367 case '\\':
368 if (*++f == 'c') {
369 return saved_argv;
370 }
371 bb_putchar(bb_process_escape_sequence((const char **)&f));
372 f--;
373 break;
374 default:
375 putchar(*f);
376 }
377 }
378
379 return argv;
380}
381
382int printf_main(int argc UNUSED_PARAM, char **argv)
383{
384 int conv_err;
385 char *format;
386 char **argv2;
387
388
389
390
391
392
393
394
395
396
397
398 if (fcntl(1, F_GETFL) == -1)
399 return 1;
400
401
402
403
404
405
406 if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
407 argv++;
408 if (!argv[1]) {
409 if (ENABLE_ASH_BUILTIN_PRINTF
410 && applet_name[0] != 'p'
411 ) {
412 bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
413 return 2;
414 }
415 bb_show_usage();
416 }
417
418 format = argv[1];
419 argv2 = argv + 2;
420
421 conv_err = 0;
422 do {
423 argv = argv2;
424 argv2 = print_formatted(format, argv, &conv_err);
425 } while (argv2 > argv && *argv2);
426
427
428
429
430
431
432 return (argv2 < argv)
433 || conv_err;
434}
435