Thread: Getting a relative path from a dirent

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    8

    Getting a relative path from a dirent

    Hey guys

    I have a function that recursively searches a directory tree for files and grabs their size and name.

    Code:
    void printdir(char *rdir, char *dir, char *fullpath, int depth, int fd)
    {
        DIR *dp;
        struct dirent *entry;
        struct stat statbuf;
        struct stat st;
    
    
        if((dp = opendir(dir)) == NULL) {
            fprintf(stderr,"cannot open directory: %s\n", dir);
            return;
        }
        
        chdir(dir);
    
    
        while((entry = readdir(dp)) != NULL) {
            lstat(entry->d_name,&statbuf);
            if(S_ISDIR(statbuf.st_mode)) {
                if(strcmp(".",entry->d_name) == 0 ||
                    strcmp("..",entry->d_name) == 0)
                                                           
                printdir(rdir, entry->d_name, fullpath, depth+4, fd);
            }
            else if(S_ISREG(statbuf.st_mode))
            {                  
               char *name = entry->d_name                  
               stat(entry->d_name, &st);
               int size = st.st_size;
            }
        }
    
    
        chdir("..");
        closedir(dp);
    }
    It works fine but I'd like to get a file's name relative to the dir that it is searching, entry->d_name just returns the name of the file. I tried messing around with strcat() and strcpy() but I couldn't get desired results.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got an interestingly-named parameter named fullpath that you don't seem to be using -- is that where you want the full path to be stored? If so, then a straightforward approach might be:
    1. Pass a blank character array the first time you call the function (ideally of size PATH_MAX).
    2. If you come across a directory, strcat the directory name into your fullpath array, and put a slash on the end.
    3. When you return from your recursive call, take out the slash at the end (i.e. overwrite it with a '\0' character), and use strrchr to find the previous slash at the end of the string so you can take out the new directory you just added. (Alternatively, and maybe easier, you can store in an int what position you should walk back to and write the '\0' character at. Technically that can require more memory to be added recursively, but it's just an int each time so you probably won't run out of memory unless you have a really complicated directory tree.)

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also are you sure about your comparisons with "." and ".."? Right now it looks like you're trying to change directories only when it is . or .., rather than all the other cases.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    8
    Forgot a continue statement when I posted my code.

  5. #5
    Registered User
    Join Date
    Nov 2018
    Posts
    8
    But if I remove only the newest directory wouldn't that that still leave the higher level directories in fullpath?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pboy View Post
    But if I remove only the newest directory wouldn't that that still leave the higher level directories in fullpath?
    It would, which is good, because you want them to still be there -- notice that (at the minimum) you still have to process all the files in the current directory so you don't want to take the path back to original blankness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prompt \w gives relative path
    By vkaushal21 in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2009, 01:09 PM
  2. concatentating dirent.h dname from dirent.h to the path in C
    By amjadcsu in forum Linux Programming
    Replies: 1
    Last Post: 07-20-2009, 04:55 PM
  3. How to judge whether a path is relative or absolute?
    By bbebfe in forum C Programming
    Replies: 2
    Last Post: 11-16-2008, 03:23 AM
  4. MSVC: output .pd under relative path?
    By pheres in forum Windows Programming
    Replies: 0
    Last Post: 02-04-2008, 10:55 AM
  5. Path of file relative to program
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-15-2007, 05:30 AM

Tags for this Thread