Getting path from file descriptor or FILE *?

Is there a way to get the path information from a file descriptor or FILE pointer?

I know that this data may not always be relevant / useful for the case when the file descriptor is a socket, pipe, etc.

+3
source share
3 answers

I don’t believe that there is any portable way, but, for example, on Linux you can invoke readlinkon "/proc/self/fd/fileno ", and the kernel will give you a path if it can, or one of the different types of funny strings if not.

+4
source

If you are fortunate enough to use Mac OS X, you can use the following code:

#define _DARWIN_C_SOURCE
#include <sys/fcntl.h>
.
.
.
char pathbuf[PATH_MAX];
if (fcntl(fd, F_GETPATH, pathbuf) >= 0) {
    // pathbuf now contains *a* path to the open file descriptor
}

, , , , ... , , , .

+3

There can be one or more names for a file, so there is no std path. I am not sure if there can be anything specific to the OS.

0
source

Source: https://habr.com/ru/post/1717370/


All Articles