BSD 4_3_Tahoe release
[unix-history] / usr / src / games / fortune / unstr.c
index 7eaadbf..316ccad 100644 (file)
@@ -1,6 +1,19 @@
+/*
+ * Copyright (c) 1986 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)unstr.c    1.1 (Berkeley) 12/9/86";
+#endif not lint
+
 # include      <stdio.h>
 # include      "strfile.h"
 
 # include      <stdio.h>
 # include      "strfile.h"
 
+# define       TRUE    1
+# define       FALSE   0
+
 /*
  *     This program un-does what "strfile" makes, thereby obtaining the
  * original file again.  This can be invoked with the name of the output
 /*
  *     This program un-does what "strfile" makes, thereby obtaining the
  * original file again.  This can be invoked with the name of the output
  *     Ken Arnold              Aug 13, 1978
  */
 
  *     Ken Arnold              Aug 13, 1978
  */
 
-# define       reg     register
-
 # define       DELIM_CH        '-'
 
 # define       DELIM_CH        '-'
 
-char   infile[50],                     /* name of input file           */
-       outfile[50],                    /* name of output file          */
-       *rindex();
+char   Infile[100],                    /* name of input file */
+       Outfile[100];                   /* name of output file */
+
+short  Oflag = FALSE;                  /* use order of initial table */
 
 
-long   *calloc();
+FILE   *Inf, *Outf;
+
+char   *rindex(), *malloc(), *strcat(), *strcpy();
 
 main(ac, av)
 int    ac;
 
 main(ac, av)
 int    ac;
-char   *av[]; {
-
-       reg char        c;
-       reg FILE        *inf, *outf;
-       int             nstr, delim;
-       long            *seekpts;
-       STRFILE         tbl;            /* description table            */
+char   **av;
+{
+       register char   c;
+       register int    nstr, delim;
+       static STRFILE  tbl;            /* description table */
 
        getargs(ac, av);
 
        getargs(ac, av);
-       if ((inf = fopen(infile, "r")) == NULL) {
-               perror(infile);
+       if ((Inf = fopen(Infile, "r")) == NULL) {
+               perror(Infile);
                exit(-1);
                exit(-1);
+               /* NOTREACHED */
        }
        }
-       fread(&tbl,sizeof tbl,1,inf);
-       nstr = tbl.str_numstr;
-       if ((seekpts = calloc(sizeof *seekpts, nstr)) == NULL) {
-               perror("calloc");
+       if ((Outf = fopen(Outfile, "w")) == NULL) {
+               perror(Outfile);
                exit(-1);
                exit(-1);
+               /* NOTREACHED */
        }
        }
-       fread(seekpts, (sizeof seekpts[0]), nstr, inf);
-       if ((outf = fopen(outfile, "w")) == NULL) {
-               perror(outfile);
-               exit(-1);
+       (void) fread((char *) &tbl, sizeof tbl, 1, Inf);
+       if (Oflag) {
+               order_unstr(&tbl);
+               exit(0);
+               /* NOTREACHED */
        }
        }
+       nstr = tbl.str_numstr;
+       (void) fseek(Inf, (long) (sizeof (long) * (nstr + 1)), 1);
        delim = 0;
        delim = 0;
-       while ((c = getc(inf)) != EOF)
+       for (nstr = 0; (c = getc(Inf)) != EOF; nstr++)
                if (c != '\0')
                if (c != '\0')
-                       putc(c, outf);
-               else if (--nstr)
-                       if (ftell(inf) == tbl.str_delims[delim]) {
-                               fputs("%-\n", outf);
+                       putc(c, Outf);
+               else if (nstr != tbl.str_numstr - 1)
+                       if (nstr == tbl.str_delims[delim]) {
+                               fputs("%-\n", Outf);
                                delim++;
                        }
                        else
                                delim++;
                        }
                        else
-                               fputs("%%\n", outf);
+                               fputs("%%\n", Outf);
+       exit(0);
+       /* NOTREACHED */
 }
 }
-getargs(ac, av)
-reg int                ac;
-reg char       **av; {
 
 
-       reg char        *sp;
+getargs(ac, av)
+register int   ac;
+register char  **av;
+{
+       register char   *sp;
 
 
+       if (ac > 1 && strcmp(av[1], "-o") == 0) {
+               Oflag++;
+               ac--;
+               av++;
+       }
        if (ac < 2) {
        if (ac < 2) {
-               printf("usage: %s datafile[.dat] [ outfile ]\n",av[0]);
+               printf("usage: %s datafile[.dat] [ outfile ]\n", av[0]);
                exit(-1);
        }
                exit(-1);
        }
-       strcpy(infile,av[1]);
+       (void) strcpy(Infile, av[1]);
        if (ac < 3) {
        if (ac < 3) {
-               strcpy(outfile,infile);
-               if ((sp = rindex(av[1])) && strcmp(sp, ".dat") == 0)
-                       outfile[strlen(outfile) - 4] = '\0';
+               (void) strcpy(Outfile, Infile);
+               if ((sp = rindex(av[1], '.')) && strcmp(sp, ".dat") == 0)
+                       Outfile[strlen(Outfile) - 4] = '\0';
                else
                else
-                       strcat(infile, ".dat");
+                       (void) strcat(Infile, ".dat");
        }
        else
        }
        else
-               strcpy(outfile, av[2]);
+               (void) strcpy(Outfile, av[2]);
+}
+
+order_unstr(tbl)
+STRFILE        *tbl;
+{
+       register int    i, c;
+       register int    delim;
+       register long   *seekpts;
+
+       seekpts = (long *) malloc(sizeof *seekpts * tbl->str_numstr);   /* NOSTRICT */
+       if (seekpts == NULL) {
+               perror("malloc");
+               exit(-1);
+               /* NOTREACHED */
+       }
+       (void) fread((char *) seekpts, sizeof *seekpts, tbl->str_numstr, Inf);
+       delim = 0;
+       for (i = 0; i < tbl->str_numstr; i++, seekpts++) {
+               if (i != 0)
+                       if (i == tbl->str_delims[delim]) {
+                               fputs("%-\n", Outf);
+                               delim++;
+                       }
+                       else
+                               fputs("%%\n", Outf);
+               (void) fseek(Inf, *seekpts, 0);
+               while ((c = getc(Inf)) != '\0')
+                       putc(c, Outf);
+       }
 }
 }