Prog#228: grep command


#1
/*
grep command
Program#228
*/
#include<stdio.h>
main(int argc,char *argv[])
{
int p,i,j,k,l,n,m;
char a[200];
if(argc!=3)
{
printf("error: no.of arg");
return 0;
}
p=open(argv[2], 0);
if(p==-1)
{
printf("file open error");
return 0;
}
n=0;
for(j=0;(i=read(p,&a[j],1)>0);j++)
{
if(a[j]=='\n')
{
n++;
a[j]='\0';
k=0;
while(a[k])
{
l=k;
for(m=0;((argv[1][m]==a[l])&&(argv[1][m]!='\0'));n++, l++);
if(argv[1][m]=='\0')
{
printf("%s", a);
break;
}
k++;
}
j=-1;
}
}
close(p);
}