Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the absolute path of a file programmatically with out realpath() under linux?

Tags:

c

linux

I know it is possible to get an absolute path of a file with realpath() function. However, according to BUGS section the manpage, there are some problem in its implementation. The details are following:


BUGS

Avoid using this function. It is broken by design since (unless using the non-standard resolved_path == NULL feature) it is impossible to determine a suitable size for the output buffer, resolved_path. According to POSIX a buffer of size PATH_MAX suffices, but PATH_MAX need not be a defined constant, and may have to be obtained using pathconf(3). And asking pathconf(3) does not really help, since on the one hand POSIX warns that the result of pathconf(3) may be huge and unsuitable for mallocing memory. And on the other hand pathconf(3) may return -1 to signify that PATH_MAX is not bounded.

The libc4 and libc5 implementation contains a buffer overflow (fixed in libc-5.4.13). Thus, set-user-ID programs like mount(8) need a private version.


So, the question is what is the best practice to get the absolute path of a file?

like image 805
jcadam Avatar asked Jul 23 '09 13:07

jcadam


People also ask

How do I find the absolute path of a file in Linux?

The pwd command prints the current/working directory, telling where you are currently located in the filesystem. This command comes to your rescue when you get lost in the filesystem, and always prints out the absolute path.

How do I find the absolute path of a file?

You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.

How do you print the absolute path of the current working directory?

The answer is the pwd command, which stands for print working directory. The word print in print working directory means “print to the screen,” not “send to printer.” The pwd command displays the full, absolute path of the current, or working, directory.

How do I find relative path in Linux?

The Linux relative path the path is defined with the current working directory (for a present relative path, we can use the “pwd” command). There is no need to start the relative path with “/”.


2 Answers

I know this question is old, but I don't see any answers that address the core issue: The man page OP referenced is wrong and outdated, for at least two reasons.

One is that POSIX 2008 added/mandated support for the NULL argument option, whereby realpath allocates the string for you. Programs using this feature will be portable to all relevant versions of GNU/Linux, probably most other modern systems, and anything conforming to POSIX 2008.

The second reason the man page is wrong is the admonition against PATH_MAX. This is purely GNU religious ideology against "arbitrary limits". In the real world, not having a pathname length limit would add all sorts of avenues for abuse/DoS, would add lots of failure cases to tasks that otherwise could not fail, and would break more interfaces than just realpath.

If you care about maximum portability, it's probably best to use a mix of both methods. See the POSIX documentation for details:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html

I would use a fixed-size, caller-provided buffer if PATH_MAX is defined, and otherwise pass NULL. This seems to cover all cases, but you might also want to check older versions of POSIX to see if they have any guidelines for what to do if PATH_MAX is not defined.

like image 58
R.. GitHub STOP HELPING ICE Avatar answered Sep 25 '22 14:09

R.. GitHub STOP HELPING ICE


Use getcwd() and readlink() which allows to give a buffer size to reimplement realpath(). Note that you have to resolve symbolic links, "." and ".." from left to right to do it correctly.

like image 20
AProgrammer Avatar answered Sep 23 '22 14:09

AProgrammer