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
__P ((char *s
));
69 static int print_esc
__P ((char *escstart
));
70 static int print_formatted
__P ((char *format
, int argc
, char **argv
));
71 static long xstrtol
__P ((char *s
));
72 static unsigned long xstrtoul
__P ((char *s
));
73 static void print_direc
__P ((char *start
, size_t length
, int field_width
, int precision
, char *argument
));
74 static void print_esc_char
__P ((int c
));
75 static void print_esc_string
__P ((char *str
));
76 static void verify
__P ((char *s
, char *end
));
78 /* The value to return to the calling program. */
79 static int exit_status
;
81 /* The name this program was run with. */
88 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
93 Usage: %s FORMAT [ARGUMENT]...\n\
96 program_name
, program_name
);
98 Print ARGUMENT(s) according to FORMAT.\n\
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\
129 main (int argc
, char **argv
)
134 program_name
= argv
[0];
135 setlocale (LC_ALL
, "");
136 bindtextdomain (PACKAGE
, LOCALEDIR
);
137 textdomain (PACKAGE
);
141 parse_long_options (argc
, argv
, "printf", version_string
, usage
);
145 fprintf (stderr
, _("Usage: %s format [argument...]\n"), program_name
);
155 args_used
= print_formatted (format
, argc
, argv
);
159 while (args_used
> 0 && argc
> 0);
164 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
165 arguments to any `%' directives.
166 Return the number of elements of ARGV used. */
169 print_formatted (char *format
, int argc
, char **argv
)
171 int save_argc
= argc
; /* Preserve original value. */
172 char *f
; /* Pointer into `format'. */
173 char *direc_start
; /* Start of % directive. */
174 size_t direc_length
; /* Length of % directive. */
175 int field_width
; /* Arg to first '*', or -1 if none. */
176 int precision
; /* Arg to second '*', or -1 if none. */
178 for (f
= format
; *f
; ++f
)
185 field_width
= precision
= -1;
195 print_esc_string (*argv
);
201 if (strchr ("-+ #", *f
))
212 field_width
= xstrtoul (*argv
);
235 precision
= xstrtoul (*argv
);
249 if (*f
== 'l' || *f
== 'L' || *f
== 'h')
254 if (!strchr ("diouxXfeEgGcs", *f
))
255 error (1, 0, _("%%%c: invalid directive"), *f
);
259 print_direc (direc_start
, direc_length
, field_width
,
265 print_direc (direc_start
, direc_length
, field_width
,
278 return save_argc
- argc
;
281 /* Print a \ escape sequence starting at ESCSTART.
282 Return the number of characters in the escape sequence
283 besides the backslash. */
286 print_esc (char *escstart
)
288 register char *p
= escstart
+ 1;
289 int esc_value
= 0; /* Value of \nnn escape. */
290 int esc_length
; /* Length of \nnn escape. */
292 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
295 for (esc_length
= 0, ++p
;
296 esc_length
< 3 && ISXDIGIT (*p
);
298 esc_value
= esc_value
* 16 + hextobin (*p
);
300 error (1, 0, _("missing hexadecimal number in escape"));
305 for (esc_length
= 0, ++p
;
306 esc_length
< 3 && isodigit (*p
);
308 esc_value
= esc_value
* 8 + octtobin (*p
);
311 else if (strchr ("\"\\abcfnrtv", *p
))
312 print_esc_char (*p
++);
314 error (1, 0, _("\\%c: invalid escape"), *p
);
315 return p
- escstart
- 1;
318 /* Output a single-character \ escape. */
321 print_esc_char (int c
)
325 case 'a': /* Alert. */
328 case 'b': /* Backspace. */
331 case 'c': /* Cancel the rest of the output. */
334 case 'f': /* Form feed. */
337 case 'n': /* New line. */
340 case 'r': /* Carriage return. */
343 case 't': /* Horizontal tab. */
346 case 'v': /* Vertical tab. */
355 /* Print string STR, evaluating \ escapes. */
358 print_esc_string (char *str
)
362 str
+= print_esc (str
);
367 /* Output a % directive. START is the start of the directive,
368 LENGTH is its length, and ARGUMENT is its argument.
369 If FIELD_WIDTH or PRECISION is non-negative, they are args for
370 '*' values in those fields. */
373 print_direc (char *start
, size_t length
, int field_width
, int precision
, char *argument
)
375 char *p
; /* Null-terminated copy of % directive. */
377 p
= xmalloc ((unsigned) (length
+ 1));
378 strncpy (p
, start
, length
);
381 switch (p
[length
- 1])
388 printf (p
, xstrtol (argument
));
390 printf (p
, precision
, xstrtol (argument
));
395 printf (p
, field_width
, xstrtol (argument
));
397 printf (p
, field_width
, precision
, xstrtol (argument
));
408 printf (p
, xstrtoul (argument
));
410 printf (p
, precision
, xstrtoul (argument
));
415 printf (p
, field_width
, xstrtoul (argument
));
417 printf (p
, field_width
, precision
, xstrtoul (argument
));
429 printf (p
, xstrtod (argument
));
431 printf (p
, precision
, xstrtod (argument
));
436 printf (p
, field_width
, xstrtod (argument
));
438 printf (p
, field_width
, precision
, xstrtod (argument
));
443 printf (p
, *argument
);
450 printf (p
, argument
);
452 printf (p
, precision
, argument
);
457 printf (p
, field_width
, argument
);
459 printf (p
, field_width
, precision
, argument
);
474 val
= strtoul (s
, &end
, 0);
486 val
= strtol (s
, &end
, 0);
498 val
= strtod (s
, &end
);
504 verify (char *s
, char *end
)
508 error (0, errno
, "%s", s
);
514 error (0, 0, _("%s: expected a numeric value"), s
);
516 error (0, 0, _("%s: value not completely converted"), s
);