Trailing whitespace deleted.
[bpt/emacs.git] / 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 * This file is in the public domain because the author published it
8 * with no copyright notice before the US signed the Bern Convention.
9 *
10 * With dynamic memory allocation.
11 */
12
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdio.h>
18 #include <ctype.h>
19 #ifdef TIME_WITH_SYS_TIME
20 #include <sys/time.h>
21 #include <time.h>
22 #else
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #else
26 #include <time.h>
27 #endif
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include "epaths.h" /* For PATH_DATA. */
33
34 #define BUFSIZE 80
35 #define SEP '\0'
36
37 #ifndef YOW_FILE
38 #define YOW_FILE "yow.lines"
39 #endif
40
41 #ifdef MSDOS
42 #define rootrelativepath(rel) \
43 ({\
44 static char res[BUFSIZE], *p;\
45 strcpy (res, argv[0]);\
46 p = res + strlen (res);\
47 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
48 strcpy (p + 1, "../");\
49 strcpy (p + 4, rel);\
50 &res;})
51 #endif
52
53 #ifndef HAVE_STDLIB_H
54 char *malloc __P ((size_t size))), *realloc __P ((POINTER_TYPE *ptr, size_t size));
55 #endif
56
57 void yow();
58 void setup_yow();
59
60 int
61 main (argc, argv)
62 int argc;
63 char *argv[];
64 {
65 FILE *fp;
66 char file[BUFSIZ];
67
68 if (argc > 2 && !strcmp (argv[1], "-f"))
69 strcpy (file, argv[2]);
70 else
71 #ifdef vms
72 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
73 #else
74 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
75 #endif
76
77 if ((fp = fopen(file, "r")) == NULL) {
78 fprintf(stderr, "yow: ");
79 perror(file);
80 exit(1);
81 }
82
83 /* initialize random seed */
84 srand((int) (getpid() + time((time_t *) 0)));
85
86 setup_yow(fp);
87 yow(fp);
88 fclose(fp);
89 return 0;
90 }
91
92 static long len = -1;
93 static long header_len;
94
95 #define AVG_LEN 40 /* average length of a quotation */
96
97 /* Sets len and header_len */
98 void
99 setup_yow(fp)
100 FILE *fp;
101 {
102 int c;
103
104 /* Get length of file */
105 /* Because the header (stuff before the first SEP) can be very long,
106 * thus biasing our search in favor of the first quotation in the file,
107 * we explicitly skip that. */
108 while ((c = getc(fp)) != SEP) {
109 if (c == EOF) {
110 fprintf(stderr, "yow: file contains no separators\n");
111 exit(2);
112 }
113 }
114 header_len = ftell(fp);
115 if (header_len > AVG_LEN)
116 header_len -= AVG_LEN; /* allow the first quotation to appear */
117
118 if (fseek(fp, 0L, 2) == -1) {
119 perror("yow");
120 exit(1);
121 }
122 len = ftell(fp) - header_len;
123 }
124
125
126 /* go to a random place in the file and print the quotation there */
127 void
128 yow (fp)
129 FILE *fp;
130 {
131 long offset;
132 int c, i = 0;
133 char *buf;
134 unsigned int bufsize;
135
136 offset = rand() % len + header_len;
137 if (fseek(fp, offset, 0) == -1) {
138 perror("yow");
139 exit(1);
140 }
141
142 /* Read until SEP, read next line, print it.
143 (Note that we will never print anything before the first separator.)
144 If we hit EOF looking for the first SEP, just recurse. */
145 while ((c = getc(fp)) != SEP)
146 if (c == EOF) {
147 yow(fp);
148 return;
149 }
150
151 /* Skip leading whitespace, then read in a quotation.
152 If we hit EOF before we find a non-whitespace char, recurse. */
153 while (isspace(c = getc(fp)))
154 ;
155 if (c == EOF) {
156 yow(fp);
157 return;
158 }
159
160 bufsize = BUFSIZE;
161 buf = malloc(bufsize);
162 if (buf == (char *)0) {
163 fprintf(stderr, "yow: virtual memory exhausted\n");
164 exit (3);
165 }
166
167 buf[i++] = c;
168 while ((c = getc(fp)) != SEP && c != EOF) {
169 buf[i++] = c;
170
171 if (i == bufsize-1) {
172 /* Yow! Is this quotation too long yet? */
173 bufsize *= 2;
174 buf = realloc(buf, bufsize);
175 if (buf == (char *)0) {
176 fprintf(stderr, "yow: virtual memory exhausted\n");
177 exit (3);
178 }
179 }
180 }
181 buf[i++] = 0;
182 printf("%s\n", buf);
183 }
184