#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <malloc.h>
/* supply argv[1] the string to be searched , supply argv[2] the DIR name */
void grepfn(char str1[],char search[], char filename1[]);
void filesearch(char filename[], char find1[]);
int main(int argc, char *argv[])
{
int chk,len;
char *filename,*file1;
char *find;
len=strlen(argv[1]);
find=(char*)malloc(sizeof(char)*len);
strcpy(find,argv[1]);
DIR *dir= NULL;
struct dirent *file = NULL;
dir= opendir(argv[2]);
while(file=readdir(dir))
{
len= sizeof(file->d_name);
filename=(char*)malloc(sizeof(char)*len);
strcpy(filename,file->d_name);
chk=check(filename);
if(chk)
{
file1=(char *)malloc(((sizeof(char))*strlen(argv[2]))+((sizeof(char))*strlen(filename)));
sprintf(file1,"%s/%s",argv[2],filename);
filesearch(file1,find);
}
else
{
continue; //skip to next file if the file has .o extension
}
}
}
int check(char filename[]) // function to check whether the file contains .o
{
int i=0;
while(filename[i]!='.'&&filename[i]!='\0')
{
i++;
}
if(filename[i]=='.'&&filename[i]!='\0')
i++;
while(filename[i]!='\0')
{
if(filename[i]=='o')
return 0;
i++;
}
return 1;
}
void filesearch(char filename[],char find1[])
{
int i=0, cnt=0,k=0, line=0;
char ch;
char str[300];
FILE *fd = NULL;
fd = fopen(filename,"r");
if(NULL == fd)
{
printf("\n fopen() Error!!!\n");
exit(0);
}
ch=(char)fgetc(fd);
i=0;
while(ch!= EOF)// this loop for counting the number of lines in the file
{
ch=(char)fgetc(fd);
if(ch=='\n')
{
cnt++;
}
}
fseek( fd, 0, SEEK_SET );//restoring the pointer to begining of the file
ch=(char)fgetc(fd);
for(line=0;line<cnt;line++)
{
while(ch!='\n')// this loop copies the line to str[]
{
str[i]=ch;
ch=(char)fgetc(fd);
i++;
}
str[i]='\0';
grepfn(str,find1,filename); // passing the line (str), search pattern(find1), and name of the file(filename)
ch=(char)fgetc(fd);
i=0;
}
fclose(fd);
}
void grepfn(char str1[], char search[], char filename1[]) //searches the pattern in a line
{
int flag=0,j=0,k=0,size,size1;
size = strlen(str1);
size1 = strlen(search);
for(k=0;k<size;k++)//loop for pattern matching
{
if(search[j]==str1[k])
{
j++;
}
else
{
j=0;
continue;
}
if(j==size1) // updates flag = 1 when it finds the pattern
{
flag=1;
break;
}
}
if(flag==1)
printf("%s\t%s\n",str1,filename1);
}
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <malloc.h>
/* supply argv[1] the string to be searched , supply argv[2] the DIR name */
void grepfn(char str1[],char search[], char filename1[]);
void filesearch(char filename[], char find1[]);
int main(int argc, char *argv[])
{
int chk,len;
char *filename,*file1;
char *find;
len=strlen(argv[1]);
find=(char*)malloc(sizeof(char)*len);
strcpy(find,argv[1]);
DIR *dir= NULL;
struct dirent *file = NULL;
dir= opendir(argv[2]);
while(file=readdir(dir))
{
len= sizeof(file->d_name);
filename=(char*)malloc(sizeof(char)*len);
strcpy(filename,file->d_name);
chk=check(filename);
if(chk)
{
file1=(char *)malloc(((sizeof(char))*strlen(argv[2]))+((sizeof(char))*strlen(filename)));
sprintf(file1,"%s/%s",argv[2],filename);
filesearch(file1,find);
}
else
{
continue; //skip to next file if the file has .o extension
}
}
}
int check(char filename[]) // function to check whether the file contains .o
{
int i=0;
while(filename[i]!='.'&&filename[i]!='\0')
{
i++;
}
if(filename[i]=='.'&&filename[i]!='\0')
i++;
while(filename[i]!='\0')
{
if(filename[i]=='o')
return 0;
i++;
}
return 1;
}
void filesearch(char filename[],char find1[])
{
int i=0, cnt=0,k=0, line=0;
char ch;
char str[300];
FILE *fd = NULL;
fd = fopen(filename,"r");
if(NULL == fd)
{
printf("\n fopen() Error!!!\n");
exit(0);
}
ch=(char)fgetc(fd);
i=0;
while(ch!= EOF)// this loop for counting the number of lines in the file
{
ch=(char)fgetc(fd);
if(ch=='\n')
{
cnt++;
}
}
fseek( fd, 0, SEEK_SET );//restoring the pointer to begining of the file
ch=(char)fgetc(fd);
for(line=0;line<cnt;line++)
{
while(ch!='\n')// this loop copies the line to str[]
{
str[i]=ch;
ch=(char)fgetc(fd);
i++;
}
str[i]='\0';
grepfn(str,find1,filename); // passing the line (str), search pattern(find1), and name of the file(filename)
ch=(char)fgetc(fd);
i=0;
}
fclose(fd);
}
void grepfn(char str1[], char search[], char filename1[]) //searches the pattern in a line
{
int flag=0,j=0,k=0,size,size1;
size = strlen(str1);
size1 = strlen(search);
for(k=0;k<size;k++)//loop for pattern matching
{
if(search[j]==str1[k])
{
j++;
}
else
{
j=0;
continue;
}
if(j==size1) // updates flag = 1 when it finds the pattern
{
flag=1;
break;
}
}
if(flag==1)
printf("%s\t%s\n",str1,filename1);
}
No comments:
Post a Comment