xref: /freebsd/usr.bin/jot/jot.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes /*
339b50d902SRodney W. Grimes  * jot - print sequential or random data
349b50d902SRodney W. Grimes  *
359b50d902SRodney W. Grimes  * Author:  John Kunze, Office of Comp. Affairs, UCB
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
381532e656SConrad Meyer #include <sys/capsicum.h>
391532e656SConrad Meyer #include <capsicum_helpers.h>
409b50d902SRodney W. Grimes #include <ctype.h>
417bd7ad50SPhilippe Charnier #include <err.h>
421532e656SConrad Meyer #include <errno.h>
439b50d902SRodney W. Grimes #include <limits.h>
449b50d902SRodney W. Grimes #include <stdio.h>
450ad736e6SMike Barcroft #include <stdint.h>
469b50d902SRodney W. Grimes #include <stdlib.h>
47a26a6612SDiomidis Spinellis #include <stdbool.h>
489b50d902SRodney W. Grimes #include <string.h>
49821df508SXin LI #include <time.h>
509d288406SAndrey A. Chernov #include <unistd.h>
519b50d902SRodney W. Grimes 
523241f274SDiomidis Spinellis /* Defaults */
539b50d902SRodney W. Grimes #define	REPS_DEF	100
549b50d902SRodney W. Grimes #define	BEGIN_DEF	1
559b50d902SRodney W. Grimes #define	ENDER_DEF	100
569b50d902SRodney W. Grimes #define	STEP_DEF	1
579b50d902SRodney W. Grimes 
583241f274SDiomidis Spinellis /* Flags of options that have been set */
59afe53a49SDiomidis Spinellis #define HAVE_STEP	1
60afe53a49SDiomidis Spinellis #define HAVE_ENDER	2
61afe53a49SDiomidis Spinellis #define HAVE_BEGIN	4
62afe53a49SDiomidis Spinellis #define HAVE_REPS	8
63afe53a49SDiomidis Spinellis 
64fee14f30SDiomidis Spinellis #define	is_default(s)	(*(s) == 0 || strcmp((s), "-") == 0)
659b50d902SRodney W. Grimes 
663241f274SDiomidis Spinellis static bool	boring;
679c24af3dSBrian Somers static int	prec = -1;
683241f274SDiomidis Spinellis static bool	longdata;
693241f274SDiomidis Spinellis static bool	intdata;
703241f274SDiomidis Spinellis static bool	chardata;
713241f274SDiomidis Spinellis static bool	nosign;
723241f274SDiomidis Spinellis static const	char *sepstring = "\n";
733241f274SDiomidis Spinellis static char	format[BUFSIZ];
749b50d902SRodney W. Grimes 
753241f274SDiomidis Spinellis static void	getformat(void);
763241f274SDiomidis Spinellis static int	getprec(const char *);
773241f274SDiomidis Spinellis static int	putdata(double, bool);
78f1bb2cd2SWarner Losh static void	usage(void);
799b50d902SRodney W. Grimes 
809b50d902SRodney W. Grimes int
main(int argc,char ** argv)81cf0def93SJuli Mallett main(int argc, char **argv)
829b50d902SRodney W. Grimes {
831532e656SConrad Meyer 	cap_rights_t rights;
843241f274SDiomidis Spinellis 	bool	have_format = false;
853241f274SDiomidis Spinellis 	bool	infinity = false;
863241f274SDiomidis Spinellis 	bool	nofinalnl = false;
873241f274SDiomidis Spinellis 	bool	randomize = false;
883241f274SDiomidis Spinellis 	bool	use_random = false;
893241f274SDiomidis Spinellis 	int	ch;
903241f274SDiomidis Spinellis 	int	mask = 0;
913241f274SDiomidis Spinellis 	int	n = 0;
9258a654f6SBrian Somers 	double	begin = BEGIN_DEF;
933241f274SDiomidis Spinellis 	double	divisor;
9458a654f6SBrian Somers 	double	ender = ENDER_DEF;
9558a654f6SBrian Somers 	double	s = STEP_DEF;
9655f965aeSDiomidis Spinellis 	double	x, y;
9755f965aeSDiomidis Spinellis 	long	i;
9858a654f6SBrian Somers 	long	reps = REPS_DEF;
999b50d902SRodney W. Grimes 
1001532e656SConrad Meyer 	if (caph_limit_stdio() < 0)
1011532e656SConrad Meyer 		err(1, "unable to limit rights for stdio");
1021532e656SConrad Meyer 	cap_rights_init(&rights);
103377421dfSMariusz Zaborski 	if (caph_rights_limit(STDIN_FILENO, &rights) < 0)
1041532e656SConrad Meyer 		err(1, "unable to limit rights for stdin");
1051532e656SConrad Meyer 
1061532e656SConrad Meyer 	/*
1071532e656SConrad Meyer 	 * Cache NLS data, for strerror, for err(3), before entering capability
1081532e656SConrad Meyer 	 * mode.
1091532e656SConrad Meyer 	 */
1101532e656SConrad Meyer 	caph_cache_catpages();
1111532e656SConrad Meyer 
1127672a014SMariusz Zaborski 	if (caph_enter() < 0)
1131532e656SConrad Meyer 		err(1, "unable to enter capability mode");
1141532e656SConrad Meyer 
1153241f274SDiomidis Spinellis 	while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1)
116cf0def93SJuli Mallett 		switch (ch) {
1179b50d902SRodney W. Grimes 		case 'b':
1183241f274SDiomidis Spinellis 			boring = true;
11915ba0427SDima Dorfman 			/* FALLTHROUGH */
1209b50d902SRodney W. Grimes 		case 'w':
12115ba0427SDima Dorfman 			if (strlcpy(format, optarg, sizeof(format)) >=
12215ba0427SDima Dorfman 			    sizeof(format))
12315ba0427SDima Dorfman 				errx(1, "-%c word too long", ch);
124d129c68aSDiomidis Spinellis 			have_format = true;
1259b50d902SRodney W. Grimes 			break;
1263241f274SDiomidis Spinellis 		case 'c':
1273241f274SDiomidis Spinellis 			chardata = true;
1283241f274SDiomidis Spinellis 			break;
1293241f274SDiomidis Spinellis 		case 'n':
1303241f274SDiomidis Spinellis 			nofinalnl = true;
1319b50d902SRodney W. Grimes 			break;
1329b50d902SRodney W. Grimes 		case 'p':
13315ba0427SDima Dorfman 			prec = atoi(optarg);
1349c24af3dSBrian Somers 			if (prec < 0)
1357bd7ad50SPhilippe Charnier 				errx(1, "bad precision value");
136d129c68aSDiomidis Spinellis 			have_format = true;
1379b50d902SRodney W. Grimes 			break;
1383241f274SDiomidis Spinellis 		case 'r':
1393241f274SDiomidis Spinellis 			randomize = true;
1403241f274SDiomidis Spinellis 			break;
1413241f274SDiomidis Spinellis 		case 's':
1423241f274SDiomidis Spinellis 			sepstring = optarg;
1433241f274SDiomidis Spinellis 			break;
1449b50d902SRodney W. Grimes 		default:
1457bd7ad50SPhilippe Charnier 			usage();
1469b50d902SRodney W. Grimes 		}
14715ba0427SDima Dorfman 	argc -= optind;
14815ba0427SDima Dorfman 	argv += optind;
1499b50d902SRodney W. Grimes 
15015ba0427SDima Dorfman 	switch (argc) {	/* examine args right to left, falling thru cases */
1519b50d902SRodney W. Grimes 	case 4:
15215ba0427SDima Dorfman 		if (!is_default(argv[3])) {
15315ba0427SDima Dorfman 			if (!sscanf(argv[3], "%lf", &s))
15415ba0427SDima Dorfman 				errx(1, "bad s value: %s", argv[3]);
155afe53a49SDiomidis Spinellis 			mask |= HAVE_STEP;
156a26a6612SDiomidis Spinellis 			if (randomize)
157d129c68aSDiomidis Spinellis 				use_random = true;
1589b50d902SRodney W. Grimes 		}
159afe53a49SDiomidis Spinellis 		/* FALLTHROUGH */
1609b50d902SRodney W. Grimes 	case 3:
16115ba0427SDima Dorfman 		if (!is_default(argv[2])) {
16215ba0427SDima Dorfman 			if (!sscanf(argv[2], "%lf", &ender))
16315ba0427SDima Dorfman 				ender = argv[2][strlen(argv[2])-1];
164afe53a49SDiomidis Spinellis 			mask |= HAVE_ENDER;
1659c24af3dSBrian Somers 			if (prec < 0)
16615ba0427SDima Dorfman 				n = getprec(argv[2]);
1679b50d902SRodney W. Grimes 		}
168afe53a49SDiomidis Spinellis 		/* FALLTHROUGH */
1699b50d902SRodney W. Grimes 	case 2:
17015ba0427SDima Dorfman 		if (!is_default(argv[1])) {
17115ba0427SDima Dorfman 			if (!sscanf(argv[1], "%lf", &begin))
17215ba0427SDima Dorfman 				begin = argv[1][strlen(argv[1])-1];
173afe53a49SDiomidis Spinellis 			mask |= HAVE_BEGIN;
1749c24af3dSBrian Somers 			if (prec < 0)
17515ba0427SDima Dorfman 				prec = getprec(argv[1]);
1769b50d902SRodney W. Grimes 			if (n > prec)		/* maximum precision */
1779b50d902SRodney W. Grimes 				prec = n;
1789b50d902SRodney W. Grimes 		}
179afe53a49SDiomidis Spinellis 		/* FALLTHROUGH */
1809b50d902SRodney W. Grimes 	case 1:
18115ba0427SDima Dorfman 		if (!is_default(argv[0])) {
18215ba0427SDima Dorfman 			if (!sscanf(argv[0], "%ld", &reps))
18315ba0427SDima Dorfman 				errx(1, "bad reps value: %s", argv[0]);
184afe53a49SDiomidis Spinellis 			mask |= HAVE_REPS;
1859b50d902SRodney W. Grimes 		}
1869b50d902SRodney W. Grimes 		break;
1879b50d902SRodney W. Grimes 	case 0:
1887bd7ad50SPhilippe Charnier 		usage();
1899b50d902SRodney W. Grimes 	default:
19015ba0427SDima Dorfman 		errx(1, "too many arguments.  What do you mean by %s?",
19115ba0427SDima Dorfman 		    argv[4]);
1929b50d902SRodney W. Grimes 	}
1939b50d902SRodney W. Grimes 	getformat();
1949c24af3dSBrian Somers 
1959c24af3dSBrian Somers 	if (prec == -1)
1969c24af3dSBrian Somers 		prec = 0;
1979c24af3dSBrian Somers 
1989b50d902SRodney W. Grimes 	while (mask)	/* 4 bit mask has 1's where last 4 args were given */
1999b50d902SRodney W. Grimes 		switch (mask) {	/* fill in the 0's by default or computation */
200afe53a49SDiomidis Spinellis 		case HAVE_STEP:
201afe53a49SDiomidis Spinellis 		case HAVE_ENDER:
202afe53a49SDiomidis Spinellis 		case HAVE_ENDER | HAVE_STEP:
203afe53a49SDiomidis Spinellis 		case HAVE_BEGIN:
204afe53a49SDiomidis Spinellis 		case HAVE_BEGIN | HAVE_STEP:
2059b50d902SRodney W. Grimes 			reps = REPS_DEF;
206d737ec1aSDiomidis Spinellis 			mask |= HAVE_REPS;
2079b50d902SRodney W. Grimes 			break;
2087d71f06cSDiomidis Spinellis 		case HAVE_BEGIN | HAVE_ENDER:
2097d71f06cSDiomidis Spinellis 			s = ender > begin ? 1 : -1;
2107d71f06cSDiomidis Spinellis 			mask |= HAVE_STEP;
2117d71f06cSDiomidis Spinellis 			break;
212afe53a49SDiomidis Spinellis 		case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
213d737ec1aSDiomidis Spinellis 			if (randomize)
2149b50d902SRodney W. Grimes 				reps = REPS_DEF;
215d737ec1aSDiomidis Spinellis 			else if (s == 0.0)
2169b50d902SRodney W. Grimes 				reps = 0;
217d737ec1aSDiomidis Spinellis 			else
2189b50d902SRodney W. Grimes 				reps = (ender - begin + s) / s;
2199b50d902SRodney W. Grimes 			if (reps <= 0)
2207bd7ad50SPhilippe Charnier 				errx(1, "impossible stepsize");
2219b50d902SRodney W. Grimes 			mask = 0;
2229b50d902SRodney W. Grimes 			break;
223afe53a49SDiomidis Spinellis 		case HAVE_REPS:
224afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_STEP:
2259b50d902SRodney W. Grimes 			begin = BEGIN_DEF;
226d737ec1aSDiomidis Spinellis 			mask |= HAVE_BEGIN;
2279b50d902SRodney W. Grimes 			break;
228afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_ENDER:
229a26a6612SDiomidis Spinellis 			s = STEP_DEF;
230afe53a49SDiomidis Spinellis 			mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP;
2319b50d902SRodney W. Grimes 			break;
232afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_ENDER | HAVE_STEP:
2339b50d902SRodney W. Grimes 			if (randomize)
2349b50d902SRodney W. Grimes 				begin = BEGIN_DEF;
2359b50d902SRodney W. Grimes 			else if (reps == 0)
2367bd7ad50SPhilippe Charnier 				errx(1, "must specify begin if reps == 0");
2379b50d902SRodney W. Grimes 			begin = ender - reps * s + s;
2389b50d902SRodney W. Grimes 			mask = 0;
2399b50d902SRodney W. Grimes 			break;
240afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_BEGIN:
241a26a6612SDiomidis Spinellis 			s = STEP_DEF;
242afe53a49SDiomidis Spinellis 			mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP;
2439b50d902SRodney W. Grimes 			break;
244afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_BEGIN | HAVE_STEP:
2459b50d902SRodney W. Grimes 			if (randomize)
2469b50d902SRodney W. Grimes 				ender = ENDER_DEF;
2479b50d902SRodney W. Grimes 			else
2489b50d902SRodney W. Grimes 				ender = begin + reps * s - s;
2499b50d902SRodney W. Grimes 			mask = 0;
2509b50d902SRodney W. Grimes 			break;
251afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER:
252*814f2cd8SBryan Drewery 			if (!randomize) {
253a26a6612SDiomidis Spinellis 				if (reps == 0)
254*814f2cd8SBryan Drewery 					errx(1, "infinite sequences cannot "
255*814f2cd8SBryan Drewery 					    "be bounded");
2569b50d902SRodney W. Grimes 				else if (reps == 1)
2579b50d902SRodney W. Grimes 					s = 0.0;
2589b50d902SRodney W. Grimes 				else
2599b50d902SRodney W. Grimes 					s = (ender - begin) / (reps - 1);
260*814f2cd8SBryan Drewery 			}
2619b50d902SRodney W. Grimes 			mask = 0;
2629b50d902SRodney W. Grimes 			break;
263afe53a49SDiomidis Spinellis 		case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
264afe53a49SDiomidis Spinellis 			/* if reps given and implied, */
2659b50d902SRodney W. Grimes 			if (!randomize && s != 0.0) {
2669b50d902SRodney W. Grimes 				long t = (ender - begin + s) / s;
2679b50d902SRodney W. Grimes 				if (t <= 0)
2687bd7ad50SPhilippe Charnier 					errx(1, "impossible stepsize");
2699b50d902SRodney W. Grimes 				if (t < reps)		/* take lesser */
2709b50d902SRodney W. Grimes 					reps = t;
2719b50d902SRodney W. Grimes 			}
2729b50d902SRodney W. Grimes 			mask = 0;
2739b50d902SRodney W. Grimes 			break;
2749b50d902SRodney W. Grimes 		default:
2757bd7ad50SPhilippe Charnier 			errx(1, "bad mask");
2769b50d902SRodney W. Grimes 		}
2779b50d902SRodney W. Grimes 	if (reps == 0)
2783241f274SDiomidis Spinellis 		infinity = true;
27915ba0427SDima Dorfman 	if (randomize) {
280d129c68aSDiomidis Spinellis 		if (use_random) {
281a26a6612SDiomidis Spinellis 			srandom((unsigned long)s);
282d129c68aSDiomidis Spinellis 			divisor = (double)INT32_MAX + 1;
283d129c68aSDiomidis Spinellis 		} else
284d129c68aSDiomidis Spinellis 			divisor = (double)UINT32_MAX + 1;
285d129c68aSDiomidis Spinellis 
286d129c68aSDiomidis Spinellis 		/*
287d129c68aSDiomidis Spinellis 		 * Attempt to DWIM when the user has specified an
288d129c68aSDiomidis Spinellis 		 * integer range within that of the random number
289d129c68aSDiomidis Spinellis 		 * generator: distribute the numbers equally in
290d129c68aSDiomidis Spinellis 		 * the range [begin .. ender].  Jot's default %.0f
291d129c68aSDiomidis Spinellis 		 * format would make the appearance of the first and
292d129c68aSDiomidis Spinellis 		 * last specified value half as likely as the rest.
293d129c68aSDiomidis Spinellis 		 */
294d129c68aSDiomidis Spinellis 		if (!have_format && prec == 0 &&
295d129c68aSDiomidis Spinellis 		    begin >= 0 && begin < divisor &&
296d129c68aSDiomidis Spinellis 		    ender >= 0 && ender < divisor) {
2979c24af3dSBrian Somers 			if (begin <= ender)
298d129c68aSDiomidis Spinellis 				ender += 1;
2999c24af3dSBrian Somers 			else
3009c24af3dSBrian Somers 				begin += 1;
3013241f274SDiomidis Spinellis 			nosign = true;
3023241f274SDiomidis Spinellis 			intdata = true;
303d129c68aSDiomidis Spinellis 			(void)strlcpy(format,
304d129c68aSDiomidis Spinellis 			    chardata ? "%c" : "%u", sizeof(format));
305d129c68aSDiomidis Spinellis 		}
3069c24af3dSBrian Somers 		x = ender - begin;
30755f965aeSDiomidis Spinellis 		for (i = 1; i <= reps || infinity; i++) {
308d129c68aSDiomidis Spinellis 			if (use_random)
309d129c68aSDiomidis Spinellis 				y = random() / divisor;
310a26a6612SDiomidis Spinellis 			else
311d129c68aSDiomidis Spinellis 				y = arc4random() / divisor;
3123241f274SDiomidis Spinellis 			if (putdata(y * x + begin, !(reps - i)))
31315ba0427SDima Dorfman 				errx(1, "range error in conversion");
31415ba0427SDima Dorfman 		}
31515ba0427SDima Dorfman 	} else
31655f965aeSDiomidis Spinellis 		for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
3173241f274SDiomidis Spinellis 			if (putdata(x, !(reps - i)))
31815ba0427SDima Dorfman 				errx(1, "range error in conversion");
31915ba0427SDima Dorfman 	if (!nofinalnl)
32015ba0427SDima Dorfman 		putchar('\n');
32115ba0427SDima Dorfman 	exit(0);
3229b50d902SRodney W. Grimes }
3239b50d902SRodney W. Grimes 
3243241f274SDiomidis Spinellis /*
3253241f274SDiomidis Spinellis  * Send x to stdout using the specified format.
3263241f274SDiomidis Spinellis  * Last is  true if this is the set's last value.
3273241f274SDiomidis Spinellis  * Return 0 if OK, or a positive number if the number passed was
3283241f274SDiomidis Spinellis  * outside the range specified by the various flags.
3293241f274SDiomidis Spinellis  */
3303241f274SDiomidis Spinellis static int
putdata(double x,bool last)3313241f274SDiomidis Spinellis putdata(double x, bool last)
3329b50d902SRodney W. Grimes {
3339b50d902SRodney W. Grimes 
3345249bd84SSheldon Hearn 	if (boring)
335612740bdSKris Kennaway 		printf("%s", format);
3365249bd84SSheldon Hearn 	else if (longdata && nosign) {
3375249bd84SSheldon Hearn 		if (x <= (double)ULONG_MAX && x >= (double)0)
3388db8a33bSSheldon Hearn 			printf(format, (unsigned long)x);
3395249bd84SSheldon Hearn 		else
3405249bd84SSheldon Hearn 			return (1);
3415249bd84SSheldon Hearn 	} else if (longdata) {
3425249bd84SSheldon Hearn 		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
3438db8a33bSSheldon Hearn 			printf(format, (long)x);
3445249bd84SSheldon Hearn 		else
3455249bd84SSheldon Hearn 			return (1);
3465249bd84SSheldon Hearn 	} else if (chardata || (intdata && !nosign)) {
3475249bd84SSheldon Hearn 		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
3485249bd84SSheldon Hearn 			printf(format, (int)x);
3495249bd84SSheldon Hearn 		else
3505249bd84SSheldon Hearn 			return (1);
3515249bd84SSheldon Hearn 	} else if (intdata) {
3525249bd84SSheldon Hearn 		if (x <= (double)UINT_MAX && x >= (double)0)
3535249bd84SSheldon Hearn 			printf(format, (unsigned int)x);
3545249bd84SSheldon Hearn 		else
3555249bd84SSheldon Hearn 			return (1);
3565249bd84SSheldon Hearn 
3575249bd84SSheldon Hearn 	} else
3589b50d902SRodney W. Grimes 		printf(format, x);
3593241f274SDiomidis Spinellis 	if (!last)
3609b50d902SRodney W. Grimes 		fputs(sepstring, stdout);
3615249bd84SSheldon Hearn 
3625249bd84SSheldon Hearn 	return (0);
3639b50d902SRodney W. Grimes }
3649b50d902SRodney W. Grimes 
3657bd7ad50SPhilippe Charnier static void
usage(void)366cf0def93SJuli Mallett usage(void)
3679b50d902SRodney W. Grimes {
3687bd7ad50SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
3697bd7ad50SPhilippe Charnier 	"usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
3707bd7ad50SPhilippe Charnier 	"           [reps [begin [end [s]]]]");
3719b50d902SRodney W. Grimes 	exit(1);
3729b50d902SRodney W. Grimes }
3739b50d902SRodney W. Grimes 
3743241f274SDiomidis Spinellis /*
3753241f274SDiomidis Spinellis  * Return the number of digits following the number's decimal point.
3763241f274SDiomidis Spinellis  * Return 0 if no decimal point is found.
3773241f274SDiomidis Spinellis  */
3783241f274SDiomidis Spinellis static int
getprec(const char * str)3793241f274SDiomidis Spinellis getprec(const char *str)
3809b50d902SRodney W. Grimes {
3813241f274SDiomidis Spinellis 	const char	*p;
3823241f274SDiomidis Spinellis 	const char	*q;
3839b50d902SRodney W. Grimes 
384edd7b267SDima Dorfman 	for (p = str; *p; p++)
3859b50d902SRodney W. Grimes 		if (*p == '.')
3869b50d902SRodney W. Grimes 			break;
3879b50d902SRodney W. Grimes 	if (!*p)
3889b50d902SRodney W. Grimes 		return (0);
3899b50d902SRodney W. Grimes 	for (q = ++p; *p; p++)
390911a3ff9STim J. Robbins 		if (!isdigit((unsigned char)*p))
3919b50d902SRodney W. Grimes 			break;
3929b50d902SRodney W. Grimes 	return (p - q);
3939b50d902SRodney W. Grimes }
3949b50d902SRodney W. Grimes 
3953241f274SDiomidis Spinellis /*
3963241f274SDiomidis Spinellis  * Set format, intdata, chardata, longdata, and nosign
3973241f274SDiomidis Spinellis  * based on the command line arguments.
3983241f274SDiomidis Spinellis  */
3993241f274SDiomidis Spinellis static void
getformat(void)400cf0def93SJuli Mallett getformat(void)
4019b50d902SRodney W. Grimes {
402edd7b267SDima Dorfman 	char	*p, *p2;
4035249bd84SSheldon Hearn 	int dot, hash, space, sign, numbers = 0;
40415ba0427SDima Dorfman 	size_t sz;
4059b50d902SRodney W. Grimes 
4069b50d902SRodney W. Grimes 	if (boring)				/* no need to bother */
4079b50d902SRodney W. Grimes 		return;
4089b50d902SRodney W. Grimes 	for (p = format; *p; p++)		/* look for '%' */
40982417e9fSXin LI 		if (*p == '%') {
41034785a9fSDiomidis Spinellis 			if (p[1] == '%')
41134785a9fSDiomidis Spinellis 				p++;		/* leave %% alone */
41234785a9fSDiomidis Spinellis 			else
4139b50d902SRodney W. Grimes 				break;
41482417e9fSXin LI 		}
41515ba0427SDima Dorfman 	sz = sizeof(format) - strlen(format) - 1;
41615ba0427SDima Dorfman 	if (!*p && !chardata) {
41715ba0427SDima Dorfman 		if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
41815ba0427SDima Dorfman 			errx(1, "-w word too long");
41915ba0427SDima Dorfman 	} else if (!*p && chardata) {
42015ba0427SDima Dorfman 		if (strlcpy(p, "%c", sz) >= sz)
42115ba0427SDima Dorfman 			errx(1, "-w word too long");
4223241f274SDiomidis Spinellis 		intdata = true;
42315ba0427SDima Dorfman 	} else if (!*(p+1)) {
42415ba0427SDima Dorfman 		if (sz <= 0)
42515ba0427SDima Dorfman 			errx(1, "-w word too long");
4269b50d902SRodney W. Grimes 		strcat(format, "%");		/* cannot end in single '%' */
42715ba0427SDima Dorfman 	} else {
4288db8a33bSSheldon Hearn 		/*
4298db8a33bSSheldon Hearn 		 * Allow conversion format specifiers of the form
4308db8a33bSSheldon Hearn 		 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
4318db8a33bSSheldon Hearn 		 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
4328db8a33bSSheldon Hearn 		 */
433edd7b267SDima Dorfman 		p2 = p++;
4348db8a33bSSheldon Hearn 		dot = hash = space = sign = numbers = 0;
435911a3ff9STim J. Robbins 		while (!isalpha((unsigned char)*p)) {
436911a3ff9STim J. Robbins 			if (isdigit((unsigned char)*p)) {
4378db8a33bSSheldon Hearn 				numbers++;
4389b50d902SRodney W. Grimes 				p++;
4398db8a33bSSheldon Hearn 			} else if ((*p == '#' && !(numbers|dot|sign|space|
4408db8a33bSSheldon Hearn 			    hash++)) ||
4418db8a33bSSheldon Hearn 			    (*p == ' ' && !(numbers|dot|space++)) ||
4428db8a33bSSheldon Hearn 			    ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
4438db8a33bSSheldon Hearn 			    || (*p == '.' && !(dot++)))
4448db8a33bSSheldon Hearn 				p++;
4458db8a33bSSheldon Hearn 			else
4465249bd84SSheldon Hearn 				goto fmt_broken;
4475249bd84SSheldon Hearn 		}
4485249bd84SSheldon Hearn 		if (*p == 'l') {
4493241f274SDiomidis Spinellis 			longdata = true;
4505249bd84SSheldon Hearn 			if (*++p == 'l') {
4515249bd84SSheldon Hearn 				if (p[1] != '\0')
4525249bd84SSheldon Hearn 					p++;
4535249bd84SSheldon Hearn 				goto fmt_broken;
4545249bd84SSheldon Hearn 			}
4558db8a33bSSheldon Hearn 		}
4569b50d902SRodney W. Grimes 		switch (*p) {
4578db8a33bSSheldon Hearn 		case 'o': case 'u': case 'x': case 'X':
4583241f274SDiomidis Spinellis 			intdata = nosign = true;
4599b50d902SRodney W. Grimes 			break;
4608db8a33bSSheldon Hearn 		case 'd': case 'i':
4613241f274SDiomidis Spinellis 			intdata = true;
4628db8a33bSSheldon Hearn 			break;
4638db8a33bSSheldon Hearn 		case 'D':
4645249bd84SSheldon Hearn 			if (!longdata) {
4653241f274SDiomidis Spinellis 				intdata = true;
4668db8a33bSSheldon Hearn 				break;
4678db8a33bSSheldon Hearn 			}
4688db8a33bSSheldon Hearn 		case 'O': case 'U':
4695249bd84SSheldon Hearn 			if (!longdata) {
4703241f274SDiomidis Spinellis 				intdata = nosign = true;
4718db8a33bSSheldon Hearn 				break;
4728db8a33bSSheldon Hearn 			}
4738db8a33bSSheldon Hearn 		case 'c':
4745249bd84SSheldon Hearn 			if (!(intdata | longdata)) {
4753241f274SDiomidis Spinellis 				chardata = true;
4768db8a33bSSheldon Hearn 				break;
4778db8a33bSSheldon Hearn 			}
4785249bd84SSheldon Hearn 		case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
4798db8a33bSSheldon Hearn 		case '$': case '*':
4805249bd84SSheldon Hearn 			goto fmt_broken;
4818db8a33bSSheldon Hearn 		case 'f': case 'e': case 'g': case 'E': case 'G':
4825249bd84SSheldon Hearn 			if (!longdata)
4838db8a33bSSheldon Hearn 				break;
4848db8a33bSSheldon Hearn 			/* FALLTHROUGH */
4859b50d902SRodney W. Grimes 		default:
4865249bd84SSheldon Hearn fmt_broken:
4878db8a33bSSheldon Hearn 			*++p = '\0';
488edd7b267SDima Dorfman 			errx(1, "illegal or unsupported format '%s'", p2);
4898db8a33bSSheldon Hearn 			/* NOTREACHED */
4908db8a33bSSheldon Hearn 		}
4918db8a33bSSheldon Hearn 		while (*++p)
4928db8a33bSSheldon Hearn 			if (*p == '%' && *(p+1) && *(p+1) != '%')
4938db8a33bSSheldon Hearn 				errx(1, "too many conversions");
4948db8a33bSSheldon Hearn 			else if (*p == '%' && *(p+1) == '%')
4958db8a33bSSheldon Hearn 				p++;
4968db8a33bSSheldon Hearn 			else if (*p == '%' && !*(p+1)) {
497f88b45d7SDiomidis Spinellis 				if (strlcat(format, "%", sizeof(format)) >=
498f88b45d7SDiomidis Spinellis 				    sizeof(format))
499f88b45d7SDiomidis Spinellis 					errx(1, "-w word too long");
5009b50d902SRodney W. Grimes 				break;
5019b50d902SRodney W. Grimes 			}
5029b50d902SRodney W. Grimes 	}
5039b50d902SRodney W. Grimes }
504