xref: /freebsd/usr.bin/printf/printf.c (revision de46de4dd1362165e9bf9c0d2a50cca7f45044b4)
1c9aca921SXin LI /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
301866e8abSJilles Tjoelker #ifndef SHELL
319b50d902SRodney W. Grimes #ifndef lint
323ec30b79SSteve Price static char const copyright[] =
339b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
349b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
359b50d902SRodney W. Grimes #endif /* not lint */
369b50d902SRodney W. Grimes #endif
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #ifndef lint
39c9e05349SWarner Losh #if 0
403ec30b79SSteve Price static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
41c9e05349SWarner Losh #endif
421ea7321bSMartin Cracauer static const char rcsid[] =
431ea7321bSMartin Cracauer   "$FreeBSD$";
449b50d902SRodney W. Grimes #endif /* not lint */
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <sys/types.h>
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <err.h>
499b50d902SRodney W. Grimes #include <errno.h>
50d41d23e1SStefan Farfeleder #include <inttypes.h>
519b50d902SRodney W. Grimes #include <limits.h>
52c9aca921SXin LI #include <locale.h>
539b50d902SRodney W. Grimes #include <stdio.h>
549b50d902SRodney W. Grimes #include <stdlib.h>
559b50d902SRodney W. Grimes #include <string.h>
569d19feb5SSteve Price #include <unistd.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #ifdef SHELL
599b50d902SRodney W. Grimes #define main printfcmd
60d9f93710SJoerg Wunsch #include "bltin/bltin.h"
613c6e4a5cSBen Smithurst #include "memalloc.h"
629897c45fSJilles Tjoelker #include "error.h"
633b53d380SBruce Evans #else
643b53d380SBruce Evans #define	warnx1(a, b, c)		warnx(a)
653b53d380SBruce Evans #define	warnx2(a, b, c)		warnx(a, b)
663b53d380SBruce Evans #define	warnx3(a, b, c)		warnx(a, b, c)
679b50d902SRodney W. Grimes #endif
689b50d902SRodney W. Grimes 
69bacab7d6STim J. Robbins #define PF(f, func) do { \
70d72f654cSPeter Wemm 	char *b = NULL; \
7198dd6386STim J. Robbins 	if (havewidth) \
7298dd6386STim J. Robbins 		if (haveprec) \
73d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, precision, func); \
749b50d902SRodney W. Grimes 		else \
75d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, func); \
7698dd6386STim J. Robbins 	else if (haveprec) \
77d72f654cSPeter Wemm 		(void)asprintf(&b, f, precision, func); \
789b50d902SRodney W. Grimes 	else \
79d72f654cSPeter Wemm 		(void)asprintf(&b, f, func); \
80d72f654cSPeter Wemm 	if (b) { \
81d72f654cSPeter Wemm 		(void)fputs(b, stdout); \
82d72f654cSPeter Wemm 		free(b); \
83d72f654cSPeter Wemm 	} \
84bacab7d6STim J. Robbins } while (0)
859b50d902SRodney W. Grimes 
86d3cb5dedSWarner Losh static int	 asciicode(void);
879897c45fSJilles Tjoelker static char	*printf_doformat(char *, int *);
883ec96cafSStefan Farfeleder static int	 escape(char *, int, size_t *);
89d3cb5dedSWarner Losh static int	 getchr(void);
90fd757c50SDavid Schultz static int	 getfloating(long double *, int);
91d3cb5dedSWarner Losh static int	 getint(int *);
92d41d23e1SStefan Farfeleder static int	 getnum(intmax_t *, uintmax_t *, int);
93bacab7d6STim J. Robbins static const char
94bacab7d6STim J. Robbins 		*getstr(void);
959180853bSXin LI static char	*mknum(char *, char);
96d3cb5dedSWarner Losh static void	 usage(void);
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes static char **gargv;
999b50d902SRodney W. Grimes 
1009b50d902SRodney W. Grimes int
101f4ac32deSDavid Malone main(int argc, char *argv[])
1029b50d902SRodney W. Grimes {
1033ec96cafSStefan Farfeleder 	size_t len;
104*de46de4dSXin LI 	int ch, chopped, end, rval;
105fbd08684SStefan Farfeleder 	char *format, *fmt, *start;
1069b50d902SRodney W. Grimes 
1071866e8abSJilles Tjoelker #ifndef SHELL
10869be0c5eSXin LI 	(void) setlocale(LC_ALL, "");
109dc7d8c99SAndrey A. Chernov #endif
1109897c45fSJilles Tjoelker #ifdef SHELL
1119897c45fSJilles Tjoelker 	optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
1129897c45fSJilles Tjoelker #endif
113*de46de4dSXin LI 	while ((ch = getopt(argc, argv, "")) != -1)
114*de46de4dSXin LI 		switch (ch) {
115*de46de4dSXin LI 		case '?':
116*de46de4dSXin LI 		default:
117*de46de4dSXin LI 			usage();
118*de46de4dSXin LI 			return (1);
1199b50d902SRodney W. Grimes 		}
120*de46de4dSXin LI 	argc -= optind;
121*de46de4dSXin LI 	argv += optind;
1229b50d902SRodney W. Grimes 
1239b50d902SRodney W. Grimes 	if (argc < 1) {
1249b50d902SRodney W. Grimes 		usage();
1255eccc000SXin LI 		return (1);
1269b50d902SRodney W. Grimes 	}
1279b50d902SRodney W. Grimes 
1289897c45fSJilles Tjoelker #ifdef SHELL
1299897c45fSJilles Tjoelker 	INTOFF;
1309897c45fSJilles Tjoelker #endif
1319b50d902SRodney W. Grimes 	/*
1329b50d902SRodney W. Grimes 	 * Basic algorithm is to scan the format string for conversion
1339b50d902SRodney W. Grimes 	 * specifications -- once one is found, find out if the field
1349b50d902SRodney W. Grimes 	 * width or precision is a '*'; if it is, gather up value.  Note,
1359b50d902SRodney W. Grimes 	 * format strings are reused as necessary to use up the provided
1369b50d902SRodney W. Grimes 	 * arguments, arguments of zero/null string are provided to use
1379b50d902SRodney W. Grimes 	 * up the format string.
1389b50d902SRodney W. Grimes 	 */
1393ec96cafSStefan Farfeleder 	fmt = format = *argv;
1403ec96cafSStefan Farfeleder 	chopped = escape(fmt, 1, &len);		/* backslash interpretation */
141fbd08684SStefan Farfeleder 	rval = end = 0;
1429b50d902SRodney W. Grimes 	gargv = ++argv;
1439b50d902SRodney W. Grimes 	for (;;) {
144fbd08684SStefan Farfeleder 		start = fmt;
1453ec96cafSStefan Farfeleder 		while (fmt < format + len) {
146fbd08684SStefan Farfeleder 			if (fmt[0] == '%') {
147fbd08684SStefan Farfeleder 				fwrite(start, 1, fmt - start, stdout);
148fbd08684SStefan Farfeleder 				if (fmt[1] == '%') {
1499b50d902SRodney W. Grimes 					/* %% prints a % */
150fbd08684SStefan Farfeleder 					putchar('%');
151fbd08684SStefan Farfeleder 					fmt += 2;
152fbd08684SStefan Farfeleder 				} else {
1539897c45fSJilles Tjoelker 					fmt = printf_doformat(fmt, &rval);
1549897c45fSJilles Tjoelker 					if (fmt == NULL) {
1559897c45fSJilles Tjoelker #ifdef SHELL
1569897c45fSJilles Tjoelker 						INTON;
1579897c45fSJilles Tjoelker #endif
158fbd08684SStefan Farfeleder 						return (1);
1599897c45fSJilles Tjoelker 					}
160fbd08684SStefan Farfeleder 					end = 0;
1619b50d902SRodney W. Grimes 				}
162fbd08684SStefan Farfeleder 				start = fmt;
163fbd08684SStefan Farfeleder 			} else
164fbd08684SStefan Farfeleder 				fmt++;
1659b50d902SRodney W. Grimes 		}
1669b50d902SRodney W. Grimes 
167fbd08684SStefan Farfeleder 		if (end == 1) {
168fbd08684SStefan Farfeleder 			warnx1("missing format character", NULL, NULL);
1699897c45fSJilles Tjoelker #ifdef SHELL
1709897c45fSJilles Tjoelker 			INTON;
1719897c45fSJilles Tjoelker #endif
172fbd08684SStefan Farfeleder 			return (1);
173fbd08684SStefan Farfeleder 		}
174fbd08684SStefan Farfeleder 		fwrite(start, 1, fmt - start, stdout);
1759897c45fSJilles Tjoelker 		if (chopped || !*gargv) {
1769897c45fSJilles Tjoelker #ifdef SHELL
1779897c45fSJilles Tjoelker 			INTON;
1789897c45fSJilles Tjoelker #endif
179fbd08684SStefan Farfeleder 			return (rval);
1809897c45fSJilles Tjoelker 		}
181fbd08684SStefan Farfeleder 		/* Restart at the beginning of the format string. */
182fbd08684SStefan Farfeleder 		fmt = format;
183fbd08684SStefan Farfeleder 		end = 1;
184fbd08684SStefan Farfeleder 	}
185fbd08684SStefan Farfeleder 	/* NOTREACHED */
186fbd08684SStefan Farfeleder }
187fbd08684SStefan Farfeleder 
188fbd08684SStefan Farfeleder 
189fbd08684SStefan Farfeleder static char *
1909897c45fSJilles Tjoelker printf_doformat(char *start, int *rval)
191fbd08684SStefan Farfeleder {
192fbd08684SStefan Farfeleder 	static const char skip1[] = "#'-+ 0";
193fbd08684SStefan Farfeleder 	static const char skip2[] = "0123456789";
194fbd08684SStefan Farfeleder 	char *fmt;
195fbd08684SStefan Farfeleder 	int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
196fbd08684SStefan Farfeleder 	char convch, nextch;
197fbd08684SStefan Farfeleder 
198fbd08684SStefan Farfeleder 	fmt = start + 1;
1999b50d902SRodney W. Grimes 	/* skip to field width */
2000ba01198SStefan Farfeleder 	fmt += strspn(fmt, skip1);
2019b50d902SRodney W. Grimes 	if (*fmt == '*') {
2029b50d902SRodney W. Grimes 		if (getint(&fieldwidth))
203fbd08684SStefan Farfeleder 			return (NULL);
20498dd6386STim J. Robbins 		havewidth = 1;
205d867cefdSJoerg Wunsch 		++fmt;
206d867cefdSJoerg Wunsch 	} else {
20798dd6386STim J. Robbins 		havewidth = 0;
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes 		/* skip to possible '.', get following precision */
2100ba01198SStefan Farfeleder 		fmt += strspn(fmt, skip2);
211d867cefdSJoerg Wunsch 	}
212d867cefdSJoerg Wunsch 	if (*fmt == '.') {
213d867cefdSJoerg Wunsch 		/* precision present? */
2149b50d902SRodney W. Grimes 		++fmt;
2159b50d902SRodney W. Grimes 		if (*fmt == '*') {
2169b50d902SRodney W. Grimes 			if (getint(&precision))
217fbd08684SStefan Farfeleder 				return (NULL);
21898dd6386STim J. Robbins 			haveprec = 1;
219d867cefdSJoerg Wunsch 			++fmt;
220d867cefdSJoerg Wunsch 		} else {
22198dd6386STim J. Robbins 			haveprec = 0;
2229b50d902SRodney W. Grimes 
2239b50d902SRodney W. Grimes 			/* skip to conversion char */
2240ba01198SStefan Farfeleder 			fmt += strspn(fmt, skip2);
225d867cefdSJoerg Wunsch 		}
226d867cefdSJoerg Wunsch 	} else
22798dd6386STim J. Robbins 		haveprec = 0;
2289b50d902SRodney W. Grimes 	if (!*fmt) {
2293b53d380SBruce Evans 		warnx1("missing format character", NULL, NULL);
230fbd08684SStefan Farfeleder 		return (NULL);
2319b50d902SRodney W. Grimes 	}
2329b50d902SRodney W. Grimes 
233fd757c50SDavid Schultz 	/*
234fd757c50SDavid Schultz 	 * Look for a length modifier.  POSIX doesn't have these, so
235fd757c50SDavid Schultz 	 * we only support them for floating-point conversions, which
236fd757c50SDavid Schultz 	 * are extensions.  This is useful because the L modifier can
237fd757c50SDavid Schultz 	 * be used to gain extra range and precision, while omitting
238fd757c50SDavid Schultz 	 * it is more likely to produce consistent results on different
239fd757c50SDavid Schultz 	 * architectures.  This is not so important for integers
240fd757c50SDavid Schultz 	 * because overflow is the only bad thing that can happen to
241fd757c50SDavid Schultz 	 * them, but consider the command  printf %a 1.1
242fd757c50SDavid Schultz 	 */
243fd757c50SDavid Schultz 	if (*fmt == 'L') {
244fd757c50SDavid Schultz 		mod_ldbl = 1;
245fd757c50SDavid Schultz 		fmt++;
246fd757c50SDavid Schultz 		if (!strchr("aAeEfFgG", *fmt)) {
247fd757c50SDavid Schultz 			warnx2("bad modifier L for %%%c", *fmt, NULL);
248fbd08684SStefan Farfeleder 			return (NULL);
249fd757c50SDavid Schultz 		}
250fd757c50SDavid Schultz 	} else {
251fd757c50SDavid Schultz 		mod_ldbl = 0;
252fd757c50SDavid Schultz 	}
253fd757c50SDavid Schultz 
2549b50d902SRodney W. Grimes 	convch = *fmt;
2559b50d902SRodney W. Grimes 	nextch = *++fmt;
2569b50d902SRodney W. Grimes 	*fmt = '\0';
2579b50d902SRodney W. Grimes 	switch (convch) {
258ab5a295bSJuli Mallett 	case 'b': {
2593ec96cafSStefan Farfeleder 		size_t len;
260ab5a295bSJuli Mallett 		char *p;
261ab5a295bSJuli Mallett 		int getout;
262ab5a295bSJuli Mallett 
263bacab7d6STim J. Robbins #ifdef SHELL
264bacab7d6STim J. Robbins 		p = savestr(getstr());
265bacab7d6STim J. Robbins #else
266bacab7d6STim J. Robbins 		p = strdup(getstr());
267bacab7d6STim J. Robbins #endif
268bacab7d6STim J. Robbins 		if (p == NULL) {
269bacab7d6STim J. Robbins 			warnx2("%s", strerror(ENOMEM), NULL);
270fbd08684SStefan Farfeleder 			return (NULL);
271bacab7d6STim J. Robbins 		}
2723ec96cafSStefan Farfeleder 		getout = escape(p, 0, &len);
273ab5a295bSJuli Mallett 		*(fmt - 1) = 's';
274bacab7d6STim J. Robbins 		PF(start, p);
275ab5a295bSJuli Mallett 		*(fmt - 1) = 'b';
276bacab7d6STim J. Robbins #ifdef SHELL
277bacab7d6STim J. Robbins 		ckfree(p);
278bacab7d6STim J. Robbins #else
279ab5a295bSJuli Mallett 		free(p);
280bacab7d6STim J. Robbins #endif
281ab5a295bSJuli Mallett 		if (getout)
282fbd08684SStefan Farfeleder 			return (fmt);
283ab5a295bSJuli Mallett 		break;
284ab5a295bSJuli Mallett 	}
2859b50d902SRodney W. Grimes 	case 'c': {
2869b50d902SRodney W. Grimes 		char p;
2879b50d902SRodney W. Grimes 
2889b50d902SRodney W. Grimes 		p = getchr();
2899b50d902SRodney W. Grimes 		PF(start, p);
2909b50d902SRodney W. Grimes 		break;
2919b50d902SRodney W. Grimes 	}
2929b50d902SRodney W. Grimes 	case 's': {
29345af1a4cSDavid Malone 		const char *p;
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes 		p = getstr();
2969b50d902SRodney W. Grimes 		PF(start, p);
2979b50d902SRodney W. Grimes 		break;
2989b50d902SRodney W. Grimes 	}
2999b50d902SRodney W. Grimes 	case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
3009b50d902SRodney W. Grimes 		char *f;
301d41d23e1SStefan Farfeleder 		intmax_t val;
302d41d23e1SStefan Farfeleder 		uintmax_t uval;
303bacab7d6STim J. Robbins 		int signedconv;
3049b50d902SRodney W. Grimes 
305bacab7d6STim J. Robbins 		signedconv = (convch == 'd' || convch == 'i');
306d41d23e1SStefan Farfeleder 		if ((f = mknum(start, convch)) == NULL)
307fbd08684SStefan Farfeleder 			return (NULL);
308d41d23e1SStefan Farfeleder 		if (getnum(&val, &uval, signedconv))
309fbd08684SStefan Farfeleder 			*rval = 1;
310bacab7d6STim J. Robbins 		if (signedconv)
311bacab7d6STim J. Robbins 			PF(f, val);
312bacab7d6STim J. Robbins 		else
313bacab7d6STim J. Robbins 			PF(f, uval);
3149b50d902SRodney W. Grimes 		break;
3159b50d902SRodney W. Grimes 	}
31603b2eaacSDavid Schultz 	case 'e': case 'E':
31703b2eaacSDavid Schultz 	case 'f': case 'F':
31803b2eaacSDavid Schultz 	case 'g': case 'G':
31903b2eaacSDavid Schultz 	case 'a': case 'A': {
320fd757c50SDavid Schultz 		long double p;
3219b50d902SRodney W. Grimes 
322fd757c50SDavid Schultz 		if (getfloating(&p, mod_ldbl))
323fbd08684SStefan Farfeleder 			*rval = 1;
324fd757c50SDavid Schultz 		if (mod_ldbl)
3259b50d902SRodney W. Grimes 			PF(start, p);
326fd757c50SDavid Schultz 		else
327fd757c50SDavid Schultz 			PF(start, (double)p);
3289b50d902SRodney W. Grimes 		break;
3299b50d902SRodney W. Grimes 	}
3309b50d902SRodney W. Grimes 	default:
3313b53d380SBruce Evans 		warnx2("illegal format character %c", convch, NULL);
332fbd08684SStefan Farfeleder 		return (NULL);
3339b50d902SRodney W. Grimes 	}
3349b50d902SRodney W. Grimes 	*fmt = nextch;
335fbd08684SStefan Farfeleder 	return (fmt);
3369b50d902SRodney W. Grimes }
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes static char *
3399180853bSXin LI mknum(char *str, char ch)
3409b50d902SRodney W. Grimes {
3413c6e4a5cSBen Smithurst 	static char *copy;
3423c6e4a5cSBen Smithurst 	static size_t copy_size;
3433c6e4a5cSBen Smithurst 	char *newcopy;
344bacab7d6STim J. Robbins 	size_t len, newlen;
3459b50d902SRodney W. Grimes 
3469b50d902SRodney W. Grimes 	len = strlen(str) + 2;
3473c6e4a5cSBen Smithurst 	if (len > copy_size) {
3483c6e4a5cSBen Smithurst 		newlen = ((len + 1023) >> 10) << 10;
3493c6e4a5cSBen Smithurst #ifdef SHELL
3503c6e4a5cSBen Smithurst 		if ((newcopy = ckrealloc(copy, newlen)) == NULL)
3513c6e4a5cSBen Smithurst #else
3523c6e4a5cSBen Smithurst 		if ((newcopy = realloc(copy, newlen)) == NULL)
3533c6e4a5cSBen Smithurst #endif
354bacab7d6STim J. Robbins 		{
355bacab7d6STim J. Robbins 			warnx2("%s", strerror(ENOMEM), NULL);
3563c6e4a5cSBen Smithurst 			return (NULL);
357bacab7d6STim J. Robbins 		}
3583c6e4a5cSBen Smithurst 		copy = newcopy;
3593c6e4a5cSBen Smithurst 		copy_size = newlen;
3603c6e4a5cSBen Smithurst 	}
36162a721e7SStefan Eßer 
3629b50d902SRodney W. Grimes 	memmove(copy, str, len - 3);
363d41d23e1SStefan Farfeleder 	copy[len - 3] = 'j';
3649b50d902SRodney W. Grimes 	copy[len - 2] = ch;
3659b50d902SRodney W. Grimes 	copy[len - 1] = '\0';
3669b50d902SRodney W. Grimes 	return (copy);
3679b50d902SRodney W. Grimes }
3689b50d902SRodney W. Grimes 
369ab5a295bSJuli Mallett static int
3703ec96cafSStefan Farfeleder escape(char *fmt, int percent, size_t *len)
3719b50d902SRodney W. Grimes {
3723ec96cafSStefan Farfeleder 	char *save, *store;
373f4ac32deSDavid Malone 	int value, c;
3749b50d902SRodney W. Grimes 
3753ec96cafSStefan Farfeleder 	for (save = store = fmt; (c = *fmt); ++fmt, ++store) {
3769b50d902SRodney W. Grimes 		if (c != '\\') {
3779b50d902SRodney W. Grimes 			*store = c;
3789b50d902SRodney W. Grimes 			continue;
3799b50d902SRodney W. Grimes 		}
3809b50d902SRodney W. Grimes 		switch (*++fmt) {
3819b50d902SRodney W. Grimes 		case '\0':		/* EOS, user error */
3829b50d902SRodney W. Grimes 			*store = '\\';
3839b50d902SRodney W. Grimes 			*++store = '\0';
3843ec96cafSStefan Farfeleder 			*len = store - save;
385ab5a295bSJuli Mallett 			return (0);
3869b50d902SRodney W. Grimes 		case '\\':		/* backslash */
3879b50d902SRodney W. Grimes 		case '\'':		/* single quote */
3889b50d902SRodney W. Grimes 			*store = *fmt;
3899b50d902SRodney W. Grimes 			break;
3909b50d902SRodney W. Grimes 		case 'a':		/* bell/alert */
391f3f148d2SStefan Farfeleder 			*store = '\a';
3929b50d902SRodney W. Grimes 			break;
3939b50d902SRodney W. Grimes 		case 'b':		/* backspace */
3949b50d902SRodney W. Grimes 			*store = '\b';
3959b50d902SRodney W. Grimes 			break;
396ab5a295bSJuli Mallett 		case 'c':
397ab5a295bSJuli Mallett 			*store = '\0';
3983ec96cafSStefan Farfeleder 			*len = store - save;
399ab5a295bSJuli Mallett 			return (1);
4009b50d902SRodney W. Grimes 		case 'f':		/* form-feed */
4019b50d902SRodney W. Grimes 			*store = '\f';
4029b50d902SRodney W. Grimes 			break;
4039b50d902SRodney W. Grimes 		case 'n':		/* newline */
4049b50d902SRodney W. Grimes 			*store = '\n';
4059b50d902SRodney W. Grimes 			break;
4069b50d902SRodney W. Grimes 		case 'r':		/* carriage-return */
4079b50d902SRodney W. Grimes 			*store = '\r';
4089b50d902SRodney W. Grimes 			break;
4099b50d902SRodney W. Grimes 		case 't':		/* horizontal tab */
4109b50d902SRodney W. Grimes 			*store = '\t';
4119b50d902SRodney W. Grimes 			break;
4129b50d902SRodney W. Grimes 		case 'v':		/* vertical tab */
413f3f148d2SStefan Farfeleder 			*store = '\v';
4149b50d902SRodney W. Grimes 			break;
4159b50d902SRodney W. Grimes 					/* octal constant */
4169b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3':
4179b50d902SRodney W. Grimes 		case '4': case '5': case '6': case '7':
4189d65050eSDavid Schultz 			c = (!percent && *fmt == '0') ? 4 : 3;
4199d65050eSDavid Schultz 			for (value = 0;
4209b50d902SRodney W. Grimes 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
4219b50d902SRodney W. Grimes 				value <<= 3;
4229b50d902SRodney W. Grimes 				value += *fmt - '0';
4239b50d902SRodney W. Grimes 			}
4249b50d902SRodney W. Grimes 			--fmt;
42512e8db40STim J. Robbins 			if (percent && value == '%') {
42637fd4590STim J. Robbins 				*store++ = '%';
42737fd4590STim J. Robbins 				*store = '%';
42837fd4590STim J. Robbins 			} else
4299b50d902SRodney W. Grimes 				*store = value;
4309b50d902SRodney W. Grimes 			break;
4319b50d902SRodney W. Grimes 		default:
4329b50d902SRodney W. Grimes 			*store = *fmt;
4339b50d902SRodney W. Grimes 			break;
4349b50d902SRodney W. Grimes 		}
4359b50d902SRodney W. Grimes 	}
4369b50d902SRodney W. Grimes 	*store = '\0';
4373ec96cafSStefan Farfeleder 	*len = store - save;
438ab5a295bSJuli Mallett 	return (0);
4399b50d902SRodney W. Grimes }
4409b50d902SRodney W. Grimes 
4419b50d902SRodney W. Grimes static int
442f4ac32deSDavid Malone getchr(void)
4439b50d902SRodney W. Grimes {
4449b50d902SRodney W. Grimes 	if (!*gargv)
4459b50d902SRodney W. Grimes 		return ('\0');
4469b50d902SRodney W. Grimes 	return ((int)**gargv++);
4479b50d902SRodney W. Grimes }
4489b50d902SRodney W. Grimes 
44945af1a4cSDavid Malone static const char *
450f4ac32deSDavid Malone getstr(void)
4519b50d902SRodney W. Grimes {
4529b50d902SRodney W. Grimes 	if (!*gargv)
4539b50d902SRodney W. Grimes 		return ("");
4549b50d902SRodney W. Grimes 	return (*gargv++);
4559b50d902SRodney W. Grimes }
4569b50d902SRodney W. Grimes 
4579b50d902SRodney W. Grimes static int
458f4ac32deSDavid Malone getint(int *ip)
4599b50d902SRodney W. Grimes {
460d41d23e1SStefan Farfeleder 	intmax_t val;
461d41d23e1SStefan Farfeleder 	uintmax_t uval;
462bacab7d6STim J. Robbins 	int rval;
4639b50d902SRodney W. Grimes 
464d41d23e1SStefan Farfeleder 	if (getnum(&val, &uval, 1))
4659b50d902SRodney W. Grimes 		return (1);
466bacab7d6STim J. Robbins 	rval = 0;
467bacab7d6STim J. Robbins 	if (val < INT_MIN || val > INT_MAX) {
4683b53d380SBruce Evans 		warnx3("%s: %s", *gargv, strerror(ERANGE));
469bacab7d6STim J. Robbins 		rval = 1;
470bacab7d6STim J. Robbins 	}
47162a721e7SStefan Eßer 	*ip = (int)val;
472bacab7d6STim J. Robbins 	return (rval);
4739b50d902SRodney W. Grimes }
4749b50d902SRodney W. Grimes 
4759b50d902SRodney W. Grimes static int
476d41d23e1SStefan Farfeleder getnum(intmax_t *ip, uintmax_t *uip, int signedconv)
4779b50d902SRodney W. Grimes {
4789b50d902SRodney W. Grimes 	char *ep;
479bacab7d6STim J. Robbins 	int rval;
4809b50d902SRodney W. Grimes 
4819b50d902SRodney W. Grimes 	if (!*gargv) {
482d41d23e1SStefan Farfeleder 		*ip = 0;
4839b50d902SRodney W. Grimes 		return (0);
4849b50d902SRodney W. Grimes 	}
485ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
486bacab7d6STim J. Robbins 		if (signedconv)
487d41d23e1SStefan Farfeleder 			*ip = asciicode();
488bacab7d6STim J. Robbins 		else
489d41d23e1SStefan Farfeleder 			*uip = asciicode();
4909b50d902SRodney W. Grimes 		return (0);
4919b50d902SRodney W. Grimes 	}
492bacab7d6STim J. Robbins 	rval = 0;
493ab5a295bSJuli Mallett 	errno = 0;
494bacab7d6STim J. Robbins 	if (signedconv)
495d41d23e1SStefan Farfeleder 		*ip = strtoimax(*gargv, &ep, 0);
496bacab7d6STim J. Robbins 	else
497d41d23e1SStefan Farfeleder 		*uip = strtoumax(*gargv, &ep, 0);
498bacab7d6STim J. Robbins 	if (ep == *gargv) {
499ab5a295bSJuli Mallett 		warnx2("%s: expected numeric value", *gargv, NULL);
500bacab7d6STim J. Robbins 		rval = 1;
501bacab7d6STim J. Robbins 	}
502bacab7d6STim J. Robbins 	else if (*ep != '\0') {
503ab5a295bSJuli Mallett 		warnx2("%s: not completely converted", *gargv, NULL);
504bacab7d6STim J. Robbins 		rval = 1;
505bacab7d6STim J. Robbins 	}
506bacab7d6STim J. Robbins 	if (errno == ERANGE) {
507ab5a295bSJuli Mallett 		warnx3("%s: %s", *gargv, strerror(ERANGE));
508bacab7d6STim J. Robbins 		rval = 1;
509bacab7d6STim J. Robbins 	}
510ab5a295bSJuli Mallett 	++gargv;
511bacab7d6STim J. Robbins 	return (rval);
5129b50d902SRodney W. Grimes }
5139b50d902SRodney W. Grimes 
514bacab7d6STim J. Robbins static int
515fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl)
5169b50d902SRodney W. Grimes {
517ab5a295bSJuli Mallett 	char *ep;
518bacab7d6STim J. Robbins 	int rval;
519ab5a295bSJuli Mallett 
5205ec2b8dcSStefan Farfeleder 	if (!*gargv) {
5215ec2b8dcSStefan Farfeleder 		*dp = 0.0;
522bacab7d6STim J. Robbins 		return (0);
5235ec2b8dcSStefan Farfeleder 	}
524ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
525bacab7d6STim J. Robbins 		*dp = asciicode();
526bacab7d6STim J. Robbins 		return (0);
527ab5a295bSJuli Mallett 	}
5288c423a99SColin Percival 	rval = 0;
529ab5a295bSJuli Mallett 	errno = 0;
530fd757c50SDavid Schultz 	if (mod_ldbl)
531fd757c50SDavid Schultz 		*dp = strtold(*gargv, &ep);
532fd757c50SDavid Schultz 	else
533bacab7d6STim J. Robbins 		*dp = strtod(*gargv, &ep);
534bacab7d6STim J. Robbins 	if (ep == *gargv) {
535ab5a295bSJuli Mallett 		warnx2("%s: expected numeric value", *gargv, NULL);
536bacab7d6STim J. Robbins 		rval = 1;
537bacab7d6STim J. Robbins 	} else if (*ep != '\0') {
538ab5a295bSJuli Mallett 		warnx2("%s: not completely converted", *gargv, NULL);
539bacab7d6STim J. Robbins 		rval = 1;
540bacab7d6STim J. Robbins 	}
541bacab7d6STim J. Robbins 	if (errno == ERANGE) {
542ab5a295bSJuli Mallett 		warnx3("%s: %s", *gargv, strerror(ERANGE));
543bacab7d6STim J. Robbins 		rval = 1;
544bacab7d6STim J. Robbins 	}
545ab5a295bSJuli Mallett 	++gargv;
546bacab7d6STim J. Robbins 	return (rval);
5479b50d902SRodney W. Grimes }
5489b50d902SRodney W. Grimes 
5499b50d902SRodney W. Grimes static int
550f4ac32deSDavid Malone asciicode(void)
5519b50d902SRodney W. Grimes {
552f4ac32deSDavid Malone 	int ch;
5539b50d902SRodney W. Grimes 
5549b50d902SRodney W. Grimes 	ch = **gargv;
5559b50d902SRodney W. Grimes 	if (ch == '\'' || ch == '"')
5569b50d902SRodney W. Grimes 		ch = (*gargv)[1];
5579b50d902SRodney W. Grimes 	++gargv;
5589b50d902SRodney W. Grimes 	return (ch);
5599b50d902SRodney W. Grimes }
5609b50d902SRodney W. Grimes 
5619b50d902SRodney W. Grimes static void
562f4ac32deSDavid Malone usage(void)
5639b50d902SRodney W. Grimes {
564f682f10cSRuslan Ermilov 	(void)fprintf(stderr, "usage: printf format [arguments ...]\n");
5659b50d902SRodney W. Grimes }
566