16ae1554aSColin Percival /*-
26ae1554aSColin Percival * Copyright (c) 1991, 1993
36ae1554aSColin Percival * The Regents of the University of California. All rights reserved.
46ae1554aSColin Percival *
56ae1554aSColin Percival * This code is derived from software contributed to Berkeley by
66ae1554aSColin Percival * Ken Arnold.
76ae1554aSColin Percival *
86ae1554aSColin Percival * Redistribution and use in source and binary forms, with or without
96ae1554aSColin Percival * modification, are permitted provided that the following conditions
106ae1554aSColin Percival * are met:
116ae1554aSColin Percival * 1. Redistributions of source code must retain the above copyright
126ae1554aSColin Percival * notice, this list of conditions and the following disclaimer.
136ae1554aSColin Percival * 2. Redistributions in binary form must reproduce the above copyright
146ae1554aSColin Percival * notice, this list of conditions and the following disclaimer in the
156ae1554aSColin Percival * documentation and/or other materials provided with the distribution.
166ae1554aSColin Percival * 3. Neither the name of the University nor the names of its contributors
176ae1554aSColin Percival * may be used to endorse or promote products derived from this software
186ae1554aSColin Percival * without specific prior written permission.
196ae1554aSColin Percival *
206ae1554aSColin Percival * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
216ae1554aSColin Percival * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226ae1554aSColin Percival * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236ae1554aSColin Percival * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
246ae1554aSColin Percival * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256ae1554aSColin Percival * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266ae1554aSColin Percival * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276ae1554aSColin Percival * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286ae1554aSColin Percival * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296ae1554aSColin Percival * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306ae1554aSColin Percival * SUCH DAMAGE.
316ae1554aSColin Percival */
326ae1554aSColin Percival
336ae1554aSColin Percival /*
346ae1554aSColin Percival * This program un-does what "strfile" makes, thereby obtaining the
356ae1554aSColin Percival * original file again. This can be invoked with the name of the output
366ae1554aSColin Percival * file, the input file, or both. If invoked with only a single argument
376ae1554aSColin Percival * ending in ".dat", it is pressumed to be the input file and the output
386ae1554aSColin Percival * file will be the same stripped of the ".dat". If the single argument
396ae1554aSColin Percival * doesn't end in ".dat", then it is presumed to be the output file, and
406ae1554aSColin Percival * the input file is that name prepended by a ".dat". If both are given
416ae1554aSColin Percival * they are treated literally as the input and output files.
426ae1554aSColin Percival *
436ae1554aSColin Percival * Ken Arnold Aug 13, 1978
446ae1554aSColin Percival */
456ae1554aSColin Percival
466ae1554aSColin Percival #include <sys/param.h>
476ae1554aSColin Percival #include <sys/endian.h>
486ae1554aSColin Percival #include <ctype.h>
496ae1554aSColin Percival #include <err.h>
506ae1554aSColin Percival #include <stdio.h>
516ae1554aSColin Percival #include <stdlib.h>
526ae1554aSColin Percival #include <string.h>
536ae1554aSColin Percival
546ae1554aSColin Percival #include "strfile.h"
556ae1554aSColin Percival
566ae1554aSColin Percival static char *Infile, /* name of input file */
576ae1554aSColin Percival Datafile[MAXPATHLEN], /* name of data file */
586ae1554aSColin Percival Delimch; /* delimiter character */
596ae1554aSColin Percival
606ae1554aSColin Percival static FILE *Inf, *Dataf;
616ae1554aSColin Percival
626ae1554aSColin Percival static void order_unstr(STRFILE *);
636ae1554aSColin Percival
646ae1554aSColin Percival /* ARGSUSED */
656ae1554aSColin Percival int
main(int argc,char * argv[])666ae1554aSColin Percival main(int argc, char *argv[])
676ae1554aSColin Percival {
686ae1554aSColin Percival static STRFILE tbl; /* description table */
696ae1554aSColin Percival
706ae1554aSColin Percival if (argc != 2) {
716ae1554aSColin Percival fprintf(stderr, "usage: unstr datafile\n");
726ae1554aSColin Percival exit(1);
736ae1554aSColin Percival }
746ae1554aSColin Percival Infile = argv[1];
75*243e9283SDon Lewis if ((size_t)snprintf(Datafile, sizeof(Datafile), "%s.dat", Infile) >=
76*243e9283SDon Lewis sizeof(Datafile))
77*243e9283SDon Lewis errx(1, "%s name too long", Infile);
786ae1554aSColin Percival if ((Inf = fopen(Infile, "r")) == NULL)
796ae1554aSColin Percival err(1, "%s", Infile);
806ae1554aSColin Percival if ((Dataf = fopen(Datafile, "r")) == NULL)
816ae1554aSColin Percival err(1, "%s", Datafile);
82*243e9283SDon Lewis if (fread((char *)&tbl, sizeof(tbl), 1, Dataf) != 1) {
83*243e9283SDon Lewis if (feof(Dataf))
84*243e9283SDon Lewis errx(1, "%s read EOF", Datafile);
85*243e9283SDon Lewis else
86*243e9283SDon Lewis err(1, "%s read", Datafile);
87*243e9283SDon Lewis }
886ae1554aSColin Percival tbl.str_version = be32toh(tbl.str_version);
896ae1554aSColin Percival tbl.str_numstr = be32toh(tbl.str_numstr);
906ae1554aSColin Percival tbl.str_longlen = be32toh(tbl.str_longlen);
916ae1554aSColin Percival tbl.str_shortlen = be32toh(tbl.str_shortlen);
926ae1554aSColin Percival tbl.str_flags = be32toh(tbl.str_flags);
936ae1554aSColin Percival if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)))
946ae1554aSColin Percival errx(1, "nothing to do -- table in file order");
956ae1554aSColin Percival Delimch = tbl.str_delim;
966ae1554aSColin Percival order_unstr(&tbl);
976ae1554aSColin Percival fclose(Inf);
986ae1554aSColin Percival fclose(Dataf);
996ae1554aSColin Percival exit(0);
1006ae1554aSColin Percival }
1016ae1554aSColin Percival
1026ae1554aSColin Percival static void
order_unstr(STRFILE * tbl)1036ae1554aSColin Percival order_unstr(STRFILE *tbl)
1046ae1554aSColin Percival {
1056ae1554aSColin Percival uint32_t i;
1066ae1554aSColin Percival char *sp;
1076ae1554aSColin Percival off_t pos;
1086ae1554aSColin Percival char buf[BUFSIZ];
1096ae1554aSColin Percival
1106ae1554aSColin Percival for (i = 0; i < tbl->str_numstr; i++) {
1116ae1554aSColin Percival fread(&pos, 1, sizeof(pos), Dataf);
1126ae1554aSColin Percival fseeko(Inf, be64toh(pos), SEEK_SET);
1136ae1554aSColin Percival if (i != 0)
1146ae1554aSColin Percival printf("%c\n", Delimch);
1156ae1554aSColin Percival for (;;) {
1166ae1554aSColin Percival sp = fgets(buf, sizeof(buf), Inf);
1176ae1554aSColin Percival if (sp == NULL || STR_ENDSTRING(sp, *tbl))
1186ae1554aSColin Percival break;
1196ae1554aSColin Percival else
1206ae1554aSColin Percival fputs(sp, stdout);
1216ae1554aSColin Percival }
1226ae1554aSColin Percival }
1236ae1554aSColin Percival }
124