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 int print_formatted ();
66 unsigned long xstrtoul ();
69 void print_esc_char ();
70 void print_esc_string ();
73 /* The name this program was run with. */
76 /* The value to return to the calling program. */
77 static int exit_status;
87 program_name = argv[0];
92 fprintf (stderr, "Usage: %s format [argument...]\n", program_name);
102 args_used = print_formatted (format, argc, argv);
106 while (args_used > 0 && argc > 0);
111 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
112 arguments to any `%' directives.
113 Return the number of elements of ARGV used. */
116 print_formatted (format, argc, argv)
121 int save_argc = argc; /* Preserve original value. */
122 char *f; /* Pointer into `format'. */
123 char *direc_start; /* Start of % directive. */
124 int direc_length; /* Length of % directive. */
125 int field_width; /* Arg to first '*', or -1 if none. */
126 int precision; /* Arg to second '*', or -1 if none. */
128 for (f = format; *f; ++f)
135 field_width = precision = -1;
145 print_esc_string (*argv);
151 if (index ("-+ #", *f))
162 field_width = xstrtoul (*argv);
185 precision = xstrtoul (*argv);
199 if (*f == 'l' || *f == 'L' || *f == 'h')
204 if (!index ("diouxXfeEgGcs", *f))
205 error (1, 0, "%%%c: invalid directive", *f);
209 print_direc (direc_start, direc_length, field_width,
215 print_direc (direc_start, direc_length, field_width,
228 return save_argc - argc;
231 /* Print a \ escape sequence starting at ESCSTART.
232 Return the number of characters in the escape sequence
233 besides the backslash. */
239 register char *p = escstart + 1;
240 int esc_value = 0; /* Value of \nnn escape. */
241 int esc_length; /* Length of \nnn escape. */
243 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
246 for (esc_length = 0, ++p;
247 esc_length < 3 && isxdigit (*p);
249 esc_value = esc_value * 16 + hextobin (*p);
251 error (1, 0, "missing hexadecimal number in escape");
256 for (esc_length = 0, ++p;
257 esc_length < 3 && isodigit (*p);
259 esc_value = esc_value * 8 + octtobin (*p);
262 else if (index ("\"\\abcfnrtv", *p))
263 print_esc_char (*p++);
265 error (1, 0, "\\%c: invalid escape", *p);
266 return p - escstart - 1;
269 /* Output a single-character \ escape. */
277 case 'a': /* Alert. */
280 case 'b': /* Backspace. */
283 case 'c': /* Cancel the rest of the output. */
286 case 'f': /* Form feed. */
289 case 'n': /* New line. */
292 case 'r': /* Carriage return. */
295 case 't': /* Horizontal tab. */
298 case 'v': /* Vertical tab. */
307 /* Print string STR, evaluating \ escapes. */
310 print_esc_string (str)
315 str += print_esc (str);
320 /* Output a % directive. START is the start of the directive,
321 LENGTH is its length, and ARGUMENT is its argument.
322 If FIELD_WIDTH or PRECISION is non-negative, they are args for
323 '*' values in those fields. */
326 print_direc (start, length, field_width, precision, argument)
333 char *p; /* Null-terminated copy of % directive. */
335 p = xmalloc ((unsigned) (length + 1));
336 strncpy (p, start, length);
339 switch (p[length - 1])
346 printf (p, xstrtol (argument));
348 printf (p, precision, xstrtol (argument));
353 printf (p, field_width, xstrtol (argument));
355 printf (p, field_width, precision, xstrtol (argument));
366 printf (p, xstrtoul (argument));
368 printf (p, precision, xstrtoul (argument));
373 printf (p, field_width, xstrtoul (argument));
375 printf (p, field_width, precision, xstrtoul (argument));
387 printf (p, xstrtod (argument));
389 printf (p, precision, xstrtod (argument));
394 printf (p, field_width, xstrtod (argument));
396 printf (p, field_width, precision, xstrtod (argument));
401 printf (p, *argument);
408 printf (p, argument);
410 printf (p, precision, argument);
415 printf (p, field_width, argument);
417 printf (p, field_width, precision, argument);
433 val = strtoul (s, &end, 0);
446 val = strtol (s, &end, 0);
459 val = strtod (s, &end);
470 error (0, errno, "%s", s);
476 error (0, 0, "%s: expected a numeric value", s);
478 error (0, 0, "%s: value not completely converted", s);