home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
unix
/
macgrep2.sit
/
grep.c
next >
Wrap
C/C++ Source or Header
|
1991-04-21
|
2KB
|
106 lines
/* The parts of the code which are specific to the Macintosh are enclosed in
C comments. For example: */
/*******************************/
/*********/
/*******************************/
/* By deleting these items, the program should compile with no changes on UNIX
machines. For instructions on how this program works, please look at the
README file. Also, if you try to build the application using Think C, be
sure to include the ANSI Library. */
/*******************************/
#include <console.h> /**********/
/*******************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHAR 1000
int getline(char *line,int max);
/*******************************/
int ccommand(char ***p); /******/
/*******************************/
int stringsearch(char *pattern,char *string);
int nextchar(char *string,int index);
main(int argc,char **argv)
{
char line[MAXCHAR];
long lineno = 0;
int c,except = 0,number = 0,found = 0;
/*******************************/
argc = ccommand(&argv); /*****/
/*******************************/
while ((--argc > 0 ) && ((*++argv)[0] == '-')) {
while (c = *++argv[0]) {
switch (c) {
case 'x' :
except = 1;
break;
case 'n' :
number = 1;
break;
default :
printf("MacGREP: illegal option %c\n",c);
exit(1);
}
}
}
if (argc != 1) {
printf("Usage: MacGREP -x -n pattern\n");
}
else {
while (getline(line,MAXCHAR) > 0) {
lineno++;
if ((stringsearch(*argv,line) != 0) != except) {
if (number) {
printf("%ld:",lineno);
}
printf("%s",line);
found++;
}
}
if (except)
printf("\nLines not matching pattern [%s]: %d",*argv,found);
else
printf("\nLines matching pattern [%s]: %d",*argv,found);
}
exit(0);
}
int getline(char *line,int max)
{
if (fgets(line,max,stdin) == NULL)
return 0;
else
return strlen(line);
}
int stringsearch(char *p,char *a)
{
int i,j,M = strlen(p),N = strlen(a);
for (i = 0,j = 0; j < M && i < N; i++,j++)
while (a[i] != p[j]) {
i -= j;
i += nextchar(a,i);
j = 0;
}
if (j == M)
return 1;
else
return 0;
}
int nextchar(char *s,int i)
{
if ((s[i] < 0) && (s[i + 1] != '\0') && (s[i +1] != '\n'))
return 2;
else
return 1;
}