closedir.c 355 B

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