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