107917fe8Szrj /*-
207917fe8Szrj * Copyright (c) 1991, 1993
307917fe8Szrj * The Regents of the University of California. All rights reserved.
407917fe8Szrj *
507917fe8Szrj * This code is derived from software contributed to Berkeley by
607917fe8Szrj * Ken Arnold.
707917fe8Szrj *
807917fe8Szrj * Redistribution and use in source and binary forms, with or without
907917fe8Szrj * modification, are permitted provided that the following conditions
1007917fe8Szrj * are met:
1107917fe8Szrj * 1. Redistributions of source code must retain the above copyright
1207917fe8Szrj * notice, this list of conditions and the following disclaimer.
1307917fe8Szrj * 2. Redistributions in binary form must reproduce the above copyright
1407917fe8Szrj * notice, this list of conditions and the following disclaimer in the
1507917fe8Szrj * documentation and/or other materials provided with the distribution.
1607917fe8Szrj * 3. Neither the name of the University nor the names of its contributors
1707917fe8Szrj * may be used to endorse or promote products derived from this software
1807917fe8Szrj * without specific prior written permission.
1907917fe8Szrj *
2007917fe8Szrj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2107917fe8Szrj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2207917fe8Szrj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2307917fe8Szrj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2407917fe8Szrj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2507917fe8Szrj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2607917fe8Szrj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2707917fe8Szrj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2807917fe8Szrj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2907917fe8Szrj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3007917fe8Szrj * SUCH DAMAGE.
3107917fe8Szrj *
3207917fe8Szrj * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
3307917fe8Szrj * @(#)unstr.c 8.1 (Berkeley) 5/31/93
34*7b5acc11Szrj * $FreeBSD: head/usr.bin/fortune/unstr/unstr.c 300705 2016-05-26 01:33:24Z truckman $
3507917fe8Szrj */
3607917fe8Szrj
3707917fe8Szrj /*
3807917fe8Szrj * This program un-does what "strfile" makes, thereby obtaining the
3907917fe8Szrj * original file again. This can be invoked with the name of the output
4007917fe8Szrj * file, the input file, or both. If invoked with only a single argument
4107917fe8Szrj * ending in ".dat", it is pressumed to be the input file and the output
4207917fe8Szrj * file will be the same stripped of the ".dat". If the single argument
4307917fe8Szrj * doesn't end in ".dat", then it is presumed to be the output file, and
4407917fe8Szrj * the input file is that name prepended by a ".dat". If both are given
4507917fe8Szrj * they are treated literally as the input and output files.
4607917fe8Szrj *
4707917fe8Szrj * Ken Arnold Aug 13, 1978
4807917fe8Szrj */
4907917fe8Szrj
5007917fe8Szrj #include <sys/param.h>
51*7b5acc11Szrj #include <sys/endian.h>
5207917fe8Szrj #include <ctype.h>
5307917fe8Szrj #include <err.h>
5407917fe8Szrj #include <stdio.h>
5507917fe8Szrj #include <stdlib.h>
5607917fe8Szrj #include <string.h>
5707917fe8Szrj
5807917fe8Szrj #include "strfile.h"
5907917fe8Szrj
6007917fe8Szrj static char *Infile; /* name of input file */
6107917fe8Szrj static char Datafile[MAXPATHLEN]; /* name of data file */
6207917fe8Szrj static char Delimch; /* delimiter character */
6307917fe8Szrj
6407917fe8Szrj static FILE *Inf, *Dataf;
6507917fe8Szrj
6607917fe8Szrj static void order_unstr(STRFILE *);
6707917fe8Szrj
6807917fe8Szrj /* ARGSUSED */
6907917fe8Szrj int
main(int argc,char * argv[])7007917fe8Szrj main(int argc, char *argv[])
7107917fe8Szrj {
7207917fe8Szrj static STRFILE tbl; /* description table */
7307917fe8Szrj
7407917fe8Szrj if (argc != 2) {
7507917fe8Szrj fprintf(stderr, "usage: unstr datafile\n");
7607917fe8Szrj exit(1);
7707917fe8Szrj }
7807917fe8Szrj Infile = argv[1];
7907917fe8Szrj if ((size_t)snprintf(Datafile, sizeof(Datafile), "%s.dat", Infile) >=
8007917fe8Szrj sizeof(Datafile))
8107917fe8Szrj errx(1, "%s name too long", Infile);
8207917fe8Szrj if ((Inf = fopen(Infile, "r")) == NULL)
8307917fe8Szrj err(1, "%s", Infile);
8407917fe8Szrj if ((Dataf = fopen(Datafile, "r")) == NULL)
8507917fe8Szrj err(1, "%s", Datafile);
8607917fe8Szrj if (fread((char *)&tbl, sizeof(tbl), 1, Dataf) != 1) {
8707917fe8Szrj if (feof(Dataf))
8807917fe8Szrj errx(1, "%s read EOF", Datafile);
8907917fe8Szrj else
9007917fe8Szrj err(1, "%s read", Datafile);
9107917fe8Szrj }
92*7b5acc11Szrj tbl.str_version = be32toh(tbl.str_version);
93*7b5acc11Szrj tbl.str_numstr = be32toh(tbl.str_numstr);
94*7b5acc11Szrj tbl.str_longlen = be32toh(tbl.str_longlen);
95*7b5acc11Szrj tbl.str_shortlen = be32toh(tbl.str_shortlen);
96*7b5acc11Szrj tbl.str_flags = be32toh(tbl.str_flags);
9707917fe8Szrj if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)))
9807917fe8Szrj errx(1, "nothing to do -- table in file order");
9907917fe8Szrj Delimch = tbl.str_delim;
10007917fe8Szrj order_unstr(&tbl);
10107917fe8Szrj fclose(Inf);
10207917fe8Szrj fclose(Dataf);
10307917fe8Szrj exit(0);
10407917fe8Szrj }
10507917fe8Szrj
10607917fe8Szrj static void
order_unstr(STRFILE * tbl)10707917fe8Szrj order_unstr(STRFILE *tbl)
10807917fe8Szrj {
109*7b5acc11Szrj uint32_t i;
11007917fe8Szrj char *sp;
111*7b5acc11Szrj off_t pos;
11207917fe8Szrj char buf[BUFSIZ];
11307917fe8Szrj
11407917fe8Szrj for (i = 0; i < tbl->str_numstr; i++) {
115*7b5acc11Szrj fread(&pos, 1, sizeof(pos), Dataf);
116*7b5acc11Szrj fseeko(Inf, be64toh(pos), SEEK_SET);
11707917fe8Szrj if (i != 0)
11807917fe8Szrj printf("%c\n", Delimch);
11907917fe8Szrj for (;;) {
12007917fe8Szrj sp = fgets(buf, sizeof(buf), Inf);
12107917fe8Szrj if (sp == NULL || STR_ENDSTRING(sp, *tbl))
12207917fe8Szrj break;
12307917fe8Szrj else
12407917fe8Szrj fputs(sp, stdout);
12507917fe8Szrj }
12607917fe8Szrj }
12707917fe8Szrj }
128