0
|
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 #include <config.h>
|
|
13
|
|
14 #include <stdio.h>
|
|
15 #include <ctype.h>
|
|
16 #include <../src/paths.h> /* For PATH_DATA. */
|
|
17
|
|
18 #if __STDC__ || defined(STDC_HEADERS)
|
|
19 #include <stdlib.h>
|
|
20 #include <unistd.h>
|
|
21 #include <string.h>
|
|
22 #include <time.h> /* for time() */
|
|
23 #endif
|
|
24
|
|
25 #define BUFSIZE 80
|
|
26 #define SEP '\0'
|
|
27
|
|
28 #ifndef YOW_FILE
|
|
29 #define YOW_FILE "yow.lines"
|
|
30 #endif
|
|
31
|
|
32 void yow (FILE *fp);
|
|
33 void setup_yow (FILE *fp);
|
|
34
|
|
35 #ifdef MSDOS
|
|
36 #define rootrelativepath(rel) \
|
|
37 ({\
|
|
38 static char res[BUFSIZE], *p;\
|
|
39 strcpy (res, argv[0]);\
|
|
40 p = res + strlen (res);\
|
|
41 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
|
|
42 strcpy (p + 1, "../");\
|
|
43 strcpy (p + 4, rel);\
|
|
44 &res;})
|
|
45 #endif
|
|
46
|
|
47 void
|
|
48 main (argc, argv)
|
|
49 int argc;
|
|
50 char *argv[];
|
|
51 {
|
|
52 FILE *fp;
|
|
53 char file[BUFSIZ];
|
|
54
|
|
55 if (argc > 2 && !strcmp (argv[1], "-f"))
|
|
56 strcpy (file, argv[2]);
|
|
57 else
|
|
58 #ifdef PATH_DATA
|
|
59 #ifdef vms
|
|
60 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
|
|
61 #else
|
|
62 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
|
|
63 #endif
|
|
64 #else /* !PATH_DATA */
|
|
65 {
|
|
66 fprintf (stderr,
|
|
67 "%s: the location of the \"%s\" file was not supplied at compile-time.\n\
|
|
68 You must supply it with the -f command-line option.\n",
|
|
69 argv[0], YOW_FILE);
|
|
70 exit (1);
|
|
71 }
|
|
72 #endif
|
|
73
|
|
74 if ((fp = fopen(file, "r")) == NULL) {
|
|
75 perror(file);
|
|
76 exit(1);
|
|
77 }
|
|
78
|
|
79 /* initialize random seed */
|
|
80 srand((int) (getpid() + time((time_t *) 0)));
|
|
81
|
|
82 setup_yow(fp);
|
|
83 yow(fp);
|
|
84 fclose(fp);
|
|
85 exit(0);
|
|
86 }
|
|
87
|
|
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) {
|
|
106 fprintf(stderr, "File contains no separators.\n");
|
|
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 */
|
|
113
|
|
114 if (fseek(fp, 0L, 2) == -1) {
|
|
115 perror("fseek 1");
|
|
116 exit(1);
|
|
117 }
|
|
118 len = ftell(fp) - header_len;
|
|
119 }
|
|
120
|
|
121
|
|
122 /* go to a random place in the file and print the quotation there */
|
|
123 void
|
|
124 yow (fp)
|
|
125 FILE *fp;
|
|
126 {
|
|
127 long offset;
|
|
128 int c, i = 0;
|
|
129 char *buf;
|
|
130 unsigned int bufsize;
|
|
131
|
|
132 offset = rand() % len + header_len;
|
|
133 if (fseek(fp, offset, 0) == -1) {
|
|
134 perror("fseek 2");
|
|
135 exit(1);
|
|
136 }
|
|
137
|
|
138 /* Read until SEP, read next line, print it.
|
|
139 (Note that we will never print anything before the first separator.)
|
|
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 }
|
|
155
|
|
156 bufsize = BUFSIZE;
|
|
157 buf = (char *) malloc(bufsize);
|
|
158 if (buf == (char *)0) {
|
|
159 fprintf(stderr, "can't allocate any memory\n");
|
|
160 exit (3);
|
|
161 }
|
|
162
|
|
163 buf[i++] = c;
|
|
164 while ((c = getc(fp)) != SEP && c != EOF) {
|
|
165 buf[i++] = c;
|
|
166
|
|
167 if (i == bufsize-1) {
|
|
168 /* Yow! Is this quotation too long yet? */
|
|
169 bufsize *= 2;
|
|
170 buf = (char *)realloc(buf, bufsize);
|
|
171 if (buf == (char *)0) {
|
|
172 fprintf(stderr, "can't allocate more memory\n");
|
|
173 exit (3);
|
|
174 }
|
|
175 }
|
|
176 }
|
|
177 buf[i++] = 0;
|
|
178 printf("%s\n", buf);
|
|
179 }
|
|
180
|