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