xref: /freebsd/usr.bin/jot/jot.c (revision cf0def9349fa4e21e8657489268524552cf29112)
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
44cf0def93SJuli Mallett #endif
45cf0def93SJuli Mallett 
46cf0def93SJuli Mallett #include <sys/cdefs.h>
47cf0def93SJuli Mallett __FBSDID("$FreeBSD$");
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes /*
509b50d902SRodney W. Grimes  * jot - print sequential or random data
519b50d902SRodney W. Grimes  *
529b50d902SRodney W. Grimes  * Author:  John Kunze, Office of Comp. Affairs, UCB
539b50d902SRodney W. Grimes  */
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes #include <ctype.h>
567bd7ad50SPhilippe Charnier #include <err.h>
579b50d902SRodney W. Grimes #include <limits.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
619b50d902SRodney W. Grimes #include <time.h>
629d288406SAndrey A. Chernov #include <unistd.h>
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes #define	REPS_DEF	100
659b50d902SRodney W. Grimes #define	BEGIN_DEF	1
669b50d902SRodney W. Grimes #define	ENDER_DEF	100
679b50d902SRodney W. Grimes #define	STEP_DEF	1
689b50d902SRodney W. Grimes 
6915ba0427SDima Dorfman #define	is_default(s)	(strcmp((s), "-") == 0)
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes double	begin;
729b50d902SRodney W. Grimes double	ender;
739b50d902SRodney W. Grimes double	s;
749b50d902SRodney W. Grimes long	reps;
759b50d902SRodney W. Grimes int	randomize;
769b50d902SRodney W. Grimes int	infinity;
779b50d902SRodney W. Grimes int	boring;
789b50d902SRodney W. Grimes int	prec;
795249bd84SSheldon Hearn int	longdata;
808db8a33bSSheldon Hearn int	intdata;
819b50d902SRodney W. Grimes int	chardata;
828db8a33bSSheldon Hearn int	nosign;
839b50d902SRodney W. Grimes int	nofinalnl;
84edd7b267SDima Dorfman const	char *sepstring = "\n";
859b50d902SRodney W. Grimes char	format[BUFSIZ];
869b50d902SRodney W. Grimes 
87f1bb2cd2SWarner Losh int		main(int, char *[]);
88f1bb2cd2SWarner Losh void		getformat(void);
89f1bb2cd2SWarner Losh int		getprec(char *);
90f1bb2cd2SWarner Losh int		putdata(double, long);
91f1bb2cd2SWarner Losh static void	usage(void);
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes int
94cf0def93SJuli Mallett main(int argc, char **argv)
959b50d902SRodney W. Grimes {
969b50d902SRodney W. Grimes 	double	xd, yd;
979b50d902SRodney W. Grimes 	long	id;
9815ba0427SDima Dorfman 	double	*x = &xd;
9915ba0427SDima Dorfman 	double	*y = &yd;
10015ba0427SDima Dorfman 	long	*i = &id;
10115ba0427SDima Dorfman 	unsigned int	mask = 0;
10215ba0427SDima Dorfman 	int	n = 0;
10315ba0427SDima Dorfman 	int	ch;
1049b50d902SRodney W. Grimes 
10515ba0427SDima Dorfman 	while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1)
106cf0def93SJuli Mallett 		switch (ch) {
1079b50d902SRodney W. Grimes 		case 'r':
1089b50d902SRodney W. Grimes 			randomize = 1;
1099b50d902SRodney W. Grimes 			break;
1109b50d902SRodney W. Grimes 		case 'c':
1119b50d902SRodney W. Grimes 			chardata = 1;
1129b50d902SRodney W. Grimes 			break;
1139b50d902SRodney W. Grimes 		case 'n':
1149b50d902SRodney W. Grimes 			nofinalnl = 1;
1159b50d902SRodney W. Grimes 			break;
1169b50d902SRodney W. Grimes 		case 'b':
1179b50d902SRodney W. Grimes 			boring = 1;
11815ba0427SDima Dorfman 			/* FALLTHROUGH */
1199b50d902SRodney W. Grimes 		case 'w':
12015ba0427SDima Dorfman 			if (strlcpy(format, optarg, sizeof(format)) >=
12115ba0427SDima Dorfman 			    sizeof(format))
12215ba0427SDima Dorfman 				errx(1, "-%c word too long", ch);
1239b50d902SRodney W. Grimes 			break;
1249b50d902SRodney W. Grimes 		case 's':
12515ba0427SDima Dorfman 			sepstring = optarg;
1269b50d902SRodney W. Grimes 			break;
1279b50d902SRodney W. Grimes 		case 'p':
12815ba0427SDima Dorfman 			prec = atoi(optarg);
1299b50d902SRodney W. Grimes 			if (prec <= 0)
1307bd7ad50SPhilippe Charnier 				errx(1, "bad precision value");
1319b50d902SRodney W. Grimes 			break;
1329b50d902SRodney W. Grimes 		default:
1337bd7ad50SPhilippe Charnier 			usage();
1349b50d902SRodney W. Grimes 		}
13515ba0427SDima Dorfman 	argc -= optind;
13615ba0427SDima Dorfman 	argv += optind;
1379b50d902SRodney W. Grimes 
13815ba0427SDima Dorfman 	switch (argc) {	/* examine args right to left, falling thru cases */
1399b50d902SRodney W. Grimes 	case 4:
14015ba0427SDima Dorfman 		if (!is_default(argv[3])) {
14115ba0427SDima Dorfman 			if (!sscanf(argv[3], "%lf", &s))
14215ba0427SDima Dorfman 				errx(1, "bad s value: %s", argv[3]);
1439b50d902SRodney W. Grimes 			mask |= 01;
1449b50d902SRodney W. Grimes 		}
1459b50d902SRodney W. Grimes 	case 3:
14615ba0427SDima Dorfman 		if (!is_default(argv[2])) {
14715ba0427SDima Dorfman 			if (!sscanf(argv[2], "%lf", &ender))
14815ba0427SDima Dorfman 				ender = argv[2][strlen(argv[2])-1];
1499b50d902SRodney W. Grimes 			mask |= 02;
1509b50d902SRodney W. Grimes 			if (!prec)
15115ba0427SDima Dorfman 				n = getprec(argv[2]);
1529b50d902SRodney W. Grimes 		}
1539b50d902SRodney W. Grimes 	case 2:
15415ba0427SDima Dorfman 		if (!is_default(argv[1])) {
15515ba0427SDima Dorfman 			if (!sscanf(argv[1], "%lf", &begin))
15615ba0427SDima Dorfman 				begin = argv[1][strlen(argv[1])-1];
1579b50d902SRodney W. Grimes 			mask |= 04;
1589b50d902SRodney W. Grimes 			if (!prec)
15915ba0427SDima Dorfman 				prec = getprec(argv[1]);
1609b50d902SRodney W. Grimes 			if (n > prec)		/* maximum precision */
1619b50d902SRodney W. Grimes 				prec = n;
1629b50d902SRodney W. Grimes 		}
1639b50d902SRodney W. Grimes 	case 1:
16415ba0427SDima Dorfman 		if (!is_default(argv[0])) {
16515ba0427SDima Dorfman 			if (!sscanf(argv[0], "%ld", &reps))
16615ba0427SDima Dorfman 				errx(1, "bad reps value: %s", argv[0]);
1679b50d902SRodney W. Grimes 			mask |= 010;
1689b50d902SRodney W. Grimes 		}
1699b50d902SRodney W. Grimes 		break;
1709b50d902SRodney W. Grimes 	case 0:
1717bd7ad50SPhilippe Charnier 		usage();
1729b50d902SRodney W. Grimes 	default:
17315ba0427SDima Dorfman 		errx(1, "too many arguments.  What do you mean by %s?",
17415ba0427SDima Dorfman 		    argv[4]);
1759b50d902SRodney W. Grimes 	}
1769b50d902SRodney W. Grimes 	getformat();
1779b50d902SRodney W. Grimes 	while (mask)	/* 4 bit mask has 1's where last 4 args were given */
1789b50d902SRodney W. Grimes 		switch (mask) {	/* fill in the 0's by default or computation */
1799b50d902SRodney W. Grimes 		case 001:
1809b50d902SRodney W. Grimes 			reps = REPS_DEF;
1819b50d902SRodney W. Grimes 			mask = 011;
1829b50d902SRodney W. Grimes 			break;
1839b50d902SRodney W. Grimes 		case 002:
1849b50d902SRodney W. Grimes 			reps = REPS_DEF;
1859b50d902SRodney W. Grimes 			mask = 012;
1869b50d902SRodney W. Grimes 			break;
1879b50d902SRodney W. Grimes 		case 003:
1889b50d902SRodney W. Grimes 			reps = REPS_DEF;
1899b50d902SRodney W. Grimes 			mask = 013;
1909b50d902SRodney W. Grimes 			break;
1919b50d902SRodney W. Grimes 		case 004:
1929b50d902SRodney W. Grimes 			reps = REPS_DEF;
1939b50d902SRodney W. Grimes 			mask = 014;
1949b50d902SRodney W. Grimes 			break;
1959b50d902SRodney W. Grimes 		case 005:
1969b50d902SRodney W. Grimes 			reps = REPS_DEF;
1979b50d902SRodney W. Grimes 			mask = 015;
1989b50d902SRodney W. Grimes 			break;
1999b50d902SRodney W. Grimes 		case 006:
2009b50d902SRodney W. Grimes 			reps = REPS_DEF;
2019b50d902SRodney W. Grimes 			mask = 016;
2029b50d902SRodney W. Grimes 			break;
2039b50d902SRodney W. Grimes 		case 007:
2049b50d902SRodney W. Grimes 			if (randomize) {
2059b50d902SRodney W. Grimes 				reps = REPS_DEF;
2069b50d902SRodney W. Grimes 				mask = 0;
2079b50d902SRodney W. Grimes 				break;
2089b50d902SRodney W. Grimes 			}
2099b50d902SRodney W. Grimes 			if (s == 0.0) {
2109b50d902SRodney W. Grimes 				reps = 0;
2119b50d902SRodney W. Grimes 				mask = 0;
2129b50d902SRodney W. Grimes 				break;
2139b50d902SRodney W. Grimes 			}
2149b50d902SRodney W. Grimes 			reps = (ender - begin + s) / s;
2159b50d902SRodney W. Grimes 			if (reps <= 0)
2167bd7ad50SPhilippe Charnier 				errx(1, "impossible stepsize");
2179b50d902SRodney W. Grimes 			mask = 0;
2189b50d902SRodney W. Grimes 			break;
2199b50d902SRodney W. Grimes 		case 010:
2209b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
2219b50d902SRodney W. Grimes 			mask = 014;
2229b50d902SRodney W. Grimes 			break;
2239b50d902SRodney W. Grimes 		case 011:
2249b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
2259b50d902SRodney W. Grimes 			mask = 015;
2269b50d902SRodney W. Grimes 			break;
2279b50d902SRodney W. Grimes 		case 012:
22815ba0427SDima Dorfman 			s = (randomize ? time(NULL) : STEP_DEF);
2299b50d902SRodney W. Grimes 			mask = 013;
2309b50d902SRodney W. Grimes 			break;
2319b50d902SRodney W. Grimes 		case 013:
2329b50d902SRodney W. Grimes 			if (randomize)
2339b50d902SRodney W. Grimes 				begin = BEGIN_DEF;
2349b50d902SRodney W. Grimes 			else if (reps == 0)
2357bd7ad50SPhilippe Charnier 				errx(1, "must specify begin if reps == 0");
2369b50d902SRodney W. Grimes 			begin = ender - reps * s + s;
2379b50d902SRodney W. Grimes 			mask = 0;
2389b50d902SRodney W. Grimes 			break;
2399b50d902SRodney W. Grimes 		case 014:
2405790155eSAndrey A. Chernov 			s = (randomize ? -1.0 : STEP_DEF);
2419b50d902SRodney W. Grimes 			mask = 015;
2429b50d902SRodney W. Grimes 			break;
2439b50d902SRodney W. Grimes 		case 015:
2449b50d902SRodney W. Grimes 			if (randomize)
2459b50d902SRodney W. Grimes 				ender = ENDER_DEF;
2469b50d902SRodney W. Grimes 			else
2479b50d902SRodney W. Grimes 				ender = begin + reps * s - s;
2489b50d902SRodney W. Grimes 			mask = 0;
2499b50d902SRodney W. Grimes 			break;
2509b50d902SRodney W. Grimes 		case 016:
2519b50d902SRodney W. Grimes 			if (randomize)
2525790155eSAndrey A. Chernov 				s = -1.0;
2539b50d902SRodney W. Grimes 			else if (reps == 0)
2547bd7ad50SPhilippe Charnier 				errx(1, "infinite sequences cannot be bounded");
2559b50d902SRodney W. Grimes 			else if (reps == 1)
2569b50d902SRodney W. Grimes 				s = 0.0;
2579b50d902SRodney W. Grimes 			else
2589b50d902SRodney W. Grimes 				s = (ender - begin) / (reps - 1);
2599b50d902SRodney W. Grimes 			mask = 0;
2609b50d902SRodney W. Grimes 			break;
2619b50d902SRodney W. Grimes 		case 017:		/* if reps given and implied, */
2629b50d902SRodney W. Grimes 			if (!randomize && s != 0.0) {
2639b50d902SRodney W. Grimes 				long t = (ender - begin + s) / s;
2649b50d902SRodney W. Grimes 				if (t <= 0)
2657bd7ad50SPhilippe Charnier 					errx(1, "impossible stepsize");
2669b50d902SRodney W. Grimes 				if (t < reps)		/* take lesser */
2679b50d902SRodney W. Grimes 					reps = t;
2689b50d902SRodney W. Grimes 			}
2699b50d902SRodney W. Grimes 			mask = 0;
2709b50d902SRodney W. Grimes 			break;
2719b50d902SRodney W. Grimes 		default:
2727bd7ad50SPhilippe Charnier 			errx(1, "bad mask");
2739b50d902SRodney W. Grimes 		}
2749b50d902SRodney W. Grimes 	if (reps == 0)
2759b50d902SRodney W. Grimes 		infinity = 1;
27615ba0427SDima Dorfman 	if (randomize) {
27715ba0427SDima Dorfman 		*x = (ender - begin) * (ender > begin ? 1 : -1);
27815ba0427SDima Dorfman 		for (*i = 1; *i <= reps || infinity; (*i)++) {
27907321a18SGarrett Wollman 			*y = arc4random() / (double)UINT32_MAX;
28015ba0427SDima Dorfman 			if (putdata(*y * *x + begin, reps - *i))
28115ba0427SDima Dorfman 				errx(1, "range error in conversion");
28215ba0427SDima Dorfman 		}
28315ba0427SDima Dorfman 	} else
28415ba0427SDima Dorfman 		for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s)
28515ba0427SDima Dorfman 			if (putdata(*x, reps - *i))
28615ba0427SDima Dorfman 				errx(1, "range error in conversion");
28715ba0427SDima Dorfman 	if (!nofinalnl)
28815ba0427SDima Dorfman 		putchar('\n');
28915ba0427SDima Dorfman 	exit(0);
2909b50d902SRodney W. Grimes }
2919b50d902SRodney W. Grimes 
2925249bd84SSheldon Hearn int
293cf0def93SJuli Mallett putdata(double x, long int notlast)
2949b50d902SRodney W. Grimes {
2959b50d902SRodney W. Grimes 
2965249bd84SSheldon Hearn 	if (boring)
297612740bdSKris Kennaway 		printf("%s", format);
2985249bd84SSheldon Hearn 	else if (longdata && nosign) {
2995249bd84SSheldon Hearn 		if (x <= (double)ULONG_MAX && x >= (double)0)
3008db8a33bSSheldon Hearn 			printf(format, (unsigned long)x);
3015249bd84SSheldon Hearn 		else
3025249bd84SSheldon Hearn 			return (1);
3035249bd84SSheldon Hearn 	} else if (longdata) {
3045249bd84SSheldon Hearn 		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
3058db8a33bSSheldon Hearn 			printf(format, (long)x);
3065249bd84SSheldon Hearn 		else
3075249bd84SSheldon Hearn 			return (1);
3085249bd84SSheldon Hearn 	} else if (chardata || (intdata && !nosign)) {
3095249bd84SSheldon Hearn 		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
3105249bd84SSheldon Hearn 			printf(format, (int)x);
3115249bd84SSheldon Hearn 		else
3125249bd84SSheldon Hearn 			return (1);
3135249bd84SSheldon Hearn 	} else if (intdata) {
3145249bd84SSheldon Hearn 		if (x <= (double)UINT_MAX && x >= (double)0)
3155249bd84SSheldon Hearn 			printf(format, (unsigned int)x);
3165249bd84SSheldon Hearn 		else
3175249bd84SSheldon Hearn 			return (1);
3185249bd84SSheldon Hearn 
3195249bd84SSheldon Hearn 	} else
3209b50d902SRodney W. Grimes 		printf(format, x);
3219b50d902SRodney W. Grimes 	if (notlast != 0)
3229b50d902SRodney W. Grimes 		fputs(sepstring, stdout);
3235249bd84SSheldon Hearn 
3245249bd84SSheldon Hearn 	return (0);
3259b50d902SRodney W. Grimes }
3269b50d902SRodney W. Grimes 
3277bd7ad50SPhilippe Charnier static void
328cf0def93SJuli Mallett usage(void)
3299b50d902SRodney W. Grimes {
3307bd7ad50SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
3317bd7ad50SPhilippe Charnier 	"usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
3327bd7ad50SPhilippe Charnier 	"           [reps [begin [end [s]]]]");
3339b50d902SRodney W. Grimes 	exit(1);
3349b50d902SRodney W. Grimes }
3359b50d902SRodney W. Grimes 
3369b50d902SRodney W. Grimes int
337cf0def93SJuli Mallett getprec(char *str)
3389b50d902SRodney W. Grimes {
33915ba0427SDima Dorfman 	char	*p;
34015ba0427SDima Dorfman 	char	*q;
3419b50d902SRodney W. Grimes 
342edd7b267SDima Dorfman 	for (p = str; *p; p++)
3439b50d902SRodney W. Grimes 		if (*p == '.')
3449b50d902SRodney W. Grimes 			break;
3459b50d902SRodney W. Grimes 	if (!*p)
3469b50d902SRodney W. Grimes 		return (0);
3479b50d902SRodney W. Grimes 	for (q = ++p; *p; p++)
3489b50d902SRodney W. Grimes 		if (!isdigit(*p))
3499b50d902SRodney W. Grimes 			break;
3509b50d902SRodney W. Grimes 	return (p - q);
3519b50d902SRodney W. Grimes }
3529b50d902SRodney W. Grimes 
3539b50d902SRodney W. Grimes void
354cf0def93SJuli Mallett getformat(void)
3559b50d902SRodney W. Grimes {
356edd7b267SDima Dorfman 	char	*p, *p2;
3575249bd84SSheldon Hearn 	int dot, hash, space, sign, numbers = 0;
35815ba0427SDima Dorfman 	size_t sz;
3599b50d902SRodney W. Grimes 
3609b50d902SRodney W. Grimes 	if (boring)				/* no need to bother */
3619b50d902SRodney W. Grimes 		return;
3629b50d902SRodney W. Grimes 	for (p = format; *p; p++)		/* look for '%' */
3639b50d902SRodney W. Grimes 		if (*p == '%' && *(p+1) != '%')	/* leave %% alone */
3649b50d902SRodney W. Grimes 			break;
36515ba0427SDima Dorfman 	sz = sizeof(format) - strlen(format) - 1;
36615ba0427SDima Dorfman 	if (!*p && !chardata) {
36715ba0427SDima Dorfman 		if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
36815ba0427SDima Dorfman 			errx(1, "-w word too long");
36915ba0427SDima Dorfman 	} else if (!*p && chardata) {
37015ba0427SDima Dorfman 		if (strlcpy(p, "%c", sz) >= sz)
37115ba0427SDima Dorfman 			errx(1, "-w word too long");
3728db8a33bSSheldon Hearn 		intdata = 1;
37315ba0427SDima Dorfman 	} else if (!*(p+1)) {
37415ba0427SDima Dorfman 		if (sz <= 0)
37515ba0427SDima Dorfman 			errx(1, "-w word too long");
3769b50d902SRodney W. Grimes 		strcat(format, "%");		/* cannot end in single '%' */
37715ba0427SDima Dorfman 	} else {
3788db8a33bSSheldon Hearn 		/*
3798db8a33bSSheldon Hearn 		 * Allow conversion format specifiers of the form
3808db8a33bSSheldon Hearn 		 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
3818db8a33bSSheldon Hearn 		 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
3828db8a33bSSheldon Hearn 		 */
383edd7b267SDima Dorfman 		p2 = p++;
3848db8a33bSSheldon Hearn 		dot = hash = space = sign = numbers = 0;
3858db8a33bSSheldon Hearn 		while (!isalpha(*p)) {
3868db8a33bSSheldon Hearn 			if (isdigit(*p)) {
3878db8a33bSSheldon Hearn 				numbers++;
3889b50d902SRodney W. Grimes 				p++;
3898db8a33bSSheldon Hearn 			} else if ((*p == '#' && !(numbers|dot|sign|space|
3908db8a33bSSheldon Hearn 			    hash++)) ||
3918db8a33bSSheldon Hearn 			    (*p == ' ' && !(numbers|dot|space++)) ||
3928db8a33bSSheldon Hearn 			    ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
3938db8a33bSSheldon Hearn 			    || (*p == '.' && !(dot++)))
3948db8a33bSSheldon Hearn 				p++;
3958db8a33bSSheldon Hearn 			else
3965249bd84SSheldon Hearn 				goto fmt_broken;
3975249bd84SSheldon Hearn 		}
3985249bd84SSheldon Hearn 		if (*p == 'l') {
3995249bd84SSheldon Hearn 			longdata = 1;
4005249bd84SSheldon Hearn 			if (*++p == 'l') {
4015249bd84SSheldon Hearn 				if (p[1] != '\0')
4025249bd84SSheldon Hearn 					p++;
4035249bd84SSheldon Hearn 				goto fmt_broken;
4045249bd84SSheldon Hearn 			}
4058db8a33bSSheldon Hearn 		}
4069b50d902SRodney W. Grimes 		switch (*p) {
4078db8a33bSSheldon Hearn 		case 'o': case 'u': case 'x': case 'X':
4088db8a33bSSheldon Hearn 			intdata = nosign = 1;
4099b50d902SRodney W. Grimes 			break;
4108db8a33bSSheldon Hearn 		case 'd': case 'i':
4118db8a33bSSheldon Hearn 			intdata = 1;
4128db8a33bSSheldon Hearn 			break;
4138db8a33bSSheldon Hearn 		case 'D':
4145249bd84SSheldon Hearn 			if (!longdata) {
4158db8a33bSSheldon Hearn 				intdata = 1;
4168db8a33bSSheldon Hearn 				break;
4178db8a33bSSheldon Hearn 			}
4188db8a33bSSheldon Hearn 		case 'O': case 'U':
4195249bd84SSheldon Hearn 			if (!longdata) {
4208db8a33bSSheldon Hearn 				intdata = nosign = 1;
4218db8a33bSSheldon Hearn 				break;
4228db8a33bSSheldon Hearn 			}
4238db8a33bSSheldon Hearn 		case 'c':
4245249bd84SSheldon Hearn 			if (!(intdata | longdata)) {
4258db8a33bSSheldon Hearn 				chardata = 1;
4268db8a33bSSheldon Hearn 				break;
4278db8a33bSSheldon Hearn 			}
4285249bd84SSheldon Hearn 		case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
4298db8a33bSSheldon Hearn 		case '$': case '*':
4305249bd84SSheldon Hearn 			goto fmt_broken;
4318db8a33bSSheldon Hearn 		case 'f': case 'e': case 'g': case 'E': case 'G':
4325249bd84SSheldon Hearn 			if (!longdata)
4338db8a33bSSheldon Hearn 				break;
4348db8a33bSSheldon Hearn 			/* FALLTHROUGH */
4359b50d902SRodney W. Grimes 		default:
4365249bd84SSheldon Hearn fmt_broken:
4378db8a33bSSheldon Hearn 			*++p = '\0';
438edd7b267SDima Dorfman 			errx(1, "illegal or unsupported format '%s'", p2);
4398db8a33bSSheldon Hearn 			/* NOTREACHED */
4408db8a33bSSheldon Hearn 		}
4418db8a33bSSheldon Hearn 		while (*++p)
4428db8a33bSSheldon Hearn 			if (*p == '%' && *(p+1) && *(p+1) != '%')
4438db8a33bSSheldon Hearn 				errx(1, "too many conversions");
4448db8a33bSSheldon Hearn 			else if (*p == '%' && *(p+1) == '%')
4458db8a33bSSheldon Hearn 				p++;
4468db8a33bSSheldon Hearn 			else if (*p == '%' && !*(p+1)) {
4478db8a33bSSheldon Hearn 				strcat(format, "%");
4489b50d902SRodney W. Grimes 				break;
4499b50d902SRodney W. Grimes 			}
4509b50d902SRodney W. Grimes 	}
4519b50d902SRodney W. Grimes }
452