在Mac OS X上实现faccessat(),如何从给定的目录文件描述符和路径名构造新路径?

在Mac OS X上实现faccessat(),如何从给定的目录文件描述符和路径名构造新路径?,c,macos,posix,C,Macos,Posix,我希望实现POSIX 2008功能 int faccessat(int dirfd, const char *pathname, int mode, int flags) 在Mac OS X上。根据功能的Linux手册页: If the pathname given in pathname is relative, then it is interpreted relative to the directory referred to by the file descriptor

我希望实现POSIX 2008功能

int faccessat(int dirfd, const char *pathname, int mode, int flags)
在Mac OS X上。根据功能的Linux手册页:

   If the pathname given in pathname is relative, then it is interpreted relative
   to the directory referred to by the file descriptor dirfd (rather than
   relative to the current working directory of the calling process, as is done
   by access(2) for a relative pathname).

如何从给定的目录文件描述符和路径名构造新路径?

在Mac OS X上,如果可用,可以使用
dirpath
的大小必须大于或等于MAXPATHLEN。如果提供的路径是相对的,则可以将
“/”
和提供的路径附加到目录路径。但是,这可能会产生一个长度超过
MAXPATHLEN
的字符串


另一种方法是使用将工作目录更改为该目录,然后直接使用相对路径。当然,这将更改进程的当前目录。如有必要,您可以先打开原始当前目录
”,然后在恢复原始当前目录后,再次使用
fchdir()
和保存的文件描述符来保存它。但是,如果您的程序是多线程的,而其他线程依赖于当前目录,则这可能仍然是一个问题。

MacOSX是否有类似Linux/proc的功能?在Linux上,您可以使用
/proc/self/fd/%d/%s
@R在旧内核上“模拟”所有
*at()
函数。。不幸的是,Mac OS X没有实现procfsOption 1似乎很好。我可以手动检查新路径的总长度是否大于MAXPATHLEN,如果大于,则将errno设置为ENAMETOOLONG并返回-1。