2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * jot - print sequential or random data
49 * Author: John Kunze, Office of Comp. Affairs, UCB
52 #include <sys/capsicum.h>
53 #include <capsicum_helpers.h>
72 /* Flags of options that have been set */
78 #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0)
86 static const char *sepstring = "\n";
87 static char format[BUFSIZ];
89 static void getformat(void);
90 static int getprec(const char *);
91 static int putdata(double, bool);
92 static void usage(void);
95 main(int argc, char **argv)
98 bool have_format = false;
99 bool infinity = false;
100 bool nofinalnl = false;
101 bool randomize = false;
102 bool use_random = false;
106 double begin = BEGIN_DEF;
108 double ender = ENDER_DEF;
112 long reps = REPS_DEF;
114 if (caph_limit_stdio() < 0)
115 err(1, "unable to limit rights for stdio");
116 cap_rights_init(&rights);
117 if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
118 err(1, "unable to limit rights for stdin");
121 * Cache NLS data, for strerror, for err(3), before entering capability
124 caph_cache_catpages();
126 if (caph_enter() < 0)
127 err(1, "unable to enter capability mode");
129 while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1)
135 if (strlcpy(format, optarg, sizeof(format)) >=
137 errx(1, "-%c word too long", ch);
149 errx(1, "bad precision value");
164 switch (argc) { /* examine args right to left, falling thru cases */
166 if (!is_default(argv[3])) {
167 if (!sscanf(argv[3], "%lf", &s))
168 errx(1, "bad s value: %s", argv[3]);
175 if (!is_default(argv[2])) {
176 if (!sscanf(argv[2], "%lf", &ender))
177 ender = argv[2][strlen(argv[2])-1];
180 n = getprec(argv[2]);
184 if (!is_default(argv[1])) {
185 if (!sscanf(argv[1], "%lf", &begin))
186 begin = argv[1][strlen(argv[1])-1];
189 prec = getprec(argv[1]);
190 if (n > prec) /* maximum precision */
195 if (!is_default(argv[0])) {
196 if (!sscanf(argv[0], "%ld", &reps))
197 errx(1, "bad reps value: %s", argv[0]);
204 errx(1, "too many arguments. What do you mean by %s?",
212 while (mask) /* 4 bit mask has 1's where last 4 args were given */
213 switch (mask) { /* fill in the 0's by default or computation */
216 case HAVE_ENDER | HAVE_STEP:
218 case HAVE_BEGIN | HAVE_STEP:
222 case HAVE_BEGIN | HAVE_ENDER:
223 s = ender > begin ? 1 : -1;
226 case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
232 reps = (ender - begin + s) / s;
234 errx(1, "impossible stepsize");
238 case HAVE_REPS | HAVE_STEP:
242 case HAVE_REPS | HAVE_ENDER:
244 mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP;
246 case HAVE_REPS | HAVE_ENDER | HAVE_STEP:
250 errx(1, "must specify begin if reps == 0");
251 begin = ender - reps * s + s;
254 case HAVE_REPS | HAVE_BEGIN:
256 mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP;
258 case HAVE_REPS | HAVE_BEGIN | HAVE_STEP:
262 ender = begin + reps * s - s;
265 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER:
267 errx(1, "infinite sequences cannot be bounded");
271 s = (ender - begin) / (reps - 1);
274 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
275 /* if reps given and implied, */
276 if (!randomize && s != 0.0) {
277 long t = (ender - begin + s) / s;
279 errx(1, "impossible stepsize");
280 if (t < reps) /* take lesser */
292 srandom((unsigned long)s);
293 divisor = (double)INT32_MAX + 1;
295 divisor = (double)UINT32_MAX + 1;
298 * Attempt to DWIM when the user has specified an
299 * integer range within that of the random number
300 * generator: distribute the numbers equally in
301 * the range [begin .. ender]. Jot's default %.0f
302 * format would make the appearance of the first and
303 * last specified value half as likely as the rest.
305 if (!have_format && prec == 0 &&
306 begin >= 0 && begin < divisor &&
307 ender >= 0 && ender < divisor) {
314 (void)strlcpy(format,
315 chardata ? "%c" : "%u", sizeof(format));
318 for (i = 1; i <= reps || infinity; i++) {
320 y = random() / divisor;
322 y = arc4random() / divisor;
323 if (putdata(y * x + begin, !(reps - i)))
324 errx(1, "range error in conversion");
327 for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
328 if (putdata(x, !(reps - i)))
329 errx(1, "range error in conversion");
336 * Send x to stdout using the specified format.
337 * Last is true if this is the set's last value.
338 * Return 0 if OK, or a positive number if the number passed was
339 * outside the range specified by the various flags.
342 putdata(double x, bool last)
346 printf("%s", format);
347 else if (longdata && nosign) {
348 if (x <= (double)ULONG_MAX && x >= (double)0)
349 printf(format, (unsigned long)x);
352 } else if (longdata) {
353 if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
354 printf(format, (long)x);
357 } else if (chardata || (intdata && !nosign)) {
358 if (x <= (double)INT_MAX && x >= (double)INT_MIN)
359 printf(format, (int)x);
362 } else if (intdata) {
363 if (x <= (double)UINT_MAX && x >= (double)0)
364 printf(format, (unsigned int)x);
371 fputs(sepstring, stdout);
379 fprintf(stderr, "%s\n%s\n",
380 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
381 " [reps [begin [end [s]]]]");
386 * Return the number of digits following the number's decimal point.
387 * Return 0 if no decimal point is found.
390 getprec(const char *str)
395 for (p = str; *p; p++)
400 for (q = ++p; *p; p++)
401 if (!isdigit((unsigned char)*p))
407 * Set format, intdata, chardata, longdata, and nosign
408 * based on the command line arguments.
414 int dot, hash, space, sign, numbers = 0;
417 if (boring) /* no need to bother */
419 for (p = format; *p; p++) /* look for '%' */
422 p++; /* leave %% alone */
426 sz = sizeof(format) - strlen(format) - 1;
427 if (!*p && !chardata) {
428 if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
429 errx(1, "-w word too long");
430 } else if (!*p && chardata) {
431 if (strlcpy(p, "%c", sz) >= sz)
432 errx(1, "-w word too long");
434 } else if (!*(p+1)) {
436 errx(1, "-w word too long");
437 strcat(format, "%"); /* cannot end in single '%' */
440 * Allow conversion format specifiers of the form
441 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
442 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
445 dot = hash = space = sign = numbers = 0;
446 while (!isalpha((unsigned char)*p)) {
447 if (isdigit((unsigned char)*p)) {
450 } else if ((*p == '#' && !(numbers|dot|sign|space|
452 (*p == ' ' && !(numbers|dot|space++)) ||
453 ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
454 || (*p == '.' && !(dot++)))
468 case 'o': case 'u': case 'x': case 'X':
469 intdata = nosign = true;
481 intdata = nosign = true;
485 if (!(intdata | longdata)) {
489 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
492 case 'f': case 'e': case 'g': case 'E': case 'G':
499 errx(1, "illegal or unsupported format '%s'", p2);
503 if (*p == '%' && *(p+1) && *(p+1) != '%')
504 errx(1, "too many conversions");
505 else if (*p == '%' && *(p+1) == '%')
507 else if (*p == '%' && !*(p+1)) {
508 if (strlcat(format, "%", sizeof(format)) >=
510 errx(1, "-w word too long");