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