Mercurial > emacs
annotate lib-src/yow.c @ 15701:5db02009315e libc-1-93 libc-960716 libc-960717 libc-960718 libc-960719 libc-960720 libc-960721 libc-960722 libc-960723 libc-960724 libc-960725 libc-960726 libc-960727 libc-960728 libc-960729 libc-960730 libc-960731 libc-960801 libc-960802 libc-960803 libc-960804 libc-960805 libc-960806 libc-960807 libc-960808 libc-960809 libc-960810 libc-960811 libc-960812 libc-960813 libc-960814 libc-960815 libc-960816 libc-960817 libc-960818 libc-960819 libc-960820 libc-960821 libc-960822 libc-960823 libc-960824 libc-960825 libc-960826 libc-960827 libc-960828 libc-960829 libc-960830 libc-960831 libc-960901 libc-960902 libc-960903 libc-960904 libc-960905 libc-960906 libc-960907 libc-960908 libc-960909 libc-960910 libc-960911 libc-960912 release-0-0 release-0-1
Fix previous change. Use sed's y command instead.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Tue, 16 Jul 1996 04:47:46 +0000 |
parents | b0652f55a85d |
children | fdac027af4e1 |
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 |
15681
b0652f55a85d
[__GNU_LIBRARY__]: Use <string.h>.
Karl Heuer <kwzh@gnu.org>
parents:
11183
diff
changeset
|
17 #ifdef __GNU_LIBRARY__ |
b0652f55a85d
[__GNU_LIBRARY__]: Use <string.h>.
Karl Heuer <kwzh@gnu.org>
parents:
11183
diff
changeset
|
18 # include <string.h> |
b0652f55a85d
[__GNU_LIBRARY__]: Use <string.h>.
Karl Heuer <kwzh@gnu.org>
parents:
11183
diff
changeset
|
19 #endif |
b0652f55a85d
[__GNU_LIBRARY__]: Use <string.h>.
Karl Heuer <kwzh@gnu.org>
parents:
11183
diff
changeset
|
20 |
42 | 21 #define BUFSIZE 80 |
12 | 22 #define SEP '\0' |
42 | 23 |
24 #ifndef YOW_FILE | |
12 | 25 #define YOW_FILE "yow.lines" |
42 | 26 #endif |
12 | 27 |
7820
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
28 #ifdef MSDOS |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
29 #define rootrelativepath(rel) \ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
30 ({\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
31 static char res[BUFSIZE], *p;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
32 strcpy (res, argv[0]);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
33 p = res + strlen (res);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
34 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
35 strcpy (p + 1, "../");\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
36 strcpy (p + 4, rel);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
37 &res;}) |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
38 #endif |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
39 |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
40 char *malloc(), *realloc(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
41 |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
42 void yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
43 void setup_yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
44 |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
45 int |
12 | 46 main (argc, argv) |
47 int argc; | |
48 char *argv[]; | |
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 vms | |
443 | 57 sprintf (file, "%s%s", PATH_DATA, YOW_FILE); |
12 | 58 #else |
443 | 59 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); |
12 | 60 #endif |
61 | |
62 if ((fp = fopen(file, "r")) == NULL) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
63 fprintf(stderr, "yow: "); |
12 | 64 perror(file); |
65 exit(1); | |
66 } | |
67 | |
68 /* initialize random seed */ | |
69 srand((int) (getpid() + time((long *) 0))); | |
70 | |
42 | 71 setup_yow(fp); |
12 | 72 yow(fp); |
73 fclose(fp); | |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
74 return 0; |
12 | 75 } |
76 | |
42 | 77 static long len = -1; |
78 static long header_len; | |
79 | |
80 #define AVG_LEN 40 /* average length of a quotation */ | |
81 | |
82 /* Sets len and header_len */ | |
83 void | |
84 setup_yow(fp) | |
85 FILE *fp; | |
86 { | |
87 int c; | |
88 | |
89 /* Get length of file */ | |
90 /* Because the header (stuff before the first SEP) can be very long, | |
91 * thus biasing our search in favor of the first quotation in the file, | |
92 * we explicitly skip that. */ | |
93 while ((c = getc(fp)) != SEP) { | |
94 if (c == EOF) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
95 fprintf(stderr, "yow: file contains no separators\n"); |
42 | 96 exit(2); |
97 } | |
98 } | |
99 header_len = ftell(fp); | |
100 if (header_len > AVG_LEN) | |
101 header_len -= AVG_LEN; /* allow the first quotation to appear */ | |
102 | |
103 if (fseek(fp, 0L, 2) == -1) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
104 perror("yow"); |
42 | 105 exit(1); |
106 } | |
107 len = ftell(fp) - header_len; | |
108 } | |
109 | |
110 | |
111 /* go to a random place in the file and print the quotation there */ | |
12 | 112 void |
113 yow (fp) | |
114 FILE *fp; | |
115 { | |
116 long offset; | |
117 int c, i = 0; | |
42 | 118 char *buf; |
119 unsigned int bufsize; | |
12 | 120 |
42 | 121 offset = rand() % len + header_len; |
12 | 122 if (fseek(fp, offset, 0) == -1) { |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
123 perror("yow"); |
12 | 124 exit(1); |
125 } | |
126 | |
127 /* 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
|
128 (Note that we will never print anything before the first separator.) |
12 | 129 If we hit EOF looking for the first SEP, just recurse. */ |
130 while ((c = getc(fp)) != SEP) | |
131 if (c == EOF) { | |
132 yow(fp); | |
133 return; | |
134 } | |
135 | |
136 /* Skip leading whitespace, then read in a quotation. | |
137 If we hit EOF before we find a non-whitespace char, recurse. */ | |
138 while (isspace(c = getc(fp))) | |
139 ; | |
140 if (c == EOF) { | |
141 yow(fp); | |
142 return; | |
143 } | |
42 | 144 |
145 bufsize = BUFSIZE; | |
146 buf = malloc(bufsize); | |
147 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
148 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 149 exit (3); |
150 } | |
151 | |
12 | 152 buf[i++] = c; |
153 while ((c = getc(fp)) != SEP && c != EOF) { | |
154 buf[i++] = c; | |
42 | 155 |
156 if (i == bufsize-1) { | |
12 | 157 /* Yow! Is this quotation too long yet? */ |
42 | 158 bufsize *= 2; |
159 buf = realloc(buf, bufsize); | |
160 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
161 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 162 exit (3); |
163 } | |
164 } | |
12 | 165 } |
166 buf[i++] = 0; | |
167 printf("%s\n", buf); | |
168 } | |
169 |