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