jot.c (fee14f30e9ba78d00e6259f720eace5498916a4a) | jot.c (d129c68a14d852e62e1ddcc3bcfe4e11761e2256) |
---|---|
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 --- 89 unchanged lines hidden (view full) --- 98int 99main(int argc, char **argv) 100{ 101 double x, y; 102 long i; 103 unsigned int mask = 0; 104 int n = 0; 105 int ch; | 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 --- 89 unchanged lines hidden (view full) --- 98int 99main(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; | 106 bool use_random = false; 107 bool have_format = false; 108 double divisor; |
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); | 109 110 while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) 111 switch (ch) { 112 case 'r': 113 randomize = 1; 114 break; 115 case 'c': 116 chardata = 1; 117 break; 118 case 'n': 119 nofinalnl = 1; 120 break; 121 case 'b': 122 boring = 1; 123 /* FALLTHROUGH */ 124 case 'w': 125 if (strlcpy(format, optarg, sizeof(format)) >= 126 sizeof(format)) 127 errx(1, "-%c word too long", ch); |
128 have_format = true; |
|
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"); | 129 break; 130 case 's': 131 sepstring = optarg; 132 break; 133 case 'p': 134 prec = atoi(optarg); 135 if (prec <= 0) 136 errx(1, "bad precision value"); |
137 have_format = true; |
|
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) | 138 break; 139 default: 140 usage(); 141 } 142 argc -= optind; 143 argv += optind; 144 145 switch (argc) { /* examine args right to left, falling thru cases */ 146 case 4: 147 if (!is_default(argv[3])) { 148 if (!sscanf(argv[3], "%lf", &s)) 149 errx(1, "bad s value: %s", argv[3]); 150 mask |= HAVE_STEP; 151 if (randomize) |
148 have_seed = true; | 152 use_random = 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) --- 96 unchanged lines hidden (view full) --- 253 mask = 0; 254 break; 255 default: 256 errx(1, "bad mask"); 257 } 258 if (reps == 0) 259 infinity = 1; 260 if (randomize) { | 153 } 154 /* FALLTHROUGH */ 155 case 3: 156 if (!is_default(argv[2])) { 157 if (!sscanf(argv[2], "%lf", &ender)) 158 ender = argv[2][strlen(argv[2])-1]; 159 mask |= HAVE_ENDER; 160 if (!prec) --- 96 unchanged lines hidden (view full) --- 257 mask = 0; 258 break; 259 default: 260 errx(1, "bad mask"); 261 } 262 if (reps == 0) 263 infinity = 1; 264 if (randomize) { |
261 x = (ender - begin) * (ender > begin ? 1 : -1); 262 if (have_seed) | 265 if (use_random) { |
263 srandom((unsigned long)s); | 266 srandom((unsigned long)s); |
267 divisor = (double)INT32_MAX + 1; 268 } else 269 divisor = (double)UINT32_MAX + 1; 270 271 /* 272 * Attempt to DWIM when the user has specified an 273 * integer range within that of the random number 274 * generator: distribute the numbers equally in 275 * the range [begin .. ender]. Jot's default %.0f 276 * format would make the appearance of the first and 277 * last specified value half as likely as the rest. 278 */ 279 if (!have_format && prec == 0 && 280 begin >= 0 && begin < divisor && 281 ender >= 0 && ender < divisor) { 282 ender += 1; 283 nosign = 1; 284 intdata = 1; 285 (void)strlcpy(format, 286 chardata ? "%c" : "%u", sizeof(format)); 287 } 288 x = (ender - begin) * (ender > begin ? 1 : -1); |
|
264 for (i = 1; i <= reps || infinity; i++) { | 289 for (i = 1; i <= reps || infinity; i++) { |
265 if (have_seed) 266 y = random() / ((double)LONG_MAX + 1); | 290 if (use_random) 291 y = random() / divisor; |
267 else | 292 else |
268 y = arc4random() / ((double)UINT32_MAX + 1); | 293 y = arc4random() / divisor; |
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) --- 164 unchanged lines hidden --- | 294 if (putdata(y * x + begin, reps - i)) 295 errx(1, "range error in conversion"); 296 } 297 } else 298 for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 299 if (putdata(x, reps - i)) 300 errx(1, "range error in conversion"); 301 if (!nofinalnl) --- 164 unchanged lines hidden --- |