home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Games / fortune / Source / unstr.c < prev   
Encoding:
C/C++ Source or Header  |  1991-06-17  |  3.5 KB  |  131 lines

  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
  22. char 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
  28. static char sccsid[] = "@(#)unstr.c    5.6 (Berkeley) 12/15/89";
  29. #endif /* not lint */
  30.  
  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.  
  44. #ifndef NeXT
  45. # include    <machine/endian.h>
  46. #endif
  47. # include    <sys/param.h>
  48. # include    "strfile.h"
  49. # include    <stdio.h>
  50. # include    <ctype.h>
  51.  
  52. # ifndef MAXPATHLEN
  53. # define    MAXPATHLEN    1024
  54. # endif    /* MAXPATHLEN */
  55.  
  56. char    *Infile,            /* name of input file */
  57.     Datafile[MAXPATHLEN],        /* name of data file */
  58.     Delimch;            /* delimiter character */
  59.  
  60. FILE    *Inf, *Dataf;
  61.  
  62. char    *strcat(), *strcpy();
  63.  
  64. /* ARGSUSED */
  65. main(ac, av)
  66. int    ac;
  67. char    **av;
  68. {
  69.     static STRFILE    tbl;        /* description table */
  70.  
  71.     getargs(av);
  72.     if ((Inf = fopen(Infile, "r")) == NULL) {
  73.         perror(Infile);
  74.         exit(1);
  75.     }
  76.     if ((Dataf = fopen(Datafile, "r")) == NULL) {
  77.         perror(Datafile);
  78.         exit(1);
  79.     }
  80.     (void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
  81.     tbl.str_version = ntohl(tbl.str_version);
  82.     tbl.str_numstr = ntohl(tbl.str_numstr);
  83.     tbl.str_longlen = ntohl(tbl.str_longlen);
  84.     tbl.str_shortlen = ntohl(tbl.str_shortlen);
  85.     tbl.str_flags = ntohl(tbl.str_flags);
  86.     if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
  87.         fprintf(stderr, "nothing to do -- table in file order\n");
  88.         exit(1);
  89.     }
  90.     Delimch = tbl.str_delim;
  91.     order_unstr(&tbl);
  92.     (void) fclose(Inf);
  93.     (void) fclose(Dataf);
  94.     exit(0);
  95. }
  96.  
  97. getargs(av)
  98. register char    *av[];
  99. {
  100.     if (!*++av) {
  101.         (void) fprintf(stderr, "usage: unstr datafile\n");
  102.         exit(1);
  103.     }
  104.     Infile = *av;
  105.     (void) strcpy(Datafile, Infile);
  106.     (void) strcat(Datafile, ".dat");
  107. }
  108.  
  109. order_unstr(tbl)
  110. register STRFILE    *tbl;
  111. {
  112.     register int    i;
  113.     register char    *sp;
  114.     auto off_t    pos;
  115.     char        buf[BUFSIZ];
  116.  
  117.     for (i = 0; i < tbl->str_numstr; i++) {
  118.         (void) fread((char *) &pos, 1, sizeof pos, Dataf);
  119.         (void) fseek(Inf, ntohl(pos), 0);
  120.         if (i != 0)
  121.             (void) printf("%c\n", Delimch);
  122.         for (;;) {
  123.             sp = fgets(buf, sizeof buf, Inf);
  124.             if (sp == NULL || STR_ENDSTRING(sp, *tbl))
  125.                 break;
  126.             else
  127.                 fputs(sp, stdout);
  128.         }
  129.     }
  130. }
  131.