Update the pciconf(8) database.
[dragonfly.git] / usr.bin / unstr / unstr.c
blob85c0917321367698e437b1f27de7f2d33d972941
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. 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.
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.
32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)unstr.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: head/usr.bin/fortune/unstr/unstr.c 300705 2016-05-26 01:33:24Z truckman $
38 * This program un-does what "strfile" makes, thereby obtaining the
39 * original file again. This can be invoked with the name of the output
40 * file, the input file, or both. If invoked with only a single argument
41 * ending in ".dat", it is pressumed to be the input file and the output
42 * file will be the same stripped of the ".dat". If the single argument
43 * doesn't end in ".dat", then it is presumed to be the output file, and
44 * the input file is that name prepended by a ".dat". If both are given
45 * they are treated literally as the input and output files.
47 * Ken Arnold Aug 13, 1978
50 #include <sys/param.h>
51 #include <sys/endian.h>
52 #include <ctype.h>
53 #include <err.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
58 #include "strfile.h"
60 static char *Infile; /* name of input file */
61 static char Datafile[MAXPATHLEN]; /* name of data file */
62 static char Delimch; /* delimiter character */
64 static FILE *Inf, *Dataf;
66 static void order_unstr(STRFILE *);
68 /* ARGSUSED */
69 int
70 main(int argc, char *argv[])
72 static STRFILE tbl; /* description table */
74 if (argc != 2) {
75 fprintf(stderr, "usage: unstr datafile\n");
76 exit(1);
78 Infile = argv[1];
79 if ((size_t)snprintf(Datafile, sizeof(Datafile), "%s.dat", Infile) >=
80 sizeof(Datafile))
81 errx(1, "%s name too long", Infile);
82 if ((Inf = fopen(Infile, "r")) == NULL)
83 err(1, "%s", Infile);
84 if ((Dataf = fopen(Datafile, "r")) == NULL)
85 err(1, "%s", Datafile);
86 if (fread((char *)&tbl, sizeof(tbl), 1, Dataf) != 1) {
87 if (feof(Dataf))
88 errx(1, "%s read EOF", Datafile);
89 else
90 err(1, "%s read", Datafile);
92 tbl.str_version = be32toh(tbl.str_version);
93 tbl.str_numstr = be32toh(tbl.str_numstr);
94 tbl.str_longlen = be32toh(tbl.str_longlen);
95 tbl.str_shortlen = be32toh(tbl.str_shortlen);
96 tbl.str_flags = be32toh(tbl.str_flags);
97 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)))
98 errx(1, "nothing to do -- table in file order");
99 Delimch = tbl.str_delim;
100 order_unstr(&tbl);
101 fclose(Inf);
102 fclose(Dataf);
103 exit(0);
106 static void
107 order_unstr(STRFILE *tbl)
109 uint32_t i;
110 char *sp;
111 off_t pos;
112 char buf[BUFSIZ];
114 for (i = 0; i < tbl->str_numstr; i++) {
115 fread(&pos, 1, sizeof(pos), Dataf);
116 fseeko(Inf, be64toh(pos), SEEK_SET);
117 if (i != 0)
118 printf("%c\n", Delimch);
119 for (;;) {
120 sp = fgets(buf, sizeof(buf), Inf);
121 if (sp == NULL || STR_ENDSTRING(sp, *tbl))
122 break;
123 else
124 fputs(sp, stdout);