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