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>
63 static const int S_IFMT = 0170000;
65 #if !defined(S_ISBLK) && defined(S_IFBLK)
66 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
68 #if !defined(S_ISCHR) && defined(S_IFCHR)
69 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
71 #if !defined(S_ISDIR) && defined(S_IFDIR)
72 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
74 #if !defined(S_ISREG) && defined(S_IFREG)
75 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
77 #if !defined(S_ISFIFO) && defined(S_IFIFO)
78 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
80 #if !defined(S_ISLNK) && defined(S_IFLNK)
81 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
83 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
84 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
86 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
87 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
88 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
90 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
91 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
94 #define IN_CTYPE_DOMAIN(c) 1
96 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
97 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
98 #define ISDIGIT(c) (((unsigned char) (c)) - '0' <= 9)
100 #define isodigit(c) ((c) >= '0' && (c) <= '7')
101 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
102 #define octtobin(c) ((c) - '0')
104 static double xstrtod __P((char *s));
105 static int print_esc __P((char *escstart));
106 static int print_formatted __P((char *format, int argc, char **argv));
107 static long xstrtol __P((char *s));
108 static unsigned long xstrtoul __P((char *s));
109 static void print_direc __P( (char *start, size_t length,
110 int field_width, int precision, char *argument));
111 static void print_esc_char __P((int c));
112 static void print_esc_string __P((char *str));
114 /* The value to return to the calling program. */
115 static int exit_status;
117 int printf_main(int argc, char **argv)
123 if (argc <= 1 || **(argv + 1) == '-') {
132 args_used = print_formatted(format, argc, argv);
136 while (args_used > 0 && argc > 0);
140 fprintf(stderr, "excess args ignored");
146 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
147 arguments to any `%' directives.
148 Return the number of elements of ARGV used. */
150 static int print_formatted(char *format, int argc, char **argv)
152 int save_argc = argc; /* Preserve original value. */
153 char *f; /* Pointer into `format'. */
154 char *direc_start; /* Start of % directive. */
155 size_t direc_length; /* Length of % directive. */
156 int field_width; /* Arg to first '*', or -1 if none. */
157 int precision; /* Arg to second '*', or -1 if none. */
159 for (f = format; *f; ++f) {
164 field_width = precision = -1;
171 print_esc_string(*argv);
177 if (strchr("-+ #", *f)) {
185 field_width = xstrtoul(*argv);
191 while (ISDIGIT(*f)) {
202 precision = xstrtoul(*argv);
208 while (ISDIGIT(*f)) {
213 if (*f == 'l' || *f == 'L' || *f == 'h') {
218 if (!strchr ("diouxXfeEgGcs", *f))
219 fprintf(stderr, "%%%c: invalid directive", *f);
223 print_direc(direc_start, direc_length, field_width,
228 print_direc(direc_start, direc_length, field_width,
241 return save_argc - argc;
244 /* Print a \ escape sequence starting at ESCSTART.
245 Return the number of characters in the escape sequence
246 besides the backslash. */
248 static int print_esc(char *escstart)
250 register char *p = escstart + 1;
251 int esc_value = 0; /* Value of \nnn escape. */
252 int esc_length; /* Length of \nnn escape. */
254 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
256 for (esc_length = 0, ++p;
257 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
258 esc_value = esc_value * 16 + hextobin(*p);
259 /* if (esc_length == 0)
260 fprintf(stderr, "missing hex in esc");
263 } else if (*p == '0') {
264 for (esc_length = 0, ++p;
265 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
266 esc_value = esc_value * 8 + octtobin(*p);
268 } else if (strchr("\"\\abcfnrtv", *p))
269 print_esc_char(*p++);
271 fprintf(stderr, "\\%c: invalid esc", *p);
273 return p - escstart - 1;
276 /* Output a single-character \ escape. */
278 static void print_esc_char(int c)
281 case 'a': /* Alert. */
284 case 'b': /* Backspace. */
287 case 'c': /* Cancel the rest of the output. */
290 case 'f': /* Form feed. */
293 case 'n': /* New line. */
296 case 'r': /* Carriage return. */
299 case 't': /* Horizontal tab. */
302 case 'v': /* Vertical tab. */
311 /* Print string STR, evaluating \ escapes. */
313 static void print_esc_string(char *str)
317 str += print_esc(str);
323 print_direc(char *start, size_t length, int field_width, int precision,
326 char *p; /* Null-terminated copy of % directive. */
328 p = xmalloc((unsigned) (length + 1));
329 strncpy(p, start, length);
332 switch (p[length - 1]) {
335 if (field_width < 0) {
337 printf(p, xstrtol(argument));
339 printf(p, precision, xstrtol(argument));
342 printf(p, field_width, xstrtol(argument));
344 printf(p, field_width, precision, xstrtol(argument));
352 if (field_width < 0) {
354 printf(p, xstrtoul(argument));
356 printf(p, precision, xstrtoul(argument));
359 printf(p, field_width, xstrtoul(argument));
361 printf(p, field_width, precision, xstrtoul(argument));
370 if (field_width < 0) {
372 printf(p, xstrtod(argument));
374 printf(p, precision, xstrtod(argument));
377 printf(p, field_width, xstrtod(argument));
379 printf(p, field_width, precision, xstrtod(argument));
384 printf(p, *argument);
388 if (field_width < 0) {
392 printf(p, precision, argument);
395 printf(p, field_width, argument);
397 printf(p, field_width, precision, argument);
405 static unsigned long xstrtoul(char *arg)
407 unsigned long result;
409 //int errno_save = errno;
414 result = strtoul(arg, &endptr, 0);
415 if (errno != 0 || *endptr!='\0' || endptr==arg)
416 fprintf(stderr, "%s", arg);
417 //errno = errno_save;
421 static long xstrtol(char *arg)
425 //int errno_save = errno;
430 result = strtoul(arg, &endptr, 0);
431 if (errno != 0 || *endptr!='\0' || endptr==arg)
432 fprintf(stderr, "%s", arg);
433 //errno = errno_save;
437 static double xstrtod(char *arg)
441 //int errno_save = errno;
446 result = strtod(arg, &endptr);
447 if (errno != 0 || *endptr!='\0' || endptr==arg)
448 fprintf(stderr, "%s", arg);
449 //errno = errno_save;