4 * Print a quotation from Zippy the Pinhead.
5 * Qux <Kaufman-David@Yale> March 6, 1986
7 * This file is in the public domain because the author published it
8 * with no copyright notice before the US signed the Bern Convention.
10 * With dynamic memory allocation.
22 #ifdef TIME_WITH_SYS_TIME
26 #ifdef HAVE_SYS_TIME_H
35 #include "epaths.h" /* For PATH_DATA. */
41 #define YOW_FILE "yow.lines"
45 #define rootrelativepath(rel) \
47 static char res[BUFSIZE], *p;\
48 strcpy (res, argv[0]);\
49 p = res + strlen (res);\
50 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
51 strcpy (p + 1, "../");\
57 char *malloc
__P ((size_t size
))), *realloc
__P ((POINTER_TYPE
*ptr
, size_t size
));
71 if (argc
> 2 && !strcmp (argv
[1], "-f"))
72 strcpy (file
, argv
[2]);
75 sprintf (file
, "%s%s", PATH_DATA
, YOW_FILE
);
77 sprintf (file
, "%s/%s", PATH_DATA
, YOW_FILE
);
80 if ((fp
= fopen(file
, "r")) == NULL
) {
81 fprintf(stderr
, "yow: ");
86 /* initialize random seed */
87 srand((int) (getpid() + time((time_t *) 0)));
96 static long header_len
;
98 #define AVG_LEN 40 /* average length of a quotation */
100 /* Sets len and header_len */
107 /* Get length of file */
108 /* Because the header (stuff before the first SEP) can be very long,
109 * thus biasing our search in favor of the first quotation in the file,
110 * we explicitly skip that. */
111 while ((c
= getc(fp
)) != SEP
) {
113 fprintf(stderr
, "yow: file contains no separators\n");
117 header_len
= ftell(fp
);
118 if (header_len
> AVG_LEN
)
119 header_len
-= AVG_LEN
; /* allow the first quotation to appear */
121 if (fseek(fp
, 0L, 2) == -1) {
125 len
= ftell(fp
) - header_len
;
129 /* go to a random place in the file and print the quotation there */
137 unsigned int bufsize
;
139 offset
= rand() % len
+ header_len
;
140 if (fseek(fp
, offset
, 0) == -1) {
145 /* Read until SEP, read next line, print it.
146 (Note that we will never print anything before the first separator.)
147 If we hit EOF looking for the first SEP, just recurse. */
148 while ((c
= getc(fp
)) != SEP
)
154 /* Skip leading whitespace, then read in a quotation.
155 If we hit EOF before we find a non-whitespace char, recurse. */
156 while (isspace(c
= getc(fp
)))
164 buf
= malloc(bufsize
);
165 if (buf
== (char *)0) {
166 fprintf(stderr
, "yow: virtual memory exhausted\n");
171 while ((c
= getc(fp
)) != SEP
&& c
!= EOF
) {
174 if (i
== bufsize
-1) {
175 /* Yow! Is this quotation too long yet? */
177 buf
= realloc(buf
, bufsize
);
178 if (buf
== (char *)0) {
179 fprintf(stderr
, "yow: virtual memory exhausted\n");