1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Ken Arnold. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char copyright[] = 13 "@(#) Copyright (c) 1991, 1993\n\ 14 The Regents of the University of California. All rights reserved.\n"; 15 #endif /* not lint */ 16 17 #ifndef lint 18 static char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 05/31/93"; 19 #endif /* not lint */ 20 21 /* 22 * This program un-does what "strfile" makes, thereby obtaining the 23 * original file again. This can be invoked with the name of the output 24 * file, the input file, or both. If invoked with only a single argument 25 * ending in ".dat", it is pressumed to be the input file and the output 26 * file will be the same stripped of the ".dat". If the single argument 27 * doesn't end in ".dat", then it is presumed to be the output file, and 28 * the input file is that name prepended by a ".dat". If both are given 29 * they are treated literally as the input and output files. 30 * 31 * Ken Arnold Aug 13, 1978 32 */ 33 34 # include <machine/endian.h> 35 # include <sys/param.h> 36 # include "strfile.h" 37 # include <stdio.h> 38 # include <ctype.h> 39 40 # ifndef MAXPATHLEN 41 # define MAXPATHLEN 1024 42 # endif /* MAXPATHLEN */ 43 44 char *Infile, /* name of input file */ 45 Datafile[MAXPATHLEN], /* name of data file */ 46 Delimch; /* delimiter character */ 47 48 FILE *Inf, *Dataf; 49 50 char *strcat(), *strcpy(); 51 52 /* ARGSUSED */ 53 main(ac, av) 54 int ac; 55 char **av; 56 { 57 static STRFILE tbl; /* description table */ 58 59 getargs(av); 60 if ((Inf = fopen(Infile, "r")) == NULL) { 61 perror(Infile); 62 exit(1); 63 } 64 if ((Dataf = fopen(Datafile, "r")) == NULL) { 65 perror(Datafile); 66 exit(1); 67 } 68 (void) fread((char *) &tbl, sizeof tbl, 1, Dataf); 69 tbl.str_version = ntohl(tbl.str_version); 70 tbl.str_numstr = ntohl(tbl.str_numstr); 71 tbl.str_longlen = ntohl(tbl.str_longlen); 72 tbl.str_shortlen = ntohl(tbl.str_shortlen); 73 tbl.str_flags = ntohl(tbl.str_flags); 74 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) { 75 fprintf(stderr, "nothing to do -- table in file order\n"); 76 exit(1); 77 } 78 Delimch = tbl.str_delim; 79 order_unstr(&tbl); 80 (void) fclose(Inf); 81 (void) fclose(Dataf); 82 exit(0); 83 } 84 85 getargs(av) 86 register char *av[]; 87 { 88 if (!*++av) { 89 (void) fprintf(stderr, "usage: unstr datafile\n"); 90 exit(1); 91 } 92 Infile = *av; 93 (void) strcpy(Datafile, Infile); 94 (void) strcat(Datafile, ".dat"); 95 } 96 97 order_unstr(tbl) 98 register STRFILE *tbl; 99 { 100 register int i; 101 register char *sp; 102 auto off_t pos; 103 char buf[BUFSIZ]; 104 105 for (i = 0; i < tbl->str_numstr; i++) { 106 (void) fread((char *) &pos, 1, sizeof pos, Dataf); 107 (void) fseek(Inf, ntohl(pos), 0); 108 if (i != 0) 109 (void) printf("%c\n", Delimch); 110 for (;;) { 111 sp = fgets(buf, sizeof buf, Inf); 112 if (sp == NULL || STR_ENDSTRING(sp, *tbl)) 113 break; 114 else 115 fputs(sp, stdout); 116 } 117 } 118 } 119