closedir.c 624 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <dirent.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include "dirstream.h"
  11. int closedir(DIR * dir)
  12. {
  13. int fd;
  14. if (!dir) {
  15. __set_errno(EBADF);
  16. return -1;
  17. }
  18. /* We need to check dd_fd. */
  19. if (dir->dd_fd == -1) {
  20. __set_errno(EBADF);
  21. return -1;
  22. }
  23. __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  24. fd = dir->dd_fd;
  25. dir->dd_fd = -1;
  26. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  27. free(dir->dd_buf);
  28. free(dir);
  29. return close(fd);
  30. }
  31. libc_hidden_def(closedir)