summary refs log tree commit diff
path: root/src/usr.bin/jot/jot.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr.bin/jot/jot.c')
-rw-r--r--src/usr.bin/jot/jot.c59
1 files changed, 29 insertions, 30 deletions
diff --git a/src/usr.bin/jot/jot.c b/src/usr.bin/jot/jot.c
index b2e4a05..a0b3de8 100644
--- a/src/usr.bin/jot/jot.c
+++ b/src/usr.bin/jot/jot.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: jot.c,v 1.49 2019/06/27 18:03:36 deraadt Exp $	*/
+/*	$OpenBSD: jot.c,v 1.56 2021/08/13 12:37:28 jmc Exp $	*/
 /*	$NetBSD: jot.c,v 1.3 1994/12/02 20:29:43 pk Exp $	*/
 
 /*-
@@ -70,6 +70,7 @@ static bool	intdata;
 static bool	longdata;
 static bool	nosign;
 static bool	randomize;
+static bool	word;
 
 static void	getformat(void);
 static int	getprec(char *);
@@ -94,10 +95,13 @@ main(int argc, char *argv[])
 		switch (ch) {
 		case 'b':
 			boring = true;
+			chardata = word = false;
 			format = optarg;
 			break;
 		case 'c':
 			chardata = true;
+			boring = word = false;
+			format = "";
 			break;
 		case 'n':
 			finalnl = false;
@@ -115,6 +119,8 @@ main(int argc, char *argv[])
 			sepstring = optarg;
 			break;
 		case 'w':
+			word = true;
+			boring = chardata = false;
 			format = optarg;
 			break;
 		default:
@@ -160,8 +166,6 @@ main(int argc, char *argv[])
 			mask |= REPS;
 			if (reps == 0)
 				infinity = true;
-			if (prec == -1)
-				prec = 0;
 		}
 	case 0:
 		if (prec == -1)
@@ -246,7 +250,7 @@ main(int argc, char *argv[])
 			if (putdata(x, reps == i && !infinity))
 				errx(1, "range error in conversion: %f", x);
 	} else { /* Random output: use defaults for omitted values. */
-		bool		use_unif;
+		bool		use_unif = 0;
 		uint32_t	pow10 = 1;
 		uint32_t	uintx = 0; /* Initialized to make gcc happy. */
 
@@ -260,21 +264,20 @@ main(int argc, char *argv[])
 		}
 		x = ender - begin;
 
-		if (prec == 0 && (fmod(ender, 1) != 0 || fmod(begin, 1) != 0))
-			use_unif = 0;
-		else {
+		if (prec > 0 || (fmod(ender, 1) == 0 && fmod(begin, 1) == 0)) {
+			double range;
+
 			while (prec-- > 0)
 				pow10 *= 10;
-			/*
-			 * If pow10 * (ender - begin) is an integer, use
-			 * arc4random_uniform().
-			 */
-			use_unif = fmod(pow10 * (ender - begin), 1) == 0;
-			if (use_unif) {
-				uintx = pow10 * (ender - begin);
-				if (uintx >= UINT32_MAX)
+
+			range = pow10 * (ender - begin);
+
+			/* If range is an integer, use arc4random_uniform(). */
+			if (fmod(range, 1) == 0) {
+				if (range >= UINT32_MAX)
 					errx(1, "requested range too large");
-				uintx++;
+				use_unif = 1;
+				uintx = range + 1;
 			}
 		}
 
@@ -305,25 +308,21 @@ putdata(double x, bool last)
 	if (boring)
 		printf("%s", format);
 	else if (longdata && nosign) {
-		if (x <= (double)ULONG_MAX && x >= 0.0)
-			printf(format, (unsigned long)x);
-		else
+		if (x < 0.0 || x > (double)ULONG_MAX)
 			return 1;
+		printf(format, (unsigned long)x);
 	} else if (longdata) {
-		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
-			printf(format, (long)x);
-		else
+		if (x < (double)LONG_MIN || x > (double)LONG_MAX)
 			return 1;
+		printf(format, (long)x);
 	} else if (chardata || (intdata && !nosign)) {
-		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
-			printf(format, (int)x);
-		else
+		if (x < (double)INT_MIN || x > (double)INT_MAX)
 			return 1;
+		printf(format, (int)x);
 	} else if (intdata) {
-		if (x <= (double)UINT_MAX && x >= 0.0)
-			printf(format, (unsigned int)x);
-		else
+		if (x < 0.0 || x > (double)UINT_MAX)
 			return 1;
+		printf(format, (unsigned int)x);
 	} else
 		printf(format, x);
 	if (!last)
@@ -337,7 +336,7 @@ usage(void)
 {
 	(void)fprintf(stderr, "usage: jot [-cnr] [-b word] [-p precision] "
 	    "[-s string] [-w word]\n"
-	    "	   [reps [begin [end [s]]]]\n");
+	    "	   [reps [begin [end [step]]]]\n");
 	exit(1);
 }
 
@@ -444,7 +443,7 @@ fmt_broken:
 
 		while ((p = strchr(p, '%')) != NULL && p[1] == '%')
 			p += 2;
-		
+
 		if (p != NULL) {
 			if (p[1] != '\0')
 				errx(1, "too many conversions");