xref: /freebsd/usr.bin/printf/printf.c (revision 62a721e79ce4b10fd0ae9cfec4ed1981973cf8b5)
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
433ec30b79SSteve Price static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
441ea7321bSMartin Cracauer static const char rcsid[] =
451ea7321bSMartin Cracauer   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/types.h>
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <err.h>
519b50d902SRodney W. Grimes #include <errno.h>
529b50d902SRodney W. Grimes #include <limits.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"
613b53d380SBruce Evans #else
623b53d380SBruce Evans #define	warnx1(a, b, c)		warnx(a)
633b53d380SBruce Evans #define	warnx2(a, b, c)		warnx(a, b)
643b53d380SBruce Evans #define	warnx3(a, b, c)		warnx(a, b, c)
659b50d902SRodney W. Grimes #endif
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes #define PF(f, func) { \
68d72f654cSPeter Wemm 	char *b = NULL; \
699b50d902SRodney W. Grimes 	if (fieldwidth) \
709b50d902SRodney W. Grimes 		if (precision) \
71d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, precision, func); \
729b50d902SRodney W. Grimes 		else \
73d72f654cSPeter Wemm 			(void)asprintf(&b, f, fieldwidth, func); \
749b50d902SRodney W. Grimes 	else if (precision) \
75d72f654cSPeter Wemm 		(void)asprintf(&b, f, precision, func); \
769b50d902SRodney W. Grimes 	else \
77d72f654cSPeter Wemm 		(void)asprintf(&b, f, func); \
78d72f654cSPeter Wemm 	if (b) { \
79d72f654cSPeter Wemm 		(void)fputs(b, stdout); \
80d72f654cSPeter Wemm 		free(b); \
81d72f654cSPeter Wemm 	} \
829b50d902SRodney W. Grimes }
839b50d902SRodney W. Grimes 
849b50d902SRodney W. Grimes static int	 asciicode __P((void));
859b50d902SRodney W. Grimes static void	 escape __P((char *));
869b50d902SRodney W. Grimes static int	 getchr __P((void));
879b50d902SRodney W. Grimes static double	 getdouble __P((void));
889b50d902SRodney W. Grimes static int	 getint __P((int *));
8962a721e7SStefan Eßer static int	 getquad __P((quad_t *));
909b50d902SRodney W. Grimes static char	*getstr __P((void));
919b50d902SRodney W. Grimes static char	*mklong __P((char *, int));
929b50d902SRodney W. Grimes static void	 usage __P((void));
939b50d902SRodney W. Grimes 
949b50d902SRodney W. Grimes static char **gargv;
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes int
979b50d902SRodney W. Grimes #ifdef BUILTIN
989b50d902SRodney W. Grimes progprintf(argc, argv)
999b50d902SRodney W. Grimes #else
1009b50d902SRodney W. Grimes main(argc, argv)
1019b50d902SRodney W. Grimes #endif
1029b50d902SRodney W. Grimes 	int argc;
1039b50d902SRodney W. Grimes 	char *argv[];
1049b50d902SRodney W. Grimes {
1059b50d902SRodney W. Grimes 	static char *skip1, *skip2;
1069b50d902SRodney W. Grimes 	int ch, end, fieldwidth, precision;
1079b50d902SRodney W. Grimes 	char convch, nextch, *format, *fmt, *start;
1089b50d902SRodney W. Grimes 
1091c8af878SWarner Losh 	while ((ch = getopt(argc, argv, "")) != -1)
1109b50d902SRodney W. Grimes 		switch (ch) {
1119b50d902SRodney W. Grimes 		case '?':
1129b50d902SRodney W. Grimes 		default:
1139b50d902SRodney W. Grimes 			usage();
1149b50d902SRodney W. Grimes 			return (1);
1159b50d902SRodney W. Grimes 		}
1169b50d902SRodney W. Grimes 	argc -= optind;
1179b50d902SRodney W. Grimes 	argv += optind;
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes 	if (argc < 1) {
1209b50d902SRodney W. Grimes 		usage();
1219b50d902SRodney W. Grimes 		return (1);
1229b50d902SRodney W. Grimes 	}
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes 	/*
1259b50d902SRodney W. Grimes 	 * Basic algorithm is to scan the format string for conversion
1269b50d902SRodney W. Grimes 	 * specifications -- once one is found, find out if the field
1279b50d902SRodney W. Grimes 	 * width or precision is a '*'; if it is, gather up value.  Note,
1289b50d902SRodney W. Grimes 	 * format strings are reused as necessary to use up the provided
1299b50d902SRodney W. Grimes 	 * arguments, arguments of zero/null string are provided to use
1309b50d902SRodney W. Grimes 	 * up the format string.
1319b50d902SRodney W. Grimes 	 */
1329b50d902SRodney W. Grimes 	skip1 = "#-+ 0";
133d867cefdSJoerg Wunsch 	skip2 = "0123456789";
1349b50d902SRodney W. Grimes 
1359b50d902SRodney W. Grimes 	escape(fmt = format = *argv);		/* backslash interpretation */
1369b50d902SRodney W. Grimes 	gargv = ++argv;
1379b50d902SRodney W. Grimes 	for (;;) {
1389b50d902SRodney W. Grimes 		end = 0;
1399b50d902SRodney W. Grimes 		/* find next format specification */
1409b50d902SRodney W. Grimes next:		for (start = fmt;; ++fmt) {
1419b50d902SRodney W. Grimes 			if (!*fmt) {
1429b50d902SRodney W. Grimes 				/* avoid infinite loop */
1439b50d902SRodney W. Grimes 				if (end == 1) {
1443b53d380SBruce Evans 					warnx1("missing format character",
145b0c9a86dSJohn Polstra 					    NULL, NULL);
1469b50d902SRodney W. Grimes 					return (1);
1479b50d902SRodney W. Grimes 				}
1489b50d902SRodney W. Grimes 				end = 1;
1499b50d902SRodney W. Grimes 				if (fmt > start)
1509b50d902SRodney W. Grimes 					(void)printf("%s", start);
1519b50d902SRodney W. Grimes 				if (!*gargv)
1529b50d902SRodney W. Grimes 					return (0);
1539b50d902SRodney W. Grimes 				fmt = format;
1549b50d902SRodney W. Grimes 				goto next;
1559b50d902SRodney W. Grimes 			}
1569b50d902SRodney W. Grimes 			/* %% prints a % */
1579b50d902SRodney W. Grimes 			if (*fmt == '%') {
1589b50d902SRodney W. Grimes 				if (*++fmt != '%')
1599b50d902SRodney W. Grimes 					break;
1609b50d902SRodney W. Grimes 				*fmt++ = '\0';
1619b50d902SRodney W. Grimes 				(void)printf("%s", start);
1629b50d902SRodney W. Grimes 				goto next;
1639b50d902SRodney W. Grimes 			}
1649b50d902SRodney W. Grimes 		}
1659b50d902SRodney W. Grimes 
1669b50d902SRodney W. Grimes 		/* skip to field width */
1679b50d902SRodney W. Grimes 		for (; strchr(skip1, *fmt); ++fmt);
1689b50d902SRodney W. Grimes 		if (*fmt == '*') {
1699b50d902SRodney W. Grimes 			if (getint(&fieldwidth))
1709b50d902SRodney W. Grimes 				return (1);
171d867cefdSJoerg Wunsch 			++fmt;
172d867cefdSJoerg Wunsch 		} else {
1739b50d902SRodney W. Grimes 			fieldwidth = 0;
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes 			/* skip to possible '.', get following precision */
1769b50d902SRodney W. Grimes 			for (; strchr(skip2, *fmt); ++fmt);
177d867cefdSJoerg Wunsch 		}
178d867cefdSJoerg Wunsch 		if (*fmt == '.') {
179d867cefdSJoerg Wunsch 			/* precision present? */
1809b50d902SRodney W. Grimes 			++fmt;
1819b50d902SRodney W. Grimes 			if (*fmt == '*') {
1829b50d902SRodney W. Grimes 				if (getint(&precision))
1839b50d902SRodney W. Grimes 					return (1);
184d867cefdSJoerg Wunsch 				++fmt;
185d867cefdSJoerg Wunsch 			} else {
1869b50d902SRodney W. Grimes 				precision = 0;
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes 				/* skip to conversion char */
1899b50d902SRodney W. Grimes 				for (; strchr(skip2, *fmt); ++fmt);
190d867cefdSJoerg Wunsch 			}
191d867cefdSJoerg Wunsch 		} else
192d867cefdSJoerg Wunsch 			precision = 0;
1939b50d902SRodney W. Grimes 		if (!*fmt) {
1943b53d380SBruce Evans 			warnx1("missing format character", NULL, NULL);
1959b50d902SRodney W. Grimes 			return (1);
1969b50d902SRodney W. Grimes 		}
1979b50d902SRodney W. Grimes 
1989b50d902SRodney W. Grimes 		convch = *fmt;
1999b50d902SRodney W. Grimes 		nextch = *++fmt;
2009b50d902SRodney W. Grimes 		*fmt = '\0';
2019b50d902SRodney W. Grimes 		switch(convch) {
2029b50d902SRodney W. Grimes 		case 'c': {
2039b50d902SRodney W. Grimes 			char p;
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes 			p = getchr();
2069b50d902SRodney W. Grimes 			PF(start, p);
2079b50d902SRodney W. Grimes 			break;
2089b50d902SRodney W. Grimes 		}
2099b50d902SRodney W. Grimes 		case 's': {
2109b50d902SRodney W. Grimes 			char *p;
2119b50d902SRodney W. Grimes 
2129b50d902SRodney W. Grimes 			p = getstr();
2139b50d902SRodney W. Grimes 			PF(start, p);
2149b50d902SRodney W. Grimes 			break;
2159b50d902SRodney W. Grimes 		}
2169b50d902SRodney W. Grimes 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
21762a721e7SStefan Eßer 			quad_t p;
2189b50d902SRodney W. Grimes 			char *f;
2199b50d902SRodney W. Grimes 
2209b50d902SRodney W. Grimes 			if ((f = mklong(start, convch)) == NULL)
2219b50d902SRodney W. Grimes 				return (1);
22262a721e7SStefan Eßer 			if (getquad(&p))
2239b50d902SRodney W. Grimes 				return (1);
2249b50d902SRodney W. Grimes 			PF(f, p);
2259b50d902SRodney W. Grimes 			break;
2269b50d902SRodney W. Grimes 		}
2279b50d902SRodney W. Grimes 		case 'e': case 'E': case 'f': case 'g': case 'G': {
2289b50d902SRodney W. Grimes 			double p;
2299b50d902SRodney W. Grimes 
2309b50d902SRodney W. Grimes 			p = getdouble();
2319b50d902SRodney W. Grimes 			PF(start, p);
2329b50d902SRodney W. Grimes 			break;
2339b50d902SRodney W. Grimes 		}
2349b50d902SRodney W. Grimes 		default:
2353b53d380SBruce Evans 			warnx2("illegal format character %c", convch, NULL);
2369b50d902SRodney W. Grimes 			return (1);
2379b50d902SRodney W. Grimes 		}
2389b50d902SRodney W. Grimes 		*fmt = nextch;
2399b50d902SRodney W. Grimes 	}
2409b50d902SRodney W. Grimes 	/* NOTREACHED */
2419b50d902SRodney W. Grimes }
2429b50d902SRodney W. Grimes 
2439b50d902SRodney W. Grimes static char *
2449b50d902SRodney W. Grimes mklong(str, ch)
2459b50d902SRodney W. Grimes 	char *str;
2469b50d902SRodney W. Grimes 	int ch;
2479b50d902SRodney W. Grimes {
2489b50d902SRodney W. Grimes 	static char copy[64];
2499b50d902SRodney W. Grimes 	int len;
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 	len = strlen(str) + 2;
25262a721e7SStefan Eßer 	if (len > sizeof copy)
25362a721e7SStefan Eßer 		return NULL;
25462a721e7SStefan Eßer 
2559b50d902SRodney W. Grimes 	memmove(copy, str, len - 3);
25662a721e7SStefan Eßer 	copy[len - 3] = 'q';
2579b50d902SRodney W. Grimes 	copy[len - 2] = ch;
2589b50d902SRodney W. Grimes 	copy[len - 1] = '\0';
2599b50d902SRodney W. Grimes 	return (copy);
2609b50d902SRodney W. Grimes }
2619b50d902SRodney W. Grimes 
2629b50d902SRodney W. Grimes static void
2639b50d902SRodney W. Grimes escape(fmt)
2649b50d902SRodney W. Grimes 	register char *fmt;
2659b50d902SRodney W. Grimes {
2669b50d902SRodney W. Grimes 	register char *store;
2679b50d902SRodney W. Grimes 	register int value, c;
2689b50d902SRodney W. Grimes 
269e6068a34SSteve Price 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
2709b50d902SRodney W. Grimes 		if (c != '\\') {
2719b50d902SRodney W. Grimes 			*store = c;
2729b50d902SRodney W. Grimes 			continue;
2739b50d902SRodney W. Grimes 		}
2749b50d902SRodney W. Grimes 		switch (*++fmt) {
2759b50d902SRodney W. Grimes 		case '\0':		/* EOS, user error */
2769b50d902SRodney W. Grimes 			*store = '\\';
2779b50d902SRodney W. Grimes 			*++store = '\0';
2789b50d902SRodney W. Grimes 			return;
2799b50d902SRodney W. Grimes 		case '\\':		/* backslash */
2809b50d902SRodney W. Grimes 		case '\'':		/* single quote */
2819b50d902SRodney W. Grimes 			*store = *fmt;
2829b50d902SRodney W. Grimes 			break;
2839b50d902SRodney W. Grimes 		case 'a':		/* bell/alert */
2849b50d902SRodney W. Grimes 			*store = '\7';
2859b50d902SRodney W. Grimes 			break;
2869b50d902SRodney W. Grimes 		case 'b':		/* backspace */
2879b50d902SRodney W. Grimes 			*store = '\b';
2889b50d902SRodney W. Grimes 			break;
2899b50d902SRodney W. Grimes 		case 'f':		/* form-feed */
2909b50d902SRodney W. Grimes 			*store = '\f';
2919b50d902SRodney W. Grimes 			break;
2929b50d902SRodney W. Grimes 		case 'n':		/* newline */
2939b50d902SRodney W. Grimes 			*store = '\n';
2949b50d902SRodney W. Grimes 			break;
2959b50d902SRodney W. Grimes 		case 'r':		/* carriage-return */
2969b50d902SRodney W. Grimes 			*store = '\r';
2979b50d902SRodney W. Grimes 			break;
2989b50d902SRodney W. Grimes 		case 't':		/* horizontal tab */
2999b50d902SRodney W. Grimes 			*store = '\t';
3009b50d902SRodney W. Grimes 			break;
3019b50d902SRodney W. Grimes 		case 'v':		/* vertical tab */
3029b50d902SRodney W. Grimes 			*store = '\13';
3039b50d902SRodney W. Grimes 			break;
3049b50d902SRodney W. Grimes 					/* octal constant */
3059b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3':
3069b50d902SRodney W. Grimes 		case '4': case '5': case '6': case '7':
3079b50d902SRodney W. Grimes 			for (c = 3, value = 0;
3089b50d902SRodney W. Grimes 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
3099b50d902SRodney W. Grimes 				value <<= 3;
3109b50d902SRodney W. Grimes 				value += *fmt - '0';
3119b50d902SRodney W. Grimes 			}
3129b50d902SRodney W. Grimes 			--fmt;
3139b50d902SRodney W. Grimes 			*store = value;
3149b50d902SRodney W. Grimes 			break;
3159b50d902SRodney W. Grimes 		default:
3169b50d902SRodney W. Grimes 			*store = *fmt;
3179b50d902SRodney W. Grimes 			break;
3189b50d902SRodney W. Grimes 		}
3199b50d902SRodney W. Grimes 	}
3209b50d902SRodney W. Grimes 	*store = '\0';
3219b50d902SRodney W. Grimes }
3229b50d902SRodney W. Grimes 
3239b50d902SRodney W. Grimes static int
3249b50d902SRodney W. Grimes getchr()
3259b50d902SRodney W. Grimes {
3269b50d902SRodney W. Grimes 	if (!*gargv)
3279b50d902SRodney W. Grimes 		return ('\0');
3289b50d902SRodney W. Grimes 	return ((int)**gargv++);
3299b50d902SRodney W. Grimes }
3309b50d902SRodney W. Grimes 
3319b50d902SRodney W. Grimes static char *
3329b50d902SRodney W. Grimes getstr()
3339b50d902SRodney W. Grimes {
3349b50d902SRodney W. Grimes 	if (!*gargv)
3359b50d902SRodney W. Grimes 		return ("");
3369b50d902SRodney W. Grimes 	return (*gargv++);
3379b50d902SRodney W. Grimes }
3389b50d902SRodney W. Grimes 
3399b50d902SRodney W. Grimes static char *Number = "+-.0123456789";
3409b50d902SRodney W. Grimes static int
3419b50d902SRodney W. Grimes getint(ip)
3429b50d902SRodney W. Grimes 	int *ip;
3439b50d902SRodney W. Grimes {
34462a721e7SStefan Eßer 	quad_t val;
3459b50d902SRodney W. Grimes 
34662a721e7SStefan Eßer 	if (getquad(&val))
3479b50d902SRodney W. Grimes 		return (1);
34862a721e7SStefan Eßer 	if (val < INT_MIN || val > INT_MAX) {
3493b53d380SBruce Evans 		warnx3("%s: %s", *gargv, strerror(ERANGE));
3509b50d902SRodney W. Grimes 		return (1);
3519b50d902SRodney W. Grimes 	}
35262a721e7SStefan Eßer 	*ip = (int)val;
3539b50d902SRodney W. Grimes 	return (0);
3549b50d902SRodney W. Grimes }
3559b50d902SRodney W. Grimes 
3569b50d902SRodney W. Grimes static int
35762a721e7SStefan Eßer getquad(lp)
35862a721e7SStefan Eßer 	quad_t *lp;
3599b50d902SRodney W. Grimes {
36062a721e7SStefan Eßer 	quad_t val;
3619b50d902SRodney W. Grimes 	char *ep;
3629b50d902SRodney W. Grimes 
3639b50d902SRodney W. Grimes 	if (!*gargv) {
3649b50d902SRodney W. Grimes 		*lp = 0;
3659b50d902SRodney W. Grimes 		return (0);
3669b50d902SRodney W. Grimes 	}
3679b50d902SRodney W. Grimes 	if (strchr(Number, **gargv)) {
3689b50d902SRodney W. Grimes 		errno = 0;
36962a721e7SStefan Eßer 		val = strtoq(*gargv, &ep, 0);
3709b50d902SRodney W. Grimes 		if (*ep != '\0') {
3713b53d380SBruce Evans 			warnx2("%s: illegal number", *gargv, NULL);
3729b50d902SRodney W. Grimes 			return (1);
3739b50d902SRodney W. Grimes 		}
3749b50d902SRodney W. Grimes 		if (errno == ERANGE)
37562a721e7SStefan Eßer 			if (val == QUAD_MAX) {
3763b53d380SBruce Evans 				warnx3("%s: %s", *gargv, strerror(ERANGE));
3779b50d902SRodney W. Grimes 				return (1);
3789b50d902SRodney W. Grimes 			}
37962a721e7SStefan Eßer 			if (val == QUAD_MIN) {
3803b53d380SBruce Evans 				warnx3("%s: %s", *gargv, strerror(ERANGE));
3819b50d902SRodney W. Grimes 				return (1);
3829b50d902SRodney W. Grimes 			}
3839b50d902SRodney W. Grimes 
3849b50d902SRodney W. Grimes 		*lp = val;
3859b50d902SRodney W. Grimes 		++gargv;
3869b50d902SRodney W. Grimes 		return (0);
3879b50d902SRodney W. Grimes 	}
3889b50d902SRodney W. Grimes 	*lp =  (long)asciicode();
3899b50d902SRodney W. Grimes 	return (0);
3909b50d902SRodney W. Grimes }
3919b50d902SRodney W. Grimes 
3929b50d902SRodney W. Grimes static double
3939b50d902SRodney W. Grimes getdouble()
3949b50d902SRodney W. Grimes {
3959b50d902SRodney W. Grimes 	if (!*gargv)
3969b50d902SRodney W. Grimes 		return ((double)0);
3979b50d902SRodney W. Grimes 	if (strchr(Number, **gargv))
3989b50d902SRodney W. Grimes 		return (atof(*gargv++));
3999b50d902SRodney W. Grimes 	return ((double)asciicode());
4009b50d902SRodney W. Grimes }
4019b50d902SRodney W. Grimes 
4029b50d902SRodney W. Grimes static int
4039b50d902SRodney W. Grimes asciicode()
4049b50d902SRodney W. Grimes {
4059b50d902SRodney W. Grimes 	register int ch;
4069b50d902SRodney W. Grimes 
4079b50d902SRodney W. Grimes 	ch = **gargv;
4089b50d902SRodney W. Grimes 	if (ch == '\'' || ch == '"')
4099b50d902SRodney W. Grimes 		ch = (*gargv)[1];
4109b50d902SRodney W. Grimes 	++gargv;
4119b50d902SRodney W. Grimes 	return (ch);
4129b50d902SRodney W. Grimes }
4139b50d902SRodney W. Grimes 
4149b50d902SRodney W. Grimes static void
4159b50d902SRodney W. Grimes usage()
4169b50d902SRodney W. Grimes {
4179b50d902SRodney W. Grimes 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
4189b50d902SRodney W. Grimes }
419