Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,109 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,698 people online right now. Registration is fast and FREE... Join Now!




find command in c

 
Reply to this topicStart new topic

find command in c

dejabu18
15 Oct, 2007 - 10:52 AM
Post #1

New D.I.C Head
*

Joined: 3 Mar, 2006
Posts: 31


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Find Command In C
15 Oct, 2007 - 11:17 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
In C programming you will have to write the entire stack implementation yourself. From the push to the pop to the IsEmpty() function. One fabulous site shows you the complete implementation of a stack structure and how to go about implementing it. At the end it even provides you the full source code (header and .c file).

Implementing a Stack in C

As far as design choice, you could use a stack if you want to also keep track of the directory paths order. But if you just want to keep a history of the paths, a simple array of strings will work too. Each path can then be stored in the array which you could then sort or whatever you like.

It is up to you as to what you need to do with the paths once you get them.

Hope this helps! smile.gif
User is offlineProfile CardPM
+Quote Post

dejabu18
RE: Find Command In C
15 Oct, 2007 - 01:57 PM
Post #3

New D.I.C Head
*

Joined: 3 Mar, 2006
Posts: 31


My Contributions
QUOTE(Martyr2 @ 15 Oct, 2007 - 12:17 PM) *

In C programming you will have to write the entire stack implementation yourself. From the push to the pop to the IsEmpty() function. One fabulous site shows you the complete implementation of a stack structure and how to go about implementing it. At the end it even provides you the full source code (header and .c file).

Implementing a Stack in C

As far as design choice, you could use a stack if you want to also keep track of the directory paths order. But if you just want to keep a history of the paths, a simple array of strings will work too. Each path can then be stored in the array which you could then sort or whatever you like.

It is up to you as to what you need to do with the paths once you get them.

Hope this helps! smile.gif


thanks for the site its amazing, but I think the stack its better because I need to keep the directory path order to re inject in the next readdir call because as you know this function returns a pointer to current directory entry stream but this data its overwritten by an other call of this function in the same directory stream so I think its better if I keep the order but an other thing is that the memory allocation of the stack must be dynamically allocated look this is my stack code it works but I don't know where to put it together
CODE
}


int emp_stack(pile **p){
    
    if((*p)->pt_prev==NULL) return 0;
    else return 1;
}
    
int  push(pile **pt_t, char *data){             //passing like arguments pointer to pointer to get the address memory of the structure
            if(pt_t !=NULL){
    pile *p_p=*pt_t;
    pile *element=NULL;

             element=malloc( sizeof( pile));  //allow the memory for the new element
            if (element !=NULL){                     //verifing if the list its empty
            //if not epmty
                          element->pt_prev=*pt_t; //point the pt_prev to the previeus element        
                      strcpy(element->data,data);     //assign the data to the data field of our structure
                              
                              *pt_t=element;
                  return 0;          //point to the top of the stack
                              }else{
                        return 1;
                                    exit(1);  
                                    
                                    }
                                    }
                                    }
      
char  *pop(pile  **p){
       char  *val=NULL;
     if(p!=NULL &&  *p!=NULL){
    pile *p_l=*p;//assignement the the last objet to the p_ls pointer
       pile *p_p=p_l->pt_prev; //saving the previeus objet
            
if (p_l!=NULL){
    
    val=p_l->data;

    free(p_l);
    p_l=NULL;
      *p=p_p;
       return val;
       }
}


but do you think that I m in the right way


thanks

This post has been edited by dejabu18: 16 Oct, 2007 - 02:23 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:28PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month