xref: /freebsd/usr.bin/jot/jot.c (revision 612740bdcf428625dce46ea9f505762c85b3cf94)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 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 #ifndef lint
357bd7ad50SPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
417bd7ad50SPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)jot.c	8.1 (Berkeley) 6/6/93";
437bd7ad50SPhilippe Charnier #endif
447bd7ad50SPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes /*
499b50d902SRodney W. Grimes  * jot - print sequential or random data
509b50d902SRodney W. Grimes  *
519b50d902SRodney W. Grimes  * Author:  John Kunze, Office of Comp. Affairs, UCB
529b50d902SRodney W. Grimes  */
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes #include <ctype.h>
557bd7ad50SPhilippe Charnier #include <err.h>
569b50d902SRodney W. Grimes #include <limits.h>
579b50d902SRodney W. Grimes #include <stdio.h>
589b50d902SRodney W. Grimes #include <stdlib.h>
599b50d902SRodney W. Grimes #include <string.h>
609b50d902SRodney W. Grimes #include <time.h>
619d288406SAndrey A. Chernov #include <unistd.h>
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes #define	REPS_DEF	100
649b50d902SRodney W. Grimes #define	BEGIN_DEF	1
659b50d902SRodney W. Grimes #define	ENDER_DEF	100
669b50d902SRodney W. Grimes #define	STEP_DEF	1
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes #define	isdefault(s)	(strcmp((s), "-") == 0)
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes double	begin;
719b50d902SRodney W. Grimes double	ender;
729b50d902SRodney W. Grimes double	s;
739b50d902SRodney W. Grimes long	reps;
749b50d902SRodney W. Grimes int	randomize;
759b50d902SRodney W. Grimes int	infinity;
769b50d902SRodney W. Grimes int	boring;
779b50d902SRodney W. Grimes int	prec;
785249bd84SSheldon Hearn int	longdata;
798db8a33bSSheldon Hearn int	intdata;
809b50d902SRodney W. Grimes int	chardata;
818db8a33bSSheldon Hearn int	nosign;
829b50d902SRodney W. Grimes int	nofinalnl;
839b50d902SRodney W. Grimes char	*sepstring = "\n";
849b50d902SRodney W. Grimes char	format[BUFSIZ];
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes void	getargs __P((int, char *[]));
879b50d902SRodney W. Grimes void	getformat __P((void));
889b50d902SRodney W. Grimes int		getprec __P((char *));
895249bd84SSheldon Hearn int	putdata __P((double, long));
907bd7ad50SPhilippe Charnier static void usage __P((void));
919b50d902SRodney W. Grimes 
929b50d902SRodney W. Grimes int
939b50d902SRodney W. Grimes main(argc, argv)
949b50d902SRodney W. Grimes 	int argc;
959b50d902SRodney W. Grimes 	char *argv[];
969b50d902SRodney W. Grimes {
979b50d902SRodney W. Grimes 	double	xd, yd;
989b50d902SRodney W. Grimes 	long	id;
999b50d902SRodney W. Grimes 	register double	*x = &xd;
1009b50d902SRodney W. Grimes 	register double	*y = &yd;
1019b50d902SRodney W. Grimes 	register long	*i = &id;
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	getargs(argc, argv);
1049b50d902SRodney W. Grimes 	if (randomize) {
1059b50d902SRodney W. Grimes 		*x = (ender - begin) * (ender > begin ? 1 : -1);
1069b50d902SRodney W. Grimes 		for (*i = 1; *i <= reps || infinity; (*i)++) {
1074b9cb490SKris Kennaway 			*y = (double) arc4random() / ULONG_MAX;
1085249bd84SSheldon Hearn 			if (putdata(*y * *x + begin, reps - *i))
1095249bd84SSheldon Hearn 				errx(1, "range error in conversion");
1109b50d902SRodney W. Grimes 		}
1118db8a33bSSheldon Hearn 	} else
1129b50d902SRodney W. Grimes 		for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s)
1135249bd84SSheldon Hearn 			if (putdata(*x, reps - *i))
1145249bd84SSheldon Hearn 				errx(1, "range error in conversion");
1159b50d902SRodney W. Grimes 	if (!nofinalnl)
1169b50d902SRodney W. Grimes 		putchar('\n');
1179b50d902SRodney W. Grimes 	exit(0);
1189b50d902SRodney W. Grimes }
1199b50d902SRodney W. Grimes 
1209b50d902SRodney W. Grimes void
1219b50d902SRodney W. Grimes getargs(ac, av)
1229b50d902SRodney W. Grimes 	int ac;
1239b50d902SRodney W. Grimes 	char *av[];
1249b50d902SRodney W. Grimes {
1259b50d902SRodney W. Grimes 	register unsigned int	mask = 0;
1269b50d902SRodney W. Grimes 	register int		n = 0;
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	while (--ac && **++av == '-' && !isdefault(*av))
1299b50d902SRodney W. Grimes 		switch ((*av)[1]) {
1309b50d902SRodney W. Grimes 		case 'r':
1319b50d902SRodney W. Grimes 			randomize = 1;
1329b50d902SRodney W. Grimes 			break;
1339b50d902SRodney W. Grimes 		case 'c':
1349b50d902SRodney W. Grimes 			chardata = 1;
1359b50d902SRodney W. Grimes 			break;
1369b50d902SRodney W. Grimes 		case 'n':
1379b50d902SRodney W. Grimes 			nofinalnl = 1;
1389b50d902SRodney W. Grimes 			break;
1399b50d902SRodney W. Grimes 		case 'b':
1409b50d902SRodney W. Grimes 			boring = 1;
1419b50d902SRodney W. Grimes 		case 'w':
1429b50d902SRodney W. Grimes 			if ((*av)[2])
1439b50d902SRodney W. Grimes 				strcpy(format, *av + 2);
1449b50d902SRodney W. Grimes 			else if (!--ac)
1457bd7ad50SPhilippe Charnier 				errx(1, "need context word after -w or -b");
1469b50d902SRodney W. Grimes 			else
1479b50d902SRodney W. Grimes 				strcpy(format, *++av);
1489b50d902SRodney W. Grimes 			break;
1499b50d902SRodney W. Grimes 		case 's':
1509b50d902SRodney W. Grimes 			if ((*av)[2])
151e29b080fSJoerg Wunsch 				sepstring = *av + 2;
1529b50d902SRodney W. Grimes 			else if (!--ac)
1537bd7ad50SPhilippe Charnier 				errx(1, "need string after -s");
1549b50d902SRodney W. Grimes 			else
155e29b080fSJoerg Wunsch 				sepstring = *++av;
1569b50d902SRodney W. Grimes 			break;
1579b50d902SRodney W. Grimes 		case 'p':
1589b50d902SRodney W. Grimes 			if ((*av)[2])
1599b50d902SRodney W. Grimes 				prec = atoi(*av + 2);
1609b50d902SRodney W. Grimes 			else if (!--ac)
1617bd7ad50SPhilippe Charnier 				errx(1, "need number after -p");
1629b50d902SRodney W. Grimes 			else
1639b50d902SRodney W. Grimes 				prec = atoi(*++av);
1649b50d902SRodney W. Grimes 			if (prec <= 0)
1657bd7ad50SPhilippe Charnier 				errx(1, "bad precision value");
1669b50d902SRodney W. Grimes 			break;
1679b50d902SRodney W. Grimes 		default:
1687bd7ad50SPhilippe Charnier 			usage();
1699b50d902SRodney W. Grimes 		}
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes 	switch (ac) {	/* examine args right to left, falling thru cases */
1729b50d902SRodney W. Grimes 	case 4:
1739b50d902SRodney W. Grimes 		if (!isdefault(av[3])) {
1749b50d902SRodney W. Grimes 			if (!sscanf(av[3], "%lf", &s))
1757bd7ad50SPhilippe Charnier 				errx(1, "bad s value: %s", av[3]);
1769b50d902SRodney W. Grimes 			mask |= 01;
1779b50d902SRodney W. Grimes 		}
1789b50d902SRodney W. Grimes 	case 3:
1799b50d902SRodney W. Grimes 		if (!isdefault(av[2])) {
1809b50d902SRodney W. Grimes 			if (!sscanf(av[2], "%lf", &ender))
1819b50d902SRodney W. Grimes 				ender = av[2][strlen(av[2])-1];
1829b50d902SRodney W. Grimes 			mask |= 02;
1839b50d902SRodney W. Grimes 			if (!prec)
1849b50d902SRodney W. Grimes 				n = getprec(av[2]);
1859b50d902SRodney W. Grimes 		}
1869b50d902SRodney W. Grimes 	case 2:
1879b50d902SRodney W. Grimes 		if (!isdefault(av[1])) {
1889b50d902SRodney W. Grimes 			if (!sscanf(av[1], "%lf", &begin))
1899b50d902SRodney W. Grimes 				begin = av[1][strlen(av[1])-1];
1909b50d902SRodney W. Grimes 			mask |= 04;
1919b50d902SRodney W. Grimes 			if (!prec)
1929b50d902SRodney W. Grimes 				prec = getprec(av[1]);
1939b50d902SRodney W. Grimes 			if (n > prec)		/* maximum precision */
1949b50d902SRodney W. Grimes 				prec = n;
1959b50d902SRodney W. Grimes 		}
1969b50d902SRodney W. Grimes 	case 1:
1979b50d902SRodney W. Grimes 		if (!isdefault(av[0])) {
1989b50d902SRodney W. Grimes 			if (!sscanf(av[0], "%ld", &reps))
1997bd7ad50SPhilippe Charnier 				errx(1, "bad reps value: %s", av[0]);
2009b50d902SRodney W. Grimes 			mask |= 010;
2019b50d902SRodney W. Grimes 		}
2029b50d902SRodney W. Grimes 		break;
2039b50d902SRodney W. Grimes 	case 0:
2047bd7ad50SPhilippe Charnier 		usage();
2059b50d902SRodney W. Grimes 	default:
2067bd7ad50SPhilippe Charnier 		errx(1, "too many arguments. What do you mean by %s?", av[4]);
2079b50d902SRodney W. Grimes 	}
2089b50d902SRodney W. Grimes 	getformat();
2099b50d902SRodney W. Grimes 	while (mask)	/* 4 bit mask has 1's where last 4 args were given */
2109b50d902SRodney W. Grimes 		switch (mask) {	/* fill in the 0's by default or computation */
2119b50d902SRodney W. Grimes 		case 001:
2129b50d902SRodney W. Grimes 			reps = REPS_DEF;
2139b50d902SRodney W. Grimes 			mask = 011;
2149b50d902SRodney W. Grimes 			break;
2159b50d902SRodney W. Grimes 		case 002:
2169b50d902SRodney W. Grimes 			reps = REPS_DEF;
2179b50d902SRodney W. Grimes 			mask = 012;
2189b50d902SRodney W. Grimes 			break;
2199b50d902SRodney W. Grimes 		case 003:
2209b50d902SRodney W. Grimes 			reps = REPS_DEF;
2219b50d902SRodney W. Grimes 			mask = 013;
2229b50d902SRodney W. Grimes 			break;
2239b50d902SRodney W. Grimes 		case 004:
2249b50d902SRodney W. Grimes 			reps = REPS_DEF;
2259b50d902SRodney W. Grimes 			mask = 014;
2269b50d902SRodney W. Grimes 			break;
2279b50d902SRodney W. Grimes 		case 005:
2289b50d902SRodney W. Grimes 			reps = REPS_DEF;
2299b50d902SRodney W. Grimes 			mask = 015;
2309b50d902SRodney W. Grimes 			break;
2319b50d902SRodney W. Grimes 		case 006:
2329b50d902SRodney W. Grimes 			reps = REPS_DEF;
2339b50d902SRodney W. Grimes 			mask = 016;
2349b50d902SRodney W. Grimes 			break;
2359b50d902SRodney W. Grimes 		case 007:
2369b50d902SRodney W. Grimes 			if (randomize) {
2379b50d902SRodney W. Grimes 				reps = REPS_DEF;
2389b50d902SRodney W. Grimes 				mask = 0;
2399b50d902SRodney W. Grimes 				break;
2409b50d902SRodney W. Grimes 			}
2419b50d902SRodney W. Grimes 			if (s == 0.0) {
2429b50d902SRodney W. Grimes 				reps = 0;
2439b50d902SRodney W. Grimes 				mask = 0;
2449b50d902SRodney W. Grimes 				break;
2459b50d902SRodney W. Grimes 			}
2469b50d902SRodney W. Grimes 			reps = (ender - begin + s) / s;
2479b50d902SRodney W. Grimes 			if (reps <= 0)
2487bd7ad50SPhilippe Charnier 				errx(1, "impossible stepsize");
2499b50d902SRodney W. Grimes 			mask = 0;
2509b50d902SRodney W. Grimes 			break;
2519b50d902SRodney W. Grimes 		case 010:
2529b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
2539b50d902SRodney W. Grimes 			mask = 014;
2549b50d902SRodney W. Grimes 			break;
2559b50d902SRodney W. Grimes 		case 011:
2569b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
2579b50d902SRodney W. Grimes 			mask = 015;
2589b50d902SRodney W. Grimes 			break;
2599b50d902SRodney W. Grimes 		case 012:
2605790155eSAndrey A. Chernov 			s = (randomize ? -1.0 : STEP_DEF);
2619b50d902SRodney W. Grimes 			mask = 013;
2629b50d902SRodney W. Grimes 			break;
2639b50d902SRodney W. Grimes 		case 013:
2649b50d902SRodney W. Grimes 			if (randomize)
2659b50d902SRodney W. Grimes 				begin = BEGIN_DEF;
2669b50d902SRodney W. Grimes 			else if (reps == 0)
2677bd7ad50SPhilippe Charnier 				errx(1, "must specify begin if reps == 0");
2685790155eSAndrey A. Chernov 			else
2699b50d902SRodney W. Grimes 				begin = ender - reps * s + s;
2709b50d902SRodney W. Grimes 			mask = 0;
2719b50d902SRodney W. Grimes 			break;
2729b50d902SRodney W. Grimes 		case 014:
2735790155eSAndrey A. Chernov 			s = (randomize ? -1.0 : STEP_DEF);
2749b50d902SRodney W. Grimes 			mask = 015;
2759b50d902SRodney W. Grimes 			break;
2769b50d902SRodney W. Grimes 		case 015:
2779b50d902SRodney W. Grimes 			if (randomize)
2789b50d902SRodney W. Grimes 				ender = ENDER_DEF;
2799b50d902SRodney W. Grimes 			else
2809b50d902SRodney W. Grimes 				ender = begin + reps * s - s;
2819b50d902SRodney W. Grimes 			mask = 0;
2829b50d902SRodney W. Grimes 			break;
2839b50d902SRodney W. Grimes 		case 016:
2849b50d902SRodney W. Grimes 			if (randomize)
2855790155eSAndrey A. Chernov 				s = -1.0;
2869b50d902SRodney W. Grimes 			else if (reps == 0)
2877bd7ad50SPhilippe Charnier 				errx(1, "infinite sequences cannot be bounded");
2889b50d902SRodney W. Grimes 			else if (reps == 1)
2899b50d902SRodney W. Grimes 				s = 0.0;
2909b50d902SRodney W. Grimes 			else
2919b50d902SRodney W. Grimes 				s = (ender - begin) / (reps - 1);
2929b50d902SRodney W. Grimes 			mask = 0;
2939b50d902SRodney W. Grimes 			break;
2949b50d902SRodney W. Grimes 		case 017:		/* if reps given and implied, */
2959b50d902SRodney W. Grimes 			if (!randomize && s != 0.0) {
2969b50d902SRodney W. Grimes 				long t = (ender - begin + s) / s;
2979b50d902SRodney W. Grimes 				if (t <= 0)
2987bd7ad50SPhilippe Charnier 					errx(1, "impossible stepsize");
2999b50d902SRodney W. Grimes 				if (t < reps)		/* take lesser */
3009b50d902SRodney W. Grimes 					reps = t;
3019b50d902SRodney W. Grimes 			}
3029b50d902SRodney W. Grimes 			mask = 0;
3039b50d902SRodney W. Grimes 			break;
3049b50d902SRodney W. Grimes 		default:
3057bd7ad50SPhilippe Charnier 			errx(1, "bad mask");
3069b50d902SRodney W. Grimes 		}
3079b50d902SRodney W. Grimes 	if (reps == 0)
3089b50d902SRodney W. Grimes 		infinity = 1;
3099b50d902SRodney W. Grimes }
3109b50d902SRodney W. Grimes 
3115249bd84SSheldon Hearn int
3129b50d902SRodney W. Grimes putdata(x, notlast)
3139b50d902SRodney W. Grimes 	double x;
3149b50d902SRodney W. Grimes 	long notlast;
3159b50d902SRodney W. Grimes {
3169b50d902SRodney W. Grimes 
3175249bd84SSheldon Hearn 	if (boring)
318612740bdSKris Kennaway 		printf("%s", format);
3195249bd84SSheldon Hearn 	else if (longdata && nosign) {
3205249bd84SSheldon Hearn 		if (x <= (double)ULONG_MAX && x >= (double)0)
3218db8a33bSSheldon Hearn 			printf(format, (unsigned long)x);
3225249bd84SSheldon Hearn 		else
3235249bd84SSheldon Hearn 			return (1);
3245249bd84SSheldon Hearn 	} else if (longdata) {
3255249bd84SSheldon Hearn 		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
3268db8a33bSSheldon Hearn 			printf(format, (long)x);
3275249bd84SSheldon Hearn 		else
3285249bd84SSheldon Hearn 			return (1);
3295249bd84SSheldon Hearn 	} else if (chardata || (intdata && !nosign)) {
3305249bd84SSheldon Hearn 		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
3315249bd84SSheldon Hearn 			printf(format, (int)x);
3325249bd84SSheldon Hearn 		else
3335249bd84SSheldon Hearn 			return (1);
3345249bd84SSheldon Hearn 	} else if (intdata) {
3355249bd84SSheldon Hearn 		if (x <= (double)UINT_MAX && x >= (double)0)
3365249bd84SSheldon Hearn 			printf(format, (unsigned int)x);
3375249bd84SSheldon Hearn 		else
3385249bd84SSheldon Hearn 			return (1);
3395249bd84SSheldon Hearn 
3405249bd84SSheldon Hearn 	} else
3419b50d902SRodney W. Grimes 		printf(format, x);
3429b50d902SRodney W. Grimes 	if (notlast != 0)
3439b50d902SRodney W. Grimes 		fputs(sepstring, stdout);
3445249bd84SSheldon Hearn 
3455249bd84SSheldon Hearn 	return (0);
3469b50d902SRodney W. Grimes }
3479b50d902SRodney W. Grimes 
3487bd7ad50SPhilippe Charnier static void
3497bd7ad50SPhilippe Charnier usage()
3509b50d902SRodney W. Grimes {
3517bd7ad50SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
3527bd7ad50SPhilippe Charnier 	"usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
3537bd7ad50SPhilippe Charnier 	"           [reps [begin [end [s]]]]");
3549b50d902SRodney W. Grimes 	exit(1);
3559b50d902SRodney W. Grimes }
3569b50d902SRodney W. Grimes 
3579b50d902SRodney W. Grimes int
3589b50d902SRodney W. Grimes getprec(s)
3599b50d902SRodney W. Grimes 	char *s;
3609b50d902SRodney W. Grimes {
3619b50d902SRodney W. Grimes 	register char	*p;
3629b50d902SRodney W. Grimes 	register char	*q;
3639b50d902SRodney W. Grimes 
3649b50d902SRodney W. Grimes 	for (p = s; *p; p++)
3659b50d902SRodney W. Grimes 		if (*p == '.')
3669b50d902SRodney W. Grimes 			break;
3679b50d902SRodney W. Grimes 	if (!*p)
3689b50d902SRodney W. Grimes 		return (0);
3699b50d902SRodney W. Grimes 	for (q = ++p; *p; p++)
3709b50d902SRodney W. Grimes 		if (!isdigit(*p))
3719b50d902SRodney W. Grimes 			break;
3729b50d902SRodney W. Grimes 	return (p - q);
3739b50d902SRodney W. Grimes }
3749b50d902SRodney W. Grimes 
3759b50d902SRodney W. Grimes void
3769b50d902SRodney W. Grimes getformat()
3779b50d902SRodney W. Grimes {
3789b50d902SRodney W. Grimes 	register char	*p;
3795249bd84SSheldon Hearn 	int dot, hash, space, sign, numbers = 0;
3808db8a33bSSheldon Hearn 	char *s;
3819b50d902SRodney W. Grimes 
3829b50d902SRodney W. Grimes 	if (boring)				/* no need to bother */
3839b50d902SRodney W. Grimes 		return;
3849b50d902SRodney W. Grimes 	for (p = format; *p; p++)		/* look for '%' */
3859b50d902SRodney W. Grimes 		if (*p == '%' && *(p+1) != '%')	/* leave %% alone */
3869b50d902SRodney W. Grimes 			break;
3879b50d902SRodney W. Grimes 	if (!*p && !chardata)
3889b50d902SRodney W. Grimes 		sprintf(p, "%%.%df", prec);
3899b50d902SRodney W. Grimes 	else if (!*p && chardata) {
3909b50d902SRodney W. Grimes 		strcpy(p, "%c");
3918db8a33bSSheldon Hearn 		intdata = 1;
3928db8a33bSSheldon Hearn 	} else if (!*(p+1))
3939b50d902SRodney W. Grimes 		strcat(format, "%");		/* cannot end in single '%' */
3949b50d902SRodney W. Grimes 	else {
3958db8a33bSSheldon Hearn 		/*
3968db8a33bSSheldon Hearn 		 * Allow conversion format specifiers of the form
3978db8a33bSSheldon Hearn 		 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
3988db8a33bSSheldon Hearn 		 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
3998db8a33bSSheldon Hearn 		 */
4008db8a33bSSheldon Hearn 		s = p++;
4018db8a33bSSheldon Hearn 		dot = hash = space = sign = numbers = 0;
4028db8a33bSSheldon Hearn 		while (!isalpha(*p)) {
4038db8a33bSSheldon Hearn 			if (isdigit(*p)) {
4048db8a33bSSheldon Hearn 				numbers++;
4059b50d902SRodney W. Grimes 				p++;
4068db8a33bSSheldon Hearn 			} else if ((*p == '#' && !(numbers|dot|sign|space|
4078db8a33bSSheldon Hearn 			    hash++)) ||
4088db8a33bSSheldon Hearn 			    (*p == ' ' && !(numbers|dot|space++)) ||
4098db8a33bSSheldon Hearn 			    ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
4108db8a33bSSheldon Hearn 			    || (*p == '.' && !(dot++)))
4118db8a33bSSheldon Hearn 				p++;
4128db8a33bSSheldon Hearn 			else
4135249bd84SSheldon Hearn 				goto fmt_broken;
4145249bd84SSheldon Hearn 		}
4155249bd84SSheldon Hearn 		if (*p == 'l') {
4165249bd84SSheldon Hearn 			longdata = 1;
4175249bd84SSheldon Hearn 			if (*++p == 'l') {
4185249bd84SSheldon Hearn 				if (p[1] != '\0')
4195249bd84SSheldon Hearn 					p++;
4205249bd84SSheldon Hearn 				goto fmt_broken;
4215249bd84SSheldon Hearn 			}
4228db8a33bSSheldon Hearn 		}
4239b50d902SRodney W. Grimes 		switch (*p) {
4248db8a33bSSheldon Hearn 		case 'o': case 'u': case 'x': case 'X':
4258db8a33bSSheldon Hearn 			intdata = nosign = 1;
4269b50d902SRodney W. Grimes 			break;
4278db8a33bSSheldon Hearn 		case 'd': case 'i':
4288db8a33bSSheldon Hearn 			intdata = 1;
4298db8a33bSSheldon Hearn 			break;
4308db8a33bSSheldon Hearn 		case 'D':
4315249bd84SSheldon Hearn 			if (!longdata) {
4328db8a33bSSheldon Hearn 				intdata = 1;
4338db8a33bSSheldon Hearn 				break;
4348db8a33bSSheldon Hearn 			}
4358db8a33bSSheldon Hearn 		case 'O': case 'U':
4365249bd84SSheldon Hearn 			if (!longdata) {
4378db8a33bSSheldon Hearn 				intdata = nosign = 1;
4388db8a33bSSheldon Hearn 				break;
4398db8a33bSSheldon Hearn 			}
4408db8a33bSSheldon Hearn 		case 'c':
4415249bd84SSheldon Hearn 			if (!(intdata | longdata)) {
4428db8a33bSSheldon Hearn 				chardata = 1;
4438db8a33bSSheldon Hearn 				break;
4448db8a33bSSheldon Hearn 			}
4455249bd84SSheldon Hearn 		case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
4468db8a33bSSheldon Hearn 		case '$': case '*':
4475249bd84SSheldon Hearn 			goto fmt_broken;
4488db8a33bSSheldon Hearn 		case 'f': case 'e': case 'g': case 'E': case 'G':
4495249bd84SSheldon Hearn 			if (!longdata)
4508db8a33bSSheldon Hearn 				break;
4518db8a33bSSheldon Hearn 			/* FALLTHROUGH */
4529b50d902SRodney W. Grimes 		default:
4535249bd84SSheldon Hearn fmt_broken:
4548db8a33bSSheldon Hearn 			*++p = '\0';
4558db8a33bSSheldon Hearn 			errx(1, "illegal or unsupported format '%s'", s);
4568db8a33bSSheldon Hearn 			/* NOTREACHED */
4578db8a33bSSheldon Hearn 		}
4588db8a33bSSheldon Hearn 		while (*++p)
4598db8a33bSSheldon Hearn 			if (*p == '%' && *(p+1) && *(p+1) != '%')
4608db8a33bSSheldon Hearn 				errx(1, "too many conversions");
4618db8a33bSSheldon Hearn 			else if (*p == '%' && *(p+1) == '%')
4628db8a33bSSheldon Hearn 				p++;
4638db8a33bSSheldon Hearn 			else if (*p == '%' && !*(p+1)) {
4648db8a33bSSheldon Hearn 				strcat(format, "%");
4659b50d902SRodney W. Grimes 				break;
4669b50d902SRodney W. Grimes 			}
4679b50d902SRodney W. Grimes 	}
4689b50d902SRodney W. Grimes }
469