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