xref: /freebsd/usr.bin/fortune/unstr/unstr.c (revision 6ae1554a5d9b318f8ad53ccc39fa5a961403da73)
1*6ae1554aSColin Percival /*-
2*6ae1554aSColin Percival  * Copyright (c) 1991, 1993
3*6ae1554aSColin Percival  *	The Regents of the University of California.  All rights reserved.
4*6ae1554aSColin Percival  *
5*6ae1554aSColin Percival  * This code is derived from software contributed to Berkeley by
6*6ae1554aSColin Percival  * Ken Arnold.
7*6ae1554aSColin Percival  *
8*6ae1554aSColin Percival  * Redistribution and use in source and binary forms, with or without
9*6ae1554aSColin Percival  * modification, are permitted provided that the following conditions
10*6ae1554aSColin Percival  * are met:
11*6ae1554aSColin Percival  * 1. Redistributions of source code must retain the above copyright
12*6ae1554aSColin Percival  *    notice, this list of conditions and the following disclaimer.
13*6ae1554aSColin Percival  * 2. Redistributions in binary form must reproduce the above copyright
14*6ae1554aSColin Percival  *    notice, this list of conditions and the following disclaimer in the
15*6ae1554aSColin Percival  *    documentation and/or other materials provided with the distribution.
16*6ae1554aSColin Percival  * 3. Neither the name of the University nor the names of its contributors
17*6ae1554aSColin Percival  *    may be used to endorse or promote products derived from this software
18*6ae1554aSColin Percival  *    without specific prior written permission.
19*6ae1554aSColin Percival  *
20*6ae1554aSColin Percival  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*6ae1554aSColin Percival  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*6ae1554aSColin Percival  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*6ae1554aSColin Percival  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*6ae1554aSColin Percival  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*6ae1554aSColin Percival  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*6ae1554aSColin Percival  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*6ae1554aSColin Percival  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*6ae1554aSColin Percival  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*6ae1554aSColin Percival  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*6ae1554aSColin Percival  * SUCH DAMAGE.
31*6ae1554aSColin Percival  */
32*6ae1554aSColin Percival 
33*6ae1554aSColin Percival #if 0
34*6ae1554aSColin Percival #ifndef lint
35*6ae1554aSColin Percival static const char copyright[] =
36*6ae1554aSColin Percival "@(#) Copyright (c) 1991, 1993\n\
37*6ae1554aSColin Percival 	The Regents of the University of California.  All rights reserved.\n";
38*6ae1554aSColin Percival #endif /* not lint */
39*6ae1554aSColin Percival 
40*6ae1554aSColin Percival #ifndef lint
41*6ae1554aSColin Percival static const char sccsid[] = "@(#)unstr.c     8.1 (Berkeley) 5/31/93";
42*6ae1554aSColin Percival #endif /* not lint */
43*6ae1554aSColin Percival #endif
44*6ae1554aSColin Percival #include <sys/cdefs.h>
45*6ae1554aSColin Percival __FBSDID("$FreeBSD$");
46*6ae1554aSColin Percival 
47*6ae1554aSColin Percival /*
48*6ae1554aSColin Percival  *	This program un-does what "strfile" makes, thereby obtaining the
49*6ae1554aSColin Percival  * original file again.  This can be invoked with the name of the output
50*6ae1554aSColin Percival  * file, the input file, or both. If invoked with only a single argument
51*6ae1554aSColin Percival  * ending in ".dat", it is pressumed to be the input file and the output
52*6ae1554aSColin Percival  * file will be the same stripped of the ".dat".  If the single argument
53*6ae1554aSColin Percival  * doesn't end in ".dat", then it is presumed to be the output file, and
54*6ae1554aSColin Percival  * the input file is that name prepended by a ".dat".  If both are given
55*6ae1554aSColin Percival  * they are treated literally as the input and output files.
56*6ae1554aSColin Percival  *
57*6ae1554aSColin Percival  *	Ken Arnold		Aug 13, 1978
58*6ae1554aSColin Percival  */
59*6ae1554aSColin Percival 
60*6ae1554aSColin Percival #include <sys/param.h>
61*6ae1554aSColin Percival #include <sys/endian.h>
62*6ae1554aSColin Percival #include <ctype.h>
63*6ae1554aSColin Percival #include <err.h>
64*6ae1554aSColin Percival #include <stdio.h>
65*6ae1554aSColin Percival #include <stdlib.h>
66*6ae1554aSColin Percival #include <string.h>
67*6ae1554aSColin Percival 
68*6ae1554aSColin Percival #include "strfile.h"
69*6ae1554aSColin Percival 
70*6ae1554aSColin Percival static char	*Infile,		/* name of input file */
71*6ae1554aSColin Percival 		Datafile[MAXPATHLEN],	/* name of data file */
72*6ae1554aSColin Percival 		Delimch;		/* delimiter character */
73*6ae1554aSColin Percival 
74*6ae1554aSColin Percival static FILE	*Inf, *Dataf;
75*6ae1554aSColin Percival 
76*6ae1554aSColin Percival static void order_unstr(STRFILE *);
77*6ae1554aSColin Percival 
78*6ae1554aSColin Percival /* ARGSUSED */
79*6ae1554aSColin Percival int
80*6ae1554aSColin Percival main(int argc, char *argv[])
81*6ae1554aSColin Percival {
82*6ae1554aSColin Percival 	static STRFILE tbl;		/* description table */
83*6ae1554aSColin Percival 
84*6ae1554aSColin Percival 	if (argc != 2) {
85*6ae1554aSColin Percival 		fprintf(stderr, "usage: unstr datafile\n");
86*6ae1554aSColin Percival 		exit(1);
87*6ae1554aSColin Percival 	}
88*6ae1554aSColin Percival 	Infile = argv[1];
89*6ae1554aSColin Percival 	strcpy(Datafile, Infile);
90*6ae1554aSColin Percival 	strcat(Datafile, ".dat");
91*6ae1554aSColin Percival 	if ((Inf = fopen(Infile, "r")) == NULL)
92*6ae1554aSColin Percival 		err(1, "%s", Infile);
93*6ae1554aSColin Percival 	if ((Dataf = fopen(Datafile, "r")) == NULL)
94*6ae1554aSColin Percival 		err(1, "%s", Datafile);
95*6ae1554aSColin Percival 	fread((char *)&tbl, sizeof(tbl), 1, Dataf);
96*6ae1554aSColin Percival 	tbl.str_version = be32toh(tbl.str_version);
97*6ae1554aSColin Percival 	tbl.str_numstr = be32toh(tbl.str_numstr);
98*6ae1554aSColin Percival 	tbl.str_longlen = be32toh(tbl.str_longlen);
99*6ae1554aSColin Percival 	tbl.str_shortlen = be32toh(tbl.str_shortlen);
100*6ae1554aSColin Percival 	tbl.str_flags = be32toh(tbl.str_flags);
101*6ae1554aSColin Percival 	if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)))
102*6ae1554aSColin Percival 		errx(1, "nothing to do -- table in file order");
103*6ae1554aSColin Percival 	Delimch = tbl.str_delim;
104*6ae1554aSColin Percival 	order_unstr(&tbl);
105*6ae1554aSColin Percival 	fclose(Inf);
106*6ae1554aSColin Percival 	fclose(Dataf);
107*6ae1554aSColin Percival 	exit(0);
108*6ae1554aSColin Percival }
109*6ae1554aSColin Percival 
110*6ae1554aSColin Percival static void
111*6ae1554aSColin Percival order_unstr(STRFILE *tbl)
112*6ae1554aSColin Percival {
113*6ae1554aSColin Percival 	uint32_t i;
114*6ae1554aSColin Percival 	char *sp;
115*6ae1554aSColin Percival 	off_t pos;
116*6ae1554aSColin Percival 	char buf[BUFSIZ];
117*6ae1554aSColin Percival 
118*6ae1554aSColin Percival 	for (i = 0; i < tbl->str_numstr; i++) {
119*6ae1554aSColin Percival 		fread(&pos, 1, sizeof(pos), Dataf);
120*6ae1554aSColin Percival 		fseeko(Inf, be64toh(pos), SEEK_SET);
121*6ae1554aSColin Percival 		if (i != 0)
122*6ae1554aSColin Percival 			printf("%c\n", Delimch);
123*6ae1554aSColin Percival 		for (;;) {
124*6ae1554aSColin Percival 			sp = fgets(buf, sizeof(buf), Inf);
125*6ae1554aSColin Percival 			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
126*6ae1554aSColin Percival 				break;
127*6ae1554aSColin Percival 			else
128*6ae1554aSColin Percival 				fputs(sp, stdout);
129*6ae1554aSColin Percival 		}
130*6ae1554aSColin Percival 	}
131*6ae1554aSColin Percival }
132