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 (caph_rights_limit(STDIN_FILENO, &rights) < 0)
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:
268 errx(1, "infinite sequences cannot "
273 s = (ender - begin) / (reps - 1);
277 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
278 /* if reps given and implied, */
279 if (!randomize && s != 0.0) {
280 long t = (ender - begin + s) / s;
282 errx(1, "impossible stepsize");
283 if (t < reps) /* take lesser */
295 srandom((unsigned long)s);
296 divisor = (double)INT32_MAX + 1;
298 divisor = (double)UINT32_MAX + 1;
301 * Attempt to DWIM when the user has specified an
302 * integer range within that of the random number
303 * generator: distribute the numbers equally in
304 * the range [begin .. ender]. Jot's default %.0f
305 * format would make the appearance of the first and
306 * last specified value half as likely as the rest.
308 if (!have_format && prec == 0 &&
309 begin >= 0 && begin < divisor &&
310 ender >= 0 && ender < divisor) {
317 (void)strlcpy(format,
318 chardata ? "%c" : "%u", sizeof(format));
321 for (i = 1; i <= reps || infinity; i++) {
323 y = random() / divisor;
325 y = arc4random() / divisor;
326 if (putdata(y * x + begin, !(reps - i)))
327 errx(1, "range error in conversion");
330 for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
331 if (putdata(x, !(reps - i)))
332 errx(1, "range error in conversion");
339 * Send x to stdout using the specified format.
340 * Last is true if this is the set's last value.
341 * Return 0 if OK, or a positive number if the number passed was
342 * outside the range specified by the various flags.
345 putdata(double x, bool last)
349 printf("%s", format);
350 else if (longdata && nosign) {
351 if (x <= (double)ULONG_MAX && x >= (double)0)
352 printf(format, (unsigned long)x);
355 } else if (longdata) {
356 if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
357 printf(format, (long)x);
360 } else if (chardata || (intdata && !nosign)) {
361 if (x <= (double)INT_MAX && x >= (double)INT_MIN)
362 printf(format, (int)x);
365 } else if (intdata) {
366 if (x <= (double)UINT_MAX && x >= (double)0)
367 printf(format, (unsigned int)x);
374 fputs(sepstring, stdout);
382 fprintf(stderr, "%s\n%s\n",
383 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
384 " [reps [begin [end [s]]]]");
389 * Return the number of digits following the number's decimal point.
390 * Return 0 if no decimal point is found.
393 getprec(const char *str)
398 for (p = str; *p; p++)
403 for (q = ++p; *p; p++)
404 if (!isdigit((unsigned char)*p))
410 * Set format, intdata, chardata, longdata, and nosign
411 * based on the command line arguments.
417 int dot, hash, space, sign, numbers = 0;
420 if (boring) /* no need to bother */
422 for (p = format; *p; p++) /* look for '%' */
425 p++; /* leave %% alone */
429 sz = sizeof(format) - strlen(format) - 1;
430 if (!*p && !chardata) {
431 if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
432 errx(1, "-w word too long");
433 } else if (!*p && chardata) {
434 if (strlcpy(p, "%c", sz) >= sz)
435 errx(1, "-w word too long");
437 } else if (!*(p+1)) {
439 errx(1, "-w word too long");
440 strcat(format, "%"); /* cannot end in single '%' */
443 * Allow conversion format specifiers of the form
444 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
445 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
448 dot = hash = space = sign = numbers = 0;
449 while (!isalpha((unsigned char)*p)) {
450 if (isdigit((unsigned char)*p)) {
453 } else if ((*p == '#' && !(numbers|dot|sign|space|
455 (*p == ' ' && !(numbers|dot|space++)) ||
456 ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
457 || (*p == '.' && !(dot++)))
471 case 'o': case 'u': case 'x': case 'X':
472 intdata = nosign = true;
484 intdata = nosign = true;
488 if (!(intdata | longdata)) {
492 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
495 case 'f': case 'e': case 'g': case 'E': case 'G':
502 errx(1, "illegal or unsupported format '%s'", p2);
506 if (*p == '%' && *(p+1) && *(p+1) != '%')
507 errx(1, "too many conversions");
508 else if (*p == '%' && *(p+1) == '%')
510 else if (*p == '%' && !*(p+1)) {
511 if (strlcat(format, "%", sizeof(format)) >=
513 errx(1, "-w word too long");