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