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
53 #include <sys/types.h>
65 # define S_IFMT 0170000
67 #if !defined(S_ISBLK) && defined(S_IFBLK)
68 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
70 #if !defined(S_ISCHR) && defined(S_IFCHR)
71 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
73 #if !defined(S_ISDIR) && defined(S_IFDIR)
74 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
76 #if !defined(S_ISREG) && defined(S_IFREG)
77 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
79 #if !defined(S_ISFIFO) && defined(S_IFIFO)
80 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
82 #if !defined(S_ISLNK) && defined(S_IFLNK)
83 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
85 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
86 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
88 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
89 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
90 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
92 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
93 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
96 #define IN_CTYPE_DOMAIN(c) 1
99 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
101 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
104 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
106 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
109 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
110 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
111 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
112 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
113 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
114 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
115 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
116 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
117 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
118 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
119 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
121 #define isodigit(c) ((c) >= '0' && (c) <= '7')
122 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
123 #define octtobin(c) ((c) - '0')
125 static double xstrtod __P((char *s));
126 static int print_esc __P((char *escstart));
127 static int print_formatted __P((char *format, int argc, char **argv));
128 static long xstrtol __P((char *s));
129 static unsigned long xstrtoul __P((char *s));
130 static void print_direc
133 (char *start, size_t length, int field_width, int precision,
135 static void print_esc_char __P((int c));
136 static void print_esc_string __P((char *str));
137 static void verify __P((char *s, char *end));
139 /* The value to return to the calling program. */
140 static int exit_status;
142 static const char printf_usage[] = "printf format [argument...]\n";
144 int printf_main(int argc, char **argv)
150 if (argc <= 1 || **(argv + 1) == '-') {
159 args_used = print_formatted(format, argc, argv);
163 while (args_used > 0 && argc > 0);
167 fprintf(stderr, "excess args ignored");
173 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
174 arguments to any `%' directives.
175 Return the number of elements of ARGV used. */
177 static int print_formatted(char *format, int argc, char **argv)
179 int save_argc = argc; /* Preserve original value. */
180 char *f; /* Pointer into `format'. */
181 char *direc_start; /* Start of % directive. */
182 size_t direc_length; /* Length of % directive. */
183 int field_width; /* Arg to first '*', or -1 if none. */
184 int precision; /* Arg to second '*', or -1 if none. */
186 for (f = format; *f; ++f) {
191 field_width = precision = -1;
198 print_esc_string(*argv);
204 if (strchr("-+ #", *f)) {
212 field_width = xstrtoul(*argv);
218 while (ISDIGIT(*f)) {
229 precision = xstrtoul(*argv);
235 while (ISDIGIT(*f)) {
240 if (*f == 'l' || *f == 'L' || *f == 'h') {
245 if (!strchr ("diouxXfeEgGcs", *f))
246 fprintf(stderr, "%%%c: invalid directive", *f);
250 print_direc(direc_start, direc_length, field_width,
255 print_direc(direc_start, direc_length, field_width,
268 return save_argc - argc;
271 /* Print a \ escape sequence starting at ESCSTART.
272 Return the number of characters in the escape sequence
273 besides the backslash. */
275 static int print_esc(char *escstart)
277 register char *p = escstart + 1;
278 int esc_value = 0; /* Value of \nnn escape. */
279 int esc_length; /* Length of \nnn escape. */
281 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
283 for (esc_length = 0, ++p;
284 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
285 esc_value = esc_value * 16 + hextobin(*p);
286 /* if (esc_length == 0)
287 fprintf(stderr, "missing hex in esc");
290 } else if (*p == '0') {
291 for (esc_length = 0, ++p;
292 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
293 esc_value = esc_value * 8 + octtobin(*p);
295 } else if (strchr("\"\\abcfnrtv", *p))
296 print_esc_char(*p++);
298 fprintf(stderr, "\\%c: invalid esc", *p);
300 return p - escstart - 1;
303 /* Output a single-character \ escape. */
305 static void print_esc_char(int c)
308 case 'a': /* Alert. */
311 case 'b': /* Backspace. */
314 case 'c': /* Cancel the rest of the output. */
317 case 'f': /* Form feed. */
320 case 'n': /* New line. */
323 case 'r': /* Carriage return. */
326 case 't': /* Horizontal tab. */
329 case 'v': /* Vertical tab. */
338 /* Print string STR, evaluating \ escapes. */
340 static void print_esc_string(char *str)
344 str += print_esc(str);
350 print_direc(char *start, size_t length, int field_width, int precision,
353 char *p; /* Null-terminated copy of % directive. */
355 p = xmalloc((unsigned) (length + 1));
356 strncpy(p, start, length);
359 switch (p[length - 1]) {
362 if (field_width < 0) {
364 printf(p, xstrtol(argument));
366 printf(p, precision, xstrtol(argument));
369 printf(p, field_width, xstrtol(argument));
371 printf(p, field_width, precision, xstrtol(argument));
379 if (field_width < 0) {
381 printf(p, xstrtoul(argument));
383 printf(p, precision, xstrtoul(argument));
386 printf(p, field_width, xstrtoul(argument));
388 printf(p, field_width, precision, xstrtoul(argument));
397 if (field_width < 0) {
399 printf(p, xstrtod(argument));
401 printf(p, precision, xstrtod(argument));
404 printf(p, field_width, xstrtod(argument));
406 printf(p, field_width, precision, xstrtod(argument));
411 printf(p, *argument);
415 if (field_width < 0) {
419 printf(p, precision, argument);
422 printf(p, field_width, argument);
424 printf(p, field_width, precision, argument);
432 static unsigned long xstrtoul(char *s)
438 val = strtoul(s, &end, 0);
443 static long xstrtol(char *s)
449 val = strtol(s, &end, 0);
454 static double xstrtod(char *s)
460 val = strtod(s, &end);
465 static void verify(char *s, char *end)
468 fprintf(stderr, "%s", s);
473 fprintf(stderr, "%s: expected numeric", s);
475 fprintf(stderr, "%s: not completely converted", s);