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