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