]>
git.saurik.com Git - apple/shell_cmds.git/blob - jot/jot.c
1 /* $NetBSD: jot.c,v 1.4 1997/10/19 03:34:49 lukem Exp $ */
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93";
46 __RCSID("$NetBSD: jot.c,v 1.4 1997/10/19 03:34:49 lukem Exp $");
50 * jot - print sequential or random data
52 * Author: John Kunze, Office of Comp. Affairs, UCB
68 #define isdefault(s) (strcmp((s), "-") == 0)
81 char sepstring
[BUFSIZ
] = "\n";
84 void error
__P((char *, char *));
85 void getargs
__P((int, char *[]));
86 void getformat
__P((void));
87 int getprec
__P((char *));
88 int main
__P((int, char **));
89 void putdata
__P((double, long));
104 *x
= (ender
- begin
) * (ender
> begin
? 1 : -1);
106 for (*i
= 1; *i
<= reps
|| infinity
; (*i
)++) {
107 *y
= (double) random() / INT_MAX
;
108 putdata(*y
* *x
+ begin
, reps
- *i
);
112 for (*i
= 1, *x
= begin
; *i
<= reps
|| infinity
; (*i
)++, *x
+= s
)
113 putdata(*x
, reps
- *i
);
124 unsigned int mask
= 0;
127 while (--ac
&& **++av
== '-' && !isdefault(*av
))
142 strcpy(format
, *av
+ 2);
144 error("Need context word after -w or -b", "");
146 strcpy(format
, *++av
);
150 strcpy(sepstring
, *av
+ 2);
152 error("Need string after -s", "");
154 strcpy(sepstring
, *++av
);
158 prec
= atoi(*av
+ 2);
160 error("Need number after -p", "");
164 error("Bad precision value", "");
167 error("Unknown option %s", *av
);
170 switch (ac
) { /* examine args right to left, falling thru cases */
172 if (!isdefault(av
[3])) {
173 if (!sscanf(av
[3], "%lf", &s
))
174 error("Bad s value: %s", av
[3]);
178 if (!isdefault(av
[2])) {
179 if (!sscanf(av
[2], "%lf", &ender
))
180 ender
= av
[2][strlen(av
[2])-1];
186 if (!isdefault(av
[1])) {
187 if (!sscanf(av
[1], "%lf", &begin
))
188 begin
= av
[1][strlen(av
[1])-1];
191 prec
= getprec(av
[1]);
192 if (n
> prec
) /* maximum precision */
196 if (!isdefault(av
[0])) {
197 if (!sscanf(av
[0], "%ld", &reps
))
198 error("Bad reps value: %s", av
[0]);
203 error("jot - print sequential or random data", "");
205 error("Too many arguments. What do you mean by %s?", av
[4]);
208 while (mask
) /* 4 bit mask has 1's where last 4 args were given */
209 switch (mask
) { /* fill in the 0's by default or computation */
245 reps
= (ender
- begin
+ s
) / s
;
247 error("Impossible stepsize", "");
259 s
= (randomize
? time(0) : STEP_DEF
);
266 error("Must specify begin if reps == 0", "");
267 begin
= ender
- reps
* s
+ s
;
271 s
= (randomize
? time(0) : STEP_DEF
);
278 ender
= begin
+ reps
* s
- s
;
285 error("Infinite sequences cannot be bounded",
290 s
= (ender
- begin
) / (reps
- 1);
293 case 017: /* if reps given and implied, */
294 if (!randomize
&& s
!= 0.0) {
295 long t
= (ender
- begin
+ s
) / s
;
297 error("Impossible stepsize", "");
298 if (t
< reps
) /* take lesser */
304 error("Bad mask", "");
318 if (boring
) /* repeated word */
319 printf("%s", format
);
320 else if (dox
) /* scalar */
325 fputs(sepstring
, stdout
);
334 "\nusage: jot [ options ] [ reps [ begin [ end [ s ] ] ] ]\n");
335 if (strncmp("jot - ", msg
, 6) == 0)
336 fprintf(stderr
, "Options:\n\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
338 "-c character data\n",
339 "-n no final newline\n",
340 "-b word repeated word\n",
341 "-w word context word\n",
342 "-s string data separator\n",
343 "-p precision number of characters\n");
359 for (q
= ++p
; *p
; p
++)
370 if (boring
) /* no need to bother */
372 for (p
= format
; *p
; p
++) /* look for '%' */
373 if (*p
== '%' && *(p
+1) != '%') /* leave %% alone */
375 if (!*p
&& !chardata
)
376 sprintf(p
, "%%.%df", prec
);
377 else if (!*p
&& chardata
) {
382 strcat(format
, "%"); /* cannot end in single '%' */
387 case 'f': case 'e': case 'g': case '%':
390 error("Cannot convert numeric data to strings", "");
392 /* case 'd': case 'o': case 'x': case 'D': case 'O': case 'X':
393 case 'c': case 'u': */