1 /* $NetBSD: unstr.c,v 1.12 2007/12/15 19:44:40 perry Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: unstr.c,v 1.12 2007/12/15 19:44:40 perry Exp $");
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.
59 * Ken Arnold Aug 13, 1978
62 # include <sys/types.h>
63 # include <sys/param.h>
64 # include <sys/endian.h>
73 # define MAXPATHLEN 1024
74 # endif /* MAXPATHLEN */
76 char *Infile
, /* name of input file */
77 Datafile
[MAXPATHLEN
], /* name of data file */
78 Delimch
; /* delimiter character */
82 void getargs(char *[]);
83 int main(int, char *[]);
84 void order_unstr(STRFILE
*);
92 static STRFILE tbl
; /* description table */
95 if ((Inf
= fopen(Infile
, "r")) == NULL
)
96 err(1, "fopen %s", Infile
);
97 if ((Dataf
= fopen(Datafile
, "r")) == NULL
)
98 err(1, "fopen %s", Datafile
);
99 (void) fread((char *) &tbl
, sizeof tbl
, 1, Dataf
);
100 BE32TOH(tbl
.str_version
);
101 BE32TOH(tbl
.str_numstr
);
102 BE32TOH(tbl
.str_longlen
);
103 BE32TOH(tbl
.str_shortlen
);
104 BE32TOH(tbl
.str_flags
);
105 if (!(tbl
.str_flags
& (STR_ORDERED
| STR_RANDOM
))) {
106 fprintf(stderr
, "nothing to do -- table in file order\n");
109 Delimch
= tbl
.str_delim
;
112 (void) fclose(Dataf
);
121 (void) fprintf(stderr
, "usage: unstr datafile\n");
125 (void) strcpy(Datafile
, Infile
);
126 (void) strcat(Datafile
, ".dat");
138 for (i
= 0; i
< tbl
->str_numstr
; i
++) {
139 (void) fread((char *) &pos
, 1, sizeof pos
, Dataf
);
140 (void) fseek(Inf
, be64toh(pos
), SEEK_SET
);
142 (void) printf("%c\n", Delimch
);
144 sp
= fgets(buf
, sizeof buf
, Inf
);
145 if (sp
== NULL
|| STR_ENDSTRING(sp
, *tbl
))