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 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 70afe53a49SDiomidis Spinellis #define HAVE_STEP 1 71afe53a49SDiomidis Spinellis #define HAVE_ENDER 2 72afe53a49SDiomidis Spinellis #define HAVE_BEGIN 4 73afe53a49SDiomidis Spinellis #define HAVE_REPS 8 74afe53a49SDiomidis Spinellis 75fee14f30SDiomidis Spinellis #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0) 769b50d902SRodney W. Grimes 779b50d902SRodney W. Grimes double begin; 789b50d902SRodney W. Grimes double ender; 799b50d902SRodney W. Grimes double s; 809b50d902SRodney W. Grimes long reps; 819b50d902SRodney W. Grimes int randomize; 829b50d902SRodney W. Grimes int infinity; 839b50d902SRodney W. Grimes int boring; 849b50d902SRodney W. Grimes int prec; 855249bd84SSheldon Hearn int longdata; 868db8a33bSSheldon Hearn int intdata; 879b50d902SRodney W. Grimes int chardata; 888db8a33bSSheldon Hearn int nosign; 899b50d902SRodney W. Grimes int nofinalnl; 90edd7b267SDima Dorfman const char *sepstring = "\n"; 919b50d902SRodney W. Grimes char format[BUFSIZ]; 929b50d902SRodney W. Grimes 93f1bb2cd2SWarner Losh void getformat(void); 94f1bb2cd2SWarner Losh int getprec(char *); 95f1bb2cd2SWarner Losh int putdata(double, long); 96f1bb2cd2SWarner Losh static void usage(void); 979b50d902SRodney W. Grimes 989b50d902SRodney W. Grimes int 99cf0def93SJuli Mallett main(int argc, char **argv) 1009b50d902SRodney W. Grimes { 10155f965aeSDiomidis Spinellis double x, y; 10255f965aeSDiomidis Spinellis long i; 10315ba0427SDima Dorfman unsigned int mask = 0; 10415ba0427SDima Dorfman int n = 0; 10515ba0427SDima Dorfman int ch; 106d129c68aSDiomidis Spinellis bool use_random = false; 107d129c68aSDiomidis Spinellis bool have_format = false; 108d129c68aSDiomidis Spinellis double divisor; 1099b50d902SRodney W. Grimes 11015ba0427SDima Dorfman while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) 111cf0def93SJuli Mallett switch (ch) { 1129b50d902SRodney W. Grimes case 'r': 1139b50d902SRodney W. Grimes randomize = 1; 1149b50d902SRodney W. Grimes break; 1159b50d902SRodney W. Grimes case 'c': 1169b50d902SRodney W. Grimes chardata = 1; 1179b50d902SRodney W. Grimes break; 1189b50d902SRodney W. Grimes case 'n': 1199b50d902SRodney W. Grimes nofinalnl = 1; 1209b50d902SRodney W. Grimes break; 1219b50d902SRodney W. Grimes case 'b': 1229b50d902SRodney W. Grimes boring = 1; 12315ba0427SDima Dorfman /* FALLTHROUGH */ 1249b50d902SRodney W. Grimes case 'w': 12515ba0427SDima Dorfman if (strlcpy(format, optarg, sizeof(format)) >= 12615ba0427SDima Dorfman sizeof(format)) 12715ba0427SDima Dorfman errx(1, "-%c word too long", ch); 128d129c68aSDiomidis Spinellis have_format = true; 1299b50d902SRodney W. Grimes break; 1309b50d902SRodney W. Grimes case 's': 13115ba0427SDima Dorfman sepstring = optarg; 1329b50d902SRodney W. Grimes break; 1339b50d902SRodney W. Grimes case 'p': 13415ba0427SDima Dorfman prec = atoi(optarg); 1359b50d902SRodney W. Grimes if (prec <= 0) 1367bd7ad50SPhilippe Charnier errx(1, "bad precision value"); 137d129c68aSDiomidis Spinellis have_format = true; 1389b50d902SRodney W. Grimes break; 1399b50d902SRodney W. Grimes default: 1407bd7ad50SPhilippe Charnier usage(); 1419b50d902SRodney W. Grimes } 14215ba0427SDima Dorfman argc -= optind; 14315ba0427SDima Dorfman argv += optind; 1449b50d902SRodney W. Grimes 14515ba0427SDima Dorfman switch (argc) { /* examine args right to left, falling thru cases */ 1469b50d902SRodney W. Grimes case 4: 14715ba0427SDima Dorfman if (!is_default(argv[3])) { 14815ba0427SDima Dorfman if (!sscanf(argv[3], "%lf", &s)) 14915ba0427SDima Dorfman errx(1, "bad s value: %s", argv[3]); 150afe53a49SDiomidis Spinellis mask |= HAVE_STEP; 151a26a6612SDiomidis Spinellis if (randomize) 152d129c68aSDiomidis Spinellis use_random = true; 1539b50d902SRodney W. Grimes } 154afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1559b50d902SRodney W. Grimes case 3: 15615ba0427SDima Dorfman if (!is_default(argv[2])) { 15715ba0427SDima Dorfman if (!sscanf(argv[2], "%lf", &ender)) 15815ba0427SDima Dorfman ender = argv[2][strlen(argv[2])-1]; 159afe53a49SDiomidis Spinellis mask |= HAVE_ENDER; 1609b50d902SRodney W. Grimes if (!prec) 16115ba0427SDima Dorfman n = getprec(argv[2]); 1629b50d902SRodney W. Grimes } 163afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1649b50d902SRodney W. Grimes case 2: 16515ba0427SDima Dorfman if (!is_default(argv[1])) { 16615ba0427SDima Dorfman if (!sscanf(argv[1], "%lf", &begin)) 16715ba0427SDima Dorfman begin = argv[1][strlen(argv[1])-1]; 168afe53a49SDiomidis Spinellis mask |= HAVE_BEGIN; 1699b50d902SRodney W. Grimes if (!prec) 17015ba0427SDima Dorfman prec = getprec(argv[1]); 1719b50d902SRodney W. Grimes if (n > prec) /* maximum precision */ 1729b50d902SRodney W. Grimes prec = n; 1739b50d902SRodney W. Grimes } 174afe53a49SDiomidis Spinellis /* FALLTHROUGH */ 1759b50d902SRodney W. Grimes case 1: 17615ba0427SDima Dorfman if (!is_default(argv[0])) { 17715ba0427SDima Dorfman if (!sscanf(argv[0], "%ld", &reps)) 17815ba0427SDima Dorfman errx(1, "bad reps value: %s", argv[0]); 179afe53a49SDiomidis Spinellis mask |= HAVE_REPS; 1809b50d902SRodney W. Grimes } 1819b50d902SRodney W. Grimes break; 1829b50d902SRodney W. Grimes case 0: 1837bd7ad50SPhilippe Charnier usage(); 1849b50d902SRodney W. Grimes default: 18515ba0427SDima Dorfman errx(1, "too many arguments. What do you mean by %s?", 18615ba0427SDima Dorfman argv[4]); 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes getformat(); 1899b50d902SRodney W. Grimes while (mask) /* 4 bit mask has 1's where last 4 args were given */ 1909b50d902SRodney W. Grimes switch (mask) { /* fill in the 0's by default or computation */ 191afe53a49SDiomidis Spinellis case HAVE_STEP: 192afe53a49SDiomidis Spinellis case HAVE_ENDER: 193afe53a49SDiomidis Spinellis case HAVE_ENDER | HAVE_STEP: 194afe53a49SDiomidis Spinellis case HAVE_BEGIN: 195afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_STEP: 196afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER: 1979b50d902SRodney W. Grimes reps = REPS_DEF; 198d737ec1aSDiomidis Spinellis mask |= HAVE_REPS; 1999b50d902SRodney W. Grimes break; 200afe53a49SDiomidis Spinellis case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 201d737ec1aSDiomidis Spinellis if (randomize) 2029b50d902SRodney W. Grimes reps = REPS_DEF; 203d737ec1aSDiomidis Spinellis else if (s == 0.0) 2049b50d902SRodney W. Grimes reps = 0; 205d737ec1aSDiomidis Spinellis else 2069b50d902SRodney W. Grimes reps = (ender - begin + s) / s; 2079b50d902SRodney W. Grimes if (reps <= 0) 2087bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2099b50d902SRodney W. Grimes mask = 0; 2109b50d902SRodney W. Grimes break; 211afe53a49SDiomidis Spinellis case HAVE_REPS: 212afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_STEP: 2139b50d902SRodney W. Grimes begin = BEGIN_DEF; 214d737ec1aSDiomidis Spinellis mask |= HAVE_BEGIN; 2159b50d902SRodney W. Grimes break; 216afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER: 217a26a6612SDiomidis Spinellis s = STEP_DEF; 218afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; 2199b50d902SRodney W. Grimes break; 220afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_ENDER | HAVE_STEP: 2219b50d902SRodney W. Grimes if (randomize) 2229b50d902SRodney W. Grimes begin = BEGIN_DEF; 2239b50d902SRodney W. Grimes else if (reps == 0) 2247bd7ad50SPhilippe Charnier errx(1, "must specify begin if reps == 0"); 2259b50d902SRodney W. Grimes begin = ender - reps * s + s; 2269b50d902SRodney W. Grimes mask = 0; 2279b50d902SRodney W. Grimes break; 228afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN: 229a26a6612SDiomidis Spinellis s = STEP_DEF; 230afe53a49SDiomidis Spinellis mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; 2319b50d902SRodney W. Grimes break; 232afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: 2339b50d902SRodney W. Grimes if (randomize) 2349b50d902SRodney W. Grimes ender = ENDER_DEF; 2359b50d902SRodney W. Grimes else 2369b50d902SRodney W. Grimes ender = begin + reps * s - s; 2379b50d902SRodney W. Grimes mask = 0; 2389b50d902SRodney W. Grimes break; 239afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: 240a26a6612SDiomidis Spinellis if (reps == 0) 2417bd7ad50SPhilippe Charnier errx(1, "infinite sequences cannot be bounded"); 2429b50d902SRodney W. Grimes else if (reps == 1) 2439b50d902SRodney W. Grimes s = 0.0; 2449b50d902SRodney W. Grimes else 2459b50d902SRodney W. Grimes s = (ender - begin) / (reps - 1); 2469b50d902SRodney W. Grimes mask = 0; 2479b50d902SRodney W. Grimes break; 248afe53a49SDiomidis Spinellis case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 249afe53a49SDiomidis Spinellis /* if reps given and implied, */ 2509b50d902SRodney W. Grimes if (!randomize && s != 0.0) { 2519b50d902SRodney W. Grimes long t = (ender - begin + s) / s; 2529b50d902SRodney W. Grimes if (t <= 0) 2537bd7ad50SPhilippe Charnier errx(1, "impossible stepsize"); 2549b50d902SRodney W. Grimes if (t < reps) /* take lesser */ 2559b50d902SRodney W. Grimes reps = t; 2569b50d902SRodney W. Grimes } 2579b50d902SRodney W. Grimes mask = 0; 2589b50d902SRodney W. Grimes break; 2599b50d902SRodney W. Grimes default: 2607bd7ad50SPhilippe Charnier errx(1, "bad mask"); 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes if (reps == 0) 2639b50d902SRodney W. Grimes infinity = 1; 26415ba0427SDima Dorfman if (randomize) { 265d129c68aSDiomidis Spinellis if (use_random) { 266a26a6612SDiomidis Spinellis srandom((unsigned long)s); 267d129c68aSDiomidis Spinellis divisor = (double)INT32_MAX + 1; 268d129c68aSDiomidis Spinellis } else 269d129c68aSDiomidis Spinellis divisor = (double)UINT32_MAX + 1; 270d129c68aSDiomidis Spinellis 271d129c68aSDiomidis Spinellis /* 272d129c68aSDiomidis Spinellis * Attempt to DWIM when the user has specified an 273d129c68aSDiomidis Spinellis * integer range within that of the random number 274d129c68aSDiomidis Spinellis * generator: distribute the numbers equally in 275d129c68aSDiomidis Spinellis * the range [begin .. ender]. Jot's default %.0f 276d129c68aSDiomidis Spinellis * format would make the appearance of the first and 277d129c68aSDiomidis Spinellis * last specified value half as likely as the rest. 278d129c68aSDiomidis Spinellis */ 279d129c68aSDiomidis Spinellis if (!have_format && prec == 0 && 280d129c68aSDiomidis Spinellis begin >= 0 && begin < divisor && 281d129c68aSDiomidis Spinellis ender >= 0 && ender < divisor) { 282d129c68aSDiomidis Spinellis ender += 1; 283d129c68aSDiomidis Spinellis nosign = 1; 284d129c68aSDiomidis Spinellis intdata = 1; 285d129c68aSDiomidis Spinellis (void)strlcpy(format, 286d129c68aSDiomidis Spinellis chardata ? "%c" : "%u", sizeof(format)); 287d129c68aSDiomidis Spinellis } 288d129c68aSDiomidis Spinellis x = (ender - begin) * (ender > begin ? 1 : -1); 28955f965aeSDiomidis Spinellis for (i = 1; i <= reps || infinity; i++) { 290d129c68aSDiomidis Spinellis if (use_random) 291d129c68aSDiomidis Spinellis y = random() / divisor; 292a26a6612SDiomidis Spinellis else 293d129c68aSDiomidis Spinellis y = arc4random() / divisor; 29455f965aeSDiomidis Spinellis if (putdata(y * x + begin, reps - i)) 29515ba0427SDima Dorfman errx(1, "range error in conversion"); 29615ba0427SDima Dorfman } 29715ba0427SDima Dorfman } else 29855f965aeSDiomidis Spinellis for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 29955f965aeSDiomidis Spinellis if (putdata(x, reps - i)) 30015ba0427SDima Dorfman errx(1, "range error in conversion"); 30115ba0427SDima Dorfman if (!nofinalnl) 30215ba0427SDima Dorfman putchar('\n'); 30315ba0427SDima Dorfman exit(0); 3049b50d902SRodney W. Grimes } 3059b50d902SRodney W. Grimes 3065249bd84SSheldon Hearn int 307cf0def93SJuli Mallett putdata(double x, long int notlast) 3089b50d902SRodney W. Grimes { 3099b50d902SRodney W. Grimes 3105249bd84SSheldon Hearn if (boring) 311612740bdSKris Kennaway printf("%s", format); 3125249bd84SSheldon Hearn else if (longdata && nosign) { 3135249bd84SSheldon Hearn if (x <= (double)ULONG_MAX && x >= (double)0) 3148db8a33bSSheldon Hearn printf(format, (unsigned long)x); 3155249bd84SSheldon Hearn else 3165249bd84SSheldon Hearn return (1); 3175249bd84SSheldon Hearn } else if (longdata) { 3185249bd84SSheldon Hearn if (x <= (double)LONG_MAX && x >= (double)LONG_MIN) 3198db8a33bSSheldon Hearn printf(format, (long)x); 3205249bd84SSheldon Hearn else 3215249bd84SSheldon Hearn return (1); 3225249bd84SSheldon Hearn } else if (chardata || (intdata && !nosign)) { 3235249bd84SSheldon Hearn if (x <= (double)INT_MAX && x >= (double)INT_MIN) 3245249bd84SSheldon Hearn printf(format, (int)x); 3255249bd84SSheldon Hearn else 3265249bd84SSheldon Hearn return (1); 3275249bd84SSheldon Hearn } else if (intdata) { 3285249bd84SSheldon Hearn if (x <= (double)UINT_MAX && x >= (double)0) 3295249bd84SSheldon Hearn printf(format, (unsigned int)x); 3305249bd84SSheldon Hearn else 3315249bd84SSheldon Hearn return (1); 3325249bd84SSheldon Hearn 3335249bd84SSheldon Hearn } else 3349b50d902SRodney W. Grimes printf(format, x); 3359b50d902SRodney W. Grimes if (notlast != 0) 3369b50d902SRodney W. Grimes fputs(sepstring, stdout); 3375249bd84SSheldon Hearn 3385249bd84SSheldon Hearn return (0); 3399b50d902SRodney W. Grimes } 3409b50d902SRodney W. Grimes 3417bd7ad50SPhilippe Charnier static void 342cf0def93SJuli Mallett usage(void) 3439b50d902SRodney W. Grimes { 3447bd7ad50SPhilippe Charnier fprintf(stderr, "%s\n%s\n", 3457bd7ad50SPhilippe Charnier "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]", 3467bd7ad50SPhilippe Charnier " [reps [begin [end [s]]]]"); 3479b50d902SRodney W. Grimes exit(1); 3489b50d902SRodney W. Grimes } 3499b50d902SRodney W. Grimes 3509b50d902SRodney W. Grimes int 351cf0def93SJuli Mallett getprec(char *str) 3529b50d902SRodney W. Grimes { 35315ba0427SDima Dorfman char *p; 35415ba0427SDima Dorfman char *q; 3559b50d902SRodney W. Grimes 356edd7b267SDima Dorfman for (p = str; *p; p++) 3579b50d902SRodney W. Grimes if (*p == '.') 3589b50d902SRodney W. Grimes break; 3599b50d902SRodney W. Grimes if (!*p) 3609b50d902SRodney W. Grimes return (0); 3619b50d902SRodney W. Grimes for (q = ++p; *p; p++) 362911a3ff9STim J. Robbins if (!isdigit((unsigned char)*p)) 3639b50d902SRodney W. Grimes break; 3649b50d902SRodney W. Grimes return (p - q); 3659b50d902SRodney W. Grimes } 3669b50d902SRodney W. Grimes 3679b50d902SRodney W. Grimes void 368cf0def93SJuli Mallett getformat(void) 3699b50d902SRodney W. Grimes { 370edd7b267SDima Dorfman char *p, *p2; 3715249bd84SSheldon Hearn int dot, hash, space, sign, numbers = 0; 37215ba0427SDima Dorfman size_t sz; 3739b50d902SRodney W. Grimes 3749b50d902SRodney W. Grimes if (boring) /* no need to bother */ 3759b50d902SRodney W. Grimes return; 3769b50d902SRodney W. Grimes for (p = format; *p; p++) /* look for '%' */ 3779b50d902SRodney W. Grimes if (*p == '%' && *(p+1) != '%') /* leave %% alone */ 3789b50d902SRodney W. Grimes break; 37915ba0427SDima Dorfman sz = sizeof(format) - strlen(format) - 1; 38015ba0427SDima Dorfman if (!*p && !chardata) { 38115ba0427SDima Dorfman if (snprintf(p, sz, "%%.%df", prec) >= (int)sz) 38215ba0427SDima Dorfman errx(1, "-w word too long"); 38315ba0427SDima Dorfman } else if (!*p && chardata) { 38415ba0427SDima Dorfman if (strlcpy(p, "%c", sz) >= sz) 38515ba0427SDima Dorfman errx(1, "-w word too long"); 3868db8a33bSSheldon Hearn intdata = 1; 38715ba0427SDima Dorfman } else if (!*(p+1)) { 38815ba0427SDima Dorfman if (sz <= 0) 38915ba0427SDima Dorfman errx(1, "-w word too long"); 3909b50d902SRodney W. Grimes strcat(format, "%"); /* cannot end in single '%' */ 39115ba0427SDima Dorfman } else { 3928db8a33bSSheldon Hearn /* 3938db8a33bSSheldon Hearn * Allow conversion format specifiers of the form 3948db8a33bSSheldon Hearn * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of 3958db8a33bSSheldon Hearn * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u} 3968db8a33bSSheldon Hearn */ 397edd7b267SDima Dorfman p2 = p++; 3988db8a33bSSheldon Hearn dot = hash = space = sign = numbers = 0; 399911a3ff9STim J. Robbins while (!isalpha((unsigned char)*p)) { 400911a3ff9STim J. Robbins if (isdigit((unsigned char)*p)) { 4018db8a33bSSheldon Hearn numbers++; 4029b50d902SRodney W. Grimes p++; 4038db8a33bSSheldon Hearn } else if ((*p == '#' && !(numbers|dot|sign|space| 4048db8a33bSSheldon Hearn hash++)) || 4058db8a33bSSheldon Hearn (*p == ' ' && !(numbers|dot|space++)) || 4068db8a33bSSheldon Hearn ((*p == '+' || *p == '-') && !(numbers|dot|sign++)) 4078db8a33bSSheldon Hearn || (*p == '.' && !(dot++))) 4088db8a33bSSheldon Hearn p++; 4098db8a33bSSheldon Hearn else 4105249bd84SSheldon Hearn goto fmt_broken; 4115249bd84SSheldon Hearn } 4125249bd84SSheldon Hearn if (*p == 'l') { 4135249bd84SSheldon Hearn longdata = 1; 4145249bd84SSheldon Hearn if (*++p == 'l') { 4155249bd84SSheldon Hearn if (p[1] != '\0') 4165249bd84SSheldon Hearn p++; 4175249bd84SSheldon Hearn goto fmt_broken; 4185249bd84SSheldon Hearn } 4198db8a33bSSheldon Hearn } 4209b50d902SRodney W. Grimes switch (*p) { 4218db8a33bSSheldon Hearn case 'o': case 'u': case 'x': case 'X': 4228db8a33bSSheldon Hearn intdata = nosign = 1; 4239b50d902SRodney W. Grimes break; 4248db8a33bSSheldon Hearn case 'd': case 'i': 4258db8a33bSSheldon Hearn intdata = 1; 4268db8a33bSSheldon Hearn break; 4278db8a33bSSheldon Hearn case 'D': 4285249bd84SSheldon Hearn if (!longdata) { 4298db8a33bSSheldon Hearn intdata = 1; 4308db8a33bSSheldon Hearn break; 4318db8a33bSSheldon Hearn } 4328db8a33bSSheldon Hearn case 'O': case 'U': 4335249bd84SSheldon Hearn if (!longdata) { 4348db8a33bSSheldon Hearn intdata = nosign = 1; 4358db8a33bSSheldon Hearn break; 4368db8a33bSSheldon Hearn } 4378db8a33bSSheldon Hearn case 'c': 4385249bd84SSheldon Hearn if (!(intdata | longdata)) { 4398db8a33bSSheldon Hearn chardata = 1; 4408db8a33bSSheldon Hearn break; 4418db8a33bSSheldon Hearn } 4425249bd84SSheldon Hearn case 'h': case 'n': case 'p': case 'q': case 's': case 'L': 4438db8a33bSSheldon Hearn case '$': case '*': 4445249bd84SSheldon Hearn goto fmt_broken; 4458db8a33bSSheldon Hearn case 'f': case 'e': case 'g': case 'E': case 'G': 4465249bd84SSheldon Hearn if (!longdata) 4478db8a33bSSheldon Hearn break; 4488db8a33bSSheldon Hearn /* FALLTHROUGH */ 4499b50d902SRodney W. Grimes default: 4505249bd84SSheldon Hearn fmt_broken: 4518db8a33bSSheldon Hearn *++p = '\0'; 452edd7b267SDima Dorfman errx(1, "illegal or unsupported format '%s'", p2); 4538db8a33bSSheldon Hearn /* NOTREACHED */ 4548db8a33bSSheldon Hearn } 4558db8a33bSSheldon Hearn while (*++p) 4568db8a33bSSheldon Hearn if (*p == '%' && *(p+1) && *(p+1) != '%') 4578db8a33bSSheldon Hearn errx(1, "too many conversions"); 4588db8a33bSSheldon Hearn else if (*p == '%' && *(p+1) == '%') 4598db8a33bSSheldon Hearn p++; 4608db8a33bSSheldon Hearn else if (*p == '%' && !*(p+1)) { 4618db8a33bSSheldon Hearn strcat(format, "%"); 4629b50d902SRodney W. Grimes break; 4639b50d902SRodney W. Grimes } 4649b50d902SRodney W. Grimes } 4659b50d902SRodney W. Grimes } 466