jot.c (55f965ae6a4d2a48110743c99952a18a811d3628) | jot.c (a26a66124e2aab78e90cbd62c66518d7e4a6a3db) |
---|---|
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 --- 43 unchanged lines hidden (view full) --- 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> | 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 --- 43 unchanged lines hidden (view full) --- 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> |
|
60#include <string.h> 61#include <time.h> 62#include <unistd.h> 63 64#define REPS_DEF 100 65#define BEGIN_DEF 1 66#define ENDER_DEF 100 67#define STEP_DEF 1 --- 29 unchanged lines hidden (view full) --- 97int 98main(int argc, char **argv) 99{ 100 double x, y; 101 long i; 102 unsigned int mask = 0; 103 int n = 0; 104 int ch; | 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 --- 29 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; |
|
105 106 while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) 107 switch (ch) { 108 case 'r': 109 randomize = 1; 110 break; 111 case 'c': 112 chardata = 1; --- 24 unchanged lines hidden (view full) --- 137 argv += optind; 138 139 switch (argc) { /* examine args right to left, falling thru cases */ 140 case 4: 141 if (!is_default(argv[3])) { 142 if (!sscanf(argv[3], "%lf", &s)) 143 errx(1, "bad s value: %s", argv[3]); 144 mask |= HAVE_STEP; | 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; --- 24 unchanged lines hidden (view full) --- 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; |
|
145 } 146 /* FALLTHROUGH */ 147 case 3: 148 if (!is_default(argv[2])) { 149 if (!sscanf(argv[2], "%lf", &ender)) 150 ender = argv[2][strlen(argv[2])-1]; 151 mask |= HAVE_ENDER; 152 if (!prec) --- 48 unchanged lines hidden (view full) --- 201 mask = 0; 202 break; 203 case HAVE_REPS: 204 case HAVE_REPS | HAVE_STEP: 205 begin = BEGIN_DEF; 206 mask |= HAVE_BEGIN; 207 break; 208 case HAVE_REPS | HAVE_ENDER: | 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) --- 48 unchanged lines hidden (view full) --- 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: |
209 s = (randomize ? time(NULL) : STEP_DEF); | 213 s = STEP_DEF; |
210 mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; 211 break; 212 case HAVE_REPS | HAVE_ENDER | HAVE_STEP: 213 if (randomize) 214 begin = BEGIN_DEF; 215 else if (reps == 0) 216 errx(1, "must specify begin if reps == 0"); 217 begin = ender - reps * s + s; 218 mask = 0; 219 break; 220 case HAVE_REPS | HAVE_BEGIN: | 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: |
221 s = (randomize ? -1.0 : STEP_DEF); | 225 s = STEP_DEF; |
222 mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; 223 break; 224 case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: 225 if (randomize) 226 ender = ENDER_DEF; 227 else 228 ender = begin + reps * s - s; 229 mask = 0; 230 break; 231 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: | 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: |
232 if (randomize) 233 s = -1.0; 234 else if (reps == 0) | 236 if (reps == 0) |
235 errx(1, "infinite sequences cannot be bounded"); 236 else if (reps == 1) 237 s = 0.0; 238 else 239 s = (ender - begin) / (reps - 1); 240 mask = 0; 241 break; 242 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP: --- 9 unchanged lines hidden (view full) --- 252 break; 253 default: 254 errx(1, "bad mask"); 255 } 256 if (reps == 0) 257 infinity = 1; 258 if (randomize) { 259 x = (ender - begin) * (ender > begin ? 1 : -1); | 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: --- 9 unchanged lines hidden (view full) --- 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); |
|
260 for (i = 1; i <= reps || infinity; i++) { | 264 for (i = 1; i <= reps || infinity; i++) { |
261 y = arc4random() / ((double)UINT32_MAX + 1); | 265 if (have_seed) 266 y = random() / ((double)LONG_MAX + 1); 267 else 268 y = arc4random() / ((double)UINT32_MAX + 1); |
262 if (putdata(y * x + begin, reps - i)) 263 errx(1, "range error in conversion"); 264 } 265 } else 266 for (i = 1, x = begin; i <= reps || infinity; i++, x += s) 267 if (putdata(x, reps - i)) 268 errx(1, "range error in conversion"); 269 if (!nofinalnl) --- 164 unchanged lines hidden --- | 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 --- |