Mercurial > emacs
annotate lib-src/yow.c @ 19860:c17fd465ea95 libc-970911 libc-970912 libc-970913 libc-970914 libc-970915 libc-970916 libc-970917 libc-970918 libc-970919 libc-970920 libc-970921 libc-970922 libc-970923 libc-970924 libc-970925 libc-970926 libc-970927 libc-970928 libc-970929 libc-970930 libc-971001 libc-971018 libc-971019 libc-971020 libc-971021 libc-971022 libc-971023 libc-971024 libc-971025 libc-971026 libc-971027 libc-971028 libc-971029 libc-971030 libc-971031 libc-971101 libc-971102 libc-971103 libc-971104 libc-971105 libc-971106 libc-971107 libc-971108 libc-971109 libc-971110 libc-971111 libc-971112 libc-971113 libc-971114 libc-971115 libc-971116 libc-971117 libc-971118 libc-971120 libc-971121 libc-971122 libc-971123 libc-971124 libc-971125 libc-971126 libc-971127 libc-971128 libc-971129 libc-971130 libc-971201 libc-971203 libc-971204 libc-971205 libc-971206 libc-971207 libc-971208 libc-971209 libc-971210 libc-971211 libc-971212 libc-971213 libc-971214 libc-971217 libc-971218 libc-971219 libc-971220 libc-971221 libc-971222 libc-971223 libc-971224 libc-971225 libc-971226 libc-971227 libc-971228 libc-971229 libc-971230 libc-971231 libc-980103 libc-980104 libc-980105 libc-980106 libc-980107 libc-980108 libc-980109 libc-980110 libc-980111 libc-980112 libc-980114 libc-980115 libc-980116 libc-980117 libc-980118 libc-980119 libc-980120 libc-980121 libc-980122 libc-980123 libc-980124 libc-980125 libc-980126 libc-980127 libc-980128
typos.
author | Jeff Law <law@redhat.com> |
---|---|
date | Wed, 10 Sep 1997 21:16:20 +0000 |
parents | fdac027af4e1 |
children | 595856efc8f1 |
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 | |
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. | |
12 | 9 * |
42 | 10 * With dynamic memory allocation. |
12 | 11 */ |
12 | |
42 | 13 #include <stdio.h> |
14 #include <ctype.h> | |
4702
2641ab559aae
Include <src/paths.h>, instead "src/paths.h".
Roland McGrath <roland@gnu.org>
parents:
3591
diff
changeset
|
15 #include <../src/paths.h> /* For PATH_DATA. */ |
42 | 16 |
17 #define BUFSIZE 80 | |
12 | 18 #define SEP '\0' |
42 | 19 |
20 #ifndef YOW_FILE | |
12 | 21 #define YOW_FILE "yow.lines" |
42 | 22 #endif |
12 | 23 |
7820
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
24 #ifdef MSDOS |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
25 #define rootrelativepath(rel) \ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
26 ({\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
27 static char res[BUFSIZE], *p;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
28 strcpy (res, argv[0]);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
29 p = res + strlen (res);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
30 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
31 strcpy (p + 1, "../");\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
32 strcpy (p + 4, rel);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
33 &res;}) |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
34 #endif |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
35 |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
36 char *malloc(), *realloc(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
37 |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
38 void yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
39 void setup_yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
40 |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
41 int |
12 | 42 main (argc, argv) |
43 int argc; | |
44 char *argv[]; | |
45 { | |
46 FILE *fp; | |
47 char file[BUFSIZ]; | |
48 | |
49 if (argc > 2 && !strcmp (argv[1], "-f")) | |
50 strcpy (file, argv[2]); | |
51 else | |
52 #ifdef vms | |
443 | 53 sprintf (file, "%s%s", PATH_DATA, YOW_FILE); |
12 | 54 #else |
443 | 55 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); |
12 | 56 #endif |
57 | |
58 if ((fp = fopen(file, "r")) == NULL) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
59 fprintf(stderr, "yow: "); |
12 | 60 perror(file); |
61 exit(1); | |
62 } | |
63 | |
64 /* initialize random seed */ | |
65 srand((int) (getpid() + time((long *) 0))); | |
66 | |
42 | 67 setup_yow(fp); |
12 | 68 yow(fp); |
69 fclose(fp); | |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
70 return 0; |
12 | 71 } |
72 | |
42 | 73 static long len = -1; |
74 static long header_len; | |
75 | |
76 #define AVG_LEN 40 /* average length of a quotation */ | |
77 | |
78 /* Sets len and header_len */ | |
79 void | |
80 setup_yow(fp) | |
81 FILE *fp; | |
82 { | |
83 int c; | |
84 | |
85 /* Get length of file */ | |
86 /* Because the header (stuff before the first SEP) can be very long, | |
87 * thus biasing our search in favor of the first quotation in the file, | |
88 * we explicitly skip that. */ | |
89 while ((c = getc(fp)) != SEP) { | |
90 if (c == EOF) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
91 fprintf(stderr, "yow: file contains no separators\n"); |
42 | 92 exit(2); |
93 } | |
94 } | |
95 header_len = ftell(fp); | |
96 if (header_len > AVG_LEN) | |
97 header_len -= AVG_LEN; /* allow the first quotation to appear */ | |
98 | |
99 if (fseek(fp, 0L, 2) == -1) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
100 perror("yow"); |
42 | 101 exit(1); |
102 } | |
103 len = ftell(fp) - header_len; | |
104 } | |
105 | |
106 | |
107 /* go to a random place in the file and print the quotation there */ | |
12 | 108 void |
109 yow (fp) | |
110 FILE *fp; | |
111 { | |
112 long offset; | |
113 int c, i = 0; | |
42 | 114 char *buf; |
115 unsigned int bufsize; | |
12 | 116 |
42 | 117 offset = rand() % len + header_len; |
12 | 118 if (fseek(fp, offset, 0) == -1) { |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
119 perror("yow"); |
12 | 120 exit(1); |
121 } | |
122 | |
123 /* 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
|
124 (Note that we will never print anything before the first separator.) |
12 | 125 If we hit EOF looking for the first SEP, just recurse. */ |
126 while ((c = getc(fp)) != SEP) | |
127 if (c == EOF) { | |
128 yow(fp); | |
129 return; | |
130 } | |
131 | |
132 /* Skip leading whitespace, then read in a quotation. | |
133 If we hit EOF before we find a non-whitespace char, recurse. */ | |
134 while (isspace(c = getc(fp))) | |
135 ; | |
136 if (c == EOF) { | |
137 yow(fp); | |
138 return; | |
139 } | |
42 | 140 |
141 bufsize = BUFSIZE; | |
142 buf = malloc(bufsize); | |
143 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
144 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 145 exit (3); |
146 } | |
147 | |
12 | 148 buf[i++] = c; |
149 while ((c = getc(fp)) != SEP && c != EOF) { | |
150 buf[i++] = c; | |
42 | 151 |
152 if (i == bufsize-1) { | |
12 | 153 /* Yow! Is this quotation too long yet? */ |
42 | 154 bufsize *= 2; |
155 buf = realloc(buf, bufsize); | |
156 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
157 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 158 exit (3); |
159 } | |
160 } | |
12 | 161 } |
162 buf[i++] = 0; | |
163 printf("%s\n", buf); | |
164 } | |
165 |