Mercurial > emacs
annotate lib-src/yow.c @ 8582:9f0d8d29d122
(LD_SWITCH_MACHINE): Use full path to ibmrs6000.inp.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 22 Aug 1994 23:18:42 +0000 |
parents | 128950f29e66 |
children | dd3b83e4ceb0 |
rev | line source |
---|---|
42 | 1 /* |
2 * yow.c | |
12 | 3 * |
4 * Print a quotation from Zippy the Pinhead. | |
5 * Qux <Kaufman-David@Yale> March 6, 1986 | |
6 * | |
42 | 7 * With dynamic memory allocation. |
12 | 8 */ |
9 | |
42 | 10 #include <stdio.h> |
11 #include <ctype.h> | |
4702
2641ab559aae
Include <src/paths.h>, instead "src/paths.h".
Roland McGrath <roland@gnu.org>
parents:
3591
diff
changeset
|
12 #include <../src/paths.h> /* For PATH_DATA. */ |
42 | 13 |
14 #define BUFSIZE 80 | |
12 | 15 #define SEP '\0' |
42 | 16 |
17 #ifndef YOW_FILE | |
12 | 18 #define YOW_FILE "yow.lines" |
42 | 19 #endif |
12 | 20 |
7820
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
21 #ifdef MSDOS |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
22 #define rootrelativepath(rel) \ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
23 ({\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
24 static char res[BUFSIZE], *p;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
25 strcpy (res, argv[0]);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
26 p = res + strlen (res);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
27 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
28 strcpy (p + 1, "../");\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
29 strcpy (p + 4, rel);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
30 &res;}) |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
31 #endif |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
32 |
12 | 33 main (argc, argv) |
34 int argc; | |
35 char *argv[]; | |
36 { | |
37 FILE *fp; | |
38 char file[BUFSIZ]; | |
42 | 39 void yow(), setup_yow(); |
12 | 40 |
41 if (argc > 2 && !strcmp (argv[1], "-f")) | |
42 strcpy (file, argv[2]); | |
43 else | |
44 #ifdef vms | |
443 | 45 sprintf (file, "%s%s", PATH_DATA, YOW_FILE); |
12 | 46 #else |
443 | 47 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); |
12 | 48 #endif |
49 | |
50 if ((fp = fopen(file, "r")) == NULL) { | |
51 perror(file); | |
52 exit(1); | |
53 } | |
54 | |
55 /* initialize random seed */ | |
56 srand((int) (getpid() + time((long *) 0))); | |
57 | |
42 | 58 setup_yow(fp); |
12 | 59 yow(fp); |
60 fclose(fp); | |
61 exit(0); | |
62 } | |
63 | |
42 | 64 static long len = -1; |
65 static long header_len; | |
66 | |
67 #define AVG_LEN 40 /* average length of a quotation */ | |
68 | |
69 /* Sets len and header_len */ | |
70 void | |
71 setup_yow(fp) | |
72 FILE *fp; | |
73 { | |
74 int c; | |
75 | |
76 /* Get length of file */ | |
77 /* Because the header (stuff before the first SEP) can be very long, | |
78 * thus biasing our search in favor of the first quotation in the file, | |
79 * we explicitly skip that. */ | |
80 while ((c = getc(fp)) != SEP) { | |
81 if (c == EOF) { | |
82 fprintf(stderr, "File contains no separators.\n"); | |
83 exit(2); | |
84 } | |
85 } | |
86 header_len = ftell(fp); | |
87 if (header_len > AVG_LEN) | |
88 header_len -= AVG_LEN; /* allow the first quotation to appear */ | |
89 | |
90 if (fseek(fp, 0L, 2) == -1) { | |
91 perror("fseek 1"); | |
92 exit(1); | |
93 } | |
94 len = ftell(fp) - header_len; | |
95 } | |
96 | |
97 | |
98 /* go to a random place in the file and print the quotation there */ | |
12 | 99 void |
100 yow (fp) | |
101 FILE *fp; | |
102 { | |
103 long offset; | |
104 int c, i = 0; | |
42 | 105 char *buf; |
106 unsigned int bufsize; | |
107 char *malloc(), *realloc(); | |
12 | 108 |
42 | 109 offset = rand() % len + header_len; |
12 | 110 if (fseek(fp, offset, 0) == -1) { |
111 perror("fseek 2"); | |
112 exit(1); | |
113 } | |
114 | |
115 /* Read until SEP, read next line, print it. | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
443
diff
changeset
|
116 (Note that we will never print anything before the first separator.) |
12 | 117 If we hit EOF looking for the first SEP, just recurse. */ |
118 while ((c = getc(fp)) != SEP) | |
119 if (c == EOF) { | |
120 yow(fp); | |
121 return; | |
122 } | |
123 | |
124 /* Skip leading whitespace, then read in a quotation. | |
125 If we hit EOF before we find a non-whitespace char, recurse. */ | |
126 while (isspace(c = getc(fp))) | |
127 ; | |
128 if (c == EOF) { | |
129 yow(fp); | |
130 return; | |
131 } | |
42 | 132 |
133 bufsize = BUFSIZE; | |
134 buf = malloc(bufsize); | |
135 if (buf == (char *)0) { | |
136 fprintf(stderr, "can't allocate any memory\n"); | |
137 exit (3); | |
138 } | |
139 | |
12 | 140 buf[i++] = c; |
141 while ((c = getc(fp)) != SEP && c != EOF) { | |
142 buf[i++] = c; | |
42 | 143 |
144 if (i == bufsize-1) { | |
12 | 145 /* Yow! Is this quotation too long yet? */ |
42 | 146 bufsize *= 2; |
147 buf = realloc(buf, bufsize); | |
148 if (buf == (char *)0) { | |
149 fprintf(stderr, "can't allocate more memory\n"); | |
150 exit (3); | |
151 } | |
152 } | |
12 | 153 } |
154 buf[i++] = 0; | |
155 printf("%s\n", buf); | |
156 } | |
157 |