home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / ARPTools / src / String.c < prev    next >
C/C++ Source or Header  |  1989-11-27  |  4KB  |  218 lines

  1. /*
  2.           String - Extract Ascii strings from a file.
  3.  
  4.           Base on the 'Strings' UNIX port by Egidio Casiraghi published
  5.           in MC-Microcumputer 84, April 1989. Many enhancements by Fabio
  6.           Rossetti: resident, wildcards, BANNER switch.
  7.  
  8.       (c) 1989 by Fabio Rossetti
  9.  
  10.       To compile under Lattice C v5.0x use:
  11.  
  12.         lc -O -cus -v string
  13.         blink lib:cres.o string.o to string lib lib:a.lib lib:lc.lib sd nd
  14.  
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <exec/libraries.h>
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22. #include <libraries/arpbase.h>
  23. #include <arpfunctions.h>
  24. #include <proto/exec.h>
  25. #include <proto/dos.h>
  26.  
  27. #define START 1
  28. #define ALPHA 2
  29. #define BUFSIZE 512
  30.  
  31. #define FROM    argv[0]
  32. #define MIN    argv[1]
  33. #define BANNER    argv[2]
  34.     STRPTR argv[3];
  35.  
  36. struct ArpBase *ArpBase;
  37.  
  38. struct UserAnchor {
  39.     struct    AnchorPath    ua_AP;
  40.     BYTE    moremem[255];    /* extension */
  41. };
  42.  
  43. struct    UserAnchor *Anchor;
  44. /* These must be global because of shutdown on ^C */
  45. TEXT *buffer=NULL;
  46. BPTR fh;
  47. TEXT Banner[256];
  48.  
  49. struct Process *Pr;
  50.             
  51.     ULONG count;
  52.     LONG minlen;
  53.  
  54. /* Delete Lattice func to keep down code size */
  55. VOID MemCleanup()
  56. {
  57. }
  58.  
  59. VOID Cleanup(r1,r2,msg)
  60. LONG r1,r2;
  61. STRPTR msg;
  62. {
  63.     if (msg) Puts(msg);
  64.     if (buffer) FreeMem(buffer,BUFSIZE);
  65.     if (Anchor) FreeAnchorChain((struct AnchorPath *)Anchor);
  66.     if (ArpBase) CloseLibrary((struct Library *)ArpBase);
  67.     Pr->pr_Result2 = r2;
  68.     exit(r1);
  69. }
  70.  
  71. /* This is the real work, a buffer with a segment of the file being
  72. examinated is scanned for strings */
  73.  
  74. BOOL strings(buf,len,minlen)
  75. LONG len;
  76. LONG minlen;
  77. TEXT *buf;
  78. {
  79.  
  80.     REGISTER LONG k,n;
  81.     REGISTER TEXT *start,c;
  82.     REGISTER LONG status;
  83.  
  84.     status=START;
  85.     for(k=0;k<(LONG)len;k++)
  86.     {
  87.         c=buf[k];
  88.         switch (status)
  89.         {
  90.         case START:
  91.             if(c<32 || c >125)
  92.                 break;
  93.             status=ALPHA;
  94.             start=buf+k;
  95.             n=1;
  96.             break;
  97.         case ALPHA:
  98.             if(c>31 && c<126)
  99.             {
  100.                 n++;
  101.                 break;
  102.             }
  103.             if((long)n>=minlen)
  104.             {
  105.                 buf[k]='\0';
  106.                 Printf("%s\n",start);
  107.                 /* Check for ^C */
  108.                 if (SetSignal(0,0) & SIGBREAKF_CTRL_C) 
  109.                     Cleanup(RETURN_WARN,NULL,"***BREAK");
  110.                 if (CheckBreak(SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F,NULL)) {
  111.                     Puts("***WARN: file read abort");
  112.                     return(TRUE);
  113.                     }
  114.             }
  115.             status=START;
  116.             break;
  117.         }
  118.     }
  119.     return (FALSE);
  120. }
  121.  
  122. VOID Scan(filh)
  123. BPTR filh;
  124. {
  125.     /* print banner, if requested */
  126.     if (BANNER) {    PathName(ArpLock(Anchor->ua_AP.ap_Buf),Banner,255);
  127.             Printf("*** File %s ***\n",
  128.             Banner);}
  129.  
  130.     for (;;)
  131.     {
  132.         count=Read(filh,buffer,BUFSIZE);
  133.         if ((!count) || (strings(buffer,count,minlen))) break;
  134.     }
  135. }        
  136.  
  137.  
  138. VOID _main(Line)
  139.  
  140. STRPTR Line;
  141.  
  142. {
  143.  
  144.     LONG    Result,fl;
  145.     ULONG argc;
  146.             
  147.     
  148.     Pr = (struct Process*)FindTask(NULL);
  149.  
  150.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion))){
  151.             Cleanup(RETURN_FAIL,ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  152.             }
  153.  
  154.     /* parse command line */
  155.  
  156.     for (argc=0;argc < 3;++argc)
  157.         argv[argc] = (STRPTR) NULL;
  158.  
  159.     while(*Line > ' ')
  160.         ++Line;
  161.  
  162.      if (GADS(++Line,
  163.             strlen(Line),
  164.             "\nUsage: String FROM pattern [MIN=M minlenght] [BANNER=B]\n",
  165.             argv,
  166.             "FROM,MIN=M/K,BANNER=B/S") <= 0) 
  167.                 Cleanup(RETURN_WARN,NULL,argv[0]);
  168.                 
  169.                 
  170.  
  171.     /* Set minimum lenght of strings to be displayed */
  172.     if((minlen=Atol(MIN))<=0)
  173.             minlen=4;
  174.  
  175.  
  176.     if(!(buffer=(TEXT *)AllocMem(BUFSIZE,MEMF_CLEAR|MEMF_PUBLIC)))
  177.         Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error: no memory");
  178.  
  179.     /* Allocate space for anchorpath */     
  180.     if ( Anchor = (struct UserAnchor *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  181.     {
  182.         Anchor->ua_AP.ap_Length = 255;    /* Want full path built */
  183.         
  184.     }
  185.     else Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error:No memory");
  186.  
  187.     /* Looks for files matching pattern */
  188.     Result = FindFirst(argv[0],(struct AnchorPath*) Anchor);
  189.     fl=-1; 
  190.     while ( Result == 0 )
  191.     {
  192.         fl = 0; 
  193.  
  194.     /* Get file if it is not a directory block */
  195.         if (Anchor->ua_AP.ap_Info.fib_DirEntryType < 0) {
  196.             fh=ArpOpen(Anchor->ua_AP.ap_Buf,MODE_OLDFILE);
  197.  
  198.         Scan(fh);
  199.         }
  200.  
  201.         Result = FindNext((struct AnchorPath*) Anchor );
  202.     
  203.  
  204.     }
  205.     /* This in case a pattern with no matches is given */
  206.     if ((Result == ERROR_OBJECT_NOT_FOUND) || 
  207.         ((Result == ERROR_NO_MORE_ENTRIES)  && fl) )
  208.             Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,"Error:Can't get file");
  209.     
  210.     /* if no FROM , look into stdin */
  211.     
  212.  
  213.     Cleanup(NULL,NULL,NULL);
  214.     
  215. }
  216.  
  217.  
  218.