-WARNS6 cleanups (36 warnings)
[dragonfly/port-amd64.git] / games / fortune / unstr / unstr.c
blob48b2a56b078acc1aea1b68628ca3547c3405bc66
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Ken Arnold.
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.
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.
36 * $FreeBSD: src/games/fortune/unstr/unstr.c,v 1.5 1999/11/16 02:57:01 billf Exp $
37 * $DragonFly: src/games/fortune/unstr/unstr.c,v 1.4 2006/08/08 16:58:59 pavalos Exp $
39 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
40 * @(#)unstr.c 8.1 (Berkeley) 5/31/93
41 * $FreeBSD: src/games/fortune/unstr/unstr.c,v 1.5 1999/11/16 02:57:01 billf Exp $
45 * This program un-does what "strfile" makes, thereby obtaining the
46 * original file again. This can be invoked with the name of the output
47 * file, the input file, or both. If invoked with only a single argument
48 * ending in ".dat", it is pressumed to be the input file and the output
49 * file will be the same stripped of the ".dat". If the single argument
50 * doesn't end in ".dat", then it is presumed to be the output file, and
51 * the input file is that name prepended by a ".dat". If both are given
52 * they are treated literally as the input and output files.
54 * Ken Arnold Aug 13, 1978
57 # include <sys/param.h>
58 # include <stdio.h>
59 # include <ctype.h>
60 # include <stdlib.h>
61 # include <string.h>
62 # include "strfile.h"
64 char *Infile, /* name of input file */
65 Datafile[MAXPATHLEN], /* name of data file */
66 Delimch; /* delimiter character */
68 FILE *Inf, *Dataf;
70 void getargs(char *[]);
71 void order_unstr(STRFILE *);
73 /* ARGSUSED */
74 int
75 main(__unused int ac, char **av)
77 static STRFILE tbl; /* description table */
79 getargs(av);
80 if ((Inf = fopen(Infile, "r")) == NULL) {
81 perror(Infile);
82 exit(1);
84 if ((Dataf = fopen(Datafile, "r")) == NULL) {
85 perror(Datafile);
86 exit(1);
88 (void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
89 tbl.str_version = ntohl(tbl.str_version);
90 tbl.str_numstr = ntohl(tbl.str_numstr);
91 tbl.str_longlen = ntohl(tbl.str_longlen);
92 tbl.str_shortlen = ntohl(tbl.str_shortlen);
93 tbl.str_flags = ntohl(tbl.str_flags);
94 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
95 fprintf(stderr, "nothing to do -- table in file order\n");
96 exit(1);
98 Delimch = tbl.str_delim;
99 order_unstr(&tbl);
100 (void) fclose(Inf);
101 (void) fclose(Dataf);
102 exit(0);
105 void
106 getargs(char *av[])
108 if (!*++av) {
109 (void) fprintf(stderr, "usage: unstr datafile\n");
110 exit(1);
112 Infile = *av;
113 (void) strcpy(Datafile, Infile);
114 (void) strcat(Datafile, ".dat");
117 void
118 order_unstr(STRFILE *tbl)
120 unsigned int i;
121 char *sp;
122 long pos;
123 char buf[BUFSIZ];
125 for (i = 0; i < tbl->str_numstr; i++) {
126 (void) fread((char *) &pos, 1, sizeof pos, Dataf);
127 (void) fseek(Inf, ntohl(pos), 0);
128 if (i != 0)
129 (void) printf("%c\n", Delimch);
130 for (;;) {
131 sp = fgets(buf, sizeof buf, Inf);
132 if (sp == NULL || STR_ENDSTRING(sp, *tbl))
133 break;
134 else
135 fputs(sp, stdout);