troubleshooting Question

C program for ls -R

Avatar of zahmadian
zahmadian asked on
C
23 Comments1 Solution2346 ViewsLast Modified:
Hi

Last time you stop answering my question, so, I split points, and close that one, and since I came up with some new ideas I post them here

I am writing a program in C, to do ls -R command, which is suppose to print the directories name and their failes, and go through sub directories and print their files and so on, recursively...

what I came up so far is following code, which its main problem is  that, its doing right thing for current directory, but not when I give the program a path like /home/d/d5m5/cs213 its still gicing me the directories nad their files in current directory which is : /home/d/d5m5/cs213/L2C

Do you have any idea how can I fixed it?

 
void recPrint(char* path)
{
        DIR* dir;                       //PIONTER TO SCANNED DIRECTORY
        struct dirent* entry;           // POINTER TO ONE DIRECTORY ENTRY
        char cwd[MAX_DIR_PATH+1];       // CURRENT WORKING DIRECTORY
        struct stat dir_stat;           //USED BY STAT()
     
        getcwd(cwd,MAX_DIR_PATH+1);
        dir=opendir(".");
        if (!dir)
        {
                fprintf(stderr, "cannot read directory '%s' : ",cwd);
                return;
        }
 
 
        while((entry=readdir(dir)))
        {
                printf("\n %s \n", entry->d_name);
       
                //check if given entry is directory
                if (stat(entry->d_name, &dir_stat)== -1)
                {
                        perror("stat:\n");
                        continue;  
                }
       
         
                //skip the '.' and '..' enteries, to avoid loops.
                if (strcmp(entry->d_name, ".") == 0)
                        continue;
 
                if (strcmp(entry->d_name, "..") == 0)

                        continue;
                       
                /* is this a directory? */
                if (S_ISDIR(dir_stat.st_mode)) {
       
                /* Change into the new directory */
                        if (chdir(entry->d_name) == -1) {
                        fprintf(stderr, "Cannot chdir into '%s': ", entry->d_name);
                        perror("");
                        continue;
                }

                /* check this directory */
                recPrint(entry->d_name);

 
                /* finally, restore the original working directory. */
                if (chdir("..") == -1) {
                        fprintf(stderr, "Cannot chdir back to '%s': ", cwd);
                        perror("");
                        exit(1);
                }// end if
        }//end while
}
                       
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 23 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros