xref: /freebsd/usr.bin/printf/printf.c (revision 03b2eaac40ae1226a1e0aae9d77f38f3e73d2c5d)
19b50d902SRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #if !defined(BUILTIN) && !defined(SHELL)
359b50d902SRodney W. Grimes #ifndef lint
363ec30b79SSteve Price static char const copyright[] =
379b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
389b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
399b50d902SRodney W. Grimes #endif /* not lint */
409b50d902SRodney W. Grimes #endif
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #ifndef lint
43c9e05349SWarner Losh #if 0
443ec30b79SSteve Price static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
45c9e05349SWarner Losh #endif
461ea7321bSMartin Cracauer static const char rcsid[] =
471ea7321bSMartin Cracauer   "$FreeBSD$";
489b50d902SRodney W. Grimes #endif /* not lint */
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <sys/types.h>
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes #include <err.h>
539b50d902SRodney W. Grimes #include <errno.h>
549b50d902SRodney W. Grimes #include <limits.h>
559b50d902SRodney W. Grimes #include <stdio.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
589d19feb5SSteve Price #include <unistd.h>
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes #ifdef SHELL
619b50d902SRodney W. Grimes #define main printfcmd
62d9f93710SJoerg Wunsch #include "bltin/bltin.h"
633c6e4a5cSBen Smithurst #include "memalloc.h"
643b53d380SBruce Evans #else
653b53d380SBruce Evans #define	warnx1(a, b, c)		warnx(a)
663b53d380SBruce Evans #define	warnx2(a, b, c)		warnx(a, b)
673b53d380SBruce Evans #define	warnx3(a, b, c)		warnx(a, b, c)
689b50d902SRodney W. Grimes #endif
699b50d902SRodney W. Grimes 
70dc7d8c99SAndrey A. Chernov #ifndef BUILTIN
71dc7d8c99SAndrey A. Chernov #include <locale.h>
72dc7d8c99SAndrey A. Chernov #endif
73dc7d8c99SAndrey A. Chernov 
74bacab7d6STim J. Robbins #define PF(f, func) do { \
75d72f654cSPeter Wemm 	char *b = NULL; \
7698dd6386STim J. Robbins 	if (havewidth) \
7798dd6386STim J. Robbins 		if (haveprec) \
78d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, precision, func); \
799b50d902SRodney W. Grimes 		else \
80d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, func); \
8198dd6386STim J. Robbins 	else if (haveprec) \
82d72f654cSPeter Wemm 		(void)asprintf(&b, f, precision, func); \
839b50d902SRodney W. Grimes 	else \
84d72f654cSPeter Wemm 		(void)asprintf(&b, f, func); \
85d72f654cSPeter Wemm 	if (b) { \
86d72f654cSPeter Wemm 		(void)fputs(b, stdout); \
87d72f654cSPeter Wemm 		free(b); \
88d72f654cSPeter Wemm 	} \
89bacab7d6STim J. Robbins } while (0)
909b50d902SRodney W. Grimes 
91d3cb5dedSWarner Losh static int	 asciicode(void);
9212e8db40STim J. Robbins static int	 escape(char *, int);
93d3cb5dedSWarner Losh static int	 getchr(void);
94bacab7d6STim J. Robbins static int	 getdouble(double *);
95d3cb5dedSWarner Losh static int	 getint(int *);
96bacab7d6STim J. Robbins static int	 getquads(quad_t *, u_quad_t *, int);
97bacab7d6STim J. Robbins static const char
98bacab7d6STim J. Robbins 		*getstr(void);
99bacab7d6STim J. Robbins static char	*mkquad(char *, int);
100d3cb5dedSWarner Losh static void	 usage(void);
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes static char **gargv;
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes int
1059b50d902SRodney W. Grimes #ifdef BUILTIN
106f4ac32deSDavid Malone progprintf(int argc, char *argv[])
1079b50d902SRodney W. Grimes #else
108f4ac32deSDavid Malone main(int argc, char *argv[])
1099b50d902SRodney W. Grimes #endif
1109b50d902SRodney W. Grimes {
11145af1a4cSDavid Malone 	static const char *skip1, *skip2;
11298dd6386STim J. Robbins 	int ch, chopped, end, fieldwidth, haveprec, havewidth, precision, rval;
1139b50d902SRodney W. Grimes 	char convch, nextch, *format, *fmt, *start;
1149b50d902SRodney W. Grimes 
115dc7d8c99SAndrey A. Chernov #ifndef BUILTIN
116dc7d8c99SAndrey A. Chernov 	(void) setlocale(LC_NUMERIC, "");
117dc7d8c99SAndrey A. Chernov #endif
1181c8af878SWarner Losh 	while ((ch = getopt(argc, argv, "")) != -1)
1199b50d902SRodney W. Grimes 		switch (ch) {
1209b50d902SRodney W. Grimes 		case '?':
1219b50d902SRodney W. Grimes 		default:
1229b50d902SRodney W. Grimes 			usage();
1239b50d902SRodney W. Grimes 			return (1);
1249b50d902SRodney W. Grimes 		}
1259b50d902SRodney W. Grimes 	argc -= optind;
1269b50d902SRodney W. Grimes 	argv += optind;
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	if (argc < 1) {
1299b50d902SRodney W. Grimes 		usage();
1309b50d902SRodney W. Grimes 		return (1);
1319b50d902SRodney W. Grimes 	}
1329b50d902SRodney W. Grimes 
1339b50d902SRodney W. Grimes 	/*
1349b50d902SRodney W. Grimes 	 * Basic algorithm is to scan the format string for conversion
1359b50d902SRodney W. Grimes 	 * specifications -- once one is found, find out if the field
1369b50d902SRodney W. Grimes 	 * width or precision is a '*'; if it is, gather up value.  Note,
1379b50d902SRodney W. Grimes 	 * format strings are reused as necessary to use up the provided
1389b50d902SRodney W. Grimes 	 * arguments, arguments of zero/null string are provided to use
1399b50d902SRodney W. Grimes 	 * up the format string.
1409b50d902SRodney W. Grimes 	 */
1419b50d902SRodney W. Grimes 	skip1 = "#-+ 0";
142d867cefdSJoerg Wunsch 	skip2 = "0123456789";
1439b50d902SRodney W. Grimes 
14412e8db40STim J. Robbins 	chopped = escape(fmt = format = *argv, 1);/* backslash interpretation */
145bacab7d6STim J. Robbins 	rval = 0;
1469b50d902SRodney W. Grimes 	gargv = ++argv;
1479b50d902SRodney W. Grimes 	for (;;) {
1489b50d902SRodney W. Grimes 		end = 0;
1499b50d902SRodney W. Grimes 		/* find next format specification */
1509b50d902SRodney W. Grimes next:		for (start = fmt;; ++fmt) {
1519b50d902SRodney W. Grimes 			if (!*fmt) {
1529b50d902SRodney W. Grimes 				/* avoid infinite loop */
153ab5a295bSJuli Mallett 				if (chopped) {
154ab5a295bSJuli Mallett 					(void)printf("%s", start);
155bacab7d6STim J. Robbins 					return (rval);
156ab5a295bSJuli Mallett 				}
1579b50d902SRodney W. Grimes 				if (end == 1) {
1583b53d380SBruce Evans 					warnx1("missing format character",
159b0c9a86dSJohn Polstra 					    NULL, NULL);
1609b50d902SRodney W. Grimes 					return (1);
1619b50d902SRodney W. Grimes 				}
1629b50d902SRodney W. Grimes 				end = 1;
1639b50d902SRodney W. Grimes 				if (fmt > start)
1649b50d902SRodney W. Grimes 					(void)printf("%s", start);
1659b50d902SRodney W. Grimes 				if (!*gargv)
166bacab7d6STim J. Robbins 					return (rval);
1679b50d902SRodney W. Grimes 				fmt = format;
1689b50d902SRodney W. Grimes 				goto next;
1699b50d902SRodney W. Grimes 			}
1709b50d902SRodney W. Grimes 			/* %% prints a % */
1719b50d902SRodney W. Grimes 			if (*fmt == '%') {
1729b50d902SRodney W. Grimes 				if (*++fmt != '%')
1739b50d902SRodney W. Grimes 					break;
1745f19035bSTim J. Robbins 				(void)printf("%.*s", (int)(fmt - start), start);
1755f19035bSTim J. Robbins 				fmt++;
1769b50d902SRodney W. Grimes 				goto next;
1779b50d902SRodney W. Grimes 			}
1789b50d902SRodney W. Grimes 		}
1799b50d902SRodney W. Grimes 
1809b50d902SRodney W. Grimes 		/* skip to field width */
1819b50d902SRodney W. Grimes 		for (; strchr(skip1, *fmt); ++fmt);
1829b50d902SRodney W. Grimes 		if (*fmt == '*') {
1839b50d902SRodney W. Grimes 			if (getint(&fieldwidth))
1849b50d902SRodney W. Grimes 				return (1);
18598dd6386STim J. Robbins 			havewidth = 1;
186d867cefdSJoerg Wunsch 			++fmt;
187d867cefdSJoerg Wunsch 		} else {
18898dd6386STim J. Robbins 			havewidth = 0;
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes 			/* skip to possible '.', get following precision */
1919b50d902SRodney W. Grimes 			for (; strchr(skip2, *fmt); ++fmt);
192d867cefdSJoerg Wunsch 		}
193d867cefdSJoerg Wunsch 		if (*fmt == '.') {
194d867cefdSJoerg Wunsch 			/* precision present? */
1959b50d902SRodney W. Grimes 			++fmt;
1969b50d902SRodney W. Grimes 			if (*fmt == '*') {
1979b50d902SRodney W. Grimes 				if (getint(&precision))
1989b50d902SRodney W. Grimes 					return (1);
19998dd6386STim J. Robbins 				haveprec = 1;
200d867cefdSJoerg Wunsch 				++fmt;
201d867cefdSJoerg Wunsch 			} else {
20298dd6386STim J. Robbins 				haveprec = 0;
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes 				/* skip to conversion char */
2059b50d902SRodney W. Grimes 				for (; strchr(skip2, *fmt); ++fmt);
206d867cefdSJoerg Wunsch 			}
207d867cefdSJoerg Wunsch 		} else
20898dd6386STim J. Robbins 			haveprec = 0;
2099b50d902SRodney W. Grimes 		if (!*fmt) {
2103b53d380SBruce Evans 			warnx1("missing format character", NULL, NULL);
2119b50d902SRodney W. Grimes 			return (1);
2129b50d902SRodney W. Grimes 		}
2139b50d902SRodney W. Grimes 
2149b50d902SRodney W. Grimes 		convch = *fmt;
2159b50d902SRodney W. Grimes 		nextch = *++fmt;
2169b50d902SRodney W. Grimes 		*fmt = '\0';
2179b50d902SRodney W. Grimes 		switch(convch) {
218ab5a295bSJuli Mallett 		case 'b': {
219ab5a295bSJuli Mallett 			char *p;
220ab5a295bSJuli Mallett 			int getout;
221ab5a295bSJuli Mallett 
222bacab7d6STim J. Robbins #ifdef SHELL
223bacab7d6STim J. Robbins 			p = savestr(getstr());
224bacab7d6STim J. Robbins #else
225bacab7d6STim J. Robbins 			p = strdup(getstr());
226bacab7d6STim J. Robbins #endif
227bacab7d6STim J. Robbins 			if (p == NULL) {
228bacab7d6STim J. Robbins 				warnx2("%s", strerror(ENOMEM), NULL);
229bacab7d6STim J. Robbins 				return (1);
230bacab7d6STim J. Robbins 			}
23112e8db40STim J. Robbins 			getout = escape(p, 0);
232ab5a295bSJuli Mallett 			*(fmt - 1) = 's';
233bacab7d6STim J. Robbins 			PF(start, p);
234ab5a295bSJuli Mallett 			*(fmt - 1) = 'b';
235bacab7d6STim J. Robbins #ifdef SHELL
236bacab7d6STim J. Robbins 			ckfree(p);
237bacab7d6STim J. Robbins #else
238ab5a295bSJuli Mallett 			free(p);
239bacab7d6STim J. Robbins #endif
240ab5a295bSJuli Mallett 			if (getout)
241bacab7d6STim J. Robbins 				return (rval);
242ab5a295bSJuli Mallett 			break;
243ab5a295bSJuli Mallett 		}
2449b50d902SRodney W. Grimes 		case 'c': {
2459b50d902SRodney W. Grimes 			char p;
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes 			p = getchr();
2489b50d902SRodney W. Grimes 			PF(start, p);
2499b50d902SRodney W. Grimes 			break;
2509b50d902SRodney W. Grimes 		}
2519b50d902SRodney W. Grimes 		case 's': {
25245af1a4cSDavid Malone 			const char *p;
2539b50d902SRodney W. Grimes 
2549b50d902SRodney W. Grimes 			p = getstr();
2559b50d902SRodney W. Grimes 			PF(start, p);
2569b50d902SRodney W. Grimes 			break;
2579b50d902SRodney W. Grimes 		}
2589b50d902SRodney W. Grimes 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
2599b50d902SRodney W. Grimes 			char *f;
260bacab7d6STim J. Robbins 			quad_t val;
261bacab7d6STim J. Robbins 			u_quad_t uval;
262bacab7d6STim J. Robbins 			int signedconv;
2639b50d902SRodney W. Grimes 
264bacab7d6STim J. Robbins 			signedconv = (convch == 'd' || convch == 'i');
265bacab7d6STim J. Robbins 			if ((f = mkquad(start, convch)) == NULL)
266bacab7d6STim J. Robbins 				return (1);
267bacab7d6STim J. Robbins 			if (getquads(&val, &uval, signedconv))
268bacab7d6STim J. Robbins 				rval = 1;
269bacab7d6STim J. Robbins 			if (signedconv)
270bacab7d6STim J. Robbins 				PF(f, val);
271bacab7d6STim J. Robbins 			else
272bacab7d6STim J. Robbins 				PF(f, uval);
2739b50d902SRodney W. Grimes 			break;
2749b50d902SRodney W. Grimes 		}
27503b2eaacSDavid Schultz 		case 'e': case 'E':
27603b2eaacSDavid Schultz 		case 'f': case 'F':
27703b2eaacSDavid Schultz 		case 'g': case 'G':
27803b2eaacSDavid Schultz 		case 'a': case 'A': {
2799b50d902SRodney W. Grimes 			double p;
2809b50d902SRodney W. Grimes 
281bacab7d6STim J. Robbins 			if (getdouble(&p))
282bacab7d6STim J. Robbins 				rval = 1;
2839b50d902SRodney W. Grimes 			PF(start, p);
2849b50d902SRodney W. Grimes 			break;
2859b50d902SRodney W. Grimes 		}
2869b50d902SRodney W. Grimes 		default:
2873b53d380SBruce Evans 			warnx2("illegal format character %c", convch, NULL);
2889b50d902SRodney W. Grimes 			return (1);
2899b50d902SRodney W. Grimes 		}
2909b50d902SRodney W. Grimes 		*fmt = nextch;
2919b50d902SRodney W. Grimes 	}
2929b50d902SRodney W. Grimes 	/* NOTREACHED */
2939b50d902SRodney W. Grimes }
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes static char *
296f4ac32deSDavid Malone mkquad(char *str, int ch)
2979b50d902SRodney W. Grimes {
2983c6e4a5cSBen Smithurst 	static char *copy;
2993c6e4a5cSBen Smithurst 	static size_t copy_size;
3003c6e4a5cSBen Smithurst 	char *newcopy;
301bacab7d6STim J. Robbins 	size_t len, newlen;
3029b50d902SRodney W. Grimes 
3039b50d902SRodney W. Grimes 	len = strlen(str) + 2;
3043c6e4a5cSBen Smithurst 	if (len > copy_size) {
3053c6e4a5cSBen Smithurst 		newlen = ((len + 1023) >> 10) << 10;
3063c6e4a5cSBen Smithurst #ifdef SHELL
3073c6e4a5cSBen Smithurst 		if ((newcopy = ckrealloc(copy, newlen)) == NULL)
3083c6e4a5cSBen Smithurst #else
3093c6e4a5cSBen Smithurst 		if ((newcopy = realloc(copy, newlen)) == NULL)
3103c6e4a5cSBen Smithurst #endif
311bacab7d6STim J. Robbins 		{
312bacab7d6STim J. Robbins 			warnx2("%s", strerror(ENOMEM), NULL);
3133c6e4a5cSBen Smithurst 			return (NULL);
314bacab7d6STim J. Robbins 		}
3153c6e4a5cSBen Smithurst 		copy = newcopy;
3163c6e4a5cSBen Smithurst 		copy_size = newlen;
3173c6e4a5cSBen Smithurst 	}
31862a721e7SStefan Eßer 
3199b50d902SRodney W. Grimes 	memmove(copy, str, len - 3);
32062a721e7SStefan Eßer 	copy[len - 3] = 'q';
3219b50d902SRodney W. Grimes 	copy[len - 2] = ch;
3229b50d902SRodney W. Grimes 	copy[len - 1] = '\0';
3239b50d902SRodney W. Grimes 	return (copy);
3249b50d902SRodney W. Grimes }
3259b50d902SRodney W. Grimes 
326ab5a295bSJuli Mallett static int
327f4ac32deSDavid Malone escape(char *fmt, int percent)
3289b50d902SRodney W. Grimes {
329f4ac32deSDavid Malone 	char *store;
330f4ac32deSDavid Malone 	int value, c;
3319b50d902SRodney W. Grimes 
332e6068a34SSteve Price 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
3339b50d902SRodney W. Grimes 		if (c != '\\') {
3349b50d902SRodney W. Grimes 			*store = c;
3359b50d902SRodney W. Grimes 			continue;
3369b50d902SRodney W. Grimes 		}
3379b50d902SRodney W. Grimes 		switch (*++fmt) {
3389b50d902SRodney W. Grimes 		case '\0':		/* EOS, user error */
3399b50d902SRodney W. Grimes 			*store = '\\';
3409b50d902SRodney W. Grimes 			*++store = '\0';
341ab5a295bSJuli Mallett 			return (0);
3429b50d902SRodney W. Grimes 		case '\\':		/* backslash */
3439b50d902SRodney W. Grimes 		case '\'':		/* single quote */
3449b50d902SRodney W. Grimes 			*store = *fmt;
3459b50d902SRodney W. Grimes 			break;
3469b50d902SRodney W. Grimes 		case 'a':		/* bell/alert */
3479b50d902SRodney W. Grimes 			*store = '\7';
3489b50d902SRodney W. Grimes 			break;
3499b50d902SRodney W. Grimes 		case 'b':		/* backspace */
3509b50d902SRodney W. Grimes 			*store = '\b';
3519b50d902SRodney W. Grimes 			break;
352ab5a295bSJuli Mallett 		case 'c':
353ab5a295bSJuli Mallett 			*store = '\0';
354ab5a295bSJuli Mallett 			return (1);
3559b50d902SRodney W. Grimes 		case 'f':		/* form-feed */
3569b50d902SRodney W. Grimes 			*store = '\f';
3579b50d902SRodney W. Grimes 			break;
3589b50d902SRodney W. Grimes 		case 'n':		/* newline */
3599b50d902SRodney W. Grimes 			*store = '\n';
3609b50d902SRodney W. Grimes 			break;
3619b50d902SRodney W. Grimes 		case 'r':		/* carriage-return */
3629b50d902SRodney W. Grimes 			*store = '\r';
3639b50d902SRodney W. Grimes 			break;
3649b50d902SRodney W. Grimes 		case 't':		/* horizontal tab */
3659b50d902SRodney W. Grimes 			*store = '\t';
3669b50d902SRodney W. Grimes 			break;
3679b50d902SRodney W. Grimes 		case 'v':		/* vertical tab */
3689b50d902SRodney W. Grimes 			*store = '\13';
3699b50d902SRodney W. Grimes 			break;
3709b50d902SRodney W. Grimes 					/* octal constant */
3719b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3':
3729b50d902SRodney W. Grimes 		case '4': case '5': case '6': case '7':
373ab5a295bSJuli Mallett 			for (c = *fmt == '0' ? 4 : 3, value = 0;
3749b50d902SRodney W. Grimes 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
3759b50d902SRodney W. Grimes 				value <<= 3;
3769b50d902SRodney W. Grimes 				value += *fmt - '0';
3779b50d902SRodney W. Grimes 			}
3789b50d902SRodney W. Grimes 			--fmt;
37912e8db40STim J. Robbins 			if (percent && value == '%') {
38037fd4590STim J. Robbins 				*store++ = '%';
38137fd4590STim J. Robbins 				*store = '%';
38237fd4590STim J. Robbins 			} else
3839b50d902SRodney W. Grimes 				*store = value;
3849b50d902SRodney W. Grimes 			break;
3859b50d902SRodney W. Grimes 		default:
3869b50d902SRodney W. Grimes 			*store = *fmt;
3879b50d902SRodney W. Grimes 			break;
3889b50d902SRodney W. Grimes 		}
3899b50d902SRodney W. Grimes 	}
3909b50d902SRodney W. Grimes 	*store = '\0';
391ab5a295bSJuli Mallett 	return (0);
3929b50d902SRodney W. Grimes }
3939b50d902SRodney W. Grimes 
3949b50d902SRodney W. Grimes static int
395f4ac32deSDavid Malone getchr(void)
3969b50d902SRodney W. Grimes {
3979b50d902SRodney W. Grimes 	if (!*gargv)
3989b50d902SRodney W. Grimes 		return ('\0');
3999b50d902SRodney W. Grimes 	return ((int)**gargv++);
4009b50d902SRodney W. Grimes }
4019b50d902SRodney W. Grimes 
40245af1a4cSDavid Malone static const char *
403f4ac32deSDavid Malone getstr(void)
4049b50d902SRodney W. Grimes {
4059b50d902SRodney W. Grimes 	if (!*gargv)
4069b50d902SRodney W. Grimes 		return ("");
4079b50d902SRodney W. Grimes 	return (*gargv++);
4089b50d902SRodney W. Grimes }
4099b50d902SRodney W. Grimes 
4109b50d902SRodney W. Grimes static int
411f4ac32deSDavid Malone getint(int *ip)
4129b50d902SRodney W. Grimes {
41362a721e7SStefan Eßer 	quad_t val;
414bacab7d6STim J. Robbins 	u_quad_t uval;
415bacab7d6STim J. Robbins 	int rval;
4169b50d902SRodney W. Grimes 
417bacab7d6STim J. Robbins 	if (getquads(&val, &uval, 1))
4189b50d902SRodney W. Grimes 		return (1);
419bacab7d6STim J. Robbins 	rval = 0;
420bacab7d6STim J. Robbins 	if (val < INT_MIN || val > INT_MAX) {
4213b53d380SBruce Evans 		warnx3("%s: %s", *gargv, strerror(ERANGE));
422bacab7d6STim J. Robbins 		rval = 1;
423bacab7d6STim J. Robbins 	}
42462a721e7SStefan Eßer 	*ip = (int)val;
425bacab7d6STim J. Robbins 	return (rval);
4269b50d902SRodney W. Grimes }
4279b50d902SRodney W. Grimes 
4289b50d902SRodney W. Grimes static int
429f4ac32deSDavid Malone getquads(quad_t *qp, u_quad_t *uqp, int signedconv)
4309b50d902SRodney W. Grimes {
4319b50d902SRodney W. Grimes 	char *ep;
432bacab7d6STim J. Robbins 	int rval;
4339b50d902SRodney W. Grimes 
4349b50d902SRodney W. Grimes 	if (!*gargv) {
435bacab7d6STim J. Robbins 		*qp = 0;
4369b50d902SRodney W. Grimes 		return (0);
4379b50d902SRodney W. Grimes 	}
438ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
439bacab7d6STim J. Robbins 		if (signedconv)
440bacab7d6STim J. Robbins 			*qp = asciicode();
441bacab7d6STim J. Robbins 		else
442bacab7d6STim J. Robbins 			*uqp = asciicode();
4439b50d902SRodney W. Grimes 		return (0);
4449b50d902SRodney W. Grimes 	}
445bacab7d6STim J. Robbins 	rval = 0;
446ab5a295bSJuli Mallett 	errno = 0;
447bacab7d6STim J. Robbins 	if (signedconv)
448bacab7d6STim J. Robbins 		*qp = strtoq(*gargv, &ep, 0);
449bacab7d6STim J. Robbins 	else
450bacab7d6STim J. Robbins 		*uqp = strtouq(*gargv, &ep, 0);
451bacab7d6STim J. Robbins 	if (ep == *gargv) {
452ab5a295bSJuli Mallett 		warnx2("%s: expected numeric value", *gargv, NULL);
453bacab7d6STim J. Robbins 		rval = 1;
454bacab7d6STim J. Robbins 	}
455bacab7d6STim J. Robbins 	else if (*ep != '\0') {
456ab5a295bSJuli Mallett 		warnx2("%s: not completely converted", *gargv, NULL);
457bacab7d6STim J. Robbins 		rval = 1;
458bacab7d6STim J. Robbins 	}
459bacab7d6STim J. Robbins 	if (errno == ERANGE) {
460ab5a295bSJuli Mallett 		warnx3("%s: %s", *gargv, strerror(ERANGE));
461bacab7d6STim J. Robbins 		rval = 1;
462bacab7d6STim J. Robbins 	}
463ab5a295bSJuli Mallett 	++gargv;
464bacab7d6STim J. Robbins 	return (rval);
4659b50d902SRodney W. Grimes }
4669b50d902SRodney W. Grimes 
467bacab7d6STim J. Robbins static int
468f4ac32deSDavid Malone getdouble(double *dp)
4699b50d902SRodney W. Grimes {
470ab5a295bSJuli Mallett 	char *ep;
471bacab7d6STim J. Robbins 	int rval;
472ab5a295bSJuli Mallett 
4739b50d902SRodney W. Grimes 	if (!*gargv)
474bacab7d6STim J. Robbins 		return (0);
475ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
476bacab7d6STim J. Robbins 		*dp = asciicode();
477bacab7d6STim J. Robbins 		return (0);
478ab5a295bSJuli Mallett 	}
4798c423a99SColin Percival 	rval = 0;
480ab5a295bSJuli Mallett 	errno = 0;
481bacab7d6STim J. Robbins 	*dp = strtod(*gargv, &ep);
482bacab7d6STim J. Robbins 	if (ep == *gargv) {
483ab5a295bSJuli Mallett 		warnx2("%s: expected numeric value", *gargv, NULL);
484bacab7d6STim J. Robbins 		rval = 1;
485bacab7d6STim J. Robbins 	} else if (*ep != '\0') {
486ab5a295bSJuli Mallett 		warnx2("%s: not completely converted", *gargv, NULL);
487bacab7d6STim J. Robbins 		rval = 1;
488bacab7d6STim J. Robbins 	}
489bacab7d6STim J. Robbins 	if (errno == ERANGE) {
490ab5a295bSJuli Mallett 		warnx3("%s: %s", *gargv, strerror(ERANGE));
491bacab7d6STim J. Robbins 		rval = 1;
492bacab7d6STim J. Robbins 	}
493ab5a295bSJuli Mallett 	++gargv;
494bacab7d6STim J. Robbins 	return (rval);
4959b50d902SRodney W. Grimes }
4969b50d902SRodney W. Grimes 
4979b50d902SRodney W. Grimes static int
498f4ac32deSDavid Malone asciicode(void)
4999b50d902SRodney W. Grimes {
500f4ac32deSDavid Malone 	int ch;
5019b50d902SRodney W. Grimes 
5029b50d902SRodney W. Grimes 	ch = **gargv;
5039b50d902SRodney W. Grimes 	if (ch == '\'' || ch == '"')
5049b50d902SRodney W. Grimes 		ch = (*gargv)[1];
5059b50d902SRodney W. Grimes 	++gargv;
5069b50d902SRodney W. Grimes 	return (ch);
5079b50d902SRodney W. Grimes }
5089b50d902SRodney W. Grimes 
5099b50d902SRodney W. Grimes static void
510f4ac32deSDavid Malone usage(void)
5119b50d902SRodney W. Grimes {
5129b50d902SRodney W. Grimes 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
5139b50d902SRodney W. Grimes }
514