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')
126 static double xstrtod __P ((char *s));
127 static int print_esc __P ((char *escstart));
128 static int print_formatted __P ((char *format, int argc, char **argv));
129 static long xstrtol __P ((char *s));
130 static unsigned long xstrtoul __P ((char *s));
131 static void print_direc __P ((char *start, size_t length, int field_width, int precision, char *argument));
132 static void print_esc_char __P ((int c));
133 static void print_esc_string __P ((char *str));
134 static void verify __P ((char *s, char *end));
136 /* The value to return to the calling program. */
137 static int exit_status;
139 static const char printf_usage[] = "printf format [argument...]\n";
142 printf_main(int argc, char** argv)
148 if ( **(argv+1) == '-' ) {
149 usage (printf_usage);
158 args_used = print_formatted (format, argc, argv);
162 while (args_used > 0 && argc > 0);
166 fprintf(stderr, "excess args ignored");
172 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
173 arguments to any `%' directives.
174 Return the number of elements of ARGV used. */
177 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)
193 field_width = precision = -1;
203 print_esc_string (*argv);
209 if (strchr ("-+ #", *f))
220 field_width = xstrtoul (*argv);
243 precision = xstrtoul (*argv);
257 if (*f == 'l' || *f == 'L' || *f == 'h')
263 if (!strchr ("diouxXfeEgGcs", *f))
264 fprintf(stderr, "%%%c: invalid directive", *f);
269 print_direc (direc_start, direc_length, field_width,
275 print_direc (direc_start, direc_length, field_width,
288 return save_argc - argc;
291 /* Print a \ escape sequence starting at ESCSTART.
292 Return the number of characters in the escape sequence
293 besides the backslash. */
296 print_esc (char *escstart)
298 register char *p = escstart + 1;
299 int esc_value = 0; /* Value of \nnn escape. */
300 int esc_length; /* Length of \nnn escape. */
302 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
305 for (esc_length = 0, ++p;
306 esc_length < 3 && ISXDIGIT (*p);
308 esc_value = esc_value * 16 + hextobin (*p);
309 /* if (esc_length == 0)
310 fprintf(stderr, "missing hex in esc");
316 for (esc_length = 0, ++p;
317 esc_length < 3 && isodigit (*p);
319 esc_value = esc_value * 8 + octtobin (*p);
322 else if (strchr ("\"\\abcfnrtv", *p))
323 print_esc_char (*p++);
325 fprintf(stderr, "\\%c: invalid esc", *p);
327 return p - escstart - 1;
330 /* Output a single-character \ escape. */
333 print_esc_char (int c)
337 case 'a': /* Alert. */
340 case 'b': /* Backspace. */
343 case 'c': /* Cancel the rest of the output. */
346 case 'f': /* Form feed. */
349 case 'n': /* New line. */
352 case 'r': /* Carriage return. */
355 case 't': /* Horizontal tab. */
358 case 'v': /* Vertical tab. */
367 /* Print string STR, evaluating \ escapes. */
370 print_esc_string (char *str)
374 str += print_esc (str);
380 print_direc (char *start, size_t length, int field_width, int precision, char *argument)
382 char *p; /* Null-terminated copy of % directive. */
384 p = xmalloc ((unsigned) (length + 1));
385 strncpy (p, start, length);
388 switch (p[length - 1])
395 printf (p, xstrtol (argument));
397 printf (p, precision, xstrtol (argument));
402 printf (p, field_width, xstrtol (argument));
404 printf (p, field_width, precision, xstrtol (argument));
415 printf (p, xstrtoul (argument));
417 printf (p, precision, xstrtoul (argument));
422 printf (p, field_width, xstrtoul (argument));
424 printf (p, field_width, precision, xstrtoul (argument));
436 printf (p, xstrtod (argument));
438 printf (p, precision, xstrtod (argument));
443 printf (p, field_width, xstrtod (argument));
445 printf (p, field_width, precision, xstrtod (argument));
450 printf (p, *argument);
457 printf (p, argument);
459 printf (p, precision, argument);
464 printf (p, field_width, argument);
466 printf (p, field_width, precision, argument);
481 val = strtoul (s, &end, 0);
493 val = strtol (s, &end, 0);
505 val = strtod (s, &end);
511 verify (char *s, char *end)
515 fprintf(stderr, "%s", s);
522 fprintf(stderr, "%s: expected numeric", s);
524 fprintf(stderr, "%s: not completely converted", s);