Mercurial > emacs
comparison lib-src/yow.c @ 88155:d7ddb3e565de
sync with trunk
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Mon, 16 Jan 2006 00:03:54 +0000 |
parents | 23a1cea22d13 |
children |
comparison
equal
deleted
inserted
replaced
88154:8ce476d3ba36 | 88155:d7ddb3e565de |
---|---|
48 strcpy (p + 1, "../");\ | 48 strcpy (p + 1, "../");\ |
49 strcpy (p + 4, rel);\ | 49 strcpy (p + 4, rel);\ |
50 &res;}) | 50 &res;}) |
51 #endif | 51 #endif |
52 | 52 |
53 #ifndef HAVE_STDLIB_H | |
54 char *malloc __P ((size_t size))), *realloc __P ((POINTER_TYPE *ptr, size_t size)); | |
55 #endif | |
56 | |
57 void yow(); | 53 void yow(); |
58 void setup_yow(); | 54 void setup_yow(); |
59 | 55 |
60 int | 56 int |
61 main (argc, argv) | 57 main (argc, argv) |
75 #endif | 71 #endif |
76 | 72 |
77 if ((fp = fopen(file, "r")) == NULL) { | 73 if ((fp = fopen(file, "r")) == NULL) { |
78 fprintf(stderr, "yow: "); | 74 fprintf(stderr, "yow: "); |
79 perror(file); | 75 perror(file); |
80 exit(1); | 76 exit(EXIT_FAILURE); |
81 } | 77 } |
82 | 78 |
83 /* initialize random seed */ | 79 /* initialize random seed */ |
84 srand((int) (getpid() + time((time_t *) 0))); | 80 srand((int) (getpid() + time((time_t *) 0))); |
85 | 81 |
86 setup_yow(fp); | 82 setup_yow(fp); |
87 yow(fp); | 83 yow(fp); |
88 fclose(fp); | 84 fclose(fp); |
89 return 0; | 85 return EXIT_SUCCESS; |
90 } | 86 } |
91 | 87 |
92 static long len = -1; | 88 static long len = -1; |
93 static long header_len; | 89 static long header_len; |
94 | 90 |
106 * thus biasing our search in favor of the first quotation in the file, | 102 * thus biasing our search in favor of the first quotation in the file, |
107 * we explicitly skip that. */ | 103 * we explicitly skip that. */ |
108 while ((c = getc(fp)) != SEP) { | 104 while ((c = getc(fp)) != SEP) { |
109 if (c == EOF) { | 105 if (c == EOF) { |
110 fprintf(stderr, "yow: file contains no separators\n"); | 106 fprintf(stderr, "yow: file contains no separators\n"); |
111 exit(2); | 107 exit(EXIT_FAILURE); |
112 } | 108 } |
113 } | 109 } |
114 header_len = ftell(fp); | 110 header_len = ftell(fp); |
115 if (header_len > AVG_LEN) | 111 if (header_len > AVG_LEN) |
116 header_len -= AVG_LEN; /* allow the first quotation to appear */ | 112 header_len -= AVG_LEN; /* allow the first quotation to appear */ |
117 | 113 |
118 if (fseek(fp, 0L, 2) == -1) { | 114 if (fseek(fp, 0L, 2) == -1) { |
119 perror("yow"); | 115 perror("yow"); |
120 exit(1); | 116 exit(EXIT_FAILURE); |
121 } | 117 } |
122 len = ftell(fp) - header_len; | 118 len = ftell(fp) - header_len; |
123 } | 119 } |
124 | 120 |
125 | 121 |
134 unsigned int bufsize; | 130 unsigned int bufsize; |
135 | 131 |
136 offset = rand() % len + header_len; | 132 offset = rand() % len + header_len; |
137 if (fseek(fp, offset, 0) == -1) { | 133 if (fseek(fp, offset, 0) == -1) { |
138 perror("yow"); | 134 perror("yow"); |
139 exit(1); | 135 exit(EXIT_FAILURE); |
140 } | 136 } |
141 | 137 |
142 /* Read until SEP, read next line, print it. | 138 /* Read until SEP, read next line, print it. |
143 (Note that we will never print anything before the first separator.) | 139 (Note that we will never print anything before the first separator.) |
144 If we hit EOF looking for the first SEP, just recurse. */ | 140 If we hit EOF looking for the first SEP, just recurse. */ |
156 yow(fp); | 152 yow(fp); |
157 return; | 153 return; |
158 } | 154 } |
159 | 155 |
160 bufsize = BUFSIZE; | 156 bufsize = BUFSIZE; |
161 buf = malloc(bufsize); | 157 buf = (char *) malloc(bufsize); |
162 if (buf == (char *)0) { | 158 if (buf == (char *)0) { |
163 fprintf(stderr, "yow: virtual memory exhausted\n"); | 159 fprintf(stderr, "yow: virtual memory exhausted\n"); |
164 exit (3); | 160 exit (EXIT_FAILURE); |
165 } | 161 } |
166 | 162 |
167 buf[i++] = c; | 163 buf[i++] = c; |
168 while ((c = getc(fp)) != SEP && c != EOF) { | 164 while ((c = getc(fp)) != SEP && c != EOF) { |
169 buf[i++] = c; | 165 buf[i++] = c; |
170 | 166 |
171 if (i == bufsize-1) { | 167 if (i == bufsize-1) { |
172 /* Yow! Is this quotation too long yet? */ | 168 /* Yow! Is this quotation too long yet? */ |
173 bufsize *= 2; | 169 bufsize *= 2; |
174 buf = realloc(buf, bufsize); | 170 buf = (char *) realloc(buf, bufsize); |
175 if (buf == (char *)0) { | 171 if (buf == (char *)0) { |
176 fprintf(stderr, "yow: virtual memory exhausted\n"); | 172 fprintf(stderr, "yow: virtual memory exhausted\n"); |
177 exit (3); | 173 exit (EXIT_FAILURE); |
178 } | 174 } |
179 } | 175 } |
180 } | 176 } |
181 buf[i++] = 0; | 177 buf[i++] = 0; |
182 printf("%s\n", buf); | 178 printf("%s\n", buf); |
183 } | 179 } |
184 | 180 |
181 /* arch-tag: e40fc0df-bafb-4001-af24-5c883d1c685e | |
182 (do not change this comment) */ | |
183 | |
184 /* yow.c ends here */ |