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