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