(Fcurrent_time_zone) [HAVE_TM_ZONE || HAVE_TZNAME]:
[emacs.git] / lib-src / yow.c
blob4efe8153415132b7e22c5a0f3848a1ab722e9a76
1 /*
2 * yow.c
3 *
4 * Print a quotation from Zippy the Pinhead.
5 * Qux <Kaufman-David@Yale> March 6, 1986
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.
9 *
10 * With dynamic memory allocation.
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <../src/epaths.h> /* For PATH_DATA. */
17 #define BUFSIZE 80
18 #define SEP '\0'
20 #ifndef YOW_FILE
21 #define YOW_FILE "yow.lines"
22 #endif
24 #ifdef MSDOS
25 #define rootrelativepath(rel) \
26 ({\
27 static char res[BUFSIZE], *p;\
28 strcpy (res, argv[0]);\
29 p = res + strlen (res);\
30 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
31 strcpy (p + 1, "../");\
32 strcpy (p + 4, rel);\
33 &res;})
34 #endif
36 char *malloc(), *realloc();
38 void yow();
39 void setup_yow();
41 int
42 main (argc, argv)
43 int argc;
44 char *argv[];
46 FILE *fp;
47 char file[BUFSIZ];
49 if (argc > 2 && !strcmp (argv[1], "-f"))
50 strcpy (file, argv[2]);
51 else
52 #ifdef vms
53 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
54 #else
55 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
56 #endif
58 if ((fp = fopen(file, "r")) == NULL) {
59 fprintf(stderr, "yow: ");
60 perror(file);
61 exit(1);
64 /* initialize random seed */
65 srand((int) (getpid() + time((long *) 0)));
67 setup_yow(fp);
68 yow(fp);
69 fclose(fp);
70 return 0;
73 static long len = -1;
74 static long header_len;
76 #define AVG_LEN 40 /* average length of a quotation */
78 /* Sets len and header_len */
79 void
80 setup_yow(fp)
81 FILE *fp;
83 int c;
85 /* Get length of file */
86 /* Because the header (stuff before the first SEP) can be very long,
87 * thus biasing our search in favor of the first quotation in the file,
88 * we explicitly skip that. */
89 while ((c = getc(fp)) != SEP) {
90 if (c == EOF) {
91 fprintf(stderr, "yow: file contains no separators\n");
92 exit(2);
95 header_len = ftell(fp);
96 if (header_len > AVG_LEN)
97 header_len -= AVG_LEN; /* allow the first quotation to appear */
99 if (fseek(fp, 0L, 2) == -1) {
100 perror("yow");
101 exit(1);
103 len = ftell(fp) - header_len;
107 /* go to a random place in the file and print the quotation there */
108 void
109 yow (fp)
110 FILE *fp;
112 long offset;
113 int c, i = 0;
114 char *buf;
115 unsigned int bufsize;
117 offset = rand() % len + header_len;
118 if (fseek(fp, offset, 0) == -1) {
119 perror("yow");
120 exit(1);
123 /* Read until SEP, read next line, print it.
124 (Note that we will never print anything before the first separator.)
125 If we hit EOF looking for the first SEP, just recurse. */
126 while ((c = getc(fp)) != SEP)
127 if (c == EOF) {
128 yow(fp);
129 return;
132 /* Skip leading whitespace, then read in a quotation.
133 If we hit EOF before we find a non-whitespace char, recurse. */
134 while (isspace(c = getc(fp)))
136 if (c == EOF) {
137 yow(fp);
138 return;
141 bufsize = BUFSIZE;
142 buf = malloc(bufsize);
143 if (buf == (char *)0) {
144 fprintf(stderr, "yow: virtual memory exhausted\n");
145 exit (3);
148 buf[i++] = c;
149 while ((c = getc(fp)) != SEP && c != EOF) {
150 buf[i++] = c;
152 if (i == bufsize-1) {
153 /* Yow! Is this quotation too long yet? */
154 bufsize *= 2;
155 buf = realloc(buf, bufsize);
156 if (buf == (char *)0) {
157 fprintf(stderr, "yow: virtual memory exhausted\n");
158 exit (3);
162 buf[i++] = 0;
163 printf("%s\n", buf);