Mercurial > emacs
comparison lib-src/yow.c @ 12:4f420246a4f7
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 13 Apr 1988 06:13:41 +0000 |
parents | |
children | 71b3e30e0dd4 |
comparison
equal
deleted
inserted
replaced
11:5d42bce31792 | 12:4f420246a4f7 |
---|---|
1 #include <stdio.h> | |
2 #include <ctype.h> | |
3 #include "../src/paths.h" | |
4 | |
5 /* zippy.c | |
6 * | |
7 * Print a quotation from Zippy the Pinhead. | |
8 * Qux <Kaufman-David@Yale> March 6, 1986 | |
9 * | |
10 */ | |
11 | |
12 #define BUFSIZE 2000 | |
13 #define SEP '\0' | |
14 #define YOW_FILE "yow.lines" | |
15 | |
16 main (argc, argv) | |
17 int argc; | |
18 char *argv[]; | |
19 { | |
20 FILE *fp; | |
21 char file[BUFSIZ]; | |
22 void yow(); | |
23 | |
24 if (argc > 2 && !strcmp (argv[1], "-f")) | |
25 strcpy (file, argv[2]); | |
26 else | |
27 #ifdef vms | |
28 sprintf (file, "%s%s", PATH_EXEC, YOW_FILE); | |
29 #else | |
30 sprintf (file, "%s/%s", PATH_EXEC, YOW_FILE); | |
31 #endif | |
32 | |
33 if ((fp = fopen(file, "r")) == NULL) { | |
34 perror(file); | |
35 exit(1); | |
36 } | |
37 | |
38 /* initialize random seed */ | |
39 srand((int) (getpid() + time((long *) 0))); | |
40 | |
41 yow(fp); | |
42 fclose(fp); | |
43 exit(0); | |
44 } | |
45 | |
46 void | |
47 yow (fp) | |
48 FILE *fp; | |
49 { | |
50 static long len = -1; | |
51 long offset; | |
52 int c, i = 0; | |
53 char buf[BUFSIZE]; | |
54 | |
55 /* Get length of file, go to a random place in it */ | |
56 if (len == -1) { | |
57 if (fseek(fp, 0, 2) == -1) { | |
58 perror("fseek 1"); | |
59 exit(1); | |
60 } | |
61 len = ftell(fp); | |
62 } | |
63 offset = rand() % len; | |
64 if (fseek(fp, offset, 0) == -1) { | |
65 perror("fseek 2"); | |
66 exit(1); | |
67 } | |
68 | |
69 /* Read until SEP, read next line, print it. | |
70 (Note that we will never print anything before the first seperator.) | |
71 If we hit EOF looking for the first SEP, just recurse. */ | |
72 while ((c = getc(fp)) != SEP) | |
73 if (c == EOF) { | |
74 yow(fp); | |
75 return; | |
76 } | |
77 | |
78 /* Skip leading whitespace, then read in a quotation. | |
79 If we hit EOF before we find a non-whitespace char, recurse. */ | |
80 while (isspace(c = getc(fp))) | |
81 ; | |
82 if (c == EOF) { | |
83 yow(fp); | |
84 return; | |
85 } | |
86 buf[i++] = c; | |
87 while ((c = getc(fp)) != SEP && c != EOF) { | |
88 buf[i++] = c; | |
89 | |
90 if (i == BUFSIZ-1) | |
91 /* Yow! Is this quotation too long yet? */ | |
92 break; | |
93 } | |
94 buf[i++] = 0; | |
95 printf("%s\n", buf); | |
96 } | |
97 |