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