/* Illustration of how to read directory entries from a given directory See man pages: (5) dir [documents dirent] (3) opendir, readdir Some example file types: DT_DIR : directory type DT_REG : regular file DT_LNK : link file */ #include #include #include int main(int argc, char *argv[ ]) { char *desiredDirectory = "."; /* current working directory */ DIR *d; /* the directory stream */ struct dirent *entry; /* holds one entry of the directory */ /* attempt to open a directory stream for the desired directory */ if ((d = opendir(desiredDirectory)) != NULL) { /* Success: now display the names of all files and directories */ while ((entry = readdir(d)) != NULL) { printf ("%s\n", entry->d_name); } closedir(d); } else { /* Failure: could not open the directory; issue error message and quit*/ perror (argv[0]); return EXIT_FAILURE; } return EXIT_SUCCESS; }