| 1 | /* |
| 2 | * yow.c |
| 3 | * |
| 4 | * Print a quotation from Zippy the Pinhead. |
| 5 | * Qux <Kaufman-David@Yale> March 6, 1986 |
| 6 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <ctype.h> |
| 15 | #include <../src/paths.h> /* For PATH_DATA. */ |
| 16 | |
| 17 | #define BUFSIZE 80 |
| 18 | #define SEP '\0' |
| 19 | |
| 20 | #ifndef YOW_FILE |
| 21 | #define YOW_FILE "yow.lines" |
| 22 | #endif |
| 23 | |
| 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 |
| 35 | |
| 36 | char *malloc(), *realloc(); |
| 37 | |
| 38 | void yow(); |
| 39 | void setup_yow(); |
| 40 | |
| 41 | int |
| 42 | main (argc, argv) |
| 43 | int argc; |
| 44 | char *argv[]; |
| 45 | { |
| 46 | FILE *fp; |
| 47 | char file[BUFSIZ]; |
| 48 | |
| 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 |
| 57 | |
| 58 | if ((fp = fopen(file, "r")) == NULL) { |
| 59 | fprintf(stderr, "yow: "); |
| 60 | perror(file); |
| 61 | exit(1); |
| 62 | } |
| 63 | |
| 64 | /* initialize random seed */ |
| 65 | srand((int) (getpid() + time((long *) 0))); |
| 66 | |
| 67 | setup_yow(fp); |
| 68 | yow(fp); |
| 69 | fclose(fp); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static long len = -1; |
| 74 | static long header_len; |
| 75 | |
| 76 | #define AVG_LEN 40 /* average length of a quotation */ |
| 77 | |
| 78 | /* Sets len and header_len */ |
| 79 | void |
| 80 | setup_yow(fp) |
| 81 | FILE *fp; |
| 82 | { |
| 83 | int c; |
| 84 | |
| 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); |
| 93 | } |
| 94 | } |
| 95 | header_len = ftell(fp); |
| 96 | if (header_len > AVG_LEN) |
| 97 | header_len -= AVG_LEN; /* allow the first quotation to appear */ |
| 98 | |
| 99 | if (fseek(fp, 0L, 2) == -1) { |
| 100 | perror("yow"); |
| 101 | exit(1); |
| 102 | } |
| 103 | len = ftell(fp) - header_len; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* go to a random place in the file and print the quotation there */ |
| 108 | void |
| 109 | yow (fp) |
| 110 | FILE *fp; |
| 111 | { |
| 112 | long offset; |
| 113 | int c, i = 0; |
| 114 | char *buf; |
| 115 | unsigned int bufsize; |
| 116 | |
| 117 | offset = rand() % len + header_len; |
| 118 | if (fseek(fp, offset, 0) == -1) { |
| 119 | perror("yow"); |
| 120 | exit(1); |
| 121 | } |
| 122 | |
| 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; |
| 130 | } |
| 131 | |
| 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))) |
| 135 | ; |
| 136 | if (c == EOF) { |
| 137 | yow(fp); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | bufsize = BUFSIZE; |
| 142 | buf = malloc(bufsize); |
| 143 | if (buf == (char *)0) { |
| 144 | fprintf(stderr, "yow: virtual memory exhausted\n"); |
| 145 | exit (3); |
| 146 | } |
| 147 | |
| 148 | buf[i++] = c; |
| 149 | while ((c = getc(fp)) != SEP && c != EOF) { |
| 150 | buf[i++] = c; |
| 151 | |
| 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); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | buf[i++] = 0; |
| 163 | printf("%s\n", buf); |
| 164 | } |
| 165 | |