xref: /netbsd/games/fortune/unstr/unstr.c (revision bf9ec67e)
1 /*	$NetBSD: unstr.c,v 1.7 1999/09/18 19:38:50 jsm Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
42 	The Regents of the University of California.  All rights reserved.\n");
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)unstr.c	8.1 (Berkeley) 5/31/93";
48 #else
49 __RCSID("$NetBSD: unstr.c,v 1.7 1999/09/18 19:38:50 jsm Exp $");
50 #endif
51 #endif /* not lint */
52 
53 /*
54  *	This program un-does what "strfile" makes, thereby obtaining the
55  * original file again.  This can be invoked with the name of the output
56  * file, the input file, or both. If invoked with only a single argument
57  * ending in ".dat", it is pressumed to be the input file and the output
58  * file will be the same stripped of the ".dat".  If the single argument
59  * doesn't end in ".dat", then it is presumed to be the output file, and
60  * the input file is that name prepended by a ".dat".  If both are given
61  * they are treated literally as the input and output files.
62  *
63  *	Ken Arnold		Aug 13, 1978
64  */
65 
66 # include	<sys/types.h>
67 # include	<sys/param.h>
68 # include	<machine/endian.h>
69 # include	<ctype.h>
70 # include	<err.h>
71 # include	<stdio.h>
72 # include	<string.h>
73 # include	"strfile.h"
74 
75 # ifndef MAXPATHLEN
76 # define	MAXPATHLEN	1024
77 # endif	/* MAXPATHLEN */
78 
79 char	*Infile,			/* name of input file */
80 	Datafile[MAXPATHLEN],		/* name of data file */
81 	Delimch;			/* delimiter character */
82 
83 FILE	*Inf, *Dataf;
84 
85 void	getargs __P((char *[]));
86 int	main __P((int, char *[]));
87 void	order_unstr __P((STRFILE *));
88 
89 /* ARGSUSED */
90 int
91 main(ac, av)
92 	int	ac;
93 	char	**av;
94 {
95 	static STRFILE	tbl;		/* description table */
96 
97 	getargs(av);
98 	if ((Inf = fopen(Infile, "r")) == NULL)
99 		err(1, "fopen %s", Infile);
100 	if ((Dataf = fopen(Datafile, "r")) == NULL)
101 		err(1, "fopen %s", Datafile);
102 	(void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
103 	BE32TOH(tbl.str_version);
104 	BE32TOH(tbl.str_numstr);
105 	BE32TOH(tbl.str_longlen);
106 	BE32TOH(tbl.str_shortlen);
107 	BE32TOH(tbl.str_flags);
108 	if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
109 		fprintf(stderr, "nothing to do -- table in file order\n");
110 		exit(1);
111 	}
112 	Delimch = tbl.str_delim;
113 	order_unstr(&tbl);
114 	(void) fclose(Inf);
115 	(void) fclose(Dataf);
116 	exit(0);
117 }
118 
119 void
120 getargs(av)
121 	char	*av[];
122 {
123 	if (!*++av) {
124 		(void) fprintf(stderr, "usage: unstr datafile\n");
125 		exit(1);
126 	}
127 	Infile = *av;
128 	(void) strcpy(Datafile, Infile);
129 	(void) strcat(Datafile, ".dat");
130 }
131 
132 void
133 order_unstr(tbl)
134 	STRFILE	*tbl;
135 {
136 	unsigned int	i;
137 	char	*sp;
138 	off_t	pos;
139 	char	buf[BUFSIZ];
140 
141 	for (i = 0; i < tbl->str_numstr; i++) {
142 		(void) fread((char *) &pos, 1, sizeof pos, Dataf);
143 		(void) fseek(Inf, be64toh(pos), SEEK_SET);
144 		if (i != 0)
145 			(void) printf("%c\n", Delimch);
146 		for (;;) {
147 			sp = fgets(buf, sizeof buf, Inf);
148 			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
149 				break;
150 			else
151 				fputs(sp, stdout);
152 		}
153 	}
154 }
155