19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 299b50d902SRodney W. Grimes 309b50d902SRodney W. Grimes #ifndef lint 317bd7ad50SPhilippe Charnier static const char copyright[] = 329b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\ 339b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 349b50d902SRodney W. Grimes #endif /* not lint */ 359b50d902SRodney W. Grimes 369b50d902SRodney W. Grimes #ifndef lint 377bd7ad50SPhilippe Charnier #if 0 389b50d902SRodney W. Grimes static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93"; 397bd7ad50SPhilippe Charnier #endif 40cf0def93SJuli Mallett #endif 41cf0def93SJuli Mallett #include <sys/cdefs.h> 42cf0def93SJuli Mallett __FBSDID("$FreeBSD$"); 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes /* 459b50d902SRodney W. Grimes * jot - print sequential or random data 469b50d902SRodney W. Grimes * 479b50d902SRodney W. Grimes * Author: John Kunze, Office of Comp. Affairs, UCB 489b50d902SRodney W. Grimes */ 499b50d902SRodney W. Grimes 50*1532e656SConrad Meyer #include <sys/capsicum.h> 51*1532e656SConrad Meyer #include <capsicum_helpers.h> 529b50d902SRodney W. Grimes #include <ctype.h> 537bd7ad50SPhilippe Charnier #include <err.h> 54*1532e656SConrad Meyer #include <errno.h> 559b50d902SRodney W. Grimes #include <limits.h> 569b50d902SRodney W. Grimes #include <stdio.h> 570ad736e6SMike Barcroft #include <stdint.h> 589b50d902SRodney W. Grimes #include <stdlib.h> 59a26a6612SDiomidis Spinellis #include <stdbool.h> 609b50d902SRodney W. Grimes #include <string.h> 61821df508SXin LI #include <time.h> 629d288406SAndrey A. Chernov #include <unistd.h> 639b50d902SRodney W. Grimes 643241f274SDiomidis Spinellis /* Defaults */ 659b50d902SRodney W. Grimes #define REPS_DEF 100 669b50d902SRodney W. Grimes #define BEGIN_DEF 1 679b50d902SRodney W. Grimes #define ENDER_DEF 100 689b50d902SRodney W. Grimes #define STEP_DEF 1 699b50d902SRodney W. Grimes 703241f274SDiomidis Spinellis /* Flags of options that have been set */ 71afe53a49SDiomidis Spinellis #define HAVE_STEP 1 72afe53a49SDiomidis Spinellis #define HAVE_ENDER 2 73afe53a49SDiomidis Spinellis #define HAVE_BEGIN 4 74afe53a49SDiomidis Spinellis #define HAVE_REPS 8 75afe53a49SDiomidis Spinellis 76fee14f30SDiomidis Spinellis #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0) 779b50d902SRodney W. Grimes 783241f274SDiomidis Spinellis static bool boring; 799c24af3dSBrian Somers static int prec = -1; 803241f274SDiomidis Spinellis static bool longdata; 813241f274SDiomidis Spinellis static bool intdata; 823241f274SDiomidis Spinellis static bool chardata; 833241f274SDiomidis Spinellis static bool nosign; 843241f274SDiomidis Spinellis static const char *sepstring = "\n"; 853241f274SDiomidis Spinellis static char format[BUFSIZ]; 869b50d902SRodney W. Grimes 873241f274SDiomidis Spinellis static void getformat(void); 883241f274SDiomidis Spinellis static int getprec(const char *); 893241f274SDiomidis Spinellis static int putdata(double, bool); 90f1bb2cd2SWarner Losh static void usage(void); 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes int 93cf0def93SJuli Mallett main(int argc, char **argv) 949b50d902SRodney W. Grimes { 95*1532e656SConrad Meyer cap_rights_t rights; 963241f274SDiomidis Spinellis bool have_format = false; 973241f274SDiomidis Spinellis bool infinity = false; 983241f274SDiomidis Spinellis bool nofinalnl = false; 993241f274SDiomidis Spinellis bool randomize = false; 1003241f274SDiomidis Spinellis bool use_random = false; 1013241f274SDiomidis Spinellis int ch; 1023241f274SDiomidis Spinellis int mask = 0; 1033241f274SDiomidis Spinellis int n = 0; 10458a654f6SBrian Somers double begin = BEGIN_DEF; 1053241f274SDiomidis Spinellis double divisor; 10658a654f6SBrian Somers double ender = ENDER_DEF; 10758a654f6SBrian Somers double s = STEP_DEF; 10855f965aeSDiomidis Spinellis double x, y; 10955f965aeSDiomidis Spinellis long i; 11058a654f6SBrian Somers long reps = REPS_DEF; 1119b50d902SRodney W. Grimes 112*1532e656SConrad Meyer if (caph_limit_stdio() < 0) 113*1532e656SConrad Meyer err(1, "unable to limit rights for stdio"); 114*1532e656SConrad Meyer cap_rights_init(&rights); 115*1532e656SConrad Meyer if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS) 116*1532e656SConrad Meyer err(1, "unable to limit rights for stdin"); 117*1532e656SConrad Meyer 118*1532e656SConrad Meyer /* 119*1532e656SConrad Meyer * Cache NLS data, for strerror, for err(3), before entering capability 120*1532e656SConrad Meyer * mode. 121*1532e656SConrad Meyer */ 122*1532e656SConrad Meyer caph_cache_catpages(); 123*1532e656SConrad Meyer 124*1532e656SConrad Meyer if (cap_enter() < 0 && errno != ENOSYS) 125*1532e656SConrad Meyer err(1, "unable to enter capability mode"); 126*1532e656SConrad Meyer 1273241f274SDiomidis Spinellis while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1) 128cf0def93SJuli Mallett switch (ch) { 1299b50d902SRodney W. Grimes case 'b': 1303241f274SDiomidis Spinellis boring = true; 13115ba0427SDima Dorfman /* FALLTHROUGH */ 1329b50d902SRodney W. Grimes case 'w': 13315ba0427SDima Dorfman if (strlcpy(format, optarg, sizeof(format)) >= 13415ba0427SDima Dorfman sizeof(format)) 13515ba0427SDima Dorfman errx(1, "-%c word too long", ch); 136d129c68aSDiomidis Spinellis have_format = true; 1379b50d902SRodney W. Grimes break; 1383241f274SDiomidis Spinellis case 'c': 1393241f274SDiomidis Spinellis chardata = true; 1403241f274SDiomidis Spinellis break; 1413241f274SDiomidis Spinellis case 'n': 1423241f274SDiomidis Spinellis nofinalnl = true; 1439b50d902SRodney W. Grimes break; 1449b50d902SRodney W. Grimes case 'p': 14515ba0427SDima Dorfman prec = atoi(optarg); 1469c24af3dSBrian Somers if (prec < 0) 1477bd7ad50SPhilippe Charnier errx(1, "bad precision value"); 148d129c68aSDiomidis Spinellis have_format = true; 1499b50d902SRodney W. Grimes break; 1503241f274SDiomidis Spinellis case 'r': 1513241f274SDiomidis Spinellis randomize = true; 1523241f274SDiomidis Spinellis break; 1533241f274SDiomidis Spinellis case 's': 1543241f274SDiomidis Spinellis sepstring = optarg; 1553241f274SDiomidis Spinellis break; 1569b50d902SRodney W. Grimes default: 1577bd7ad50SPhilippe Charnier usage(); 1589b50d902SRodney W. Grimes } 15915ba0427SDima Dorfman argc -= optind; 16015ba0427SDima Dorfman argv += optind; 1619b50d902SRodney W. Grimes 16215ba0427SDima Dorfman switch (argc) { /* examine args right to left, falling thru cases */ 1639b50d902SRodney W. Grimes case 4: 16415ba0427SDima Dorfman if (!is_default(argv[3])) { 16515ba0427SDima Dorfman if (!sscanf(argv[3], "%lf", &s)) 16615ba0427SDima Dorfman errx(1, "bad s value: %s", argv[3]); 167afe53a49SDiomidis Spinellis mask |= HAVE_STEP; 168a26a6612SDiomidis Spinellis if (randomize) 169d129c68aSDiomidis Spinellis use_random = true; 1709b50d902SRodney W. Grimes } 171afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1729b50d902SRodney W. Grimes case 3: 17315ba0427SDima Dorfman if (!is_default(argv[2])) { 17415ba0427SDima Dorfman if (!sscanf(argv[2], "%lf", &ender)) 17515ba0427SDima Dorfman ender = argv[2][strlen(argv[2])-1]; 176afe53a49SDiomidis Spinellis mask |= HAVE_ENDER; 1779c24af3dSBrian Somers if (prec < 0) 17815ba0427SDima Dorfman n = getprec(argv[2]); 1799b50d902SRodney W. Grimes } 180afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1819b50d902SRodney W. Grimes case 2: 18215ba0427SDima Dorfman if (!is_default(argv[1])) { 18315ba0427SDima Dorfman if (!sscanf(argv[1], "%lf", &begin)) 18415ba0427SDima Dorfman begin = argv[1][strlen(argv[1])-1]; 185afe53a49SDiomidis Spinellis mask |= HAVE_BEGIN; 1869c24af3dSBrian Somers if (prec < 0) 18715ba0427SDima Dorfman prec = getprec(argv[1]); 1889b50d902SRodney W. Grimes if (n > prec) /* maximum precision */ 1899b50d902SRodney W. Grimes prec = n; 1909b50d902SRodney W. Grimes } 191afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1929b50d902SRodney W. Grimes case 1: 19315ba0427SDima Dorfman if (!is_default(argv[0])) { 19415ba0427SDima Dorfman if (!sscanf(argv[0], "%ld", &reps)) 19515ba0427SDima Dorfman errx(1, "bad reps value: %s", argv[0]); 196afe53a49SDiomidis Spinellis mask |= HAVE_REPS; 1979b50d902SRodney W. Grimes } 1989b50d902SRodney W. Grimes break; 1999b50d902SRodney W. Grimes case 0: 2007bd7ad50SPhilippe Charnier usage(); 2019b50d902SRodney W. Grimes default: 20215ba0427SDima Dorfman errx(1, "too many arguments. What do you mean by %s?", 20315ba0427SDima Dorfman argv[4]); 2049b50d902SRodney W. Grimes } 2059b50d902SRodney W. Grimes getformat(); 2069c24af3dSBrian Somers 2079c24af3dSBrian Somers if (prec == -1) 2089c24af3dSBrian Somers prec = 0; 2099c24af3dSBrian Somers 2109b50d902SRodney W. Grimes while (mask) /* 4 bit mask has 1's where last 4 args were given */ 2119b50d902SRodney W. Grimes switch (mask) { /* fill in the 0's by default or computation */ 212afe53a49SDiomidis Spinellis case HAVE_STEP: 213afe53a49SDiomidis Spinellis case HAVE_ENDER: 214afe53a49SDiomidis Spinellis case HAVE_ENDER | HAVE_STEP: 215afe53a49SDiomidis Spinellis case HAVE_BEGIN: 216afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_STEP: 2179b50d902SRodney W. Grimes reps = REPS_DEF; 218d737ec1aSDiomidis Spinellis mask |= HAVE_REPS; 2199b50d902SRodney W. Grimes break; 2207d71f06cSDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER: 2217d71f06cSDiomidis Spinellis s = ender > begin ? 1 : -1; 2227d71f06cSDiomidis Spinellis mask |= HAVE_STEP; 2237d71f06cSDiomidis Spinellis break; 224afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 225d737ec1aSDiomidis Spinellis if (randomize) 2269b50d902SRodney W. Grimes reps = REPS_DEF; 227d737ec1aSDiomidis Spinellis else if (s == 0.0) 2289b50d902SRodney W. Grimes reps = 0; 229d737ec1aSDiomidis Spinellis else 2309b50d902SRodney W. Grimes reps = (ender - begin + s) / s; 2319b50d902SRodney W. Grimes if (reps <= 0) 2327bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2339b50d902SRodney W. Grimes mask = 0; 2349b50d902SRodney W. Grimes break; 235afe53a49SDiomidis Spinellis case HAVE_REPS: 236afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_STEP: 2379b50d902SRodney W. Grimes begin = BEGIN_DEF; 238d737ec1aSDiomidis Spinellis mask |= HAVE_BEGIN; 2399b50d902SRodney W. Grimes break; 240afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER: 241a26a6612SDiomidis Spinellis s = STEP_DEF; 242afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; 2439b50d902SRodney W. Grimes break; 244afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER | HAVE_STEP: 2459b50d902SRodney W. Grimes if (randomize) 2469b50d902SRodney W. Grimes begin = BEGIN_DEF; 2479b50d902SRodney W. Grimes else if (reps == 0) 2487bd7ad50SPhilippe Charnier errx(1, "must specify begin if reps == 0"); 2499b50d902SRodney W. Grimes begin = ender - reps * s + s; 2509b50d902SRodney W. Grimes mask = 0; 2519b50d902SRodney W. Grimes break; 252afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN: 253a26a6612SDiomidis Spinellis s = STEP_DEF; 254afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; 2559b50d902SRodney W. Grimes break; 256afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: 2579b50d902SRodney W. Grimes if (randomize) 2589b50d902SRodney W. Grimes ender = ENDER_DEF; 2599b50d902SRodney W. Grimes else 2609b50d902SRodney W. Grimes ender = begin + reps * s - s; 2619b50d902SRodney W. Grimes mask = 0; 2629b50d902SRodney W. Grimes break; 263afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: 264a26a6612SDiomidis Spinellis if (reps == 0) 2657bd7ad50SPhilippe Charnier errx(1, "infinite sequences cannot be bounded"); 2669b50d902SRodney W. Grimes else if (reps == 1) 2679b50d902SRodney W. Grimes s = 0.0; 2689b50d902SRodney W. Grimes else 2699b50d902SRodney W. Grimes s = (ender - begin) / (reps - 1); 2709b50d902SRodney W. Grimes mask = 0; 2719b50d902SRodney W. Grimes break; 272afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 273afe53a49SDiomidis Spinellis /* if reps given and implied, */ 2749b50d902SRodney W. Grimes if (!randomize && s != 0.0) { 2759b50d902SRodney W. Grimes long t = (ender - begin + s) / s; 2769b50d902SRodney W. Grimes if (t <= 0) 2777bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2789b50d902SRodney W. Grimes if (t < reps) /* take lesser */ 2799b50d902SRodney W. Grimes reps = t; 2809b50d902SRodney W. Grimes } 2819b50d902SRodney W. Grimes mask = 0; 2829b50d902SRodney W. Grimes break; 2839b50d902SRodney W. Grimes default: 2847bd7ad50SPhilippe Charnier errx(1, "bad mask"); 2859b50d902SRodney W. Grimes } 2869b50d902SRodney W. Grimes if (reps == 0) 2873241f274SDiomidis Spinellis infinity = true; 28815ba0427SDima Dorfman if (randomize) { 289d129c68aSDiomidis Spinellis if (use_random) { 290a26a6612SDiomidis Spinellis srandom((unsigned long)s); 291d129c68aSDiomidis Spinellis divisor = (double)INT32_MAX + 1; 292d129c68aSDiomidis Spinellis } else 293d129c68aSDiomidis Spinellis divisor = (double)UINT32_MAX + 1; 294d129c68aSDiomidis Spinellis 295d129c68aSDiomidis Spinellis /* 296d129c68aSDiomidis Spinellis * Attempt to DWIM when the user has specified an 297d129c68aSDiomidis Spinellis * integer range within that of the random number 298d129c68aSDiomidis Spinellis * generator: distribute the numbers equally in 299d129c68aSDiomidis Spinellis * the range [begin .. ender]. Jot's default %.0f 300d129c68aSDiomidis Spinellis * format would make the appearance of the first and 301d129c68aSDiomidis Spinellis * last specified value half as likely as the rest. 302d129c68aSDiomidis Spinellis */ 303d129c68aSDiomidis Spinellis if (!have_format && prec == 0 && 304d129c68aSDiomidis Spinellis begin >= 0 && begin < divisor && 305d129c68aSDiomidis Spinellis ender >= 0 && ender < divisor) { 3069c24af3dSBrian Somers if (begin <= ender) 307d129c68aSDiomidis Spinellis ender += 1; 3089c24af3dSBrian Somers else 3099c24af3dSBrian Somers begin += 1; 3103241f274SDiomidis Spinellis nosign = true; 3113241f274SDiomidis Spinellis intdata = true; 312d129c68aSDiomidis Spinellis (void)strlcpy(format, 313d129c68aSDiomidis Spinellis chardata ? "%c" : "%u", sizeof(format)); 314d129c68aSDiomidis Spinellis } 3159c24af3dSBrian Somers x = ender - begin; 31655f965aeSDiomidis Spinellis for (i = 1; i <= reps || infinity; i++) { 317d129c68aSDiomidis Spinellis if (use_random) 318d129c68aSDiomidis Spinellis y = random() / divisor; 319a26a6612SDiomidis Spinellis else 320d129c68aSDiomidis Spinellis y = arc4random() / divisor; 3213241f274SDiomidis Spinellis if (putdata(y * x + begin, !(reps - i))) 32215ba0427SDima Dorfman errx(1, "range error in conversion"); 32315ba0427SDima Dorfman } 32415ba0427SDima Dorfman } else 32555f965aeSDiomidis Spinellis for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 3263241f274SDiomidis Spinellis if (putdata(x, !(reps - i))) 32715ba0427SDima Dorfman errx(1, "range error in conversion"); 32815ba0427SDima Dorfman if (!nofinalnl) 32915ba0427SDima Dorfman putchar('\n'); 33015ba0427SDima Dorfman exit(0); 3319b50d902SRodney W. Grimes } 3329b50d902SRodney W. Grimes 3333241f274SDiomidis Spinellis /* 3343241f274SDiomidis Spinellis * Send x to stdout using the specified format. 3353241f274SDiomidis Spinellis * Last is true if this is the set's last value. 3363241f274SDiomidis Spinellis * Return 0 if OK, or a positive number if the number passed was 3373241f274SDiomidis Spinellis * outside the range specified by the various flags. 3383241f274SDiomidis Spinellis */ 3393241f274SDiomidis Spinellis static int 3403241f274SDiomidis Spinellis putdata(double x, bool last) 3419b50d902SRodney W. Grimes { 3429b50d902SRodney W. Grimes 3435249bd84SSheldon Hearn if (boring) 344612740bdSKris Kennaway printf("%s", format); 3455249bd84SSheldon Hearn else if (longdata && nosign) { 3465249bd84SSheldon Hearn if (x <= (double)ULONG_MAX && x >= (double)0) 3478db8a33bSSheldon Hearn printf(format, (unsigned long)x); 3485249bd84SSheldon Hearn else 3495249bd84SSheldon Hearn return (1); 3505249bd84SSheldon Hearn } else if (longdata) { 3515249bd84SSheldon Hearn if (x <= (double)LONG_MAX && x >= (double)LONG_MIN) 3528db8a33bSSheldon Hearn printf(format, (long)x); 3535249bd84SSheldon Hearn else 3545249bd84SSheldon Hearn return (1); 3555249bd84SSheldon Hearn } else if (chardata || (intdata && !nosign)) { 3565249bd84SSheldon Hearn if (x <= (double)INT_MAX && x >= (double)INT_MIN) 3575249bd84SSheldon Hearn printf(format, (int)x); 3585249bd84SSheldon Hearn else 3595249bd84SSheldon Hearn return (1); 3605249bd84SSheldon Hearn } else if (intdata) { 3615249bd84SSheldon Hearn if (x <= (double)UINT_MAX && x >= (double)0) 3625249bd84SSheldon Hearn printf(format, (unsigned int)x); 3635249bd84SSheldon Hearn else 3645249bd84SSheldon Hearn return (1); 3655249bd84SSheldon Hearn 3665249bd84SSheldon Hearn } else 3679b50d902SRodney W. Grimes printf(format, x); 3683241f274SDiomidis Spinellis if (!last) 3699b50d902SRodney W. Grimes fputs(sepstring, stdout); 3705249bd84SSheldon Hearn 3715249bd84SSheldon Hearn return (0); 3729b50d902SRodney W. Grimes } 3739b50d902SRodney W. Grimes 3747bd7ad50SPhilippe Charnier static void 375cf0def93SJuli Mallett usage(void) 3769b50d902SRodney W. Grimes { 3777bd7ad50SPhilippe Charnier fprintf(stderr, "%s\n%s\n", 3787bd7ad50SPhilippe Charnier "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]", 3797bd7ad50SPhilippe Charnier " [reps [begin [end [s]]]]"); 3809b50d902SRodney W. Grimes exit(1); 3819b50d902SRodney W. Grimes } 3829b50d902SRodney W. Grimes 3833241f274SDiomidis Spinellis /* 3843241f274SDiomidis Spinellis * Return the number of digits following the number's decimal point. 3853241f274SDiomidis Spinellis * Return 0 if no decimal point is found. 3863241f274SDiomidis Spinellis */ 3873241f274SDiomidis Spinellis static int 3883241f274SDiomidis Spinellis getprec(const char *str) 3899b50d902SRodney W. Grimes { 3903241f274SDiomidis Spinellis const char *p; 3913241f274SDiomidis Spinellis const char *q; 3929b50d902SRodney W. Grimes 393edd7b267SDima Dorfman for (p = str; *p; p++) 3949b50d902SRodney W. Grimes if (*p == '.') 3959b50d902SRodney W. Grimes break; 3969b50d902SRodney W. Grimes if (!*p) 3979b50d902SRodney W. Grimes return (0); 3989b50d902SRodney W. Grimes for (q = ++p; *p; p++) 399911a3ff9STim J. Robbins if (!isdigit((unsigned char)*p)) 4009b50d902SRodney W. Grimes break; 4019b50d902SRodney W. Grimes return (p - q); 4029b50d902SRodney W. Grimes } 4039b50d902SRodney W. Grimes 4043241f274SDiomidis Spinellis /* 4053241f274SDiomidis Spinellis * Set format, intdata, chardata, longdata, and nosign 4063241f274SDiomidis Spinellis * based on the command line arguments. 4073241f274SDiomidis Spinellis */ 4083241f274SDiomidis Spinellis static void 409cf0def93SJuli Mallett getformat(void) 4109b50d902SRodney W. Grimes { 411edd7b267SDima Dorfman char *p, *p2; 4125249bd84SSheldon Hearn int dot, hash, space, sign, numbers = 0; 41315ba0427SDima Dorfman size_t sz; 4149b50d902SRodney W. Grimes 4159b50d902SRodney W. Grimes if (boring) /* no need to bother */ 4169b50d902SRodney W. Grimes return; 4179b50d902SRodney W. Grimes for (p = format; *p; p++) /* look for '%' */ 41882417e9fSXin LI if (*p == '%') { 41934785a9fSDiomidis Spinellis if (p[1] == '%') 42034785a9fSDiomidis Spinellis p++; /* leave %% alone */ 42134785a9fSDiomidis Spinellis else 4229b50d902SRodney W. Grimes break; 42382417e9fSXin LI } 42415ba0427SDima Dorfman sz = sizeof(format) - strlen(format) - 1; 42515ba0427SDima Dorfman if (!*p && !chardata) { 42615ba0427SDima Dorfman if (snprintf(p, sz, "%%.%df", prec) >= (int)sz) 42715ba0427SDima Dorfman errx(1, "-w word too long"); 42815ba0427SDima Dorfman } else if (!*p && chardata) { 42915ba0427SDima Dorfman if (strlcpy(p, "%c", sz) >= sz) 43015ba0427SDima Dorfman errx(1, "-w word too long"); 4313241f274SDiomidis Spinellis intdata = true; 43215ba0427SDima Dorfman } else if (!*(p+1)) { 43315ba0427SDima Dorfman if (sz <= 0) 43415ba0427SDima Dorfman errx(1, "-w word too long"); 4359b50d902SRodney W. Grimes strcat(format, "%"); /* cannot end in single '%' */ 43615ba0427SDima Dorfman } else { 4378db8a33bSSheldon Hearn /* 4388db8a33bSSheldon Hearn * Allow conversion format specifiers of the form 4398db8a33bSSheldon Hearn * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of 4408db8a33bSSheldon Hearn * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u} 4418db8a33bSSheldon Hearn */ 442edd7b267SDima Dorfman p2 = p++; 4438db8a33bSSheldon Hearn dot = hash = space = sign = numbers = 0; 444911a3ff9STim J. Robbins while (!isalpha((unsigned char)*p)) { 445911a3ff9STim J. Robbins if (isdigit((unsigned char)*p)) { 4468db8a33bSSheldon Hearn numbers++; 4479b50d902SRodney W. Grimes p++; 4488db8a33bSSheldon Hearn } else if ((*p == '#' && !(numbers|dot|sign|space| 4498db8a33bSSheldon Hearn hash++)) || 4508db8a33bSSheldon Hearn (*p == ' ' && !(numbers|dot|space++)) || 4518db8a33bSSheldon Hearn ((*p == '+' || *p == '-') && !(numbers|dot|sign++)) 4528db8a33bSSheldon Hearn || (*p == '.' && !(dot++))) 4538db8a33bSSheldon Hearn p++; 4548db8a33bSSheldon Hearn else 4555249bd84SSheldon Hearn goto fmt_broken; 4565249bd84SSheldon Hearn } 4575249bd84SSheldon Hearn if (*p == 'l') { 4583241f274SDiomidis Spinellis longdata = true; 4595249bd84SSheldon Hearn if (*++p == 'l') { 4605249bd84SSheldon Hearn if (p[1] != '\0') 4615249bd84SSheldon Hearn p++; 4625249bd84SSheldon Hearn goto fmt_broken; 4635249bd84SSheldon Hearn } 4648db8a33bSSheldon Hearn } 4659b50d902SRodney W. Grimes switch (*p) { 4668db8a33bSSheldon Hearn case 'o': case 'u': case 'x': case 'X': 4673241f274SDiomidis Spinellis intdata = nosign = true; 4689b50d902SRodney W. Grimes break; 4698db8a33bSSheldon Hearn case 'd': case 'i': 4703241f274SDiomidis Spinellis intdata = true; 4718db8a33bSSheldon Hearn break; 4728db8a33bSSheldon Hearn case 'D': 4735249bd84SSheldon Hearn if (!longdata) { 4743241f274SDiomidis Spinellis intdata = true; 4758db8a33bSSheldon Hearn break; 4768db8a33bSSheldon Hearn } 4778db8a33bSSheldon Hearn case 'O': case 'U': 4785249bd84SSheldon Hearn if (!longdata) { 4793241f274SDiomidis Spinellis intdata = nosign = true; 4808db8a33bSSheldon Hearn break; 4818db8a33bSSheldon Hearn } 4828db8a33bSSheldon Hearn case 'c': 4835249bd84SSheldon Hearn if (!(intdata | longdata)) { 4843241f274SDiomidis Spinellis chardata = true; 4858db8a33bSSheldon Hearn break; 4868db8a33bSSheldon Hearn } 4875249bd84SSheldon Hearn case 'h': case 'n': case 'p': case 'q': case 's': case 'L': 4888db8a33bSSheldon Hearn case '$': case '*': 4895249bd84SSheldon Hearn goto fmt_broken; 4908db8a33bSSheldon Hearn case 'f': case 'e': case 'g': case 'E': case 'G': 4915249bd84SSheldon Hearn if (!longdata) 4928db8a33bSSheldon Hearn break; 4938db8a33bSSheldon Hearn /* FALLTHROUGH */ 4949b50d902SRodney W. Grimes default: 4955249bd84SSheldon Hearn fmt_broken: 4968db8a33bSSheldon Hearn *++p = '\0'; 497edd7b267SDima Dorfman errx(1, "illegal or unsupported format '%s'", p2); 4988db8a33bSSheldon Hearn /* NOTREACHED */ 4998db8a33bSSheldon Hearn } 5008db8a33bSSheldon Hearn while (*++p) 5018db8a33bSSheldon Hearn if (*p == '%' && *(p+1) && *(p+1) != '%') 5028db8a33bSSheldon Hearn errx(1, "too many conversions"); 5038db8a33bSSheldon Hearn else if (*p == '%' && *(p+1) == '%') 5048db8a33bSSheldon Hearn p++; 5058db8a33bSSheldon Hearn else if (*p == '%' && !*(p+1)) { 506f88b45d7SDiomidis Spinellis if (strlcat(format, "%", sizeof(format)) >= 507f88b45d7SDiomidis Spinellis sizeof(format)) 508f88b45d7SDiomidis Spinellis errx(1, "-w word too long"); 5099b50d902SRodney W. Grimes break; 5109b50d902SRodney W. Grimes } 5119b50d902SRodney W. Grimes } 5129b50d902SRodney W. Grimes } 513