GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/jot/jot.c Lines: 148 206 71.8 %
Date: 2017-11-13 Branches: 154 239 64.4 %

Line Branch Exec Source
1
/*	$OpenBSD: jot.c,v 1.36 2016/09/02 14:23:09 tb Exp $	*/
2
/*	$NetBSD: jot.c,v 1.3 1994/12/02 20:29:43 pk Exp $	*/
3
4
/*-
5
 * Copyright (c) 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
/*
34
 * jot - print sequential or random data
35
 *
36
 * Author:  John Kunze, Office of Comp. Affairs, UCB
37
 */
38
39
#include <ctype.h>
40
#include <err.h>
41
#include <limits.h>
42
#include <math.h>
43
#include <stdbool.h>
44
#include <stdint.h>
45
#include <stdio.h>
46
#include <stdlib.h>
47
#include <string.h>
48
#include <unistd.h>
49
50
#define	REPS	1
51
#define	BEGIN	2
52
#define	ENDER	4
53
#define	STEP	8
54
55
#define	is_default(s)	(strcmp((s), "-") == 0)
56
57
static long	reps	= 100;
58
static double	begin	= 1;
59
static double	ender	= 100;
60
static double	step	= 1;
61
62
static char	format[BUFSIZ];
63
static char	sepstring[BUFSIZ] = "\n";
64
static int	prec = -1;
65
static bool	boring;
66
static bool	chardata;
67
static bool	finalnl = true;
68
static bool	infinity;
69
static bool	intdata;
70
static bool	longdata;
71
static bool	nosign;
72
static bool	randomize;
73
74
static void	getformat(void);
75
static int	getprec(char *);
76
static int	putdata(double, bool);
77
static void __dead	usage(void);
78
79
int
80
main(int argc, char *argv[])
81
{
82
	double		x;
83
	double		y;
84
	long		i;
85
	unsigned int	mask = 0;
86
	int		n = 0;
87
	int		ch;
88
1132
	const	char	*errstr;
89
90
566
	if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
91
		err(1, "pledge");
92
93
1636
	while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1) {
94


252
		switch (ch) {
95
		case 'b':
96
41
			boring = true;
97
41
			if (strlcpy(format, optarg, sizeof(format)) >=
98
			    sizeof(format))
99
				errx(1, "-b word too long");
100
			break;
101
		case 'c':
102
5
			chardata = true;
103
5
			break;
104
		case 'n':
105
			finalnl = false;
106
			break;
107
		case 'p':
108
10
			prec = strtonum(optarg, 0, INT_MAX, &errstr);
109
10
			if (errstr != NULL)
110
				errx(1, "bad precision value, %s: %s", errstr,
111
					optarg);
112
			break;
113
		case 'r':
114
20
			randomize = true;
115
20
			break;
116
		case 's':
117
41
			if (strlcpy(sepstring, optarg, sizeof(sepstring)) >=
118
			    sizeof(sepstring))
119
				errx(1, "-s string too long");
120
			break;
121
		case 'w':
122
135
			if (strlcpy(format, optarg, sizeof(format)) >=
123
			    sizeof(format))
124
				errx(1, "-w word too long");
125
			break;
126
		default:
127
			usage();
128
		}
129
	}
130
1626
	argc -= optind;
131
1626
	argv += optind;
132
133

1626
	switch (argc) {	/* examine args right to left, falling thru cases */
134
	case 4:
135
320
		if (!is_default(argv[3])) {
136
190
			if (!sscanf(argv[3], "%lf", &step))
137
				errx(1, "Bad s value:  %s", argv[3]);
138
			mask |= STEP;
139
190
			if (randomize)
140
10
				warnx("random seeding not supported");
141
		}
142
	case 3:
143
350
		if (!is_default(argv[2])) {
144
245
			if (!sscanf(argv[2], "%lf", &ender))
145
				ender = argv[2][strlen(argv[2])-1];
146
245
			mask |= ENDER;
147
245
			if (prec == -1)
148
235
				n = getprec(argv[2]);
149
		}
150
	case 2:
151
390
		if (!is_default(argv[1])) {
152
285
			if (!sscanf(argv[1], "%lf", &begin))
153
10
				begin = argv[1][strlen(argv[1])-1];
154
285
			mask |= BEGIN;
155
285
			if (prec == -1)
156
275
				prec = getprec(argv[1]);
157
285
			if (n > prec)		/* maximum precision */
158
10
				prec = n;
159
		}
160
	case 1:
161
566
		if (!is_default(argv[0])) {
162
436
			if (!sscanf(argv[0], "%ld", &reps))
163
				errx(1, "Bad reps value:  %s", argv[0]);
164
436
			mask |= REPS;
165
436
			if (reps == 0)
166
				infinity = true;
167
436
			if (prec == -1)
168
241
				prec = 0;
169
		}
170
		break;
171
	case 0:
172
		usage();
173
		break;
174
	default:
175
		errx(1, "Too many arguments.  What do you mean by %s?",
176
		    argv[4]);
177
	}
