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.
15 #include <../src/epaths.h> /* For PATH_DATA. */
21 #define YOW_FILE "yow.lines"
25 #define rootrelativepath(rel) \
27 static char res[BUFSIZE], *p;\
28 strcpy (res, argv[0]);\
29 p = res + strlen (res);\
30 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
31 strcpy (p + 1, "../");\
36 char *malloc(), *realloc();
49 if (argc
> 2 && !strcmp (argv
[1], "-f"))
50 strcpy (file
, argv
[2]);
53 sprintf (file
, "%s%s", PATH_DATA
, YOW_FILE
);
55 sprintf (file
, "%s/%s", PATH_DATA
, YOW_FILE
);
58 if ((fp
= fopen(file
, "r")) == NULL
) {
59 fprintf(stderr
, "yow: ");
64 /* initialize random seed */
65 srand((int) (getpid() + time((long *) 0)));
74 static long header_len
;
76 #define AVG_LEN 40 /* average length of a quotation */
78 /* Sets len and header_len */
85 /* Get length of file */
86 /* Because the header (stuff before the first SEP) can be very long,
87 * thus biasing our search in favor of the first quotation in the file,
88 * we explicitly skip that. */
89 while ((c
= getc(fp
)) != SEP
) {
91 fprintf(stderr
, "yow: file contains no separators\n");
95 header_len
= ftell(fp
);
96 if (header_len
> AVG_LEN
)
97 header_len
-= AVG_LEN
; /* allow the first quotation to appear */
99 if (fseek(fp
, 0L, 2) == -1) {
103 len
= ftell(fp
) - header_len
;
107 /* go to a random place in the file and print the quotation there */
115 unsigned int bufsize
;
117 offset
= rand() % len
+ header_len
;
118 if (fseek(fp
, offset
, 0) == -1) {
123 /* Read until SEP, read next line, print it.
124 (Note that we will never print anything before the first separator.)
125 If we hit EOF looking for the first SEP, just recurse. */
126 while ((c
= getc(fp
)) != SEP
)
132 /* Skip leading whitespace, then read in a quotation.
133 If we hit EOF before we find a non-whitespace char, recurse. */
134 while (isspace(c
= getc(fp
)))
142 buf
= malloc(bufsize
);
143 if (buf
== (char *)0) {
144 fprintf(stderr
, "yow: virtual memory exhausted\n");
149 while ((c
= getc(fp
)) != SEP
&& c
!= EOF
) {
152 if (i
== bufsize
-1) {
153 /* Yow! Is this quotation too long yet? */
155 buf
= realloc(buf
, bufsize
);
156 if (buf
== (char *)0) {
157 fprintf(stderr
, "yow: virtual memory exhausted\n");