Skip to content

*nix

realpath

1
2
3
4
5
#include <limits.h>
#include <stdlib.h>

char path[PATH_MAX];
char *p = realpath("../path/to/files", path);

Retrieve filename from file descriptor

  • Use readlink on /proc/self/fd/XXX (Linux)
1
2
3
4
5
char fdpath[PATH_MAX];
snprintf(fdpath, sizeof(fdpath), "/proc/self/fd/%d", fd);
int siz = readlink(fdpath, buf, buf_size);
if(siz != -1)
    buf[siz == PATH_MAX ? PATH_MAX-1 : siz] = '\0';
  • Use fcntl
1
2
3
4
5
6
7
8
#include <sys/syslimits.h>
#include <fcntl.h>

char filePath[PATH_MAX];
if (fcntl(fd, F_GETPATH, filePath) != -1)
{
    // do something with the file path
}