annotate lib-src/yow.c @ 3067:38fa0e3705f6

Fix the fix.
author Jim Blandy <jimb@redhat.com>
date Tue, 25 May 1993 09:26:44 +0000
parents 78165914b12d
children 507f64624555
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>
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
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
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 main (argc, argv)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 int argc;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 char *argv[];
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24 {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 FILE *fp;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 char file[BUFSIZ];
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
27 void yow(), setup_yow();
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 if (argc > 2 && !strcmp (argv[1], "-f"))
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 strcpy (file, argv[2]);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 else
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 #ifdef vms
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
diff changeset
33 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 #else
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
diff changeset
35 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 #endif
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 if ((fp = fopen(file, "r")) == NULL) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 perror(file);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 exit(1);
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
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 /* initialize random seed */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 srand((int) (getpid() + time((long *) 0)));
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
46 setup_yow(fp);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 fclose(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 exit(0);
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
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
52 static long len = -1;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
53 static long header_len;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
54
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
55 #define AVG_LEN 40 /* average length of a quotation */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
56
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
57 /* Sets len and header_len */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
58 void
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
59 setup_yow(fp)
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
60 FILE *fp;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
61 {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
62 int c;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
63
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
64 /* Get length of file */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
65 /* 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
66 * 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
67 * we explicitly skip that. */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
68 while ((c = getc(fp)) != SEP) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
69 if (c == EOF) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
70 fprintf(stderr, "File contains no separators.\n");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
71 exit(2);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
72 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
73 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
74 header_len = ftell(fp);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
75 if (header_len > AVG_LEN)
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
76 header_len -= AVG_LEN; /* allow the first quotation to appear */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
77
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
78 if (fseek(fp, 0L, 2) == -1) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
79 perror("fseek 1");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
80 exit(1);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
81 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
82 len = ftell(fp) - header_len;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
83 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
84
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 /* 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
87 void
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 yow (fp)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 FILE *fp;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 long offset;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 int c, i = 0;
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
93 char *buf;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
94 unsigned int bufsize;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
95 char *malloc(), *realloc();
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
97 offset = rand() % len + header_len;
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 if (fseek(fp, offset, 0) == -1) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 perror("fseek 2");
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 exit(1);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 /* Read until SEP, read next line, print it.
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 (Note that we will never print anything before the first seperator.)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 If we hit EOF looking for the first SEP, just recurse. */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 while ((c = getc(fp)) != SEP)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 if (c == EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 return;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 /* Skip leading whitespace, then read in a quotation.
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 If we hit EOF before we find a non-whitespace char, recurse. */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 while (isspace(c = getc(fp)))
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 if (c == EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 return;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 }
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
120
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
121 bufsize = BUFSIZE;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
122 buf = malloc(bufsize);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
123 if (buf == (char *)0) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
124 fprintf(stderr, "can't allocate any memory\n");
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
125 exit (3);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
126 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
127
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 buf[i++] = c;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 while ((c = getc(fp)) != SEP && c != EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 buf[i++] = c;
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
131
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
132 if (i == bufsize-1) {
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 /* Yow! Is this quotation too long yet? */
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
134 bufsize *= 2;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
135 buf = realloc(buf, 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 more 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 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 buf[i++] = 0;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 printf("%s\n", buf);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145