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