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
29 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
30 * @(#)jot.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/jot/jot.c,v 1.13.2.3 2001/12/17 13:49:50 gallatin Exp $
35 * jot - print sequential or random data
37 * Author: John Kunze, Office of Comp. Affairs, UCB
55 #define is_default(s) (strcmp((s), "-") == 0)
66 static const char *sepstring
= "\n";
67 static char format
[BUFSIZ
];
69 static void getformat(void);
70 static int getprec(const char *);
71 static int putdata(double, long);
72 static void usage(void) __dead2
;
75 main(int argc
, char **argv
)
82 unsigned int mask
= 0;
85 double begin
= BEGIN_DEF
;
86 double ender
= ENDER_DEF
;
90 while ((ch
= getopt(argc
, argv
, "rb:w:cs:np:")) != -1)
105 if (strlcpy(format
, optarg
, sizeof(format
)) >=
107 errx(1, "-%c word too long", ch
);
115 errx(1, "bad precision value");
123 switch (argc
) { /* examine args right to left, falling thru cases */
125 if (!is_default(argv
[3])) {
126 if (!sscanf(argv
[3], "%lf", &s
))
127 errx(1, "bad s value: %s", argv
[3]);
132 if (!is_default(argv
[2])) {
133 if (!sscanf(argv
[2], "%lf", &ender
))
134 ender
= argv
[2][strlen(argv
[2])-1];
137 n
= getprec(argv
[2]);
141 if (!is_default(argv
[1])) {
142 if (!sscanf(argv
[1], "%lf", &begin
))
143 begin
= argv
[1][strlen(argv
[1])-1];
146 prec
= getprec(argv
[1]);
147 if (n
> prec
) /* maximum precision */
152 if (!is_default(argv
[0])) {
153 if (!sscanf(argv
[0], "%ld", &reps
))
154 errx(1, "bad reps value: %s", argv
[0]);
161 errx(1, "too many arguments. What do you mean by %s?",
165 while (mask
) /* 4 bit mask has 1's where last 4 args were given */
166 switch (mask
) { /* fill in the 0's by default or computation */
202 reps
= (ender
- begin
+ s
) / s
;
204 errx(1, "impossible stepsize");
216 s
= (randomize
? time(NULL
) : STEP_DEF
);
223 errx(1, "must specify begin if reps == 0");
224 begin
= ender
- reps
* s
+ s
;
228 s
= (randomize
? -1.0 : STEP_DEF
);
235 ender
= begin
+ reps
* s
- s
;
242 errx(1, "infinite sequences cannot be bounded");
246 s
= (ender
- begin
) / (reps
- 1);
249 case 017: /* if reps given and implied, */
250 if (!randomize
&& s
!= 0.0) {
251 long t
= (ender
- begin
+ s
) / s
;
253 errx(1, "impossible stepsize");
254 if (t
< reps
) /* take lesser */
265 *x
= (ender
- begin
) * (ender
> begin
? 1 : -1);
266 for (*i
= 1; *i
<= reps
|| infinity
; (*i
)++) {
267 *y
= arc4random() / (double)0xffffffffU
;
268 if (putdata(*y
* *x
+ begin
, reps
- *i
))
269 errx(1, "range error in conversion");
272 for (*i
= 1, *x
= begin
; *i
<= reps
|| infinity
; (*i
)++, *x
+= s
)
273 if (putdata(*x
, reps
- *i
))
274 errx(1, "range error in conversion");
281 putdata(double x
, long notlast
)
285 printf("%s", format
);
286 else if (longdata
&& nosign
) {
287 if (x
<= (double)ULONG_MAX
&& x
>= (double)0)
288 printf(format
, (unsigned long)x
);
291 } else if (longdata
) {
292 if (x
<= (double)LONG_MAX
&& x
>= (double)LONG_MIN
)
293 printf(format
, (long)x
);
296 } else if (chardata
|| (intdata
&& !nosign
)) {
297 if (x
<= (double)INT_MAX
&& x
>= (double)INT_MIN
)
298 printf(format
, (int)x
);
301 } else if (intdata
) {
302 if (x
<= (double)UINT_MAX
&& x
>= (double)0)
303 printf(format
, (unsigned int)x
);
310 fputs(sepstring
, stdout
);
318 fprintf(stderr
, "%s\n%s\n",
319 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
320 " [reps [begin [end [s]]]]");
325 getprec(const char *str
)
330 for (p
= str
; *p
; p
++)
335 for (q
= ++p
; *p
; p
++)
345 int dot
, hash
, space
, sign
, numbers
= 0;
348 if (boring
) /* no need to bother */
350 for (p
= format
; *p
; p
++) /* look for '%' */
351 if (*p
== '%' && *(p
+1) != '%') /* leave %% alone */
353 sz
= sizeof(format
) - strlen(format
) - 1;
354 if (!*p
&& !chardata
) {
355 if (snprintf(p
, sz
, "%%.%df", prec
) >= (int)sz
)
356 errx(1, "-w word too long");
357 } else if (!*p
&& chardata
) {
358 if (strlcpy(p
, "%c", sz
) >= sz
)
359 errx(1, "-w word too long");
361 } else if (!*(p
+1)) {
363 errx(1, "-w word too long");
364 strcat(format
, "%"); /* cannot end in single '%' */
367 * Allow conversion format specifiers of the form
368 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
369 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
372 dot
= hash
= space
= sign
= numbers
= 0;
373 while (!isalpha(*p
)) {
377 } else if ((*p
== '#' && !(numbers
|dot
|sign
|space
|
379 (*p
== ' ' && !(numbers
|dot
|space
++)) ||
380 ((*p
== '+' || *p
== '-') && !(numbers
|dot
|sign
++))
381 || (*p
== '.' && !(dot
++)))
395 case 'o': case 'u': case 'x': case 'X':
396 intdata
= nosign
= 1;
409 intdata
= nosign
= 1;
412 if (!(intdata
| longdata
)) {
416 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
419 case 'f': case 'e': case 'g': case 'E': case 'G':
426 errx(1, "illegal or unsupported format '%s'", p2
);
430 if (*p
== '%' && *(p
+1) && *(p
+1) != '%')
431 errx(1, "too many conversions");
432 else if (*p
== '%' && *(p
+1) == '%')
434 else if (*p
== '%' && !*(p
+1)) {