GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/jot/jot.c Lines: 152 212 71.7 %
Date: 2017-11-07 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
2026
	const	char	*errstr;
89
90
1013
	if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
91
		err(1, "pledge");
92
93
1409
	while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1) {
94


396
		switch (ch) {
95
		case 'b':
96
45
			boring = true;
97
45
			if (strlcpy(format, optarg, sizeof(format)) >=
98
			    sizeof(format))
99
				errx(1, "-b word too long");
100
			break;
101
		case 'c':
102
9
			chardata = true;
103
9
			break;
104
		case 'n':
105
			finalnl = false;
106
			break;
107
		case 'p':
108
18
			prec = strtonum(optarg, 0, INT_MAX, &errstr);
109
18
			if (errstr != NULL)
110
				errx(1, "bad precision value, %s: %s", errstr,
111
					optarg);
112
			break;
113
		case 'r':
114
36
			randomize = true;
115
36
			break;
116
		case 's':
117
45
			if (strlcpy(sepstring, optarg, sizeof(sepstring)) >=
118
			    sizeof(sepstring))
119
				errx(1, "-s string too long");
120
			break;
121
		case 'w':
122
243
			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
2921
	argc -= optind;
131
2921
	argv += optind;
132
133

2921
	switch (argc) {	/* examine args right to left, falling thru cases */
134
	case 4:
135
576
		if (!is_default(argv[3])) {
136
342
			if (!sscanf(argv[3], "%lf", &step))
137
				errx(1, "Bad s value:  %s", argv[3]);
138
			mask |= STEP;
139
342
			if (randomize)
140
18
				warnx("random seeding not supported");
141
		}
142
	case 3:
143
630
		if (!is_default(argv[2])) {
144
441
			if (!sscanf(argv[2], "%lf", &ender))
145
				ender = argv[2][strlen(argv[2])-1];
146
441
			mask |= ENDER;
147
441
			if (prec == -1)
148
423
				n = getprec(argv[2]);
149
		}
150
	case 2:
151
702
		if (!is_default(argv[1])) {
152
513
			if (!sscanf(argv[1], "%lf", &begin))
153
18
				begin = argv[1][strlen(argv[1])-1];
154
513
			mask |= BEGIN;
155
513
			if (prec == -1)
156
495
				prec = getprec(argv[1]);
157
513
			if (n > prec)		/* maximum precision */
158
18
				prec = n;
159
		}
160
	case 1:
161
1013
		if (!is_default(argv[0])) {
162
779
			if (!sscanf(argv[0], "%ld", &reps))
163
				errx(1, "Bad reps value:  %s", argv[0]);
164
779
			mask |= REPS;
165
779
			if (reps == 0)
166
				infinity = true;
167
779
			if (prec == -1)
168
428
				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
1013
	getformat();
180
181
1013
	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


1436
		switch (mask) { /* Four cases involve both begin and ender. */
195
		case REPS | BEGIN | ENDER | STEP:
196
81
			if (infinity)
197
				errx(1,
198
				    "Can't specify end of infinite sequence");
199
81
			if (step != 0.0) {
200
81
				long t = (ender - begin + step) / step;
201
81
				if (t <= 0)
202
					errx(1, "Impossible stepsize");
203
81
				if (t < reps)
204
9
					reps = t;
205
81
			}
206
			break;
207
		case REPS | BEGIN | ENDER:
208
81
			if (infinity)
209
				errx(1,
210
				    "Can't specify end of infinite sequence");
211
81
			if (reps == 1)
212
				step = 0.0;
213
			else
214
81
				step = (ender - begin) / (reps - 1);
215
81
			break;
216
		case BEGIN | ENDER:
217
45
			step = ender > begin ? 1 : -1; /* FreeBSD's behavior. */
218
			/* FALLTHROUGH */
219
		case BEGIN | ENDER | STEP:
220
126
			if (step == 0.0) {
221
				reps = 0;
222
				infinity = true;
223
				break;
224
			}
225
126
			reps = (ender - begin + step) / step;
226
126
			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
126
			if (infinity)
234
				errx(1,
235
				    "Must specify start of infinite sequence");
236
126
			begin = ender - reps * step + step;
237
126
			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
119000
		for (i = 1, x = begin; i <= reps || infinity; i++, x += step)
249

118041
			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
36
		if (prec > 9)	/* pow(10, prec) > UINT32_MAX */
257
			errx(1, "requested precision too large");
258
259
36
		if (ender < begin) {
260
			x = begin;
261
9
			begin = ender;
262
9
			ender = x;
263
9
		}
264
36
		x = ender - begin;
265
266

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

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

229595
	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
229595
	} else if (longdata) {
316
		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
317
			printf(format, (long)x);
318
		else
319
			return 1;
320

460252
	} else if (chardata || (intdata && !nosign)) {
321
3123
		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
322
3114
			printf(format, (int)x);
323
		else
324
9
			return 1;
325
229586
	} else if (intdata) {
326
1359
		if (x <= (double)UINT_MAX && x >= 0.0)
327
1350
			printf(format, (unsigned int)x);
328
		else
329
9
			return 1;
330
1350
	} else
331
225113
		printf(format, x);
332
240323
	if (!last)
333
239328
		fputs(sepstring, stdout);
334
335
240323
	return 0;
336
240341
}
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
1836
	if ((s = strchr(s, '.')) == NULL)
351
882
		return 0;
352
36
	return strspn(s + 1, "0123456789");
353
918
}
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
2026
	if (boring)				/* no need to bother */
363
45
		return;
364
3232
	for (p = format; *p != '\0'; p++)	/* look for '%' */
365
891
		if (*p == '%') {
366
288
			if (*(p+1) != '%')
367
				break;
368
45
			p++;			/* leave %% alone */
369
45
		}
370
968
	sz = sizeof(format) - strlen(format) - 1;
371

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

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

977
	} else if (*p == '\0' && chardata) {
378
9
		if (strlcpy(p, "%c", sz) >= sz)
379
			errx(1, "-w word too long");
380
9
		intdata = true;
381
252
	} 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
243
		p2 = p++;
393
		dot = hash = space = sign = numbers = 0;
394
567
		while (!isalpha((unsigned char)*p)) {
395
81
			if (isdigit((unsigned char)*p)) {
396
63
				numbers++;
397
63
				p++;
398

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

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

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

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




243
		switch (*p) {
416
		case 'o': case 'u': case 'x': case 'X':
417
63
			intdata = nosign = true;
418
63
			break;
419
		case 'd': case 'i':
420
108
			intdata = true;
421
108
			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
27
			if (!(intdata | longdata)) {
434
27
				chardata = true;
435
27
				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
45
			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
693
		while (*++p != '\0')
450

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

540
			else if (*p == '%' && *(p+1) == '%')
453
72
				p++;
454

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