Back to index

glibc  2.9
bug-readdir1.c
Go to the documentation of this file.
00001 #include <dirent.h>
00002 #include <errno.h>
00003 #include <errno.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <unistd.h>
00007 #include <sys/types.h>
00008 
00009 
00010 int
00011 main (void)
00012 {
00013   DIR *dirp;
00014   struct dirent* ent;
00015 
00016   /* open a dir stream */
00017   dirp = opendir ("/tmp");
00018   if (dirp == NULL)
00019     {
00020       if (errno == ENOENT)
00021        exit (0);
00022 
00023       perror ("opendir");
00024       exit (1);
00025     }
00026 
00027   /* close the directory file descriptor, making it invalid */
00028   if (close (dirfd (dirp)) != 0)
00029     {
00030       puts ("could not close directory file descriptor");
00031       /* This is not an error.  It is not guaranteed this is possible.  */
00032       return 0;
00033     }
00034 
00035   ent = readdir (dirp);
00036 
00037   return ent != NULL || errno != EBADF;
00038 }