Mercurial > emacs
comparison lib-src/yow.c @ 10198:aa59550d809f
Include program name in error messages.
author | David J. MacKenzie <djm@gnu.org> |
---|---|
date | Wed, 21 Dec 1994 15:58:28 +0000 |
parents | dd3b83e4ceb0 |
children | 2f57a6fbd720 |
comparison
equal
deleted
inserted
replaced
10197:66681eddfd70 | 10198:aa59550d809f |
---|---|
28 strcpy (p + 1, "../");\ | 28 strcpy (p + 1, "../");\ |
29 strcpy (p + 4, rel);\ | 29 strcpy (p + 4, rel);\ |
30 &res;}) | 30 &res;}) |
31 #endif | 31 #endif |
32 | 32 |
33 char *malloc(), *realloc(); | |
34 | |
35 void yow(); | |
36 void setup_yow(); | |
37 | |
33 int | 38 int |
34 main (argc, argv) | 39 main (argc, argv) |
35 int argc; | 40 int argc; |
36 char *argv[]; | 41 char *argv[]; |
37 { | 42 { |
38 FILE *fp; | 43 FILE *fp; |
39 char file[BUFSIZ]; | 44 char file[BUFSIZ]; |
40 void yow(), setup_yow(); | |
41 | 45 |
42 if (argc > 2 && !strcmp (argv[1], "-f")) | 46 if (argc > 2 && !strcmp (argv[1], "-f")) |
43 strcpy (file, argv[2]); | 47 strcpy (file, argv[2]); |
44 else | 48 else |
45 #ifdef vms | 49 #ifdef vms |
47 #else | 51 #else |
48 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); | 52 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE); |
49 #endif | 53 #endif |
50 | 54 |
51 if ((fp = fopen(file, "r")) == NULL) { | 55 if ((fp = fopen(file, "r")) == NULL) { |
56 fprintf(stderr, "yow: "); | |
52 perror(file); | 57 perror(file); |
53 exit(1); | 58 exit(1); |
54 } | 59 } |
55 | 60 |
56 /* initialize random seed */ | 61 /* initialize random seed */ |
78 /* Because the header (stuff before the first SEP) can be very long, | 83 /* Because the header (stuff before the first SEP) can be very long, |
79 * thus biasing our search in favor of the first quotation in the file, | 84 * thus biasing our search in favor of the first quotation in the file, |
80 * we explicitly skip that. */ | 85 * we explicitly skip that. */ |
81 while ((c = getc(fp)) != SEP) { | 86 while ((c = getc(fp)) != SEP) { |
82 if (c == EOF) { | 87 if (c == EOF) { |
83 fprintf(stderr, "File contains no separators.\n"); | 88 fprintf(stderr, "yow: file contains no separators\n"); |
84 exit(2); | 89 exit(2); |
85 } | 90 } |
86 } | 91 } |
87 header_len = ftell(fp); | 92 header_len = ftell(fp); |
88 if (header_len > AVG_LEN) | 93 if (header_len > AVG_LEN) |
89 header_len -= AVG_LEN; /* allow the first quotation to appear */ | 94 header_len -= AVG_LEN; /* allow the first quotation to appear */ |
90 | 95 |
91 if (fseek(fp, 0L, 2) == -1) { | 96 if (fseek(fp, 0L, 2) == -1) { |
92 perror("fseek 1"); | 97 perror("yow"); |
93 exit(1); | 98 exit(1); |
94 } | 99 } |
95 len = ftell(fp) - header_len; | 100 len = ftell(fp) - header_len; |
96 } | 101 } |
97 | 102 |
103 { | 108 { |
104 long offset; | 109 long offset; |
105 int c, i = 0; | 110 int c, i = 0; |
106 char *buf; | 111 char *buf; |
107 unsigned int bufsize; | 112 unsigned int bufsize; |
108 char *malloc(), *realloc(); | |
109 | 113 |
110 offset = rand() % len + header_len; | 114 offset = rand() % len + header_len; |
111 if (fseek(fp, offset, 0) == -1) { | 115 if (fseek(fp, offset, 0) == -1) { |
112 perror("fseek 2"); | 116 perror("yow"); |
113 exit(1); | 117 exit(1); |
114 } | 118 } |
115 | 119 |
116 /* Read until SEP, read next line, print it. | 120 /* Read until SEP, read next line, print it. |
117 (Note that we will never print anything before the first separator.) | 121 (Note that we will never print anything before the first separator.) |
132 } | 136 } |
133 | 137 |
134 bufsize = BUFSIZE; | 138 bufsize = BUFSIZE; |
135 buf = malloc(bufsize); | 139 buf = malloc(bufsize); |
136 if (buf == (char *)0) { | 140 if (buf == (char *)0) { |
137 fprintf(stderr, "can't allocate any memory\n"); | 141 fprintf(stderr, "yow: virtual memory exhausted\n"); |
138 exit (3); | 142 exit (3); |
139 } | 143 } |
140 | 144 |
141 buf[i++] = c; | 145 buf[i++] = c; |
142 while ((c = getc(fp)) != SEP && c != EOF) { | 146 while ((c = getc(fp)) != SEP && c != EOF) { |
145 if (i == bufsize-1) { | 149 if (i == bufsize-1) { |
146 /* Yow! Is this quotation too long yet? */ | 150 /* Yow! Is this quotation too long yet? */ |
147 bufsize *= 2; | 151 bufsize *= 2; |
148 buf = realloc(buf, bufsize); | 152 buf = realloc(buf, bufsize); |
149 if (buf == (char *)0) { | 153 if (buf == (char *)0) { |
150 fprintf(stderr, "can't allocate more memory\n"); | 154 fprintf(stderr, "yow: virtual memory exhausted\n"); |
151 exit (3); | 155 exit (3); |
152 } | 156 } |
153 } | 157 } |
154 } | 158 } |
155 buf[i++] = 0; | 159 buf[i++] = 0; |