xref: /freebsd/usr.bin/jot/jot.c (revision 9d288406f538c1c8c580c6679a95b34869398988)
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
359b50d902SRodney W. Grimes static 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
419b50d902SRodney W. Grimes static char sccsid[] = "@(#)jot.c	8.1 (Berkeley) 6/6/93";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * jot - print sequential or random data
469b50d902SRodney W. Grimes  *
479b50d902SRodney W. Grimes  * Author:  John Kunze, Office of Comp. Affairs, UCB
489b50d902SRodney W. Grimes  */
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <ctype.h>
519b50d902SRodney W. Grimes #include <limits.h>
529b50d902SRodney W. Grimes #include <stdio.h>
539b50d902SRodney W. Grimes #include <stdlib.h>
549b50d902SRodney W. Grimes #include <string.h>
559b50d902SRodney W. Grimes #include <time.h>
569d288406SAndrey A. Chernov #include <unistd.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #define	REPS_DEF	100
599b50d902SRodney W. Grimes #define	BEGIN_DEF	1
609b50d902SRodney W. Grimes #define	ENDER_DEF	100
619b50d902SRodney W. Grimes #define	STEP_DEF	1
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes #define	isdefault(s)	(strcmp((s), "-") == 0)
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes double	begin;
669b50d902SRodney W. Grimes double	ender;
679b50d902SRodney W. Grimes double	s;
689b50d902SRodney W. Grimes long	reps;
699b50d902SRodney W. Grimes int	randomize;
709b50d902SRodney W. Grimes int	infinity;
719b50d902SRodney W. Grimes int	boring;
729b50d902SRodney W. Grimes int	prec;
739b50d902SRodney W. Grimes int	dox;
749b50d902SRodney W. Grimes int	chardata;
759b50d902SRodney W. Grimes int	nofinalnl;
769b50d902SRodney W. Grimes char	*sepstring = "\n";
779b50d902SRodney W. Grimes char	format[BUFSIZ];
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes void	error __P((char *, char *));
809b50d902SRodney W. Grimes void	getargs __P((int, char *[]));
819b50d902SRodney W. Grimes void	getformat __P((void));
829b50d902SRodney W. Grimes int	getprec __P((char *));
839b50d902SRodney W. Grimes void	putdata __P((double, long));
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes int
869b50d902SRodney W. Grimes main(argc, argv)
879b50d902SRodney W. Grimes 	int argc;
889b50d902SRodney W. Grimes 	char *argv[];
899b50d902SRodney W. Grimes {
909b50d902SRodney W. Grimes 	double	xd, yd;
919b50d902SRodney W. Grimes 	long	id;
929b50d902SRodney W. Grimes 	register double	*x = &xd;
939b50d902SRodney W. Grimes 	register double	*y = &yd;
949b50d902SRodney W. Grimes 	register long	*i = &id;
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes 	getargs(argc, argv);
979b50d902SRodney W. Grimes 	if (randomize) {
989b50d902SRodney W. Grimes 		*x = (ender - begin) * (ender > begin ? 1 : -1);
999d288406SAndrey A. Chernov 		srandom((unsigned) s);
1009b50d902SRodney W. Grimes 		for (*i = 1; *i <= reps || infinity; (*i)++) {
1019d288406SAndrey A. Chernov 			*y = (double) random() / LONG_MAX;
1029b50d902SRodney W. Grimes 			putdata(*y * *x + begin, reps - *i);
1039b50d902SRodney W. Grimes 		}
1049b50d902SRodney W. Grimes 	}
1059b50d902SRodney W. Grimes 	else
1069b50d902SRodney W. Grimes 		for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s)
1079b50d902SRodney W. Grimes 			putdata(*x, reps - *i);
1089b50d902SRodney W. Grimes 	if (!nofinalnl)
1099b50d902SRodney W. Grimes 		putchar('\n');
1109b50d902SRodney W. Grimes 	exit(0);
1119b50d902SRodney W. Grimes }
1129b50d902SRodney W. Grimes 
1139b50d902SRodney W. Grimes void
1149b50d902SRodney W. Grimes getargs(ac, av)
1159b50d902SRodney W. Grimes 	int ac;
1169b50d902SRodney W. Grimes 	char *av[];
1179b50d902SRodney W. Grimes {
1189b50d902SRodney W. Grimes 	register unsigned int	mask = 0;
1199b50d902SRodney W. Grimes 	register int		n = 0;
1209b50d902SRodney W. Grimes 
1219b50d902SRodney W. Grimes 	while (--ac && **++av == '-' && !isdefault(*av))
1229b50d902SRodney W. Grimes 		switch ((*av)[1]) {
1239b50d902SRodney W. Grimes 		case 'r':
1249b50d902SRodney W. Grimes 			randomize = 1;
1259b50d902SRodney W. Grimes 			break;
1269b50d902SRodney W. Grimes 		case 'c':
1279b50d902SRodney W. Grimes 			chardata = 1;
1289b50d902SRodney W. Grimes 			break;
1299b50d902SRodney W. Grimes 		case 'n':
1309b50d902SRodney W. Grimes 			nofinalnl = 1;
1319b50d902SRodney W. Grimes 			break;
1329b50d902SRodney W. Grimes 		case 'b':
1339b50d902SRodney W. Grimes 			boring = 1;
1349b50d902SRodney W. Grimes 		case 'w':
1359b50d902SRodney W. Grimes 			if ((*av)[2])
1369b50d902SRodney W. Grimes 				strcpy(format, *av + 2);
1379b50d902SRodney W. Grimes 			else if (!--ac)
1389b50d902SRodney W. Grimes 				error("Need context word after -w or -b", "");
1399b50d902SRodney W. Grimes 			else
1409b50d902SRodney W. Grimes 				strcpy(format, *++av);
1419b50d902SRodney W. Grimes 			break;
1429b50d902SRodney W. Grimes 		case 's':
1439b50d902SRodney W. Grimes 			if ((*av)[2])
144e29b080fSJoerg Wunsch 				sepstring = *av + 2;
1459b50d902SRodney W. Grimes 			else if (!--ac)
1469b50d902SRodney W. Grimes 				error("Need string after -s", "");
1479b50d902SRodney W. Grimes 			else
148e29b080fSJoerg Wunsch 				sepstring = *++av;
1499b50d902SRodney W. Grimes 			break;
1509b50d902SRodney W. Grimes 		case 'p':
1519b50d902SRodney W. Grimes 			if ((*av)[2])
1529b50d902SRodney W. Grimes 				prec = atoi(*av + 2);
1539b50d902SRodney W. Grimes 			else if (!--ac)
1549b50d902SRodney W. Grimes 				error("Need number after -p", "");
1559b50d902SRodney W. Grimes 			else
1569b50d902SRodney W. Grimes 				prec = atoi(*++av);
1579b50d902SRodney W. Grimes 			if (prec <= 0)
1589b50d902SRodney W. Grimes 				error("Bad precision value", "");
1599b50d902SRodney W. Grimes 			break;
1609b50d902SRodney W. Grimes 		default:
1619b50d902SRodney W. Grimes 			error("Unknown option %s", *av);
1629b50d902SRodney W. Grimes 		}
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes 	switch (ac) {	/* examine args right to left, falling thru cases */
1659b50d902SRodney W. Grimes 	case 4:
1669b50d902SRodney W. Grimes 		if (!isdefault(av[3])) {
1679b50d902SRodney W. Grimes 			if (!sscanf(av[3], "%lf", &s))
1689b50d902SRodney W. Grimes 				error("Bad s value:  %s", av[3]);
1699b50d902SRodney W. Grimes 			mask |= 01;
1709b50d902SRodney W. Grimes 		}
1719b50d902SRodney W. Grimes 	case 3:
1729b50d902SRodney W. Grimes 		if (!isdefault(av[2])) {
1739b50d902SRodney W. Grimes 			if (!sscanf(av[2], "%lf", &ender))
1749b50d902SRodney W. Grimes 				ender = av[2][strlen(av[2])-1];
1759b50d902SRodney W. Grimes 			mask |= 02;
1769b50d902SRodney W. Grimes 			if (!prec)
1779b50d902SRodney W. Grimes 				n = getprec(av[2]);
1789b50d902SRodney W. Grimes 		}
1799b50d902SRodney W. Grimes 	case 2:
1809b50d902SRodney W. Grimes 		if (!isdefault(av[1])) {
1819b50d902SRodney W. Grimes 			if (!sscanf(av[1], "%lf", &begin))
1829b50d902SRodney W. Grimes 				begin = av[1][strlen(av[1])-1];
1839b50d902SRodney W. Grimes 			mask |= 04;
1849b50d902SRodney W. Grimes 			if (!prec)
1859b50d902SRodney W. Grimes 				prec = getprec(av[1]);
1869b50d902SRodney W. Grimes 			if (n > prec)		/* maximum precision */
1879b50d902SRodney W. Grimes 				prec = n;
1889b50d902SRodney W. Grimes 		}
1899b50d902SRodney W. Grimes 	case 1:
1909b50d902SRodney W. Grimes 		if (!isdefault(av[0])) {
1919b50d902SRodney W. Grimes 			if (!sscanf(av[0], "%ld", &reps))
1929b50d902SRodney W. Grimes 				error("Bad reps value:  %s", av[0]);
1939b50d902SRodney W. Grimes 			mask |= 010;
1949b50d902SRodney W. Grimes 		}
1959b50d902SRodney W. Grimes 		break;
1969b50d902SRodney W. Grimes 	case 0:
1979b50d902SRodney W. Grimes 		error("jot - print sequential or random data", "");
1989b50d902SRodney W. Grimes 	default:
1999b50d902SRodney W. Grimes 		error("Too many arguments.  What do you mean by %s?", av[4]);
2009b50d902SRodney W. Grimes 	}
2019b50d902SRodney W. Grimes 	getformat();
2029b50d902SRodney W. Grimes 	while (mask)	/* 4 bit mask has 1's where last 4 args were given */
2039b50d902SRodney W. Grimes 		switch (mask) {	/* fill in the 0's by default or computation */
2049b50d902SRodney W. Grimes 		case 001:
2059b50d902SRodney W. Grimes 			reps = REPS_DEF;
2069b50d902SRodney W. Grimes 			mask = 011;
2079b50d902SRodney W. Grimes 			break;
2089b50d902SRodney W. Grimes 		case 002:
2099b50d902SRodney W. Grimes 			reps = REPS_DEF;
2109b50d902SRodney W. Grimes 			mask = 012;
2119b50d902SRodney W. Grimes 			break;
2129b50d902SRodney W. Grimes 		case 003:
2139b50d902SRodney W. Grimes 			reps = REPS_DEF;
2149b50d902SRodney W. Grimes 			mask = 013;
2159b50d902SRodney W. Grimes 			break;
2169b50d902SRodney W. Grimes 		case 004:
2179b50d902SRodney W. Grimes 			reps = REPS_DEF;
2189b50d902SRodney W. Grimes 			mask = 014;
2199b50d902SRodney W. Grimes 			break;
2209b50d902SRodney W. Grimes 		case 005:
2219b50d902SRodney W. Grimes 			reps = REPS_DEF;
2229b50d902SRodney W. Grimes 			mask = 015;
2239b50d902SRodney W. Grimes 			break;
2249b50d902SRodney W. Grimes 		case 006:
2259b50d902SRodney W. Grimes 			reps = REPS_DEF;
2269b50d902SRodney W. Grimes 			mask = 016;
2279b50d902SRodney W. Grimes 			break;
2289b50d902SRodney W. Grimes 		case 007:
2299b50d902SRodney W. Grimes 			if (randomize) {
2309b50d902SRodney W. Grimes 				reps = REPS_DEF;
2319b50d902SRodney W. Grimes 				mask = 0;
2329b50d902SRodney W. Grimes 				break;
2339b50d902SRodney W. Grimes 			}
2349b50d902SRodney W. Grimes 			if (s == 0.0) {
2359b50d902SRodney W. Grimes 				reps = 0;
2369b50d902SRodney W. Grimes 				mask = 0;
2379b50d902SRodney W. Grimes 				break;
2389b50d902SRodney W. Grimes 			}
2399b50d902SRodney W. Grimes 			reps = (ender - begin + s) / s;
2409b50d902SRodney W. Grimes 			if (reps <= 0)
2419b50d902SRodney W. Grimes 				error("Impossible stepsize", "");
2429b50d902SRodney W. Grimes 			mask = 0;
2439b50d902SRodney W. Grimes 			break;
2449b50d902SRodney W. Grimes 		case 010:
2459b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
2469b50d902SRodney W. Grimes 			mask = 014;
2479b50d902SRodney W. Grimes 			break;
2489b50d902SRodney W. Grimes 		case 011:
2499b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
2509b50d902SRodney W. Grimes 			mask = 015;
2519b50d902SRodney W. Grimes 			break;
2529b50d902SRodney W. Grimes 		case 012:
2539d288406SAndrey A. Chernov 			s = (randomize ? time(NULL) ^ getpid() : STEP_DEF);
2549b50d902SRodney W. Grimes 			mask = 013;
2559b50d902SRodney W. Grimes 			break;
2569b50d902SRodney W. Grimes 		case 013:
2579b50d902SRodney W. Grimes 			if (randomize)
2589b50d902SRodney W. Grimes 				begin = BEGIN_DEF;
2599b50d902SRodney W. Grimes 			else if (reps == 0)
2609b50d902SRodney W. Grimes 				error("Must specify begin if reps == 0", "");
2619b50d902SRodney W. Grimes 			begin = ender - reps * s + s;
2629b50d902SRodney W. Grimes 			mask = 0;
2639b50d902SRodney W. Grimes 			break;
2649b50d902SRodney W. Grimes 		case 014:
2659d288406SAndrey A. Chernov 			s = (randomize ? time(NULL) ^ getpid() : STEP_DEF);
2669b50d902SRodney W. Grimes 			mask = 015;
2679b50d902SRodney W. Grimes 			break;
2689b50d902SRodney W. Grimes 		case 015:
2699b50d902SRodney W. Grimes 			if (randomize)
2709b50d902SRodney W. Grimes 				ender = ENDER_DEF;
2719b50d902SRodney W. Grimes 			else
2729b50d902SRodney W. Grimes 				ender = begin + reps * s - s;
2739b50d902SRodney W. Grimes 			mask = 0;
2749b50d902SRodney W. Grimes 			break;
2759b50d902SRodney W. Grimes 		case 016:
2769b50d902SRodney W. Grimes 			if (randomize)
2779d288406SAndrey A. Chernov 				s = time(NULL) ^ getpid();
2789b50d902SRodney W. Grimes 			else if (reps == 0)
2799b50d902SRodney W. Grimes 				error("Infinite sequences cannot be bounded",
2809b50d902SRodney W. Grimes 				    "");
2819b50d902SRodney W. Grimes 			else if (reps == 1)
2829b50d902SRodney W. Grimes 				s = 0.0;
2839b50d902SRodney W. Grimes 			else
2849b50d902SRodney W. Grimes 				s = (ender - begin) / (reps - 1);
2859b50d902SRodney W. Grimes 			mask = 0;
2869b50d902SRodney W. Grimes 			break;
2879b50d902SRodney W. Grimes 		case 017:		/* if reps given and implied, */
2889b50d902SRodney W. Grimes 			if (!randomize && s != 0.0) {
2899b50d902SRodney W. Grimes 				long t = (ender - begin + s) / s;
2909b50d902SRodney W. Grimes 				if (t <= 0)
2919b50d902SRodney W. Grimes 					error("Impossible stepsize", "");
2929b50d902SRodney W. Grimes 				if (t < reps)		/* take lesser */
2939b50d902SRodney W. Grimes 					reps = t;
2949b50d902SRodney W. Grimes 			}
2959b50d902SRodney W. Grimes 			mask = 0;
2969b50d902SRodney W. Grimes 			break;
2979b50d902SRodney W. Grimes 		default:
2989b50d902SRodney W. Grimes 			error("Bad mask", "");
2999b50d902SRodney W. Grimes 		}
3009b50d902SRodney W. Grimes 	if (reps == 0)
3019b50d902SRodney W. Grimes 		infinity = 1;
3029b50d902SRodney W. Grimes }
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes void
3059b50d902SRodney W. Grimes putdata(x, notlast)
3069b50d902SRodney W. Grimes 	double x;
3079b50d902SRodney W. Grimes 	long notlast;
3089b50d902SRodney W. Grimes {
3099b50d902SRodney W. Grimes 	long		d = x;
3109b50d902SRodney W. Grimes 	register long	*dp = &d;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	if (boring)				/* repeated word */
3139b50d902SRodney W. Grimes 		printf(format);
3149b50d902SRodney W. Grimes 	else if (dox)				/* scalar */
3159b50d902SRodney W. Grimes 		printf(format, *dp);
3169b50d902SRodney W. Grimes 	else					/* real */
3179b50d902SRodney W. Grimes 		printf(format, x);
3189b50d902SRodney W. Grimes 	if (notlast != 0)
3199b50d902SRodney W. Grimes 		fputs(sepstring, stdout);
3209b50d902SRodney W. Grimes }
3219b50d902SRodney W. Grimes 
3229b50d902SRodney W. Grimes void
3239b50d902SRodney W. Grimes error(msg, s)
3249b50d902SRodney W. Grimes 	char *msg, *s;
3259b50d902SRodney W. Grimes {
3269b50d902SRodney W. Grimes 	fprintf(stderr, "jot: ");
3279b50d902SRodney W. Grimes 	fprintf(stderr, msg, s);
3289b50d902SRodney W. Grimes 	fprintf(stderr,
3299b50d902SRodney W. Grimes 	    "\nusage:  jot [ options ] [ reps [ begin [ end [ s ] ] ] ]\n");
3309b50d902SRodney W. Grimes 	if (strncmp("jot - ", msg, 6) == 0)
3319b50d902SRodney W. Grimes 		fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
3329b50d902SRodney W. Grimes 			"-r		random data\n",
3339b50d902SRodney W. Grimes 			"-c		character data\n",
3349b50d902SRodney W. Grimes 			"-n		no final newline\n",
3359b50d902SRodney W. Grimes 			"-b word		repeated word\n",
3369b50d902SRodney W. Grimes 			"-w word		context word\n",
3379b50d902SRodney W. Grimes 			"-s string	data separator\n",
3389b50d902SRodney W. Grimes 			"-p precision	number of characters\n");
3399b50d902SRodney W. Grimes 	exit(1);
3409b50d902SRodney W. Grimes }
3419b50d902SRodney W. Grimes 
3429b50d902SRodney W. Grimes int
3439b50d902SRodney W. Grimes getprec(s)
3449b50d902SRodney W. Grimes 	char *s;
3459b50d902SRodney W. Grimes {
3469b50d902SRodney W. Grimes 	register char	*p;
3479b50d902SRodney W. Grimes 	register char	*q;
3489b50d902SRodney W. Grimes 
3499b50d902SRodney W. Grimes 	for (p = s; *p; p++)
3509b50d902SRodney W. Grimes 		if (*p == '.')
3519b50d902SRodney W. Grimes 			break;
3529b50d902SRodney W. Grimes 	if (!*p)
3539b50d902SRodney W. Grimes 		return (0);
3549b50d902SRodney W. Grimes 	for (q = ++p; *p; p++)
3559b50d902SRodney W. Grimes 		if (!isdigit(*p))
3569b50d902SRodney W. Grimes 			break;
3579b50d902SRodney W. Grimes 	return (p - q);
3589b50d902SRodney W. Grimes }
3599b50d902SRodney W. Grimes 
3609b50d902SRodney W. Grimes void
3619b50d902SRodney W. Grimes getformat()
3629b50d902SRodney W. Grimes {
3639b50d902SRodney W. Grimes 	register char	*p;
3649b50d902SRodney W. Grimes 
3659b50d902SRodney W. Grimes 	if (boring)				/* no need to bother */
3669b50d902SRodney W. Grimes 		return;
3679b50d902SRodney W. Grimes 	for (p = format; *p; p++)		/* look for '%' */
3689b50d902SRodney W. Grimes 		if (*p == '%' && *(p+1) != '%')	/* leave %% alone */
3699b50d902SRodney W. Grimes 			break;
3709b50d902SRodney W. Grimes 	if (!*p && !chardata)
3719b50d902SRodney W. Grimes 		sprintf(p, "%%.%df", prec);
3729b50d902SRodney W. Grimes 	else if (!*p && chardata) {
3739b50d902SRodney W. Grimes 		strcpy(p, "%c");
3749b50d902SRodney W. Grimes 		dox = 1;
3759b50d902SRodney W. Grimes 	}
3769b50d902SRodney W. Grimes 	else if (!*(p+1))
3779b50d902SRodney W. Grimes 		strcat(format, "%");		/* cannot end in single '%' */
3789b50d902SRodney W. Grimes 	else {
3799b50d902SRodney W. Grimes 		while (!isalpha(*p))
3809b50d902SRodney W. Grimes 			p++;
3819b50d902SRodney W. Grimes 		switch (*p) {
3829b50d902SRodney W. Grimes 		case 'f': case 'e': case 'g': case '%':
3839b50d902SRodney W. Grimes 			break;
3849b50d902SRodney W. Grimes 		case 's':
3859b50d902SRodney W. Grimes 			error("Cannot convert numeric data to strings", "");
3869b50d902SRodney W. Grimes 			break;
3879b50d902SRodney W. Grimes 		/* case 'd': case 'o': case 'x': case 'D': case 'O': case 'X':
3889b50d902SRodney W. Grimes 		case 'c': case 'u': */
3899b50d902SRodney W. Grimes 		default:
3909b50d902SRodney W. Grimes 			dox = 1;
3919b50d902SRodney W. Grimes 			break;
3929b50d902SRodney W. Grimes 		}
3939b50d902SRodney W. Grimes 	}
3949b50d902SRodney W. Grimes }
395