home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / BOUNDUTI.ZIP / GREP.C next >
C/C++ Source or Header  |  1990-08-16  |  3KB  |  116 lines

  1. /*
  2.     -----------------------------------------
  3.       grep.c - locate all lines with string
  4.     -----------------------------------------
  5.  
  6.     written 8/15/90 by M. Mackey
  7. */
  8. #define INCL_DOS
  9. #include <os2.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14.  
  15. #define MAXLINE 256
  16. #define TRUE  1
  17. #define FALSE 0
  18. #define ATTR 0x0021
  19. #define NORMAL_FILE 0x0000
  20. #define OPEN_FLAG 0x01
  21. #define OPEN_MODE 0x0040
  22. #define LENGTH sizeof(buff)
  23.  
  24. char pattern[MAXLINE],line[MAXLINE];
  25.  
  26. static int index(void);
  27. static void error(char *fmt,char *arg);
  28.  
  29. char *signon="\ngrep -  simple file pattern searcher\n";
  30. char *signoff="grep - finished\n";
  31. char *usage="\nsyntax: grep [-n(umber)] pattern filename\n";
  32.  
  33. void main(int argc,char *argv[])
  34. {
  35.     char pathname[_MAX_PATH],buf[10];
  36.     unsigned long lineno;
  37.     unsigned number=0,nlines,handle=0xffff,count=1;
  38.     struct _FILEFINDBUF buff;
  39.     unsigned short fhandle,rc,i;
  40.     unsigned faction,nbytes;
  41.  
  42.     fprintf(stderr,"%s",signon);
  43.     if(argv[1][0]=='-') {
  44.         if(tolower(argv[1][1])=='n') {
  45.             number=1;
  46.             argv++;
  47.             argc--;
  48.         } else {
  49.             if(tolower(argv[1][1])!='h')
  50.                 fprintf(stderr,"GREP: illegal option %c\n",argv[1][1]);
  51.             error("%s",usage);
  52.         }
  53.     }
  54.     if(argc!=3)
  55.         error("%s",usage);
  56.     strcpy(pattern,argv[1]);
  57.     strcpy(pathname,argv[2]);
  58.     if(rc=DosFindFirst(pathname,&handle,ATTR,&buff,LENGTH,&count,0L))
  59.         if(rc==18)
  60.             error("grep: file %s not found\n",pathname);
  61.         else
  62.             error("grep: internal error %s\n",itoa((unsigned)rc,buf,10));
  63.     while(TRUE) {
  64.         if(DosOpen(buff.achName,&fhandle,&faction,0L,NORMAL_FILE,OPEN_FLAG,
  65.                         OPEN_MODE,0L))
  66.             error("grep: cannot open input file %s",buff.achName);
  67.         printf("-------------------- %s\n",buff.achName);
  68.         lineno=0L;
  69.         nlines=0;
  70.         while(TRUE) {
  71.             i=0;
  72.             while(TRUE) {
  73.                 DosRead(fhandle,&line[i],1,&nbytes);
  74.                 if(!nbytes || line[i]==0x0a)
  75.                     break;
  76.                 i++;
  77.             }
  78.             if(!(nbytes+i))
  79.                 break;
  80.             line[++i]='\0';
  81.             lineno++;
  82.             if((index() >= 0)) {
  83.                 if(number)
  84.                     printf("%ld: ",lineno);
  85.                 printf("%s",line);
  86.                 nlines++;
  87.             }
  88.         }
  89.         printf("%u Line(s) found\n",nlines);
  90.         DosClose(fhandle);
  91.         DosFindNext(handle,&buff,LENGTH,&count);
  92.         if(!count)
  93.             break;
  94.     }
  95.     DosFindClose(handle);
  96.     fprintf(stderr,"%s",signoff);
  97.     exit(0);
  98. }
  99. int index(void)
  100. {
  101.     int i,j,k;
  102.  
  103.     for(i=0;line[i] != '\0';i++) {
  104.         for(j=i,k=0;pattern[k]!='\0' && line[j]==pattern[k];j++,k++)
  105.                                 ;
  106.         if(pattern[k]=='\0')
  107.             return(i);
  108.     }
  109.     return(-1);
  110. }
  111. void error(char *fmt,char *arg)
  112. {
  113.     fprintf(stderr,fmt,arg);
  114.     exit(1);
  115. }
  116.