style: compare return value of getopt() against -1 rather than EOF
[dragonfly.git] / games / fortune / strfile / strfile.c
blob81d2e311bb6d1be8acdb4b690e8f22419c395fbd
1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Ken Arnold.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * $FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $
37 * $DragonFly: src/games/fortune/strfile/strfile.c,v 1.6 2008/07/10 18:29:51 swildner Exp $
39 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
40 * @(#)strfile.c 8.1 (Berkeley) 5/31/93
41 * $FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $
44 # include <sys/param.h>
45 # include <stdbool.h>
46 # include <stdio.h>
47 # include <stdlib.h>
48 # include <ctype.h>
49 # include <string.h>
50 # include <time.h>
51 # include <locale.h>
52 # include <unistd.h>
53 # include "strfile.h"
56 * This program takes a file composed of strings separated by
57 * lines starting with two consecutive delimiting character (default
58 * character is '%') and creates another file which consists of a table
59 * describing the file (structure from "strfile.h"), a table of seek
60 * pointers to the start of the strings, and the strings, each terminated
61 * by a null byte. Usage:
63 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
65 * C - Allow comments marked by a double delimiter at line's beginning
66 * c - Change delimiting character from '%' to 'C'
67 * s - Silent. Give no summary of data processed at the end of
68 * the run.
69 * o - order the strings in alphabetic order
70 * i - if ordering, ignore case
71 * r - randomize the order of the strings
72 * x - set rotated bit
74 * Ken Arnold Sept. 7, 1978 --
76 * Added ordering options.
79 # define STORING_PTRS (Oflag || Rflag)
80 # define CHUNKSIZE 512
82 # define ALLOC(ptr,sz) { \
83 if (ptr == NULL) \
84 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
85 else if (((sz) + 1) % CHUNKSIZE == 0) \
86 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
87 if (ptr == NULL) { \
88 fprintf(stderr, "out of space\n"); \
89 exit(1); \
90 } \
93 typedef struct {
94 char first;
95 long pos;
96 } STR;
98 char *Infile = NULL, /* input file name */
99 Outfile[MAXPATHLEN] = "", /* output file name */
100 Delimch = '%'; /* delimiting character */
102 int Cflag = false; /* embedded comments */
103 int Sflag = false; /* silent run flag */
104 int Oflag = false; /* ordering flag */
105 int Iflag = false; /* ignore case flag */
106 int Rflag = false; /* randomize order flag */
107 int Xflag = false; /* set rotated bit */
108 long Num_pts = 0; /* number of pointers/strings */
110 long *Seekpts;
112 FILE *Sort_1, *Sort_2; /* pointers for sorting */
114 STRFILE Tbl; /* statistics table */
116 STR *Firstch; /* first chars of each string */
118 void add_offset (FILE *, long);
119 int cmp_str (const void *, const void *);
120 static int collate_range_cmp (int, int);
121 void do_order (void);
122 void getargs (int, char **);
123 void randomize (void);
124 void usage (void);
127 * main:
128 * Drive the sucker. There are two main modes -- either we store
129 * the seek pointers, if the table is to be sorted or randomized,
130 * or we write the pointer directly to the file, if we are to stay
131 * in file order. If the former, we allocate and re-allocate in
132 * CHUNKSIZE blocks; if the latter, we just write each pointer,
133 * and then seek back to the beginning to write in the table.
136 main(int ac, char **av)
138 char *sp, dc;
139 FILE *inf, *outf;
140 long last_off, length, pos, *p;
141 int first, cnt;
142 char *nsp;
143 STR *fp;
144 static char string[257];
146 (void) setlocale(LC_ALL, "");
148 getargs(ac, av); /* evalute arguments */
149 dc = Delimch;
150 if ((inf = fopen(Infile, "r")) == NULL) {
151 perror(Infile);
152 exit(1);
155 if ((outf = fopen(Outfile, "w")) == NULL) {
156 perror(Outfile);
157 exit(1);
159 if (!STORING_PTRS)
160 (void) fseek(outf, (long) sizeof Tbl, 0);
163 * Write the strings onto the file
166 Tbl.str_longlen = 0;
167 Tbl.str_shortlen = ~((unsigned long) 0);
168 Tbl.str_delim = dc;
169 Tbl.str_version = VERSION;
170 first = Oflag;
171 add_offset(outf, ftell(inf));
172 last_off = 0;
173 do {
174 sp = fgets(string, 256, inf);
175 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
176 pos = ftell(inf);
177 length = pos - last_off - (sp ? strlen(sp) : 0);
178 last_off = pos;
179 if (!length)
180 continue;
181 add_offset(outf, pos);
182 if (Tbl.str_longlen < (unsigned long)length)
183 Tbl.str_longlen = length;
184 if (Tbl.str_shortlen > (unsigned long)length)
185 Tbl.str_shortlen = length;
186 first = Oflag;
188 else if (first) {
189 for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
190 continue;
191 ALLOC(Firstch, Num_pts);
192 fp = &Firstch[Num_pts - 1];
193 if (Iflag && isupper((unsigned char)*nsp))
194 fp->first = tolower((unsigned char)*nsp);
195 else
196 fp->first = *nsp;
197 fp->pos = Seekpts[Num_pts - 1];
198 first = false;
200 } while (sp != NULL);
203 * write the tables in
206 (void) fclose(inf);
207 Tbl.str_numstr = Num_pts - 1;
209 if (Cflag)
210 Tbl.str_flags |= STR_COMMENTS;
212 if (Oflag)
213 do_order();
214 else if (Rflag)
215 randomize();
217 if (Xflag)
218 Tbl.str_flags |= STR_ROTATED;
220 if (!Sflag) {
221 printf("\"%s\" created\n", Outfile);
222 if (Num_pts == 2)
223 puts("There was 1 string");
224 else
225 printf("There were %ld strings\n", Num_pts - 1);
226 printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
227 Tbl.str_longlen == 1 ? "" : "s");
228 printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
229 Tbl.str_shortlen == 1 ? "" : "s");
232 rewind(outf);
233 Tbl.str_version = htonl(Tbl.str_version);
234 Tbl.str_numstr = htonl(Tbl.str_numstr);
235 Tbl.str_longlen = htonl(Tbl.str_longlen);
236 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
237 Tbl.str_flags = htonl(Tbl.str_flags);
238 (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
239 if (STORING_PTRS) {
240 for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
241 *p = htonl(*p);
242 (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
244 (void) fclose(outf);
245 exit(0);
249 * This routine evaluates arguments from the command line
251 void
252 getargs(int argc, char **argv)
254 int ch;
256 while ((ch = getopt(argc, argv, "Cc:iorsx")) != -1)
257 switch(ch) {
258 case 'C': /* embedded comments */
259 Cflag++;
260 break;
261 case 'c': /* new delimiting char */
262 Delimch = *optarg;
263 if (!isascii(Delimch)) {
264 printf("bad delimiting character: '\\%o\n'",
265 (unsigned char)Delimch);
267 break;
268 case 'i': /* ignore case in ordering */
269 Iflag++;
270 break;
271 case 'o': /* order strings */
272 Oflag++;
273 break;
274 case 'r': /* randomize pointers */
275 Rflag++;
276 break;
277 case 's': /* silent */
278 Sflag++;
279 break;
280 case 'x': /* set the rotated bit */
281 Xflag++;
282 break;
283 case '?':
284 default:
285 usage();
287 argv += optind;
289 if (*argv) {
290 Infile = *argv;
291 if (*++argv)
292 (void) strcpy(Outfile, *argv);
294 if (!Infile) {
295 puts("No input file name");
296 usage();
298 if (*Outfile == '\0') {
299 (void) strcpy(Outfile, Infile);
300 (void) strcat(Outfile, ".dat");
304 void
305 usage(void)
307 (void) fprintf(stderr,
308 "strfile [-Ciorsx] [-c char] sourcefile [datafile]\n");
309 exit(1);
313 * add_offset:
314 * Add an offset to the list, or write it out, as appropriate.
316 void
317 add_offset(FILE *fp, long off)
319 long net;
321 if (!STORING_PTRS) {
322 net = htonl(off);
323 fwrite(&net, 1, sizeof net, fp);
324 } else {
325 ALLOC(Seekpts, Num_pts + 1);
326 Seekpts[Num_pts] = off;
328 Num_pts++;
332 * do_order:
333 * Order the strings alphabetically (possibly ignoring case).
335 void
336 do_order(void)
338 int i;
339 long *lp;
340 STR *fp;
342 Sort_1 = fopen(Infile, "r");
343 Sort_2 = fopen(Infile, "r");
344 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
345 i = Tbl.str_numstr;
346 lp = Seekpts;
347 fp = Firstch;
348 while (i--)
349 *lp++ = fp++->pos;
350 (void) fclose(Sort_1);
351 (void) fclose(Sort_2);
352 Tbl.str_flags |= STR_ORDERED;
355 static int
356 collate_range_cmp (int c1, int c2)
358 static char s1[2], s2[2];
359 int ret;
361 c1 &= UCHAR_MAX;
362 c2 &= UCHAR_MAX;
363 if (c1 == c2)
364 return (0);
365 s1[0] = c1;
366 s2[0] = c2;
367 if ((ret = strcoll(s1, s2)) != 0)
368 return (ret);
369 return (c1 - c2);
373 * cmp_str:
374 * Compare two strings in the file
377 cmp_str(const void *s1, const void *s2)
379 const STR *p1, *p2;
380 int c1, c2;
381 int n1, n2;
382 int r;
384 # define SET_N(nf,ch) (nf = (ch == '\n'))
385 # define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char) Delimch && nf))
387 p1 = (const STR *) s1;
388 p2 = (const STR *) s2;
390 c1 = (unsigned char) p1->first;
391 c2 = (unsigned char) p2->first;
392 if ((r = collate_range_cmp(c1, c2)) != 0)
393 return r;
395 (void) fseek(Sort_1, p1->pos, 0);
396 (void) fseek(Sort_2, p2->pos, 0);
398 n1 = false;
399 n2 = false;
400 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
401 SET_N(n1, c1);
402 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
403 SET_N(n2, c2);
405 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
406 if (Iflag) {
407 if (isupper(c1))
408 c1 = tolower(c1);
409 if (isupper(c2))
410 c2 = tolower(c2);
412 if ((r = collate_range_cmp(c1, c2)) != 0)
413 return r;
414 SET_N(n1, c1);
415 SET_N(n2, c2);
416 c1 = getc(Sort_1);
417 c2 = getc(Sort_2);
419 if (IS_END(c1, n1))
420 c1 = 0;
421 if (IS_END(c2, n2))
422 c2 = 0;
423 return collate_range_cmp(c1, c2);
427 * randomize:
428 * Randomize the order of the string table. We must be careful
429 * not to randomize across delimiter boundaries. All
430 * randomization is done within each block.
432 void
433 randomize(void)
435 int cnt, i;
436 long tmp;
437 long *sp;
439 srandomdev();
441 Tbl.str_flags |= STR_RANDOM;
442 cnt = Tbl.str_numstr;
445 * move things around randomly
448 for (sp = Seekpts; cnt > 0; cnt--, sp++) {
449 i = random() % cnt;
450 tmp = sp[0];
451 sp[0] = sp[i];
452 sp[i] = tmp;