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 * jot - print sequential or random data
35 * Author: John Kunze, Office of Comp. Affairs, UCB
38 #include <sys/capsicum.h>
39 #include <capsicum_helpers.h>
58 /* Flags of options that have been set */
64 #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0)
72 static const char *sepstring = "\n";
73 static char format[BUFSIZ];
75 static void getformat(void);
76 static int getprec(const char *);
77 static int putdata(double, bool);
78 static void usage(void);
81 main(int argc, char **argv)
84 bool have_format = false;
85 bool infinity = false;
86 bool nofinalnl = false;
87 bool randomize = false;
88 bool use_random = false;
92 double begin = BEGIN_DEF;
94 double ender = ENDER_DEF;
100 if (caph_limit_stdio() < 0)
101 err(1, "unable to limit rights for stdio");
102 cap_rights_init(&rights);
103 if (caph_rights_limit(STDIN_FILENO, &rights) < 0)
104 err(1, "unable to limit rights for stdin");
107 * Cache NLS data, for strerror, for err(3), before entering capability
110 caph_cache_catpages();
112 if (caph_enter() < 0)
113 err(1, "unable to enter capability mode");
115 while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1)
121 if (strlcpy(format, optarg, sizeof(format)) >=
123 errx(1, "-%c word too long", ch);
135 errx(1, "bad precision value");
150 switch (argc) { /* examine args right to left, falling thru cases */
152 if (!is_default(argv[3])) {
153 if (!sscanf(argv[3], "%lf", &s))
154 errx(1, "bad s value: %s", argv[3]);
161 if (!is_default(argv[2])) {
162 if (!sscanf(argv[2], "%lf", &ender))
163 ender = argv[2][strlen(argv[2])-1];
166 n = getprec(argv[2]);
170 if (!is_default(argv[1])) {
171 if (!sscanf(argv[1], "%lf", &begin))
172 begin = argv[1][strlen(argv[1])-1];
175 prec = getprec(argv[1]);
176 if (n > prec) /* maximum precision */
181 if (!is_default(argv[0])) {
182 if (!sscanf(argv[0], "%ld", &reps))
183 errx(1, "bad reps value: %s", argv[0]);
190 errx(1, "too many arguments. What do you mean by %s?",
198 while (mask) /* 4 bit mask has 1's where last 4 args were given */
199 switch (mask) { /* fill in the 0's by default or computation */
202 case HAVE_ENDER | HAVE_STEP:
204 case HAVE_BEGIN | HAVE_STEP:
208 case HAVE_BEGIN | HAVE_ENDER:
209 s = ender > begin ? 1 : -1;
212 case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
218 reps = (ender - begin + s) / s;
220 errx(1, "impossible stepsize");
224 case HAVE_REPS | HAVE_STEP:
228 case HAVE_REPS | HAVE_ENDER:
230 mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP;
232 case HAVE_REPS | HAVE_ENDER | HAVE_STEP:
236 errx(1, "must specify begin if reps == 0");
237 begin = ender - reps * s + s;
240 case HAVE_REPS | HAVE_BEGIN:
242 mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP;
244 case HAVE_REPS | HAVE_BEGIN | HAVE_STEP:
248 ender = begin + reps * s - s;
251 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER:
254 errx(1, "infinite sequences cannot "
259 s = (ender - begin) / (reps - 1);
263 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
264 /* if reps given and implied, */
265 if (!randomize && s != 0.0) {
266 long t = (ender - begin + s) / s;
268 errx(1, "impossible stepsize");
269 if (t < reps) /* take lesser */
281 srandom((unsigned long)s);
282 divisor = (double)INT32_MAX + 1;
284 divisor = (double)UINT32_MAX + 1;
287 * Attempt to DWIM when the user has specified an
288 * integer range within that of the random number
289 * generator: distribute the numbers equally in
290 * the range [begin .. ender]. Jot's default %.0f
291 * format would make the appearance of the first and
292 * last specified value half as likely as the rest.
294 if (!have_format && prec == 0 &&
295 begin >= 0 && begin < divisor &&
296 ender >= 0 && ender < divisor) {
303 (void)strlcpy(format,
304 chardata ? "%c" : "%u", sizeof(format));
307 for (i = 1; i <= reps || infinity; i++) {
309 y = random() / divisor;
311 y = arc4random() / divisor;
312 if (putdata(y * x + begin, !(reps - i)))
313 errx(1, "range error in conversion");
316 for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
317 if (putdata(x, !(reps - i)))
318 errx(1, "range error in conversion");
325 * Send x to stdout using the specified format.
326 * Last is true if this is the set's last value.
327 * Return 0 if OK, or a positive number if the number passed was
328 * outside the range specified by the various flags.
331 putdata(double x, bool last)
335 printf("%s", format);
336 else if (longdata && nosign) {
337 if (x <= (double)ULONG_MAX && x >= (double)0)
338 printf(format, (unsigned long)x);
341 } else if (longdata) {
342 if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
343 printf(format, (long)x);
346 } else if (chardata || (intdata && !nosign)) {
347 if (x <= (double)INT_MAX && x >= (double)INT_MIN)
348 printf(format, (int)x);
351 } else if (intdata) {
352 if (x <= (double)UINT_MAX && x >= (double)0)
353 printf(format, (unsigned int)x);
360 fputs(sepstring, stdout);
368 fprintf(stderr, "%s\n%s\n",
369 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
370 " [reps [begin [end [s]]]]");
375 * Return the number of digits following the number's decimal point.
376 * Return 0 if no decimal point is found.
379 getprec(const char *str)
384 for (p = str; *p; p++)
389 for (q = ++p; *p; p++)
390 if (!isdigit((unsigned char)*p))
396 * Set format, intdata, chardata, longdata, and nosign
397 * based on the command line arguments.
403 int dot, hash, space, sign, numbers = 0;
406 if (boring) /* no need to bother */
408 for (p = format; *p; p++) /* look for '%' */
411 p++; /* leave %% alone */
415 sz = sizeof(format) - strlen(format) - 1;
416 if (!*p && !chardata) {
417 if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
418 errx(1, "-w word too long");
419 } else if (!*p && chardata) {
420 if (strlcpy(p, "%c", sz) >= sz)
421 errx(1, "-w word too long");
423 } else if (!*(p+1)) {
425 errx(1, "-w word too long");
426 strcat(format, "%"); /* cannot end in single '%' */
429 * Allow conversion format specifiers of the form
430 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
431 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
434 dot = hash = space = sign = numbers = 0;
435 while (!isalpha((unsigned char)*p)) {
436 if (isdigit((unsigned char)*p)) {
439 } else if ((*p == '#' && !(numbers|dot|sign|space|
441 (*p == ' ' && !(numbers|dot|space++)) ||
442 ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
443 || (*p == '.' && !(dot++)))
457 case 'o': case 'u': case 'x': case 'X':
458 intdata = nosign = true;
470 intdata = nosign = true;
474 if (!(intdata | longdata)) {
478 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
481 case 'f': case 'e': case 'g': case 'E': case 'G':
488 errx(1, "illegal or unsupported format '%s'", p2);
492 if (*p == '%' && *(p+1) && *(p+1) != '%')
493 errx(1, "too many conversions");
494 else if (*p == '%' && *(p+1) == '%')
496 else if (*p == '%' && !*(p+1)) {
497 if (strlcat(format, "%", sizeof(format)) >=
499 errx(1, "-w word too long");