home *** CD-ROM | disk | FTP | other *** search
/ Micro R&D 1 / MicroRD-CD-ROM-Vol1-1994.iso / os20 / cli / cdtools1_0.lha / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-20  |  3.9 KB  |  187 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/libraries.h>
  4. #include <dos/dosextens.h>
  5. #include <dos/stdio.h>
  6.  
  7. #include <proto/exec_protos.h>
  8. #include <proto/dos_protos.h>
  9. #include <clib/alib_protos.h>
  10.  
  11. #include <stdlib.h>
  12.  
  13. #ifndef __COMMODORE_DATE__
  14. #define __COMMODORE_DATE__ __DATE__
  15. #endif
  16.  
  17. #define BASENAME "Strings"
  18. #define MAXPATHLEN 256
  19.  
  20. const STATIC char VER[] = "\0$VER: " BASENAME " 1.0 (" __COMMODORE_DATE__ ")";
  21. const STATIC char Basename[] = BASENAME;
  22.  
  23. STATIC BOOL stop = FALSE;
  24.  
  25. STATIC BOOL CheckBreak(VOID)
  26. {
  27.    if (stop)
  28.       return TRUE;
  29.  
  30.    if (stop = (SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C))
  31.       SetIoErr(ERROR_BREAK);
  32.  
  33.    return stop;
  34. }
  35.  
  36. STATIC VOID Strings(BPTR file,UBYTE *buffer,LONG len,BOOL showoffs)
  37. {
  38. ULONG offs = 0,oldoffs = 0;
  39. LONG c;
  40. UBYTE *bp = buffer;
  41.  
  42.    while ((c = FGetC(file)) != -1 && !CheckBreak())
  43.       {
  44.       offs++;
  45.  
  46.       if (c>=32 && c<127)
  47.      {
  48.      if (bp==buffer)
  49.         oldoffs = offs;
  50.      *bp++ = c;
  51.      if (bp == buffer+len)
  52.         {
  53.         *bp = 0;
  54.         if (showoffs)
  55.            Printf("%ld : ",oldoffs-1);
  56.         PutStr(buffer);
  57.  
  58.         while ((c = FGetC(file)) != -1 && !CheckBreak())
  59.            {
  60.            offs++;
  61.            if (c>=32 && c<127)
  62.           WriteChar(c);
  63.            else
  64.           break;
  65.            };
  66.  
  67.         WriteChar('\n');
  68.         bp = buffer;
  69.         };
  70.      }
  71.       else
  72.      bp = buffer;
  73.       };
  74. }
  75.  
  76. __stkargs void _main(int unused_arglen, char *unused_argptr)
  77. {
  78. int ret = RETURN_FAIL;
  79.  
  80.    if ((DOSBase->lib_Version>=37) && (SysBase->lib_Version >= 37))
  81.       {
  82.       if (Input() && Output())
  83.      {
  84.      LONG array[] = { NULL, NULL, FALSE, FALSE };
  85.      struct RDArgs *rdargs = ReadArgs("PATTERN/M,LENGTH/N/K,OFFS/S,ALL/S", array, NULL);
  86.      if (rdargs)
  87.         {
  88.         LONG len = 10;
  89.         if (array[1])
  90.            len = *(LONG *)array[1];
  91.         if (len<2)
  92.            len = 2;
  93.  
  94.         UBYTE *buffer = AllocVec(len+1,MEMF_ANY);
  95.         if (buffer)
  96.            {
  97.            STRPTR *multi = (STRPTR *)array[0];
  98.            if (!*multi)
  99.           Strings(Input(),buffer,len,array[2]);
  100.            else
  101.           {
  102.           struct AnchorPath *anchorpath = AllocVec(sizeof(struct AnchorPath)+MAXPATHLEN,MEMF_CLEAR);
  103.           if (anchorpath)
  104.              {
  105.              anchorpath->ap_BreakBits = SIGBREAKF_CTRL_C;
  106.              anchorpath->ap_Strlen   = MAXPATHLEN;
  107.  
  108.              ret = RETURN_OK;
  109.  
  110.              while (*multi && !CheckBreak())
  111.             {
  112.             LONG mferr = MatchFirst(*multi,anchorpath);
  113.             while (mferr == 0 && !CheckBreak())
  114.                {
  115.                if (anchorpath->ap_Flags &  APF_DIDDIR)
  116.                   anchorpath->ap_Flags &= ~(APF_DIDDIR | APF_DODIR);
  117.                else
  118.                   {
  119.                   if (anchorpath->ap_Info.fib_DirEntryType > 0) /* directory */
  120.                  {
  121.                  if (array[3])
  122.                     anchorpath->ap_Flags |= APF_DODIR;
  123.                  }
  124.                   else if (anchorpath->ap_Info.fib_DirEntryType < 0) /* file */
  125.                  {
  126.                  BPTR file = Open(anchorpath->ap_Buf, MODE_OLDFILE);
  127.                  if (file)
  128.                     {
  129.                     Printf("%ls\n",anchorpath->ap_Buf);
  130.                     Strings(file,buffer,len,array[2]);
  131.                     Close(file);
  132.                     }
  133.                  else
  134.                     {
  135.                     PrintFault(ret = IoErr(),anchorpath->ap_Buf);
  136.                     break;
  137.                     };
  138.                  };
  139.                   };
  140.                mferr = MatchNext(anchorpath);
  141.                };
  142.             multi++;
  143.             if (mferr != ERROR_NO_MORE_ENTRIES)
  144.                {
  145.                PrintFault(ret = IoErr(),Basename);
  146.                break;
  147.                };
  148.             if (mferr == ERROR_BREAK)
  149.                stop = TRUE;
  150.             };
  151.  
  152.              FreeVec(anchorpath);
  153.              }
  154.           else
  155.              PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
  156.           };
  157.  
  158.            if (CheckBreak())
  159.           PrintFault(ret = ERROR_BREAK,Basename);
  160.            }
  161.         else
  162.            PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
  163.  
  164.         FreeArgs(rdargs);
  165.         }
  166.      else
  167.         PrintFault(ret = IoErr(),Basename);
  168.      }
  169.       else
  170.      PrintFault(ret = IoErr(),Basename);
  171.       }
  172.    else
  173.       {
  174.       if (DOSBase->lib_Version >= LIBRARY_MINIMUM)
  175.      {
  176.      if (Output())
  177.         {
  178.         Write(Output(),Basename,sizeof(Basename)-1);
  179.         Write(Output(),": Can't open dos.library v37 (OS 2.04)\n",39);
  180.         };
  181.      ret = ERROR_INVALID_RESIDENT_LIBRARY;
  182.      };
  183.       };
  184.  
  185.    _exit(ret);
  186. }
  187.