xref: /original-bsd/games/fortune/unstr/unstr.c (revision b3f911e0)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * 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 are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #ifndef lint
22 char copyright[] =
23 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
24  All rights reserved.\n";
25 #endif /* not lint */
26 
27 #ifndef lint
28 static char sccsid[] = "@(#)unstr.c	5.6 (Berkeley) 12/15/89";
29 #endif /* not lint */
30 
31 /*
32  *	This program un-does what "strfile" makes, thereby obtaining the
33  * original file again.  This can be invoked with the name of the output
34  * file, the input file, or both. If invoked with only a single argument
35  * ending in ".dat", it is pressumed to be the input file and the output
36  * file will be the same stripped of the ".dat".  If the single argument
37  * doesn't end in ".dat", then it is presumed to be the output file, and
38  * the input file is that name prepended by a ".dat".  If both are given
39  * they are treated literally as the input and output files.
40  *
41  *	Ken Arnold		Aug 13, 1978
42  */
43 
44 # include	<machine/endian.h>
45 # include	<sys/param.h>
46 # include	"strfile.h"
47 # include	<stdio.h>
48 # include	<ctype.h>
49 
50 # ifndef MAXPATHLEN
51 # define	MAXPATHLEN	1024
52 # endif	/* MAXPATHLEN */
53 
54 char	*Infile,			/* name of input file */
55 	Datafile[MAXPATHLEN],		/* name of data file */
56 	Delimch;			/* delimiter character */
57 
58 FILE	*Inf, *Dataf;
59 
60 char	*strcat(), *strcpy();
61 
62 /* ARGSUSED */
63 main(ac, av)
64 int	ac;
65 char	**av;
66 {
67 	static STRFILE	tbl;		/* description table */
68 
69 	getargs(av);
70 	if ((Inf = fopen(Infile, "r")) == NULL) {
71 		perror(Infile);
72 		exit(1);
73 	}
74 	if ((Dataf = fopen(Datafile, "r")) == NULL) {
75 		perror(Datafile);
76 		exit(1);
77 	}
78 	(void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
79 	tbl.str_version = ntohl(tbl.str_version);
80 	tbl.str_numstr = ntohl(tbl.str_numstr);
81 	tbl.str_longlen = ntohl(tbl.str_longlen);
82 	tbl.str_shortlen = ntohl(tbl.str_shortlen);
83 	tbl.str_flags = ntohl(tbl.str_flags);
84 	if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
85 		fprintf(stderr, "nothing to do -- table in file order\n");
86 		exit(1);
87 	}
88 	Delimch = tbl.str_delim;
89 	order_unstr(&tbl);
90 	(void) fclose(Inf);
91 	(void) fclose(Dataf);
92 	exit(0);
93 }
94 
95 getargs(av)
96 register char	*av[];
97 {
98 	if (!*++av) {
99 		(void) fprintf(stderr, "usage: unstr datafile\n");
100 		exit(1);
101 	}
102 	Infile = *av;
103 	(void) strcpy(Datafile, Infile);
104 	(void) strcat(Datafile, ".dat");
105 }
106 
107 order_unstr(tbl)
108 register STRFILE	*tbl;
109 {
110 	register int	i;
111 	register char	*sp;
112 	auto off_t	pos;
113 	char		buf[BUFSIZ];
114 
115 	for (i = 0; i < tbl->str_numstr; i++) {
116 		(void) fread((char *) &pos, 1, sizeof pos, Dataf);
117 		(void) fseek(Inf, ntohl(pos), 0);
118 		if (i != 0)
119 			(void) printf("%c\n", Delimch);
120 		for (;;) {
121 			sp = fgets(buf, sizeof buf, Inf);
122 			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
123 				break;
124 			else
125 				fputs(sp, stdout);
126 		}
127 	}
128 }
129