c++ - 索拉里斯 10 : Alternative to dirfd()

标签 c++ c linux solaris-10

我曾在 RHEL 6.5 上工作并开发了一些使用函数 dirfd() 的代码对于 readdir_r() ,如下图所示:

    #include <dirent.h>
    #include <sys/types.h>

    void some_function(){
            DIR *dir = NULL;
            struct dirent *pentry = NULL, *next_file = NULL;
                if ((dir = opendir("/ZB_RQ/")) != NULL) {
                        len_pentry = offsetof(struct dirent, d_name) + fpathconf(dirfd(dir), _PC_NAME_MAX) + 1;
                        pentry = malloc(len_pentry);
                        if(!pentry){
                            exit(0);
                        }

                        for(;;){
                            readdir_r(dir, pentry, &next_file);
                            if(!next_file){
                                //No file to iterate.
                                break;
                            }
                            else{
                                // do something
                            }
                        }
                  }
    }

这段代码在 RHEL 6.5 (Linux) 中运行良好,但是当我在 Oracle Solaris 10 中运行它时, 它失败并出现错误 Undefined symbol dirfd .

我在/usr/include/dirent.h 搜索过这个函数但它不在那里。同样可在 dirent.h 中找到Linux 版本。

我在某处读到 dirfd()在 Solaris 9 和 10 中不可用。

那么,在 Solaris 10 中是否有此功能的等效解决方法?

最佳答案

这个晚期 BSD 功能在 2008 年标准化,而 Solaris 9 于 2001 年发布,Solaris 10 于 2005 年发布。这就是为什么这些版本不提供它的原因。

dirfd 适用于当前版本 Solaris 11。

对于旧的,重新实现 dirfd 似乎是显而易见的,因为文件描述符已经在传递的结构中,这里是 dir->d_fd dir->dd_fd 取决于是否定义了 __XOPEN_OR_POSIX

关于c++ - 索拉里斯 10 : Alternative to dirfd(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28022507/

相关文章:

c++ - 从 VS08/10 编译 C++,无需运行时库/MFC

main() 函数可以返回 double 吗?

c++ - 专门针对模板类的 C++ 类成员函数

c++ - 返回没有类型参数的 std::make_unique

c - C 文件的问题

c - 如何将 char var[] 附加到 char ? C

linux - 将连续流通过管道传输到多个文件

c - 将共享库注入(inject)流程

linux - 使用数字顺序对制表符分隔文件中的行进行排序

c++ - 如何在两个不同的命名空间(但只写一次)中使用不同的命名空间实现来定义相同的头文件?