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.
19 #ifdef TIME_WITH_SYS_TIME
23 #ifdef HAVE_SYS_TIME_H
32 #include "epaths.h" /* For PATH_DATA. */
38 #define YOW_FILE "yow.lines"
42 #define rootrelativepath(rel) \
44 static char res[BUFSIZE], *p;\
45 strcpy (res, argv[0]);\
46 p = res + strlen (res);\
47 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
48 strcpy (p + 1, "../");\
64 if (argc
> 2 && !strcmp (argv
[1], "-f"))
65 strcpy (file
, argv
[2]);
68 sprintf (file
, "%s%s", PATH_DATA
, YOW_FILE
);
70 sprintf (file
, "%s/%s", PATH_DATA
, YOW_FILE
);
73 if ((fp
= fopen(file
, "r")) == NULL
) {
74 fprintf(stderr
, "yow: ");
79 /* initialize random seed */
80 srand((int) (getpid() + time((time_t *) 0)));
89 static long header_len
;
91 #define AVG_LEN 40 /* average length of a quotation */
93 /* Sets len and header_len */
100 /* Get length of file */
101 /* Because the header (stuff before the first SEP) can be very long,
102 * thus biasing our search in favor of the first quotation in the file,
103 * we explicitly skip that. */
104 while ((c
= getc(fp
)) != SEP
) {
106 fprintf(stderr
, "yow: file contains no separators\n");
110 header_len
= ftell(fp
);
111 if (header_len
> AVG_LEN
)
112 header_len
-= AVG_LEN
; /* allow the first quotation to appear */
114 if (fseek(fp
, 0L, 2) == -1) {
118 len
= ftell(fp
) - header_len
;
122 /* go to a random place in the file and print the quotation there */
130 unsigned int bufsize
;
132 offset
= rand() % len
+ header_len
;
133 if (fseek(fp
, offset
, 0) == -1) {
138 /* Read until SEP, read next line, print it.
139 (Note that we will never print anything before the first separator.)
140 If we hit EOF looking for the first SEP, just recurse. */
141 while ((c
= getc(fp
)) != SEP
)
147 /* Skip leading whitespace, then read in a quotation.
148 If we hit EOF before we find a non-whitespace char, recurse. */
149 while (isspace(c
= getc(fp
)))
157 buf
= (char *) malloc(bufsize
);
158 if (buf
== (char *)0) {
159 fprintf(stderr
, "yow: virtual memory exhausted\n");
164 while ((c
= getc(fp
)) != SEP
&& c
!= EOF
) {
167 if (i
== bufsize
-1) {
168 /* Yow! Is this quotation too long yet? */
170 buf
= (char *) realloc(buf
, bufsize
);
171 if (buf
== (char *)0) {
172 fprintf(stderr
, "yow: virtual memory exhausted\n");
181 /* arch-tag: e40fc0df-bafb-4001-af24-5c883d1c685e
182 (do not change this comment) */
184 /* yow.c ends here */