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 #include <../src/config.h>
16 #include <../src/paths.h> /* For PATH_DATA. */
18 #if __STDC__ || defined(STDC_HEADERS)
22 #include <time.h> /* for time() */
29 #define YOW_FILE "yow.lines"
33 void setup_yow (FILE *fp);
36 #define rootrelativepath(rel) \
38 static char res[BUFSIZE], *p;\
39 strcpy (res, argv[0]);\
40 p = res + strlen (res);\
41 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
42 strcpy (p + 1, "../");\
48 main (int argc, char *argv[])
53 if (argc > 2 && !strcmp (argv[1], "-f"))
54 strcpy (file, argv[2]);
58 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
60 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
62 #else /* !PATH_DATA */
65 "%s: the location of the \"%s\" file was not supplied at compile-time.\n\
66 You must supply it with the -f command-line option.\n",
72 if ((fp = fopen(file, "r")) == NULL) {
77 /* initialize random seed */
78 srand((int) (getpid() + time((time_t *) 0)));
87 static long header_len;
89 #define AVG_LEN 40 /* average length of a quotation */
91 /* 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 */
128 unsigned int bufsize;
130 offset = rand() % len + header_len;
131 if (fseek(fp, offset, 0) == -1) {
136 /* Read until SEP, read next line, print it.
137 (Note that we will never print anything before the first separator.)
138 If we hit EOF looking for the first SEP, just recurse. */
139 while ((c = getc(fp)) != SEP)
145 /* Skip leading whitespace, then read in a quotation.
146 If we hit EOF before we find a non-whitespace char, recurse. */
147 while (isspace(c = getc(fp)))
155 buf = (char *) malloc(bufsize);
156 if (buf == (char *)0) {
157 fprintf(stderr, "can't allocate any memory\n");
162 while ((c = getc(fp)) != SEP && c != EOF) {
165 if (i == bufsize-1) {
166 /* Yow! Is this quotation too long yet? */
168 buf = (char *)realloc(buf, bufsize);
169 if (buf == (char *)0) {
170 fprintf(stderr, "can't allocate more memory\n");