3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
49 * jot - print sequential or random data
51 * Author: John Kunze, Office of Comp. Affairs, UCB
68 #define is_default(s) (strcmp((s), "-") == 0)
83 const char *sepstring = "\n";
86 int main __P((int, char *[]));
87 void getformat __P((void));
88 int getprec __P((char *));
89 int putdata __P((double, long));
90 static void usage __P((void));
102 unsigned int mask = 0;
106 while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1)
121 if (strlcpy(format, optarg, sizeof(format)) >=
123 errx(1, "-%c word too long", ch);
131 errx(1, "bad precision value");
139 switch (argc) { /* examine args right to left, falling thru cases */
141 if (!is_default(argv[3])) {
142 if (!sscanf(argv[3], "%lf", &s))
143 errx(1, "bad s value: %s", argv[3]);
147 if (!is_default(argv[2])) {
148 if (!sscanf(argv[2], "%lf", &ender))
149 ender = argv[2][strlen(argv[2])-1];
152 n = getprec(argv[2]);
155 if (!is_default(argv[1])) {
156 if (!sscanf(argv[1], "%lf", &begin))
157 begin = argv[1][strlen(argv[1])-1];
160 prec = getprec(argv[1]);
161 if (n > prec) /* maximum precision */
165 if (!is_default(argv[0])) {
166 if (!sscanf(argv[0], "%ld", &reps))
167 errx(1, "bad reps value: %s", argv[0]);
174 errx(1, "too many arguments. What do you mean by %s?",
178 while (mask) /* 4 bit mask has 1's where last 4 args were given */
179 switch (mask) { /* fill in the 0's by default or computation */
215 reps = (ender - begin + s) / s;
217 errx(1, "impossible stepsize");
229 s = (randomize ? time(NULL) : STEP_DEF);
236 errx(1, "must specify begin if reps == 0");
237 begin = ender - reps * s + s;
241 s = (randomize ? -1.0 : STEP_DEF);
248 ender = begin + reps * s - s;
255 errx(1, "infinite sequences cannot be bounded");
259 s = (ender - begin) / (reps - 1);
262 case 017: /* if reps given and implied, */
263 if (!randomize && s != 0.0) {
264 long t = (ender - begin + s) / s;
266 errx(1, "impossible stepsize");
267 if (t < reps) /* take lesser */
278 *x = (ender - begin) * (ender > begin ? 1 : -1);
279 for (*i = 1; *i <= reps || infinity; (*i)++) {
280 *y = arc4random() / (double)ULONG_MAX;
281 if (putdata(*y * *x + begin, reps - *i))
282 errx(1, "range error in conversion");
285 for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s)
286 if (putdata(*x, reps - *i))
287 errx(1, "range error in conversion");
300 printf("%s", format);
301 else if (longdata && nosign) {
302 if (x <= (double)ULONG_MAX && x >= (double)0)
303 printf(format, (unsigned long)x);
306 } else if (longdata) {
307 if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
308 printf(format, (long)x);
311 } else if (chardata || (intdata && !nosign)) {
312 if (x <= (double)INT_MAX && x >= (double)INT_MIN)
313 printf(format, (int)x);
316 } else if (intdata) {
317 if (x <= (double)UINT_MAX && x >= (double)0)
318 printf(format, (unsigned int)x);
325 fputs(sepstring, stdout);
333 fprintf(stderr, "%s\n%s\n",
334 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
335 " [reps [begin [end [s]]]]");
346 for (p = str; *p; p++)
351 for (q = ++p; *p; p++)
361 int dot, hash, space, sign, numbers = 0;
364 if (boring) /* no need to bother */
366 for (p = format; *p; p++) /* look for '%' */
367 if (*p == '%' && *(p+1) != '%') /* leave %% alone */
369 sz = sizeof(format) - strlen(format) - 1;
370 if (!*p && !chardata) {
371 if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
372 errx(1, "-w word too long");
373 } else if (!*p && chardata) {
374 if (strlcpy(p, "%c", sz) >= sz)
375 errx(1, "-w word too long");
377 } else if (!*(p+1)) {
379 errx(1, "-w word too long");
380 strcat(format, "%"); /* cannot end in single '%' */
383 * Allow conversion format specifiers of the form
384 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
385 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
388 dot = hash = space = sign = numbers = 0;
389 while (!isalpha(*p)) {
393 } else if ((*p == '#' && !(numbers|dot|sign|space|
395 (*p == ' ' && !(numbers|dot|space++)) ||
396 ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
397 || (*p == '.' && !(dot++)))
411 case 'o': case 'u': case 'x': case 'X':
412 intdata = nosign = 1;
424 intdata = nosign = 1;
428 if (!(intdata | longdata)) {
432 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
435 case 'f': case 'e': case 'g': case 'E': case 'G':
442 errx(1, "illegal or unsupported format '%s'", p2);
446 if (*p == '%' && *(p+1) && *(p+1) != '%')
447 errx(1, "too many conversions");
448 else if (*p == '%' && *(p+1) == '%')
450 else if (*p == '%' && !*(p+1)) {