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