]>
git.saurik.com Git - apple/shell_cmds.git/blob - jot/jot.c
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
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/usr.bin/jot/jot.c,v 1.24 2002/07/05 15:58:27 mike Exp $");
49 * jot - print sequential or random data
51 * Author: John Kunze, Office of Comp. Affairs, UCB
69 #define is_default(s) (strcmp((s), "-") == 0)
84 const char *sepstring
= "\n";
89 int putdata(double, long);
90 static void usage(void);
93 main(int argc
, char **argv
)
100 unsigned int mask
= 0;
104 while ((ch
= getopt(argc
, argv
, "rb:w:cs:np:")) != -1)
119 if (strlcpy(format
, optarg
, sizeof(format
)) >=
121 errx(1, "-%c word too long", ch
);
129 errx(1, "bad precision value");
137 switch (argc
) { /* examine args right to left, falling thru cases */
139 if (!is_default(argv
[3])) {
140 if (!sscanf(argv
[3], "%lf", &s
))
141 errx(1, "bad s value: %s", argv
[3]);
145 if (!is_default(argv
[2])) {
146 if (!sscanf(argv
[2], "%lf", &ender
))
147 ender
= argv
[2][strlen(argv
[2])-1];
150 n
= getprec(argv
[2]);
153 if (!is_default(argv
[1])) {
154 if (!sscanf(argv
[1], "%lf", &begin
))
155 begin
= argv
[1][strlen(argv
[1])-1];
158 prec
= getprec(argv
[1]);
159 if (n
> prec
) /* maximum precision */
163 if (!is_default(argv
[0])) {
164 if (!sscanf(argv
[0], "%ld", &reps
))
165 errx(1, "bad reps value: %s", argv
[0]);
172 errx(1, "too many arguments. What do you mean by %s?",
176 while (mask
) /* 4 bit mask has 1's where last 4 args were given */
177 switch (mask
) { /* fill in the 0's by default or computation */
213 reps
= (ender
- begin
+ s
) / s
;
215 errx(1, "impossible stepsize");
227 s
= (randomize
? time(NULL
) : STEP_DEF
);
234 errx(1, "must specify begin if reps == 0");
235 begin
= ender
- reps
* s
+ s
;
239 s
= (randomize
? -1.0 : STEP_DEF
);
246 ender
= begin
+ reps
* s
- s
;
253 errx(1, "infinite sequences cannot be bounded");
257 s
= (ender
- begin
) / (reps
- 1);
260 case 017: /* if reps given and implied, */
261 if (!randomize
&& s
!= 0.0) {
262 long t
= (ender
- begin
+ s
) / s
;
264 errx(1, "impossible stepsize");
265 if (t
< reps
) /* take lesser */
276 *x
= (ender
- begin
) * (ender
> begin
? 1 : -1);
277 for (*i
= 1; *i
<= reps
|| infinity
; (*i
)++) {
278 *y
= arc4random() / (double)UINT32_MAX
;
279 if (putdata(*y
* *x
+ begin
, reps
- *i
))
280 errx(1, "range error in conversion");
283 for (*i
= 1, *x
= begin
; *i
<= reps
|| infinity
; (*i
)++, *x
+= s
)
284 if (putdata(*x
, reps
- *i
))
285 errx(1, "range error in conversion");
292 putdata(double x
, long int notlast
)
296 printf("%s", format
);
297 else if (longdata
&& nosign
) {
298 if (x
<= (double)ULONG_MAX
&& x
>= (double)0)
299 printf(format
, (unsigned long)x
);
302 } else if (longdata
) {
303 if (x
<= (double)LONG_MAX
&& x
>= (double)LONG_MIN
)
304 printf(format
, (long)x
);
307 } else if (chardata
|| (intdata
&& !nosign
)) {
308 if (x
<= (double)INT_MAX
&& x
>= (double)INT_MIN
)
309 printf(format
, (int)x
);
312 } else if (intdata
) {
313 if (x
<= (double)UINT_MAX
&& x
>= (double)0)
314 printf(format
, (unsigned int)x
);
321 fputs(sepstring
, stdout
);
329 fprintf(stderr
, "%s\n%s\n",
330 "usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
331 " [reps [begin [end [s]]]]");
341 for (p
= str
; *p
; p
++)
346 for (q
= ++p
; *p
; p
++)
356 int dot
, hash
, space
, sign
, numbers
= 0;
359 if (boring
) /* no need to bother */
361 for (p
= format
; *p
; p
++) /* look for '%' */
362 if (*p
== '%' && *(p
+1) != '%') /* leave %% alone */
364 sz
= sizeof(format
) - strlen(format
) - 1;
365 if (!*p
&& !chardata
) {
366 if (snprintf(p
, sz
, "%%.%df", prec
) >= (int)sz
)
367 errx(1, "-w word too long");
368 } else if (!*p
&& chardata
) {
369 if (strlcpy(p
, "%c", sz
) >= sz
)
370 errx(1, "-w word too long");
372 } else if (!*(p
+1)) {
374 errx(1, "-w word too long");
375 strcat(format
, "%"); /* cannot end in single '%' */
378 * Allow conversion format specifiers of the form
379 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
380 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
383 dot
= hash
= space
= sign
= numbers
= 0;
384 while (!isalpha(*p
)) {
388 } else if ((*p
== '#' && !(numbers
|dot
|sign
|space
|
390 (*p
== ' ' && !(numbers
|dot
|space
++)) ||
391 ((*p
== '+' || *p
== '-') && !(numbers
|dot
|sign
++))
392 || (*p
== '.' && !(dot
++)))
406 case 'o': case 'u': case 'x': case 'X':
407 intdata
= nosign
= 1;
419 intdata
= nosign
= 1;
423 if (!(intdata
| longdata
)) {
427 case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
430 case 'f': case 'e': case 'g': case 'E': case 'G':
437 errx(1, "illegal or unsupported format '%s'", p2
);
441 if (*p
== '%' && *(p
+1) && *(p
+1) != '%')
442 errx(1, "too many conversions");
443 else if (*p
== '%' && *(p
+1) == '%')
445 else if (*p
== '%' && !*(p
+1)) {