4 * Print a quotation from Zippy the Pinhead.
5 * Qux <Kaufman-David@Yale> March 6, 1986
7 * With dynamic memory allocation.
10 /* Synched up with: FSF 19.28. */
12 #define DONT_ENCAPSULATE
17 #include <../src/paths.h> /* For PATH_DATA. */
19 #if __STDC__ || defined(STDC_HEADERS)
23 #include <time.h> /* for time() */
30 #define YOW_FILE "yow.lines"
34 void setup_yow (FILE *fp);
37 #define rootrelativepath(rel) \
39 static char res[BUFSIZE], *p;\
40 strcpy (res, argv[0]);\
41 p = res + strlen (res);\
42 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
43 strcpy (p + 1, "../");\
49 main (int argc, char *argv[])
54 if (argc > 2 && !strcmp (argv[1], "-f"))
55 strcpy (file, argv[2]);
59 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
61 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
63 #else /* !PATH_DATA */
66 "%s: the location of the \"%s\" file was not supplied at compile-time.\n\
67 You must supply it with the -f command-line option.\n",
73 if ((fp = fopen(file, "r")) == NULL) {
78 /* initialize random seed */
79 srand((int) (getpid() + time((time_t *) 0)));
88 static long header_len;
90 #define AVG_LEN 40 /* average length of a quotation */
92 /* Sets len and header_len */
98 /* Get length of file */
99 /* Because the header (stuff before the first SEP) can be very long,
100 * thus biasing our search in favor of the first quotation in the file,
101 * we explicitly skip that. */
102 while ((c = getc(fp)) != SEP) {
104 fprintf(stderr, "File contains no separators.\n");
108 header_len = ftell(fp);
109 if (header_len > AVG_LEN)
110 header_len -= AVG_LEN; /* allow the first quotation to appear */
112 if (fseek(fp, 0L, 2) == -1) {
116 len = ftell(fp) - header_len;
120 /* go to a random place in the file and print the quotation there */
129 offset = rand() % len + header_len;
130 if (fseek(fp, offset, 0) == -1) {
135 /* Read until SEP, read next line, print it.
136 (Note that we will never print anything before the first separator.)
137 If we hit EOF looking for the first SEP, just recurse. */
138 while ((c = getc(fp)) != SEP)
144 /* Skip leading whitespace, then read in a quotation.
145 If we hit EOF before we find a non-whitespace char, recurse. */
146 while (isspace(c = getc(fp)))
154 buf = (char *) malloc(bufsize);
155 if (buf == (char *)0) {
156 fprintf(stderr, "can't allocate any memory\n");
161 while ((c = getc(fp)) != SEP && c != EOF) {
164 if (i == bufsize-1) {
165 /* Yow! Is this quotation too long yet? */
167 buf = (char *)realloc(buf, bufsize);
168 if (buf == (char *)0) {
169 fprintf(stderr, "can't allocate more memory\n");