bug-readdir1.c 688 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. int
  9. main (void)
  10. {
  11. DIR *dirp;
  12. struct dirent* ent;
  13. /* open a dir stream */
  14. dirp = opendir ("/tmp");
  15. if (dirp == NULL)
  16. {
  17. if (errno == ENOENT)
  18. exit (0);
  19. perror ("opendir");
  20. exit (1);
  21. }
  22. /* close the directory file descriptor, making it invalid */
  23. if (close (dirfd (dirp)) != 0)
  24. {
  25. puts ("could not close directory file descriptor");
  26. /* This is not an error. It is not guaranteed this is possible. */
  27. return 0;
  28. }
  29. ent = readdir (dirp);
  30. return ent != NULL || errno != EBADF;
  31. }