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