3 #include <dirent.h> /* directory */
4 #include <stdlib.h> /* C standard library */
10 * Input: the directory name and a computation function
11 * Return: 0 if OK, -1 on errors
13 int DirScan(char * dirname, int(*compute)(struct dirent *))
16 struct dirent *direntry;
18 if ( (dir = opendir(dirname)) == NULL) { /* open directory */
19 printf("Opening %s\n", dirname); /* on error print messages */
20 perror("Cannot open directory"); /* and then return */
23 fd = dirfd(dir); /* get file descriptor */
24 fchdir(fd); /* change directory */
25 /* loop on directory entries */
26 while ( (direntry = readdir(dir)) != NULL) { /* read entry */
27 if (compute(direntry)) { /* execute function on it */
28 return -1; /* on error return */