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