closedir.c 689 B

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