xref: /freebsd/usr.bin/fortune/unstr/unstr.c (revision 243e928310d073338c5ec089f0dce238a80b9866)
16ae1554aSColin Percival /*-
26ae1554aSColin Percival  * Copyright (c) 1991, 1993
36ae1554aSColin Percival  *	The Regents of the University of California.  All rights reserved.
46ae1554aSColin Percival  *
56ae1554aSColin Percival  * This code is derived from software contributed to Berkeley by
66ae1554aSColin Percival  * Ken Arnold.
76ae1554aSColin Percival  *
86ae1554aSColin Percival  * Redistribution and use in source and binary forms, with or without
96ae1554aSColin Percival  * modification, are permitted provided that the following conditions
106ae1554aSColin Percival  * are met:
116ae1554aSColin Percival  * 1. Redistributions of source code must retain the above copyright
126ae1554aSColin Percival  *    notice, this list of conditions and the following disclaimer.
136ae1554aSColin Percival  * 2. Redistributions in binary form must reproduce the above copyright
146ae1554aSColin Percival  *    notice, this list of conditions and the following disclaimer in the
156ae1554aSColin Percival  *    documentation and/or other materials provided with the distribution.
166ae1554aSColin Percival  * 3. Neither the name of the University nor the names of its contributors
176ae1554aSColin Percival  *    may be used to endorse or promote products derived from this software
186ae1554aSColin Percival  *    without specific prior written permission.
196ae1554aSColin Percival  *
206ae1554aSColin Percival  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
216ae1554aSColin Percival  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226ae1554aSColin Percival  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236ae1554aSColin Percival  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
246ae1554aSColin Percival  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256ae1554aSColin Percival  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266ae1554aSColin Percival  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276ae1554aSColin Percival  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286ae1554aSColin Percival  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296ae1554aSColin Percival  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306ae1554aSColin Percival  * SUCH DAMAGE.
316ae1554aSColin Percival  */
326ae1554aSColin Percival 
336ae1554aSColin Percival #if 0
346ae1554aSColin Percival #ifndef lint
356ae1554aSColin Percival static const char copyright[] =
366ae1554aSColin Percival "@(#) Copyright (c) 1991, 1993\n\
376ae1554aSColin Percival 	The Regents of the University of California.  All rights reserved.\n";
386ae1554aSColin Percival #endif /* not lint */
396ae1554aSColin Percival 
406ae1554aSColin Percival #ifndef lint
416ae1554aSColin Percival static const char sccsid[] = "@(#)unstr.c     8.1 (Berkeley) 5/31/93";
426ae1554aSColin Percival #endif /* not lint */
436ae1554aSColin Percival #endif
446ae1554aSColin Percival #include <sys/cdefs.h>
456ae1554aSColin Percival __FBSDID("$FreeBSD$");
466ae1554aSColin Percival 
476ae1554aSColin Percival /*
486ae1554aSColin Percival  *	This program un-does what "strfile" makes, thereby obtaining the
496ae1554aSColin Percival  * original file again.  This can be invoked with the name of the output
506ae1554aSColin Percival  * file, the input file, or both. If invoked with only a single argument
516ae1554aSColin Percival  * ending in ".dat", it is pressumed to be the input file and the output
526ae1554aSColin Percival  * file will be the same stripped of the ".dat".  If the single argument
536ae1554aSColin Percival  * doesn't end in ".dat", then it is presumed to be the output file, and
546ae1554aSColin Percival  * the input file is that name prepended by a ".dat".  If both are given
556ae1554aSColin Percival  * they are treated literally as the input and output files.
566ae1554aSColin Percival  *
576ae1554aSColin Percival  *	Ken Arnold		Aug 13, 1978
586ae1554aSColin Percival  */
596ae1554aSColin Percival 
606ae1554aSColin Percival #include <sys/param.h>
616ae1554aSColin Percival #include <sys/endian.h>
626ae1554aSColin Percival #include <ctype.h>
636ae1554aSColin Percival #include <err.h>
646ae1554aSColin Percival #include <stdio.h>
656ae1554aSColin Percival #include <stdlib.h>
666ae1554aSColin Percival #include <string.h>
676ae1554aSColin Percival 
686ae1554aSColin Percival #include "strfile.h"
696ae1554aSColin Percival 
706ae1554aSColin Percival static char	*Infile,		/* name of input file */
716ae1554aSColin Percival 		Datafile[MAXPATHLEN],	/* name of data file */
726ae1554aSColin Percival 		Delimch;		/* delimiter character */
736ae1554aSColin Percival 
746ae1554aSColin Percival static FILE	*Inf, *Dataf;
756ae1554aSColin Percival 
766ae1554aSColin Percival static void order_unstr(STRFILE *);
776ae1554aSColin Percival 
786ae1554aSColin Percival /* ARGSUSED */
796ae1554aSColin Percival int
806ae1554aSColin Percival main(int argc, char *argv[])
816ae1554aSColin Percival {
826ae1554aSColin Percival 	static STRFILE tbl;		/* description table */
836ae1554aSColin Percival 
846ae1554aSColin Percival 	if (argc != 2) {
856ae1554aSColin Percival 		fprintf(stderr, "usage: unstr datafile\n");
866ae1554aSColin Percival 		exit(1);
876ae1554aSColin Percival 	}
886ae1554aSColin Percival 	Infile = argv[1];
89*243e9283SDon Lewis 	if ((size_t)snprintf(Datafile, sizeof(Datafile), "%s.dat", Infile) >=
90*243e9283SDon Lewis 	    sizeof(Datafile))
91*243e9283SDon Lewis 		errx(1, "%s name too long", Infile);
926ae1554aSColin Percival 	if ((Inf = fopen(Infile, "r")) == NULL)
936ae1554aSColin Percival 		err(1, "%s", Infile);
946ae1554aSColin Percival 	if ((Dataf = fopen(Datafile, "r")) == NULL)
956ae1554aSColin Percival 		err(1, "%s", Datafile);
96*243e9283SDon Lewis 	if (fread((char *)&tbl, sizeof(tbl), 1, Dataf) != 1) {
97*243e9283SDon Lewis 		if (feof(Dataf))
98*243e9283SDon Lewis 			errx(1, "%s read EOF", Datafile);
99*243e9283SDon Lewis 		else
100*243e9283SDon Lewis 			err(1, "%s read", Datafile);
101*243e9283SDon Lewis 	}
1026ae1554aSColin Percival 	tbl.str_version = be32toh(tbl.str_version);
1036ae1554aSColin Percival 	tbl.str_numstr = be32toh(tbl.str_numstr);
1046ae1554aSColin Percival 	tbl.str_longlen = be32toh(tbl.str_longlen);
1056ae1554aSColin Percival 	tbl.str_shortlen = be32toh(tbl.str_shortlen);
1066ae1554aSColin Percival 	tbl.str_flags = be32toh(tbl.str_flags);
1076ae1554aSColin Percival 	if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)))
1086ae1554aSColin Percival 		errx(1, "nothing to do -- table in file order");
1096ae1554aSColin Percival 	Delimch = tbl.str_delim;
1106ae1554aSColin Percival 	order_unstr(&tbl);
1116ae1554aSColin Percival 	fclose(Inf);
1126ae1554aSColin Percival 	fclose(Dataf);
1136ae1554aSColin Percival 	exit(0);
1146ae1554aSColin Percival }
1156ae1554aSColin Percival 
1166ae1554aSColin Percival static void
1176ae1554aSColin Percival order_unstr(STRFILE *tbl)
1186ae1554aSColin Percival {
1196ae1554aSColin Percival 	uint32_t i;
1206ae1554aSColin Percival 	char *sp;
1216ae1554aSColin Percival 	off_t pos;
1226ae1554aSColin Percival 	char buf[BUFSIZ];
1236ae1554aSColin Percival 
1246ae1554aSColin Percival 	for (i = 0; i < tbl->str_numstr; i++) {
1256ae1554aSColin Percival 		fread(&pos, 1, sizeof(pos), Dataf);
1266ae1554aSColin Percival 		fseeko(Inf, be64toh(pos), SEEK_SET);
1276ae1554aSColin Percival 		if (i != 0)
1286ae1554aSColin Percival 			printf("%c\n", Delimch);
1296ae1554aSColin Percival 		for (;;) {
1306ae1554aSColin Percival 			sp = fgets(buf, sizeof(buf), Inf);
1316ae1554aSColin Percival 			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
1326ae1554aSColin Percival 				break;
1336ae1554aSColin Percival 			else
1346ae1554aSColin Percival 				fputs(sp, stdout);
1356ae1554aSColin Percival 		}
1366ae1554aSColin Percival 	}
1376ae1554aSColin Percival }
138