blob: 28a011dcd9236ce598068e65403dfaf5b07b1d3e [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/* printf - format and print data
3 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19/* Usage: printf format [argument...]
20
21 A front end to the printf function that lets it be used from the shell.
22
23 Backslash escapes:
24
25 \" = double quote
26 \\ = backslash
27 \a = alert (bell)
28 \b = backspace
29 \c = produce no further output
30 \f = form feed
31 \n = new line
32 \r = carriage return
33 \t = horizontal tab
34 \v = vertical tab
35 \0ooo = octal number (ooo is 0 to 3 digits)
36 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
37
38 Additional directive:
39
40 %b = print an argument string, interpreting backslash escapes
41
42 The `format' argument is re-used as many times as necessary
43 to convert all of the given arguments.
44
45 David MacKenzie <djm@gnu.ai.mit.edu> */
Erik Andersene49d5ec2000-02-08 19:58:47 +000046
Eric Andersencc8ed391999-10-05 16:24:54 +000047
48// 19990508 Busy Boxed! Dave Cinege
49
Eric Andersenb0e9a701999-10-18 22:28:26 +000050#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000051#include <unistd.h>
52#include <stdio.h>
53#include <sys/types.h>
54#include <getopt.h>
55#include <sys/stat.h>
56#include <string.h>
57#include <errno.h>
58#include <stdlib.h>
59#include <fcntl.h>
60#include <ctype.h>
61#include <libintl.h>
62
63
64#ifndef S_IFMT
65# define S_IFMT 0170000
66#endif
67#if !defined(S_ISBLK) && defined(S_IFBLK)
68# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
69#endif
70#if !defined(S_ISCHR) && defined(S_IFCHR)
71# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
72#endif
73#if !defined(S_ISDIR) && defined(S_IFDIR)
74# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
75#endif
76#if !defined(S_ISREG) && defined(S_IFREG)
77# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
78#endif
79#if !defined(S_ISFIFO) && defined(S_IFIFO)
80# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
81#endif
82#if !defined(S_ISLNK) && defined(S_IFLNK)
83# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
84#endif
85#if !defined(S_ISSOCK) && defined(S_IFSOCK)
86# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
87#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000088#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
Eric Andersencc8ed391999-10-05 16:24:54 +000089# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
90# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
91#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000092#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
Eric Andersencc8ed391999-10-05 16:24:54 +000093# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
94#endif
95
96#define IN_CTYPE_DOMAIN(c) 1
97
98#ifdef isblank
99# define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
100#else
101# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
102#endif
103#ifdef isgraph
104# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
105#else
106# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
107#endif
108
109#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
110#define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
111#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
112#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
113#define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
114#define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
115#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
116#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
117#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
118#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
119#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
120
121#define isodigit(c) ((c) >= '0' && (c) <= '7')
122#define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
123#define octtobin(c) ((c) - '0')
124
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125static double xstrtod __P((char *s));
126static int print_esc __P((char *escstart));
127static int print_formatted __P((char *format, int argc, char **argv));
128static long xstrtol __P((char *s));
129static unsigned long xstrtoul __P((char *s));
130static void print_direc
131__P(
132
133 (char *start, size_t length, int field_width, int precision,
134 char *argument));
135static void print_esc_char __P((int c));
136static void print_esc_string __P((char *str));
137static void verify __P((char *s, char *end));
Eric Andersencc8ed391999-10-05 16:24:54 +0000138
139/* The value to return to the calling program. */
140static int exit_status;
141
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000142static const char printf_usage[] = "printf FORMAT [ARGUMENT...]\n"
143#ifndef BB_FEATURE_TRIVIAL_HELP
144 "\nFormats and prints ARGUMENT(s) according to FORMAT,\n"
145 "Where FORMAT controls the output exactly as in C printf.\n"
146#endif
147 ;
Eric Andersencc8ed391999-10-05 16:24:54 +0000148
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149int printf_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000150{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 char *format;
152 int args_used;
Eric Andersencc8ed391999-10-05 16:24:54 +0000153
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 exit_status = 0;
155 if (argc <= 1 || **(argv + 1) == '-') {
156 usage(printf_usage);
157 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000158
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 format = argv[1];
160 argc -= 2;
161 argv += 2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 do {
164 args_used = print_formatted(format, argc, argv);
165 argc -= args_used;
166 argv += args_used;
167 }
168 while (args_used > 0 && argc > 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
170/*
171 if (argc > 0)
172 fprintf(stderr, "excess args ignored");
173*/
174
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 exit(exit_status);
Eric Andersencc8ed391999-10-05 16:24:54 +0000176}
177
178/* Print the text in FORMAT, using ARGV (with ARGC elements) for
179 arguments to any `%' directives.
180 Return the number of elements of ARGV used. */
181
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182static int print_formatted(char *format, int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000183{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 int save_argc = argc; /* Preserve original value. */
185 char *f; /* Pointer into `format'. */
186 char *direc_start; /* Start of % directive. */
187 size_t direc_length; /* Length of % directive. */
188 int field_width; /* Arg to first '*', or -1 if none. */
189 int precision; /* Arg to second '*', or -1 if none. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000190
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 for (f = format; *f; ++f) {
192 switch (*f) {
193 case '%':
194 direc_start = f++;
195 direc_length = 1;
196 field_width = precision = -1;
197 if (*f == '%') {
198 putchar('%');
199 break;
200 }
201 if (*f == 'b') {
202 if (argc > 0) {
203 print_esc_string(*argv);
204 ++argv;
205 --argc;
206 }
207 break;
208 }
209 if (strchr("-+ #", *f)) {
210 ++f;
211 ++direc_length;
212 }
213 if (*f == '*') {
214 ++f;
215 ++direc_length;
216 if (argc > 0) {
217 field_width = xstrtoul(*argv);
218 ++argv;
219 --argc;
220 } else
221 field_width = 0;
222 } else
223 while (ISDIGIT(*f)) {
224 ++f;
225 ++direc_length;
226 }
227 if (*f == '.') {
228 ++f;
229 ++direc_length;
230 if (*f == '*') {
231 ++f;
232 ++direc_length;
233 if (argc > 0) {
234 precision = xstrtoul(*argv);
235 ++argv;
236 --argc;
237 } else
238 precision = 0;
239 } else
240 while (ISDIGIT(*f)) {
241 ++f;
242 ++direc_length;
243 }
244 }
245 if (*f == 'l' || *f == 'L' || *f == 'h') {
246 ++f;
247 ++direc_length;
248 }
249 /*
250 if (!strchr ("diouxXfeEgGcs", *f))
251 fprintf(stderr, "%%%c: invalid directive", *f);
252 */
253 ++direc_length;
254 if (argc > 0) {
255 print_direc(direc_start, direc_length, field_width,
256 precision, *argv);
257 ++argv;
258 --argc;
259 } else
260 print_direc(direc_start, direc_length, field_width,
261 precision, "");
262 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000263
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 case '\\':
265 f += print_esc(f);
266 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 default:
269 putchar(*f);
270 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000271 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000272
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 return save_argc - argc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000274}
275
276/* Print a \ escape sequence starting at ESCSTART.
277 Return the number of characters in the escape sequence
278 besides the backslash. */
279
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280static int print_esc(char *escstart)
Eric Andersencc8ed391999-10-05 16:24:54 +0000281{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 register char *p = escstart + 1;
283 int esc_value = 0; /* Value of \nnn escape. */
284 int esc_length; /* Length of \nnn escape. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000285
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
287 if (*p == 'x') {
288 for (esc_length = 0, ++p;
289 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
290 esc_value = esc_value * 16 + hextobin(*p);
Eric Andersencc8ed391999-10-05 16:24:54 +0000291/* if (esc_length == 0)
292 fprintf(stderr, "missing hex in esc");
293*/
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 putchar(esc_value);
295 } else if (*p == '0') {
296 for (esc_length = 0, ++p;
297 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
298 esc_value = esc_value * 8 + octtobin(*p);
299 putchar(esc_value);
300 } else if (strchr("\"\\abcfnrtv", *p))
301 print_esc_char(*p++);
Eric Andersencc8ed391999-10-05 16:24:54 +0000302/* else
303 fprintf(stderr, "\\%c: invalid esc", *p);
304*/
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 return p - escstart - 1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000306}
307
308/* Output a single-character \ escape. */
309
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310static void print_esc_char(int c)
Eric Andersencc8ed391999-10-05 16:24:54 +0000311{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 switch (c) {
313 case 'a': /* Alert. */
314 putchar(7);
315 break;
316 case 'b': /* Backspace. */
317 putchar(8);
318 break;
319 case 'c': /* Cancel the rest of the output. */
320 exit(0);
321 break;
322 case 'f': /* Form feed. */
323 putchar(12);
324 break;
325 case 'n': /* New line. */
326 putchar(10);
327 break;
328 case 'r': /* Carriage return. */
329 putchar(13);
330 break;
331 case 't': /* Horizontal tab. */
332 putchar(9);
333 break;
334 case 'v': /* Vertical tab. */
335 putchar(11);
336 break;
337 default:
338 putchar(c);
339 break;
340 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000341}
342
343/* Print string STR, evaluating \ escapes. */
344
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345static void print_esc_string(char *str)
Eric Andersencc8ed391999-10-05 16:24:54 +0000346{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 for (; *str; str++)
348 if (*str == '\\')
349 str += print_esc(str);
350 else
351 putchar(*str);
Eric Andersencc8ed391999-10-05 16:24:54 +0000352}
353
354static void
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355print_direc(char *start, size_t length, int field_width, int precision,
356 char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000357{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358 char *p; /* Null-terminated copy of % directive. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000359
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 p = xmalloc((unsigned) (length + 1));
361 strncpy(p, start, length);
362 p[length] = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000363
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 switch (p[length - 1]) {
365 case 'd':
366 case 'i':
367 if (field_width < 0) {
368 if (precision < 0)
369 printf(p, xstrtol(argument));
370 else
371 printf(p, precision, xstrtol(argument));
372 } else {
373 if (precision < 0)
374 printf(p, field_width, xstrtol(argument));
375 else
376 printf(p, field_width, precision, xstrtol(argument));
377 }
378 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000379
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 case 'o':
381 case 'u':
382 case 'x':
383 case 'X':
384 if (field_width < 0) {
385 if (precision < 0)
386 printf(p, xstrtoul(argument));
387 else
388 printf(p, precision, xstrtoul(argument));
389 } else {
390 if (precision < 0)
391 printf(p, field_width, xstrtoul(argument));
392 else
393 printf(p, field_width, precision, xstrtoul(argument));
394 }
395 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000396
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397 case 'f':
398 case 'e':
399 case 'E':
400 case 'g':
401 case 'G':
402 if (field_width < 0) {
403 if (precision < 0)
404 printf(p, xstrtod(argument));
405 else
406 printf(p, precision, xstrtod(argument));
407 } else {
408 if (precision < 0)
409 printf(p, field_width, xstrtod(argument));
410 else
411 printf(p, field_width, precision, xstrtod(argument));
412 }
413 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000414
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415 case 'c':
416 printf(p, *argument);
417 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000418
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 case 's':
420 if (field_width < 0) {
421 if (precision < 0)
422 printf(p, argument);
423 else
424 printf(p, precision, argument);
425 } else {
426 if (precision < 0)
427 printf(p, field_width, argument);
428 else
429 printf(p, field_width, precision, argument);
430 }
431 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000432 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000433
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 free(p);
Eric Andersencc8ed391999-10-05 16:24:54 +0000435}
436
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437static unsigned long xstrtoul(char *s)
Eric Andersencc8ed391999-10-05 16:24:54 +0000438{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439 char *end;
440 unsigned long val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000441
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 errno = 0;
443 val = strtoul(s, &end, 0);
444 verify(s, end);
445 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000446}
447
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448static long xstrtol(char *s)
Eric Andersencc8ed391999-10-05 16:24:54 +0000449{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 char *end;
451 long val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 errno = 0;
454 val = strtol(s, &end, 0);
455 verify(s, end);
456 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000457}
458
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459static double xstrtod(char *s)
Eric Andersencc8ed391999-10-05 16:24:54 +0000460{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 char *end;
462 double val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000463
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 errno = 0;
465 val = strtod(s, &end);
466 verify(s, end);
467 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000468}
469
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470static void verify(char *s, char *end)
Eric Andersencc8ed391999-10-05 16:24:54 +0000471{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 if (errno) {
473 fprintf(stderr, "%s", s);
474 exit_status = 1;
475 } else if (*end) {
476 /*
477 if (s == end)
478 fprintf(stderr, "%s: expected numeric", s);
479 else
480 fprintf(stderr, "%s: not completely converted", s);
481 */
482 exit_status = 1;
483 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000484}