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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 * jot - print sequential or random data
47 * Author: John Kunze, Office of Comp. Affairs, UCB
50 #include <sys/capsicum.h>
51 #include <capsicum_helpers.h>
70 /* Flags of options that have been set */
76 #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0)
84 static const char *sepstring = "\n";
85 static char format[BUFSIZ];
87 static void getformat(void);
88 static int getprec(const char *);
89 static int putdata(double, bool);
90 static void usage(void);
93 main(int argc, char **argv)
96 bool have_format = false;
97 bool infinity = false;
98 bool nofinalnl = false;
99 bool randomize = false;
100 bool use_random = false;
104 double begin = BEGIN_DEF;
106 double ender = ENDER_DEF;
110 long reps = REPS_DEF;
112 if (caph_limit_stdio() < 0)
113 err(1, "unable to limit rights for stdio");
114 cap_rights_init(&rights);
115 if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
116 err(1, "unable to limit rights for stdin");
119 * Cache NLS data, for strerror, for err(3), before entering capability
122 caph_cache_catpages();
124 if (cap_enter() < 0 && errno != ENOSYS)
125 err(1, "unable to enter capability mode");
127 while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1)
133 if (strlcpy(format, optarg, sizeof(format)) >=
135 errx(1, "-%c word too long", ch);
147 errx(1, "bad precision value");
162 switch (argc) { /* examine args right to left, falling thru cases */
164 if (!is_default(argv[3])) {
165 if (!sscanf(argv[3], "%lf", &s))
166 errx(1, "bad s value: %s", argv[3]);
173 if (!is_default(argv[2])) {
174 if (!sscanf(argv[2], "%lf", &ender))
175 ender = argv[2][strlen(argv[2])-1];
178 n = getprec(argv[2]);
182 if (!is_default(argv[1])) {
183 if (!sscanf(argv[1], "%lf", &begin))
184 begin = argv[1][strlen(argv[1])-1];
187 prec = getprec(argv[1]);
188 if (n > prec) /* maximum precision */
193 if (!is_default(argv[0])) {
194 if (!sscanf(argv[0], "%ld", &reps))
195 errx(1, "bad reps value: %s", argv[0]);
202 errx(1, "too many arguments. What do you mean by %s?",
210 while (mask) /* 4 bit mask has 1's where last 4 args were given */
211 switch (mask) { /* fill in the 0's by default or computation */
214 case HAVE_ENDER | HAVE_STEP:
216 case HAVE_BEGIN | HAVE_STEP:
220 case HAVE_BEGIN | HAVE_ENDER:
221 s = ender > begin ? 1 : -1;
224 case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
230 reps = (ender - begin + s) / s;
232 errx(1, "impossible stepsize");
236 case HAVE_REPS | HAVE_STEP:
240 case HAVE_REPS | HAVE_ENDER:
242 mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP;
244 case HAVE_REPS | HAVE_ENDER | HAVE_STEP:
248 errx(1, "must specify begin if reps == 0");
249 begin = ender - reps * s + s;
252 case HAVE_REPS | HAVE_BEGIN:
254 mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP;
256 case HAVE_REPS | HAVE_BEGIN | HAVE_STEP:
260 ender = begin + reps * s - s;
263 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER:
265 errx(1, "infinite sequences cannot be bounded");
269 s = (ender - begin) / (reps - 1);
272 case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
273 /* if reps given and implied, */
274 if (!randomize && s != 0.0) {
275 long t = (ender - begin + s) / s;
277 errx(1, "impossible stepsize");
278 if (t < reps) /* take lesser */
290 srandom((unsigned long)s);
291 divisor = (double)INT32_MAX + 1;
293 divisor = (double)UINT32_MAX + 1;
296 * Attempt to DWIM when the user has specified an
297 * integer range within that of the random number
298 * generator: distribute the numbers equally in
299 * the range [begin .. ender]. Jot's default %.0f
300 * format would make the appearance of the first and
301 * last specified value half as likely as the rest.
303 if (!have_format && prec == 0 &&
304 begin >= 0 && begin < divisor &&
305 ender >= 0 && ender < divisor) {
312 (void)strlcpy(format,
313 chardata ? "%c" : "%u", sizeof(format));
316 for (i = 1; i <= reps || infinity; i++) {
318 y = random() / divisor;
320 y = arc4random() / divisor;
321 if (putdata(y * x + begin, !(reps - i)))
322 errx(1, "range error in conversion");
325 for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
326 if (putdata(x, !(reps - i)))
327 errx(1, "range error in conversion");
334 * Send x to stdout using the specified format.
335 * Last is true if this is the set's last value.
336 * Return 0 if OK, or a positive number if the number passed was
337 * outside the range specified by the various flags.
340 putdata(double x, bool last)
344 printf("%s", format);
345 else if (longdata && nosign) {
346 if (x <= (double)ULONG_MAX && x >= (double)0)
347 printf(format, (unsigned long)x);
350 } else if (longdata) {
351 if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
352 printf(format, (long)x);
355 } else if (chardata || (intdata && !nosign)) {
356 if (x <= (double)INT_MAX && x >= (double)INT_MIN)
357 printf(format, (int)x);
360 } else if (intdata) {
361 if (x <= (double)UINT_MAX && x >= (double)0)
362 printf(format, (unsigned int)x);
369 fputs(sepstring, stdout);
377 fprintf(stderr, "%s\n%s\n",
378 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
379 " [reps [begin [end [s]]]]");
384 * Return the number of digits following the number's decimal point.
385 * Return 0 if no decimal point is found.
388 getprec(const char *str)
393 for (p = str; *p; p++)
398 for (q = ++p; *p; p++)
399 if (!isdigit((unsigned char)*p))
405 * Set format, intdata, chardata, longdata, and nosign
406 * based on the command line arguments.
412 int dot, hash, space, sign, numbers = 0;
415 if (boring) /* no need to bother */
417 for (p = format; *p; p++) /* look for '%' */
420 p++; /* leave %% alone */
424 sz = sizeof(format) - strlen(format) - 1;
425 if (!*p && !chardata) {
426 if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
427 errx(1, "-w word too long");
428 } else if (!*p && chardata) {
429 if (strlcpy(p, "%c", sz) >= sz)
430 errx(1, "-w word too long");
432 } else if (!*(p+1)) {
434 errx(1, "-w word too long");
435 strcat(format, "%"); /* cannot end in single '%' */
438 * Allow conversion format specifiers of the form
439 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
440 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
443 dot = hash = space = sign = numbers = 0;
444 while (!isalpha((unsigned char)*p)) {
445 if (isdigit((unsigned char)*p)) {
448 } else if ((*p == '#' && !(numbers|dot|sign|space|
450 (*p == ' ' && !(numbers|dot|space++)) ||
451 ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
452 || (*p == '.' && !(dot++)))
466 case 'o': case 'u': case 'x': case 'X':
467 intdata = nosign = true;
479 intdata = nosign = true;
483 if (!(intdata | longdata)) {
487 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
490 case 'f': case 'e': case 'g': case 'E': case 'G':
497 errx(1, "illegal or unsupported format '%s'", p2);
501 if (*p == '%' && *(p+1) && *(p+1) != '%')
502 errx(1, "too many conversions");
503 else if (*p == '%' && *(p+1) == '%')
505 else if (*p == '%' && !*(p+1)) {
506 if (strlcat(format, "%", sizeof(format)) >=
508 errx(1, "-w word too long");