178
179
566
	getformat();
180
181
566
	if (!randomize) {
182
		/*
183
		 * Consolidate the values of reps, begin, ender, step:
184
		 * The formula ender - begin == (reps - 1) * step shows that any
185
		 * three determine the fourth (unless reps == 1 or step == 0).
186
		 * The manual states the following rules:
187
		 * 1. If four are specified, compare the given and the computed
188
		 *    value of reps and take the smaller of the two.
189
		 * 2. If steps was omitted, it takes the default, unless both
190
		 *    begin and ender were specified.
191
		 * 3. Assign defaults to omitted values for reps, begin, ender,
192
		 *    from left to right.
193
		 */
194


801
		switch (mask) { /* Four cases involve both begin and ender. */
195
		case REPS | BEGIN | ENDER | STEP:
196
45
			if (infinity)
197
				errx(1,
198
				    "Can't specify end of infinite sequence");
199
45
			if (step != 0.0) {
200
45
				long t = (ender - begin + step) / step;
201
45
				if (t <= 0)
202
					errx(1, "Impossible stepsize");
203
45
				if (t < reps)
204
5
					reps = t;
205
45
			}
206
			break;
207
		case REPS | BEGIN | ENDER:
208
45
			if (infinity)
209
				errx(1,
210
				    "Can't specify end of infinite sequence");
211
45
			if (reps == 1)
212
				step = 0.0;
213
			else
214
45
				step = (ender - begin) / (reps - 1);
215
45
			break;
216
		case BEGIN | ENDER:
217
25
			step = ender > begin ? 1 : -1; /* FreeBSD's behavior. */
218
			/* FALLTHROUGH */
219
		case BEGIN | ENDER | STEP:
220
70
			if (step == 0.0) {
221
				reps = 0;
222
				infinity = true;
223
				break;
224
			}
225
70
			reps = (ender - begin + step) / step;
226
70
			if (reps <= 0)
227
				errx(1, "Impossible stepsize");
228
			break;
229
		case ENDER:		/* Four cases involve only ender. */
230
		case ENDER | STEP:
231
		case REPS | ENDER:
232
		case REPS | ENDER | STEP:
233
70
			if (infinity)
234
				errx(1,
235
				    "Must specify start of infinite sequence");
236
70
			begin = ender - reps * step + step;
237
70
			break;
238
		default:
239
			/*
240
			 * The remaining eight cases omit ender.  We don't need
241
			 * to compute anything because only reps, begin, step
242
			 * are used for producing output below.  Rules 2. and 3.
243
			 * together imply that ender will be set last.
244
			 */
245
			break;
246
		}
247
248
2226162
		for (i = 1, x = begin; i <= reps || infinity; i++, x += step)
249

2225626
			if (putdata(x, reps == i && !infinity))
250
				errx(1, "range error in conversion: %f", x);
251
	} else { /* Random output: use defaults for omitted values. */
252
		bool		use_unif;
253
		uint32_t	pow10 = 1;
254
		uint32_t	uintx = 0; /* Initialized to make gcc happy. */
255
256
20
		if (prec > 9)	/* pow(10, prec) > UINT32_MAX */
257
			errx(1, "requested precision too large");
258
259
20
		if (ender < begin) {
260
			x = begin;
261
5
			begin = ender;
262
5
			ender = x;
263
5
		}
264
20
		x = ender - begin;
265
266

60
		if (prec == 0 && (fmod(ender, 1) != 0 || fmod(begin, 1) != 0))
267
			use_unif = 0;
268
		else {
269
40
			while (prec-- > 0)
270
				pow10 *= 10;
271
			/*
272
			 * If pow10 * (ender - begin) is an integer, use
273
			 * arc4random_uniform().
274
			 */
275
20
			use_unif = fmod(pow10 * (ender - begin), 1) == 0;
276
20
			if (use_unif) {
277
20
				uintx = pow10 * (ender - begin);
278
20
				if (uintx >= UINT32_MAX)
279
					errx(1, "requested range too large");
280
20
				uintx++;
281
20
			}
282
		}
283
284
202040
		for (i = 1; i <= reps || infinity; i++) {
285
			double v;
286
287
101000
			if (use_unif) {
288
101000
				y = arc4random_uniform(uintx) / (double)pow10;
289
101000
				v = y + begin;
290
101000
			} else {
291
				y = arc4random() / ((double)0xffffffff + 1);
292
				v = y * x + begin;
293
			}
294

202020
			if (putdata(v, reps == i && !infinity))
295
				errx(1, "range error in conversion: %f", v);
296
		}
297
	}
298
299
556
	if (finalnl)
300
1112
		putchar('\n');
301
302
556
	return 0;
