Mercurial > emacs
annotate lib-src/yow.c @ 48506:249dfdd236fb
(store_frame_title_char) [PROTOTYPES]: Provide ISO C
arglist.
author | Dave Love <fx@gnu.org> |
---|---|
date | Fri, 22 Nov 2002 12:22:20 +0000 |
parents | 83c1951257f1 |
children | 644c7fb7d69d |
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 | |
42412 | 13 #ifdef HAVE_CONFIG_H |
14 #include <config.h> | |
15 #endif | |
16 | |
42 | 17 #include <stdio.h> |
18 #include <ctype.h> | |
42131
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
19 #ifdef HAVE_STRING_H |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
20 #include <string.h> |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
21 #endif |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
22 #ifdef TIME_WITH_SYS_TIME |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
23 #include <sys/time.h> |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
24 #include <time.h> |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
25 #else |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
26 #ifdef HAVE_SYS_TIME_H |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
27 #include <sys/time.h> |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
28 #else |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
29 #include <time.h> |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
30 #endif |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
31 #endif |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
32 #ifdef HAVE_UNISTD_H |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
33 #include <unistd.h> |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
34 #endif |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
35 #include "epaths.h" /* For PATH_DATA. */ |
42 | 36 |
37 #define BUFSIZE 80 | |
12 | 38 #define SEP '\0' |
42 | 39 |
40 #ifndef YOW_FILE | |
12 | 41 #define YOW_FILE "yow.lines" |
42 | 42 #endif |
12 | 43 |
7820
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
44 #ifdef MSDOS |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
45 #define rootrelativepath(rel) \ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
46 ({\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
47 static char res[BUFSIZE], *p;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
48 strcpy (res, argv[0]);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
49 p = res + strlen (res);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
50 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
51 strcpy (p + 1, "../");\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
52 strcpy (p + 4, rel);\ |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
53 &res;}) |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
54 #endif |
128950f29e66
(rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents:
4702
diff
changeset
|
55 |
42131
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
56 #ifndef HAVE_STDLIB_H |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
57 char *malloc __P ((size_t size))), *realloc __P ((POINTER_TYPE *ptr, size_t size)); |
c8cfb8893fac
Conditionally include various headers. Use "epaths.h",
Dave Love <fx@gnu.org>
parents:
24408
diff
changeset
|
58 #endif |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
59 |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
60 void yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
61 void setup_yow(); |
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
62 |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
63 int |
12 | 64 main (argc, argv) |
65 int argc; | |
66 char *argv[]; | |
67 { | |
68 FILE *fp; | |
69 char file[BUFSIZ]; | |
70 | |
71 if (argc > 2 && !strcmp (argv[1], "-f")) | |
72 strcpy (file, argv[2]); | |
73 else | |
74 #ifdef vms | |
443 | 75 sprintf (file, "%s%s", PATH_DATA, YOW_FILE); |
12 | 76 #else |
443 | 77 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); |
12 | 78 #endif |
79 | |
80 if ((fp = fopen(file, "r")) == NULL) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
81 fprintf(stderr, "yow: "); |
12 | 82 perror(file); |
83 exit(1); | |
84 } | |
85 | |
86 /* initialize random seed */ | |
42151
e9a441eabced
(main): Use time_t, not long, to avoid a compiler warning.
Eli Zaretskii <eliz@gnu.org>
parents:
42131
diff
changeset
|
87 srand((int) (getpid() + time((time_t *) 0))); |
12 | 88 |
42 | 89 setup_yow(fp); |
12 | 90 yow(fp); |
91 fclose(fp); | |
9491
dd3b83e4ceb0
Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents:
7820
diff
changeset
|
92 return 0; |
12 | 93 } |
94 | |
42 | 95 static long len = -1; |
96 static long header_len; | |
97 | |
98 #define AVG_LEN 40 /* average length of a quotation */ | |
99 | |
100 /* Sets len and header_len */ | |
101 void | |
102 setup_yow(fp) | |
103 FILE *fp; | |
104 { | |
105 int c; | |
106 | |
107 /* Get length of file */ | |
108 /* Because the header (stuff before the first SEP) can be very long, | |
109 * thus biasing our search in favor of the first quotation in the file, | |
110 * we explicitly skip that. */ | |
111 while ((c = getc(fp)) != SEP) { | |
112 if (c == EOF) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
113 fprintf(stderr, "yow: file contains no separators\n"); |
42 | 114 exit(2); |
115 } | |
116 } | |
117 header_len = ftell(fp); | |
118 if (header_len > AVG_LEN) | |
119 header_len -= AVG_LEN; /* allow the first quotation to appear */ | |
120 | |
121 if (fseek(fp, 0L, 2) == -1) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
122 perror("yow"); |
42 | 123 exit(1); |
124 } | |
125 len = ftell(fp) - header_len; | |
126 } | |
127 | |
128 | |
129 /* go to a random place in the file and print the quotation there */ | |
12 | 130 void |
131 yow (fp) | |
132 FILE *fp; | |
133 { | |
134 long offset; | |
135 int c, i = 0; | |
42 | 136 char *buf; |
137 unsigned int bufsize; | |
12 | 138 |
42 | 139 offset = rand() % len + header_len; |
12 | 140 if (fseek(fp, offset, 0) == -1) { |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
141 perror("yow"); |
12 | 142 exit(1); |
143 } | |
144 | |
145 /* 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
|
146 (Note that we will never print anything before the first separator.) |
12 | 147 If we hit EOF looking for the first SEP, just recurse. */ |
148 while ((c = getc(fp)) != SEP) | |
149 if (c == EOF) { | |
150 yow(fp); | |
151 return; | |
152 } | |
153 | |
154 /* Skip leading whitespace, then read in a quotation. | |
155 If we hit EOF before we find a non-whitespace char, recurse. */ | |
156 while (isspace(c = getc(fp))) | |
157 ; | |
158 if (c == EOF) { | |
159 yow(fp); | |
160 return; | |
161 } | |
42 | 162 |
163 bufsize = BUFSIZE; | |
164 buf = malloc(bufsize); | |
165 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
166 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 167 exit (3); |
168 } | |
169 | |
12 | 170 buf[i++] = c; |
171 while ((c = getc(fp)) != SEP && c != EOF) { | |
172 buf[i++] = c; | |
42 | 173 |
174 if (i == bufsize-1) { | |
12 | 175 /* Yow! Is this quotation too long yet? */ |
42 | 176 bufsize *= 2; |
177 buf = realloc(buf, bufsize); | |
178 if (buf == (char *)0) { | |
10198
aa59550d809f
Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents:
9491
diff
changeset
|
179 fprintf(stderr, "yow: virtual memory exhausted\n"); |
42 | 180 exit (3); |
181 } | |
182 } | |
12 | 183 } |
184 buf[i++] = 0; | |
185 printf("%s\n", buf); | |
186 } | |
187 |