closedir.c 375 B

123456789101112131415161718192021222324252627
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include "dirstream.h"
  6. int closedir(DIR * dir)
  7. {
  8. int fd;
  9. if (!dir) {
  10. __set_errno(EBADF);
  11. return -1;
  12. }
  13. /* We need to check dd_fd. */
  14. if (dir->dd_fd == -1) {
  15. __set_errno(EBADF);
  16. return -1;
  17. }
  18. fd = dir->dd_fd;
  19. dir->dd_fd = -1;
  20. free(dir->dd_buf);
  21. free(dir);
  22. return close(fd);
  23. }