BSD 4 development
[unix-history] / usr / src / games / fortune / unstr.c
CommitLineData
38ae4d69
BJ
1# include <stdio.h>
2# include "strfile.h"
3
4/*
5 * This program un-does what "strfile" makes, thereby obtaining the
6 * original file again. This can be invoked with the name of the output
7 * file, the input file, or both. If invoked with only a single argument
8 * ending in ".dat", it is pressumed to be the input file and the output
9 * file will be the same stripped of the ".dat". If the single argument
10 * doesn't end in ".dat", then it is presumed to be the output file, and
11 * the input file is that name prepended by a ".dat". If both are given
12 * they are treated literally as the input and output files.
13 *
14 * Ken Arnold Aug 13, 1978
15 */
16
17# define reg register
18
19# define DELIM_CH '-'
20
21char infile[50], /* name of input file */
22 outfile[50], /* name of output file */
23 *rindex();
24
25long *calloc();
26
27main(ac, av)
28int ac;
29char *av[]; {
30
31 reg char c;
32 reg FILE *inf, *outf;
33 int nstr, delim;
34 long *seekpts;
35 STRFILE tbl; /* description table */
36
37 getargs(ac, av);
38 if ((inf = fopen(infile, "r")) == NULL) {
39 perror(infile);
40 exit(-1);
41 }
42 fread(&tbl,sizeof tbl,1,inf);
43 nstr = tbl.str_numstr;
44 if ((seekpts = calloc(sizeof *seekpts, nstr)) == NULL) {
45 perror("calloc");
46 exit(-1);
47 }
48 fread(seekpts, (sizeof seekpts[0]), nstr, inf);
49 if ((outf = fopen(outfile, "w")) == NULL) {
50 perror(outfile);
51 exit(-1);
52 }
53 delim = 0;
54 while ((c = getc(inf)) != EOF)
55 if (c != '\0')
56 putc(c, outf);
57 else if (--nstr)
58 if (ftell(inf) == tbl.str_delims[delim]) {
59 fputs("%-\n", outf);
60 delim++;
61 }
62 else
63 fputs("%%\n", outf);
64}
65getargs(ac, av)
66reg int ac;
67reg char **av; {
68
69 reg char *sp;
70
71 if (ac < 2) {
72 printf("usage: %s datafile[.dat] [ outfile ]\n",av[0]);
73 exit(-1);
74 }
75 strcpy(infile,av[1]);
76 if (ac < 3) {
77 strcpy(outfile,infile);
78 if ((sp = rindex(av[1])) && strcmp(sp, ".dat") == 0)
79 outfile[strlen(outfile) - 4] = '\0';
80 else
81 strcat(infile, ".dat");
82 }
83 else
84 strcpy(outfile, av[2]);
85}