(Fml_provide_prefix_argument, Fml_prefix_argument_loop): Use perdisplay.
[emacs.git] / lib-src / yow.c
blob606074834cd21c816d1fb7be75d8133160aa2545
1 /*
2 * yow.c
3 *
4 * Print a quotation from Zippy the Pinhead.
5 * Qux <Kaufman-David@Yale> March 6, 1986
6 *
7 * With dynamic memory allocation.
8 */
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <../src/paths.h> /* For PATH_DATA. */
14 #define BUFSIZE 80
15 #define SEP '\0'
17 #ifndef YOW_FILE
18 #define YOW_FILE "yow.lines"
19 #endif
21 #ifdef MSDOS
22 #define rootrelativepath(rel) \
23 ({\
24 static char res[BUFSIZE], *p;\
25 strcpy (res, argv[0]);\
26 p = res + strlen (res);\
27 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
28 strcpy (p + 1, "../");\
29 strcpy (p + 4, rel);\
30 &res;})
31 #endif
33 char *malloc(), *realloc();
35 void yow();
36 void setup_yow();
38 int
39 main (argc, argv)
40 int argc;
41 char *argv[];
43 FILE *fp;
44 char file[BUFSIZ];
46 if (argc > 2 && !strcmp (argv[1], "-f"))
47 strcpy (file, argv[2]);
48 else
49 #ifdef vms
50 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
51 #else
52 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
53 #endif
55 if ((fp = fopen(file, "r")) == NULL) {
56 fprintf(stderr, "yow: ");
57 perror(file);
58 exit(1);
61 /* initialize random seed */
62 srand((int) (getpid() + time((long *) 0)));
64 setup_yow(fp);
65 yow(fp);
66 fclose(fp);
67 return 0;
70 static long len = -1;
71 static long header_len;
73 #define AVG_LEN 40 /* average length of a quotation */
75 /* Sets len and header_len */
76 void
77 setup_yow(fp)
78 FILE *fp;
80 int c;
82 /* Get length of file */
83 /* Because the header (stuff before the first SEP) can be very long,
84 * thus biasing our search in favor of the first quotation in the file,
85 * we explicitly skip that. */
86 while ((c = getc(fp)) != SEP) {
87 if (c == EOF) {
88 fprintf(stderr, "yow: file contains no separators\n");
89 exit(2);
92 header_len = ftell(fp);
93 if (header_len > AVG_LEN)
94 header_len -= AVG_LEN; /* allow the first quotation to appear */
96 if (fseek(fp, 0L, 2) == -1) {
97 perror("yow");
98 exit(1);
100 len = ftell(fp) - header_len;
104 /* go to a random place in the file and print the quotation there */
105 void
106 yow (fp)
107 FILE *fp;
109 long offset;
110 int c, i = 0;
111 char *buf;
112 unsigned int bufsize;
114 offset = rand() % len + header_len;
115 if (fseek(fp, offset, 0) == -1) {
116 perror("yow");
117 exit(1);
120 /* Read until SEP, read next line, print it.
121 (Note that we will never print anything before the first separator.)
122 If we hit EOF looking for the first SEP, just recurse. */
123 while ((c = getc(fp)) != SEP)
124 if (c == EOF) {
125 yow(fp);
126 return;
129 /* Skip leading whitespace, then read in a quotation.
130 If we hit EOF before we find a non-whitespace char, recurse. */
131 while (isspace(c = getc(fp)))
133 if (c == EOF) {
134 yow(fp);
135 return;
138 bufsize = BUFSIZE;
139 buf = malloc(bufsize);
140 if (buf == (char *)0) {
141 fprintf(stderr, "yow: virtual memory exhausted\n");
142 exit (3);
145 buf[i++] = c;
146 while ((c = getc(fp)) != SEP && c != EOF) {
147 buf[i++] = c;
149 if (i == bufsize-1) {
150 /* Yow! Is this quotation too long yet? */
151 bufsize *= 2;
152 buf = realloc(buf, bufsize);
153 if (buf == (char *)0) {
154 fprintf(stderr, "yow: virtual memory exhausted\n");
155 exit (3);
159 buf[i++] = 0;
160 printf("%s\n", buf);