1 /* printf - format and print data
2 Copyright (C) 90, 91, 92, 93, 94, 1995 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>
53 #include "long-options.h"
59 unsigned long strtoul ();
62 #define isodigit(c) ((c) >= '0' && (c) <= '7')
63 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
64 #define octtobin(c) ((c) - '0')
68 static double xstrtod ();
69 static int print_esc ();
70 static int print_formatted ();
71 static long xstrtol ();
72 static unsigned long xstrtoul ();
73 static void print_direc ();
74 static void print_esc_char ();
75 static void print_esc_string ();
76 static void verify ();
78 /* The value to return to the calling program. */
79 static int exit_status;
81 /* The name this program was run with. */
89 fprintf (stderr, "Try `%s --help' for more information.\n",
94 Usage: %s FORMAT [ARGUMENT]...\n\
97 program_name, program_name);
100 --help display this help and exit\n\
101 --version output version information and exit\n\
103 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
106 \\0NNN character with octal value NNN (0 to 3 digits)\n\
110 \\c produce no further output\n\
113 \\r carriage return\n\
114 \\t horizontal tab\n\
116 \\xNNN character with hexadecimal value NNN (1 to 3 digits)\n\
119 %%b ARGUMENT as a string with `\\' escapes interpreted\n\
121 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
122 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
136 program_name = argv[0];
139 parse_long_options (argc, argv, "printf", version_string, usage);
143 fprintf (stderr, "Usage: %s format [argument...]\n", program_name);
153 args_used = print_formatted (format, argc, argv);
157 while (args_used > 0 && argc > 0);
162 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
163 arguments to any `%' directives.
164 Return the number of elements of ARGV used. */
167 print_formatted (format, argc, argv)
172 int save_argc = argc; /* Preserve original value. */
173 char *f; /* Pointer into `format'. */
174 char *direc_start; /* Start of % directive. */
175 size_t direc_length; /* Length of % directive. */
176 int field_width; /* Arg to first '*', or -1 if none. */
177 int precision; /* Arg to second '*', or -1 if none. */
179 for (f = format; *f; ++f)
186 field_width = precision = -1;
196 print_esc_string (*argv);
202 if (strchr ("-+ #", *f))
213 field_width = xstrtoul (*argv);
236 precision = xstrtoul (*argv);
250 if (*f == 'l' || *f == 'L' || *f == 'h')
255 if (!strchr ("diouxXfeEgGcs", *f))
256 error (1, 0, "%%%c: invalid directive", *f);
260 print_direc (direc_start, direc_length, field_width,
266 print_direc (direc_start, direc_length, field_width,
279 return save_argc - argc;
282 /* Print a \ escape sequence starting at ESCSTART.
283 Return the number of characters in the escape sequence
284 besides the backslash. */
290 register char *p = escstart + 1;
291 int esc_value = 0; /* Value of \nnn escape. */
292 int esc_length; /* Length of \nnn escape. */
294 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
297 for (esc_length = 0, ++p;
298 esc_length < 3 && ISXDIGIT (*p);
300 esc_value = esc_value * 16 + hextobin (*p);
302 error (1, 0, "missing hexadecimal number in escape");
307 for (esc_length = 0, ++p;
308 esc_length < 3 && isodigit (*p);
310 esc_value = esc_value * 8 + octtobin (*p);
313 else if (strchr ("\"\\abcfnrtv", *p))
314 print_esc_char (*p++);
316 error (1, 0, "\\%c: invalid escape", *p);
317 return p - escstart - 1;
320 /* Output a single-character \ escape. */
328 case 'a': /* Alert. */
331 case 'b': /* Backspace. */
334 case 'c': /* Cancel the rest of the output. */
337 case 'f': /* Form feed. */
340 case 'n': /* New line. */
343 case 'r': /* Carriage return. */
346 case 't': /* Horizontal tab. */
349 case 'v': /* Vertical tab. */
358 /* Print string STR, evaluating \ escapes. */
361 print_esc_string (str)
366 str += print_esc (str);
371 /* Output a % directive. START is the start of the directive,
372 LENGTH is its length, and ARGUMENT is its argument.
373 If FIELD_WIDTH or PRECISION is non-negative, they are args for
374 '*' values in those fields. */
377 print_direc (start, length, field_width, precision, argument)
384 char *p; /* Null-terminated copy of % directive. */
386 p = xmalloc ((unsigned) (length + 1));
387 strncpy (p, start, length);
390 switch (p[length - 1])
397 printf (p, xstrtol (argument));
399 printf (p, precision, xstrtol (argument));
404 printf (p, field_width, xstrtol (argument));
406 printf (p, field_width, precision, xstrtol (argument));
417 printf (p, xstrtoul (argument));
419 printf (p, precision, xstrtoul (argument));
424 printf (p, field_width, xstrtoul (argument));
426 printf (p, field_width, precision, xstrtoul (argument));
438 printf (p, xstrtod (argument));
440 printf (p, precision, xstrtod (argument));
445 printf (p, field_width, xstrtod (argument));
447 printf (p, field_width, precision, xstrtod (argument));
452 printf (p, *argument);
459 printf (p, argument);
461 printf (p, precision, argument);
466 printf (p, field_width, argument);
468 printf (p, field_width, precision, argument);
484 val = strtoul (s, &end, 0);
497 val = strtol (s, &end, 0);
510 val = strtod (s, &end);
521 error (0, errno, "%s", s);
527 error (0, 0, "%s: expected a numeric value", s);
529 error (0, 0, "%s: value not completely converted", s);