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