Mercurial > emacs
annotate lib-src/yow.c @ 10213:dd2102e33b29
(dired-string-replace-match): Moved to dired.el.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 22 Dec 1994 04:16:36 +0000 |
parents | aa59550d809f |
children | 2f57a6fbd720 |
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 |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
33 char *malloc(), *realloc(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
34 |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
35 void yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
36 void setup_yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
37 |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
38 int |
12 | 39 main (argc, argv) |
40 int argc; | |
41 char *argv[]; | |
42 { | |
43 FILE *fp; | |
44 char file[BUFSIZ]; | |
45 | |
46 if (argc > 2 && !strcmp (argv[1], "-f")) | |
47 strcpy (file, argv[2]); | |
48 else | |
49 #ifdef vms | |
443 | 50 sprintf (file, "%s%s", PATH_DATA, YOW_FILE); |
12 | 51 #else |
443 | 52 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); |
12 | 53 #endif |
54 | |
55 if ((fp = fopen(file, "r")) == NULL) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
56 fprintf(stderr, "yow: "); |
12 | 57 perror(file); |
58 exit(1); | |
59 } | |
60 | |
61 /* initialize random seed */ | |
62 srand((int) (getpid() + time((long *) 0))); | |
63 | |
42 | 64 setup_yow(fp); |
12 | 65 yow(fp); |
66 fclose(fp); | |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
67 return 0; |
12 | 68 } |
69 | |
42 | 70 static long len = -1; |
71 static long header_len; | |
72 | |
73 #define AVG_LEN 40 /* average length of a quotation */ | |
74 | |
75 /* Sets len and header_len */ | |
76 void | |
77 setup_yow(fp) | |
78 FILE *fp; | |
79 { | |
80 int c; | |
81 | |
82 /* Get length of file */ | |
83 /* Because the header (stuff before the first SEP) can be very long, | |
84 * thus biasing our search in favor of the first quotation in the file, | |
85 * we explicitly skip that. */ | |
86 while ((c = getc(fp)) != SEP) { | |
87 if (c == EOF) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
88 fprintf(stderr, "yow: file contains no separators\n"); |
42 | 89 exit(2); |
90 } | |
91 } | |
92 header_len = ftell(fp); | |
93 if (header_len > AVG_LEN) | |
94 header_len -= AVG_LEN; /* allow the first quotation to appear */ | |
95 | |
96 if (fseek(fp, 0L, 2) == -1) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
97 perror("yow"); |
42 | 98 exit(1); |
99 } | |
100 len = ftell(fp) - header_len; | |
101 } | |
102 | |
103 | |
104 /* go to a random place in the file and print the quotation there */ | |
12 | 105 void |
106 yow (fp) | |
107 FILE *fp; | |
108 { | |
109 long offset; | |
110 int c, i = 0; | |
42 | 111 char *buf; |
112 unsigned int bufsize; | |
12 | 113 |
42 | 114 offset = rand() % len + header_len; |
12 | 115 if (fseek(fp, offset, 0) == -1) { |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
116 perror("yow"); |
12 | 117 exit(1); |
118 } | |
119 | |
120 /* 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
|
121 (Note that we will never print anything before the first separator.) |
12 | 122 If we hit EOF looking for the first SEP, just recurse. */ |
123 while ((c = getc(fp)) != SEP) | |
124 if (c == EOF) { | |
125 yow(fp); | |
126 return; | |
127 } | |
128 | |
129 /* Skip leading whitespace, then read in a quotation. | |
130 If we hit EOF before we find a non-whitespace char, recurse. */ | |
131 while (isspace(c = getc(fp))) | |
132 ; | |
133 if (c == EOF) { | |
134 yow(fp); | |
135 return; | |
136 } | |
42 | 137 |
138 bufsize = BUFSIZE; | |
139 buf = malloc(bufsize); | |
140 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
141 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 142 exit (3); |
143 } | |
144 | |
12 | 145 buf[i++] = c; |
146 while ((c = getc(fp)) != SEP && c != EOF) { | |
147 buf[i++] = c; | |
42 | 148 |
149 if (i == bufsize-1) { | |
12 | 150 /* Yow! Is this quotation too long yet? */ |
42 | 151 bufsize *= 2; |
152 buf = realloc(buf, bufsize); | |
153 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
154 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 155 exit (3); |
156 } | |
157 } | |
12 | 158 } |
159 buf[i++] = 0; | |
160 printf("%s\n", buf); | |
161 } | |
162 |