unstr.c revision 1.3 1 /* $NetBSD: unstr.c,v 1.3 1995/03/23 08:29:00 cgd 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 #ifndef lint
40 static char copyright[] =
41 "@(#) 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 static char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93";
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 <machine/endian.h>
63 # include <sys/param.h>
64 # include "strfile.h"
65 # include <stdio.h>
66 # include <ctype.h>
67
68 # ifndef MAXPATHLEN
69 # define MAXPATHLEN 1024
70 # endif /* MAXPATHLEN */
71
72 char *Infile, /* name of input file */
73 Datafile[MAXPATHLEN], /* name of data file */
74 Delimch; /* delimiter character */
75
76 FILE *Inf, *Dataf;
77
78 char *strcat(), *strcpy();
79
80 /* ARGSUSED */
81 main(ac, av)
82 int ac;
83 char **av;
84 {
85 static STRFILE tbl; /* description table */
86
87 getargs(av);
88 if ((Inf = fopen(Infile, "r")) == NULL) {
89 perror(Infile);
90 exit(1);
91 }
92 if ((Dataf = fopen(Datafile, "r")) == NULL) {
93 perror(Datafile);
94 exit(1);
95 }
96 (void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
97 tbl.str_version = ntohl(tbl.str_version);
98 tbl.str_numstr = ntohl(tbl.str_numstr);
99 tbl.str_longlen = ntohl(tbl.str_longlen);
100 tbl.str_shortlen = ntohl(tbl.str_shortlen);
101 tbl.str_flags = ntohl(tbl.str_flags);
102 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
103 fprintf(stderr, "nothing to do -- table in file order\n");
104 exit(1);
105 }
106 Delimch = tbl.str_delim;
107 order_unstr(&tbl);
108 (void) fclose(Inf);
109 (void) fclose(Dataf);
110 exit(0);
111 }
112
113 getargs(av)
114 register char *av[];
115 {
116 if (!*++av) {
117 (void) fprintf(stderr, "usage: unstr datafile\n");
118 exit(1);
119 }
120 Infile = *av;
121 (void) strcpy(Datafile, Infile);
122 (void) strcat(Datafile, ".dat");
123 }
124
125 order_unstr(tbl)
126 register STRFILE *tbl;
127 {
128 register int i;
129 register char *sp;
130 auto off_t pos;
131 char buf[BUFSIZ];
132
133 for (i = 0; i < tbl->str_numstr; i++) {
134 (void) fread((char *) &pos, 1, sizeof pos, Dataf);
135 (void) fseek(Inf, ntohl(pos), 0);
136 if (i != 0)
137 (void) printf("%c\n", Delimch);
138 for (;;) {
139 sp = fgets(buf, sizeof buf, Inf);
140 if (sp == NULL || STR_ENDSTRING(sp, *tbl))
141 break;
142 else
143 fputs(sp, stdout);
144 }
145 }
146 }
147