closedir.c 659 B

12345678910111213141516171819202122232425262728293031323334353637
  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. #include <not-cancel.h>
  12. int closedir(DIR * dir)
  13. {
  14. int fd;
  15. if (!dir) {
  16. __set_errno(EBADF);
  17. return -1;
  18. }
  19. /* We need to check dd_fd. */
  20. if (dir->dd_fd == -1) {
  21. __set_errno(EBADF);
  22. return -1;
  23. }
  24. __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  25. fd = dir->dd_fd;
  26. dir->dd_fd = -1;
  27. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  28. free(dir->dd_buf);
  29. free(dir);
  30. return close_not_cancel(fd);
  31. }
  32. libc_hidden_def(closedir)