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.

12

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.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật