home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / gnuc / util-41s.lzh / util-41 / strings.c < prev    next >
C/C++ Source or Header  |  1998-10-05  |  6KB  |  238 lines

  1. /*
  2.  * Copyright (c) 1980, 1987 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  * Wed Jun 22 22:22:37 1994, faith@cs.unc.edu:
  34.  *     Added internationalization patches from Vitor Duarte <vad@fct.unl.pt>
  35.  */
  36.  
  37. #ifndef lint
  38. char copyright[] =
  39. "@(#) Copyright (c) 1980, 1987 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[] = "@(#)strings.c    5.10 (Berkeley) 5/23/91";
  45. #endif /* not lint */
  46.  
  47. #include <sys/types.h>
  48. #include <fcntl.h>
  49. #include <errno.h>
  50. #include <unistd.h>
  51. #include <stdio.h>
  52. #include <ctype.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <locale.h>
  56.  
  57. #ifdef CROSSATARI
  58. #include "cross-inc/gnu-out.h"
  59. #else
  60. #include <gnu-out.h>
  61. #endif
  62.  
  63. #define DEF_LEN        4        /* default minimum string length */
  64. #if 0
  65. #define ISSTR(ch)    (isascii(ch) && (isprint(ch) || ch == '\t'))
  66. #else
  67. #define ISSTR(ch)    (isprint(ch) || ch == '\t')
  68. #endif
  69.  
  70. typedef struct exec    EXEC;        /* struct exec cast */
  71.  
  72. static long    foff;            /* offset in the file */
  73. static int    hcnt,            /* head count */
  74.         head_len,        /* length of header */
  75.         read_len;        /* length to read */
  76. static u_char    hbfr[sizeof(EXEC)];    /* buffer for struct exec */
  77.  
  78. static void usage();
  79.  
  80. extern void dump_version(char *prog);
  81. char *progname = "strings";
  82.  
  83. main(argc, argv)
  84.     int argc;
  85.     char **argv;
  86. {
  87.     extern char *optarg;
  88.     extern int optind;
  89.     register int ch, cnt;
  90.     register u_char *C;
  91.     EXEC *head;
  92.     int exitcode, minlen;
  93.     short asdata, oflg, fflg;
  94.     u_char *bfr;
  95.     char *file, *p;
  96.  
  97.   if (argv[0][0] != '\0')
  98.     progname = argv[0];
  99.  
  100.     setlocale(LC_CTYPE, "");
  101.  
  102.  
  103.     /*
  104.      * for backward compatibility, allow '-' to specify 'a' flag; no
  105.      * longer documented in the man page or usage string.
  106.      */
  107.     asdata = exitcode = fflg = oflg = 0;
  108.     minlen = -1;
  109.     while ((ch = getopt(argc, argv, "-0123456789an:ofv")) != EOF)
  110.         switch((char)ch) {
  111.         case '0': case '1': case '2': case '3': case '4':
  112.         case '5': case '6': case '7': case '8': case '9':
  113.             /*
  114.              * kludge: strings was originally designed to take
  115.              * a number after a dash.
  116.              */
  117.             if (minlen == -1) {
  118.                 p = argv[optind - 1];
  119.                 if (p[0] == '-' && p[1] == ch && !p[2])
  120.                     minlen = atoi(++p);
  121.                 else
  122.                     minlen = atoi(argv[optind] + 1);
  123.             }
  124.             break;
  125.         case '-':
  126.         case 'a':
  127.             asdata = 1;
  128.             break;
  129.         case 'f':
  130.             fflg = 1;
  131.             break;
  132.         case 'n':
  133.             minlen = atoi(optarg);
  134.             break;
  135.         case 'o':
  136.             oflg = 1;
  137.             break;
  138.         case 'v':
  139.             dump_version(progname);
  140.             exit(0);
  141.             break;
  142.         case '?':
  143.         default:
  144.             usage();
  145.         }
  146.     argc -= optind;
  147.     argv += optind;
  148.  
  149.     if (minlen == -1)
  150.         minlen = DEF_LEN;
  151.  
  152.     if (!(bfr = malloc((u_int)minlen))) {
  153.         (void)fprintf(stderr, "strings: %s\n", strerror(errno));
  154.         exit(1);
  155.     }
  156.     bfr[minlen] = '\0';
  157.     file = "stdin";
  158.     do {
  159.         if (*argv) {
  160.             file = *argv++;
  161.             if (!freopen(file, "r", stdin)) {
  162.                 (void)fprintf(stderr,
  163.                     "strings; %s: %s\n", file, strerror(errno));
  164.                 exitcode = 1;
  165.                 goto nextfile;
  166.             }
  167.         }
  168.         foff = 0;
  169. #define DO_EVERYTHING()        {read_len = -1; head_len = 0; goto start;}
  170.         read_len = -1;
  171.         if (asdata)
  172.             DO_EVERYTHING()
  173.         else {
  174.             head = (EXEC *)hbfr;
  175.             if ((head_len =
  176.                 read(fileno(stdin), head, sizeof(EXEC))) == -1)
  177.                 DO_EVERYTHING()
  178.             if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
  179.                 foff = N_TXTOFF(*head);
  180.                 if (fseek(stdin, foff, SEEK_SET) == -1)
  181.                     DO_EVERYTHING()
  182.                 read_len = head->a_text + head->a_data;
  183.                 head_len = 0;
  184.             }
  185.             else
  186.                 hcnt = 0;
  187.         }
  188. start:
  189.         for (cnt = 0; (ch = getch()) != EOF;) {
  190.             if (ISSTR(ch)) {
  191.                 if (!cnt)
  192.                     C = bfr;
  193.                 *C++ = ch;
  194.                 if (++cnt < minlen)
  195.                     continue;
  196.                 if (fflg)
  197.                     printf("%s:", file);
  198.                 if (oflg)
  199.                     printf("%07ld %s",
  200.                         foff - minlen, (char *)bfr);
  201.                 else
  202.                     printf("%s", bfr);
  203.                 while ((ch = getch()) != EOF && ISSTR(ch))
  204.                     putchar((char)ch);
  205.                 putchar('\n');
  206.             }
  207.             cnt = 0;
  208.         }
  209. nextfile: ;
  210.     } while (*argv);
  211.     exit(exitcode);
  212. }
  213.  
  214. /*
  215.  * getch --
  216.  *    get next character from wherever
  217.  */
  218. getch()
  219. {
  220.     ++foff;
  221.     if (head_len) {
  222.         if (hcnt < head_len)
  223.             return((int)hbfr[hcnt++]);
  224.         head_len = 0;
  225.     }
  226.     if (read_len == -1 || read_len-- > 0)
  227.         return(getchar());
  228.     return(EOF);
  229. }
  230.  
  231. static void
  232. usage()
  233. {
  234.     (void)fprintf(stderr,
  235.         "usage: strings [-afo] [-n length] [file ... ]\n");
  236.     exit(1);
  237. }
  238.