jot.c revision 1.21 1 1.21 dsl /* $NetBSD: jot.c,v 1.21 2008/02/29 22:43:48 dsl 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.4 lukem __COPYRIGHT("@(#) Copyright (c) 1993\n\
35 1.4 lukem The Regents of the University of California. All rights reserved.\n");
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.21 dsl __RCSID("$NetBSD: jot.c,v 1.21 2008/02/29 22:43:48 dsl 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.16 dsl double x, y;
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.17 dsl x = (ender + dox - begin) * (ender > begin ? 1 : -1);
96 1.18 dsl srandom((unsigned long) step);
97 1.21 dsl for (i = 1; i <= reps || reps == 0; i++) {
98 1.16 dsl y = (double) random() / INT_MAX;
99 1.16 dsl putdata(y * x + begin, reps - i);
100 1.16 dsl }
101 1.20 dsl } else {
102 1.21 dsl for (i = 1, x = begin; i <= reps || reps == 0; i++, x += step)
103 1.16 dsl putdata(x, reps - i);
104 1.20 dsl }
105 1.1 jtc if (!nofinalnl)
106 1.1 jtc putchar('\n');
107 1.1 jtc exit(0);
108 1.1 jtc }
109 1.1 jtc
110 1.20 dsl static void
111 1.13 perry getargs(int argc, char *argv[])
112 1.1 jtc {
113 1.21 dsl unsigned int have = 0;
114 1.21 dsl #define BEGIN 1
115 1.21 dsl #define STEP 2 /* seed if -r */
116 1.21 dsl #define REPS 4
117 1.21 dsl #define ENDER 8
118 1.21 dsl int n = 0;
119 1.21 dsl long t;
120 1.20 dsl char *ep;
121 1.1 jtc
122 1.20 dsl for (;;) {
123 1.20 dsl switch (getopt(argc, argv, "b:cnp:rs:w:")) {
124 1.20 dsl default:
125 1.20 dsl usage();
126 1.20 dsl case -1:
127 1.1 jtc break;
128 1.1 jtc case 'c':
129 1.1 jtc chardata = 1;
130 1.20 dsl continue;
131 1.1 jtc case 'n':
132 1.1 jtc nofinalnl = 1;
133 1.20 dsl continue;
134 1.20 dsl case 'p':
135 1.20 dsl prec = strtol(optarg, &ep, 0);
136 1.20 dsl if (*ep != 0 || prec < 0)
137 1.20 dsl errx(EXIT_FAILURE, "Bad precision value");
138 1.20 dsl continue;
139 1.20 dsl case 'r':
140 1.20 dsl randomize = 1;
141 1.20 dsl continue;
142 1.20 dsl case 's':
143 1.20 dsl sepstring = optarg;
144 1.20 dsl continue;
145 1.1 jtc case 'b':
146 1.1 jtc boring = 1;
147 1.20 dsl /* FALLTHROUGH */
148 1.1 jtc case 'w':
149 1.20 dsl strlcpy(format, optarg, sizeof(format));
150 1.20 dsl continue;
151 1.1 jtc }
152 1.20 dsl break;
153 1.16 dsl }
154 1.20 dsl argc -= optind;
155 1.20 dsl argv += optind;
156 1.1 jtc
157 1.7 jdolecek switch (argc) { /* examine args right to left, falling thru cases */
158 1.1 jtc case 4:
159 1.7 jdolecek if (!is_default(argv[3])) {
160 1.21 dsl step = strtod(argv[3], &ep);
161 1.21 dsl if (*ep != 0)
162 1.20 dsl errx(EXIT_FAILURE, "Bad step value: %s",
163 1.20 dsl argv[3]);
164 1.21 dsl have |= STEP;
165 1.1 jtc }
166 1.1 jtc case 3:
167 1.7 jdolecek if (!is_default(argv[2])) {
168 1.7 jdolecek if (!sscanf(argv[2], "%lf", &ender))
169 1.7 jdolecek ender = argv[2][strlen(argv[2])-1];
170 1.21 dsl have |= ENDER;
171 1.18 dsl if (prec < 0)
172 1.7 jdolecek n = getprec(argv[2]);
173 1.1 jtc }
174 1.1 jtc case 2:
175 1.7 jdolecek if (!is_default(argv[1])) {
176 1.7 jdolecek if (!sscanf(argv[1], "%lf", &begin))
177 1.7 jdolecek begin = argv[1][strlen(argv[1])-1];
178 1.21 dsl have |= BEGIN;
179 1.18 dsl if (prec < 0)
180 1.7 jdolecek prec = getprec(argv[1]);
181 1.1 jtc if (n > prec) /* maximum precision */
182 1.1 jtc prec = n;
183 1.1 jtc }
184 1.1 jtc case 1:
185 1.7 jdolecek if (!is_default(argv[0])) {
186 1.21 dsl reps = strtoul(argv[0], &ep, 0);
187 1.21 dsl if (*ep != 0 || reps < 0)
188 1.20 dsl errx(EXIT_FAILURE, "Bad reps value: %s",
189 1.20 dsl argv[0]);
190 1.21 dsl have |= REPS;
191 1.1 jtc }
192 1.1 jtc break;
193 1.1 jtc case 0:
194 1.7 jdolecek usage();
195 1.7 jdolecek break;
196 1.1 jtc default:
197 1.20 dsl errx(EXIT_FAILURE,
198 1.20 dsl "Too many arguments. What do you mean by %s?", argv[4]);
199 1.1 jtc }
200 1.1 jtc getformat();
201 1.21 dsl
202 1.21 dsl if (prec == -1)
203 1.21 dsl prec = 0;
204 1.21 dsl
205 1.21 dsl if (randomize) {
206 1.21 dsl /* 'step' is the seed here, use pseudo-random default */
207 1.21 dsl if (!(have & STEP))
208 1.21 dsl step = time(NULL) * getpid();
209 1.21 dsl /* Take the default values for everything else */
210 1.21 dsl return;
211 1.21 dsl }
212 1.21 dsl
213 1.21 dsl /*
214 1.21 dsl * The loop we run uses begin/step/reps, so if we have been
215 1.21 dsl * given an end value (ender) we must use it to replace the
216 1.21 dsl * default values of the others.
217 1.21 dsl * We will assume a begin of 0 and step of 1 if necessary.
218 1.21 dsl */
219 1.21 dsl
220 1.21 dsl switch (have) {
221 1.21 dsl
222 1.21 dsl case ENDER | STEP:
223 1.21 dsl case ENDER | STEP | BEGIN:
224 1.21 dsl /* Calculate reps */
225 1.21 dsl if (step == 0.0)
226 1.21 dsl reps = 0; /* ie infinite */
227 1.21 dsl else {
228 1.18 dsl reps = (ender - begin + step) / step;
229 1.1 jtc if (reps <= 0)
230 1.20 dsl errx(EXIT_FAILURE, "Impossible stepsize");
231 1.21 dsl }
232 1.21 dsl break;
233 1.21 dsl
234 1.21 dsl case REPS | ENDER:
235 1.21 dsl case REPS | ENDER | STEP:
236 1.21 dsl /* Calculate begin */
237 1.21 dsl if (reps == 0)
238 1.21 dsl errx(EXIT_FAILURE,
239 1.21 dsl "Must specify begin if reps == 0");
240 1.21 dsl begin = ender - reps * step + step;
241 1.21 dsl break;
242 1.21 dsl
243 1.21 dsl case REPS | BEGIN | ENDER:
244 1.21 dsl /* Calculate step */
245 1.21 dsl if (reps == 0)
246 1.21 dsl errx(EXIT_FAILURE,
247 1.21 dsl "Infinite sequences cannot be bounded");
248 1.21 dsl if (reps == 1)
249 1.21 dsl step = 0.0;
250 1.21 dsl else
251 1.21 dsl step = (ender - begin) / (reps - 1);
252 1.21 dsl break;
253 1.21 dsl
254 1.21 dsl case REPS | BEGIN | ENDER | STEP:
255 1.21 dsl /* reps given and implied - take smaller */
256 1.21 dsl if (step == 0.0)
257 1.1 jtc break;
258 1.21 dsl t = (ender - begin + step) / step;
259 1.21 dsl if (t <= 0)
260 1.21 dsl errx(EXIT_FAILURE,
261 1.21 dsl "Impossible stepsize");
262 1.21 dsl if (t < reps)
263 1.21 dsl reps = t;
264 1.21 dsl break;
265 1.21 dsl
266 1.21 dsl default:
267 1.21 dsl /* No values can be calculated, use defaults */
268 1.21 dsl break;
269 1.16 dsl }
270 1.1 jtc }
271 1.1 jtc
272 1.20 dsl static void
273 1.13 perry putdata(double x, long notlast)
274 1.1 jtc {
275 1.19 dsl long d = floor(x);
276 1.1 jtc
277 1.1 jtc if (boring) /* repeated word */
278 1.3 pk printf("%s", format);
279 1.1 jtc else if (dox) /* scalar */
280 1.16 dsl printf(format, d);
281 1.1 jtc else /* real */
282 1.1 jtc printf(format, x);
283 1.1 jtc if (notlast != 0)
284 1.1 jtc fputs(sepstring, stdout);
285 1.1 jtc }
286 1.1 jtc
287 1.20 dsl __dead static void
288 1.7 jdolecek usage(void)
289 1.1 jtc {
290 1.12 peter (void)fprintf(stderr, "usage: %s [-cnr] [-b word] [-p precision] "
291 1.20 dsl "[-s string] [-w word] [reps [begin [end [step | seed]]]]\n",
292 1.20 dsl getprogname());
293 1.1 jtc exit(1);
294 1.1 jtc }
295 1.1 jtc
296 1.20 dsl static int
297 1.20 dsl getprec(char *num_str)
298 1.1 jtc {
299 1.1 jtc
300 1.20 dsl num_str = strchr(num_str, '.');
301 1.20 dsl if (num_str == NULL)
302 1.20 dsl return 0;
303 1.20 dsl return strspn(num_str + 1, "0123456789");
304 1.1 jtc }
305 1.1 jtc
306 1.20 dsl static void
307 1.13 perry getformat(void)
308 1.1 jtc {
309 1.4 lukem char *p;
310 1.7 jdolecek size_t sz;
311 1.1 jtc
312 1.1 jtc if (boring) /* no need to bother */
313 1.1 jtc return;
314 1.20 dsl for (p = format; *p; p++) { /* look for '%' */
315 1.7 jdolecek if (*p == '%') {
316 1.7 jdolecek if (*(p+1) != '%')
317 1.7 jdolecek break;
318 1.7 jdolecek p++; /* leave %% alone */
319 1.7 jdolecek }
320 1.20 dsl }
321 1.7 jdolecek sz = sizeof(format) - strlen(format) - 1;
322 1.7 jdolecek if (!*p) {
323 1.17 dsl if (chardata || prec == 0) {
324 1.17 dsl if (snprintf(p, sz, "%%%s", chardata ? "c" : "ld") >= sz)
325 1.20 dsl errx(EXIT_FAILURE, "-w word too long");
326 1.8 simonb dox = 1;
327 1.7 jdolecek } else {
328 1.8 simonb if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
329 1.20 dsl errx(EXIT_FAILURE, "-w word too long");
330 1.7 jdolecek }
331 1.7 jdolecek } else if (!*(p+1)) {
332 1.7 jdolecek if (sz <= 0)
333 1.20 dsl errx(EXIT_FAILURE, "-w word too long");
334 1.1 jtc strcat(format, "%"); /* cannot end in single '%' */
335 1.7 jdolecek } else {
336 1.7 jdolecek p++; /* skip leading % */
337 1.7 jdolecek for(; *p && !isalpha((unsigned char)*p); p++) {
338 1.7 jdolecek /* allow all valid printf(3) flags, but deny '*' */
339 1.7 jdolecek if (!strchr("0123456789#-+. ", *p))
340 1.7 jdolecek break;
341 1.7 jdolecek }
342 1.7 jdolecek /* Allow 'l' prefix, but no other. */
343 1.7 jdolecek if (*p == 'l')
344 1.7 jdolecek p++;
345 1.1 jtc switch (*p) {
346 1.1 jtc case 'f': case 'e': case 'g': case '%':
347 1.7 jdolecek case 'E': case 'G':
348 1.1 jtc break;
349 1.1 jtc case 's':
350 1.20 dsl errx(EXIT_FAILURE,
351 1.20 dsl "cannot convert numeric data to strings");
352 1.1 jtc break;
353 1.7 jdolecek case 'd': case 'o': case 'x': case 'u':
354 1.7 jdolecek case 'D': case 'O': case 'X': case 'U':
355 1.7 jdolecek case 'c': case 'i':
356 1.1 jtc dox = 1;
357 1.1 jtc break;
358 1.7 jdolecek default:
359 1.20 dsl errx(EXIT_FAILURE, "unknown or invalid format `%s'",
360 1.20 dsl format);
361 1.1 jtc }
362 1.7 jdolecek /* Need to check for trailing stuff to print */
363 1.7 jdolecek for (; *p; p++) /* look for '%' */
364 1.7 jdolecek if (*p == '%') {
365 1.7 jdolecek if (*(p+1) != '%')
366 1.7 jdolecek break;
367 1.7 jdolecek p++; /* leave %% alone */
368 1.7 jdolecek }
369 1.7 jdolecek if (*p)
370 1.20 dsl errx(EXIT_FAILURE, "unknown or invalid format `%s'",
371 1.20 dsl format);
372 1.1 jtc }
373 1.1 jtc }
374