games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.git] / games / fortune / strfile / strfile.c
blob23c891d9e86df432757d89fbc9f8ec024ffaa96e
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * $FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $
33 * $DragonFly: src/games/fortune/strfile/strfile.c,v 1.6 2008/07/10 18:29:51 swildner Exp $
35 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
36 * @(#)strfile.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $
40 #include <sys/param.h>
41 #include <netinet/in.h>
42 #include <ctype.h>
43 #include <locale.h>
44 #include <stdbool.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <unistd.h>
50 #include "strfile.h"
53 * This program takes a file composed of strings separated by
54 * lines starting with two consecutive delimiting character (default
55 * character is '%') and creates another file which consists of a table
56 * describing the file (structure from "strfile.h"), a table of seek
57 * pointers to the start of the strings, and the strings, each terminated
58 * by a null byte. Usage:
60 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
62 * C - Allow comments marked by a double delimiter at line's beginning
63 * c - Change delimiting character from '%' to 'C'
64 * s - Silent. Give no summary of data processed at the end of
65 * the run.
66 * o - order the strings in alphabetic order
67 * i - if ordering, ignore case
68 * r - randomize the order of the strings
69 * x - set rotated bit
71 * Ken Arnold Sept. 7, 1978 --
73 * Added ordering options.
76 #define STORING_PTRS (Oflag || Rflag)
77 #define CHUNKSIZE 512
79 #define ALLOC(ptr,sz) do { \
80 if (ptr == NULL) \
81 ptr = malloc((unsigned int)(CHUNKSIZE * sizeof(*ptr))); \
82 else if (((sz) + 1) % CHUNKSIZE == 0) \
83 ptr = realloc(ptr, ((unsigned int)((sz) + CHUNKSIZE) * sizeof(*ptr))); \
84 if (ptr == NULL) { \
85 fprintf(stderr, "out of space\n"); \
86 exit(1); \
87 } \
88 } while (0)
90 typedef struct {
91 char first;
92 long pos;
93 } STR;
95 char *Infile = NULL, /* input file name */
96 Outfile[MAXPATHLEN] = "", /* output file name */
97 Delimch = '%'; /* delimiting character */
99 int Cflag = false; /* embedded comments */
100 int Sflag = false; /* silent run flag */
101 int Oflag = false; /* ordering flag */
102 int Iflag = false; /* ignore case flag */
103 int Rflag = false; /* randomize order flag */
104 int Xflag = false; /* set rotated bit */
105 long Num_pts = 0; /* number of pointers/strings */
107 long *Seekpts;
109 FILE *Sort_1, *Sort_2; /* pointers for sorting */
111 STRFILE Tbl; /* statistics table */
113 STR *Firstch; /* first chars of each string */
115 void add_offset(FILE *, long);
116 int cmp_str(const void *, const void *);
117 static int collate_range_cmp(int, int);
118 void do_order(void);
119 void getargs(int, char **);
120 void randomize(void);
121 void usage(void);
124 * main:
125 * Drive the sucker. There are two main modes -- either we store
126 * the seek pointers, if the table is to be sorted or randomized,
127 * or we write the pointer directly to the file, if we are to stay
128 * in file order. If the former, we allocate and re-allocate in
129 * CHUNKSIZE blocks; if the latter, we just write each pointer,
130 * and then seek back to the beginning to write in the table.
133 main(int ac, char *av[])
135 char *sp, *nsp, dc;
136 FILE *inf, *outf;
137 long last_off, length, pos, *p;
138 int first, cnt;
139 STR *fp;
140 static char string[257];
142 setlocale(LC_ALL, "");
144 getargs(ac, av); /* evalute arguments */
145 dc = Delimch;
146 if ((inf = fopen(Infile, "r")) == NULL) {
147 perror(Infile);
148 exit(1);
151 if ((outf = fopen(Outfile, "w")) == NULL) {
152 perror(Outfile);
153 exit(1);
155 if (!STORING_PTRS)
156 fseek(outf, sizeof(Tbl), SEEK_SET);
159 * Write the strings onto the file
162 Tbl.str_longlen = 0;
163 Tbl.str_shortlen = ~((unsigned long)0);
164 Tbl.str_delim = dc;
165 Tbl.str_version = VERSION;
166 first = Oflag;
167 add_offset(outf, ftell(inf));
168 last_off = 0;
169 do {
170 sp = fgets(string, 256, inf);
171 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
172 pos = ftell(inf);
173 length = pos - last_off - (sp ? strlen(sp) : 0);
174 last_off = pos;
175 if (!length)
176 continue;
177 add_offset(outf, pos);
178 if (Tbl.str_longlen < (unsigned long)length)
179 Tbl.str_longlen = length;
180 if (Tbl.str_shortlen > (unsigned long)length)
181 Tbl.str_shortlen = length;
182 first = Oflag;
184 else if (first) {
185 for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
186 continue;
187 ALLOC(Firstch, Num_pts);
188 fp = &Firstch[Num_pts - 1];
189 if (Iflag && isupper((unsigned char)*nsp))
190 fp->first = tolower((unsigned char)*nsp);
191 else
192 fp->first = *nsp;
193 fp->pos = Seekpts[Num_pts - 1];
194 first = false;
196 } while (sp != NULL);
199 * write the tables in
202 fclose(inf);
203 Tbl.str_numstr = Num_pts - 1;
205 if (Cflag)
206 Tbl.str_flags |= STR_COMMENTS;
208 if (Oflag)
209 do_order();
210 else if (Rflag)
211 randomize();
213 if (Xflag)
214 Tbl.str_flags |= STR_ROTATED;
216 if (!Sflag) {
217 printf("\"%s\" created\n", Outfile);
218 if (Num_pts == 2)
219 puts("There was 1 string");
220 else
221 printf("There were %ld strings\n", Num_pts - 1);
222 printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
223 Tbl.str_longlen == 1 ? "" : "s");
224 printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
225 Tbl.str_shortlen == 1 ? "" : "s");
228 rewind(outf);
229 Tbl.str_version = htonl(Tbl.str_version);
230 Tbl.str_numstr = htonl(Tbl.str_numstr);
231 Tbl.str_longlen = htonl(Tbl.str_longlen);
232 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
233 Tbl.str_flags = htonl(Tbl.str_flags);
234 fwrite((char *)&Tbl, sizeof(Tbl), 1, outf);
235 if (STORING_PTRS) {
236 for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
237 *p = htonl(*p);
238 fwrite((char *)Seekpts, sizeof(*Seekpts), (int)Num_pts, outf);
240 fclose(outf);
241 exit(0);
245 * This routine evaluates arguments from the command line
247 void
248 getargs(int argc, char **argv)
250 int ch;
252 while ((ch = getopt(argc, argv, "Cc:iorsx")) != -1)
253 switch(ch) {
254 case 'C': /* embedded comments */
255 Cflag++;
256 break;
257 case 'c': /* new delimiting char */
258 Delimch = *optarg;
259 if (!isascii(Delimch)) {
260 printf("bad delimiting character: '\\%o\n'",
261 (unsigned char)Delimch);
263 break;
264 case 'i': /* ignore case in ordering */
265 Iflag++;
266 break;
267 case 'o': /* order strings */
268 Oflag++;
269 break;
270 case 'r': /* randomize pointers */
271 Rflag++;
272 break;
273 case 's': /* silent */
274 Sflag++;
275 break;
276 case 'x': /* set the rotated bit */
277 Xflag++;
278 break;
279 case '?':
280 default:
281 usage();
283 argv += optind;
285 if (*argv) {
286 Infile = *argv;
287 if (*++argv)
288 strcpy(Outfile, *argv);
290 if (!Infile) {
291 puts("No input file name");
292 usage();
294 if (*Outfile == '\0') {
295 strcpy(Outfile, Infile);
296 strcat(Outfile, ".dat");
300 void
301 usage(void)
303 fprintf(stderr,
304 "strfile [-Ciorsx] [-c char] sourcefile [datafile]\n");
305 exit(1);
309 * add_offset:
310 * Add an offset to the list, or write it out, as appropriate.
312 void
313 add_offset(FILE *fp, long off)
315 long net;
317 if (!STORING_PTRS) {
318 net = htonl(off);
319 fwrite(&net, 1, sizeof(net), fp);
320 } else {
321 ALLOC(Seekpts, Num_pts + 1);
322 Seekpts[Num_pts] = off;
324 Num_pts++;
328 * do_order:
329 * Order the strings alphabetically (possibly ignoring case).
331 void
332 do_order(void)
334 int i;
335 long *lp;
336 STR *fp;
338 Sort_1 = fopen(Infile, "r");
339 Sort_2 = fopen(Infile, "r");
340 qsort((char *)Firstch, (int)Tbl.str_numstr, sizeof(*Firstch), cmp_str);
341 i = Tbl.str_numstr;
342 lp = Seekpts;
343 fp = Firstch;
344 while (i--)
345 *lp++ = fp++->pos;
346 fclose(Sort_1);
347 fclose(Sort_2);
348 Tbl.str_flags |= STR_ORDERED;
351 static int
352 collate_range_cmp (int c1, int c2)
354 static char s1[2], s2[2];
355 int ret;
357 c1 &= UCHAR_MAX;
358 c2 &= UCHAR_MAX;
359 if (c1 == c2)
360 return (0);
361 s1[0] = c1;
362 s2[0] = c2;
363 if ((ret = strcoll(s1, s2)) != 0)
364 return (ret);
365 return (c1 - c2);
369 * cmp_str:
370 * Compare two strings in the file
373 cmp_str(const void *s1, const void *s2)
375 const STR *p1, *p2;
376 int c1, c2, n1, n2, r;
378 #define SET_N(nf,ch) (nf = (ch == '\n'))
379 #define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char)Delimch && nf))
381 p1 = (const STR *)s1;
382 p2 = (const STR *)s2;
384 c1 = (unsigned char)p1->first;
385 c2 = (unsigned char)p2->first;
386 if ((r = collate_range_cmp(c1, c2)) != 0)
387 return r;
389 fseek(Sort_1, p1->pos, SEEK_SET);
390 fseek(Sort_2, p2->pos, SEEK_SET);
392 n1 = false;
393 n2 = false;
394 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
395 SET_N(n1, c1);
396 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
397 SET_N(n2, c2);
399 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
400 if (Iflag) {
401 if (isupper(c1))
402 c1 = tolower(c1);
403 if (isupper(c2))
404 c2 = tolower(c2);
406 if ((r = collate_range_cmp(c1, c2)) != 0)
407 return r;
408 SET_N(n1, c1);
409 SET_N(n2, c2);
410 c1 = getc(Sort_1);
411 c2 = getc(Sort_2);
413 if (IS_END(c1, n1))
414 c1 = 0;
415 if (IS_END(c2, n2))
416 c2 = 0;
418 return (collate_range_cmp(c1, c2));
422 * randomize:
423 * Randomize the order of the string table. We must be careful
424 * not to randomize across delimiter boundaries. All
425 * randomization is done within each block.
427 void
428 randomize(void)
430 int cnt, i;
431 long tmp;
432 long *sp;
434 srandomdev();
436 Tbl.str_flags |= STR_RANDOM;
437 cnt = Tbl.str_numstr;
440 * move things around randomly
443 for (sp = Seekpts; cnt > 0; cnt--, sp++) {
444 i = random() % cnt;
445 tmp = sp[0];
446 sp[0] = sp[i];
447 sp[i] = tmp;