diff options
| author | Richard M. Stallman | 1988-04-13 06:13:41 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1988-04-13 06:13:41 +0000 |
| commit | bc8511901ee26794349266971a1016ad6f0cd63c (patch) | |
| tree | ab8dfaa6160154e87eeb346e42278be4224da650 /lib-src | |
| parent | d7cc518448c8af66db86a842bbd0372e7e13a95a (diff) | |
| download | emacs-bc8511901ee26794349266971a1016ad6f0cd63c.tar.gz emacs-bc8511901ee26794349266971a1016ad6f0cd63c.zip | |
Initial revision
Diffstat (limited to 'lib-src')
| -rw-r--r-- | lib-src/yow.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/lib-src/yow.c b/lib-src/yow.c new file mode 100644 index 00000000000..f39822c9687 --- /dev/null +++ b/lib-src/yow.c | |||
| @@ -0,0 +1,97 @@ | |||
| 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 | |||