Home | History | Annotate | Line # | Download | only in unstr
unstr.c revision 1.9
      1 /*	$NetBSD: unstr.c,v 1.9 2004/01/27 20:25:07 jsm 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. 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.
     21  *
     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
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     38 	The Regents of the University of California.  All rights reserved.\n");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)unstr.c	8.1 (Berkeley) 5/31/93";
     44 #else
     45 __RCSID("$NetBSD: unstr.c,v 1.9 2004/01/27 20:25:07 jsm Exp $");
     46 #endif
     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	<sys/types.h>
     63 # include	<sys/param.h>
     64 # include	<sys/endian.h>
     65 # include	<ctype.h>
     66 # include	<err.h>
     67 # include	<stdio.h>
     68 # include	<stdlib.h>
     69 # include	<string.h>
     70 # include	"strfile.h"
     71 
     72 # ifndef MAXPATHLEN
     73 # define	MAXPATHLEN	1024
     74 # endif	/* MAXPATHLEN */
     75 
     76 char	*Infile,			/* name of input file */
     77 	Datafile[MAXPATHLEN],		/* name of data file */
     78 	Delimch;			/* delimiter character */
     79 
     80 FILE	*Inf, *Dataf;
     81 
     82 void	getargs __P((char *[]));
     83 int	main __P((int, char *[]));
     84 void	order_unstr __P((STRFILE *));
     85 
     86 /* ARGSUSED */
     87 int
     88 main(ac, av)
     89 	int	ac;
     90 	char	**av;
     91 {
     92 	static STRFILE	tbl;		/* description table */
     93 
     94 	getargs(av);
     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");
    107 		exit(1);
    108 	}
    109 	Delimch = tbl.str_delim;
    110 	order_unstr(&tbl);
    111 	(void) fclose(Inf);
    112 	(void) fclose(Dataf);
    113 	exit(0);
    114 }
    115 
    116 void
    117 getargs(av)
    118 	char	*av[];
    119 {
    120 	if (!*++av) {
    121 		(void) fprintf(stderr, "usage: unstr datafile\n");
    122 		exit(1);
    123 	}
    124 	Infile = *av;
    125 	(void) strcpy(Datafile, Infile);
    126 	(void) strcat(Datafile, ".dat");
    127 }
    128 
    129 void
    130 order_unstr(tbl)
    131 	STRFILE	*tbl;
    132 {
    133 	unsigned int	i;
    134 	char	*sp;
    135 	off_t	pos;
    136 	char	buf[BUFSIZ];
    137 
    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);
    141 		if (i != 0)
    142 			(void) printf("%c\n", Delimch);
    143 		for (;;) {
    144 			sp = fgets(buf, sizeof buf, Inf);
    145 			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
    146 				break;
    147 			else
    148 				fputs(sp, stdout);
    149 		}
    150 	}
    151 }
    152