1 /* printf - format and print data
2 Copyright (C) 1990-2013 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Usage: printf format [argument...]
19 A front end to the printf function that lets it be used from the shell.
27 \c = produce no further output
34 \ooo = octal number (ooo is 1 to 3 digits)
35 \xhh = hexadecimal number (hhh is 1 to 2 digits)
36 \uhhhh = 16-bit Unicode character (hhhh is 4 digits)
37 \Uhhhhhhhh = 32-bit Unicode character (hhhhhhhh is 8 digits)
41 %b = print an argument string, interpreting backslash escapes,
42 except that octal escapes are of the form \0 or \0ooo.
44 The 'format' argument is re-used as many times as necessary
45 to convert all of the given arguments.
47 David MacKenzie <djm@gnu.ai.mit.edu> */
51 #include <sys/types.h>
57 #include "unicodeio.h"
60 /* The official name of this program (e.g., no 'g' prefix). */
61 #define PROGRAM_NAME "printf"
63 #define AUTHORS proper_name ("David MacKenzie")
65 #define isodigit(c) ((c) >= '0' && (c) <= '7')
66 #define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
67 (c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
68 #define octtobin(c) ((c) - '0')
70 /* The value to return to the calling program. */
71 static int exit_status
;
73 /* True if the POSIXLY_CORRECT environment variable is set. */
74 static bool posixly_correct
;
76 /* This message appears in N_() here rather than just in _() below because
77 the sole use would have been in a #define. */
78 static char const *const cfcc_msg
=
79 N_("warning: %s: character(s) following character constant have been ignored");
84 if (status
!= EXIT_SUCCESS
)
89 Usage: %s FORMAT [ARGUMENT]...\n\
92 program_name
, program_name
);
94 Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:\n\
97 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
98 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
101 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
109 \\c produce no further output\n\
113 \\r carriage return\n\
114 \\t horizontal tab\n\
118 \\NNN byte with octal value NNN (1 to 3 digits)\n\
119 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
120 \\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)\n\
121 \\UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits)\n\
125 %b ARGUMENT as a string with '\\' escapes interpreted,\n\
126 except that octal escapes are of the form \\0 or \\0NNN\n\
128 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
129 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
131 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
132 emit_ancillary_info ();
138 verify_numeric (const char *s
, const char *end
)
142 error (0, errno
, "%s", s
);
143 exit_status
= EXIT_FAILURE
;
148 error (0, 0, _("%s: expected a numeric value"), s
);
150 error (0, 0, _("%s: value not completely converted"), s
);
151 exit_status
= EXIT_FAILURE
;
155 #define STRTOX(TYPE, FUNC_NAME, LIB_FUNC_EXPR) \
157 FUNC_NAME (char const *s) \
162 if ((*s == '\"' || *s == '\'') && *(s + 1)) \
164 unsigned char ch = *++s; \
166 /* If POSIXLY_CORRECT is not set, then give a warning that there \
167 are characters following the character constant and that GNU \
168 printf is ignoring those characters. If POSIXLY_CORRECT *is* \
169 set, then don't give the warning. */ \
170 if (*++s != 0 && !posixly_correct) \
171 error (0, 0, _(cfcc_msg), s); \
176 val = (LIB_FUNC_EXPR); \
177 verify_numeric (s, end); \
182 STRTOX (intmax_t, vstrtoimax, strtoimax (s, &end, 0))
183 STRTOX (uintmax_t, vstrtoumax
, strtoumax (s
, &end
, 0))
184 STRTOX (long double, vstrtold
, c_strtold (s
, &end
))
186 /* Output a single-character \ escape. */
189 print_esc_char (char c
)
193 case 'a': /* Alert. */
196 case 'b': /* Backspace. */
199 case 'c': /* Cancel the rest of the output. */
202 case 'e': /* Escape. */
205 case 'f': /* Form feed. */
208 case 'n': /* New line. */
211 case 'r': /* Carriage return. */
214 case 't': /* Horizontal tab. */
217 case 'v': /* Vertical tab. */
226 /* Print a \ escape sequence starting at ESCSTART.
227 Return the number of characters in the escape sequence
228 besides the backslash.
229 If OCTAL_0 is nonzero, octal escapes are of the form \0ooo, where o
230 is an octal digit; otherwise they are of the form \ooo. */
233 print_esc (const char *escstart
, bool octal_0
)
235 const char *p
= escstart
+ 1;
236 int esc_value
= 0; /* Value of \nnn escape. */
237 int esc_length
; /* Length of \nnn escape. */
241 /* A hexadecimal \xhh escape sequence must have 1 or 2 hex. digits. */
242 for (esc_length
= 0, ++p
;
243 esc_length
< 2 && isxdigit (to_uchar (*p
));
245 esc_value
= esc_value
* 16 + hextobin (*p
);
247 error (EXIT_FAILURE
, 0, _("missing hexadecimal number in escape"));
250 else if (isodigit (*p
))
252 /* Parse \0ooo (if octal_0 && *p == '0') or \ooo (otherwise).
253 Allow \ooo if octal_0 && *p != '0'; this is an undocumented
254 extension to POSIX that is compatible with Bash 2.05b. */
255 for (esc_length
= 0, p
+= octal_0
&& *p
== '0';
256 esc_length
< 3 && isodigit (*p
);
258 esc_value
= esc_value
* 8 + octtobin (*p
);
261 else if (*p
&& strchr ("\"\\abcefnrtv", *p
))
262 print_esc_char (*p
++);
263 else if (*p
== 'u' || *p
== 'U')
266 unsigned int uni_value
;
269 for (esc_length
= (esc_char
== 'u' ? 4 : 8), ++p
;
273 if (! isxdigit (to_uchar (*p
)))
274 error (EXIT_FAILURE
, 0, _("missing hexadecimal number in escape"));
275 uni_value
= uni_value
* 16 + hextobin (*p
);
278 /* A universal character name shall not specify a character short
279 identifier in the range 00000000 through 00000020, 0000007F through
280 0000009F, or 0000D800 through 0000DFFF inclusive. A universal
281 character name shall not designate a character in the required
283 if ((uni_value
<= 0x9f
284 && uni_value
!= 0x24 && uni_value
!= 0x40 && uni_value
!= 0x60)
285 || (uni_value
>= 0xd800 && uni_value
<= 0xdfff))
286 error (EXIT_FAILURE
, 0, _("invalid universal character name \\%c%0*x"),
287 esc_char
, (esc_char
== 'u' ? 4 : 8), uni_value
);
289 print_unicode_char (stdout
, uni_value
, 0);
300 return p
- escstart
- 1;
303 /* Print string STR, evaluating \ escapes. */
306 print_esc_string (const char *str
)
310 str
+= print_esc (str
, true);
315 /* Evaluate a printf conversion specification. START is the start of
316 the directive, LENGTH is its length, and CONVERSION specifies the
317 type of conversion. LENGTH does not include any length modifier or
318 the conversion specifier itself. FIELD_WIDTH and PRECISION are the
319 field width and precision for '*' values, if HAVE_FIELD_WIDTH and
320 HAVE_PRECISION are true, respectively. ARGUMENT is the argument to
324 print_direc (const char *start
, size_t length
, char conversion
,
325 bool have_field_width
, int field_width
,
326 bool have_precision
, int precision
,
327 char const *argument
)
329 char *p
; /* Null-terminated copy of % directive. */
331 /* Create a null-terminated copy of the % directive, with an
332 intmax_t-wide length modifier substituted for any existing
333 integer length modifier. */
336 char const *length_modifier
;
337 size_t length_modifier_len
;
341 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
342 length_modifier
= PRIdMAX
;
343 length_modifier_len
= sizeof PRIdMAX
- 2;
346 case 'a': case 'e': case 'f': case 'g':
347 case 'A': case 'E': case 'F': case 'G':
348 length_modifier
= "L";
349 length_modifier_len
= 1;
353 length_modifier
= start
; /* Any valid pointer will do. */
354 length_modifier_len
= 0;
358 p
= xmalloc (length
+ length_modifier_len
+ 2);
359 q
= mempcpy (p
, start
, length
);
360 q
= mempcpy (q
, length_modifier
, length_modifier_len
);
370 intmax_t arg
= vstrtoimax (argument
);
371 if (!have_field_width
)
376 xprintf (p
, precision
, arg
);
381 xprintf (p
, field_width
, arg
);
383 xprintf (p
, field_width
, precision
, arg
);
393 uintmax_t arg
= vstrtoumax (argument
);
394 if (!have_field_width
)
399 xprintf (p
, precision
, arg
);
404 xprintf (p
, field_width
, arg
);
406 xprintf (p
, field_width
, precision
, arg
);
420 long double arg
= vstrtold (argument
);
421 if (!have_field_width
)
426 xprintf (p
, precision
, arg
);
431 xprintf (p
, field_width
, arg
);
433 xprintf (p
, field_width
, precision
, arg
);
439 if (!have_field_width
)
440 xprintf (p
, *argument
);
442 xprintf (p
, field_width
, *argument
);
446 if (!have_field_width
)
449 xprintf (p
, argument
);
451 xprintf (p
, precision
, argument
);
456 xprintf (p
, field_width
, argument
);
458 xprintf (p
, field_width
, precision
, argument
);
466 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
467 arguments to any '%' directives.
468 Return the number of elements of ARGV used. */
471 print_formatted (const char *format
, int argc
, char **argv
)
473 int save_argc
= argc
; /* Preserve original value. */
474 const char *f
; /* Pointer into 'format'. */
475 const char *direc_start
; /* Start of % directive. */
476 size_t direc_length
; /* Length of % directive. */
477 bool have_field_width
; /* True if FIELD_WIDTH is valid. */
478 int field_width
= 0; /* Arg to first '*'. */
479 bool have_precision
; /* True if PRECISION is valid. */
480 int precision
= 0; /* Arg to second '*'. */
481 char ok
[UCHAR_MAX
+ 1]; /* ok['x'] is true if %x is allowed. */
483 for (f
= format
; *f
; ++f
)
490 have_field_width
= have_precision
= false;
498 /* FIXME: Field width and precision are not supported
499 for %b, even though POSIX requires it. */
502 print_esc_string (*argv
);
509 memset (ok
, 0, sizeof ok
);
510 ok
['a'] = ok
['A'] = ok
['c'] = ok
['d'] = ok
['e'] = ok
['E'] =
511 ok
['f'] = ok
['F'] = ok
['g'] = ok
['G'] = ok
['i'] = ok
['o'] =
512 ok
['s'] = ok
['u'] = ok
['x'] = ok
['X'] = 1;
514 for (;; f
++, direc_length
++)
517 #if (__GLIBC__ == 2 && 2 <= __GLIBC_MINOR__) || 3 <= __GLIBC__
521 ok
['a'] = ok
['A'] = ok
['c'] = ok
['e'] = ok
['E'] =
522 ok
['o'] = ok
['s'] = ok
['x'] = ok
['X'] = 0;
524 case '-': case '+': case ' ':
527 ok
['c'] = ok
['d'] = ok
['i'] = ok
['s'] = ok
['u'] = 0;
530 ok
['c'] = ok
['s'] = 0;
533 goto no_more_flag_characters
;
535 no_more_flag_characters
:
543 intmax_t width
= vstrtoimax (*argv
);
544 if (INT_MIN
<= width
&& width
<= INT_MAX
)
547 error (EXIT_FAILURE
, 0, _("invalid field width: %s"),
554 have_field_width
= true;
573 intmax_t prec
= vstrtoimax (*argv
);
576 /* A negative precision is taken as if the
577 precision were omitted, so -1 is safe
578 here even if prec < INT_MIN. */
581 else if (INT_MAX
< prec
)
582 error (EXIT_FAILURE
, 0, _("invalid precision: %s"),
591 have_precision
= true;
601 while (*f
== 'l' || *f
== 'L' || *f
== 'h'
602 || *f
== 'j' || *f
== 't' || *f
== 'z')
606 unsigned char conversion
= *f
;
607 if (! ok
[conversion
])
608 error (EXIT_FAILURE
, 0,
609 _("%.*s: invalid conversion specification"),
610 (int) (f
+ 1 - direc_start
), direc_start
);
613 print_direc (direc_start
, direc_length
, *f
,
614 have_field_width
, field_width
,
615 have_precision
, precision
,
616 (argc
<= 0 ? "" : (argc
--, *argv
++)));
620 f
+= print_esc (f
, false);
628 return save_argc
- argc
;
632 main (int argc
, char **argv
)
637 initialize_main (&argc
, &argv
);
638 set_program_name (argv
[0]);
639 setlocale (LC_ALL
, "");
640 bindtextdomain (PACKAGE
, LOCALEDIR
);
641 textdomain (PACKAGE
);
643 atexit (close_stdout
);
645 exit_status
= EXIT_SUCCESS
;
647 posixly_correct
= (getenv ("POSIXLY_CORRECT") != NULL
);
649 /* We directly parse options, rather than use parse_long_options, in
650 order to avoid accepting abbreviations. */
653 if (STREQ (argv
[1], "--help"))
654 usage (EXIT_SUCCESS
);
656 if (STREQ (argv
[1], "--version"))
658 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
664 /* The above handles --help and --version.
665 Since there is no other invocation of getopt, handle '--' here. */
666 if (1 < argc
&& STREQ (argv
[1], "--"))
674 error (0, 0, _("missing operand"));
675 usage (EXIT_FAILURE
);
684 args_used
= print_formatted (format
, argc
, argv
);
688 while (args_used
> 0 && argc
> 0);
692 _("warning: ignoring excess arguments, starting with %s"),