closedir.c 536 B

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