303
556
}
304
305
static int
306
putdata(double x, bool last)
307
{
308
1213545
	if (boring)
309
1087270
		printf("%s", format);
310

126275
	else if (longdata && nosign) {
311
		if (x <= (double)ULONG_MAX && x >= 0.0)
312
			printf(format, (unsigned long)x);
313
		else
314
			return 1;
315
126275
	} else if (longdata) {
316
		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
317
			printf(format, (long)x);
318
		else
319
			return 1;
320

253140
	} else if (chardata || (intdata && !nosign)) {
321
1735
		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
322
1730
			printf(format, (int)x);
323
		else
324
5
			return 1;
325
126270
	} else if (intdata) {
326
755
		if (x <= (double)UINT_MAX && x >= 0.0)
327
750
			printf(format, (unsigned int)x);
328
		else
329
5
			return 1;
330
750
	} else
331
123785
		printf(format, x);
332
1213535
	if (!last)
333
1212979
		fputs(sepstring, stdout);
334
335
1213535
	return 0;
336
1213545
}
337
338
static void __dead
339
usage(void)
340
{
341
	(void)fprintf(stderr, "usage: jot [-cnr] [-b word] [-p precision] "
342
	    "[-s string] [-w word]\n"
343
	    "	   [reps [begin [end [s]]]]\n");
344
	exit(1);
345
}
346
347
static int
348
getprec(char *s)
349
{
350
1020
	if ((s = strchr(s, '.')) == NULL)
351
490
		return 0;
352
20
	return strspn(s + 1, "0123456789");
353
510
}
354
355
static void
356
getformat(void)
357
{
358
	char	*p, *p2;
359
	int dot, hash, space, sign, numbers = 0;
360
	size_t sz;
361
362
1132
	if (boring)				/* no need to bother */
363
41
		return;
364
1770
	for (p = format; *p != '\0'; p++)	/* look for '%' */
365
495
		if (*p == '%') {
366
160
			if (*(p+1) != '%')
367
				break;
368
25
			p++;			/* leave %% alone */
369
25
		}
370
525
	sz = sizeof(format) - strlen(format) - 1;
371

915
	if (*p == '\0' && !chardata) {
372
		int n;
373
374
385
		n = snprintf(p, sz, "%%.%df", prec);
375

770
		if (n == -1 || n >= (int)sz)
376
			errx(1, "-w word too long");
377

530
	} else if (*p == '\0' && chardata) {
378
5
		if (strlcpy(p, "%c", sz) >= sz)
379
			errx(1, "-w word too long");
380
5
		intdata = true;
381
140
	} else if (*(p+1) == '\0') {
382
		if (sz <= 0)
383
			errx(1, "-w word too long");
384
		/* cannot end in single '%' */
385
		strlcat(format, "%", sizeof format);
386
	} else {
387
		/*
388
		 * Allow conversion format specifiers of the form
389
		 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
390
		 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
391
		 */
392
135
		p2 = p++;
393
		dot = hash = space = sign = numbers = 0;
394
360
		while (!isalpha((unsigned char)*p)) {
395
45
			if (isdigit((unsigned char)*p)) {
396
35
				numbers++;
397
35
				p++;
398

50
			} else if ((*p == '#' && !(numbers|dot|sign|space|
399
			    hash++)) ||
400

10
			    (*p == ' ' && !(numbers|dot|space++)) ||
401

25
			    ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
402

15
			    || (*p == '.' && !(dot++)))
403
10
				p++;
404
			else
405
				goto fmt_broken;
406
		}
407
135
		if (*p == 'l') {
408
			longdata = true;
409
			if (*++p == 'l') {
410
				if (p[1] != '\0')
411
					p++;
412
				goto fmt_broken;
413
			}
414
		}
415




185
		switch (*p) {
416
		case 'o': case 'u': case 'x': case 'X':
417
35
			intdata = nosign = true;
418
35
			break;
419
		case 'd': case 'i':
420
			intdata = true;
421
			break;
422
		case 'D':
423
			if (!longdata) {
424
				intdata = true;
425
				break;
426
			}
427
		case 'O': case 'U':
428
			if (!longdata) {
429
				intdata = nosign = true;
430
				break;
431
			}
432
		case 'c':
433
15
			if (!(intdata | longdata)) {
434
				chardata = true;
435
				break;
436
			}
437
		case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
438
		case '$': case '*':
439
			goto fmt_broken;
440
		case 'f': case 'e': case 'g': case 'E': case 'G':
441
25
			if (!longdata)
442
				break;
443
			/* FALLTHROUGH */
444
		default:
445
fmt_broken:
446
			*++p = '\0';
447
			errx(1, "illegal or unsupported format '%s'", p2);
448
		}
449
880
		while (*++p != '\0')
450

340
			if (*p == '%' && *(p+1) != '\0' && *(p+1) != '%')
451
				errx(1, "too many conversions");
452

300
			else if (*p == '%' && *(p+1) == '%')
453
40
				p++;
454

220
			else if (*p == '%' && *(p+1) == '\0') {
455
5
				strlcat(format, "%", sizeof format);
456
5
				break;
457
			}
458
	}
459
1091
}