1 /*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 #endif 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 /* 49 * jot - print sequential or random data 50 * 51 * Author: John Kunze, Office of Comp. Affairs, UCB 52 */ 53 54 #include <ctype.h> 55 #include <err.h> 56 #include <limits.h> 57 #include <stdio.h> 58 #include <stdint.h> 59 #include <stdlib.h> 60 #include <stdbool.h> 61 #include <string.h> 62 #include <time.h> 63 #include <unistd.h> 64 65 #define REPS_DEF 100 66 #define BEGIN_DEF 1 67 #define ENDER_DEF 100 68 #define STEP_DEF 1 69 70 #define HAVE_STEP 1 71 #define HAVE_ENDER 2 72 #define HAVE_BEGIN 4 73 #define HAVE_REPS 8 74 75 #define is_default(s) (strcmp((s), "-") == 0) 76 77 double begin; 78 double ender; 79 double s; 80 long reps; 81 int randomize; 82 int infinity; 83 int boring; 84 int prec; 85 int longdata; 86 int intdata; 87 int chardata; 88 int nosign; 89 int nofinalnl; 90 const char *sepstring = "\n"; 91 char format[BUFSIZ]; 92 93 void getformat(void); 94 int getprec(char *); 95 int putdata(double, long); 96 static void usage(void); 97 98 int 99 main(int argc, char **argv) 100 { 101 double x, y; 102 long i; 103 unsigned int mask = 0; 104 int n = 0; 105 int ch; 106 bool have_seed = false; 107 108 while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) 109 switch (ch) { 110 case 'r': 111 randomize = 1; 112 break; 113 case 'c': 114 chardata = 1; 115 break; 116 case 'n': 117 nofinalnl = 1; 118 break; 119 case 'b': 120 boring = 1; 121 /* FALLTHROUGH */ 122 case 'w': 123 if (strlcpy(format, optarg, sizeof(format)) >= 124 sizeof(format)) 125 errx(1, "-%c word too long", ch); 126 break; 127 case 's': 128 sepstring = optarg; 129 break; 130 case 'p': 131 prec = atoi(optarg); 132 if (prec <= 0) 133 errx(1, "bad precision value"); 134 break; 135 default: 136 usage(); 137 } 138 argc -= optind; 139 argv += optind; 140 141 switch (argc) { /* examine args right to left, falling thru cases */ 142 case 4: 143 if (!is_default(argv[3])) { 144 if (!sscanf(argv[3], "%lf", &s)) 145 errx(1, "bad s value: %s", argv[3]); 146 mask |= HAVE_STEP; 147 if (randomize) 148 have_seed = true; 149 } 150 /* FALLTHROUGH */ 151 case 3: 152 if (!is_default(argv[2])) { 153 if (!sscanf(argv[2], "%lf", &ender)) 154 ender = argv[2][strlen(argv[2])-1]; 155 mask |= HAVE_ENDER; 156 if (!prec) 157 n = getprec(argv[2]); 158 } 159 /* FALLTHROUGH */ 160 case 2: 161 if (!is_default(argv[1])) { 162 if (!sscanf(argv[1], "%lf", &begin)) 163 begin = argv[1][strlen(argv[1])-1]; 164 mask |= HAVE_BEGIN; 165 if (!prec) 166 prec = getprec(argv[1]); 167 if (n > prec) /* maximum precision */ 168 prec = n; 169 } 170 /* FALLTHROUGH */ 171 case 1: 172 if (!is_default(argv[0])) { 173 if (!sscanf(argv[0], "%ld", &reps)) 174 errx(1, "bad reps value: %s", argv[0]); 175 mask |= HAVE_REPS; 176 } 177 break; 178 case 0: 179 usage(); 180 default: 181 errx(1, "too many arguments. What do you mean by %s?", 182 argv[4]); 183 } 184 getformat(); 185 while (mask) /* 4 bit mask has 1's where last 4 args were given */ 186 switch (mask) { /* fill in the 0's by default or computation */ 187 case HAVE_STEP: 188 case HAVE_ENDER: 189 case HAVE_ENDER | HAVE_STEP: 190 case HAVE_BEGIN: 191 case HAVE_BEGIN | HAVE_STEP: 192 case HAVE_BEGIN | HAVE_ENDER: 193 reps = REPS_DEF; 194 mask |= HAVE_REPS; 195 break; 196 case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 197 if (randomize) 198 reps = REPS_DEF; 199 else if (s == 0.0) 200 reps = 0; 201 else 202 reps = (ender - begin + s) / s; 203 if (reps <= 0) 204 errx(1, "impossible stepsize"); 205 mask = 0; 206 break; 207 case HAVE_REPS: 208 case HAVE_REPS | HAVE_STEP: 209 begin = BEGIN_DEF; 210 mask |= HAVE_BEGIN; 211 break; 212 case HAVE_REPS | HAVE_ENDER: 213 s = STEP_DEF; 214 mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; 215 break; 216 case HAVE_REPS | HAVE_ENDER | HAVE_STEP: 217 if (randomize) 218 begin = BEGIN_DEF; 219 else if (reps == 0) 220 errx(1, "must specify begin if reps == 0"); 221 begin = ender - reps * s + s; 222 mask = 0; 223 break; 224 case HAVE_REPS | HAVE_BEGIN: 225 s = STEP_DEF; 226 mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; 227 break; 228 case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: 229 if (randomize) 230 ender = ENDER_DEF; 231 else 232 ender = begin + reps * s - s; 233 mask = 0; 234 break; 235 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: 236 if (reps == 0) 237 errx(1, "infinite sequences cannot be bounded"); 238 else if (reps == 1) 239 s = 0.0; 240 else 241 s = (ender - begin) / (reps - 1); 242 mask = 0; 243 break; 244 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: 245 /* if reps given and implied, */ 246 if (!randomize && s != 0.0) { 247 long t = (ender - begin + s) / s; 248 if (t <= 0) 249 errx(1, "impossible stepsize"); 250 if (t < reps) /* take lesser */ 251 reps = t; 252 } 253 mask = 0; 254 break; 255 default: 256 errx(1, "bad mask"); 257 } 258 if (reps == 0) 259 infinity = 1; 260 if (randomize) { 261 x = (ender - begin) * (ender > begin ? 1 : -1); 262 if (have_seed) 263 srandom((unsigned long)s); 264 for (i = 1; i <= reps || infinity; i++) { 265 if (have_seed) 266 y = random() / ((double)LONG_MAX + 1); 267 else 268 y = arc4random() / ((double)UINT32_MAX + 1); 269 if (putdata(y * x + begin, reps - i)) 270 errx(1, "range error in conversion"); 271 } 272 } else 273 for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 274 if (putdata(x, reps - i)) 275 errx(1, "range error in conversion"); 276 if (!nofinalnl) 277 putchar('\n'); 278 exit(0); 279 } 280 281 int 282 putdata(double x, long int notlast) 283 { 284 285 if (boring) 286 printf("%s", format); 287 else if (longdata && nosign) { 288 if (x <= (double)ULONG_MAX && x >= (double)0) 289 printf(format, (unsigned long)x); 290 else 291 return (1); 292 } else if (longdata) { 293 if (x <= (double)LONG_MAX && x >= (double)LONG_MIN) 294 printf(format, (long)x); 295 else 296 return (1); 297 } else if (chardata || (intdata && !nosign)) { 298 if (x <= (double)INT_MAX && x >= (double)INT_MIN) 299 printf(format, (int)x); 300 else 301 return (1); 302 } else if (intdata) { 303 if (x <= (double)UINT_MAX && x >= (double)0) 304 printf(format, (unsigned int)x); 305 else 306 return (1); 307 308 } else 309 printf(format, x); 310 if (notlast != 0) 311 fputs(sepstring, stdout); 312 313 return (0); 314 } 315 316 static void 317 usage(void) 318 { 319 fprintf(stderr, "%s\n%s\n", 320 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]", 321 " [reps [begin [end [s]]]]"); 322 exit(1); 323 } 324 325 int 326 getprec(char *str) 327 { 328 char *p; 329 char *q; 330 331 for (p = str; *p; p++) 332 if (*p == '.') 333 break; 334 if (!*p) 335 return (0); 336 for (q = ++p; *p; p++) 337 if (!isdigit((unsigned char)*p)) 338 break; 339 return (p - q); 340 } 341 342 void 343 getformat(void) 344 { 345 char *p, *p2; 346 int dot, hash, space, sign, numbers = 0; 347 size_t sz; 348 349 if (boring) /* no need to bother */ 350 return; 351 for (p = format; *p; p++) /* look for '%' */ 352 if (*p == '%' && *(p+1) != '%') /* leave %% alone */ 353 break; 354 sz = sizeof(format) - strlen(format) - 1; 355 if (!*p && !chardata) { 356 if (snprintf(p, sz, "%%.%df", prec) >= (int)sz) 357 errx(1, "-w word too long"); 358 } else if (!*p && chardata) { 359 if (strlcpy(p, "%c", sz) >= sz) 360 errx(1, "-w word too long"); 361 intdata = 1; 362 } else if (!*(p+1)) { 363 if (sz <= 0) 364 errx(1, "-w word too long"); 365 strcat(format, "%"); /* cannot end in single '%' */ 366 } else { 367 /* 368 * Allow conversion format specifiers of the form 369 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of 370 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u} 371 */ 372 p2 = p++; 373 dot = hash = space = sign = numbers = 0; 374 while (!isalpha((unsigned char)*p)) { 375 if (isdigit((unsigned char)*p)) { 376 numbers++; 377 p++; 378 } else if ((*p == '#' && !(numbers|dot|sign|space| 379 hash++)) || 380 (*p == ' ' && !(numbers|dot|space++)) || 381 ((*p == '+' || *p == '-') && !(numbers|dot|sign++)) 382 || (*p == '.' && !(dot++))) 383 p++; 384 else 385 goto fmt_broken; 386 } 387 if (*p == 'l') { 388 longdata = 1; 389 if (*++p == 'l') { 390 if (p[1] != '\0') 391 p++; 392 goto fmt_broken; 393 } 394 } 395 switch (*p) { 396 case 'o': case 'u': case 'x': case 'X': 397 intdata = nosign = 1; 398 break; 399 case 'd': case 'i': 400 intdata = 1; 401 break; 402 case 'D': 403 if (!longdata) { 404 intdata = 1; 405 break; 406 } 407 case 'O': case 'U': 408 if (!longdata) { 409 intdata = nosign = 1; 410 break; 411 } 412 case 'c': 413 if (!(intdata | longdata)) { 414 chardata = 1; 415 break; 416 } 417 case 'h': case 'n': case 'p': case 'q': case 's': case 'L': 418 case '$': case '*': 419 goto fmt_broken; 420 case 'f': case 'e': case 'g': case 'E': case 'G': 421 if (!longdata) 422 break; 423 /* FALLTHROUGH */ 424 default: 425 fmt_broken: 426 *++p = '\0'; 427 errx(1, "illegal or unsupported format '%s'", p2); 428 /* NOTREACHED */ 429 } 430 while (*++p) 431 if (*p == '%' && *(p+1) && *(p+1) != '%') 432 errx(1, "too many conversions"); 433 else if (*p == '%' && *(p+1) == '%') 434 p++; 435 else if (*p == '%' && !*(p+1)) { 436 strcat(format, "%"); 437 break; 438 } 439 } 440 } 441