games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/games/fortune/unstr/unstr.c,v 1.5 1999/11/16 02:57:01 billf Exp $
33  * $DragonFly: src/games/fortune/unstr/unstr.c,v 1.4 2006/08/08 16:58:59 pavalos Exp $
34  *
35  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
36  * @(#)unstr.c     8.1 (Berkeley) 5/31/93
37  * $FreeBSD: src/games/fortune/unstr/unstr.c,v 1.5 1999/11/16 02:57:01 billf Exp $
38  */
39
40 /*
41  *      This program un-does what "strfile" makes, thereby obtaining the
42  * original file again.  This can be invoked with the name of the output
43  * file, the input file, or both. If invoked with only a single argument
44  * ending in ".dat", it is pressumed to be the input file and the output
45  * file will be the same stripped of the ".dat".  If the single argument
46  * doesn't end in ".dat", then it is presumed to be the output file, and
47  * the input file is that name prepended by a ".dat".  If both are given
48  * they are treated literally as the input and output files.
49  *
50  *      Ken Arnold              Aug 13, 1978
51  */
52
53 #include <sys/param.h>
54 #include <netinet/in.h>
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include "strfile.h"
60
61 char    *Infile,                        /* name of input file */
62         Datafile[MAXPATHLEN],           /* name of data file */
63         Delimch;                        /* delimiter character */
64
65 FILE    *Inf, *Dataf;
66
67 void getargs(char *[]);
68 void order_unstr(STRFILE *);
69
70 /* ARGSUSED */
71 int
72 main(int ac __unused, char *av[])
73 {
74         static STRFILE tbl;             /* description table */
75
76         getargs(av);
77         if ((Inf = fopen(Infile, "r")) == NULL) {
78                 perror(Infile);
79                 exit(1);
80         }
81         if ((Dataf = fopen(Datafile, "r")) == NULL) {
82                 perror(Datafile);
83                 exit(1);
84         }
85         fread((char *)&tbl, sizeof(tbl), 1, Dataf);
86         tbl.str_version = ntohl(tbl.str_version);
87         tbl.str_numstr = ntohl(tbl.str_numstr);
88         tbl.str_longlen = ntohl(tbl.str_longlen);
89         tbl.str_shortlen = ntohl(tbl.str_shortlen);
90         tbl.str_flags = ntohl(tbl.str_flags);
91         if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
92                 fprintf(stderr, "nothing to do -- table in file order\n");
93                 exit(1);
94         }
95         Delimch = tbl.str_delim;
96         order_unstr(&tbl);
97         fclose(Inf);
98         fclose(Dataf);
99         exit(0);
100 }
101
102 void
103 getargs(char *av[])
104 {
105         if (!*++av) {
106                 fprintf(stderr, "usage: unstr datafile\n");
107                 exit(1);
108         }
109         Infile = *av;
110         strcpy(Datafile, Infile);
111         strcat(Datafile, ".dat");
112 }
113
114 void
115 order_unstr(STRFILE *tbl)
116 {
117         unsigned int i;
118         char *sp;
119         long pos;
120         char buf[BUFSIZ];
121
122         for (i = 0; i < tbl->str_numstr; i++) {
123                 fread((char *)&pos, 1, sizeof(pos), Dataf);
124                 fseek(Inf, ntohl(pos), SEEK_SET);
125                 if (i != 0)
126                         printf("%c\n", Delimch);
127                 for (;;) {
128                         sp = fgets(buf, sizeof(buf), Inf);
129                         if (sp == NULL || STR_ENDSTRING(sp, *tbl))
130                                 break;
131                         else
132                                 fputs(sp, stdout);
133                 }
134         }
135 }