1 /* printf - format and print data
2 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Usage: printf format [argument...]
20 A front end to the printf function that lets it be used from the shell.
28 \c = produce no further output
34 \0ooo = octal number (ooo is 0 to 3 digits)
35 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
39 %b = print an argument string, interpreting backslash escapes
41 The `format' argument is re-used as many times as necessary
42 to convert all of the given arguments.
44 David MacKenzie <djm@gnu.ai.mit.edu> */
47 // 19990508 Busy Boxed! Dave Cinege
52 #include <sys/types.h>
64 # define S_IFMT 0170000
66 #if !defined(S_ISBLK) && defined(S_IFBLK)
67 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
69 #if !defined(S_ISCHR) && defined(S_IFCHR)
70 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
72 #if !defined(S_ISDIR) && defined(S_IFDIR)
73 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
75 #if !defined(S_ISREG) && defined(S_IFREG)
76 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
78 #if !defined(S_ISFIFO) && defined(S_IFIFO)
79 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
81 #if !defined(S_ISLNK) && defined(S_IFLNK)
82 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
84 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
85 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
87 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
88 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
89 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
91 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
92 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
95 #define IN_CTYPE_DOMAIN(c) 1
98 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
100 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
103 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
105 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
108 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
109 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
110 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
111 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
112 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
113 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
114 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
115 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
116 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
117 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
118 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
120 #define isodigit(c) ((c) >= '0' && (c) <= '7')
121 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
122 #define octtobin(c) ((c) - '0')
124 static double xstrtod __P ((char *s));
125 static int print_esc __P ((char *escstart));
126 static int print_formatted __P ((char *format, int argc, char **argv));
127 static long xstrtol __P ((char *s));
128 static unsigned long xstrtoul __P ((char *s));
129 static void print_direc __P ((char *start, size_t length, int field_width, int precision, char *argument));
130 static void print_esc_char __P ((int c));
131 static void print_esc_string __P ((char *str));
132 static void verify __P ((char *s, char *end));
134 /* The value to return to the calling program. */
135 static int exit_status;
137 static const char printf_usage[] = "printf format [argument...]\n";
140 printf_main(int argc, char** argv)
146 if ( **(argv+1) == '-' ) {
147 usage (printf_usage);
156 args_used = print_formatted (format, argc, argv);
160 while (args_used > 0 && argc > 0);
164 fprintf(stderr, "excess args ignored");
170 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
171 arguments to any `%' directives.
172 Return the number of elements of ARGV used. */
175 print_formatted (char *format, int argc, char **argv)
177 int save_argc = argc; /* Preserve original value. */
178 char *f; /* Pointer into `format'. */
179 char *direc_start; /* Start of % directive. */
180 size_t direc_length; /* Length of % directive. */
181 int field_width; /* Arg to first '*', or -1 if none. */
182 int precision; /* Arg to second '*', or -1 if none. */
184 for (f = format; *f; ++f)
191 field_width = precision = -1;
201 print_esc_string (*argv);
207 if (strchr ("-+ #", *f))
218 field_width = xstrtoul (*argv);
241 precision = xstrtoul (*argv);
255 if (*f == 'l' || *f == 'L' || *f == 'h')
261 if (!strchr ("diouxXfeEgGcs", *f))
262 fprintf(stderr, "%%%c: invalid directive", *f);
267 print_direc (direc_start, direc_length, field_width,
273 print_direc (direc_start, direc_length, field_width,
286 return save_argc - argc;
289 /* Print a \ escape sequence starting at ESCSTART.
290 Return the number of characters in the escape sequence
291 besides the backslash. */
294 print_esc (char *escstart)
296 register char *p = escstart + 1;
297 int esc_value = 0; /* Value of \nnn escape. */
298 int esc_length; /* Length of \nnn escape. */
300 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
303 for (esc_length = 0, ++p;
304 esc_length < 3 && ISXDIGIT (*p);
306 esc_value = esc_value * 16 + hextobin (*p);
307 /* if (esc_length == 0)
308 fprintf(stderr, "missing hex in esc");
314 for (esc_length = 0, ++p;
315 esc_length < 3 && isodigit (*p);
317 esc_value = esc_value * 8 + octtobin (*p);
320 else if (strchr ("\"\\abcfnrtv", *p))
321 print_esc_char (*p++);
323 fprintf(stderr, "\\%c: invalid esc", *p);
325 return p - escstart - 1;
328 /* Output a single-character \ escape. */
331 print_esc_char (int c)
335 case 'a': /* Alert. */
338 case 'b': /* Backspace. */
341 case 'c': /* Cancel the rest of the output. */
344 case 'f': /* Form feed. */
347 case 'n': /* New line. */
350 case 'r': /* Carriage return. */
353 case 't': /* Horizontal tab. */
356 case 'v': /* Vertical tab. */
365 /* Print string STR, evaluating \ escapes. */
368 print_esc_string (char *str)
372 str += print_esc (str);
378 print_direc (char *start, size_t length, int field_width, int precision, char *argument)
380 char *p; /* Null-terminated copy of % directive. */
382 p = xmalloc ((unsigned) (length + 1));
383 strncpy (p, start, length);
386 switch (p[length - 1])
393 printf (p, xstrtol (argument));
395 printf (p, precision, xstrtol (argument));
400 printf (p, field_width, xstrtol (argument));
402 printf (p, field_width, precision, xstrtol (argument));
413 printf (p, xstrtoul (argument));
415 printf (p, precision, xstrtoul (argument));
420 printf (p, field_width, xstrtoul (argument));
422 printf (p, field_width, precision, xstrtoul (argument));
434 printf (p, xstrtod (argument));
436 printf (p, precision, xstrtod (argument));
441 printf (p, field_width, xstrtod (argument));
443 printf (p, field_width, precision, xstrtod (argument));
448 printf (p, *argument);
455 printf (p, argument);
457 printf (p, precision, argument);
462 printf (p, field_width, argument);
464 printf (p, field_width, precision, argument);
479 val = strtoul (s, &end, 0);
491 val = strtol (s, &end, 0);
503 val = strtod (s, &end);
509 verify (char *s, char *end)
513 fprintf(stderr, "%s", s);
520 fprintf(stderr, "%s: expected numeric", s);
522 fprintf(stderr, "%s: not completely converted", s);