/* POSIX directory-stream wrappers. */ #include #include #include "aksl_internal.h" akerr_ErrorContext AKERR_NOIGNORE *aksl_opendir(const char *pathname, DIR **dest) { PREPARE_ERROR(e); FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "pathname=%p, dest=%p", (void *)pathname, (void *)dest); *dest = NULL; FAIL_ZERO_RETURN(e, pathname, AKERR_NULLPOINTER, "pathname=%p, dest=%p", (void *)pathname, (void *)dest); errno = 0; *dest = opendir(pathname); FAIL_ZERO_RETURN(e, *dest, AKSL_ERRNO_OR(AKERR_IO), "pathname=%s", pathname); SUCCEED_RETURN(e); } akerr_ErrorContext AKERR_NOIGNORE *aksl_fdopendir(int fd, DIR **dest) { PREPARE_ERROR(e); FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "fd=%d, dest=%p", fd, (void *)dest); *dest = NULL; errno = 0; *dest = fdopendir(fd); FAIL_ZERO_RETURN(e, *dest, AKSL_ERRNO_OR(AKERR_IO), "fd=%d", fd); SUCCEED_RETURN(e); } akerr_ErrorContext AKERR_NOIGNORE *aksl_readdir(DIR *dirp, struct dirent *dest) { struct dirent *entry = NULL; PREPARE_ERROR(e); FAIL_ZERO_RETURN(e, dirp, AKERR_NULLPOINTER, "dirp=%p, dest=%p", (void *)dirp, (void *)dest); FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "dirp=%p, dest=%p", (void *)dirp, (void *)dest); /* readdir uses errno to distinguish failure from end-of-directory. */ errno = 0; entry = readdir(dirp); if ( entry == NULL ) { FAIL_NONZERO_RETURN(e, errno, AKSL_ERRNO_OR(AKERR_IO), "readdir failed"); FAIL_RETURN(e, AKERR_EOF, "end of directory"); } *dest = *entry; SUCCEED_RETURN(e); } akerr_ErrorContext AKERR_NOIGNORE *aksl_closedir(DIR *dirp) { PREPARE_ERROR(e); FAIL_ZERO_RETURN(e, dirp, AKERR_NULLPOINTER, "dirp=%p", (void *)dirp); errno = 0; FAIL_NONZERO_RETURN(e, closedir(dirp), AKSL_ERRNO_OR(AKERR_IO), "closedir failed"); SUCCEED_RETURN(e); } akerr_ErrorContext AKERR_NOIGNORE *aksl_rewinddir(DIR *dirp) { PREPARE_ERROR(e); FAIL_ZERO_RETURN(e, dirp, AKERR_NULLPOINTER, "dirp=%p", (void *)dirp); rewinddir(dirp); SUCCEED_RETURN(e); }