我希望实现 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 上,您可以使用 fcntl(dirfd, F_GETPATH, dirpath)
获取目录路径(如果可用) ; dirpath
的大小必须为 MAXPATHLEN
或更大。如果提供的路径是相对路径,则可以将 "/"
和提供的路径附加到目录路径。但是,这可能会产生比 MAXPATHLEN
更长的字符串。
另一种方法是使用 fchdir()
将工作目录更改为该目录,然后直接使用相对路径。当然,这会更改进程的当前目录。如果需要,您可以先打开原始当前目录 "."
来保存它,然后在恢复原始目录后再次使用保存的文件描述符使用 fchdir()
当前目录。但是,如果您的程序是多线程的并且其他线程依赖于当前目录,那么这可能仍然是一个问题。
关于c - 在 Mac OS X 上实现 faccessat(),如何根据给定的目录文件描述符和路径名构造新路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9540909/