Annotation of src/usr.bin/jot/jot.c, Revision 1.46
1.46 ! schwarze 1: /* $OpenBSD: jot.c,v 1.45 2018/01/13 15:43:39 tb Exp $ */
1.1 deraadt 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.
1.12 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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:
1.36 tb 39: #include <ctype.h>
1.7 aaron 40: #include <err.h>
1.1 deraadt 41: #include <limits.h>
1.27 tb 42: #include <math.h>
1.36 tb 43: #include <stdbool.h>
1.27 tb 44: #include <stdint.h>
1.1 deraadt 45: #include <stdio.h>
46: #include <stdlib.h>
47: #include <string.h>
1.22 millert 48: #include <unistd.h>
1.1 deraadt 49:
1.36 tb 50: #define REPS 1
51: #define BEGIN 2
52: #define ENDER 4
53: #define STEP 8
1.32 tb 54:
1.8 pjanzen 55: #define is_default(s) (strcmp((s), "-") == 0)
1.1 deraadt 56:
1.36 tb 57: static long reps = 100;
58: static double begin = 1;
59: static double ender = 100;
60: static double step = 1;
61:
1.42 tb 62: static char *format = "";
63: static char *sepstring = "\n";
1.36 tb 64: static int prec = -1;
65: static bool boring;
66: static bool chardata;
67: static bool finalnl = true;
1.15 otto 68: static bool infinity;
1.17 otto 69: static bool intdata;
70: static bool longdata;
71: static bool nosign;
1.36 tb 72: static bool randomize;
1.15 otto 73:
74: static void getformat(void);
75: static int getprec(char *);
1.17 otto 76: static int putdata(double, bool);
1.33 tb 77: static void __dead usage(void);
1.1 deraadt 78:
79: int
1.13 deraadt 80: main(int argc, char *argv[])
1.1 deraadt 81: {
1.15 otto 82: double x;
83: double y;
84: long i;
1.8 pjanzen 85: unsigned int mask = 0;
1.15 otto 86: int n = 0;
87: int ch;
1.42 tb 88: const char *errstr;
1.25 deraadt 89:
1.26 deraadt 90: if (pledge("stdio", NULL) == -1)
91: err(1, "pledge");
1.1 deraadt 92:
1.36 tb 93: while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1) {
1.15 otto 94: switch (ch) {
1.36 tb 95: case 'b':
96: boring = true;
1.42 tb 97: format = optarg;
1.1 deraadt 98: break;
99: case 'c':
1.15 otto 100: chardata = true;
1.1 deraadt 101: break;
102: case 'n':
1.15 otto 103: finalnl = false;
1.1 deraadt 104: break;
1.36 tb 105: case 'p':
106: prec = strtonum(optarg, 0, INT_MAX, &errstr);
107: if (errstr != NULL)
108: errx(1, "bad precision value, %s: %s", errstr,
109: optarg);
1.8 pjanzen 110: break;
1.36 tb 111: case 'r':
112: randomize = true;
1.1 deraadt 113: break;
114: case 's':
1.42 tb 115: sepstring = optarg;
1.1 deraadt 116: break;
1.36 tb 117: case 'w':
1.42 tb 118: format = optarg;
1.1 deraadt 119: break;
120: default:
1.7 aaron 121: usage();
1.1 deraadt 122: }
1.36 tb 123: }
1.8 pjanzen 124: argc -= optind;
125: argv += optind;
1.1 deraadt 126:
1.8 pjanzen 127: switch (argc) { /* examine args right to left, falling thru cases */
1.1 deraadt 128: case 4:
1.8 pjanzen 129: if (!is_default(argv[3])) {
1.32 tb 130: if (!sscanf(argv[3], "%lf", &step))
1.8 pjanzen 131: errx(1, "Bad s value: %s", argv[3]);
1.32 tb 132: mask |= STEP;
1.16 otto 133: if (randomize)
134: warnx("random seeding not supported");
1.1 deraadt 135: }
136: case 3:
1.8 pjanzen 137: if (!is_default(argv[2])) {
138: if (!sscanf(argv[2], "%lf", &ender))
139: ender = argv[2][strlen(argv[2])-1];
1.32 tb 140: mask |= ENDER;
1.15 otto 141: if (prec == -1)
1.8 pjanzen 142: n = getprec(argv[2]);
1.1 deraadt 143: }
144: case 2:
1.8 pjanzen 145: if (!is_default(argv[1])) {
146: if (!sscanf(argv[1], "%lf", &begin))
147: begin = argv[1][strlen(argv[1])-1];
1.32 tb 148: mask |= BEGIN;
1.15 otto 149: if (prec == -1)
1.8 pjanzen 150: prec = getprec(argv[1]);
1.1 deraadt 151: if (n > prec) /* maximum precision */
152: prec = n;
153: }
154: case 1:
1.8 pjanzen 155: if (!is_default(argv[0])) {
156: if (!sscanf(argv[0], "%ld", &reps))
157: errx(1, "Bad reps value: %s", argv[0]);
1.32 tb 158: mask |= REPS;
1.35 tb 159: if (reps == 0)
160: infinity = true;
1.15 otto 161: if (prec == -1)
162: prec = 0;
1.1 deraadt 163: }
164: case 0:
1.8 pjanzen 165: break;
1.1 deraadt 166: default:
1.15 otto 167: errx(1, "Too many arguments. What do you mean by %s?",
168: argv[4]);
1.1 deraadt 169: }
1.35 tb 170:
1.42 tb 171: if (!boring)
172: getformat();
1.35 tb 173:
1.32 tb 174: if (!randomize) {
175: /*
176: * Consolidate the values of reps, begin, ender, step:
177: * The formula ender - begin == (reps - 1) * step shows that any
178: * three determine the fourth (unless reps == 1 or step == 0).
179: * The manual states the following rules:
180: * 1. If four are specified, compare the given and the computed
181: * value of reps and take the smaller of the two.
182: * 2. If steps was omitted, it takes the default, unless both
183: * begin and ender were specified.
184: * 3. Assign defaults to omitted values for reps, begin, ender,
185: * from left to right.
186: */
187: switch (mask) { /* Four cases involve both begin and ender. */
188: case REPS | BEGIN | ENDER | STEP:
1.35 tb 189: if (infinity)
1.32 tb 190: errx(1,
191: "Can't specify end of infinite sequence");
192: if (step != 0.0) {
193: long t = (ender - begin + step) / step;
194: if (t <= 0)
195: errx(1, "Impossible stepsize");
196: if (t < reps)
197: reps = t;
198: }
199: break;
200: case REPS | BEGIN | ENDER:
1.35 tb 201: if (infinity)
1.32 tb 202: errx(1,
203: "Can't specify end of infinite sequence");
204: if (reps == 1)
205: step = 0.0;
206: else
207: step = (ender - begin) / (reps - 1);
208: break;
209: case BEGIN | ENDER:
210: step = ender > begin ? 1 : -1; /* FreeBSD's behavior. */
1.31 tb 211: /* FALLTHROUGH */
1.32 tb 212: case BEGIN | ENDER | STEP:
213: if (step == 0.0) {
1.1 deraadt 214: reps = 0;
1.35 tb 215: infinity = true;
1.1 deraadt 216: break;
217: }
1.32 tb 218: reps = (ender - begin + step) / step;
1.1 deraadt 219: if (reps <= 0)
1.7 aaron 220: errx(1, "Impossible stepsize");
1.1 deraadt 221: break;
1.32 tb 222: case ENDER: /* Four cases involve only ender. */
223: case ENDER | STEP:
224: case REPS | ENDER:
225: case REPS | ENDER | STEP:
1.35 tb 226: if (infinity)
1.32 tb 227: errx(1,
228: "Must specify start of infinite sequence");
229: begin = ender - reps * step + step;
1.1 deraadt 230: break;
1.32 tb 231: default:
232: /*
233: * The remaining eight cases omit ender. We don't need
234: * to compute anything because only reps, begin, step
235: * are used for producing output below. Rules 2. and 3.
236: * together imply that ender will be set last.
237: */
1.1 deraadt 238: break;
239: }
1.35 tb 240:
241: for (i = 1, x = begin; i <= reps || infinity; i++, x += step)
242: if (putdata(x, reps == i && !infinity))
243: errx(1, "range error in conversion: %f", x);
244: } else { /* Random output: use defaults for omitted values. */
1.27 tb 245: bool use_unif;
246: uint32_t pow10 = 1;
247: uint32_t uintx = 0; /* Initialized to make gcc happy. */
248:
249: if (prec > 9) /* pow(10, prec) > UINT32_MAX */
250: errx(1, "requested precision too large");
251:
252: if (ender < begin) {
253: x = begin;
254: begin = ender;
255: ender = x;
256: }
257: x = ender - begin;
258:
1.28 tb 259: if (prec == 0 && (fmod(ender, 1) != 0 || fmod(begin, 1) != 0))
260: use_unif = 0;
261: else {
262: while (prec-- > 0)
263: pow10 *= 10;
264: /*
265: * If pow10 * (ender - begin) is an integer, use
266: * arc4random_uniform().
267: */
268: use_unif = fmod(pow10 * (ender - begin), 1) == 0;
269: if (use_unif) {
270: uintx = pow10 * (ender - begin);
271: if (uintx >= UINT32_MAX)
272: errx(1, "requested range too large");
273: uintx++;
274: }
1.27 tb 275: }
276:
1.15 otto 277: for (i = 1; i <= reps || infinity; i++) {
1.17 otto 278: double v;
1.27 tb 279:
280: if (use_unif) {
281: y = arc4random_uniform(uintx) / (double)pow10;
282: v = y + begin;
283: } else {
284: y = arc4random() / ((double)0xffffffff + 1);
285: v = y * x + begin;
286: }
1.17 otto 287: if (putdata(v, reps == i && !infinity))
288: errx(1, "range error in conversion: %f", v);
1.8 pjanzen 289: }
1.32 tb 290: }
1.35 tb 291:
1.15 otto 292: if (finalnl)
1.8 pjanzen 293: putchar('\n');
1.35 tb 294:
1.36 tb 295: return 0;
1.1 deraadt 296: }
297:
1.17 otto 298: static int
1.15 otto 299: putdata(double x, bool last)
1.1 deraadt 300: {
1.17 otto 301: if (boring)
1.1 deraadt 302: printf("%s", format);
1.17 otto 303: else if (longdata && nosign) {
304: if (x <= (double)ULONG_MAX && x >= 0.0)
305: printf(format, (unsigned long)x);
306: else
1.36 tb 307: return 1;
1.17 otto 308: } else if (longdata) {
309: if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
310: printf(format, (long)x);
311: else
1.36 tb 312: return 1;
1.17 otto 313: } else if (chardata || (intdata && !nosign)) {
314: if (x <= (double)INT_MAX && x >= (double)INT_MIN)
315: printf(format, (int)x);
316: else
1.36 tb 317: return 1;
1.17 otto 318: } else if (intdata) {
319: if (x <= (double)UINT_MAX && x >= 0.0)
320: printf(format, (unsigned int)x);
321: else
1.36 tb 322: return 1;
1.17 otto 323: } else
1.1 deraadt 324: printf(format, x);
1.15 otto 325: if (!last)
1.1 deraadt 326: fputs(sepstring, stdout);
1.17 otto 327:
1.36 tb 328: return 0;
1.1 deraadt 329: }
330:
1.33 tb 331: static void __dead
1.7 aaron 332: usage(void)
1.1 deraadt 333: {
1.14 jmc 334: (void)fprintf(stderr, "usage: jot [-cnr] [-b word] [-p precision] "
335: "[-s string] [-w word]\n"
336: " [reps [begin [end [s]]]]\n");
1.1 deraadt 337: exit(1);
338: }
339:
1.15 otto 340: static int
1.13 deraadt 341: getprec(char *s)
1.1 deraadt 342: {
1.34 tb 343: if ((s = strchr(s, '.')) == NULL)
1.36 tb 344: return 0;
345: return strspn(s + 1, "0123456789");
1.1 deraadt 346: }
347:
1.15 otto 348: static void
1.13 deraadt 349: getformat(void)
1.1 deraadt 350: {
1.42 tb 351: char *p;
352:
353: p = format;
354: while ((p = strchr(p, '%')) != NULL && p[1] == '%')
355: p += 2;
356:
357: if (p == NULL && !chardata) {
358: if (asprintf(&format, "%s%%.%df", format, prec) < 0)
359: err(1, NULL);
360: } else if (p == NULL && chardata) {
361: if (asprintf(&format, "%s%%c", format) < 0)
362: err(1, NULL);
363: } else if (p[1] == '\0') {
1.39 tb 364: /* cannot end in single '%' */
1.42 tb 365: if (asprintf(&format, "%s%%", format) < 0)
366: err(1, NULL);
1.8 pjanzen 367: } else {
1.17 otto 368: /*
369: * Allow conversion format specifiers of the form
370: * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
1.45 tb 371: * [l]{d,i,o,u,x} or {f,e,g,F,E,G,d,o,x,D,O,U,X,c,u}
1.17 otto 372: */
1.42 tb 373: char *fmt;
374: int dot, hash, space, sign, numbers;
375:
376: fmt = p++;
1.17 otto 377: dot = hash = space = sign = numbers = 0;
1.23 deraadt 378: while (!isalpha((unsigned char)*p)) {
379: if (isdigit((unsigned char)*p)) {
1.17 otto 380: numbers++;
381: p++;
382: } else if ((*p == '#' && !(numbers|dot|sign|space|
383: hash++)) ||
384: (*p == ' ' && !(numbers|dot|space++)) ||
385: ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
386: || (*p == '.' && !(dot++)))
387: p++;
388: else
389: goto fmt_broken;
390: }
391: if (*p == 'l') {
392: longdata = true;
393: if (*++p == 'l') {
1.40 tb 394: p++;
1.17 otto 395: goto fmt_broken;
396: }
397: }
1.1 deraadt 398: switch (*p) {
1.43 tb 399: case 'd':
400: case 'i':
401: intdata = true;
402: break;
403: case 'o':
404: case 'u':
405: case 'x':
406: case 'X':
1.17 otto 407: intdata = nosign = true;
1.1 deraadt 408: break;
1.43 tb 409: case 'D':
410: if (longdata)
411: goto fmt_broken;
412: longdata = intdata = true; /* same as %ld */
413: break;
414: case 'O':
415: case 'U':
416: if (longdata)
417: goto fmt_broken;
418: longdata = intdata = nosign = true; /* same as %l[ou] */
1.1 deraadt 419: break;
1.17 otto 420: case 'c':
1.43 tb 421: if (longdata)
422: goto fmt_broken;
423: chardata = true;
424: break;
425: case 'e':
426: case 'E':
427: case 'f':
1.44 tb 428: case 'F':
1.43 tb 429: case 'g':
430: case 'G':
431: if (longdata)
432: goto fmt_broken;
433: /* No cast needed for printing in putdata() */
434: break;
1.8 pjanzen 435: default:
1.17 otto 436: fmt_broken:
1.42 tb 437: errx(1, "illegal or unsupported format '%.*s'",
438: (int)(p + 1 - fmt), fmt);
1.1 deraadt 439: }
1.42 tb 440:
441: while ((p = strchr(p, '%')) != NULL && p[1] == '%')
442: p += 2;
443:
444: if (p != NULL) {
445: if (p[1] != '\0')
1.17 otto 446: errx(1, "too many conversions");
1.42 tb 447: /* cannot end in single '%' */
448: if (asprintf(&format, "%s%%", format) < 0)
449: err(1, NULL);
450: }
1.1 deraadt 451: }
452: }