1 /* $OpenBSD: unstr.c,v 1.11 2014/11/16 04:49:48 guenther Exp $ */ 2 /* $NetBSD: unstr.c,v 1.3 1995/03/23 08:29:00 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Ken Arnold. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /* 37 * This program un-does what "strfile" makes, thereby obtaining the 38 * original file again. This can be invoked with the name of the output 39 * file, the input file, or both. If invoked with only a single argument 40 * ending in ".dat", it is pressumed to be the input file and the output 41 * file will be the same stripped of the ".dat". If the single argument 42 * doesn't end in ".dat", then it is presumed to be the output file, and 43 * the input file is that name prepended by a ".dat". If both are given 44 * they are treated literally as the input and output files. 45 * 46 * Ken Arnold Aug 13, 1978 47 */ 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <limits.h> 52 #include <stdio.h> 53 #include <string.h> 54 #include "strfile.h" 55 56 char *Infile, /* name of input file */ 57 Datafile[PATH_MAX], /* name of data file */ 58 Delimch; /* delimiter character */ 59 60 FILE *Inf, *Dataf; 61 62 void getargs(char *[]); 63 void order_unstr(STRFILE *); 64 65 /* ARGSUSED */ 66 int 67 main(int ac, char *av[]) 68 { 69 static STRFILE tbl; /* description table */ 70 71 getargs(av); 72 if ((Inf = fopen(Infile, "r")) == NULL) 73 err(1, "fopen `%s'", Infile); 74 if ((Dataf = fopen(Datafile, "r")) == NULL) 75 err(1, "fopen `%s'", Datafile); 76 (void) fread(&tbl.str_version, sizeof(tbl.str_version), 1, Dataf); 77 (void) fread(&tbl.str_numstr, sizeof(tbl.str_numstr), 1, Dataf); 78 (void) fread(&tbl.str_longlen, sizeof(tbl.str_longlen), 1, Dataf); 79 (void) fread(&tbl.str_shortlen, sizeof(tbl.str_shortlen), 1, Dataf); 80 (void) fread(&tbl.str_flags, sizeof(tbl.str_flags), 1, Dataf); 81 (void) fread( tbl.stuff, sizeof(tbl.stuff), 1, Dataf); 82 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) 83 errx(1, "nothing to do -- table in file order"); 84 Delimch = tbl.str_delim; 85 order_unstr(&tbl); 86 (void) fclose(Inf); 87 (void) fclose(Dataf); 88 exit(0); 89 } 90 91 void 92 getargs(char *av[]) 93 { 94 if (!*++av) { 95 (void) fprintf(stderr, "usage: unstr datafile\n"); 96 exit(1); 97 } 98 Infile = *av; 99 (void) strlcpy(Datafile, Infile, sizeof(Datafile)); 100 if (strlcat(Datafile, ".dat", sizeof(Datafile)) >= sizeof(Datafile)) 101 errx(1, "`%s': filename too long", Infile); 102 } 103 104 void 105 order_unstr(STRFILE *tbl) 106 { 107 unsigned int i; 108 char *sp; 109 int32_t pos; 110 char buf[BUFSIZ]; 111 112 for (i = 0; i < tbl->str_numstr; i++) { 113 (void) fread((char *) &pos, 1, sizeof pos, Dataf); 114 (void) fseek(Inf, ntohl(pos), SEEK_SET); 115 if (i != 0) 116 (void) printf("%c\n", Delimch); 117 for (;;) { 118 sp = fgets(buf, sizeof buf, Inf); 119 if (sp == NULL || STR_ENDSTRING(sp, *tbl)) 120 break; 121 else 122 fputs(sp, stdout); 123 } 124 } 125 } 126