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