1 // I may still need some more cleaning...fix my error checking
6 /* printf - format and print data
7 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Usage: printf format [argument...]
25 A front end to the printf function that lets it be used from the shell.
33 \c = produce no further output
39 \0ooo = octal number (ooo is 0 to 3 digits)
40 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
44 %b = print an argument string, interpreting backslash escapes
46 The `format' argument is re-used as many times as necessary
47 to convert all of the given arguments.
49 David MacKenzie <djm@gnu.ai.mit.edu> */
52 // 19990508 Busy Boxed! Dave Cinege
56 #include <sys/types.h>
68 # define S_IFMT 0170000
70 #if !defined(S_ISBLK) && defined(S_IFBLK)
71 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
73 #if !defined(S_ISCHR) && defined(S_IFCHR)
74 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
76 #if !defined(S_ISDIR) && defined(S_IFDIR)
77 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
79 #if !defined(S_ISREG) && defined(S_IFREG)
80 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
82 #if !defined(S_ISFIFO) && defined(S_IFIFO)
83 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
85 #if !defined(S_ISLNK) && defined(S_IFLNK)
86 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
88 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
89 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
91 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
92 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
93 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
95 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
96 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
99 #define IN_CTYPE_DOMAIN(c) 1
102 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
104 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
107 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
109 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
112 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
113 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
114 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
115 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
116 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
117 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
118 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
119 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
120 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
121 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
122 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
124 #define isodigit(c) ((c) >= '0' && (c) <= '7')
125 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
126 #define octtobin(c) ((c) - '0')
130 static double xstrtod __P ((char *s));
131 static int print_esc __P ((char *escstart));
132 static int print_formatted __P ((char *format, int argc, char **argv));
133 static long xstrtol __P ((char *s));
134 static unsigned long xstrtoul __P ((char *s));
135 static void print_direc __P ((char *start, size_t length, int field_width, int precision, char *argument));
136 static void print_esc_char __P ((int c));
137 static void print_esc_string __P ((char *str));
138 static void verify __P ((char *s, char *end));
140 /* The value to return to the calling program. */
141 static int exit_status;
143 const char printf_usage[] = "Usage: printf format [argument...]\n";
146 printf_main(struct FileInfo * i, int argc, char * * argv)
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. */
178 print_formatted (char *format, int argc, char **argv)
180 int save_argc = argc; /* Preserve original value. */
181 char *f; /* Pointer into `format'. */
182 char *direc_start; /* Start of % directive. */
183 size_t direc_length; /* Length of % directive. */
184 int field_width; /* Arg to first '*', or -1 if none. */
185 int precision; /* Arg to second '*', or -1 if none. */
187 for (f = format; *f; ++f)
194 field_width = precision = -1;
204 print_esc_string (*argv);
210 if (strchr ("-+ #", *f))
221 field_width = xstrtoul (*argv);
244 precision = xstrtoul (*argv);
258 if (*f == 'l' || *f == 'L' || *f == 'h')
264 if (!strchr ("diouxXfeEgGcs", *f))
265 fprintf(stderr, "%%%c: invalid directive", *f);
270 print_direc (direc_start, direc_length, field_width,
276 print_direc (direc_start, direc_length, field_width,
289 return save_argc - argc;
292 /* Print a \ escape sequence starting at ESCSTART.
293 Return the number of characters in the escape sequence
294 besides the backslash. */
297 print_esc (char *escstart)
299 register char *p = escstart + 1;
300 int esc_value = 0; /* Value of \nnn escape. */
301 int esc_length; /* Length of \nnn escape. */
303 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
306 for (esc_length = 0, ++p;
307 esc_length < 3 && ISXDIGIT (*p);
309 esc_value = esc_value * 16 + hextobin (*p);
310 /* if (esc_length == 0)
311 fprintf(stderr, "missing hex in esc");
317 for (esc_length = 0, ++p;
318 esc_length < 3 && isodigit (*p);
320 esc_value = esc_value * 8 + octtobin (*p);
323 else if (strchr ("\"\\abcfnrtv", *p))
324 print_esc_char (*p++);
326 fprintf(stderr, "\\%c: invalid esc", *p);
328 return p - escstart - 1;
331 /* Output a single-character \ escape. */
334 print_esc_char (int c)
338 case 'a': /* Alert. */
341 case 'b': /* Backspace. */
344 case 'c': /* Cancel the rest of the output. */
347 case 'f': /* Form feed. */
350 case 'n': /* New line. */
353 case 'r': /* Carriage return. */
356 case 't': /* Horizontal tab. */
359 case 'v': /* Vertical tab. */
368 /* Print string STR, evaluating \ escapes. */
371 print_esc_string (char *str)
375 str += print_esc (str);
381 print_direc (char *start, size_t length, int field_width, int precision, char *argument)
383 char *p; /* Null-terminated copy of % directive. */
385 p = xmalloc ((unsigned) (length + 1));
386 strncpy (p, start, length);
389 switch (p[length - 1])
396 printf (p, xstrtol (argument));
398 printf (p, precision, xstrtol (argument));
403 printf (p, field_width, xstrtol (argument));
405 printf (p, field_width, precision, xstrtol (argument));
416 printf (p, xstrtoul (argument));
418 printf (p, precision, xstrtoul (argument));
423 printf (p, field_width, xstrtoul (argument));
425 printf (p, field_width, precision, xstrtoul (argument));
437 printf (p, xstrtod (argument));
439 printf (p, precision, xstrtod (argument));
444 printf (p, field_width, xstrtod (argument));
446 printf (p, field_width, precision, xstrtod (argument));
451 printf (p, *argument);
458 printf (p, argument);
460 printf (p, precision, argument);
465 printf (p, field_width, argument);
467 printf (p, field_width, precision, argument);
482 val = strtoul (s, &end, 0);
494 val = strtol (s, &end, 0);
506 val = strtod (s, &end);
512 verify (char *s, char *end)
516 fprintf(stderr, "%s", s);
523 fprintf(stderr, "%s: expected numeric", s);
525 fprintf(stderr, "%s: not completely converted", s);