1 /* vi: set sw=4 ts=4: */
2 /* printf - format and print data
3 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Usage: printf format [argument...]
21 A front end to the printf function that lets it be used from the shell.
29 \c = produce no further output
35 \0ooo = octal number (ooo is 0 to 3 digits)
36 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
40 %b = print an argument string, interpreting backslash escapes
42 The `format' argument is re-used as many times as necessary
43 to convert all of the given arguments.
45 David MacKenzie <djm@gnu.ai.mit.edu> */
48 // 19990508 Busy Boxed! Dave Cinege
52 #include <sys/types.h>
61 static double xstrtod __P((char *s));
62 static long xstrtol __P((char *s));
63 static unsigned long xstrtoul __P((char *s));
64 static void print_esc_string __P((char *str));
65 static int print_formatted __P((char *format, int argc, char **argv));
66 static void print_direc __P( (char *start, size_t length,
67 int field_width, int precision, char *argument));
69 int printf_main(int argc, char **argv)
74 if (argc <= 1 || **(argv + 1) == '-') {
83 args_used = print_formatted(format, argc, argv);
87 while (args_used > 0 && argc > 0);
91 fprintf(stderr, "excess args ignored");
97 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
98 arguments to any `%' directives.
99 Return the number of elements of ARGV used. */
101 static int print_formatted(char *format, int argc, char **argv)
103 int save_argc = argc; /* Preserve original value. */
104 char *f; /* Pointer into `format'. */
105 char *direc_start; /* Start of % directive. */
106 size_t direc_length; /* Length of % directive. */
107 int field_width; /* Arg to first '*', or -1 if none. */
108 int precision; /* Arg to second '*', or -1 if none. */
110 for (f = format; *f; ++f) {
115 field_width = precision = -1;
122 print_esc_string(*argv);
128 if (strchr("-+ #", *f)) {
136 field_width = xstrtoul(*argv);
142 while (isdigit(*f)) {
153 precision = xstrtoul(*argv);
159 while (isdigit(*f)) {
164 if (*f == 'l' || *f == 'L' || *f == 'h') {
169 if (!strchr ("diouxXfeEgGcs", *f))
170 fprintf(stderr, "%%%c: invalid directive", *f);
174 print_direc(direc_start, direc_length, field_width,
179 print_direc(direc_start, direc_length, field_width,
186 putchar(bb_process_escape_sequence((const char **)&f));
195 return save_argc - argc;
199 print_direc(char *start, size_t length, int field_width, int precision,
202 char *p; /* Null-terminated copy of % directive. */
204 p = xmalloc((unsigned) (length + 1));
205 strncpy(p, start, length);
208 switch (p[length - 1]) {
211 if (field_width < 0) {
213 printf(p, xstrtol(argument));
215 printf(p, precision, xstrtol(argument));
218 printf(p, field_width, xstrtol(argument));
220 printf(p, field_width, precision, xstrtol(argument));
228 if (field_width < 0) {
230 printf(p, xstrtoul(argument));
232 printf(p, precision, xstrtoul(argument));
235 printf(p, field_width, xstrtoul(argument));
237 printf(p, field_width, precision, xstrtoul(argument));
246 if (field_width < 0) {
248 printf(p, xstrtod(argument));
250 printf(p, precision, xstrtod(argument));
253 printf(p, field_width, xstrtod(argument));
255 printf(p, field_width, precision, xstrtod(argument));
260 printf(p, *argument);
264 if (field_width < 0) {
268 printf(p, precision, argument);
271 printf(p, field_width, argument);
273 printf(p, field_width, precision, argument);
281 static unsigned long xstrtoul(char *arg)
283 unsigned long result;
284 if (safe_strtoul(arg, &result))
285 fprintf(stderr, "%s", arg);
289 static long xstrtol(char *arg)
292 if (safe_strtol(arg, &result))
293 fprintf(stderr, "%s", arg);
297 static double xstrtod(char *arg)
300 if (safe_strtod(arg, &result))
301 fprintf(stderr, "%s", arg);
305 static void print_esc_string(char *str)
307 for (; *str; str++) {
310 putchar(bb_process_escape_sequence((const char **)&str));