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 */
99 /* Get length of file */
100 /* Because the header (stuff before the first SEP) can be very long,
101 * thus biasing our search in favor of the first quotation in the file,
102 * we explicitly skip that. */
103 while ((c = getc(fp)) != SEP) {
105 fprintf(stderr, "File contains no separators.\n");
109 header_len = ftell(fp);
110 if (header_len > AVG_LEN)
111 header_len -= AVG_LEN; /* allow the first quotation to appear */
113 if (fseek(fp, 0L, 2) == -1) {
117 len = ftell(fp) - header_len;
121 /* go to a random place in the file and print the quotation there */
129 unsigned int bufsize;
131 offset = rand() % len + header_len;
132 if (fseek(fp, offset, 0) == -1) {
137 /* Read until SEP, read next line, print it.
138 (Note that we will never print anything before the first separator.)
139 If we hit EOF looking for the first SEP, just recurse. */
140 while ((c = getc(fp)) != SEP)
146 /* Skip leading whitespace, then read in a quotation.
147 If we hit EOF before we find a non-whitespace char, recurse. */
148 while (isspace(c = getc(fp)))
156 buf = (char *) malloc(bufsize);
157 if (buf == (char *)0) {
158 fprintf(stderr, "can't allocate any memory\n");
163 while ((c = getc(fp)) != SEP && c != EOF) {
166 if (i == bufsize-1) {
167 /* Yow! Is this quotation too long yet? */
169 buf = (char *)realloc(buf, bufsize);
170 if (buf == (char *)0) {
171 fprintf(stderr, "can't allocate more memory\n");