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