home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / extras / dirwalker / sword.c < prev   
Encoding:
C/C++ Source or Header  |  1996-12-24  |  1.7 KB  |  104 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <proto/dos.h>
  5. #include <proto/exec.h>
  6. #include <dos.h>
  7. #include <ctype.h>
  8.  
  9. #include "dirwalker.h"
  10.  
  11. char extbuf[FESIZE+1];
  12.  
  13. #define ISEXT(x) !stricmp(extbuf, x)
  14.  
  15. char *word;
  16.  
  17. char *skipexts[] =
  18. {
  19.    "o", "map", "exe", "nd", "info", NULL
  20. };
  21.  
  22. int doext(char *ext)
  23. {
  24.    int i;
  25.    for(i=0; skipexts[i]; i++)
  26.       if(!stricmp(skipexts[i], ext)) return(0);
  27.    return(1);
  28. }
  29.  
  30. int isbinary(char *name)
  31. {
  32.    BPTR fh;
  33.    int i;
  34.    char buf[32];
  35.  
  36.    if(!(fh=Open(name, MODE_OLDFILE))) return(0);
  37.    i = Read(fh, buf, sizeof(buf));
  38.    Close(fh);
  39.  
  40.    for(i--; i>=0; i--)
  41.    {
  42.       if(!isprint(buf[i]) && !isspace(buf[i]))
  43.          return(1);
  44.    }
  45.  
  46.    return(0);
  47. }
  48.  
  49. int doabort;
  50.  
  51. void chkabort(void)
  52. {
  53.    if(SetSignal(0,0) & (SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D))
  54.    {
  55.       Write(Output(), "***Break", 8);
  56.       doabort = 1;
  57.    }
  58. }
  59.  
  60. int filefunc(char *path, char *name)
  61. {
  62.    char cmdbuf[256];
  63.  
  64.    chkabort();
  65.    if(doabort) return(1);
  66.  
  67.    stcgfe(extbuf, name);
  68.  
  69.    if(!doext(extbuf)) return(0);
  70.  
  71.    if(isbinary(name)) return(0);
  72.  
  73.    printf("%s%s\n", path, name);
  74.    sprintf(cmdbuf, "search quick %s %s", name, word);
  75.    // printf("%s\n", cmdbuf); /* For debugging */
  76.  
  77.    Execute(cmdbuf, NULL, Output());
  78.  
  79.    return(0);
  80. }
  81.  
  82. void main(int argc, char *argv[])
  83. {
  84.    char *dir = NULL;
  85.  
  86.    while(argc > 1)
  87.    {
  88.       if(!word) word = argv[1];
  89.       else if(!dir) dir = argv[1];
  90.       else
  91.       {
  92.          usage:
  93.             fprintf(stderr, "sword - search for word in all text files\n");
  94.          fprintf(stderr, "USAGE: sword <word> [dir]\n");
  95.         exit(99);
  96.       }
  97.       argc--, argv++;
  98.    }
  99.    if(!word) goto usage;
  100.    if(!dir) dir = "";
  101.  
  102.    dirwalker(dir, NULL, filefunc);
  103. }
  104.