Is there a POSIX script for resolving file system paths?

Is there a POSIX script for resolving file system paths? I have a CWD for the path as well as the file path from that CWD. I cannot use chdir

to switch to directory because I need to resolve paths from multiple threads at the same time. I've considered adding /

between CWD and outline, but it feels like a hack for some reason. Is this the correct way to resolve relative paths?

+2


a source to share


2 answers


I think that the addition /

should be sufficient in almost all situations - even with the .

, ..

, additional /

s or symbolic link, it must be done correctly. If you really want to do this with the standard library, you can use realpath(3)

path normalization, but I don't know how to do exactly what you want.



+3


a source


Depending on what you want to do with the file, and if the relatively recent additions to POSIX.1-2008 are acceptable to you openat

and friends might be of interest:

int dirfd = open(desired_cwd_path, O_RDONLY);
int fd = openat(dirfd, file_relpath, O_RDONLY);
close(dirfd);
// ...use fd

      



(These system calls have been around, for example, Solaris and Linux for some time.)

+1


a source







All Articles