home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / games / fortune / unstr / unstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-08  |  4.4 KB  |  145 lines

  1. /*-
  2.  * Copyright (c) 1991 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, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. char copyright[] =
  39. "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
  40.  All rights reserved.\n";
  41. #endif /* not lint */
  42.  
  43. #ifndef lint
  44. static char sccsid[] = "@(#)unstr.c    5.8 (Berkeley) 4/8/91";
  45. #endif /* not lint */
  46.  
  47. /*
  48.  *    This program un-does what "strfile" makes, thereby obtaining the
  49.  * original file again.  This can be invoked with the name of the output
  50.  * file, the input file, or both. If invoked with only a single argument
  51.  * ending in ".dat", it is pressumed to be the input file and the output
  52.  * file will be the same stripped of the ".dat".  If the single argument
  53.  * doesn't end in ".dat", then it is presumed to be the output file, and
  54.  * the input file is that name prepended by a ".dat".  If both are given
  55.  * they are treated literally as the input and output files.
  56.  *
  57.  *    Ken Arnold        Aug 13, 1978
  58.  */
  59.  
  60. # include    <machine/endian.h>
  61. # include    <sys/param.h>
  62. # include    "strfile.h"
  63. # include    <stdio.h>
  64. # include    <ctype.h>
  65.  
  66. # ifndef MAXPATHLEN
  67. # define    MAXPATHLEN    1024
  68. # endif    /* MAXPATHLEN */
  69.  
  70. char    *Infile,            /* name of input file */
  71.     Datafile[MAXPATHLEN],        /* name of data file */
  72.     Delimch;            /* delimiter character */
  73.  
  74. FILE    *Inf, *Dataf;
  75.  
  76. char    *strcat(), *strcpy();
  77.  
  78. /* ARGSUSED */
  79. main(ac, av)
  80. int    ac;
  81. char    **av;
  82. {
  83.     static STRFILE    tbl;        /* description table */
  84.  
  85.     getargs(av);
  86.     if ((Inf = fopen(Infile, "r")) == NULL) {
  87.         perror(Infile);
  88.         exit(1);
  89.     }
  90.     if ((Dataf = fopen(Datafile, "r")) == NULL) {
  91.         perror(Datafile);
  92.         exit(1);
  93.     }
  94.     (void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
  95.     tbl.str_version = ntohl(tbl.str_version);
  96.     tbl.str_numstr = ntohl(tbl.str_numstr);
  97.     tbl.str_longlen = ntohl(tbl.str_longlen);
  98.     tbl.str_shortlen = ntohl(tbl.str_shortlen);
  99.     tbl.str_flags = ntohl(tbl.str_flags);
  100.     if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
  101.         fprintf(stderr, "nothing to do -- table in file order\n");
  102.         exit(1);
  103.     }
  104.     Delimch = tbl.str_delim;
  105.     order_unstr(&tbl);
  106.     (void) fclose(Inf);
  107.     (void) fclose(Dataf);
  108.     exit(0);
  109. }
  110.  
  111. getargs(av)
  112. register char    *av[];
  113. {
  114.     if (!*++av) {
  115.         (void) fprintf(stderr, "usage: unstr datafile\n");
  116.         exit(1);
  117.     }
  118.     Infile = *av;
  119.     (void) strcpy(Datafile, Infile);
  120.     (void) strcat(Datafile, ".dat");
  121. }
  122.  
  123. order_unstr(tbl)
  124. register STRFILE    *tbl;
  125. {
  126.     register int    i;
  127.     register char    *sp;
  128.     auto off_t    pos;
  129.     char        buf[BUFSIZ];
  130.  
  131.     for (i = 0; i < tbl->str_numstr; i++) {
  132.         (void) fread((char *) &pos, 1, sizeof pos, Dataf);
  133.         (void) fseek(Inf, ntohl(pos), 0);
  134.         if (i != 0)
  135.             (void) printf("%c\n", Delimch);
  136.         for (;;) {
  137.             sp = fgets(buf, sizeof buf, Inf);
  138.             if (sp == NULL || STR_ENDSTRING(sp, *tbl))
  139.                 break;
  140.             else
  141.                 fputs(sp, stdout);
  142.         }
  143.     }
  144. }
  145.