4 * Print a quotation from Zippy the Pinhead.
5 * Qux <Kaufman-David@Yale> March 6, 1986
7 * With dynamic memory allocation.
12 #include <../src/paths.h> /* For PATH_DATA. */
18 #define YOW_FILE "yow.lines"
27 void yow(), setup_yow();
29 if (argc
> 2 && !strcmp (argv
[1], "-f"))
30 strcpy (file
, argv
[2]);
33 sprintf (file
, "%s%s", PATH_DATA
, YOW_FILE
);
35 sprintf (file
, "%s/%s", PATH_DATA
, YOW_FILE
);
38 if ((fp
= fopen(file
, "r")) == NULL
) {
43 /* initialize random seed */
44 srand((int) (getpid() + time((long *) 0)));
53 static long header_len
;
55 #define AVG_LEN 40 /* average length of a quotation */
57 /* Sets len and header_len */
64 /* Get length of file */
65 /* Because the header (stuff before the first SEP) can be very long,
66 * thus biasing our search in favor of the first quotation in the file,
67 * we explicitly skip that. */
68 while ((c
= getc(fp
)) != SEP
) {
70 fprintf(stderr
, "File contains no separators.\n");
74 header_len
= ftell(fp
);
75 if (header_len
> AVG_LEN
)
76 header_len
-= AVG_LEN
; /* allow the first quotation to appear */
78 if (fseek(fp
, 0L, 2) == -1) {
82 len
= ftell(fp
) - header_len
;
86 /* go to a random place in the file and print the quotation there */
95 char *malloc(), *realloc();
97 offset
= rand() % len
+ header_len
;
98 if (fseek(fp
, offset
, 0) == -1) {
103 /* Read until SEP, read next line, print it.
104 (Note that we will never print anything before the first separator.)
105 If we hit EOF looking for the first SEP, just recurse. */
106 while ((c
= getc(fp
)) != SEP
)
112 /* Skip leading whitespace, then read in a quotation.
113 If we hit EOF before we find a non-whitespace char, recurse. */
114 while (isspace(c
= getc(fp
)))
122 buf
= malloc(bufsize
);
123 if (buf
== (char *)0) {
124 fprintf(stderr
, "can't allocate any memory\n");
129 while ((c
= getc(fp
)) != SEP
&& c
!= EOF
) {
132 if (i
== bufsize
-1) {
133 /* Yow! Is this quotation too long yet? */
135 buf
= realloc(buf
, bufsize
);
136 if (buf
== (char *)0) {
137 fprintf(stderr
, "can't allocate more memory\n");