hello I'm making a little program in c to search files in the unix directory structure I've already finish to programming the search function here's the code
CODE
void op_dir(char * sfile, int op) { //SEARCH "sfile" in the current directory or list the contens of the given directory
DIR* dir_point; //point to the current directory
struct dirent* enter; //point to one directory entry
struct stat f_details; //used by lstat()
char cwdir[MAX_DIR_PATH];
//get the current working directory
if (!getcwd(cwdir, MAX_DIR_PATH+1)) {
perror("getcwd:");
return;
}
// open the directory for reading
if ((dir_point=opendir("."))==NULL){
printf("error trying to open file \n" );
}else{
// scan the directory, traversing each sub-directory, and
while ((enter=readdir(dir_point)))
{
if (op==1){
//display all the files and directories in the current directory
printf("%s/%s\n", cwdir, enter->d_name);
continue;
}
//matching the pattern for each file name.
if (enter->d_name && strstr(enter->d_name, sfile)) {
printf("%s/%s\n", cwdir, enter->d_name);
}
// check if the given entry is a directory.
if (lstat(enter->d_name, &f_details)!=-1){
if ((strcmp(enter->d_name,".")==0 || strcmp(enter->d_name, "..") == 0) && S_ISDIR(f_details.st_mode)) {
if (chdir(enter->d_name)!= -1){
op_dir(enter->d_name,op);
}
}
}
}
}
}
it can search files or print in the screen the files of a current directory but I fond that the readdir function don't save the path of a directory when it change to an other one
so I need to create a data structure to stock the path of this directory I thought to implement a stack for re inject the paths but I cant find an event or the moment to push a path or to pop , do you know how can I implement or if its the stack a real solution or not
thank you