1 /* printf - format and print data
2 Copyright (C) 1990, 1991 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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> */
48 #include <sys/types.h>
54 unsigned long strtoul ();
57 #define isodigit(c) ((c) >= '0' && (c) <= '7')
58 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
59 #define octtobin(c) ((c) - '0')
64 static double xstrtod ();
65 static int print_esc ();
66 static int print_formatted ();
67 static long xstrtol ();
68 static unsigned long xstrtoul ();
69 static void print_direc ();
70 static void print_esc_char ();
71 static void print_esc_string ();
72 static void verify ();
74 /* The name this program was run with. */
77 /* The value to return to the calling program. */
78 static int exit_status;
88 program_name = argv[0];
93 fprintf (stderr, "Usage: %s format [argument...]\n", program_name);
103 args_used = print_formatted (format, argc, argv);
107 while (args_used > 0 && argc > 0);
112 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
113 arguments to any `%' directives.
114 Return the number of elements of ARGV used. */
117 print_formatted (format, argc, argv)
122 int save_argc = argc; /* Preserve original value. */
123 char *f; /* Pointer into `format'. */
124 char *direc_start; /* Start of % directive. */
125 int direc_length; /* Length of % directive. */
126 int field_width; /* Arg to first '*', or -1 if none. */
127 int precision; /* Arg to second '*', or -1 if none. */
129 for (f = format; *f; ++f)
136 field_width = precision = -1;
146 print_esc_string (*argv);
152 if (index ("-+ #", *f))
163 field_width = xstrtoul (*argv);
186 precision = xstrtoul (*argv);
200 if (*f == 'l' || *f == 'L' || *f == 'h')
205 if (!index ("diouxXfeEgGcs", *f))
206 error (1, 0, "%%%c: invalid directive", *f);
210 print_direc (direc_start, direc_length, field_width,
216 print_direc (direc_start, direc_length, field_width,
229 return save_argc - argc;
232 /* Print a \ escape sequence starting at ESCSTART.
233 Return the number of characters in the escape sequence
234 besides the backslash. */
240 register char *p = escstart + 1;
241 int esc_value = 0; /* Value of \nnn escape. */
242 int esc_length; /* Length of \nnn escape. */
244 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
247 for (esc_length = 0, ++p;
248 esc_length < 3 && isxdigit (*p);
250 esc_value = esc_value * 16 + hextobin (*p);
252 error (1, 0, "missing hexadecimal number in escape");
257 for (esc_length = 0, ++p;
258 esc_length < 3 && isodigit (*p);
260 esc_value = esc_value * 8 + octtobin (*p);
263 else if (index ("\"\\abcfnrtv", *p))
264 print_esc_char (*p++);
266 error (1, 0, "\\%c: invalid escape", *p);
267 return p - escstart - 1;
270 /* Output a single-character \ escape. */
278 case 'a': /* Alert. */
281 case 'b': /* Backspace. */
284 case 'c': /* Cancel the rest of the output. */
287 case 'f': /* Form feed. */
290 case 'n': /* New line. */
293 case 'r': /* Carriage return. */
296 case 't': /* Horizontal tab. */
299 case 'v': /* Vertical tab. */
308 /* Print string STR, evaluating \ escapes. */
311 print_esc_string (str)
316 str += print_esc (str);
321 /* Output a % directive. START is the start of the directive,
322 LENGTH is its length, and ARGUMENT is its argument.
323 If FIELD_WIDTH or PRECISION is non-negative, they are args for
324 '*' values in those fields. */
327 print_direc (start, length, field_width, precision, argument)
334 char *p; /* Null-terminated copy of % directive. */
336 p = xmalloc ((unsigned) (length + 1));
337 strncpy (p, start, length);
340 switch (p[length - 1])
347 printf (p, xstrtol (argument));
349 printf (p, precision, xstrtol (argument));
354 printf (p, field_width, xstrtol (argument));
356 printf (p, field_width, precision, xstrtol (argument));
367 printf (p, xstrtoul (argument));
369 printf (p, precision, xstrtoul (argument));
374 printf (p, field_width, xstrtoul (argument));
376 printf (p, field_width, precision, xstrtoul (argument));
388 printf (p, xstrtod (argument));
390 printf (p, precision, xstrtod (argument));
395 printf (p, field_width, xstrtod (argument));
397 printf (p, field_width, precision, xstrtod (argument));
402 printf (p, *argument);
409 printf (p, argument);
411 printf (p, precision, argument);
416 printf (p, field_width, argument);
418 printf (p, field_width, precision, argument);
434 val = strtoul (s, &end, 0);
447 val = strtol (s, &end, 0);
460 val = strtod (s, &end);
471 error (0, errno, "%s", s);
477 error (0, 0, "%s: expected a numeric value", s);
479 error (0, 0, "%s: value not completely converted", s);