1 /* $OpenBSD: unstr.c,v 1.15 2016/03/07 12:07:56 mestre 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 <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "strfile.h" 58 59 char *Infile, /* name of input file */ 60 Datafile[PATH_MAX], /* name of data file */ 61 Delimch; /* delimiter character */ 62 63 FILE *Inf, *Dataf; 64 65 void getargs(char *[]); 66 void order_unstr(STRFILE *); 67 68 int 69 main(int ac, char *av[]) 70 { 71 static STRFILE tbl; /* description table */ 72 73 if (pledge("stdio rpath wpath cpath", NULL) == -1) 74 err(1, "pledge"); 75 76 getargs(av); 77 if ((Inf = fopen(Infile, "r")) == NULL) 78 err(1, "fopen `%s'", Infile); 79 if ((Dataf = fopen(Datafile, "r")) == NULL) 80 err(1, "fopen `%s'", Datafile); 81 82 if (pledge("stdio", NULL) == -1) 83 err(1, "pledge"); 84 85 (void) fread(&tbl.str_version, sizeof(tbl.str_version), 1, Dataf); 86 (void) fread(&tbl.str_numstr, sizeof(tbl.str_numstr), 1, Dataf); 87 (void) fread(&tbl.str_longlen, sizeof(tbl.str_longlen), 1, Dataf); 88 (void) fread(&tbl.str_shortlen, sizeof(tbl.str_shortlen), 1, Dataf); 89 (void) fread(&tbl.str_flags, sizeof(tbl.str_flags), 1, Dataf); 90 (void) fread( tbl.stuff, sizeof(tbl.stuff), 1, Dataf); 91 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) 92 errx(1, "nothing to do -- table in file order"); 93 Delimch = tbl.str_delim; 94 order_unstr(&tbl); 95 (void) fclose(Inf); 96 (void) fclose(Dataf); 97 return 0; 98 } 99 100 void 101 getargs(char *av[]) 102 { 103 if (!*++av) { 104 (void) fprintf(stderr, "usage: %s datafile\n", getprogname()); 105 exit(1); 106 } 107 Infile = *av; 108 (void) strlcpy(Datafile, Infile, sizeof(Datafile)); 109 if (strlcat(Datafile, ".dat", sizeof(Datafile)) >= sizeof(Datafile)) 110 errx(1, "`%s': filename too long", Infile); 111 } 112 113 void 114 order_unstr(STRFILE *tbl) 115 { 116 unsigned int i; 117 char *sp; 118 int32_t pos; 119 char buf[BUFSIZ]; 120 121 for (i = 0; i < tbl->str_numstr; i++) { 122 (void) fread((char *) &pos, 1, sizeof pos, Dataf); 123 (void) fseek(Inf, ntohl(pos), SEEK_SET); 124 if (i != 0) 125 (void) printf("%c\n", Delimch); 126 for (;;) { 127 sp = fgets(buf, sizeof buf, Inf); 128 if (sp == NULL || STR_ENDSTRING(sp, *tbl)) 129 break; 130 else 131 fputs(sp, stdout); 132 } 133 } 134 } 135