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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
1 /*
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
2 * yow.c
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 *
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4 * Print a quotation from Zippy the Pinhead.
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 * Qux <Kaufman-David@Yale> March 6, 1986
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 *
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
7 * With dynamic memory allocation.
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
10 #include <stdio.h>
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
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
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
13
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
14 #define BUFSIZE 80
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 #define SEP '\0'
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
16
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
17 #ifndef YOW_FILE
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 #define YOW_FILE "yow.lines"
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
19 #endif
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 main (argc, argv)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 int argc;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 char *argv[];
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 FILE *fp;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 char file[BUFSIZ];
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
40 void yow(), setup_yow();
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 if (argc > 2 && !strcmp (argv[1], "-f"))
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 strcpy (file, argv[2]);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 else
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 #ifdef vms
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
diff changeset
46 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 #else
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
diff changeset
48 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 #endif
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 if ((fp = fopen(file, "r")) == NULL) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 perror(file);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 exit(1);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 /* initialize random seed */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 srand((int) (getpid() + time((long *) 0)));
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
59 setup_yow(fp);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 fclose(fp);
9491
dd3b83e4ceb0 Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents: 7820
diff changeset
62 return 0;
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
65 static long len = -1;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
66 static long header_len;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
67
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
68 #define AVG_LEN 40 /* average length of a quotation */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
69
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
70 /* Sets len and header_len */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
71 void
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
72 setup_yow(fp)
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
73 FILE *fp;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
74 {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
75 int c;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
76
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
77 /* Get length of file */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
78 /* Because the header (stuff before the first SEP) can be very long,
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
79 * thus biasing our search in favor of the first quotation in the file,
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
80 * we explicitly skip that. */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
81 while ((c = getc(fp)) != SEP) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
82 if (c == EOF) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
83 fprintf(stderr, "File contains no separators.\n");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
84 exit(2);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
85 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
86 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
87 header_len = ftell(fp);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
88 if (header_len > AVG_LEN)
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
89 header_len -= AVG_LEN; /* allow the first quotation to appear */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
90
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
91 if (fseek(fp, 0L, 2) == -1) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
92 perror("fseek 1");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
93 exit(1);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
94 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
95 len = ftell(fp) - header_len;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
96 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
97
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
98
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
99 /* go to a random place in the file and print the quotation there */
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 void
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 yow (fp)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 FILE *fp;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 long offset;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 int c, i = 0;
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
106 char *buf;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
107 unsigned int bufsize;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
108 char *malloc(), *realloc();
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
110 offset = rand() % len + header_len;
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 if (fseek(fp, offset, 0) == -1) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 perror("fseek 2");
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 exit(1);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 If we hit EOF looking for the first SEP, just recurse. */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 while ((c = getc(fp)) != SEP)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 if (c == EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 return;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 /* Skip leading whitespace, then read in a quotation.
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 If we hit EOF before we find a non-whitespace char, recurse. */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 while (isspace(c = getc(fp)))
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 ;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 if (c == EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 return;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 }
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
133
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
134 bufsize = BUFSIZE;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
135 buf = malloc(bufsize);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
136 if (buf == (char *)0) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
137 fprintf(stderr, "can't allocate any memory\n");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
138 exit (3);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
139 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
140
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 buf[i++] = c;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 while ((c = getc(fp)) != SEP && c != EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 buf[i++] = c;
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
144
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
145 if (i == bufsize-1) {
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 /* Yow! Is this quotation too long yet? */
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
147 bufsize *= 2;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
148 buf = realloc(buf, bufsize);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
149 if (buf == (char *)0) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
150 fprintf(stderr, "can't allocate more memory\n");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
151 exit (3);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
152 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
153 }
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 buf[i++] = 0;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 printf("%s\n", buf);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158