home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / macgrep2.sit / grep.c next >
C/C++ Source or Header  |  1991-04-21  |  2KB  |  106 lines

  1. /* The parts of the code which are specific to the Macintosh are enclosed in
  2.    C comments. For example: */
  3.  
  4. /*******************************/
  5.                       /*********/
  6. /*******************************/
  7.  
  8. /* By deleting these items, the program should compile with no changes on UNIX
  9.    machines. For instructions on how this program works, please look at the
  10.    README file. Also, if you try to build the application using Think C, be
  11.    sure to include the ANSI Library. */
  12.  
  13. /*******************************/
  14. #include <console.h> /**********/
  15. /*******************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #define MAXCHAR   1000
  21.  
  22. int getline(char *line,int max);
  23. /*******************************/
  24. int ccommand(char ***p); /******/
  25. /*******************************/
  26. int stringsearch(char *pattern,char *string);
  27. int nextchar(char *string,int index);
  28.  
  29. main(int argc,char **argv)
  30. {
  31.   char line[MAXCHAR];
  32.   long lineno = 0;
  33.   int c,except = 0,number = 0,found = 0;
  34.  
  35. /*******************************/
  36.   argc = ccommand(&argv); /*****/
  37. /*******************************/
  38.  
  39.   while ((--argc > 0 ) && ((*++argv)[0] == '-')) {
  40.     while (c = *++argv[0]) {
  41.       switch (c) {
  42.         case 'x' :
  43.           except = 1;
  44.           break;
  45.         case 'n' :
  46.           number = 1;
  47.           break;
  48.         default :
  49.           printf("MacGREP: illegal option %c\n",c);
  50.           exit(1);
  51.       }
  52.     }
  53.   }
  54.   if (argc != 1) {
  55.     printf("Usage: MacGREP -x -n pattern\n");
  56.   }
  57.   else {
  58.     while (getline(line,MAXCHAR) > 0) {
  59.       lineno++;
  60.       if ((stringsearch(*argv,line) != 0) != except) {
  61.         if (number) {
  62.           printf("%ld:",lineno);
  63.         }
  64.         printf("%s",line);
  65.         found++;
  66.       }
  67.     }
  68.     if (except)
  69.       printf("\nLines not matching pattern [%s]: %d",*argv,found);
  70.     else
  71.       printf("\nLines matching pattern [%s]: %d",*argv,found);
  72.   }
  73.   exit(0);
  74. }
  75.  
  76. int getline(char *line,int max)
  77. {
  78.   if (fgets(line,max,stdin) == NULL)
  79.     return 0;
  80.   else
  81.     return strlen(line);
  82. }
  83.  
  84. int stringsearch(char *p,char *a)
  85. {
  86.   int i,j,M = strlen(p),N = strlen(a);
  87.  
  88.   for (i = 0,j = 0; j < M && i < N; i++,j++)
  89.     while (a[i] != p[j]) {
  90.       i -= j;
  91.       i += nextchar(a,i);
  92.       j = 0;
  93.     }
  94.   if (j == M)
  95.     return 1;
  96.   else
  97.     return 0;
  98. }
  99.  
  100. int nextchar(char *s,int i)
  101. {
  102.   if ((s[i] < 0) && (s[i + 1] != '\0') && (s[i +1] != '\n'))
  103.     return 2;
  104.   else
  105.     return 1;
  106. }