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