diff options
| author | Richard M. Stallman | 1990-01-02 17:15:30 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1990-01-02 17:15:30 +0000 |
| commit | 701570d7d6b2239163cd22150e8709dbf53fcaf1 (patch) | |
| tree | db34dc0428d10224ed0f4487ddad610bdcbd7ee1 /lib-src | |
| parent | d80259179fbe6a034149764069e521615a86ee10 (diff) | |
| download | emacs-701570d7d6b2239163cd22150e8709dbf53fcaf1.tar.gz emacs-701570d7d6b2239163cd22150e8709dbf53fcaf1.zip | |
*** empty log message ***
Diffstat (limited to 'lib-src')
| -rw-r--r-- | lib-src/yow.c | 90 |
1 files changed, 69 insertions, 21 deletions
diff --git a/lib-src/yow.c b/lib-src/yow.c index f39822c9687..bfff0e7d3ea 100644 --- a/lib-src/yow.c +++ b/lib-src/yow.c | |||
| @@ -1,17 +1,22 @@ | |||
| 1 | #include <stdio.h> | 1 | /* |
| 2 | #include <ctype.h> | 2 | * yow.c |
| 3 | #include "../src/paths.h" | ||
| 4 | |||
| 5 | /* zippy.c | ||
| 6 | * | 3 | * |
| 7 | * Print a quotation from Zippy the Pinhead. | 4 | * Print a quotation from Zippy the Pinhead. |
| 8 | * Qux <Kaufman-David@Yale> March 6, 1986 | 5 | * Qux <Kaufman-David@Yale> March 6, 1986 |
| 9 | * | 6 | * |
| 7 | * With dynamic memory allocation. | ||
| 10 | */ | 8 | */ |
| 11 | 9 | ||
| 12 | #define BUFSIZE 2000 | 10 | #include <stdio.h> |
| 11 | #include <ctype.h> | ||
| 12 | #include "../src/paths.h" /* For PATH_EXEC. */ | ||
| 13 | |||
| 14 | #define BUFSIZE 80 | ||
| 13 | #define SEP '\0' | 15 | #define SEP '\0' |
| 16 | |||
| 17 | #ifndef YOW_FILE | ||
| 14 | #define YOW_FILE "yow.lines" | 18 | #define YOW_FILE "yow.lines" |
| 19 | #endif | ||
| 15 | 20 | ||
| 16 | main (argc, argv) | 21 | main (argc, argv) |
| 17 | int argc; | 22 | int argc; |
| @@ -19,7 +24,7 @@ main (argc, argv) | |||
| 19 | { | 24 | { |
| 20 | FILE *fp; | 25 | FILE *fp; |
| 21 | char file[BUFSIZ]; | 26 | char file[BUFSIZ]; |
| 22 | void yow(); | 27 | void yow(), setup_yow(); |
| 23 | 28 | ||
| 24 | if (argc > 2 && !strcmp (argv[1], "-f")) | 29 | if (argc > 2 && !strcmp (argv[1], "-f")) |
| 25 | strcpy (file, argv[2]); | 30 | strcpy (file, argv[2]); |
| @@ -38,29 +43,58 @@ main (argc, argv) | |||
| 38 | /* initialize random seed */ | 43 | /* initialize random seed */ |
| 39 | srand((int) (getpid() + time((long *) 0))); | 44 | srand((int) (getpid() + time((long *) 0))); |
| 40 | 45 | ||
| 46 | setup_yow(fp); | ||
| 41 | yow(fp); | 47 | yow(fp); |
| 42 | fclose(fp); | 48 | fclose(fp); |
| 43 | exit(0); | 49 | exit(0); |
| 44 | } | 50 | } |
| 45 | 51 | ||
| 52 | static long len = -1; | ||
| 53 | static long header_len; | ||
| 54 | |||
| 55 | #define AVG_LEN 40 /* average length of a quotation */ | ||
| 56 | |||
| 57 | /* Sets len and header_len */ | ||
| 58 | void | ||
| 59 | setup_yow(fp) | ||
| 60 | FILE *fp; | ||
| 61 | { | ||
| 62 | int c; | ||
| 63 | |||
| 64 | /* Get length of file */ | ||
| 65 | /* Because the header (stuff before the first SEP) can be very long, | ||
| 66 | * thus biasing our search in favor of the first quotation in the file, | ||
| 67 | * we explicitly skip that. */ | ||
| 68 | while ((c = getc(fp)) != SEP) { | ||
| 69 | if (c == EOF) { | ||
| 70 | fprintf(stderr, "File contains no separators.\n"); | ||
| 71 | exit(2); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | header_len = ftell(fp); | ||
| 75 | if (header_len > AVG_LEN) | ||
| 76 | header_len -= AVG_LEN; /* allow the first quotation to appear */ | ||
| 77 | |||
| 78 | if (fseek(fp, 0L, 2) == -1) { | ||
| 79 | perror("fseek 1"); | ||
| 80 | exit(1); | ||
| 81 | } | ||
| 82 | len = ftell(fp) - header_len; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | /* go to a random place in the file and print the quotation there */ | ||
| 46 | void | 87 | void |
| 47 | yow (fp) | 88 | yow (fp) |
| 48 | FILE *fp; | 89 | FILE *fp; |
| 49 | { | 90 | { |
| 50 | static long len = -1; | ||
| 51 | long offset; | 91 | long offset; |
| 52 | int c, i = 0; | 92 | int c, i = 0; |
| 53 | char buf[BUFSIZE]; | 93 | char *buf; |
| 94 | unsigned int bufsize; | ||
| 95 | char *malloc(), *realloc(); | ||
| 54 | 96 | ||
| 55 | /* Get length of file, go to a random place in it */ | 97 | offset = rand() % len + header_len; |
| 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) { | 98 | if (fseek(fp, offset, 0) == -1) { |
| 65 | perror("fseek 2"); | 99 | perror("fseek 2"); |
| 66 | exit(1); | 100 | exit(1); |
| @@ -83,13 +117,27 @@ yow (fp) | |||
| 83 | yow(fp); | 117 | yow(fp); |
| 84 | return; | 118 | return; |
| 85 | } | 119 | } |
| 120 | |||
| 121 | bufsize = BUFSIZE; | ||
| 122 | buf = malloc(bufsize); | ||
| 123 | if (buf == (char *)0) { | ||
| 124 | fprintf(stderr, "can't allocate any memory\n"); | ||
| 125 | exit (3); | ||
| 126 | } | ||
| 127 | |||
| 86 | buf[i++] = c; | 128 | buf[i++] = c; |
| 87 | while ((c = getc(fp)) != SEP && c != EOF) { | 129 | while ((c = getc(fp)) != SEP && c != EOF) { |
| 88 | buf[i++] = c; | 130 | buf[i++] = c; |
| 89 | 131 | ||
| 90 | if (i == BUFSIZ-1) | 132 | if (i == bufsize-1) { |
| 91 | /* Yow! Is this quotation too long yet? */ | 133 | /* Yow! Is this quotation too long yet? */ |
| 92 | break; | 134 | bufsize *= 2; |
| 135 | buf = realloc(buf, bufsize); | ||
| 136 | if (buf == (char *)0) { | ||
| 137 | fprintf(stderr, "can't allocate more memory\n"); | ||
| 138 | exit (3); | ||
| 139 | } | ||
| 140 | } | ||
| 93 | } | 141 | } |
| 94 | buf[i++] = 0; | 142 | buf[i++] = 0; |
| 95 | printf("%s\n", buf); | 143 | printf("%s\n", buf); |