Search code examples
c++clinuxsolaris-10

Solaris 10: Alternative to dirfd()


I had worked on RHEL 6.5 and developed some code which would use the function dirfd() for readdir_r(), like shown below:

    #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
                            }
                        }
                  }
    }

This piece of codes work fine in RHEL 6.5 (Linux) but when I run this in Oracle Solaris 10, it fails with an error Undefined symbol dirfd.

I have searched this fucntion in /usr/include/dirent.h but it is not there. The same is available in dirent.h version of Linux.

I have read somewhere that dirfd() is not available in Solaris 9 and 10.

So, is there any equivalent workaround of this function in Solaris 10?


Solution

  • This late BSD function was standardized in 2008 while Solaris 9 was released in 2001 and Solaris 10 in 2005. That's the reason why it isn't available with these versions.

    dirfd is available with the current version, Solaris 11.

    For older ones, reimplementing dirfd seems to be obvious, given the fact the file descriptor is already in the passed structure, here dir->d_fd or dir->dd_fd depending on whether __XOPEN_OR_POSIX is defined or not.