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>
62 static const int S_IFMT = 0170000;
64 #if !defined(S_ISBLK) && defined(S_IFBLK)
65 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
67 #if !defined(S_ISCHR) && defined(S_IFCHR)
68 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
70 #if !defined(S_ISDIR) && defined(S_IFDIR)
71 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73 #if !defined(S_ISREG) && defined(S_IFREG)
74 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76 #if !defined(S_ISFIFO) && defined(S_IFIFO)
77 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
79 #if !defined(S_ISLNK) && defined(S_IFLNK)
80 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
82 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
83 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
85 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
86 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
87 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
89 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
90 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
93 #define IN_CTYPE_DOMAIN(c) 1
95 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
96 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
97 #define ISDIGIT(c) (((unsigned char) (c)) - '0' <= 9)
99 #define isodigit(c) ((c) >= '0' && (c) <= '7')
100 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
101 #define octtobin(c) ((c) - '0')
103 static double xstrtod __P((char *s));
104 static int print_esc __P((char *escstart));
105 static int print_formatted __P((char *format, int argc, char **argv));
106 static long xstrtol __P((char *s));
107 static unsigned long xstrtoul __P((char *s));
108 static void print_direc
111 (char *start, size_t length, int field_width, int precision,
113 static void print_esc_char __P((int c));
114 static void print_esc_string __P((char *str));
115 static void verify __P((char *s, char *end));
117 /* The value to return to the calling program. */
118 static int exit_status;
120 int printf_main(int argc, char **argv)
126 if (argc <= 1 || **(argv + 1) == '-') {
135 args_used = print_formatted(format, argc, argv);
139 while (args_used > 0 && argc > 0);
143 fprintf(stderr, "excess args ignored");
149 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
150 arguments to any `%' directives.
151 Return the number of elements of ARGV used. */
153 static int print_formatted(char *format, int argc, char **argv)
155 int save_argc = argc; /* Preserve original value. */
156 char *f; /* Pointer into `format'. */
157 char *direc_start; /* Start of % directive. */
158 size_t direc_length; /* Length of % directive. */
159 int field_width; /* Arg to first '*', or -1 if none. */
160 int precision; /* Arg to second '*', or -1 if none. */
162 for (f = format; *f; ++f) {
167 field_width = precision = -1;
174 print_esc_string(*argv);
180 if (strchr("-+ #", *f)) {
188 field_width = xstrtoul(*argv);
194 while (ISDIGIT(*f)) {
205 precision = xstrtoul(*argv);
211 while (ISDIGIT(*f)) {
216 if (*f == 'l' || *f == 'L' || *f == 'h') {
221 if (!strchr ("diouxXfeEgGcs", *f))
222 fprintf(stderr, "%%%c: invalid directive", *f);
226 print_direc(direc_start, direc_length, field_width,
231 print_direc(direc_start, direc_length, field_width,
244 return save_argc - argc;
247 /* Print a \ escape sequence starting at ESCSTART.
248 Return the number of characters in the escape sequence
249 besides the backslash. */
251 static int print_esc(char *escstart)
253 register char *p = escstart + 1;
254 int esc_value = 0; /* Value of \nnn escape. */
255 int esc_length; /* Length of \nnn escape. */
257 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
259 for (esc_length = 0, ++p;
260 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
261 esc_value = esc_value * 16 + hextobin(*p);
262 /* if (esc_length == 0)
263 fprintf(stderr, "missing hex in esc");
266 } else if (*p == '0') {
267 for (esc_length = 0, ++p;
268 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
269 esc_value = esc_value * 8 + octtobin(*p);
271 } else if (strchr("\"\\abcfnrtv", *p))
272 print_esc_char(*p++);
274 fprintf(stderr, "\\%c: invalid esc", *p);
276 return p - escstart - 1;
279 /* Output a single-character \ escape. */
281 static void print_esc_char(int c)
284 case 'a': /* Alert. */
287 case 'b': /* Backspace. */
290 case 'c': /* Cancel the rest of the output. */
293 case 'f': /* Form feed. */
296 case 'n': /* New line. */
299 case 'r': /* Carriage return. */
302 case 't': /* Horizontal tab. */
305 case 'v': /* Vertical tab. */
314 /* Print string STR, evaluating \ escapes. */
316 static void print_esc_string(char *str)
320 str += print_esc(str);
326 print_direc(char *start, size_t length, int field_width, int precision,
329 char *p; /* Null-terminated copy of % directive. */
331 p = xmalloc((unsigned) (length + 1));
332 strncpy(p, start, length);
335 switch (p[length - 1]) {
338 if (field_width < 0) {
340 printf(p, xstrtol(argument));
342 printf(p, precision, xstrtol(argument));
345 printf(p, field_width, xstrtol(argument));
347 printf(p, field_width, precision, xstrtol(argument));
355 if (field_width < 0) {
357 printf(p, xstrtoul(argument));
359 printf(p, precision, xstrtoul(argument));
362 printf(p, field_width, xstrtoul(argument));
364 printf(p, field_width, precision, xstrtoul(argument));
373 if (field_width < 0) {
375 printf(p, xstrtod(argument));
377 printf(p, precision, xstrtod(argument));
380 printf(p, field_width, xstrtod(argument));
382 printf(p, field_width, precision, xstrtod(argument));
387 printf(p, *argument);
391 if (field_width < 0) {
395 printf(p, precision, argument);
398 printf(p, field_width, argument);
400 printf(p, field_width, precision, argument);
408 static unsigned long xstrtoul(char *s)
414 val = strtoul(s, &end, 0);
419 static long xstrtol(char *s)
425 val = strtol(s, &end, 0);
430 static double xstrtod(char *s)
436 val = strtod(s, &end);
441 static void verify(char *s, char *end)
444 fprintf(stderr, "%s", s);
449 fprintf(stderr, "%s: expected numeric", s);
451 fprintf(stderr, "%s: not completely converted", s);