xref: /netbsd-src/games/fortune/strfile/strfile.c (revision 5a58ccfbecb506b305b6ec0fa77e4caac7faed71)
1 /*	$NetBSD: strfile.c,v 1.44 2022/08/07 11:06:18 andvar Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ken Arnold.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38 
39 #ifdef __NetBSD__
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
43  The Regents of the University of California.  All rights reserved.");
44 #endif /* not lint */
45 
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)strfile.c	8.1 (Berkeley) 5/31/93";
49 #else
50 __RCSID("$NetBSD: strfile.c,v 1.44 2022/08/07 11:06:18 andvar Exp $");
51 #endif
52 #endif /* not lint */
53 #endif /* __NetBSD__ */
54 
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <ctype.h>
58 #include <stdarg.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
64 #include <inttypes.h>
65 #include <err.h>
66 
67 #include "strfile.h"
68 
69 #ifndef MAXPATHLEN
70 #define	MAXPATHLEN	1024
71 #endif	/* MAXPATHLEN */
72 
73 /*
74  *	This program takes a file composed of strings separated by
75  * lines starting with two consecutive delimiting character (default
76  * character is '%') and creates another file which consists of a table
77  * describing the file (structure from "strfile.h"), a table of seek
78  * pointers to the start of the strings, and the strings, each terminated
79  * by a null byte.  Usage:
80  *
81  *	% strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
82  *
83  *	c - Change delimiting character from '%' to 'C'
84  *	s - Silent.  Give no summary of data processed at the end of
85  *	    the run.
86  *	o - order the strings in alphabetic order
87  *	i - if ordering, ignore case
88  *	r - randomize the order of the strings
89  *	x - set rotated bit
90  *
91  *		Ken Arnold	Sept. 7, 1978 --
92  *
93  *	Added ordering options.
94  */
95 
96 # define	STORING_PTRS	(Oflag || Rflag)
97 # define	CHUNKSIZE	512
98 
99 # define	ALLOC(ptr,sz)	do { \
100 			if (ptr == NULL) \
101 				ptr = malloc(CHUNKSIZE * sizeof *ptr); \
102 			else if (((sz) + 1) % CHUNKSIZE == 0) \
103 				ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
104 			if (ptr == NULL) \
105 				err(1, "out of space"); \
106 		} while (0)
107 
108 typedef struct {
109 	char	first;
110 	off_t	pos;
111 } STR;
112 
113 static char *Infile = NULL;		/* input file name */
114 static char Outfile[MAXPATHLEN] = "";	/* output file name */
115 static char Delimch = '%';		/* delimiting character */
116 
117 static int Sflag	= 0;		/* silent run flag */
118 static int Oflag	= 0;		/* ordering flag */
119 static int Iflag	= 0;		/* ignore case flag */
120 static int Rflag	= 0;		/* randomize order flag */
121 static int Xflag	= 0;		/* set rotated bit */
122 static long Num_pts	= 0;		/* number of pointers/strings */
123 
124 static off_t *Seekpts;
125 
126 static FILE *Sort_1, *Sort_2;		/* pointers for sorting */
127 
128 static STRFILE Tbl;			/* statistics table */
129 
130 static STR *Firstch;			/* first chars of each string */
131 
132 
133 static uint32_t h2nl(uint32_t h);
134 static void getargs(int argc, char **argv);
135 static void usage(const char *, ...) __dead __printflike(1, 2);
136 static void add_offset(FILE *fp, off_t off);
137 static void do_order(void);
138 static int cmp_str(const void *vp1, const void *vp2);
139 static void randomize(void);
140 static void fwrite_be_offt(off_t off, FILE *f);
141 
142 
143 /*
144  * main:
145  *	Drive the sucker.  There are two main modes -- either we store
146  *	the seek pointers, if the table is to be sorted or randomized,
147  *	or we write the pointer directly to the file, if we are to stay
148  *	in file order.  If the former, we allocate and re-allocate in
149  *	CHUNKSIZE blocks; if the latter, we just write each pointer,
150  *	and then seek back to the beginning to write in the table.
151  */
152 int
main(int ac,char ** av)153 main(int ac, char **av)
154 {
155 	char		*sp, dc;
156 	FILE		*inf, *outf;
157 	off_t		last_off, length, pos;
158 	int		first;
159 	char		*nsp;
160 	STR		*fp;
161 	static char	string[257];
162 	long		i;
163 
164 	/* sanity test */
165 	if (sizeof(uint32_t) != 4)
166 		errx(1, "sizeof(uint32_t) != 4");
167 
168 	getargs(ac, av);		/* evalute arguments */
169 	dc = Delimch;
170 	if ((inf = fopen(Infile, "r")) == NULL)
171 		err(1, "open `%s'", Infile);
172 
173 	if ((outf = fopen(Outfile, "w")) == NULL)
174 		err(1, "open `%s'", Outfile);
175 	if (!STORING_PTRS)
176 		(void) fseek(outf, sizeof Tbl, SEEK_SET);
177 
178 	/*
179 	 * Write the strings onto the file
180 	 */
181 
182 	Tbl.str_longlen = 0;
183 	Tbl.str_shortlen = (unsigned int) 0x7fffffff;
184 	Tbl.str_delim = dc;
185 	Tbl.str_version = VERSION;
186 	first = Oflag;
187 	add_offset(outf, ftell(inf));
188 	last_off = 0;
189 	do {
190 		sp = fgets(string, 256, inf);
191 		if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
192 			pos = ftell(inf);
193 			length = pos - last_off - (sp ? strlen(sp) : 0);
194 			last_off = pos;
195 			if (!length)
196 				continue;
197 			add_offset(outf, pos);
198 			if ((off_t)Tbl.str_longlen < length)
199 				Tbl.str_longlen = length;
200 			if ((off_t)Tbl.str_shortlen > length)
201 				Tbl.str_shortlen = length;
202 			first = Oflag;
203 		}
204 		else if (first) {
205 			for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
206 				continue;
207 			ALLOC(Firstch, Num_pts);
208 			fp = &Firstch[Num_pts - 1];
209 			if (Iflag && isupper((unsigned char)*nsp))
210 				fp->first = tolower((unsigned char)*nsp);
211 			else
212 				fp->first = *nsp;
213 			fp->pos = Seekpts[Num_pts - 1];
214 			first = 0;
215 		}
216 	} while (sp != NULL);
217 
218 	/*
219 	 * write the tables in
220 	 */
221 
222 	(void) fclose(inf);
223 
224 	if (Oflag)
225 		do_order();
226 	else if (Rflag)
227 		randomize();
228 
229 	if (Xflag)
230 		Tbl.str_flags |= STR_ROTATED;
231 
232 	if (!Sflag) {
233 		printf("\"%s\" created\n", Outfile);
234 		if (Num_pts == 2)
235 			puts("There was 1 string");
236 		else
237 			printf("There were %d strings\n", (int)(Num_pts - 1));
238 		printf("Longest string: %lu byte%s\n", (unsigned long)Tbl.str_longlen,
239 		       Tbl.str_longlen == 1 ? "" : "s");
240 		printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl.str_shortlen,
241 		       Tbl.str_shortlen == 1 ? "" : "s");
242 	}
243 
244 	(void) fseek(outf, (off_t) 0, SEEK_SET);
245 	Tbl.str_version = h2nl(Tbl.str_version);
246 	Tbl.str_numstr = h2nl(Num_pts - 1);
247 	Tbl.str_longlen = h2nl(Tbl.str_longlen);
248 	Tbl.str_shortlen = h2nl(Tbl.str_shortlen);
249 	Tbl.str_flags = h2nl(Tbl.str_flags);
250 	(void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
251 	if (STORING_PTRS) {
252 		for (i = 0; i < Num_pts; i++)
253 			fwrite_be_offt(Seekpts[i], outf);
254 	}
255 	fflush(outf);
256 	if (ferror(outf))
257 		err(1, "fwrite %s", Outfile);
258 	(void) fclose(outf);
259 	exit(0);
260 }
261 
262 /*
263  *	This routine evaluates arguments from the command line
264  */
265 static void
getargs(int argc,char ** argv)266 getargs(int argc, char **argv)
267 {
268 	int	ch;
269 	extern	int optind;
270 	extern	char *optarg;
271 	size_t	len;
272 
273 	while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
274 		switch(ch) {
275 		case 'c':			/* new delimiting char */
276 			Delimch = *optarg;
277 			if (!isascii(Delimch)) {
278 				printf("bad delimiting character: '\\%o\n'",
279 				       Delimch);
280 			}
281 			break;
282 		case 'i':			/* ignore case in ordering */
283 			Iflag++;
284 			break;
285 		case 'o':			/* order strings */
286 			Oflag++;
287 			break;
288 		case 'r':			/* randomize pointers */
289 			Rflag++;
290 			break;
291 		case 's':			/* silent */
292 			Sflag++;
293 			break;
294 		case 'x':			/* set the rotated bit */
295 			Xflag++;
296 			break;
297 		case '?':
298 		default:
299 			usage(NULL);
300 		}
301 	argv += optind;
302 
303 	if (*argv) {
304 		Infile = *argv;
305 		if (*++argv) {
306 			len = strlcpy(Outfile, *argv, sizeof(Outfile));
307 			if (len >= sizeof(Outfile)) {
308 				usage("Too long output filename");
309 			}
310 		}
311 	}
312 	if (!Infile) {
313 		usage("No input file name");
314 	}
315 	if (*Outfile == '\0') {
316 		len = snprintf(Outfile, sizeof(Outfile), "%s.dat", Infile);
317 		if (len >= sizeof(Outfile)) {
318 			usage("Too long input filename");
319 		}
320 	}
321 }
322 
323 static void
usage(const char * fmt,...)324 usage(const char *fmt, ...)
325 {
326 	if (fmt) {
327 		va_list ap;
328 		va_start(ap, fmt);
329 		vwarnx(fmt, ap);
330 		va_end(ap);
331 	}
332 	(void) fprintf(stderr,
333 	    "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
334 	    getprogname());
335 	exit(1);
336 }
337 
338 /*
339  * add_offset:
340  *	Add an offset to the list, or write it out, as appropriate.
341  */
342 static void
add_offset(FILE * fp,off_t off)343 add_offset(FILE *fp, off_t off)
344 {
345 
346 	if (!STORING_PTRS) {
347 		fwrite_be_offt(off, fp);
348 	} else {
349 		ALLOC(Seekpts, Num_pts + 1);
350 		Seekpts[Num_pts] = off;
351 	}
352 	Num_pts++;
353 }
354 
355 /*
356  * do_order:
357  *	Order the strings alphabetically (possibly ignoring case).
358  */
359 static void
do_order(void)360 do_order(void)
361 {
362 	int	i;
363 	off_t	*lp;
364 	STR	*fp;
365 
366 	Sort_1 = fopen(Infile, "r");
367 	Sort_2 = fopen(Infile, "r");
368 	qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
369 	i = Tbl.str_numstr;
370 	lp = Seekpts;
371 	fp = Firstch;
372 	while (i--)
373 		*lp++ = fp++->pos;
374 	(void) fclose(Sort_1);
375 	(void) fclose(Sort_2);
376 	Tbl.str_flags |= STR_ORDERED;
377 }
378 
379 static int
cmp_str(const void * vp1,const void * vp2)380 cmp_str(const void *vp1, const void *vp2)
381 {
382 	const STR	*p1, *p2;
383 	int	c1, c2;
384 	int	n1, n2;
385 
386 	p1 = (const STR *)vp1;
387 	p2 = (const STR *)vp2;
388 
389 # define	SET_N(nf,ch)	(nf = (ch == '\n'))
390 # define	IS_END(ch,nf)	(ch == Delimch && nf)
391 
392 	c1 = p1->first;
393 	c2 = p2->first;
394 	if (c1 != c2)
395 		return c1 - c2;
396 
397 	(void) fseek(Sort_1, p1->pos, SEEK_SET);
398 	(void) fseek(Sort_2, p2->pos, SEEK_SET);
399 
400 	n1 = 0;
401 	n2 = 0;
402 	while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
403 		SET_N(n1, c1);
404 	while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
405 		SET_N(n2, c2);
406 
407 	while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
408 		if (Iflag) {
409 			if (isupper(c1))
410 				c1 = tolower(c1);
411 			if (isupper(c2))
412 				c2 = tolower(c2);
413 		}
414 		if (c1 != c2)
415 			return c1 - c2;
416 		SET_N(n1, c1);
417 		SET_N(n2, c2);
418 		c1 = getc(Sort_1);
419 		c2 = getc(Sort_2);
420 	}
421 	if (IS_END(c1, n1))
422 		c1 = 0;
423 	if (IS_END(c2, n2))
424 		c2 = 0;
425 	return c1 - c2;
426 }
427 
428 /*
429  * randomize:
430  *	Randomize the order of the string table.  We must be careful
431  *	not to randomize across delimiter boundaries.  All
432  *	randomization is done within each block.
433  */
434 static void
randomize(void)435 randomize(void)
436 {
437 	int	cnt, i;
438 	off_t	tmp;
439 	off_t	*sp;
440 
441 	srandom((int)(time(NULL) + getpid()));
442 
443 	Tbl.str_flags |= STR_RANDOM;
444 	cnt = Tbl.str_numstr;
445 
446 	/*
447 	 * move things around randomly
448 	 */
449 
450 	for (sp = Seekpts; cnt > 0; cnt--, sp++) {
451 		i = random() % cnt;
452 		tmp = sp[0];
453 		sp[0] = sp[i];
454 		sp[i] = tmp;
455 	}
456 }
457 
458 /*
459  * fwrite_be_offt:
460  *	Write out the off parameter as a 64 bit big endian number
461  */
462 
463 static void
fwrite_be_offt(off_t off,FILE * f)464 fwrite_be_offt(off_t off, FILE *f)
465 {
466 	int		i;
467 	unsigned char	c[8];
468 
469 	for (i = 7; i >= 0; i--) {
470 		c[i] = off & 0xff;
471 		off >>= 8;
472 	}
473 	fwrite(c, sizeof(c), 1, f);
474 }
475 
476 static uint32_t
h2nl(uint32_t h)477 h2nl(uint32_t h)
478 {
479         unsigned char c[4];
480         uint32_t rv;
481 
482         c[0] = (h >> 24) & 0xff;
483         c[1] = (h >> 16) & 0xff;
484         c[2] = (h >>  8) & 0xff;
485         c[3] = (h >>  0) & 0xff;
486         memcpy(&rv, c, sizeof rv);
487 
488         return (rv);
489 }
490