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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
34 * @(#)jot.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/jot/jot.c,v 1.13.2.3 2001/12/17 13:49:50 gallatin Exp $
36 * $DragonFly: src/usr.bin/jot/jot.c,v 1.4 2004/07/31 10:19:53 eirikn Exp $
40 * jot - print sequential or random data
42 * Author: John Kunze, Office of Comp. Affairs, UCB
60 #define is_default(s) (strcmp((s), "-") == 0)
75 const char *sepstring
= "\n";
80 int putdata(double, long);
81 static void usage(void);
84 main(int argc
, char **argv
)
91 unsigned int mask
= 0;
95 while ((ch
= getopt(argc
, argv
, "rb:w:cs:np:")) != -1)
110 if (strlcpy(format
, optarg
, sizeof(format
)) >=
112 errx(1, "-%c word too long", ch
);
120 errx(1, "bad precision value");
128 switch (argc
) { /* examine args right to left, falling thru cases */
130 if (!is_default(argv
[3])) {
131 if (!sscanf(argv
[3], "%lf", &s
))
132 errx(1, "bad s value: %s", argv
[3]);
136 if (!is_default(argv
[2])) {
137 if (!sscanf(argv
[2], "%lf", &ender
))
138 ender
= argv
[2][strlen(argv
[2])-1];
141 n
= getprec(argv
[2]);
144 if (!is_default(argv
[1])) {
145 if (!sscanf(argv
[1], "%lf", &begin
))
146 begin
= argv
[1][strlen(argv
[1])-1];
149 prec
= getprec(argv
[1]);
150 if (n
> prec
) /* maximum precision */
154 if (!is_default(argv
[0])) {
155 if (!sscanf(argv
[0], "%ld", &reps
))
156 errx(1, "bad reps value: %s", argv
[0]);
163 errx(1, "too many arguments. What do you mean by %s?",
167 while (mask
) /* 4 bit mask has 1's where last 4 args were given */
168 switch (mask
) { /* fill in the 0's by default or computation */
204 reps
= (ender
- begin
+ s
) / s
;
206 errx(1, "impossible stepsize");
218 s
= (randomize
? time(NULL
) : STEP_DEF
);
225 errx(1, "must specify begin if reps == 0");
226 begin
= ender
- reps
* s
+ s
;
230 s
= (randomize
? -1.0 : STEP_DEF
);
237 ender
= begin
+ reps
* s
- s
;
244 errx(1, "infinite sequences cannot be bounded");
248 s
= (ender
- begin
) / (reps
- 1);
251 case 017: /* if reps given and implied, */
252 if (!randomize
&& s
!= 0.0) {
253 long t
= (ender
- begin
+ s
) / s
;
255 errx(1, "impossible stepsize");
256 if (t
< reps
) /* take lesser */
267 *x
= (ender
- begin
) * (ender
> begin
? 1 : -1);
268 for (*i
= 1; *i
<= reps
|| infinity
; (*i
)++) {
269 *y
= arc4random() / (double)0xffffffffU
;
270 if (putdata(*y
* *x
+ begin
, reps
- *i
))
271 errx(1, "range error in conversion");
274 for (*i
= 1, *x
= begin
; *i
<= reps
|| infinity
; (*i
)++, *x
+= s
)
275 if (putdata(*x
, reps
- *i
))
276 errx(1, "range error in conversion");
283 putdata(double x
, long notlast
)
287 printf("%s", format
);
288 else if (longdata
&& nosign
) {
289 if (x
<= (double)ULONG_MAX
&& x
>= (double)0)
290 printf(format
, (unsigned long)x
);
293 } else if (longdata
) {
294 if (x
<= (double)LONG_MAX
&& x
>= (double)LONG_MIN
)
295 printf(format
, (long)x
);
298 } else if (chardata
|| (intdata
&& !nosign
)) {
299 if (x
<= (double)INT_MAX
&& x
>= (double)INT_MIN
)
300 printf(format
, (int)x
);
303 } else if (intdata
) {
304 if (x
<= (double)UINT_MAX
&& x
>= (double)0)
305 printf(format
, (unsigned int)x
);
312 fputs(sepstring
, stdout
);
320 fprintf(stderr
, "%s\n%s\n",
321 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
322 " [reps [begin [end [s]]]]");
332 for (p
= str
; *p
; p
++)
337 for (q
= ++p
; *p
; p
++)
347 int dot
, hash
, space
, sign
, numbers
= 0;
350 if (boring
) /* no need to bother */
352 for (p
= format
; *p
; p
++) /* look for '%' */
353 if (*p
== '%' && *(p
+1) != '%') /* leave %% alone */
355 sz
= sizeof(format
) - strlen(format
) - 1;
356 if (!*p
&& !chardata
) {
357 if (snprintf(p
, sz
, "%%.%df", prec
) >= (int)sz
)
358 errx(1, "-w word too long");
359 } else if (!*p
&& chardata
) {
360 if (strlcpy(p
, "%c", sz
) >= sz
)
361 errx(1, "-w word too long");
363 } else if (!*(p
+1)) {
365 errx(1, "-w word too long");
366 strcat(format
, "%"); /* cannot end in single '%' */
369 * Allow conversion format specifiers of the form
370 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
371 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
374 dot
= hash
= space
= sign
= numbers
= 0;
375 while (!isalpha(*p
)) {
379 } else if ((*p
== '#' && !(numbers
|dot
|sign
|space
|
381 (*p
== ' ' && !(numbers
|dot
|space
++)) ||
382 ((*p
== '+' || *p
== '-') && !(numbers
|dot
|sign
++))
383 || (*p
== '.' && !(dot
++)))
397 case 'o': case 'u': case 'x': case 'X':
398 intdata
= nosign
= 1;
410 intdata
= nosign
= 1;
414 if (!(intdata
| longdata
)) {
418 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
421 case 'f': case 'e': case 'g': case 'E': case 'G':
428 errx(1, "illegal or unsupported format '%s'", p2
);
432 if (*p
== '%' && *(p
+1) && *(p
+1) != '%')
433 errx(1, "too many conversions");
434 else if (*p
== '%' && *(p
+1) == '%')
436 else if (*p
== '%' && !*(p
+1)) {