closedir.c 531 B

123456789101112131415161718192021222324252627282930313233
  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. #ifdef __UCLIBC_HAS_THREADS__
  19. __pthread_mutex_lock(&(dir->dd_lock));
  20. #endif
  21. fd = dir->dd_fd;
  22. dir->dd_fd = -1;
  23. #ifdef __UCLIBC_HAS_THREADS__
  24. __pthread_mutex_unlock(&(dir->dd_lock));
  25. #endif
  26. free(dir->dd_buf);
  27. free(dir);
  28. return close(fd);
  29. }