Annotation of 43BSDTahoe/games/fortune/unstr.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1986 Regents of the University of California.
        !             3:  * All rights reserved.  The Berkeley software License Agreement
        !             4:  * specifies the terms and conditions for redistribution.
        !             5:  */
        !             6: 
        !             7: #ifndef lint
        !             8: static char sccsid[] = "@(#)unstr.c    1.1 (Berkeley) 12/9/86";
        !             9: #endif not lint
        !            10: 
        !            11: # include      <stdio.h>
        !            12: # include      "strfile.h"
        !            13: 
        !            14: # define       TRUE    1
        !            15: # define       FALSE   0
        !            16: 
        !            17: /*
        !            18:  *     This program un-does what "strfile" makes, thereby obtaining the
        !            19:  * original file again.  This can be invoked with the name of the output
        !            20:  * file, the input file, or both. If invoked with only a single argument
        !            21:  * ending in ".dat", it is pressumed to be the input file and the output
        !            22:  * file will be the same stripped of the ".dat".  If the single argument
        !            23:  * doesn't end in ".dat", then it is presumed to be the output file, and
        !            24:  * the input file is that name prepended by a ".dat".  If both are given
        !            25:  * they are treated literally as the input and output files.
        !            26:  *
        !            27:  *     Ken Arnold              Aug 13, 1978
        !            28:  */
        !            29: 
        !            30: # define       DELIM_CH        '-'
        !            31: 
        !            32: char   Infile[100],                    /* name of input file */
        !            33:        Outfile[100];                   /* name of output file */
        !            34: 
        !            35: short  Oflag = FALSE;                  /* use order of initial table */
        !            36: 
        !            37: FILE   *Inf, *Outf;
        !            38: 
        !            39: char   *rindex(), *malloc(), *strcat(), *strcpy();
        !            40: 
        !            41: main(ac, av)
        !            42: int    ac;
        !            43: char   **av;
        !            44: {
        !            45:        register char   c;
        !            46:        register int    nstr, delim;
        !            47:        static STRFILE  tbl;            /* description table */
        !            48: 
        !            49:        getargs(ac, av);
        !            50:        if ((Inf = fopen(Infile, "r")) == NULL) {
        !            51:                perror(Infile);
        !            52:                exit(-1);
        !            53:                /* NOTREACHED */
        !            54:        }
        !            55:        if ((Outf = fopen(Outfile, "w")) == NULL) {
        !            56:                perror(Outfile);
        !            57:                exit(-1);
        !            58:                /* NOTREACHED */
        !            59:        }
        !            60:        (void) fread((char *) &tbl, sizeof tbl, 1, Inf);
        !            61:        if (Oflag) {
        !            62:                order_unstr(&tbl);
        !            63:                exit(0);
        !            64:                /* NOTREACHED */
        !            65:        }
        !            66:        nstr = tbl.str_numstr;
        !            67:        (void) fseek(Inf, (long) (sizeof (long) * (nstr + 1)), 1);
        !            68:        delim = 0;
        !            69:        for (nstr = 0; (c = getc(Inf)) != EOF; nstr++)
        !            70:                if (c != '\0')
        !            71:                        putc(c, Outf);
        !            72:                else if (nstr != tbl.str_numstr - 1)
        !            73:                        if (nstr == tbl.str_delims[delim]) {
        !            74:                                fputs("%-\n", Outf);
        !            75:                                delim++;
        !            76:                        }
        !            77:                        else
        !            78:                                fputs("%%\n", Outf);
        !            79:        exit(0);
        !            80:        /* NOTREACHED */
        !            81: }
        !            82: 
        !            83: getargs(ac, av)
        !            84: register int   ac;
        !            85: register char  **av;
        !            86: {
        !            87:        register char   *sp;
        !            88: 
        !            89:        if (ac > 1 && strcmp(av[1], "-o") == 0) {
        !            90:                Oflag++;
        !            91:                ac--;
        !            92:                av++;
        !            93:        }
        !            94:        if (ac < 2) {
        !            95:                printf("usage: %s datafile[.dat] [ outfile ]\n", av[0]);
        !            96:                exit(-1);
        !            97:        }
        !            98:        (void) strcpy(Infile, av[1]);
        !            99:        if (ac < 3) {
        !           100:                (void) strcpy(Outfile, Infile);
        !           101:                if ((sp = rindex(av[1], '.')) && strcmp(sp, ".dat") == 0)
        !           102:                        Outfile[strlen(Outfile) - 4] = '\0';
        !           103:                else
        !           104:                        (void) strcat(Infile, ".dat");
        !           105:        }
        !           106:        else
        !           107:                (void) strcpy(Outfile, av[2]);
        !           108: }
        !           109: 
        !           110: order_unstr(tbl)
        !           111: STRFILE        *tbl;
        !           112: {
        !           113:        register int    i, c;
        !           114:        register int    delim;
        !           115:        register long   *seekpts;
        !           116: 
        !           117:        seekpts = (long *) malloc(sizeof *seekpts * tbl->str_numstr);   /* NOSTRICT */
        !           118:        if (seekpts == NULL) {
        !           119:                perror("malloc");
        !           120:                exit(-1);
        !           121:                /* NOTREACHED */
        !           122:        }
        !           123:        (void) fread((char *) seekpts, sizeof *seekpts, tbl->str_numstr, Inf);
        !           124:        delim = 0;
        !           125:        for (i = 0; i < tbl->str_numstr; i++, seekpts++) {
        !           126:                if (i != 0)
        !           127:                        if (i == tbl->str_delims[delim]) {
        !           128:                                fputs("%-\n", Outf);
        !           129:                                delim++;
        !           130:                        }
        !           131:                        else
        !           132:                                fputs("%%\n", Outf);
        !           133:                (void) fseek(Inf, *seekpts, 0);
        !           134:                while ((c = getc(Inf)) != '\0')
        !           135:                        putc(c, Outf);
        !           136:        }
        !           137: }

unix.superglobalmegacorp.com

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