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 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 static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 /* 45 * jot - print sequential or random data 46 * 47 * Author: John Kunze, Office of Comp. Affairs, UCB 48 */ 49 50 #include <ctype.h> 51 #include <limits.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <unistd.h> 57 58 #define REPS_DEF 100 59 #define BEGIN_DEF 1 60 #define ENDER_DEF 100 61 #define STEP_DEF 1 62 63 #define isdefault(s) (strcmp((s), "-") == 0) 64 65 double begin; 66 double ender; 67 double s; 68 long reps; 69 int randomize; 70 int infinity; 71 int boring; 72 int prec; 73 int dox; 74 int chardata; 75 int nofinalnl; 76 char *sepstring = "\n"; 77 char format[BUFSIZ]; 78 79 void error __P((char *, char *)); 80 void getargs __P((int, char *[])); 81 void getformat __P((void)); 82 int getprec __P((char *)); 83 void putdata __P((double, long)); 84 85 int 86 main(argc, argv) 87 int argc; 88 char *argv[]; 89 { 90 double xd, yd; 91 long id; 92 register double *x = &xd; 93 register double *y = &yd; 94 register long *i = &id; 95 96 getargs(argc, argv); 97 if (randomize) { 98 *x = (ender - begin) * (ender > begin ? 1 : -1); 99 if (s == -1.0) { 100 if (srandomdev() < 0) 101 srandom(time(NULL) ^ getpid()); 102 } else 103 srandom((unsigned long) s); 104 for (*i = 1; *i <= reps || infinity; (*i)++) { 105 *y = (double) random() / LONG_MAX; 106 putdata(*y * *x + begin, reps - *i); 107 } 108 } 109 else 110 for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s) 111 putdata(*x, reps - *i); 112 if (!nofinalnl) 113 putchar('\n'); 114 exit(0); 115 } 116 117 void 118 getargs(ac, av) 119 int ac; 120 char *av[]; 121 { 122 register unsigned int mask = 0; 123 register int n = 0; 124 125 while (--ac && **++av == '-' && !isdefault(*av)) 126 switch ((*av)[1]) { 127 case 'r': 128 randomize = 1; 129 break; 130 case 'c': 131 chardata = 1; 132 break; 133 case 'n': 134 nofinalnl = 1; 135 break; 136 case 'b': 137 boring = 1; 138 case 'w': 139 if ((*av)[2]) 140 strcpy(format, *av + 2); 141 else if (!--ac) 142 error("Need context word after -w or -b", ""); 143 else 144 strcpy(format, *++av); 145 break; 146 case 's': 147 if ((*av)[2]) 148 sepstring = *av + 2; 149 else if (!--ac) 150 error("Need string after -s", ""); 151 else 152 sepstring = *++av; 153 break; 154 case 'p': 155 if ((*av)[2]) 156 prec = atoi(*av + 2); 157 else if (!--ac) 158 error("Need number after -p", ""); 159 else 160 prec = atoi(*++av); 161 if (prec <= 0) 162 error("Bad precision value", ""); 163 break; 164 default: 165 error("Unknown option %s", *av); 166 } 167 168 switch (ac) { /* examine args right to left, falling thru cases */ 169 case 4: 170 if (!isdefault(av[3])) { 171 if (!sscanf(av[3], "%lf", &s)) 172 error("Bad s value: %s", av[3]); 173 mask |= 01; 174 } 175 case 3: 176 if (!isdefault(av[2])) { 177 if (!sscanf(av[2], "%lf", &ender)) 178 ender = av[2][strlen(av[2])-1]; 179 mask |= 02; 180 if (!prec) 181 n = getprec(av[2]); 182 } 183 case 2: 184 if (!isdefault(av[1])) { 185 if (!sscanf(av[1], "%lf", &begin)) 186 begin = av[1][strlen(av[1])-1]; 187 mask |= 04; 188 if (!prec) 189 prec = getprec(av[1]); 190 if (n > prec) /* maximum precision */ 191 prec = n; 192 } 193 case 1: 194 if (!isdefault(av[0])) { 195 if (!sscanf(av[0], "%ld", &reps)) 196 error("Bad reps value: %s", av[0]); 197 mask |= 010; 198 } 199 break; 200 case 0: 201 error("jot - print sequential or random data", ""); 202 default: 203 error("Too many arguments. What do you mean by %s?", av[4]); 204 } 205 getformat(); 206 while (mask) /* 4 bit mask has 1's where last 4 args were given */ 207 switch (mask) { /* fill in the 0's by default or computation */ 208 case 001: 209 reps = REPS_DEF; 210 mask = 011; 211 break; 212 case 002: 213 reps = REPS_DEF; 214 mask = 012; 215 break; 216 case 003: 217 reps = REPS_DEF; 218 mask = 013; 219 break; 220 case 004: 221 reps = REPS_DEF; 222 mask = 014; 223 break; 224 case 005: 225 reps = REPS_DEF; 226 mask = 015; 227 break; 228 case 006: 229 reps = REPS_DEF; 230 mask = 016; 231 break; 232 case 007: 233 if (randomize) { 234 reps = REPS_DEF; 235 mask = 0; 236 break; 237 } 238 if (s == 0.0) { 239 reps = 0; 240 mask = 0; 241 break; 242 } 243 reps = (ender - begin + s) / s; 244 if (reps <= 0) 245 error("Impossible stepsize", ""); 246 mask = 0; 247 break; 248 case 010: 249 begin = BEGIN_DEF; 250 mask = 014; 251 break; 252 case 011: 253 begin = BEGIN_DEF; 254 mask = 015; 255 break; 256 case 012: 257 s = (randomize ? -1.0 : STEP_DEF); 258 mask = 013; 259 break; 260 case 013: 261 if (randomize) 262 begin = BEGIN_DEF; 263 else if (reps == 0) 264 error("Must specify begin if reps == 0", ""); 265 else 266 begin = ender - reps * s + s; 267 mask = 0; 268 break; 269 case 014: 270 s = (randomize ? -1.0 : STEP_DEF); 271 mask = 015; 272 break; 273 case 015: 274 if (randomize) 275 ender = ENDER_DEF; 276 else 277 ender = begin + reps * s - s; 278 mask = 0; 279 break; 280 case 016: 281 if (randomize) 282 s = -1.0; 283 else if (reps == 0) 284 error("Infinite sequences cannot be bounded", 285 ""); 286 else if (reps == 1) 287 s = 0.0; 288 else 289 s = (ender - begin) / (reps - 1); 290 mask = 0; 291 break; 292 case 017: /* if reps given and implied, */ 293 if (!randomize && s != 0.0) { 294 long t = (ender - begin + s) / s; 295 if (t <= 0) 296 error("Impossible stepsize", ""); 297 if (t < reps) /* take lesser */ 298 reps = t; 299 } 300 mask = 0; 301 break; 302 default: 303 error("Bad mask", ""); 304 } 305 if (reps == 0) 306 infinity = 1; 307 } 308 309 void 310 putdata(x, notlast) 311 double x; 312 long notlast; 313 { 314 long d = x; 315 register long *dp = &d; 316 317 if (boring) /* repeated word */ 318 printf(format); 319 else if (dox) /* scalar */ 320 printf(format, *dp); 321 else /* real */ 322 printf(format, x); 323 if (notlast != 0) 324 fputs(sepstring, stdout); 325 } 326 327 void 328 error(msg, s) 329 char *msg, *s; 330 { 331 fprintf(stderr, "jot: "); 332 fprintf(stderr, msg, s); 333 fprintf(stderr, 334 "\nusage: jot [ options ] [ reps [ begin [ end [ s ] ] ] ]\n"); 335 if (strncmp("jot - ", msg, 6) == 0) 336 fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s\t%s\t%s", 337 "-r random data\n", 338 "-c character data\n", 339 "-n no final newline\n", 340 "-b word repeated word\n", 341 "-w word context word\n", 342 "-s string data separator\n", 343 "-p precision number of characters\n"); 344 exit(1); 345 } 346 347 int 348 getprec(s) 349 char *s; 350 { 351 register char *p; 352 register char *q; 353 354 for (p = s; *p; p++) 355 if (*p == '.') 356 break; 357 if (!*p) 358 return (0); 359 for (q = ++p; *p; p++) 360 if (!isdigit(*p)) 361 break; 362 return (p - q); 363 } 364 365 void 366 getformat() 367 { 368 register char *p; 369 370 if (boring) /* no need to bother */ 371 return; 372 for (p = format; *p; p++) /* look for '%' */ 373 if (*p == '%' && *(p+1) != '%') /* leave %% alone */ 374 break; 375 if (!*p && !chardata) 376 sprintf(p, "%%.%df", prec); 377 else if (!*p && chardata) { 378 strcpy(p, "%c"); 379 dox = 1; 380 } 381 else if (!*(p+1)) 382 strcat(format, "%"); /* cannot end in single '%' */ 383 else { 384 while (!isalpha(*p)) 385 p++; 386 switch (*p) { 387 case 'f': case 'e': case 'g': case '%': 388 break; 389 case 's': 390 error("Cannot convert numeric data to strings", ""); 391 break; 392 /* case 'd': case 'o': case 'x': case 'D': case 'O': case 'X': 393 case 'c': case 'u': */ 394 default: 395 dox = 1; 396 break; 397 } 398 } 399 } 400