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