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 * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 357bd7ad50SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 417bd7ad50SPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93"; 437bd7ad50SPhilippe Charnier #endif 44cf0def93SJuli Mallett #endif 45cf0def93SJuli Mallett #include <sys/cdefs.h> 46cf0def93SJuli Mallett __FBSDID("$FreeBSD$"); 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes /* 499b50d902SRodney W. Grimes * jot - print sequential or random data 509b50d902SRodney W. Grimes * 519b50d902SRodney W. Grimes * Author: John Kunze, Office of Comp. Affairs, UCB 529b50d902SRodney W. Grimes */ 539b50d902SRodney W. Grimes 549b50d902SRodney W. Grimes #include <ctype.h> 557bd7ad50SPhilippe Charnier #include <err.h> 569b50d902SRodney W. Grimes #include <limits.h> 579b50d902SRodney W. Grimes #include <stdio.h> 580ad736e6SMike Barcroft #include <stdint.h> 599b50d902SRodney W. Grimes #include <stdlib.h> 60a26a6612SDiomidis Spinellis #include <stdbool.h> 619b50d902SRodney W. Grimes #include <string.h> 629b50d902SRodney W. Grimes #include <time.h> 639d288406SAndrey A. Chernov #include <unistd.h> 649b50d902SRodney W. Grimes 653241f274SDiomidis Spinellis /* Defaults */ 669b50d902SRodney W. Grimes #define REPS_DEF 100 679b50d902SRodney W. Grimes #define BEGIN_DEF 1 689b50d902SRodney W. Grimes #define ENDER_DEF 100 699b50d902SRodney W. Grimes #define STEP_DEF 1 709b50d902SRodney W. Grimes 713241f274SDiomidis Spinellis /* Flags of options that have been set */ 72afe53a49SDiomidis Spinellis #define HAVE_STEP 1 73afe53a49SDiomidis Spinellis #define HAVE_ENDER 2 74afe53a49SDiomidis Spinellis #define HAVE_BEGIN 4 75afe53a49SDiomidis Spinellis #define HAVE_REPS 8 76afe53a49SDiomidis Spinellis 77fee14f30SDiomidis Spinellis #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0) 789b50d902SRodney W. Grimes 793241f274SDiomidis Spinellis static bool boring; 803241f274SDiomidis Spinellis static int prec; 813241f274SDiomidis Spinellis static bool longdata; 823241f274SDiomidis Spinellis static bool intdata; 833241f274SDiomidis Spinellis static bool chardata; 843241f274SDiomidis Spinellis static bool nosign; 853241f274SDiomidis Spinellis static const char *sepstring = "\n"; 863241f274SDiomidis Spinellis static char format[BUFSIZ]; 879b50d902SRodney W. Grimes 883241f274SDiomidis Spinellis static void getformat(void); 893241f274SDiomidis Spinellis static int getprec(const char *); 903241f274SDiomidis Spinellis static int putdata(double, bool); 91f1bb2cd2SWarner Losh static void usage(void); 929b50d902SRodney W. Grimes 939b50d902SRodney W. Grimes int 94cf0def93SJuli Mallett main(int argc, char **argv) 959b50d902SRodney W. Grimes { 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; 1043241f274SDiomidis Spinellis double begin; 1053241f274SDiomidis Spinellis double divisor; 1063241f274SDiomidis Spinellis double ender; 1073241f274SDiomidis Spinellis double s; 10855f965aeSDiomidis Spinellis double x, y; 10955f965aeSDiomidis Spinellis long i; 1103241f274SDiomidis Spinellis long reps; 1119b50d902SRodney W. Grimes 1123241f274SDiomidis Spinellis while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1) 113cf0def93SJuli Mallett switch (ch) { 1149b50d902SRodney W. Grimes case 'b': 1153241f274SDiomidis Spinellis boring = true; 11615ba0427SDima Dorfman /* FALLTHROUGH */ 1179b50d902SRodney W. Grimes case 'w': 11815ba0427SDima Dorfman if (strlcpy(format, optarg, sizeof(format)) >= 11915ba0427SDima Dorfman sizeof(format)) 12015ba0427SDima Dorfman errx(1, "-%c word too long", ch); 121d129c68aSDiomidis Spinellis have_format = true; 1229b50d902SRodney W. Grimes break; 1233241f274SDiomidis Spinellis case 'c': 1243241f274SDiomidis Spinellis chardata = true; 1253241f274SDiomidis Spinellis break; 1263241f274SDiomidis Spinellis case 'n': 1273241f274SDiomidis Spinellis nofinalnl = true; 1289b50d902SRodney W. Grimes break; 1299b50d902SRodney W. Grimes case 'p': 13015ba0427SDima Dorfman prec = atoi(optarg); 1319b50d902SRodney W. Grimes if (prec <= 0) 1327bd7ad50SPhilippe Charnier errx(1, "bad precision value"); 133d129c68aSDiomidis Spinellis have_format = true; 1349b50d902SRodney W. Grimes break; 1353241f274SDiomidis Spinellis case 'r': 1363241f274SDiomidis Spinellis randomize = true; 1373241f274SDiomidis Spinellis break; 1383241f274SDiomidis Spinellis case 's': 1393241f274SDiomidis Spinellis sepstring = optarg; 1403241f274SDiomidis Spinellis break; 1419b50d902SRodney W. Grimes default: 1427bd7ad50SPhilippe Charnier usage(); 1439b50d902SRodney W. Grimes } 14415ba0427SDima Dorfman argc -= optind; 14515ba0427SDima Dorfman argv += optind; 1469b50d902SRodney W. Grimes 14715ba0427SDima Dorfman switch (argc) { /* examine args right to left, falling thru cases */ 1489b50d902SRodney W. Grimes case 4: 14915ba0427SDima Dorfman if (!is_default(argv[3])) { 15015ba0427SDima Dorfman if (!sscanf(argv[3], "%lf", &s)) 15115ba0427SDima Dorfman errx(1, "bad s value: %s", argv[3]); 152afe53a49SDiomidis Spinellis mask |= HAVE_STEP; 153a26a6612SDiomidis Spinellis if (randomize) 154d129c68aSDiomidis Spinellis use_random = true; 1559b50d902SRodney W. Grimes } 156afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1579b50d902SRodney W. Grimes case 3: 15815ba0427SDima Dorfman if (!is_default(argv[2])) { 15915ba0427SDima Dorfman if (!sscanf(argv[2], "%lf", &ender)) 16015ba0427SDima Dorfman ender = argv[2][strlen(argv[2])-1]; 161afe53a49SDiomidis Spinellis mask |= HAVE_ENDER; 1629b50d902SRodney W. Grimes if (!prec) 16315ba0427SDima Dorfman n = getprec(argv[2]); 1649b50d902SRodney W. Grimes } 165afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1669b50d902SRodney W. Grimes case 2: 16715ba0427SDima Dorfman if (!is_default(argv[1])) { 16815ba0427SDima Dorfman if (!sscanf(argv[1], "%lf", &begin)) 16915ba0427SDima Dorfman begin = argv[1][strlen(argv[1])-1]; 170afe53a49SDiomidis Spinellis mask |= HAVE_BEGIN; 1719b50d902SRodney W. Grimes if (!prec) 17215ba0427SDima Dorfman prec = getprec(argv[1]); 1739b50d902SRodney W. Grimes if (n > prec) /* maximum precision */ 1749b50d902SRodney W. Grimes prec = n; 1759b50d902SRodney W. Grimes } 176afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1779b50d902SRodney W. Grimes case 1: 17815ba0427SDima Dorfman if (!is_default(argv[0])) { 17915ba0427SDima Dorfman if (!sscanf(argv[0], "%ld", &reps)) 18015ba0427SDima Dorfman errx(1, "bad reps value: %s", argv[0]); 181afe53a49SDiomidis Spinellis mask |= HAVE_REPS; 1829b50d902SRodney W. Grimes } 1839b50d902SRodney W. Grimes break; 1849b50d902SRodney W. Grimes case 0: 1857bd7ad50SPhilippe Charnier usage(); 1869b50d902SRodney W. Grimes default: 18715ba0427SDima Dorfman errx(1, "too many arguments. What do you mean by %s?", 18815ba0427SDima Dorfman argv[4]); 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes getformat(); 1919b50d902SRodney W. Grimes while (mask) /* 4 bit mask has 1's where last 4 args were given */ 1929b50d902SRodney W. Grimes switch (mask) { /* fill in the 0's by default or computation */ 193afe53a49SDiomidis Spinellis case HAVE_STEP: 194afe53a49SDiomidis Spinellis case HAVE_ENDER: 195afe53a49SDiomidis Spinellis case HAVE_ENDER | HAVE_STEP: 196afe53a49SDiomidis Spinellis case HAVE_BEGIN: 197afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_STEP: 1989b50d902SRodney W. Grimes reps = REPS_DEF; 199d737ec1aSDiomidis Spinellis mask |= HAVE_REPS; 2009b50d902SRodney W. Grimes break; 2017d71f06cSDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER: 2027d71f06cSDiomidis Spinellis s = ender > begin ? 1 : -1; 2037d71f06cSDiomidis Spinellis mask |= HAVE_STEP; 2047d71f06cSDiomidis Spinellis break; 205afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 206d737ec1aSDiomidis Spinellis if (randomize) 2079b50d902SRodney W. Grimes reps = REPS_DEF; 208d737ec1aSDiomidis Spinellis else if (s == 0.0) 2099b50d902SRodney W. Grimes reps = 0; 210d737ec1aSDiomidis Spinellis else 2119b50d902SRodney W. Grimes reps = (ender - begin + s) / s; 2129b50d902SRodney W. Grimes if (reps <= 0) 2137bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2149b50d902SRodney W. Grimes mask = 0; 2159b50d902SRodney W. Grimes break; 216afe53a49SDiomidis Spinellis case HAVE_REPS: 217afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_STEP: 2189b50d902SRodney W. Grimes begin = BEGIN_DEF; 219d737ec1aSDiomidis Spinellis mask |= HAVE_BEGIN; 2209b50d902SRodney W. Grimes break; 221afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER: 222a26a6612SDiomidis Spinellis s = STEP_DEF; 223afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; 2249b50d902SRodney W. Grimes break; 225afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER | HAVE_STEP: 2269b50d902SRodney W. Grimes if (randomize) 2279b50d902SRodney W. Grimes begin = BEGIN_DEF; 2289b50d902SRodney W. Grimes else if (reps == 0) 2297bd7ad50SPhilippe Charnier errx(1, "must specify begin if reps == 0"); 2309b50d902SRodney W. Grimes begin = ender - reps * s + s; 2319b50d902SRodney W. Grimes mask = 0; 2329b50d902SRodney W. Grimes break; 233afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN: 234a26a6612SDiomidis Spinellis s = STEP_DEF; 235afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; 2369b50d902SRodney W. Grimes break; 237afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: 2389b50d902SRodney W. Grimes if (randomize) 2399b50d902SRodney W. Grimes ender = ENDER_DEF; 2409b50d902SRodney W. Grimes else 2419b50d902SRodney W. Grimes ender = begin + reps * s - s; 2429b50d902SRodney W. Grimes mask = 0; 2439b50d902SRodney W. Grimes break; 244afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: 245a26a6612SDiomidis Spinellis if (reps == 0) 2467bd7ad50SPhilippe Charnier errx(1, "infinite sequences cannot be bounded"); 2479b50d902SRodney W. Grimes else if (reps == 1) 2489b50d902SRodney W. Grimes s = 0.0; 2499b50d902SRodney W. Grimes else 2509b50d902SRodney W. Grimes s = (ender - begin) / (reps - 1); 2519b50d902SRodney W. Grimes mask = 0; 2529b50d902SRodney W. Grimes break; 253afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 254afe53a49SDiomidis Spinellis /* if reps given and implied, */ 2559b50d902SRodney W. Grimes if (!randomize && s != 0.0) { 2569b50d902SRodney W. Grimes long t = (ender - begin + s) / s; 2579b50d902SRodney W. Grimes if (t <= 0) 2587bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2599b50d902SRodney W. Grimes if (t < reps) /* take lesser */ 2609b50d902SRodney W. Grimes reps = t; 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes mask = 0; 2639b50d902SRodney W. Grimes break; 2649b50d902SRodney W. Grimes default: 2657bd7ad50SPhilippe Charnier errx(1, "bad mask"); 2669b50d902SRodney W. Grimes } 2679b50d902SRodney W. Grimes if (reps == 0) 2683241f274SDiomidis Spinellis infinity = true; 26915ba0427SDima Dorfman if (randomize) { 270d129c68aSDiomidis Spinellis if (use_random) { 271a26a6612SDiomidis Spinellis srandom((unsigned long)s); 272d129c68aSDiomidis Spinellis divisor = (double)INT32_MAX + 1; 273d129c68aSDiomidis Spinellis } else 274d129c68aSDiomidis Spinellis divisor = (double)UINT32_MAX + 1; 275d129c68aSDiomidis Spinellis 276d129c68aSDiomidis Spinellis /* 277d129c68aSDiomidis Spinellis * Attempt to DWIM when the user has specified an 278d129c68aSDiomidis Spinellis * integer range within that of the random number 279d129c68aSDiomidis Spinellis * generator: distribute the numbers equally in 280d129c68aSDiomidis Spinellis * the range [begin .. ender]. Jot's default %.0f 281d129c68aSDiomidis Spinellis * format would make the appearance of the first and 282d129c68aSDiomidis Spinellis * last specified value half as likely as the rest. 283d129c68aSDiomidis Spinellis */ 284d129c68aSDiomidis Spinellis if (!have_format && prec == 0 && 285d129c68aSDiomidis Spinellis begin >= 0 && begin < divisor && 286d129c68aSDiomidis Spinellis ender >= 0 && ender < divisor) { 287d129c68aSDiomidis Spinellis ender += 1; 2883241f274SDiomidis Spinellis nosign = true; 2893241f274SDiomidis Spinellis intdata = true; 290d129c68aSDiomidis Spinellis (void)strlcpy(format, 291d129c68aSDiomidis Spinellis chardata ? "%c" : "%u", sizeof(format)); 292d129c68aSDiomidis Spinellis } 293d129c68aSDiomidis Spinellis x = (ender - begin) * (ender > begin ? 1 : -1); 29455f965aeSDiomidis Spinellis for (i = 1; i <= reps || infinity; i++) { 295d129c68aSDiomidis Spinellis if (use_random) 296d129c68aSDiomidis Spinellis y = random() / divisor; 297a26a6612SDiomidis Spinellis else 298d129c68aSDiomidis Spinellis y = arc4random() / divisor; 2993241f274SDiomidis Spinellis if (putdata(y * x + begin, !(reps - i))) 30015ba0427SDima Dorfman errx(1, "range error in conversion"); 30115ba0427SDima Dorfman } 30215ba0427SDima Dorfman } else 30355f965aeSDiomidis Spinellis for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 3043241f274SDiomidis Spinellis if (putdata(x, !(reps - i))) 30515ba0427SDima Dorfman errx(1, "range error in conversion"); 30615ba0427SDima Dorfman if (!nofinalnl) 30715ba0427SDima Dorfman putchar('\n'); 30815ba0427SDima Dorfman exit(0); 3099b50d902SRodney W. Grimes } 3109b50d902SRodney W. Grimes 3113241f274SDiomidis Spinellis /* 3123241f274SDiomidis Spinellis * Send x to stdout using the specified format. 3133241f274SDiomidis Spinellis * Last is true if this is the set's last value. 3143241f274SDiomidis Spinellis * Return 0 if OK, or a positive number if the number passed was 3153241f274SDiomidis Spinellis * outside the range specified by the various flags. 3163241f274SDiomidis Spinellis */ 3173241f274SDiomidis Spinellis static int 3183241f274SDiomidis Spinellis putdata(double x, bool last) 3199b50d902SRodney W. Grimes { 3209b50d902SRodney W. Grimes 3215249bd84SSheldon Hearn if (boring) 322612740bdSKris Kennaway printf("%s", format); 3235249bd84SSheldon Hearn else if (longdata && nosign) { 3245249bd84SSheldon Hearn if (x <= (double)ULONG_MAX && x >= (double)0) 3258db8a33bSSheldon Hearn printf(format, (unsigned long)x); 3265249bd84SSheldon Hearn else 3275249bd84SSheldon Hearn return (1); 3285249bd84SSheldon Hearn } else if (longdata) { 3295249bd84SSheldon Hearn if (x <= (double)LONG_MAX && x >= (double)LONG_MIN) 3308db8a33bSSheldon Hearn printf(format, (long)x); 3315249bd84SSheldon Hearn else 3325249bd84SSheldon Hearn return (1); 3335249bd84SSheldon Hearn } else if (chardata || (intdata && !nosign)) { 3345249bd84SSheldon Hearn if (x <= (double)INT_MAX && x >= (double)INT_MIN) 3355249bd84SSheldon Hearn printf(format, (int)x); 3365249bd84SSheldon Hearn else 3375249bd84SSheldon Hearn return (1); 3385249bd84SSheldon Hearn } else if (intdata) { 3395249bd84SSheldon Hearn if (x <= (double)UINT_MAX && x >= (double)0) 3405249bd84SSheldon Hearn printf(format, (unsigned int)x); 3415249bd84SSheldon Hearn else 3425249bd84SSheldon Hearn return (1); 3435249bd84SSheldon Hearn 3445249bd84SSheldon Hearn } else 3459b50d902SRodney W. Grimes printf(format, x); 3463241f274SDiomidis Spinellis if (!last) 3479b50d902SRodney W. Grimes fputs(sepstring, stdout); 3485249bd84SSheldon Hearn 3495249bd84SSheldon Hearn return (0); 3509b50d902SRodney W. Grimes } 3519b50d902SRodney W. Grimes 3527bd7ad50SPhilippe Charnier static void 353cf0def93SJuli Mallett usage(void) 3549b50d902SRodney W. Grimes { 3557bd7ad50SPhilippe Charnier fprintf(stderr, "%s\n%s\n", 3567bd7ad50SPhilippe Charnier "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]", 3577bd7ad50SPhilippe Charnier " [reps [begin [end [s]]]]"); 3589b50d902SRodney W. Grimes exit(1); 3599b50d902SRodney W. Grimes } 3609b50d902SRodney W. Grimes 3613241f274SDiomidis Spinellis /* 3623241f274SDiomidis Spinellis * Return the number of digits following the number's decimal point. 3633241f274SDiomidis Spinellis * Return 0 if no decimal point is found. 3643241f274SDiomidis Spinellis */ 3653241f274SDiomidis Spinellis static int 3663241f274SDiomidis Spinellis getprec(const char *str) 3679b50d902SRodney W. Grimes { 3683241f274SDiomidis Spinellis const char *p; 3693241f274SDiomidis Spinellis const char *q; 3709b50d902SRodney W. Grimes 371edd7b267SDima Dorfman for (p = str; *p; p++) 3729b50d902SRodney W. Grimes if (*p == '.') 3739b50d902SRodney W. Grimes break; 3749b50d902SRodney W. Grimes if (!*p) 3759b50d902SRodney W. Grimes return (0); 3769b50d902SRodney W. Grimes for (q = ++p; *p; p++) 377911a3ff9STim J. Robbins if (!isdigit((unsigned char)*p)) 3789b50d902SRodney W. Grimes break; 3799b50d902SRodney W. Grimes return (p - q); 3809b50d902SRodney W. Grimes } 3819b50d902SRodney W. Grimes 3823241f274SDiomidis Spinellis /* 3833241f274SDiomidis Spinellis * Set format, intdata, chardata, longdata, and nosign 3843241f274SDiomidis Spinellis * based on the command line arguments. 3853241f274SDiomidis Spinellis */ 3863241f274SDiomidis Spinellis static void 387cf0def93SJuli Mallett getformat(void) 3889b50d902SRodney W. Grimes { 389edd7b267SDima Dorfman char *p, *p2; 3905249bd84SSheldon Hearn int dot, hash, space, sign, numbers = 0; 39115ba0427SDima Dorfman size_t sz; 3929b50d902SRodney W. Grimes 3939b50d902SRodney W. Grimes if (boring) /* no need to bother */ 3949b50d902SRodney W. Grimes return; 3959b50d902SRodney W. Grimes for (p = format; *p; p++) /* look for '%' */ 39634785a9fSDiomidis Spinellis if (*p == '%') 39734785a9fSDiomidis Spinellis if (p[1] == '%') 39834785a9fSDiomidis Spinellis p++; /* leave %% alone */ 39934785a9fSDiomidis Spinellis else 4009b50d902SRodney W. Grimes break; 40115ba0427SDima Dorfman sz = sizeof(format) - strlen(format) - 1; 40215ba0427SDima Dorfman if (!*p && !chardata) { 40315ba0427SDima Dorfman if (snprintf(p, sz, "%%.%df", prec) >= (int)sz) 40415ba0427SDima Dorfman errx(1, "-w word too long"); 40515ba0427SDima Dorfman } else if (!*p && chardata) { 40615ba0427SDima Dorfman if (strlcpy(p, "%c", sz) >= sz) 40715ba0427SDima Dorfman errx(1, "-w word too long"); 4083241f274SDiomidis Spinellis intdata = true; 40915ba0427SDima Dorfman } else if (!*(p+1)) { 41015ba0427SDima Dorfman if (sz <= 0) 41115ba0427SDima Dorfman errx(1, "-w word too long"); 4129b50d902SRodney W. Grimes strcat(format, "%"); /* cannot end in single '%' */ 41315ba0427SDima Dorfman } else { 4148db8a33bSSheldon Hearn /* 4158db8a33bSSheldon Hearn * Allow conversion format specifiers of the form 4168db8a33bSSheldon Hearn * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of 4178db8a33bSSheldon Hearn * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u} 4188db8a33bSSheldon Hearn */ 419edd7b267SDima Dorfman p2 = p++; 4208db8a33bSSheldon Hearn dot = hash = space = sign = numbers = 0; 421911a3ff9STim J. Robbins while (!isalpha((unsigned char)*p)) { 422911a3ff9STim J. Robbins if (isdigit((unsigned char)*p)) { 4238db8a33bSSheldon Hearn numbers++; 4249b50d902SRodney W. Grimes p++; 4258db8a33bSSheldon Hearn } else if ((*p == '#' && !(numbers|dot|sign|space| 4268db8a33bSSheldon Hearn hash++)) || 4278db8a33bSSheldon Hearn (*p == ' ' && !(numbers|dot|space++)) || 4288db8a33bSSheldon Hearn ((*p == '+' || *p == '-') && !(numbers|dot|sign++)) 4298db8a33bSSheldon Hearn || (*p == '.' && !(dot++))) 4308db8a33bSSheldon Hearn p++; 4318db8a33bSSheldon Hearn else 4325249bd84SSheldon Hearn goto fmt_broken; 4335249bd84SSheldon Hearn } 4345249bd84SSheldon Hearn if (*p == 'l') { 4353241f274SDiomidis Spinellis longdata = true; 4365249bd84SSheldon Hearn if (*++p == 'l') { 4375249bd84SSheldon Hearn if (p[1] != '\0') 4385249bd84SSheldon Hearn p++; 4395249bd84SSheldon Hearn goto fmt_broken; 4405249bd84SSheldon Hearn } 4418db8a33bSSheldon Hearn } 4429b50d902SRodney W. Grimes switch (*p) { 4438db8a33bSSheldon Hearn case 'o': case 'u': case 'x': case 'X': 4443241f274SDiomidis Spinellis intdata = nosign = true; 4459b50d902SRodney W. Grimes break; 4468db8a33bSSheldon Hearn case 'd': case 'i': 4473241f274SDiomidis Spinellis intdata = true; 4488db8a33bSSheldon Hearn break; 4498db8a33bSSheldon Hearn case 'D': 4505249bd84SSheldon Hearn if (!longdata) { 4513241f274SDiomidis Spinellis intdata = true; 4528db8a33bSSheldon Hearn break; 4538db8a33bSSheldon Hearn } 4548db8a33bSSheldon Hearn case 'O': case 'U': 4555249bd84SSheldon Hearn if (!longdata) { 4563241f274SDiomidis Spinellis intdata = nosign = true; 4578db8a33bSSheldon Hearn break; 4588db8a33bSSheldon Hearn } 4598db8a33bSSheldon Hearn case 'c': 4605249bd84SSheldon Hearn if (!(intdata | longdata)) { 4613241f274SDiomidis Spinellis chardata = true; 4628db8a33bSSheldon Hearn break; 4638db8a33bSSheldon Hearn } 4645249bd84SSheldon Hearn case 'h': case 'n': case 'p': case 'q': case 's': case 'L': 4658db8a33bSSheldon Hearn case '$': case '*': 4665249bd84SSheldon Hearn goto fmt_broken; 4678db8a33bSSheldon Hearn case 'f': case 'e': case 'g': case 'E': case 'G': 4685249bd84SSheldon Hearn if (!longdata) 4698db8a33bSSheldon Hearn break; 4708db8a33bSSheldon Hearn /* FALLTHROUGH */ 4719b50d902SRodney W. Grimes default: 4725249bd84SSheldon Hearn fmt_broken: 4738db8a33bSSheldon Hearn *++p = '\0'; 474edd7b267SDima Dorfman errx(1, "illegal or unsupported format '%s'", p2); 4758db8a33bSSheldon Hearn /* NOTREACHED */ 4768db8a33bSSheldon Hearn } 4778db8a33bSSheldon Hearn while (*++p) 4788db8a33bSSheldon Hearn if (*p == '%' && *(p+1) && *(p+1) != '%') 4798db8a33bSSheldon Hearn errx(1, "too many conversions"); 4808db8a33bSSheldon Hearn else if (*p == '%' && *(p+1) == '%') 4818db8a33bSSheldon Hearn p++; 4828db8a33bSSheldon Hearn else if (*p == '%' && !*(p+1)) { 4838db8a33bSSheldon Hearn strcat(format, "%"); 4849b50d902SRodney W. Grimes break; 4859b50d902SRodney W. Grimes } 4869b50d902SRodney W. Grimes } 4879b50d902SRodney W. Grimes } 488