9660858941c795e331d343ca24c46193b4789e94
[sxemacs] / lib-src / yow.c
1 /*
2  * yow.c
3  * 
4  * Print a quotation from Zippy the Pinhead.
5  * Qux <Kaufman-David@Yale> March 6, 1986
6  * 
7  * With dynamic memory allocation.
8  */
9
10 /* Synched up with: FSF 19.28. */
11
12 #define DONT_ENCAPSULATE
13 #include <config.h>
14
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <../src/sxe-paths.h>   /* For PATH_DATA.  */
18
19 #if __STDC__ || defined(STDC_HEADERS)
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <time.h>               /* for time() */
24 #endif
25
26 #define BUFSIZE  80
27 #define SEP      '\0'
28
29 #ifndef YOW_FILE
30 #define YOW_FILE "yow.lines"
31 #endif
32
33 void yow(FILE * fp);
34 void setup_yow(FILE * fp);
35
36 int main(int argc, char *argv[])
37 {
38         FILE *fp;
39         char file[BUFSIZ];
40
41         if (argc > 2 && !strcmp(argv[1], "-f"))
42                 strcpy(file, argv[2]);
43         else
44 #ifdef PATH_DATA
45 #ifdef vms
46                 sprintf(file, "%s%s", PATH_DATA, YOW_FILE);
47 #else
48                 sprintf(file, "%s/%s", PATH_DATA, YOW_FILE);
49 #endif
50 #else                           /* !PATH_DATA */
51         {
52                 fprintf(stderr,
53                         "%s: the location of the \"%s\" file was not supplied at compile-time.\n\
54         You must supply it with the -f command-line option.\n",
55                         argv[0], YOW_FILE);
56                 exit(1);
57         }
58 #endif
59
60         if ((fp = fopen(file, "r")) == NULL) {
61                 perror(file);
62                 exit(1);
63         }
64
65         /* initialize random seed */
66         srand((int)(getpid() + time((time_t *) 0)));
67
68         setup_yow(fp);
69         yow(fp);
70         fclose(fp);
71         return 0;
72 }
73
74 static long len = -1;
75 static long header_len;
76
77 #define AVG_LEN 40              /* average length of a quotation */
78
79 /* Sets len and header_len */
80 void setup_yow(FILE * fp)
81 {
82         int c;
83
84         /* Get length of file */
85         /* Because the header (stuff before the first SEP) can be very long,
86          * thus biasing our search in favor of the first quotation in the file,
87          * we explicitly skip that. */
88         while ((c = getc(fp)) != SEP) {
89                 if (c == EOF) {
90                         fprintf(stderr, "File contains no separators.\n");
91                         exit(2);
92                 }
93         }
94         header_len = ftell(fp);
95         if (header_len > AVG_LEN)
96                 header_len -= AVG_LEN;  /* allow the first quotation to appear */
97
98         if (fseek(fp, 0L, 2) == -1) {
99                 perror("fseek 1");
100                 exit(1);
101         }
102         len = ftell(fp) - header_len;
103 }
104
105 /* go to a random place in the file and print the quotation there */
106 void yow(FILE * fp)
107 {
108         long offset;
109         int c, i = 0;
110         char *buf;
111         int bufsize;
112
113         offset = rand() % len + header_len;
114         if (fseek(fp, offset, 0) == -1) {
115                 perror("fseek 2");
116                 exit(1);
117         }
118
119         /* Read until SEP, read next line, print it.
120            (Note that we will never print anything before the first separator.)
121            If we hit EOF looking for the first SEP, just recurse. */
122         while ((c = getc(fp)) != SEP)
123                 if (c == EOF) {
124                         yow(fp);
125                         return;
126                 }
127
128         /* Skip leading whitespace, then read in a quotation.
129            If we hit EOF before we find a non-whitespace char, recurse. */
130         while (isspace(c = getc(fp))) ;
131         if (c == EOF) {
132                 yow(fp);
133                 return;
134         }
135
136         bufsize = BUFSIZE;
137         buf = (char *)malloc(bufsize);
138         if (buf == (char *)0) {
139                 fprintf(stderr, "can't allocate any memory\n");
140                 exit(3);
141         }
142
143         buf[i++] = c;
144         while ((c = getc(fp)) != SEP && c != EOF) {
145                 buf[i++] = c;
146
147                 if (i == bufsize - 1) {
148                         /* Yow! Is this quotation too long yet? */
149                         bufsize *= 2;
150                         buf = (char *)realloc(buf, bufsize);
151                         if (buf == (char *)0) {
152                                 fprintf(stderr, "can't allocate more memory\n");
153                                 exit(3);
154                         }
155                 }
156         }
157         buf[i++] = 0;
158         printf("%s\n", buf);
159 }