clinuxfilepathrelative-path

How can I get the path to a file relative to another file?


I am making a C program that needs to read various files in GNU/Linux.

Imagine the following file structure.

root
 |- dir1
 |   |- file1.txt
 |
 |- dir2
     |- file2.txt

Now imagine that I already got the relative path to root/dir1/file1.txt and I read the following contents:

Load: ../dir2/file2.txt

...

I can store that ../dir2/file2.txt string in a C buffer, but I can't use it directly with something like fopen(3), because it will try to open that path relative to the directory where the executable was launched.

This behavior also happens when using C's #include. The path is relative to the source where it was specified, not to the path where the compiler was launched.

My question is, how do I open file2.txt knowing the (relative) path to file1.txt and the relative path stored in it? I thought about getting the directory of file1.txt (root/dir1/) and concatenating the relative path I got from there (root/dir1/../dir2/file2.txt), but perhaps there is a better way.


Solution

  • how do I open file2.txt knowing the (relative) path to file1.txt and the relative path stored in it? I thought about getting the directory of file1.txt (root/dir1/) and concatenating the relative path I got from there (root/dir1/../dir2/file2.txt), but perhaps there is a better way.

    C does not have standard library functions for path manipulation. POSIX defines a few, though, so if you can assume a POSIX environment (and you've tagged Linux, which is close enough) then you might find dirname(), basename(), and realpath() of some use to you, though these do not directly solve your problem.

    The method you describe should work fine for well-formed inputs, including also if the known path to file1.txt is absolute. I don't think there's a simpler way. However, you probably do want to verify that the path to file2.txt is in fact relative before apply that approach. You might also want to consider validating that the .. components of the second path do not attempt to navigate beyond the first component of the first path.

    If you can rely on POSIX, though, then another alternative would involve using openat():

    #include <libgen.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    // ...
    
        char *file1_copy = strdup(path_to_file1);   // DO free() this
        char *file1_dirname = dirname(file1_copy);  // DO NOT free() this
    
        // O_DIRECTORY and O_PATH are optional and Linux-specific
        int dir1fd = open(file1_dirname, O_RDONLY | O_DIRECTORY | O_PATH);
        // ... verify success ...
    
        free(file1_copy);
    
        int file2fd = openat(dir1fd, path_to_file2, O_RDONLY);  // Possibly different flags
        // ... verify success ...
    
        close(dir1fd);
    
        // And maybe you want that as a stream, in which case:
        FILE *file2 = fdopen(file2fd, "r");
        // ... verify success ...
    

    Note that this approach works whether the given path to file2.txt is relative or absolute. I'm not saying that it's better in any absolute sense, but it might have advantages for you or otherwise be to your liking.