xref: /freebsd/usr.bin/printf/printf.c (revision f3f148d238851e68b0ec00daca83c0c0dec7ee03)
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);
92fbd08684SStefan Farfeleder static char	*doformat(char *, int *);
9312e8db40STim J. Robbins static int	 escape(char *, int);
94d3cb5dedSWarner Losh static int	 getchr(void);
95fd757c50SDavid Schultz static int	 getfloating(long double *, int);
96d3cb5dedSWarner Losh static int	 getint(int *);
97bacab7d6STim J. Robbins static int	 getquads(quad_t *, u_quad_t *, int);
98bacab7d6STim J. Robbins static const char
99bacab7d6STim J. Robbins 		*getstr(void);
100bacab7d6STim J. Robbins static char	*mkquad(char *, int);
101d3cb5dedSWarner Losh static void	 usage(void);
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes static char **gargv;
1049b50d902SRodney W. Grimes 
1059b50d902SRodney W. Grimes int
1069b50d902SRodney W. Grimes #ifdef BUILTIN
107f4ac32deSDavid Malone progprintf(int argc, char *argv[])
1089b50d902SRodney W. Grimes #else
109f4ac32deSDavid Malone main(int argc, char *argv[])
1109b50d902SRodney W. Grimes #endif
1119b50d902SRodney W. Grimes {
112fbd08684SStefan Farfeleder 	int ch, chopped, end, rval;
113fbd08684SStefan Farfeleder 	char *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 	 */
14112e8db40STim J. Robbins 	chopped = escape(fmt = format = *argv, 1);/* backslash interpretation */
142fbd08684SStefan Farfeleder 	rval = end = 0;
1439b50d902SRodney W. Grimes 	gargv = ++argv;
1449b50d902SRodney W. Grimes 	for (;;) {
145fbd08684SStefan Farfeleder 		start = fmt;
146fbd08684SStefan Farfeleder 		while (*fmt != '\0') {
147fbd08684SStefan Farfeleder 			if (fmt[0] == '%') {
148fbd08684SStefan Farfeleder 				fwrite(start, 1, fmt - start, stdout);
149fbd08684SStefan Farfeleder 				if (fmt[1] == '%') {
1509b50d902SRodney W. Grimes 					/* %% prints a % */
151fbd08684SStefan Farfeleder 					putchar('%');
152fbd08684SStefan Farfeleder 					fmt += 2;
153fbd08684SStefan Farfeleder 				} else {
154fbd08684SStefan Farfeleder 					fmt = doformat(fmt, &rval);
155fbd08684SStefan Farfeleder 					if (fmt == NULL)
156fbd08684SStefan Farfeleder 						return (1);
157fbd08684SStefan Farfeleder 					end = 0;
1589b50d902SRodney W. Grimes 				}
159fbd08684SStefan Farfeleder 				start = fmt;
160fbd08684SStefan Farfeleder 			} else
161fbd08684SStefan Farfeleder 				fmt++;
1629b50d902SRodney W. Grimes 		}
1639b50d902SRodney W. Grimes 
164fbd08684SStefan Farfeleder 		if (end == 1) {
165fbd08684SStefan Farfeleder 			warnx1("missing format character", NULL, NULL);
166fbd08684SStefan Farfeleder 			return (1);
167fbd08684SStefan Farfeleder 		}
168fbd08684SStefan Farfeleder 		fwrite(start, 1, fmt - start, stdout);
169fbd08684SStefan Farfeleder 		if (chopped || !*gargv)
170fbd08684SStefan Farfeleder 			return (rval);
171fbd08684SStefan Farfeleder 		/* Restart at the beginning of the format string. */
172fbd08684SStefan Farfeleder 		fmt = format;
173fbd08684SStefan Farfeleder 		end = 1;
174fbd08684SStefan Farfeleder 	}
175fbd08684SStefan Farfeleder 	/* NOTREACHED */
176fbd08684SStefan Farfeleder }
177fbd08684SStefan Farfeleder 
178fbd08684SStefan Farfeleder 
179fbd08684SStefan Farfeleder static char *
180fbd08684SStefan Farfeleder doformat(char *start, int *rval)
181fbd08684SStefan Farfeleder {
182fbd08684SStefan Farfeleder 	static const char skip1[] = "#'-+ 0";
183fbd08684SStefan Farfeleder 	static const char skip2[] = "0123456789";
184fbd08684SStefan Farfeleder 	char *fmt;
185fbd08684SStefan Farfeleder 	int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
186fbd08684SStefan Farfeleder 	char convch, nextch;
187fbd08684SStefan Farfeleder 
188fbd08684SStefan Farfeleder 	fmt = start + 1;
1899b50d902SRodney W. Grimes 	/* skip to field width */
1900ba01198SStefan Farfeleder 	fmt += strspn(fmt, skip1);
1919b50d902SRodney W. Grimes 	if (*fmt == '*') {
1929b50d902SRodney W. Grimes 		if (getint(&fieldwidth))
193fbd08684SStefan Farfeleder 			return (NULL);
19498dd6386STim J. Robbins 		havewidth = 1;
195d867cefdSJoerg Wunsch 		++fmt;
196d867cefdSJoerg Wunsch 	} else {
19798dd6386STim J. Robbins 		havewidth = 0;
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 		/* skip to possible '.', get following precision */
2000ba01198SStefan Farfeleder 		fmt += strspn(fmt, skip2);
201d867cefdSJoerg Wunsch 	}
202d867cefdSJoerg Wunsch 	if (*fmt == '.') {
203d867cefdSJoerg Wunsch 		/* precision present? */
2049b50d902SRodney W. Grimes 		++fmt;
2059b50d902SRodney W. Grimes 		if (*fmt == '*') {
2069b50d902SRodney W. Grimes 			if (getint(&precision))
207fbd08684SStefan Farfeleder 				return (NULL);
20898dd6386STim J. Robbins 			haveprec = 1;
209d867cefdSJoerg Wunsch 			++fmt;
210d867cefdSJoerg Wunsch 		} else {
21198dd6386STim J. Robbins 			haveprec = 0;
2129b50d902SRodney W. Grimes 
2139b50d902SRodney W. Grimes 			/* skip to conversion char */
2140ba01198SStefan Farfeleder 			fmt += strspn(fmt, skip2);
215d867cefdSJoerg Wunsch 		}
216d867cefdSJoerg Wunsch 	} else
21798dd6386STim J. Robbins 		haveprec = 0;
2189b50d902SRodney W. Grimes 	if (!*fmt) {
2193b53d380SBruce Evans 		warnx1("missing format character", NULL, NULL);
220fbd08684SStefan Farfeleder 		return (NULL);
2219b50d902SRodney W. Grimes 	}
2229b50d902SRodney W. Grimes 
223fd757c50SDavid Schultz 	/*
224fd757c50SDavid Schultz 	 * Look for a length modifier.  POSIX doesn't have these, so
225fd757c50SDavid Schultz 	 * we only support them for floating-point conversions, which
226fd757c50SDavid Schultz 	 * are extensions.  This is useful because the L modifier can
227fd757c50SDavid Schultz 	 * be used to gain extra range and precision, while omitting
228fd757c50SDavid Schultz 	 * it is more likely to produce consistent results on different
229fd757c50SDavid Schultz 	 * architectures.  This is not so important for integers
230fd757c50SDavid Schultz 	 * because overflow is the only bad thing that can happen to
231fd757c50SDavid Schultz 	 * them, but consider the command  printf %a 1.1
232fd757c50SDavid Schultz 	 */
233fd757c50SDavid Schultz 	if (*fmt == 'L') {
234fd757c50SDavid Schultz 		mod_ldbl = 1;
235fd757c50SDavid Schultz 		fmt++;
236fd757c50SDavid Schultz 		if (!strchr("aAeEfFgG", *fmt)) {
237fd757c50SDavid Schultz 			warnx2("bad modifier L for %%%c", *fmt, NULL);
238fbd08684SStefan Farfeleder 			return (NULL);
239fd757c50SDavid Schultz 		}
240fd757c50SDavid Schultz 	} else {
241fd757c50SDavid Schultz 		mod_ldbl = 0;
242fd757c50SDavid Schultz 	}
243fd757c50SDavid Schultz 
2449b50d902SRodney W. Grimes 	convch = *fmt;
2459b50d902SRodney W. Grimes 	nextch = *++fmt;
2469b50d902SRodney W. Grimes 	*fmt = '\0';
2479b50d902SRodney W. Grimes 	switch (convch) {
248ab5a295bSJuli Mallett 	case 'b': {
249ab5a295bSJuli Mallett 		char *p;
250ab5a295bSJuli Mallett 		int getout;
251ab5a295bSJuli Mallett 
252bacab7d6STim J. Robbins #ifdef SHELL
253bacab7d6STim J. Robbins 		p = savestr(getstr());
254bacab7d6STim J. Robbins #else
255bacab7d6STim J. Robbins 		p = strdup(getstr());
256bacab7d6STim J. Robbins #endif
257bacab7d6STim J. Robbins 		if (p == NULL) {
258bacab7d6STim J. Robbins 			warnx2("%s", strerror(ENOMEM), NULL);
259fbd08684SStefan Farfeleder 			return (NULL);
260bacab7d6STim J. Robbins 		}
26112e8db40STim J. Robbins 		getout = escape(p, 0);
262ab5a295bSJuli Mallett 		*(fmt - 1) = 's';
263bacab7d6STim J. Robbins 		PF(start, p);
264ab5a295bSJuli Mallett 		*(fmt - 1) = 'b';
265bacab7d6STim J. Robbins #ifdef SHELL
266bacab7d6STim J. Robbins 		ckfree(p);
267bacab7d6STim J. Robbins #else
268ab5a295bSJuli Mallett 		free(p);
269bacab7d6STim J. Robbins #endif
270ab5a295bSJuli Mallett 		if (getout)
271fbd08684SStefan Farfeleder 			return (fmt);
272ab5a295bSJuli Mallett 		break;
273ab5a295bSJuli Mallett 	}
2749b50d902SRodney W. Grimes 	case 'c': {
2759b50d902SRodney W. Grimes 		char p;
2769b50d902SRodney W. Grimes 
2779b50d902SRodney W. Grimes 		p = getchr();
2789b50d902SRodney W. Grimes 		PF(start, p);
2799b50d902SRodney W. Grimes 		break;
2809b50d902SRodney W. Grimes 	}
2819b50d902SRodney W. Grimes 	case 's': {
28245af1a4cSDavid Malone 		const char *p;
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes 		p = getstr();
2859b50d902SRodney W. Grimes 		PF(start, p);
2869b50d902SRodney W. Grimes 		break;
2879b50d902SRodney W. Grimes 	}
2889b50d902SRodney W. Grimes 	case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
2899b50d902SRodney W. Grimes 		char *f;
290bacab7d6STim J. Robbins 		quad_t val;
291bacab7d6STim J. Robbins 		u_quad_t uval;
292bacab7d6STim J. Robbins 		int signedconv;
2939b50d902SRodney W. Grimes 
294bacab7d6STim J. Robbins 		signedconv = (convch == 'd' || convch == 'i');
295bacab7d6STim J. Robbins 		if ((f = mkquad(start, convch)) == NULL)
296fbd08684SStefan Farfeleder 			return (NULL);
297bacab7d6STim J. Robbins 		if (getquads(&val, &uval, signedconv))
298fbd08684SStefan Farfeleder 			*rval = 1;
299bacab7d6STim J. Robbins 		if (signedconv)
300bacab7d6STim J. Robbins 			PF(f, val);
301bacab7d6STim J. Robbins 		else
302bacab7d6STim J. Robbins 			PF(f, uval);
3039b50d902SRodney W. Grimes 		break;
3049b50d902SRodney W. Grimes 	}
30503b2eaacSDavid Schultz 	case 'e': case 'E':
30603b2eaacSDavid Schultz 	case 'f': case 'F':
30703b2eaacSDavid Schultz 	case 'g': case 'G':
30803b2eaacSDavid Schultz 	case 'a': case 'A': {
309fd757c50SDavid Schultz 		long double p;
3109b50d902SRodney W. Grimes 
311fd757c50SDavid Schultz 		if (getfloating(&p, mod_ldbl))
312fbd08684SStefan Farfeleder 			*rval = 1;
313fd757c50SDavid Schultz 		if (mod_ldbl)
3149b50d902SRodney W. Grimes 			PF(start, p);
315fd757c50SDavid Schultz 		else
316fd757c50SDavid Schultz 			PF(start, (double)p);
3179b50d902SRodney W. Grimes 		break;
3189b50d902SRodney W. Grimes 	}
3199b50d902SRodney W. Grimes 	default:
3203b53d380SBruce Evans 		warnx2("illegal format character %c", convch, NULL);
321fbd08684SStefan Farfeleder 		return (NULL);
3229b50d902SRodney W. Grimes 	}
3239b50d902SRodney W. Grimes 	*fmt = nextch;
324fbd08684SStefan Farfeleder 	return (fmt);
3259b50d902SRodney W. Grimes }
3269b50d902SRodney W. Grimes 
3279b50d902SRodney W. Grimes static char *
328f4ac32deSDavid Malone mkquad(char *str, int ch)
3299b50d902SRodney W. Grimes {
3303c6e4a5cSBen Smithurst 	static char *copy;
3313c6e4a5cSBen Smithurst 	static size_t copy_size;
3323c6e4a5cSBen Smithurst 	char *newcopy;
333bacab7d6STim J. Robbins 	size_t len, newlen;
3349b50d902SRodney W. Grimes 
3359b50d902SRodney W. Grimes 	len = strlen(str) + 2;
3363c6e4a5cSBen Smithurst 	if (len > copy_size) {
3373c6e4a5cSBen Smithurst 		newlen = ((len + 1023) >> 10) << 10;
3383c6e4a5cSBen Smithurst #ifdef SHELL
3393c6e4a5cSBen Smithurst 		if ((newcopy = ckrealloc(copy, newlen)) == NULL)
3403c6e4a5cSBen Smithurst #else
3413c6e4a5cSBen Smithurst 		if ((newcopy = realloc(copy, newlen)) == NULL)
3423c6e4a5cSBen Smithurst #endif
343bacab7d6STim J. Robbins 		{
344bacab7d6STim J. Robbins 			warnx2("%s", strerror(ENOMEM), NULL);
3453c6e4a5cSBen Smithurst 			return (NULL);
346bacab7d6STim J. Robbins 		}
3473c6e4a5cSBen Smithurst 		copy = newcopy;
3483c6e4a5cSBen Smithurst 		copy_size = newlen;
3493c6e4a5cSBen Smithurst 	}
35062a721e7SStefan Eßer 
3519b50d902SRodney W. Grimes 	memmove(copy, str, len - 3);
35262a721e7SStefan Eßer 	copy[len - 3] = 'q';
3539b50d902SRodney W. Grimes 	copy[len - 2] = ch;
3549b50d902SRodney W. Grimes 	copy[len - 1] = '\0';
3559b50d902SRodney W. Grimes 	return (copy);
3569b50d902SRodney W. Grimes }
3579b50d902SRodney W. Grimes 
358ab5a295bSJuli Mallett static int
359f4ac32deSDavid Malone escape(char *fmt, int percent)
3609b50d902SRodney W. Grimes {
361f4ac32deSDavid Malone 	char *store;
362f4ac32deSDavid Malone 	int value, c;
3639b50d902SRodney W. Grimes 
364e6068a34SSteve Price 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
3659b50d902SRodney W. Grimes 		if (c != '\\') {
3669b50d902SRodney W. Grimes 			*store = c;
3679b50d902SRodney W. Grimes 			continue;
3689b50d902SRodney W. Grimes 		}
3699b50d902SRodney W. Grimes 		switch (*++fmt) {
3709b50d902SRodney W. Grimes 		case '\0':		/* EOS, user error */
3719b50d902SRodney W. Grimes 			*store = '\\';
3729b50d902SRodney W. Grimes 			*++store = '\0';
373ab5a295bSJuli Mallett 			return (0);
3749b50d902SRodney W. Grimes 		case '\\':		/* backslash */
3759b50d902SRodney W. Grimes 		case '\'':		/* single quote */
3769b50d902SRodney W. Grimes 			*store = *fmt;
3779b50d902SRodney W. Grimes 			break;
3789b50d902SRodney W. Grimes 		case 'a':		/* bell/alert */
379f3f148d2SStefan Farfeleder 			*store = '\a';
3809b50d902SRodney W. Grimes 			break;
3819b50d902SRodney W. Grimes 		case 'b':		/* backspace */
3829b50d902SRodney W. Grimes 			*store = '\b';
3839b50d902SRodney W. Grimes 			break;
384ab5a295bSJuli Mallett 		case 'c':
385ab5a295bSJuli Mallett 			*store = '\0';
386ab5a295bSJuli Mallett 			return (1);
3879b50d902SRodney W. Grimes 		case 'f':		/* form-feed */
3889b50d902SRodney W. Grimes 			*store = '\f';
3899b50d902SRodney W. Grimes 			break;
3909b50d902SRodney W. Grimes 		case 'n':		/* newline */
3919b50d902SRodney W. Grimes 			*store = '\n';
3929b50d902SRodney W. Grimes 			break;
3939b50d902SRodney W. Grimes 		case 'r':		/* carriage-return */
3949b50d902SRodney W. Grimes 			*store = '\r';
3959b50d902SRodney W. Grimes 			break;
3969b50d902SRodney W. Grimes 		case 't':		/* horizontal tab */
3979b50d902SRodney W. Grimes 			*store = '\t';
3989b50d902SRodney W. Grimes 			break;
3999b50d902SRodney W. Grimes 		case 'v':		/* vertical tab */
400f3f148d2SStefan Farfeleder 			*store = '\v';
4019b50d902SRodney W. Grimes 			break;
4029b50d902SRodney W. Grimes 					/* octal constant */
4039b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3':
4049b50d902SRodney W. Grimes 		case '4': case '5': case '6': case '7':
405ab5a295bSJuli Mallett 			for (c = *fmt == '0' ? 4 : 3, value = 0;
4069b50d902SRodney W. Grimes 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
4079b50d902SRodney W. Grimes 				value <<= 3;
4089b50d902SRodney W. Grimes 				value += *fmt - '0';
4099b50d902SRodney W. Grimes 			}
4109b50d902SRodney W. Grimes 			--fmt;
41112e8db40STim J. Robbins 			if (percent && value == '%') {
41237fd4590STim J. Robbins 				*store++ = '%';
41337fd4590STim J. Robbins 				*store = '%';
41437fd4590STim J. Robbins 			} else
4159b50d902SRodney W. Grimes 				*store = value;
4169b50d902SRodney W. Grimes 			break;
4179b50d902SRodney W. Grimes 		default:
4189b50d902SRodney W. Grimes 			*store = *fmt;
4199b50d902SRodney W. Grimes 			break;
4209b50d902SRodney W. Grimes 		}
4219b50d902SRodney W. Grimes 	}
4229b50d902SRodney W. Grimes 	*store = '\0';
423ab5a295bSJuli Mallett 	return (0);
4249b50d902SRodney W. Grimes }
4259b50d902SRodney W. Grimes 
4269b50d902SRodney W. Grimes static int
427f4ac32deSDavid Malone getchr(void)
4289b50d902SRodney W. Grimes {
4299b50d902SRodney W. Grimes 	if (!*gargv)
4309b50d902SRodney W. Grimes 		return ('\0');
4319b50d902SRodney W. Grimes 	return ((int)**gargv++);
4329b50d902SRodney W. Grimes }
4339b50d902SRodney W. Grimes 
43445af1a4cSDavid Malone static const char *
435f4ac32deSDavid Malone getstr(void)
4369b50d902SRodney W. Grimes {
4379b50d902SRodney W. Grimes 	if (!*gargv)
4389b50d902SRodney W. Grimes 		return ("");
4399b50d902SRodney W. Grimes 	return (*gargv++);
4409b50d902SRodney W. Grimes }
4419b50d902SRodney W. Grimes 
4429b50d902SRodney W. Grimes static int
443f4ac32deSDavid Malone getint(int *ip)
4449b50d902SRodney W. Grimes {
44562a721e7SStefan Eßer 	quad_t val;
446bacab7d6STim J. Robbins 	u_quad_t uval;
447bacab7d6STim J. Robbins 	int rval;
4489b50d902SRodney W. Grimes 
449bacab7d6STim J. Robbins 	if (getquads(&val, &uval, 1))
4509b50d902SRodney W. Grimes 		return (1);
451bacab7d6STim J. Robbins 	rval = 0;
452bacab7d6STim J. Robbins 	if (val < INT_MIN || val > INT_MAX) {
4533b53d380SBruce Evans 		warnx3("%s: %s", *gargv, strerror(ERANGE));
454bacab7d6STim J. Robbins 		rval = 1;
455bacab7d6STim J. Robbins 	}
45662a721e7SStefan Eßer 	*ip = (int)val;
457bacab7d6STim J. Robbins 	return (rval);
4589b50d902SRodney W. Grimes }
4599b50d902SRodney W. Grimes 
4609b50d902SRodney W. Grimes static int
461f4ac32deSDavid Malone getquads(quad_t *qp, u_quad_t *uqp, int signedconv)
4629b50d902SRodney W. Grimes {
4639b50d902SRodney W. Grimes 	char *ep;
464bacab7d6STim J. Robbins 	int rval;
4659b50d902SRodney W. Grimes 
4669b50d902SRodney W. Grimes 	if (!*gargv) {
467bacab7d6STim J. Robbins 		*qp = 0;
4689b50d902SRodney W. Grimes 		return (0);
4699b50d902SRodney W. Grimes 	}
470ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
471bacab7d6STim J. Robbins 		if (signedconv)
472bacab7d6STim J. Robbins 			*qp = asciicode();
473bacab7d6STim J. Robbins 		else
474bacab7d6STim J. Robbins 			*uqp = asciicode();
4759b50d902SRodney W. Grimes 		return (0);
4769b50d902SRodney W. Grimes 	}
477bacab7d6STim J. Robbins 	rval = 0;
478ab5a295bSJuli Mallett 	errno = 0;
479bacab7d6STim J. Robbins 	if (signedconv)
480bacab7d6STim J. Robbins 		*qp = strtoq(*gargv, &ep, 0);
481bacab7d6STim J. Robbins 	else
482bacab7d6STim J. Robbins 		*uqp = strtouq(*gargv, &ep, 0);
483bacab7d6STim J. Robbins 	if (ep == *gargv) {
484ab5a295bSJuli Mallett 		warnx2("%s: expected numeric value", *gargv, NULL);
485bacab7d6STim J. Robbins 		rval = 1;
486bacab7d6STim J. Robbins 	}
487bacab7d6STim J. Robbins 	else if (*ep != '\0') {
488ab5a295bSJuli Mallett 		warnx2("%s: not completely converted", *gargv, NULL);
489bacab7d6STim J. Robbins 		rval = 1;
490bacab7d6STim J. Robbins 	}
491bacab7d6STim J. Robbins 	if (errno == ERANGE) {
492ab5a295bSJuli Mallett 		warnx3("%s: %s", *gargv, strerror(ERANGE));
493bacab7d6STim J. Robbins 		rval = 1;
494bacab7d6STim J. Robbins 	}
495ab5a295bSJuli Mallett 	++gargv;
496bacab7d6STim J. Robbins 	return (rval);
4979b50d902SRodney W. Grimes }
4989b50d902SRodney W. Grimes 
499bacab7d6STim J. Robbins static int
500fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl)
5019b50d902SRodney W. Grimes {
502ab5a295bSJuli Mallett 	char *ep;
503bacab7d6STim J. Robbins 	int rval;
504ab5a295bSJuli Mallett 
5055ec2b8dcSStefan Farfeleder 	if (!*gargv) {
5065ec2b8dcSStefan Farfeleder 		*dp = 0.0;
507bacab7d6STim J. Robbins 		return (0);
5085ec2b8dcSStefan Farfeleder 	}
509ab5a295bSJuli Mallett 	if (**gargv == '"' || **gargv == '\'') {
510bacab7d6STim J. Robbins 		*dp = asciicode();
511bacab7d6STim J. Robbins 		return (0);
512ab5a295bSJuli Mallett 	}
5138c423a99SColin Percival 	rval = 0;
514ab5a295bSJuli Mallett 	errno = 0;
515fd757c50SDavid Schultz 	if (mod_ldbl)
516fd757c50SDavid Schultz 		*dp = strtold(*gargv, &ep);
517fd757c50SDavid Schultz 	else
518bacab7d6STim J. Robbins 		*dp = strtod(*gargv, &ep);
519bacab7d6STim J. Robbins 	if (ep == *gargv) {
520ab5a295bSJuli Mallett 		warnx2("%s: expected numeric value", *gargv, NULL);
521bacab7d6STim J. Robbins 		rval = 1;
522bacab7d6STim J. Robbins 	} else if (*ep != '\0') {
523ab5a295bSJuli Mallett 		warnx2("%s: not completely converted", *gargv, NULL);
524bacab7d6STim J. Robbins 		rval = 1;
525bacab7d6STim J. Robbins 	}
526bacab7d6STim J. Robbins 	if (errno == ERANGE) {
527ab5a295bSJuli Mallett 		warnx3("%s: %s", *gargv, strerror(ERANGE));
528bacab7d6STim J. Robbins 		rval = 1;
529bacab7d6STim J. Robbins 	}
530ab5a295bSJuli Mallett 	++gargv;
531bacab7d6STim J. Robbins 	return (rval);
5329b50d902SRodney W. Grimes }
5339b50d902SRodney W. Grimes 
5349b50d902SRodney W. Grimes static int
535f4ac32deSDavid Malone asciicode(void)
5369b50d902SRodney W. Grimes {
537f4ac32deSDavid Malone 	int ch;
5389b50d902SRodney W. Grimes 
5399b50d902SRodney W. Grimes 	ch = **gargv;
5409b50d902SRodney W. Grimes 	if (ch == '\'' || ch == '"')
5419b50d902SRodney W. Grimes 		ch = (*gargv)[1];
5429b50d902SRodney W. Grimes 	++gargv;
5439b50d902SRodney W. Grimes 	return (ch);
5449b50d902SRodney W. Grimes }
5459b50d902SRodney W. Grimes 
5469b50d902SRodney W. Grimes static void
547f4ac32deSDavid Malone usage(void)
5489b50d902SRodney W. Grimes {
5499b50d902SRodney W. Grimes 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
5509b50d902SRodney W. Grimes }
551