1 /* printf - format and print data
2 Copyright (C) 1990-1998, 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
52 #include "long-options.h"
55 /* The official name of this program (e.g., no `g' prefix). */
56 #define PROGRAM_NAME "printf"
58 #define AUTHORS "David MacKenzie"
63 unsigned long int strtoul ();
66 #define isodigit(c) ((c) >= '0' && (c) <= '7')
67 #define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
68 (c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
69 #define octtobin(c) ((c) - '0')
71 /* The value to return to the calling program. */
72 static int exit_status
;
74 /* Non-zero if the POSIXLY_CORRECT environment variable is set. */
75 static int posixly_correct
;
77 /* This message appears in N_() here rather than just in _() below because
78 the sole use would have been in a #define, and xgettext doesn't look for
79 strings in cpp directives. */
80 static char *const cfcc_msg
=
81 N_("warning: %s: character(s) following character constant have been ignored");
83 /* The name this program was run with. */
90 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
95 Usage: %s FORMAT [ARGUMENT]...\n\
98 program_name
, program_name
);
100 Print ARGUMENT(s) according to FORMAT.\n\
102 --help display this help and exit\n\
103 --version output version information and exit\n\
105 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
108 \\0NNN character with octal value NNN (0 to 3 digits)\n\
112 \\c produce no further output\n\
115 \\r carriage return\n\
116 \\t horizontal tab\n\
118 \\xNNN character with hexadecimal value NNN (1 to 3 digits)\n\
121 %%b ARGUMENT as a string with `\\' escapes interpreted\n\
123 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
124 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
126 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
132 verify (const char *s
, const char *end
)
136 error (0, errno
, "%s", s
);
142 error (0, 0, _("%s: expected a numeric value"), s
);
144 error (0, 0, _("%s: value not completely converted"), s
);
149 #define STRTOX(TYPE, FUNC_NAME, LIB_FUNC_EXPR) \
157 if (*s == '\"' || *s == '\'') \
159 val = *(unsigned char *) ++s; \
160 /* If POSIXLY_CORRECT is not set, then give a warning that there \
161 are characters following the character constant and that GNU \
162 printf is ignoring those characters. If POSIXLY_CORRECT *is* \
163 set, then don't give the warning. */ \
164 if (*++s != 0 && !posixly_correct) \
165 error (0, 0, _(cfcc_msg), s); \
170 val = LIB_FUNC_EXPR; \
176 STRTOX (unsigned long int, xstrtoul, (strtoul (s, &end, 0)))
177 STRTOX (long int, xstrtol
, (strtol (s
, &end
, 0)))
178 STRTOX (double, xstrtod
, (strtod (s
, &end
)))
180 /* Output a single-character \ escape. */
183 print_esc_char (int c
)
187 case 'a': /* Alert. */
190 case 'b': /* Backspace. */
193 case 'c': /* Cancel the rest of the output. */
196 case 'f': /* Form feed. */
199 case 'n': /* New line. */
202 case 'r': /* Carriage return. */
205 case 't': /* Horizontal tab. */
208 case 'v': /* Vertical tab. */
217 /* Print a \ escape sequence starting at ESCSTART.
218 Return the number of characters in the escape sequence
219 besides the backslash. */
222 print_esc (const char *escstart
)
224 register const char *p
= escstart
+ 1;
225 int esc_value
= 0; /* Value of \nnn escape. */
226 int esc_length
; /* Length of \nnn escape. */
228 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
231 for (esc_length
= 0, ++p
;
232 esc_length
< 3 && ISXDIGIT (*p
);
234 esc_value
= esc_value
* 16 + hextobin (*p
);
236 error (1, 0, _("missing hexadecimal number in escape"));
241 for (esc_length
= 0, ++p
;
242 esc_length
< 3 && isodigit (*p
);
244 esc_value
= esc_value
* 8 + octtobin (*p
);
247 else if (strchr ("\"\\abcfnrtv", *p
))
248 print_esc_char (*p
++);
250 error (1, 0, _("\\%c: invalid escape"), *p
);
251 return p
- escstart
- 1;
254 /* Print string STR, evaluating \ escapes. */
257 print_esc_string (const char *str
)
261 str
+= print_esc (str
);
266 /* Output a % directive. START is the start of the directive,
267 LENGTH is its length, and ARGUMENT is its argument.
268 If FIELD_WIDTH or PRECISION is non-negative, they are args for
269 '*' values in those fields. */
272 print_direc (const char *start
, size_t length
, int field_width
,
273 int precision
, const char *argument
)
275 char *p
; /* Null-terminated copy of % directive. */
277 p
= xmalloc ((unsigned) (length
+ 1));
278 strncpy (p
, start
, length
);
281 switch (p
[length
- 1])
288 printf (p
, xstrtol (argument
));
290 printf (p
, precision
, xstrtol (argument
));
295 printf (p
, field_width
, xstrtol (argument
));
297 printf (p
, field_width
, precision
, xstrtol (argument
));
308 printf (p
, xstrtoul (argument
));
310 printf (p
, precision
, xstrtoul (argument
));
315 printf (p
, field_width
, xstrtoul (argument
));
317 printf (p
, field_width
, precision
, xstrtoul (argument
));
329 printf (p
, xstrtod (argument
));
331 printf (p
, precision
, xstrtod (argument
));
336 printf (p
, field_width
, xstrtod (argument
));
338 printf (p
, field_width
, precision
, xstrtod (argument
));
343 printf (p
, *argument
);
350 printf (p
, argument
);
352 printf (p
, precision
, argument
);
357 printf (p
, field_width
, argument
);
359 printf (p
, field_width
, precision
, argument
);
367 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
368 arguments to any `%' directives.
369 Return the number of elements of ARGV used. */
372 print_formatted (const char *format
, int argc
, char **argv
)
374 int save_argc
= argc
; /* Preserve original value. */
375 const char *f
; /* Pointer into `format'. */
376 const char *direc_start
; /* Start of % directive. */
377 size_t direc_length
; /* Length of % directive. */
378 int field_width
; /* Arg to first '*', or -1 if none. */
379 int precision
; /* Arg to second '*', or -1 if none. */
381 for (f
= format
; *f
; ++f
)
388 field_width
= precision
= -1;
398 print_esc_string (*argv
);
404 if (strchr ("-+ #", *f
))
415 field_width
= xstrtoul (*argv
);
438 precision
= xstrtoul (*argv
);
452 if (*f
== 'l' || *f
== 'L' || *f
== 'h')
457 if (!strchr ("diouxXfeEgGcs", *f
))
458 error (1, 0, _("%%%c: invalid directive"), *f
);
462 print_direc (direc_start
, direc_length
, field_width
,
468 print_direc (direc_start
, direc_length
, field_width
,
481 return save_argc
- argc
;
485 main (int argc
, char **argv
)
490 program_name
= argv
[0];
491 setlocale (LC_ALL
, "");
492 bindtextdomain (PACKAGE
, LOCALEDIR
);
493 textdomain (PACKAGE
);
497 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
498 posixly_correct
= (getenv ("POSIXLY_CORRECT") != NULL
);
499 if (!posixly_correct
)
500 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
505 fprintf (stderr
, _("Usage: %s format [argument...]\n"), program_name
);
515 args_used
= print_formatted (format
, argc
, argv
);
519 while (args_used
> 0 && argc
> 0);
522 error (0, 0, _("warning: excess arguments have been ignored"));