xref: /freebsd-13.1/usr.bin/printf/printf.c (revision b0620803)
1c9aca921SXin LI /*-
2*b0620803SPedro F. Giffuni  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
39b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  *
69b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
79b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
89b50d902SRodney W. Grimes  * are met:
99b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
109b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
119b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
139b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
149b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
159b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
169b50d902SRodney W. Grimes  *    without specific prior written permission.
179b50d902SRodney W. Grimes  *
189b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
229b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289b50d902SRodney W. Grimes  * SUCH DAMAGE.
299b50d902SRodney W. Grimes  */
3077fae5c1SJilles Tjoelker /*
3177fae5c1SJilles Tjoelker  * Important: This file is used both as a standalone program /usr/bin/printf
3277fae5c1SJilles Tjoelker  * and as a builtin for /bin/sh (#define SHELL).
3377fae5c1SJilles Tjoelker  */
349b50d902SRodney W. Grimes 
351866e8abSJilles Tjoelker #ifndef SHELL
369b50d902SRodney W. Grimes #ifndef lint
373ec30b79SSteve Price static char const copyright[] =
389b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
399b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes #endif
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
44c9e05349SWarner Losh #if 0
453ec30b79SSteve Price static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
46c9e05349SWarner Losh #endif
471ea7321bSMartin Cracauer static const char rcsid[] =
481ea7321bSMartin Cracauer   "$FreeBSD$";
499b50d902SRodney W. Grimes #endif /* not lint */
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <sys/types.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <err.h>
549b50d902SRodney W. Grimes #include <errno.h>
55d41d23e1SStefan Farfeleder #include <inttypes.h>
569b50d902SRodney W. Grimes #include <limits.h>
57c9aca921SXin LI #include <locale.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
619d19feb5SSteve Price #include <unistd.h>
6298102dabSJilles Tjoelker #include <wchar.h>
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes #ifdef SHELL
659b50d902SRodney W. Grimes #define	main printfcmd
66d9f93710SJoerg Wunsch #include "bltin/bltin.h"
679897c45fSJilles Tjoelker #include "error.h"
687cbda738SJilles Tjoelker #include "options.h"
699b50d902SRodney W. Grimes #endif
709b50d902SRodney W. Grimes 
71bacab7d6STim J. Robbins #define	PF(f, func) do {						\
72d72f654cSPeter Wemm 	char *b = NULL;							\
73*b0620803SPedro F. Giffuni 	int dollar = 0;							\
74*b0620803SPedro F. Giffuni 	if (*f == '$') 	{						\
75*b0620803SPedro F. Giffuni 		dollar++;						\
76*b0620803SPedro F. Giffuni 		*f = '%';						\
77*b0620803SPedro F. Giffuni 	} 								\
7898dd6386STim J. Robbins 	if (havewidth)							\
7998dd6386STim J. Robbins 		if (haveprec)						\
80d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, precision, func); \
819b50d902SRodney W. Grimes 		else							\
82d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, func);	\
8398dd6386STim J. Robbins 	else if (haveprec)						\
84d72f654cSPeter Wemm 		(void)asprintf(&b, f, precision, func);			\
859b50d902SRodney W. Grimes 	else								\
86d72f654cSPeter Wemm 		(void)asprintf(&b, f, func);				\
87d72f654cSPeter Wemm 	if (b) {							\
88d72f654cSPeter Wemm 		(void)fputs(b, stdout);					\
89d72f654cSPeter Wemm 		free(b);						\
90d72f654cSPeter Wemm 	}								\
91*b0620803SPedro F. Giffuni 	if (dollar)							\
92*b0620803SPedro F. Giffuni 		*f = '$';						\
93bacab7d6STim J. Robbins } while (0)
949b50d902SRodney W. Grimes 
95d3cb5dedSWarner Losh static int	 asciicode(void);
969897c45fSJilles Tjoelker static char	*printf_doformat(char *, int *);
973ec96cafSStefan Farfeleder static int	 escape(char *, int, size_t *);
98d3cb5dedSWarner Losh static int	 getchr(void);
99fd757c50SDavid Schultz static int	 getfloating(long double *, int);
100d3cb5dedSWarner Losh static int	 getint(int *);
101d41d23e1SStefan Farfeleder static int	 getnum(intmax_t *, uintmax_t *, int);
102bacab7d6STim J. Robbins static const char
103bacab7d6STim J. Robbins 		*getstr(void);
1049180853bSXin LI static char	*mknum(char *, char);
105d3cb5dedSWarner Losh static void	 usage(void);
1069b50d902SRodney W. Grimes 
107*b0620803SPedro F. Giffuni static int  myargc;
108*b0620803SPedro F. Giffuni static char **myargv;
1099b50d902SRodney W. Grimes static char **gargv;
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes int
112f4ac32deSDavid Malone main(int argc, char *argv[])
1139b50d902SRodney W. Grimes {
1143ec96cafSStefan Farfeleder 	size_t len;
1157cbda738SJilles Tjoelker 	int chopped, end, rval;
116fbd08684SStefan Farfeleder 	char *format, *fmt, *start;
1171866e8abSJilles Tjoelker #ifndef SHELL
1187cbda738SJilles Tjoelker 	int ch;
1197cbda738SJilles Tjoelker 
12069be0c5eSXin LI 	(void) setlocale(LC_ALL, "");
121dc7d8c99SAndrey A. Chernov #endif
1227cbda738SJilles Tjoelker 
1239897c45fSJilles Tjoelker #ifdef SHELL
1247cbda738SJilles Tjoelker 	nextopt("");
1257cbda738SJilles Tjoelker 	argc -= argptr - argv;
1267cbda738SJilles Tjoelker 	argv = argptr;
1277cbda738SJilles Tjoelker #else
128de46de4dSXin LI 	while ((ch = getopt(argc, argv, "")) != -1)
129de46de4dSXin LI 		switch (ch) {
130de46de4dSXin LI 		case '?':
131de46de4dSXin LI 		default:
132de46de4dSXin LI 			usage();
133de46de4dSXin LI 			return (1);
1349b50d902SRodney W. Grimes 		}
135de46de4dSXin LI 	argc -= optind;
136de46de4dSXin LI 	argv += optind;
1377cbda738SJilles Tjoelker #endif
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 	if (argc < 1) {
1409b50d902SRodney W. Grimes 		usage();
1415eccc000SXin LI 		return (1);
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 
1449897c45fSJilles Tjoelker #ifdef SHELL
1459897c45fSJilles Tjoelker 	INTOFF;
1469897c45fSJilles Tjoelker #endif
1479b50d902SRodney W. Grimes 	/*
1489b50d902SRodney W. Grimes 	 * Basic algorithm is to scan the format string for conversion
1499b50d902SRodney W. Grimes 	 * specifications -- once one is found, find out if the field
1509b50d902SRodney W. Grimes 	 * width or precision is a '*'; if it is, gather up value.  Note,
1519b50d902SRodney W. Grimes 	 * format strings are reused as necessary to use up the provided
1529b50d902SRodney W. Grimes 	 * arguments, arguments of zero/null string are provided to use
1539b50d902SRodney W. Grimes 	 * up the format string.
1549b50d902SRodney W. Grimes 	 */
1553ec96cafSStefan Farfeleder 	fmt = format = *argv;
1563ec96cafSStefan Farfeleder 	chopped = escape(fmt, 1, &len);		/* backslash interpretation */
157fbd08684SStefan Farfeleder 	rval = end = 0;
1589b50d902SRodney W. Grimes 	gargv = ++argv;
159*b0620803SPedro F. Giffuni 
1609b50d902SRodney W. Grimes 	for (;;) {
161*b0620803SPedro F. Giffuni 		char **maxargv = gargv;
162*b0620803SPedro F. Giffuni 
163*b0620803SPedro F. Giffuni 		myargv = gargv;
164*b0620803SPedro F. Giffuni 		for (myargc = 0; gargv[myargc]; myargc++)
165*b0620803SPedro F. Giffuni 			/* nop */;
166fbd08684SStefan Farfeleder 		start = fmt;
1673ec96cafSStefan Farfeleder 		while (fmt < format + len) {
168fbd08684SStefan Farfeleder 			if (fmt[0] == '%') {
169fbd08684SStefan Farfeleder 				fwrite(start, 1, fmt - start, stdout);
170fbd08684SStefan Farfeleder 				if (fmt[1] == '%') {
1719b50d902SRodney W. Grimes 					/* %% prints a % */
172fbd08684SStefan Farfeleder 					putchar('%');
173fbd08684SStefan Farfeleder 					fmt += 2;
174fbd08684SStefan Farfeleder 				} else {
1759897c45fSJilles Tjoelker 					fmt = printf_doformat(fmt, &rval);
1769897c45fSJilles Tjoelker 					if (fmt == NULL) {
1779897c45fSJilles Tjoelker #ifdef SHELL
1789897c45fSJilles Tjoelker 						INTON;
1799897c45fSJilles Tjoelker #endif
180fbd08684SStefan Farfeleder 						return (1);
1819897c45fSJilles Tjoelker 					}
182fbd08684SStefan Farfeleder 					end = 0;
1839b50d902SRodney W. Grimes 				}
184fbd08684SStefan Farfeleder 				start = fmt;
185fbd08684SStefan Farfeleder 			} else
186fbd08684SStefan Farfeleder 				fmt++;
187*b0620803SPedro F. Giffuni 			if (gargv > maxargv)
188*b0620803SPedro F. Giffuni 				maxargv = gargv;
1899b50d902SRodney W. Grimes 		}
190*b0620803SPedro F. Giffuni 		gargv = maxargv;
1919b50d902SRodney W. Grimes 
192fbd08684SStefan Farfeleder 		if (end == 1) {
1936a6760dbSJilles Tjoelker 			warnx("missing format character");
1949897c45fSJilles Tjoelker #ifdef SHELL
1959897c45fSJilles Tjoelker 			INTON;
1969897c45fSJilles Tjoelker #endif
197fbd08684SStefan Farfeleder 			return (1);
198fbd08684SStefan Farfeleder 		}
199fbd08684SStefan Farfeleder 		fwrite(start, 1, fmt - start, stdout);
2009897c45fSJilles Tjoelker 		if (chopped || !*gargv) {
2019897c45fSJilles Tjoelker #ifdef SHELL
2029897c45fSJilles Tjoelker 			INTON;
2039897c45fSJilles Tjoelker #endif
204fbd08684SStefan Farfeleder 			return (rval);
2059897c45fSJilles Tjoelker 		}
206fbd08684SStefan Farfeleder 		/* Restart at the beginning of the format string. */
207fbd08684SStefan Farfeleder 		fmt = format;
208fbd08684SStefan Farfeleder 		end = 1;
209fbd08684SStefan Farfeleder 	}
210fbd08684SStefan Farfeleder 	/* NOTREACHED */
211fbd08684SStefan Farfeleder }
212fbd08684SStefan Farfeleder 
213fbd08684SStefan Farfeleder 
214fbd08684SStefan Farfeleder static char *
2159897c45fSJilles Tjoelker printf_doformat(char *start, int *rval)
216fbd08684SStefan Farfeleder {
217fbd08684SStefan Farfeleder 	static const char skip1[] = "#'-+ 0";
218fbd08684SStefan Farfeleder 	static const char skip2[] = "0123456789";
219fbd08684SStefan Farfeleder 	char *fmt;
220fbd08684SStefan Farfeleder 	int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
221fbd08684SStefan Farfeleder 	char convch, nextch;
222fbd08684SStefan Farfeleder 
223fbd08684SStefan Farfeleder 	fmt = start + 1;
224*b0620803SPedro F. Giffuni 
225*b0620803SPedro F. Giffuni 	/* look for "n$" field index specifier */
226*b0620803SPedro F. Giffuni 	fmt += strspn(fmt, skip2);
227*b0620803SPedro F. Giffuni 	if ((*fmt == '$') && (fmt != (start + 1))) {
228*b0620803SPedro F. Giffuni 		int idx = atoi(start + 1);
229*b0620803SPedro F. Giffuni 		if (idx <= myargc) {
230*b0620803SPedro F. Giffuni 			gargv = &myargv[idx - 1];
231*b0620803SPedro F. Giffuni 		} else {
232*b0620803SPedro F. Giffuni 			gargv = &myargv[myargc];
233*b0620803SPedro F. Giffuni 		}
234*b0620803SPedro F. Giffuni 		start = fmt;
235*b0620803SPedro F. Giffuni 		fmt++;
236*b0620803SPedro F. Giffuni 	} else {
237*b0620803SPedro F. Giffuni 		fmt = start + 1;
238*b0620803SPedro F. Giffuni 	}
239*b0620803SPedro F. Giffuni 
2409b50d902SRodney W. Grimes 	/* skip to field width */
2410ba01198SStefan Farfeleder 	fmt += strspn(fmt, skip1);
2429b50d902SRodney W. Grimes 	if (*fmt == '*') {
2439b50d902SRodney W. Grimes 		if (getint(&fieldwidth))
244fbd08684SStefan Farfeleder 			return (NULL);
24598dd6386STim J. Robbins 		havewidth = 1;
246d867cefdSJoerg Wunsch 		++fmt;
247d867cefdSJoerg Wunsch 	} else {
24898dd6386STim J. Robbins 		havewidth = 0;
2499b50d902SRodney W. Grimes 
2509b50d902SRodney W. Grimes 		/* skip to possible '.', get following precision */
2510ba01198SStefan Farfeleder 		fmt += strspn(fmt, skip2);
252d867cefdSJoerg Wunsch 	}
253d867cefdSJoerg Wunsch 	if (*fmt == '.') {
254d867cefdSJoerg Wunsch 		/* precision present? */
2559b50d902SRodney W. Grimes 		++fmt;
2569b50d902SRodney W. Grimes 		if (*fmt == '*') {
2579b50d902SRodney W. Grimes 			if (getint(&precision))
258fbd08684SStefan Farfeleder 				return (NULL);
25998dd6386STim J. Robbins 			haveprec = 1;
260d867cefdSJoerg Wunsch 			++fmt;
261d867cefdSJoerg Wunsch 		} else {
26298dd6386STim J. Robbins 			haveprec = 0;
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes 			/* skip to conversion char */
2650ba01198SStefan Farfeleder 			fmt += strspn(fmt, skip2);
266d867cefdSJoerg Wunsch 		}
267d867cefdSJoerg Wunsch 	} else
26898dd6386STim J. Robbins 		haveprec = 0;
2699b50d902SRodney W. Grimes 	if (!*fmt) {
2706a6760dbSJilles Tjoelker 		warnx("missing format character");
271fbd08684SStefan Farfeleder 		return (NULL);
2729b50d902SRodney W. Grimes 	}
2739b50d902SRodney W. Grimes 
274fd757c50SDavid Schultz 	/*
275fd757c50SDavid Schultz 	 * Look for a length modifier.  POSIX doesn't have these, so
276fd757c50SDavid Schultz 	 * we only support them for floating-point conversions, which
277fd757c50SDavid Schultz 	 * are extensions.  This is useful because the L modifier can
278fd757c50SDavid Schultz 	 * be used to gain extra range and precision, while omitting
279fd757c50SDavid Schultz 	 * it is more likely to produce consistent results on different
280fd757c50SDavid Schultz 	 * architectures.  This is not so important for integers
281fd757c50SDavid Schultz 	 * because overflow is the only bad thing that can happen to
282fd757c50SDavid Schultz 	 * them, but consider the command  printf %a 1.1
283fd757c50SDavid Schultz 	 */
284fd757c50SDavid Schultz 	if (*fmt == 'L') {
285fd757c50SDavid Schultz 		mod_ldbl = 1;
286fd757c50SDavid Schultz 		fmt++;
287fd757c50SDavid Schultz 		if (!strchr("aAeEfFgG", *fmt)) {
2886a6760dbSJilles Tjoelker 			warnx("bad modifier L for %%%c", *fmt);
289fbd08684SStefan Farfeleder 			return (NULL);
290fd757c50SDavid Schultz 		}
291fd757c50SDavid Schultz 	} else {
292fd757c50SDavid Schultz 		mod_ldbl = 0;
293fd757c50SDavid Schultz 	}
294fd757c50SDavid Schultz 
2959b50d902SRodney W. Grimes 	convch = *fmt;
2969b50d902SRodney W. Grimes 	nextch = *++fmt;
2979b50d902SRodney W. Grimes 	*fmt = '\0';
2989b50d902SRodney W. Grimes 	switch (convch) {
299ab5a295bSJuli Mallett 	case 'b': {
3003ec96cafSStefan Farfeleder 		size_t len;
301ab5a295bSJuli Mallett 		char *p;
302ab5a295bSJuli Mallett 		int getout;
303ab5a295bSJuli Mallett 
304bacab7d6STim J. Robbins 		p = strdup(getstr());
305bacab7d6STim J. Robbins 		if (p == NULL) {
3066a6760dbSJilles Tjoelker 			warnx("%s", strerror(ENOMEM));
307fbd08684SStefan Farfeleder 			return (NULL);
308bacab7d6STim J. Robbins 		}
3093ec96cafSStefan Farfeleder 		getout = escape(p, 0, &len);
310ab5a295bSJuli Mallett 		*(fmt - 1) = 's';
311bacab7d6STim J. Robbins 		PF(start, p);
312ab5a295bSJuli Mallett 		*(fmt - 1) = 'b';
313ab5a295bSJuli Mallett 		free(p);
314ab5a295bSJuli Mallett 		if (getout)
315fbd08684SStefan Farfeleder 			return (fmt);
316ab5a295bSJuli Mallett 		break;
317ab5a295bSJuli Mallett 	}
3189b50d902SRodney W. Grimes 	case 'c': {
3199b50d902SRodney W. Grimes 		char p;
3209b50d902SRodney W. Grimes 
3219b50d902SRodney W. Grimes 		p = getchr();
3229b50d902SRodney W. Grimes 		PF(start, p);
3239b50d902SRodney W. Grimes 		break;
3249b50d902SRodney W. Grimes 	}
3259b50d902SRodney W. Grimes 	case 's': {
32645af1a4cSDavid Malone 		const char *p;
3279b50d902SRodney W. Grimes 
3289b50d902SRodney W. Grimes 		p = getstr();
3299b50d902SRodney W. Grimes 		PF(start, p);
3309b50d902SRodney W. Grimes 		break;
3319b50d902SRodney W. Grimes 	}
3329b50d902SRodney W. Grimes 	case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
3339b50d902SRodney W. Grimes 		char *f;
334d41d23e1SStefan Farfeleder 		intmax_t val;
335d41d23e1SStefan Farfeleder 		uintmax_t uval;
336bacab7d6STim J. Robbins 		int signedconv;
3379b50d902SRodney W. Grimes 
338bacab7d6STim J. Robbins 		signedconv = (convch == 'd' || convch == 'i');
339d41d23e1SStefan Farfeleder 		if ((f = mknum(start, convch)) == NULL)
340fbd08684SStefan Farfeleder 			return (NULL);
341d41d23e1SStefan Farfeleder 		if (getnum(&val, &uval, signedconv))
342fbd08684SStefan Farfeleder 			*rval = 1;
343bacab7d6STim J. Robbins 		if (signedconv)
344bacab7d6STim J. Robbins 			PF(f, val);
345bacab7d6STim J. Robbins 		else
346bacab7d6STim J. Robbins 			PF(f, uval);
3479b50d902SRodney W. Grimes 		break;
3489b50d902SRodney W. Grimes 	}
34903b2eaacSDavid Schultz 	case 'e': case 'E':
35003b2eaacSDavid Schultz 	case 'f': case 'F':
35103b2eaacSDavid Schultz 	case 'g': case 'G':
35203b2eaacSDavid Schultz 	case 'a': case 'A': {
353fd757c50SDavid Schultz 		long double p;
3549b50d902SRodney W. Grimes 
355fd757c50SDavid Schultz 		if (getfloating(&p, mod_ldbl))
356fbd08684SStefan Farfeleder 			*rval = 1;
357fd757c50SDavid Schultz 		if (mod_ldbl)
3589b50d902SRodney W. Grimes 			PF(start, p);
359fd757c50SDavid Schultz 		else
360fd757c50SDavid Schultz 			PF(start, (double)p);
3619b50d902SRodney W. Grimes 		break;
3629b50d902SRodney W. Grimes 	}
3639b50d902SRodney W. Grimes 	default:
3646a6760dbSJilles Tjoelker 		warnx("illegal format character %c", convch);
365fbd08684SStefan Farfeleder 		return (NULL);
3669b50d902SRodney W. Grimes 	}
3679b50d902SRodney W. Grimes 	*fmt = nextch;
368fbd08684SStefan Farfeleder 	return (fmt);
3699b50d902SRodney W. Grimes }
3709b50d902SRodney W. Grimes 
3719b50d902SRodney W. Grimes static char *
3729180853bSXin LI mknum(char *str, char ch)
3739b50d902SRodney W. Grimes {
3743c6e4a5cSBen Smithurst 	static char *copy;
3753c6e4a5cSBen Smithurst 	static size_t copy_size;
3763c6e4a5cSBen Smithurst 	char *newcopy;
377bacab7d6STim J. Robbins 	size_t len, newlen;
3789b50d902SRodney W. Grimes 
3799b50d902SRodney W. Grimes 	len = strlen(str) + 2;
3803c6e4a5cSBen Smithurst 	if (len > copy_size) {
3813c6e4a5cSBen Smithurst 		newlen = ((len + 1023) >> 10) << 10;
3823c6e4a5cSBen Smithurst 		if ((newcopy = realloc(copy, newlen)) == NULL)
383bacab7d6STim J. Robbins 		{
3846a6760dbSJilles Tjoelker 			warnx("%s", strerror(ENOMEM));
3853c6e4a5cSBen Smithurst 			return (NULL);
386bacab7d6STim J. Robbins 		}
3873c6e4a5cSBen Smithurst 		copy = newcopy;
3883c6e4a5cSBen Smithurst 		copy_size = newlen;
3893c6e4a5cSBen Smithurst 	}
39062a721e7SStefan Eßer 
3919b50d902SRodney W. Grimes 	memmove(copy, str, len - 3);
392d41d23e1SStefan Farfeleder 	copy[len - 3] = 'j';
3939b50d902SRodney W. Grimes 	copy[len - 2] = ch;
3949b50d902SRodney W. Grimes 	copy[len - 1] = '\0';
3959b50d902SRodney W. Grimes 	return (copy);
3969b50d902SRodney W. Grimes }
3979b50d902SRodney W. Grimes 
398ab5a295bSJuli Mallett static int
3993ec96cafSStefan Farfeleder escape(char *fmt, int percent, size_t *len)
4009b50d902SRodney W. Grimes {
401ab71f271SPedro F. Giffuni 	char *save, *store, c;
402ab71f271SPedro F. Giffuni 	int value;
4039b50d902SRodney W. Grimes 
404ab71f271SPedro F. Giffuni 	for (save = store = fmt; ((c = *fmt) != 0); ++fmt, ++store) {
4059b50d902SRodney W. Grimes 		if (c != '\\') {
4069b50d902SRodney W. Grimes 			*store = c;
4079b50d902SRodney W. Grimes 			continue;
4089b50d902SRodney W. Grimes 		}
4099b50d902SRodney W. Grimes 		switch (*++fmt) {
4109b50d902SRodney W. Grimes 		case '\0':		/* EOS, user error */
4119b50d902SRodney W. Grimes 			*store = '\\';
4129b50d902SRodney W. Grimes 			*++store = '\0';
4133ec96cafSStefan Farfeleder 			*len = store - save;
414ab5a295bSJuli Mallett 			return (0);
4159b50d902SRodney W. Grimes 		case '\\':		/* backslash */
4169b50d902SRodney W. Grimes 		case '\'':		/* single quote */
4179b50d902SRodney W. Grimes 			*store = *fmt;
4189b50d902SRodney W. Grimes 			break;
4199b50d902SRodney W. Grimes 		case 'a':		/* bell/alert */
420f3f148d2SStefan Farfeleder 			*store = '\a';
4219b50d902SRodney W. Grimes 			break;
4229b50d902SRodney W. Grimes 		case 'b':		/* backspace */
4239b50d902SRodney W. Grimes 			*store = '\b';
4249b50d902SRodney W. Grimes 			break;
425ab5a295bSJuli Mallett 		case 'c':
426ab5a295bSJuli Mallett 			*store = '\0';
4273ec96cafSStefan Farfeleder 			*len = store - save;
428ab5a295bSJuli Mallett 			return (1);
4299b50d902SRodney W. Grimes 		case 'f':		/* form-feed */
4309b50d902SRodney W. Grimes 			*store = '\f';
4319b50d902SRodney W. Grimes 			break;
4329b50d902SRodney W. Grimes 		case 'n':		/* newline */
4339b50d902SRodney W. Grimes 			*store = '\n';
4349b50d902SRodney W. Grimes 			break;
4359b50d902SRodney W. Grimes 		case 'r':		/* carriage-return */
4369b50d902SRodney W. Grimes 			*store = '\r';
4379b50d902SRodney W. Grimes 			break;
4389b50d902SRodney W. Grimes 		case 't':		/* horizontal tab */
4399b50d902SRodney W. Grimes 			*store = '\t';
4409b50d902SRodney W. Grimes 			break;
4419b50d902SRodney W. Grimes 		case 'v':		/* vertical tab */
442f3f148d2SStefan Farfeleder 			*store = '\v';
4439b50d902SRodney W. Grimes 			break;
4449b50d902SRodney W. Grimes 					/* octal constant */
4459b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3':
4469b50d902SRodney W. Grimes 		case '4': case '5': case '6': case '7':
4479d65050eSDavid Schultz 			c = (!percent && *fmt == '0') ? 4 : 3;
4489d65050eSDavid Schultz 			for (value = 0;
4499b50d902SRodney W. Grimes 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
4509b50d902SRodney W. Grimes 				value <<= 3;
4519b50d902SRodney W. Grimes 				value += *fmt - '0';
4529b50d902SRodney W. Grimes 			}
4539b50d902SRodney W. Grimes 			--fmt;
45412e8db40STim J. Robbins 			if (percent && value == '%') {
45537fd4590STim J. Robbins 				*store++ = '%';
45637fd4590STim J. Robbins 				*store = '%';
45737fd4590STim J. Robbins 			} else
458ab71f271SPedro F. Giffuni 				*store = (char)value;
4599b50d902SRodney W. Grimes 			break;
4609b50d902SRodney W. Grimes 		default:
4619b50d902SRodney W. Grimes 			*store = *fmt;
4629b50d902SRodney W. Grimes 			break;
4639b50d902SRodney W. Grimes 		}
4649b50d902SRodney W. Grimes 	}
4659b50d902SRodney W. Grimes 	*store = '\0';
4663ec96cafSStefan Farfeleder 	*len = store - save;
467ab5a295bSJuli Mallett 	return (0);
4689b50d902SRodney W. Grimes }
4699b50d902SRodney W. Grimes 
4709b50d902SRodney W. Grimes static int
471f4ac32deSDavid Malone getchr(void)
4729b50d902SRodney W. Grimes {
4739b50d902SRodney W. Grimes 	if (!*gargv)
4749b50d902SRodney W. Grimes 		return ('\0');
4759b50d902SRodney W. Grimes 	return ((int)**gargv++);
4769b50d902SRodney W. Grimes }
4779b50d902SRodney W. Grimes 
47845af1a4cSDavid Malone static const char *
479f4ac32deSDavid Malone getstr(void)
4809b50d902SRodney W. Grimes {
4819b50d902SRodney W. Grimes 	if (!*gargv)
4829b50d902SRodney W. Grimes 		return ("");
4839b50d902SRodney W. Grimes 	return (*gargv++);
4849b50d902SRodney W. Grimes }
4859b50d902SRodney W. Grimes 
4869b50d902SRodney W. Grimes static int
487f4ac32deSDavid Malone getint(int *ip)
4889b50d902SRodney W. Grimes {
489d41d23e1SStefan Farfeleder 	intmax_t val;
490d41d23e1SStefan Farfeleder 	uintmax_t uval;
491bacab7d6STim J. Robbins 	int rval;
4929b50d902SRodney W. Grimes 
493d41d23e1SStefan Farfeleder 	if (getnum(&val, &uval, 1))
4949b50d902SRodney W. Grimes 		return (1);
495bacab7d6STim J. Robbins 	rval = 0;
496bacab7d6STim J. Robbins 	if (val < INT_MIN || val > INT_MAX) {
4976a6760dbSJilles Tjoelker 		warnx("%s: %s", *gargv, strerror(ERANGE));
498bacab7d6STim J. Robbins 		rval = 1;
499bacab7d6STim J. Robbins 	}
50062a721e7SStefan Eßer 	*ip = (int)val;
501bacab7d6STim J. Robbins 	return (rval);
5029b50d902SRodney W. Grimes }
5039b50d902SRodney W. Grimes 
5049b50d902SRodney W. Grimes static int
505d41d23e1SStefan Farfeleder getnum(intmax_t *ip, uintmax_t *uip, int signedconv)
5069b50d902SRodney W. Grimes {
5079b50d902SRodney W. Grimes 	char *ep;
508bacab7d6STim J. Robbins 	int rval;
5099b50d902SRodney W. Grimes 
5109b50d902SRodney W. Grimes 	if (!*gargv) {
5112f01c791SEitan Adler 		*ip = *uip = 0;
5129b50d902SRodney W. Grimes 		return (0);
5139b50d902SRodney W. Grimes 	}
514ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
515bacab7d6STim J. Robbins 		if (signedconv)
516d41d23e1SStefan Farfeleder 			*ip = asciicode();
517bacab7d6STim J. Robbins 		else
518d41d23e1SStefan Farfeleder 			*uip = asciicode();
5199b50d902SRodney W. Grimes 		return (0);
5209b50d902SRodney W. Grimes 	}
521bacab7d6STim J. Robbins 	rval = 0;
522ab5a295bSJuli Mallett 	errno = 0;
523bacab7d6STim J. Robbins 	if (signedconv)
524d41d23e1SStefan Farfeleder 		*ip = strtoimax(*gargv, &ep, 0);
525bacab7d6STim J. Robbins 	else
526d41d23e1SStefan Farfeleder 		*uip = strtoumax(*gargv, &ep, 0);
527bacab7d6STim J. Robbins 	if (ep == *gargv) {
5286a6760dbSJilles Tjoelker 		warnx("%s: expected numeric value", *gargv);
529bacab7d6STim J. Robbins 		rval = 1;
530bacab7d6STim J. Robbins 	}
531bacab7d6STim J. Robbins 	else if (*ep != '\0') {
5326a6760dbSJilles Tjoelker 		warnx("%s: not completely converted", *gargv);
533bacab7d6STim J. Robbins 		rval = 1;
534bacab7d6STim J. Robbins 	}
535bacab7d6STim J. Robbins 	if (errno == ERANGE) {
5366a6760dbSJilles Tjoelker 		warnx("%s: %s", *gargv, strerror(ERANGE));
537bacab7d6STim J. Robbins 		rval = 1;
538bacab7d6STim J. Robbins 	}
539ab5a295bSJuli Mallett 	++gargv;
540bacab7d6STim J. Robbins 	return (rval);
5419b50d902SRodney W. Grimes }
5429b50d902SRodney W. Grimes 
543bacab7d6STim J. Robbins static int
544fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl)
5459b50d902SRodney W. Grimes {
546ab5a295bSJuli Mallett 	char *ep;
547bacab7d6STim J. Robbins 	int rval;
548ab5a295bSJuli Mallett 
5495ec2b8dcSStefan Farfeleder 	if (!*gargv) {
5505ec2b8dcSStefan Farfeleder 		*dp = 0.0;
551bacab7d6STim J. Robbins 		return (0);
5525ec2b8dcSStefan Farfeleder 	}
553ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
554bacab7d6STim J. Robbins 		*dp = asciicode();
555bacab7d6STim J. Robbins 		return (0);
556ab5a295bSJuli Mallett 	}
5578c423a99SColin Percival 	rval = 0;
558ab5a295bSJuli Mallett 	errno = 0;
559fd757c50SDavid Schultz 	if (mod_ldbl)
560fd757c50SDavid Schultz 		*dp = strtold(*gargv, &ep);
561fd757c50SDavid Schultz 	else
562bacab7d6STim J. Robbins 		*dp = strtod(*gargv, &ep);
563bacab7d6STim J. Robbins 	if (ep == *gargv) {
5646a6760dbSJilles Tjoelker 		warnx("%s: expected numeric value", *gargv);
565bacab7d6STim J. Robbins 		rval = 1;
566bacab7d6STim J. Robbins 	} else if (*ep != '\0') {
5676a6760dbSJilles Tjoelker 		warnx("%s: not completely converted", *gargv);
568bacab7d6STim J. Robbins 		rval = 1;
569bacab7d6STim J. Robbins 	}
570bacab7d6STim J. Robbins 	if (errno == ERANGE) {
5716a6760dbSJilles Tjoelker 		warnx("%s: %s", *gargv, strerror(ERANGE));
572bacab7d6STim J. Robbins 		rval = 1;
573bacab7d6STim J. Robbins 	}
574ab5a295bSJuli Mallett 	++gargv;
575bacab7d6STim J. Robbins 	return (rval);
5769b50d902SRodney W. Grimes }
5779b50d902SRodney W. Grimes 
5789b50d902SRodney W. Grimes static int
579f4ac32deSDavid Malone asciicode(void)
5809b50d902SRodney W. Grimes {
581f4ac32deSDavid Malone 	int ch;
58298102dabSJilles Tjoelker 	wchar_t wch;
58398102dabSJilles Tjoelker 	mbstate_t mbs;
5849b50d902SRodney W. Grimes 
58598102dabSJilles Tjoelker 	ch = (unsigned char)**gargv;
58698102dabSJilles Tjoelker 	if (ch == '\'' || ch == '"') {
58798102dabSJilles Tjoelker 		memset(&mbs, 0, sizeof(mbs));
58898102dabSJilles Tjoelker 		switch (mbrtowc(&wch, *gargv + 1, MB_LEN_MAX, &mbs)) {
58998102dabSJilles Tjoelker 		case (size_t)-2:
59098102dabSJilles Tjoelker 		case (size_t)-1:
59198102dabSJilles Tjoelker 			wch = (unsigned char)gargv[0][1];
59298102dabSJilles Tjoelker 			break;
59398102dabSJilles Tjoelker 		case 0:
59498102dabSJilles Tjoelker 			wch = 0;
59598102dabSJilles Tjoelker 			break;
59698102dabSJilles Tjoelker 		}
59798102dabSJilles Tjoelker 		ch = wch;
59898102dabSJilles Tjoelker 	}
5999b50d902SRodney W. Grimes 	++gargv;
6009b50d902SRodney W. Grimes 	return (ch);
6019b50d902SRodney W. Grimes }
6029b50d902SRodney W. Grimes 
6039b50d902SRodney W. Grimes static void
604f4ac32deSDavid Malone usage(void)
6059b50d902SRodney W. Grimes {
606f682f10cSRuslan Ermilov 	(void)fprintf(stderr, "usage: printf format [arguments ...]\n");
6079b50d902SRodney W. Grimes }
608