link factor.6 and primes.6
[unix-history] / usr / src / games / fortune / unstr / unstr.c
CommitLineData
94c01391
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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 are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21#ifndef lint
22char copyright[] =
23"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
24 All rights reserved.\n";
25#endif /* not lint */
26
27#ifndef lint
9366121a 28static char sccsid[] = "@(#)unstr.c 5.6 (Berkeley) %G%";
94c01391
KB
29#endif /* not lint */
30
294218bc
KB
31/*
32 * This program un-does what "strfile" makes, thereby obtaining the
33 * original file again. This can be invoked with the name of the output
34 * file, the input file, or both. If invoked with only a single argument
35 * ending in ".dat", it is pressumed to be the input file and the output
36 * file will be the same stripped of the ".dat". If the single argument
37 * doesn't end in ".dat", then it is presumed to be the output file, and
38 * the input file is that name prepended by a ".dat". If both are given
39 * they are treated literally as the input and output files.
40 *
41 * Ken Arnold Aug 13, 1978
42 */
43
9366121a 44# include <machine/endian.h>
5c683701
KB
45# include <sys/param.h>
46# include "strfile.h"
94c01391
KB
47# include <stdio.h>
48# include <ctype.h>
5c683701 49
5c683701
KB
50# ifndef MAXPATHLEN
51# define MAXPATHLEN 1024
52# endif /* MAXPATHLEN */
294218bc 53
174cf4a9
KB
54char *Infile, /* name of input file */
55 Datafile[MAXPATHLEN], /* name of data file */
5c683701 56 Delimch; /* delimiter character */
294218bc 57
174cf4a9 58FILE *Inf, *Dataf;
5c683701 59
174cf4a9 60char *strcat(), *strcpy();
294218bc 61
174cf4a9 62/* ARGSUSED */
294218bc
KB
63main(ac, av)
64int ac;
65char **av;
66{
294218bc
KB
67 static STRFILE tbl; /* description table */
68
174cf4a9 69 getargs(av);
294218bc
KB
70 if ((Inf = fopen(Infile, "r")) == NULL) {
71 perror(Infile);
174cf4a9
KB
72 exit(1);
73 }
74 if ((Dataf = fopen(Datafile, "r")) == NULL) {
75 perror(Datafile);
76 exit(1);
294218bc 77 }
174cf4a9 78 (void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
447fd1f6
KB
79 tbl.str_version = ntohl(tbl.str_version);
80 tbl.str_numstr = ntohl(tbl.str_numstr);
81 tbl.str_longlen = ntohl(tbl.str_longlen);
82 tbl.str_shortlen = ntohl(tbl.str_shortlen);
83 tbl.str_flags = ntohl(tbl.str_flags);
5c683701
KB
84 if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
85 fprintf(stderr, "nothing to do -- table in file order\n");
86 exit(1);
87 }
88 Delimch = tbl.str_delim;
5c683701 89 order_unstr(&tbl);
174cf4a9
KB
90 (void) fclose(Inf);
91 (void) fclose(Dataf);
294218bc 92 exit(0);
294218bc
KB
93}
94
174cf4a9 95getargs(av)
5c683701 96register char *av[];
294218bc 97{
174cf4a9
KB
98 if (!*++av) {
99 (void) fprintf(stderr, "usage: unstr datafile\n");
100 exit(1);
294218bc 101 }
174cf4a9
KB
102 Infile = *av;
103 (void) strcpy(Datafile, Infile);
104 (void) strcat(Datafile, ".dat");
294218bc
KB
105}
106
107order_unstr(tbl)
5c683701 108register STRFILE *tbl;
294218bc 109{
5c683701
KB
110 register int i;
111 register char *sp;
112 auto off_t pos;
113 char buf[BUFSIZ];
294218bc 114
5c683701 115 for (i = 0; i < tbl->str_numstr; i++) {
174cf4a9 116 (void) fread((char *) &pos, 1, sizeof pos, Dataf);
447fd1f6 117 (void) fseek(Inf, ntohl(pos), 0);
294218bc 118 if (i != 0)
174cf4a9 119 (void) printf("%c\n", Delimch);
5c683701 120 for (;;) {
174cf4a9 121 sp = fgets(buf, sizeof buf, Inf);
5c683701
KB
122 if (sp == NULL || STR_ENDSTRING(sp, *tbl))
123 break;
294218bc 124 else
174cf4a9 125 fputs(sp, stdout);
5c683701 126 }
294218bc
KB
127 }
128}