Prog#229: grep all files


#1
/*
grep all files
Program#229
*/
#include<stdio.h>
main(int argc,char *argv[])
{
int p,i,j,k,l,m,c,n;
char a[100];
if(argc<3)
{
printf("error: no.of arg");
return 0;
}
c=2;
while(c<argc)
{
p=open(argv[c], 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'));l++, m++);
if(argv[1][m]=='\0')
printf("%s", a);
k++;
}
j=-1;
}
}
close(p);
c++;
}
}