bug-readdir1.c 669 B

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