home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / text-utils / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  5.6 KB  |  223 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 <a.out.h>
  51. #include <unistd.h>
  52. #include <stdio.h>
  53. #include <ctype.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <locale.h>
  57.  
  58. #define DEF_LEN        4        /* default minimum string length */
  59. #if 0
  60. #define ISSTR(ch)    (isascii(ch) && (isprint(ch) || ch == '\t'))
  61. #else
  62. #define ISSTR(ch)    (isprint(ch) || ch == '\t')
  63. #endif
  64.  
  65. typedef struct exec    EXEC;        /* struct exec cast */
  66.  
  67. static long    foff;            /* offset in the file */
  68. static int    hcnt,            /* head count */
  69.         head_len,        /* length of header */
  70.         read_len;        /* length to read */
  71. static u_char    hbfr[sizeof(EXEC)];    /* buffer for struct exec */
  72.  
  73. static void usage();
  74.  
  75. main(argc, argv)
  76.     int argc;
  77.     char **argv;
  78. {
  79.     extern char *optarg;
  80.     extern int optind;
  81.     register int ch, cnt;
  82.     register u_char *C;
  83.     EXEC *head;
  84.     int exitcode, minlen;
  85.     short asdata, oflg, fflg;
  86.     u_char *bfr;
  87.     char *file, *p;
  88.  
  89.     setlocale(LC_CTYPE, "");
  90.  
  91.  
  92.     /*
  93.      * for backward compatibility, allow '-' to specify 'a' flag; no
  94.      * longer documented in the man page or usage string.
  95.      */
  96.     asdata = exitcode = fflg = oflg = 0;
  97.     minlen = -1;
  98.     while ((ch = getopt(argc, argv, "-0123456789an:of")) != EOF)
  99.         switch((char)ch) {
  100.         case '0': case '1': case '2': case '3': case '4':
  101.         case '5': case '6': case '7': case '8': case '9':
  102.             /*
  103.              * kludge: strings was originally designed to take
  104.              * a number after a dash.
  105.              */
  106.             if (minlen == -1) {
  107.                 p = argv[optind - 1];
  108.                 if (p[0] == '-' && p[1] == ch && !p[2])
  109.                     minlen = atoi(++p);
  110.                 else
  111.                     minlen = atoi(argv[optind] + 1);
  112.             }
  113.             break;
  114.         case '-':
  115.         case 'a':
  116.             asdata = 1;
  117.             break;
  118.         case 'f':
  119.             fflg = 1;
  120.             break;
  121.         case 'n':
  122.             minlen = atoi(optarg);
  123.             break;
  124.         case 'o':
  125.             oflg = 1;
  126.             break;
  127.         case '?':
  128.         default:
  129.             usage();
  130.         }
  131.     argc -= optind;
  132.     argv += optind;
  133.  
  134.     if (minlen == -1)
  135.         minlen = DEF_LEN;
  136.  
  137.     if (!(bfr = malloc((u_int)minlen))) {
  138.         (void)fprintf(stderr, "strings: %s\n", strerror(errno));
  139.         exit(1);
  140.     }
  141.     bfr[minlen] = '\0';
  142.     file = "stdin";
  143.     do {
  144.         if (*argv) {
  145.             file = *argv++;
  146.             if (!freopen(file, "r", stdin)) {
  147.                 (void)fprintf(stderr,
  148.                     "strings; %s: %s\n", file, strerror(errno));
  149.                 exitcode = 1;
  150.                 goto nextfile;
  151.             }
  152.         }
  153.         foff = 0;
  154. #define DO_EVERYTHING()        {read_len = -1; head_len = 0; goto start;}
  155.         read_len = -1;
  156.         if (asdata)
  157.             DO_EVERYTHING()
  158.         else {
  159.             head = (EXEC *)hbfr;
  160.             if ((head_len =
  161.                 read(fileno(stdin), head, sizeof(EXEC))) == -1)
  162.                 DO_EVERYTHING()
  163.             if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
  164.                 foff = N_TXTOFF(*head);
  165.                 if (fseek(stdin, foff, SEEK_SET) == -1)
  166.                     DO_EVERYTHING()
  167.                 read_len = head->a_text + head->a_data;
  168.                 head_len = 0;
  169.             }
  170.             else
  171.                 hcnt = 0;
  172.         }
  173. start:
  174.         for (cnt = 0; (ch = getch()) != EOF;) {
  175.             if (ISSTR(ch)) {
  176.                 if (!cnt)
  177.                     C = bfr;
  178.                 *C++ = ch;
  179.                 if (++cnt < minlen)
  180.                     continue;
  181.                 if (fflg)
  182.                     printf("%s:", file);
  183.                 if (oflg)
  184.                     printf("%07ld %s",
  185.                         foff - minlen, (char *)bfr);
  186.                 else
  187.                     printf("%s", bfr);
  188.                 while ((ch = getch()) != EOF && ISSTR(ch))
  189.                     putchar((char)ch);
  190.                 putchar('\n');
  191.             }
  192.             cnt = 0;
  193.         }
  194. nextfile: ;
  195.     } while (*argv);
  196.     exit(exitcode);
  197. }
  198.  
  199. /*
  200.  * getch --
  201.  *    get next character from wherever
  202.  */
  203. getch()
  204. {
  205.     ++foff;
  206.     if (head_len) {
  207.         if (hcnt < head_len)
  208.             return((int)hbfr[hcnt++]);
  209.         head_len = 0;
  210.     }
  211.     if (read_len == -1 || read_len-- > 0)
  212.         return(getchar());
  213.     return(EOF);
  214. }
  215.  
  216. static void
  217. usage()
  218. {
  219.     (void)fprintf(stderr,
  220.         "usage: strings [-afo] [-n length] [file ... ]\n");
  221.     exit(1);
  222. }
  223.