home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / UNIX / Games / fortune-mod-9708-I / util / unstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-08  |  7.5 KB  |  251 lines

  1. /*      $NetBSD: unstr.c,v 1.3 1995/03/23 08:29:00 cgd Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1991, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Ken Arnold.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39. #if 0
  40. #ifndef lint
  41. static char copyright[] =
  42. "@(#) Copyright (c) 1991, 1993\n\
  43.     The Regents of the University of California.  All rights reserved.\n";
  44.  
  45. #endif /* not lint */
  46.  
  47. #ifndef lint
  48. static char sccsid[] = "@(#)unstr.c    8.1 (Berkeley) 5/31/93";
  49.  
  50. #endif /* not lint */
  51. #endif /* comment out the dreck, kill the warnings */
  52.  
  53. /*
  54.  *    This program un-does what "strfile" makes, thereby obtaining the
  55.  * original file again.  This can be invoked with the name of the output
  56.  * file, the input file, or both. If invoked with only a single argument
  57.  * ending in ".dat", it is pressumed to be the input file and the output
  58.  * file will be the same stripped of the ".dat".  If the single argument
  59.  * doesn't end in ".dat", then it is presumed to be the output file, and
  60.  * the input file is that name prepended by a ".dat".  If both are given
  61.  * they are treated literally as the input and output files.
  62.  *
  63.  *      Ken Arnold              Aug 13, 1978
  64.  */
  65.  
  66. /*
  67.  * Umm.  Well, when I got this thing, it didn't work like that.  It now
  68.  * treats the *first* filename listed as the name of the datafile; if
  69.  * the file happens to have an extension, that's stripped off and the
  70.  * result is the name of the strings file.  If there is no extension, then
  71.  * the datafile has '.dat' added, and the strings file is the filename.
  72.  * The only problem with this is if you happen to have a strings file
  73.  * with a dot in it--in that case, specify the dat file fully.
  74.  * 
  75.  * The program also now accepts an optional second filename, which is the
  76.  * name of the output file; if not specified, it dumps to stdout.
  77.  * 
  78.  * It can also take one parameter, which defines a new separator character.
  79.  * This was added chiefly in order to avoid having to run sed over a
  80.  * strings file; unstr *can* do it easily, so it should.
  81.  *
  82.  * We also had to add some code to make the special cases of a null fortune
  83.  * (two separators on successive lines, a common enough error when editing
  84.  * a strings file) and no trailing separator be treated properly.  Unstr
  85.  * now writes out a separator string at the end of a file; strfile does
  86.  * not consider the null string following this to be a fortune.  Be careful
  87.  * not to put an extra newline after the required last newline--if so, you'll
  88.  * get a fortune that contains nothing but a newline.  Karo syrup, syrup.
  89.  * For the gory details, and lots of cussing, see strfile.c
  90.  */
  91. #ifdef NeXT
  92.     #include    <libc.h>
  93. #endif
  94. #include    <sys/types.h>
  95. #include    <netinet/in.h>
  96. #include    <sys/param.h>
  97. #include    "strfile.h"
  98. #include    <stdio.h>
  99. #include    <ctype.h>
  100. #include    <string.h>
  101. #include    <unistd.h>
  102.  
  103. #ifndef MAXPATHLEN
  104. #define    MAXPATHLEN    1024
  105. #endif /* MAXPATHLEN */
  106.  
  107. char *Infile,            /* name of input file */
  108.   Datafile[MAXPATHLEN],        /* name of data file */
  109.   Delimch,            /* delimiter character */
  110.   Outfile[MAXPATHLEN];
  111.  
  112. char NewDelch = '\0';        /* a replacement delimiter character */
  113.  
  114. FILE *Inf, *Dataf, *Outf;
  115.  
  116. /* ARGSUSED */
  117. void getargs(int ac, char *av[])
  118. {
  119.     extern int optind;
  120.     extern char *optarg;
  121.     char *extc;
  122.     int ch;
  123.  
  124.     while ((ch = getopt(ac, av, "c:")) != EOF)
  125.     switch (ch)
  126.       {
  127.       case 'c':
  128.           NewDelch = *optarg;
  129.           if (!isascii(NewDelch))
  130.           {
  131.           fprintf(stderr, "Bad delimiting characher: '\\%o'\n", NewDelch);
  132.           }
  133.           break;
  134.       case '?':
  135.       default:
  136.           fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext] [outputfile]\n");
  137.           exit(1);
  138.       }
  139.  
  140.     av += optind;
  141.  
  142.     if (*av)
  143.     {
  144.     Infile = *av;
  145.     fprintf(stderr, "Input file: %s\n", Infile);
  146.     if (!strrchr(Infile, '.'))
  147.     {
  148.         strcpy(Datafile, Infile);
  149.         strcat(Datafile, ".dat");
  150.     }
  151.     else
  152.     {
  153.         strcpy(Datafile, Infile);
  154.         extc = strrchr(Infile, '.');
  155.         *extc = '\0';
  156.     }
  157.     if (*++av)
  158.     {
  159.         strcpy(Outfile, *av);
  160.         fprintf(stderr, "Output file: %s\n", Outfile);
  161.     }
  162.     }
  163.     else
  164.     {
  165.     fprintf(stderr, "No input file name\n");
  166.     fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext] [outputfile]\n");
  167.     exit(1);
  168.     }
  169.     if (!strcmp(Infile, Outfile))
  170.     {
  171.     fprintf(stderr, "The input file for strings (%s) must be different from the output file (%s)\n", Infile, Outfile);
  172.     exit(1);
  173.     }
  174. }
  175.  
  176. void order_unstr(tbl)
  177.      register STRFILE *tbl;
  178. {
  179.     register int i;
  180.     register unsigned char *sp;
  181.     auto off_t pos;
  182.     char buf[BUFSIZ];
  183.     int printedsome;
  184.  
  185.     for (i = 0; i <= tbl->str_numstr; i++)
  186.     {
  187.     fread((char *) &pos, 1, sizeof pos, Dataf);
  188.     fseek(Inf, ntohl(pos), 0);
  189.     printedsome = 0;
  190.     for (;;)
  191.     {
  192.         sp = fgets(buf, sizeof buf, Inf);
  193.         if (sp == NULL || STR_ENDSTRING(sp, *tbl))
  194.         {
  195.         if (sp || printedsome)
  196.             fprintf(Outf, "%c\n", Delimch);
  197.         break;
  198.         }
  199.         else
  200.         {
  201.         printedsome = 1;
  202.         fputs(sp, Outf);
  203.         }
  204.     }
  205.     }
  206. }
  207.  
  208. int main(int ac, char **av)
  209. {
  210.     static STRFILE tbl;        /* description table */
  211.  
  212.     getargs(ac, av);
  213.     if ((Inf = fopen(Infile, "r")) == NULL)
  214.     {
  215.     perror(Infile);
  216.     exit(1);
  217.     }
  218.     if ((Dataf = fopen(Datafile, "r")) == NULL)
  219.     {
  220.     perror(Datafile);
  221.     exit(1);
  222.     }
  223.     if (*Outfile == '\0')
  224.     Outf = stdout;
  225.     else if ((Outf = fopen(Outfile, "w+")) == NULL)
  226.     {
  227.     perror(Outfile);
  228.     exit(1);
  229.     }
  230.     fread((char *) &tbl, sizeof tbl, 1, Dataf);
  231.     tbl.str_version = ntohl(tbl.str_version);
  232.     tbl.str_numstr = ntohl(tbl.str_numstr);
  233.     tbl.str_longlen = ntohl(tbl.str_longlen);
  234.     tbl.str_shortlen = ntohl(tbl.str_shortlen);
  235.     tbl.str_flags = ntohl(tbl.str_flags);
  236.     if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)) && (!NewDelch))
  237.     {
  238.     fprintf(stderr, "nothing to do -- table in file order\n");
  239.     exit(1);
  240.     }
  241.     if (NewDelch)
  242.     Delimch = NewDelch;
  243.     else
  244.     Delimch = tbl.str_delim;
  245.     order_unstr(&tbl);
  246.     fclose(Inf);
  247.     fclose(Dataf);
  248.     fclose(Outf);
  249.     exit(0);
  250. }
  251.