Annotation of 42BSD/games/fortune/unstr.c, revision 1.1.1.1

1.1       root        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: char   sccsid[] = "@(#)unstr.c 1.2 (Berkeley) 5/14/81";
                     18: 
                     19: # define       reg     register
                     20: 
                     21: # define       DELIM_CH        '-'
                     22: 
                     23: char   infile[50],                     /* name of input file           */
                     24:        outfile[50],                    /* name of output file          */
                     25:        *rindex();
                     26: 
                     27: long   *calloc();
                     28: 
                     29: main(ac, av)
                     30: int    ac;
                     31: char   *av[]; {
                     32: 
                     33:        reg char        c;
                     34:        reg FILE        *inf, *outf;
                     35:        int             nstr, delim;
                     36:        long            *seekpts;
                     37:        STRFILE         tbl;            /* description table            */
                     38: 
                     39:        getargs(ac, av);
                     40:        if ((inf = fopen(infile, "r")) == NULL) {
                     41:                perror(infile);
                     42:                exit(-1);
                     43:        }
                     44:        fread(&tbl,sizeof tbl,1,inf);
                     45:        nstr = tbl.str_numstr;
                     46:        if ((seekpts = calloc(sizeof *seekpts, nstr)) == NULL) {
                     47:                perror("calloc");
                     48:                exit(-1);
                     49:        }
                     50:        fread(seekpts, (sizeof seekpts[0]), nstr, inf);
                     51:        if ((outf = fopen(outfile, "w")) == NULL) {
                     52:                perror(outfile);
                     53:                exit(-1);
                     54:        }
                     55:        delim = 0;
                     56:        while ((c = getc(inf)) != EOF)
                     57:                if (c != '\0')
                     58:                        putc(c, outf);
                     59:                else if (--nstr)
                     60:                        if (ftell(inf) == tbl.str_delims[delim]) {
                     61:                                fputs("%-\n", outf);
                     62:                                delim++;
                     63:                        }
                     64:                        else
                     65:                                fputs("%%\n", outf);
                     66: }
                     67: getargs(ac, av)
                     68: reg int                ac;
                     69: reg char       **av; {
                     70: 
                     71:        reg char        *sp;
                     72: 
                     73:        if (ac < 2) {
                     74:                printf("usage: %s datafile[.dat] [ outfile ]\n",av[0]);
                     75:                exit(-1);
                     76:        }
                     77:        strcpy(infile,av[1]);
                     78:        if (ac < 3) {
                     79:                strcpy(outfile,infile);
                     80:                if ((sp = rindex(av[1])) && strcmp(sp, ".dat") == 0)
                     81:                        outfile[strlen(outfile) - 4] = '\0';
                     82:                else
                     83:                        strcat(infile, ".dat");
                     84:        }
                     85:        else
                     86:                strcpy(outfile, av[2]);
                     87: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.