xref: /freebsd/usr.bin/printf/printf.c (revision da52b4caaf187775f6b56a72c6b16e94ad728f7b)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef SHELL
31 #ifndef lint
32 static char const copyright[] =
33 "@(#) Copyright (c) 1989, 1993\n\
34 	The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36 #endif
37 
38 #ifndef lint
39 #if 0
40 static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
41 #endif
42 static const char rcsid[] =
43   "$FreeBSD$";
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <inttypes.h>
51 #include <limits.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #ifdef SHELL
58 #define main printfcmd
59 #include "bltin/bltin.h"
60 #include "memalloc.h"
61 #include "error.h"
62 #else
63 #define	warnx1(a, b, c)		warnx(a)
64 #define	warnx2(a, b, c)		warnx(a, b)
65 #define	warnx3(a, b, c)		warnx(a, b, c)
66 #endif
67 
68 #include <locale.h>
69 
70 #define PF(f, func) do { \
71 	char *b = NULL; \
72 	if (havewidth) \
73 		if (haveprec) \
74 			(void)asprintf(&b, f, fieldwidth, precision, func); \
75 		else \
76 			(void)asprintf(&b, f, fieldwidth, func); \
77 	else if (haveprec) \
78 		(void)asprintf(&b, f, precision, func); \
79 	else \
80 		(void)asprintf(&b, f, func); \
81 	if (b) { \
82 		(void)fputs(b, stdout); \
83 		free(b); \
84 	} \
85 } while (0)
86 
87 static int	 asciicode(void);
88 static char	*printf_doformat(char *, int *);
89 static int	 escape(char *, int, size_t *);
90 static int	 getchr(void);
91 static int	 getfloating(long double *, int);
92 static int	 getint(int *);
93 static int	 getnum(intmax_t *, uintmax_t *, int);
94 static const char
95 		*getstr(void);
96 static char	*mknum(char *, int);
97 static void	 usage(void);
98 
99 static char **gargv;
100 
101 int
102 main(int argc, char *argv[])
103 {
104 	size_t len;
105 	int ch, chopped, end, rval;
106 	char *format, *fmt, *start;
107 
108 #ifndef SHELL
109 	(void) setlocale(LC_NUMERIC, "");
110 #endif
111 #ifdef SHELL
112 	optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
113 #endif
114 	while ((ch = getopt(argc, argv, "")) != -1)
115 		switch (ch) {
116 		case '?':
117 		default:
118 			usage();
119 			return (1);
120 		}
121 	argc -= optind;
122 	argv += optind;
123 
124 	if (argc < 1) {
125 		usage();
126 		return (1);
127 	}
128 
129 #ifdef SHELL
130 	INTOFF;
131 #endif
132 	/*
133 	 * Basic algorithm is to scan the format string for conversion
134 	 * specifications -- once one is found, find out if the field
135 	 * width or precision is a '*'; if it is, gather up value.  Note,
136 	 * format strings are reused as necessary to use up the provided
137 	 * arguments, arguments of zero/null string are provided to use
138 	 * up the format string.
139 	 */
140 	fmt = format = *argv;
141 	chopped = escape(fmt, 1, &len);		/* backslash interpretation */
142 	rval = end = 0;
143 	gargv = ++argv;
144 	for (;;) {
145 		start = fmt;
146 		while (fmt < format + len) {
147 			if (fmt[0] == '%') {
148 				fwrite(start, 1, fmt - start, stdout);
149 				if (fmt[1] == '%') {
150 					/* %% prints a % */
151 					putchar('%');
152 					fmt += 2;
153 				} else {
154 					fmt = printf_doformat(fmt, &rval);
155 					if (fmt == NULL) {
156 #ifdef SHELL
157 						INTON;
158 #endif
159 						return (1);
160 					}
161 					end = 0;
162 				}
163 				start = fmt;
164 			} else
165 				fmt++;
166 		}
167 
168 		if (end == 1) {
169 			warnx1("missing format character", NULL, NULL);
170 #ifdef SHELL
171 			INTON;
172 #endif
173 			return (1);
174 		}
175 		fwrite(start, 1, fmt - start, stdout);
176 		if (chopped || !*gargv) {
177 #ifdef SHELL
178 			INTON;
179 #endif
180 			return (rval);
181 		}
182 		/* Restart at the beginning of the format string. */
183 		fmt = format;
184 		end = 1;
185 	}
186 	/* NOTREACHED */
187 }
188 
189 
190 static char *
191 printf_doformat(char *start, int *rval)
192 {
193 	static const char skip1[] = "#'-+ 0";
194 	static const char skip2[] = "0123456789";
195 	char *fmt;
196 	int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
197 	char convch, nextch;
198 
199 	fmt = start + 1;
200 	/* skip to field width */
201 	fmt += strspn(fmt, skip1);
202 	if (*fmt == '*') {
203 		if (getint(&fieldwidth))
204 			return (NULL);
205 		havewidth = 1;
206 		++fmt;
207 	} else {
208 		havewidth = 0;
209 
210 		/* skip to possible '.', get following precision */
211 		fmt += strspn(fmt, skip2);
212 	}
213 	if (*fmt == '.') {
214 		/* precision present? */
215 		++fmt;
216 		if (*fmt == '*') {
217 			if (getint(&precision))
218 				return (NULL);
219 			haveprec = 1;
220 			++fmt;
221 		} else {
222 			haveprec = 0;
223 
224 			/* skip to conversion char */
225 			fmt += strspn(fmt, skip2);
226 		}
227 	} else
228 		haveprec = 0;
229 	if (!*fmt) {
230 		warnx1("missing format character", NULL, NULL);
231 		return (NULL);
232 	}
233 
234 	/*
235 	 * Look for a length modifier.  POSIX doesn't have these, so
236 	 * we only support them for floating-point conversions, which
237 	 * are extensions.  This is useful because the L modifier can
238 	 * be used to gain extra range and precision, while omitting
239 	 * it is more likely to produce consistent results on different
240 	 * architectures.  This is not so important for integers
241 	 * because overflow is the only bad thing that can happen to
242 	 * them, but consider the command  printf %a 1.1
243 	 */
244 	if (*fmt == 'L') {
245 		mod_ldbl = 1;
246 		fmt++;
247 		if (!strchr("aAeEfFgG", *fmt)) {
248 			warnx2("bad modifier L for %%%c", *fmt, NULL);
249 			return (NULL);
250 		}
251 	} else {
252 		mod_ldbl = 0;
253 	}
254 
255 	convch = *fmt;
256 	nextch = *++fmt;
257 	*fmt = '\0';
258 	switch (convch) {
259 	case 'b': {
260 		size_t len;
261 		char *p;
262 		int getout;
263 
264 #ifdef SHELL
265 		p = savestr(getstr());
266 #else
267 		p = strdup(getstr());
268 #endif
269 		if (p == NULL) {
270 			warnx2("%s", strerror(ENOMEM), NULL);
271 			return (NULL);
272 		}
273 		getout = escape(p, 0, &len);
274 		*(fmt - 1) = 's';
275 		PF(start, p);
276 		*(fmt - 1) = 'b';
277 #ifdef SHELL
278 		ckfree(p);
279 #else
280 		free(p);
281 #endif
282 		if (getout)
283 			return (fmt);
284 		break;
285 	}
286 	case 'c': {
287 		char p;
288 
289 		p = getchr();
290 		PF(start, p);
291 		break;
292 	}
293 	case 's': {
294 		const char *p;
295 
296 		p = getstr();
297 		PF(start, p);
298 		break;
299 	}
300 	case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
301 		char *f;
302 		intmax_t val;
303 		uintmax_t uval;
304 		int signedconv;
305 
306 		signedconv = (convch == 'd' || convch == 'i');
307 		if ((f = mknum(start, convch)) == NULL)
308 			return (NULL);
309 		if (getnum(&val, &uval, signedconv))
310 			*rval = 1;
311 		if (signedconv)
312 			PF(f, val);
313 		else
314 			PF(f, uval);
315 		break;
316 	}
317 	case 'e': case 'E':
318 	case 'f': case 'F':
319 	case 'g': case 'G':
320 	case 'a': case 'A': {
321 		long double p;
322 
323 		if (getfloating(&p, mod_ldbl))
324 			*rval = 1;
325 		if (mod_ldbl)
326 			PF(start, p);
327 		else
328 			PF(start, (double)p);
329 		break;
330 	}
331 	default:
332 		warnx2("illegal format character %c", convch, NULL);
333 		return (NULL);
334 	}
335 	*fmt = nextch;
336 	return (fmt);
337 }
338 
339 static char *
340 mknum(char *str, int ch)
341 {
342 	static char *copy;
343 	static size_t copy_size;
344 	char *newcopy;
345 	size_t len, newlen;
346 
347 	len = strlen(str) + 2;
348 	if (len > copy_size) {
349 		newlen = ((len + 1023) >> 10) << 10;
350 #ifdef SHELL
351 		if ((newcopy = ckrealloc(copy, newlen)) == NULL)
352 #else
353 		if ((newcopy = realloc(copy, newlen)) == NULL)
354 #endif
355 		{
356 			warnx2("%s", strerror(ENOMEM), NULL);
357 			return (NULL);
358 		}
359 		copy = newcopy;
360 		copy_size = newlen;
361 	}
362 
363 	memmove(copy, str, len - 3);
364 	copy[len - 3] = 'j';
365 	copy[len - 2] = ch;
366 	copy[len - 1] = '\0';
367 	return (copy);
368 }
369 
370 static int
371 escape(char *fmt, int percent, size_t *len)
372 {
373 	char *save, *store;
374 	int value, c;
375 
376 	for (save = store = fmt; (c = *fmt); ++fmt, ++store) {
377 		if (c != '\\') {
378 			*store = c;
379 			continue;
380 		}
381 		switch (*++fmt) {
382 		case '\0':		/* EOS, user error */
383 			*store = '\\';
384 			*++store = '\0';
385 			*len = store - save;
386 			return (0);
387 		case '\\':		/* backslash */
388 		case '\'':		/* single quote */
389 			*store = *fmt;
390 			break;
391 		case 'a':		/* bell/alert */
392 			*store = '\a';
393 			break;
394 		case 'b':		/* backspace */
395 			*store = '\b';
396 			break;
397 		case 'c':
398 			*store = '\0';
399 			*len = store - save;
400 			return (1);
401 		case 'f':		/* form-feed */
402 			*store = '\f';
403 			break;
404 		case 'n':		/* newline */
405 			*store = '\n';
406 			break;
407 		case 'r':		/* carriage-return */
408 			*store = '\r';
409 			break;
410 		case 't':		/* horizontal tab */
411 			*store = '\t';
412 			break;
413 		case 'v':		/* vertical tab */
414 			*store = '\v';
415 			break;
416 					/* octal constant */
417 		case '0': case '1': case '2': case '3':
418 		case '4': case '5': case '6': case '7':
419 			c = (!percent && *fmt == '0') ? 4 : 3;
420 			for (value = 0;
421 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
422 				value <<= 3;
423 				value += *fmt - '0';
424 			}
425 			--fmt;
426 			if (percent && value == '%') {
427 				*store++ = '%';
428 				*store = '%';
429 			} else
430 				*store = value;
431 			break;
432 		default:
433 			*store = *fmt;
434 			break;
435 		}
436 	}
437 	*store = '\0';
438 	*len = store - save;
439 	return (0);
440 }
441 
442 static int
443 getchr(void)
444 {
445 	if (!*gargv)
446 		return ('\0');
447 	return ((int)**gargv++);
448 }
449 
450 static const char *
451 getstr(void)
452 {
453 	if (!*gargv)
454 		return ("");
455 	return (*gargv++);
456 }
457 
458 static int
459 getint(int *ip)
460 {
461 	intmax_t val;
462 	uintmax_t uval;
463 	int rval;
464 
465 	if (getnum(&val, &uval, 1))
466 		return (1);
467 	rval = 0;
468 	if (val < INT_MIN || val > INT_MAX) {
469 		warnx3("%s: %s", *gargv, strerror(ERANGE));
470 		rval = 1;
471 	}
472 	*ip = (int)val;
473 	return (rval);
474 }
475 
476 static int
477 getnum(intmax_t *ip, uintmax_t *uip, int signedconv)
478 {
479 	char *ep;
480 	int rval;
481 
482 	if (!*gargv) {
483 		*ip = 0;
484 		return (0);
485 	}
486 	if (**gargv == '"' || **gargv == '\'') {
487 		if (signedconv)
488 			*ip = asciicode();
489 		else
490 			*uip = asciicode();
491 		return (0);
492 	}
493 	rval = 0;
494 	errno = 0;
495 	if (signedconv)
496 		*ip = strtoimax(*gargv, &ep, 0);
497 	else
498 		*uip = strtoumax(*gargv, &ep, 0);
499 	if (ep == *gargv) {
500 		warnx2("%s: expected numeric value", *gargv, NULL);
501 		rval = 1;
502 	}
503 	else if (*ep != '\0') {
504 		warnx2("%s: not completely converted", *gargv, NULL);
505 		rval = 1;
506 	}
507 	if (errno == ERANGE) {
508 		warnx3("%s: %s", *gargv, strerror(ERANGE));
509 		rval = 1;
510 	}
511 	++gargv;
512 	return (rval);
513 }
514 
515 static int
516 getfloating(long double *dp, int mod_ldbl)
517 {
518 	char *ep;
519 	int rval;
520 
521 	if (!*gargv) {
522 		*dp = 0.0;
523 		return (0);
524 	}
525 	if (**gargv == '"' || **gargv == '\'') {
526 		*dp = asciicode();
527 		return (0);
528 	}
529 	rval = 0;
530 	errno = 0;
531 	if (mod_ldbl)
532 		*dp = strtold(*gargv, &ep);
533 	else
534 		*dp = strtod(*gargv, &ep);
535 	if (ep == *gargv) {
536 		warnx2("%s: expected numeric value", *gargv, NULL);
537 		rval = 1;
538 	} else if (*ep != '\0') {
539 		warnx2("%s: not completely converted", *gargv, NULL);
540 		rval = 1;
541 	}
542 	if (errno == ERANGE) {
543 		warnx3("%s: %s", *gargv, strerror(ERANGE));
544 		rval = 1;
545 	}
546 	++gargv;
547 	return (rval);
548 }
549 
550 static int
551 asciicode(void)
552 {
553 	int ch;
554 
555 	ch = **gargv;
556 	if (ch == '\'' || ch == '"')
557 		ch = (*gargv)[1];
558 	++gargv;
559 	return (ch);
560 }
561 
562 static void
563 usage(void)
564 {
565 	(void)fprintf(stderr, "usage: printf format [arguments ...]\n");
566 }
567