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> 62821df508SXin LI #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; 80*9c24af3dSBrian Somers static int prec = -1; 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; 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 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); 131*9c24af3dSBrian Somers 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; 162*9c24af3dSBrian Somers if (prec < 0) 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; 171*9c24af3dSBrian Somers if (prec < 0) 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(); 191*9c24af3dSBrian Somers 192*9c24af3dSBrian Somers if (prec == -1) 193*9c24af3dSBrian Somers prec = 0; 194*9c24af3dSBrian Somers 1959b50d902SRodney W. Grimes while (mask) /* 4 bit mask has 1's where last 4 args were given */ 1969b50d902SRodney W. Grimes switch (mask) { /* fill in the 0's by default or computation */ 197afe53a49SDiomidis Spinellis case HAVE_STEP: 198afe53a49SDiomidis Spinellis case HAVE_ENDER: 199afe53a49SDiomidis Spinellis case HAVE_ENDER | HAVE_STEP: 200afe53a49SDiomidis Spinellis case HAVE_BEGIN: 201afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_STEP: 2029b50d902SRodney W. Grimes reps = REPS_DEF; 203d737ec1aSDiomidis Spinellis mask |= HAVE_REPS; 2049b50d902SRodney W. Grimes break; 2057d71f06cSDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER: 2067d71f06cSDiomidis Spinellis s = ender > begin ? 1 : -1; 2077d71f06cSDiomidis Spinellis mask |= HAVE_STEP; 2087d71f06cSDiomidis Spinellis break; 209afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 210d737ec1aSDiomidis Spinellis if (randomize) 2119b50d902SRodney W. Grimes reps = REPS_DEF; 212d737ec1aSDiomidis Spinellis else if (s == 0.0) 2139b50d902SRodney W. Grimes reps = 0; 214d737ec1aSDiomidis Spinellis else 2159b50d902SRodney W. Grimes reps = (ender - begin + s) / s; 2169b50d902SRodney W. Grimes if (reps <= 0) 2177bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2189b50d902SRodney W. Grimes mask = 0; 2199b50d902SRodney W. Grimes break; 220afe53a49SDiomidis Spinellis case HAVE_REPS: 221afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_STEP: 2229b50d902SRodney W. Grimes begin = BEGIN_DEF; 223d737ec1aSDiomidis Spinellis mask |= HAVE_BEGIN; 2249b50d902SRodney W. Grimes break; 225afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER: 226a26a6612SDiomidis Spinellis s = STEP_DEF; 227afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; 2289b50d902SRodney W. Grimes break; 229afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER | HAVE_STEP: 2309b50d902SRodney W. Grimes if (randomize) 2319b50d902SRodney W. Grimes begin = BEGIN_DEF; 2329b50d902SRodney W. Grimes else if (reps == 0) 2337bd7ad50SPhilippe Charnier errx(1, "must specify begin if reps == 0"); 2349b50d902SRodney W. Grimes begin = ender - reps * s + s; 2359b50d902SRodney W. Grimes mask = 0; 2369b50d902SRodney W. Grimes break; 237afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN: 238a26a6612SDiomidis Spinellis s = STEP_DEF; 239afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; 2409b50d902SRodney W. Grimes break; 241afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: 2429b50d902SRodney W. Grimes if (randomize) 2439b50d902SRodney W. Grimes ender = ENDER_DEF; 2449b50d902SRodney W. Grimes else 2459b50d902SRodney W. Grimes ender = begin + reps * s - s; 2469b50d902SRodney W. Grimes mask = 0; 2479b50d902SRodney W. Grimes break; 248afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: 249a26a6612SDiomidis Spinellis if (reps == 0) 2507bd7ad50SPhilippe Charnier errx(1, "infinite sequences cannot be bounded"); 2519b50d902SRodney W. Grimes else if (reps == 1) 2529b50d902SRodney W. Grimes s = 0.0; 2539b50d902SRodney W. Grimes else 2549b50d902SRodney W. Grimes s = (ender - begin) / (reps - 1); 2559b50d902SRodney W. Grimes mask = 0; 2569b50d902SRodney W. Grimes break; 257afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 258afe53a49SDiomidis Spinellis /* if reps given and implied, */ 2599b50d902SRodney W. Grimes if (!randomize && s != 0.0) { 2609b50d902SRodney W. Grimes long t = (ender - begin + s) / s; 2619b50d902SRodney W. Grimes if (t <= 0) 2627bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2639b50d902SRodney W. Grimes if (t < reps) /* take lesser */ 2649b50d902SRodney W. Grimes reps = t; 2659b50d902SRodney W. Grimes } 2669b50d902SRodney W. Grimes mask = 0; 2679b50d902SRodney W. Grimes break; 2689b50d902SRodney W. Grimes default: 2697bd7ad50SPhilippe Charnier errx(1, "bad mask"); 2709b50d902SRodney W. Grimes } 2719b50d902SRodney W. Grimes if (reps == 0) 2723241f274SDiomidis Spinellis infinity = true; 27315ba0427SDima Dorfman if (randomize) { 274d129c68aSDiomidis Spinellis if (use_random) { 275a26a6612SDiomidis Spinellis srandom((unsigned long)s); 276d129c68aSDiomidis Spinellis divisor = (double)INT32_MAX + 1; 277d129c68aSDiomidis Spinellis } else 278d129c68aSDiomidis Spinellis divisor = (double)UINT32_MAX + 1; 279d129c68aSDiomidis Spinellis 280d129c68aSDiomidis Spinellis /* 281d129c68aSDiomidis Spinellis * Attempt to DWIM when the user has specified an 282d129c68aSDiomidis Spinellis * integer range within that of the random number 283d129c68aSDiomidis Spinellis * generator: distribute the numbers equally in 284d129c68aSDiomidis Spinellis * the range [begin .. ender]. Jot's default %.0f 285d129c68aSDiomidis Spinellis * format would make the appearance of the first and 286d129c68aSDiomidis Spinellis * last specified value half as likely as the rest. 287d129c68aSDiomidis Spinellis */ 288d129c68aSDiomidis Spinellis if (!have_format && prec == 0 && 289d129c68aSDiomidis Spinellis begin >= 0 && begin < divisor && 290d129c68aSDiomidis Spinellis ender >= 0 && ender < divisor) { 291*9c24af3dSBrian Somers if (begin <= ender) 292d129c68aSDiomidis Spinellis ender += 1; 293*9c24af3dSBrian Somers else 294*9c24af3dSBrian Somers begin += 1; 2953241f274SDiomidis Spinellis nosign = true; 2963241f274SDiomidis Spinellis intdata = true; 297d129c68aSDiomidis Spinellis (void)strlcpy(format, 298d129c68aSDiomidis Spinellis chardata ? "%c" : "%u", sizeof(format)); 299d129c68aSDiomidis Spinellis } 300*9c24af3dSBrian Somers x = ender - begin; 30155f965aeSDiomidis Spinellis for (i = 1; i <= reps || infinity; i++) { 302d129c68aSDiomidis Spinellis if (use_random) 303d129c68aSDiomidis Spinellis y = random() / divisor; 304a26a6612SDiomidis Spinellis else 305d129c68aSDiomidis Spinellis y = arc4random() / divisor; 3063241f274SDiomidis Spinellis if (putdata(y * x + begin, !(reps - i))) 30715ba0427SDima Dorfman errx(1, "range error in conversion"); 30815ba0427SDima Dorfman } 30915ba0427SDima Dorfman } else 31055f965aeSDiomidis Spinellis for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 3113241f274SDiomidis Spinellis if (putdata(x, !(reps - i))) 31215ba0427SDima Dorfman errx(1, "range error in conversion"); 31315ba0427SDima Dorfman if (!nofinalnl) 31415ba0427SDima Dorfman putchar('\n'); 31515ba0427SDima Dorfman exit(0); 3169b50d902SRodney W. Grimes } 3179b50d902SRodney W. Grimes 3183241f274SDiomidis Spinellis /* 3193241f274SDiomidis Spinellis * Send x to stdout using the specified format. 3203241f274SDiomidis Spinellis * Last is true if this is the set's last value. 3213241f274SDiomidis Spinellis * Return 0 if OK, or a positive number if the number passed was 3223241f274SDiomidis Spinellis * outside the range specified by the various flags. 3233241f274SDiomidis Spinellis */ 3243241f274SDiomidis Spinellis static int 3253241f274SDiomidis Spinellis putdata(double x, bool last) 3269b50d902SRodney W. Grimes { 3279b50d902SRodney W. Grimes 3285249bd84SSheldon Hearn if (boring) 329612740bdSKris Kennaway printf("%s", format); 3305249bd84SSheldon Hearn else if (longdata && nosign) { 3315249bd84SSheldon Hearn if (x <= (double)ULONG_MAX && x >= (double)0) 3328db8a33bSSheldon Hearn printf(format, (unsigned long)x); 3335249bd84SSheldon Hearn else 3345249bd84SSheldon Hearn return (1); 3355249bd84SSheldon Hearn } else if (longdata) { 3365249bd84SSheldon Hearn if (x <= (double)LONG_MAX && x >= (double)LONG_MIN) 3378db8a33bSSheldon Hearn printf(format, (long)x); 3385249bd84SSheldon Hearn else 3395249bd84SSheldon Hearn return (1); 3405249bd84SSheldon Hearn } else if (chardata || (intdata && !nosign)) { 3415249bd84SSheldon Hearn if (x <= (double)INT_MAX && x >= (double)INT_MIN) 3425249bd84SSheldon Hearn printf(format, (int)x); 3435249bd84SSheldon Hearn else 3445249bd84SSheldon Hearn return (1); 3455249bd84SSheldon Hearn } else if (intdata) { 3465249bd84SSheldon Hearn if (x <= (double)UINT_MAX && x >= (double)0) 3475249bd84SSheldon Hearn printf(format, (unsigned int)x); 3485249bd84SSheldon Hearn else 3495249bd84SSheldon Hearn return (1); 3505249bd84SSheldon Hearn 3515249bd84SSheldon Hearn } else 3529b50d902SRodney W. Grimes printf(format, x); 3533241f274SDiomidis Spinellis if (!last) 3549b50d902SRodney W. Grimes fputs(sepstring, stdout); 3555249bd84SSheldon Hearn 3565249bd84SSheldon Hearn return (0); 3579b50d902SRodney W. Grimes } 3589b50d902SRodney W. Grimes 3597bd7ad50SPhilippe Charnier static void 360cf0def93SJuli Mallett usage(void) 3619b50d902SRodney W. Grimes { 3627bd7ad50SPhilippe Charnier fprintf(stderr, "%s\n%s\n", 3637bd7ad50SPhilippe Charnier "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]", 3647bd7ad50SPhilippe Charnier " [reps [begin [end [s]]]]"); 3659b50d902SRodney W. Grimes exit(1); 3669b50d902SRodney W. Grimes } 3679b50d902SRodney W. Grimes 3683241f274SDiomidis Spinellis /* 3693241f274SDiomidis Spinellis * Return the number of digits following the number's decimal point. 3703241f274SDiomidis Spinellis * Return 0 if no decimal point is found. 3713241f274SDiomidis Spinellis */ 3723241f274SDiomidis Spinellis static int 3733241f274SDiomidis Spinellis getprec(const char *str) 3749b50d902SRodney W. Grimes { 3753241f274SDiomidis Spinellis const char *p; 3763241f274SDiomidis Spinellis const char *q; 3779b50d902SRodney W. Grimes 378edd7b267SDima Dorfman for (p = str; *p; p++) 3799b50d902SRodney W. Grimes if (*p == '.') 3809b50d902SRodney W. Grimes break; 3819b50d902SRodney W. Grimes if (!*p) 3829b50d902SRodney W. Grimes return (0); 3839b50d902SRodney W. Grimes for (q = ++p; *p; p++) 384911a3ff9STim J. Robbins if (!isdigit((unsigned char)*p)) 3859b50d902SRodney W. Grimes break; 3869b50d902SRodney W. Grimes return (p - q); 3879b50d902SRodney W. Grimes } 3889b50d902SRodney W. Grimes 3893241f274SDiomidis Spinellis /* 3903241f274SDiomidis Spinellis * Set format, intdata, chardata, longdata, and nosign 3913241f274SDiomidis Spinellis * based on the command line arguments. 3923241f274SDiomidis Spinellis */ 3933241f274SDiomidis Spinellis static void 394cf0def93SJuli Mallett getformat(void) 3959b50d902SRodney W. Grimes { 396edd7b267SDima Dorfman char *p, *p2; 3975249bd84SSheldon Hearn int dot, hash, space, sign, numbers = 0; 39815ba0427SDima Dorfman size_t sz; 3999b50d902SRodney W. Grimes 4009b50d902SRodney W. Grimes if (boring) /* no need to bother */ 4019b50d902SRodney W. Grimes return; 4029b50d902SRodney W. Grimes for (p = format; *p; p++) /* look for '%' */ 40382417e9fSXin LI if (*p == '%') { 40434785a9fSDiomidis Spinellis if (p[1] == '%') 40534785a9fSDiomidis Spinellis p++; /* leave %% alone */ 40634785a9fSDiomidis Spinellis else 4079b50d902SRodney W. Grimes break; 40882417e9fSXin LI } 40915ba0427SDima Dorfman sz = sizeof(format) - strlen(format) - 1; 41015ba0427SDima Dorfman if (!*p && !chardata) { 41115ba0427SDima Dorfman if (snprintf(p, sz, "%%.%df", prec) >= (int)sz) 41215ba0427SDima Dorfman errx(1, "-w word too long"); 41315ba0427SDima Dorfman } else if (!*p && chardata) { 41415ba0427SDima Dorfman if (strlcpy(p, "%c", sz) >= sz) 41515ba0427SDima Dorfman errx(1, "-w word too long"); 4163241f274SDiomidis Spinellis intdata = true; 41715ba0427SDima Dorfman } else if (!*(p+1)) { 41815ba0427SDima Dorfman if (sz <= 0) 41915ba0427SDima Dorfman errx(1, "-w word too long"); 4209b50d902SRodney W. Grimes strcat(format, "%"); /* cannot end in single '%' */ 42115ba0427SDima Dorfman } else { 4228db8a33bSSheldon Hearn /* 4238db8a33bSSheldon Hearn * Allow conversion format specifiers of the form 4248db8a33bSSheldon Hearn * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of 4258db8a33bSSheldon Hearn * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u} 4268db8a33bSSheldon Hearn */ 427edd7b267SDima Dorfman p2 = p++; 4288db8a33bSSheldon Hearn dot = hash = space = sign = numbers = 0; 429911a3ff9STim J. Robbins while (!isalpha((unsigned char)*p)) { 430911a3ff9STim J. Robbins if (isdigit((unsigned char)*p)) { 4318db8a33bSSheldon Hearn numbers++; 4329b50d902SRodney W. Grimes p++; 4338db8a33bSSheldon Hearn } else if ((*p == '#' && !(numbers|dot|sign|space| 4348db8a33bSSheldon Hearn hash++)) || 4358db8a33bSSheldon Hearn (*p == ' ' && !(numbers|dot|space++)) || 4368db8a33bSSheldon Hearn ((*p == '+' || *p == '-') && !(numbers|dot|sign++)) 4378db8a33bSSheldon Hearn || (*p == '.' && !(dot++))) 4388db8a33bSSheldon Hearn p++; 4398db8a33bSSheldon Hearn else 4405249bd84SSheldon Hearn goto fmt_broken; 4415249bd84SSheldon Hearn } 4425249bd84SSheldon Hearn if (*p == 'l') { 4433241f274SDiomidis Spinellis longdata = true; 4445249bd84SSheldon Hearn if (*++p == 'l') { 4455249bd84SSheldon Hearn if (p[1] != '\0') 4465249bd84SSheldon Hearn p++; 4475249bd84SSheldon Hearn goto fmt_broken; 4485249bd84SSheldon Hearn } 4498db8a33bSSheldon Hearn } 4509b50d902SRodney W. Grimes switch (*p) { 4518db8a33bSSheldon Hearn case 'o': case 'u': case 'x': case 'X': 4523241f274SDiomidis Spinellis intdata = nosign = true; 4539b50d902SRodney W. Grimes break; 4548db8a33bSSheldon Hearn case 'd': case 'i': 4553241f274SDiomidis Spinellis intdata = true; 4568db8a33bSSheldon Hearn break; 4578db8a33bSSheldon Hearn case 'D': 4585249bd84SSheldon Hearn if (!longdata) { 4593241f274SDiomidis Spinellis intdata = true; 4608db8a33bSSheldon Hearn break; 4618db8a33bSSheldon Hearn } 4628db8a33bSSheldon Hearn case 'O': case 'U': 4635249bd84SSheldon Hearn if (!longdata) { 4643241f274SDiomidis Spinellis intdata = nosign = true; 4658db8a33bSSheldon Hearn break; 4668db8a33bSSheldon Hearn } 4678db8a33bSSheldon Hearn case 'c': 4685249bd84SSheldon Hearn if (!(intdata | longdata)) { 4693241f274SDiomidis Spinellis chardata = true; 4708db8a33bSSheldon Hearn break; 4718db8a33bSSheldon Hearn } 4725249bd84SSheldon Hearn case 'h': case 'n': case 'p': case 'q': case 's': case 'L': 4738db8a33bSSheldon Hearn case '$': case '*': 4745249bd84SSheldon Hearn goto fmt_broken; 4758db8a33bSSheldon Hearn case 'f': case 'e': case 'g': case 'E': case 'G': 4765249bd84SSheldon Hearn if (!longdata) 4778db8a33bSSheldon Hearn break; 4788db8a33bSSheldon Hearn /* FALLTHROUGH */ 4799b50d902SRodney W. Grimes default: 4805249bd84SSheldon Hearn fmt_broken: 4818db8a33bSSheldon Hearn *++p = '\0'; 482edd7b267SDima Dorfman errx(1, "illegal or unsupported format '%s'", p2); 4838db8a33bSSheldon Hearn /* NOTREACHED */ 4848db8a33bSSheldon Hearn } 4858db8a33bSSheldon Hearn while (*++p) 4868db8a33bSSheldon Hearn if (*p == '%' && *(p+1) && *(p+1) != '%') 4878db8a33bSSheldon Hearn errx(1, "too many conversions"); 4888db8a33bSSheldon Hearn else if (*p == '%' && *(p+1) == '%') 4898db8a33bSSheldon Hearn p++; 4908db8a33bSSheldon Hearn else if (*p == '%' && !*(p+1)) { 491f88b45d7SDiomidis Spinellis if (strlcat(format, "%", sizeof(format)) >= 492f88b45d7SDiomidis Spinellis sizeof(format)) 493f88b45d7SDiomidis Spinellis errx(1, "-w word too long"); 4949b50d902SRodney W. Grimes break; 4959b50d902SRodney W. Grimes } 4969b50d902SRodney W. Grimes } 4979b50d902SRodney W. Grimes } 498