42
|
1 /*
|
|
2 * yow.c
|
49600
|
3 *
|
12
|
4 * Print a quotation from Zippy the Pinhead.
|
|
5 * Qux <Kaufman-David@Yale> March 6, 1986
|
11183
|
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.
|
49600
|
9 *
|
42
|
10 * With dynamic memory allocation.
|
12
|
11 */
|
|
12
|
42412
|
13 #ifdef HAVE_CONFIG_H
|
|
14 #include <config.h>
|
|
15 #endif
|
|
16
|
42
|
17 #include <stdio.h>
|
|
18 #include <ctype.h>
|
42131
|
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. */
|
42
|
33
|
|
34 #define BUFSIZE 80
|
12
|
35 #define SEP '\0'
|
42
|
36
|
|
37 #ifndef YOW_FILE
|
12
|
38 #define YOW_FILE "yow.lines"
|
42
|
39 #endif
|
12
|
40
|
7820
|
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
|
10198
|
53 void yow();
|
|
54 void setup_yow();
|
|
55
|
9491
|
56 int
|
12
|
57 main (argc, argv)
|
|
58 int argc;
|
|
59 char *argv[];
|
|
60 {
|
|
61 FILE *fp;
|
|
62 char file[BUFSIZ];
|
|
63
|
|
64 if (argc > 2 && !strcmp (argv[1], "-f"))
|
|
65 strcpy (file, argv[2]);
|
|
66 else
|
|
67 #ifdef vms
|
443
|
68 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
|
12
|
69 #else
|
443
|
70 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
|
12
|
71 #endif
|
|
72
|
|
73 if ((fp = fopen(file, "r")) == NULL) {
|
10198
|
74 fprintf(stderr, "yow: ");
|
12
|
75 perror(file);
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
76 exit(EXIT_FAILURE);
|
12
|
77 }
|
|
78
|
|
79 /* initialize random seed */
|
42151
|
80 srand((int) (getpid() + time((time_t *) 0)));
|
12
|
81
|
42
|
82 setup_yow(fp);
|
12
|
83 yow(fp);
|
|
84 fclose(fp);
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
85 return EXIT_SUCCESS;
|
12
|
86 }
|
|
87
|
42
|
88 static long len = -1;
|
|
89 static long header_len;
|
|
90
|
|
91 #define AVG_LEN 40 /* average length of a quotation */
|
|
92
|
|
93 /* Sets len and header_len */
|
|
94 void
|
|
95 setup_yow(fp)
|
|
96 FILE *fp;
|
|
97 {
|
|
98 int c;
|
|
99
|
|
100 /* Get length of file */
|
|
101 /* Because the header (stuff before the first SEP) can be very long,
|
|
102 * thus biasing our search in favor of the first quotation in the file,
|
|
103 * we explicitly skip that. */
|
|
104 while ((c = getc(fp)) != SEP) {
|
|
105 if (c == EOF) {
|
10198
|
106 fprintf(stderr, "yow: file contains no separators\n");
|
42
|
107 exit(2);
|
|
108 }
|
|
109 }
|
|
110 header_len = ftell(fp);
|
|
111 if (header_len > AVG_LEN)
|
|
112 header_len -= AVG_LEN; /* allow the first quotation to appear */
|
49600
|
113
|
42
|
114 if (fseek(fp, 0L, 2) == -1) {
|
10198
|
115 perror("yow");
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
116 exit(EXIT_FAILURE);
|
42
|
117 }
|
|
118 len = ftell(fp) - header_len;
|
|
119 }
|
|
120
|
|
121
|
|
122 /* go to a random place in the file and print the quotation there */
|
12
|
123 void
|
|
124 yow (fp)
|
|
125 FILE *fp;
|
|
126 {
|
|
127 long offset;
|
|
128 int c, i = 0;
|
42
|
129 char *buf;
|
|
130 unsigned int bufsize;
|
12
|
131
|
42
|
132 offset = rand() % len + header_len;
|
12
|
133 if (fseek(fp, offset, 0) == -1) {
|
10198
|
134 perror("yow");
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
135 exit(EXIT_FAILURE);
|
12
|
136 }
|
|
137
|
|
138 /* Read until SEP, read next line, print it.
|
3591
|
139 (Note that we will never print anything before the first separator.)
|
12
|
140 If we hit EOF looking for the first SEP, just recurse. */
|
|
141 while ((c = getc(fp)) != SEP)
|
|
142 if (c == EOF) {
|
|
143 yow(fp);
|
|
144 return;
|
|
145 }
|
|
146
|
|
147 /* Skip leading whitespace, then read in a quotation.
|
|
148 If we hit EOF before we find a non-whitespace char, recurse. */
|
|
149 while (isspace(c = getc(fp)))
|
|
150 ;
|
|
151 if (c == EOF) {
|
|
152 yow(fp);
|
|
153 return;
|
|
154 }
|
42
|
155
|
|
156 bufsize = BUFSIZE;
|
49802
|
157 buf = (char *) malloc(bufsize);
|
42
|
158 if (buf == (char *)0) {
|
10198
|
159 fprintf(stderr, "yow: virtual memory exhausted\n");
|
42
|
160 exit (3);
|
|
161 }
|
|
162
|
12
|
163 buf[i++] = c;
|
|
164 while ((c = getc(fp)) != SEP && c != EOF) {
|
|
165 buf[i++] = c;
|
49600
|
166
|
42
|
167 if (i == bufsize-1) {
|
12
|
168 /* Yow! Is this quotation too long yet? */
|
42
|
169 bufsize *= 2;
|
49802
|
170 buf = (char *) realloc(buf, bufsize);
|
42
|
171 if (buf == (char *)0) {
|
10198
|
172 fprintf(stderr, "yow: virtual memory exhausted\n");
|
42
|
173 exit (3);
|
|
174 }
|
|
175 }
|
12
|
176 }
|
|
177 buf[i++] = 0;
|
|
178 printf("%s\n", buf);
|
|
179 }
|
|
180
|
52401
|
181 /* arch-tag: e40fc0df-bafb-4001-af24-5c883d1c685e
|
|
182 (do not change this comment) */
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
183
|
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
184 /* yow.c ends here */
|