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 
9 
10 int
main(void)11 main (void)
12 {
13   DIR *dirp;
14   struct dirent* ent;
15 
16   /* open a dir stream */
17   dirp = opendir ("/tmp");
18   if (dirp == NULL)
19     {
20       if (errno == ENOENT)
21 	exit (0);
22 
23       perror ("opendir");
24       exit (1);
25     }
26 
27   /* close the directory file descriptor, making it invalid */
28   if (close (dirfd (dirp)) != 0)
29     {
30       puts ("could not close directory file descriptor");
31       /* This is not an error.  It is not guaranteed this is possible.  */
32       return 0;
33     }
34 
35   ent = readdir (dirp);
36 
37   return ent != NULL || errno != EBADF;
38 }
39