xref: /freebsd/usr.bin/fortune/unstr/unstr.c (revision e0c4386e)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ken Arnold.
7  *
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.
19  *
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.
31  */
32 
33 /*
34  *	This program un-does what "strfile" makes, thereby obtaining the
35  * original file again.  This can be invoked with the name of the output
36  * file, the input file, or both. If invoked with only a single argument
37  * ending in ".dat", it is pressumed to be the input file and the output
38  * file will be the same stripped of the ".dat".  If the single argument
39  * doesn't end in ".dat", then it is presumed to be the output file, and
40  * the input file is that name prepended by a ".dat".  If both are given
41  * they are treated literally as the input and output files.
42  *
43  *	Ken Arnold		Aug 13, 1978
44  */
45 
46 #include <sys/param.h>
47 #include <sys/endian.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #include "strfile.h"
55 
56 static char	*Infile,		/* name of input file */
57 		Datafile[MAXPATHLEN],	/* name of data file */
58 		Delimch;		/* delimiter character */
59 
60 static FILE	*Inf, *Dataf;
61 
62 static void order_unstr(STRFILE *);
63 
64 /* ARGSUSED */
65 int
66 main(int argc, char *argv[])
67 {
68 	static STRFILE tbl;		/* description table */
69 
70 	if (argc != 2) {
71 		fprintf(stderr, "usage: unstr datafile\n");
72 		exit(1);
73 	}
74 	Infile = argv[1];
75 	if ((size_t)snprintf(Datafile, sizeof(Datafile), "%s.dat", Infile) >=
76 	    sizeof(Datafile))
77 		errx(1, "%s name too long", Infile);
78 	if ((Inf = fopen(Infile, "r")) == NULL)
79 		err(1, "%s", Infile);
80 	if ((Dataf = fopen(Datafile, "r")) == NULL)
81 		err(1, "%s", Datafile);
82 	if (fread((char *)&tbl, sizeof(tbl), 1, Dataf) != 1) {
83 		if (feof(Dataf))
84 			errx(1, "%s read EOF", Datafile);
85 		else
86 			err(1, "%s read", Datafile);
87 	}
88 	tbl.str_version = be32toh(tbl.str_version);
89 	tbl.str_numstr = be32toh(tbl.str_numstr);
90 	tbl.str_longlen = be32toh(tbl.str_longlen);
91 	tbl.str_shortlen = be32toh(tbl.str_shortlen);
92 	tbl.str_flags = be32toh(tbl.str_flags);
93 	if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)))
94 		errx(1, "nothing to do -- table in file order");
95 	Delimch = tbl.str_delim;
96 	order_unstr(&tbl);
97 	fclose(Inf);
98 	fclose(Dataf);
99 	exit(0);
100 }
101 
102 static void
103 order_unstr(STRFILE *tbl)
104 {
105 	uint32_t i;
106 	char *sp;
107 	off_t pos;
108 	char buf[BUFSIZ];
109 
110 	for (i = 0; i < tbl->str_numstr; i++) {
111 		fread(&pos, 1, sizeof(pos), Dataf);
112 		fseeko(Inf, be64toh(pos), SEEK_SET);
113 		if (i != 0)
114 			printf("%c\n", Delimch);
115 		for (;;) {
116 			sp = fgets(buf, sizeof(buf), Inf);
117 			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
118 				break;
119 			else
120 				fputs(sp, stdout);
121 		}
122 	}
123 }
124