libfuse

readdir_inode.c
1 /*
2 * Prints each directory entry and its inode as returned by 'readdir'.
3 * Skips '.' and '..' because readdir is not required to return them and
4 * some of our examples don't.
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <errno.h>
12
13 int main(int argc, char* argv[])
14 {
15 DIR* dirp;
16 struct dirent* dent;
17
18 if (argc != 2) {
19 fprintf(stderr, "Usage: readdir_inode dir\n");
20 return 1;
21 }
22
23 dirp = opendir(argv[1]);
24 if (dirp == NULL) {
25 perror("failed to open directory");
26 return 2;
27 }
28
29 errno = 0;
30 dent = readdir(dirp);
31 while (dent != NULL) {
32 if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) {
33 printf("%llu %s\n", (unsigned long long)dent->d_ino, dent->d_name);
34 }
35 dent = readdir(dirp);
36 }
37 if (errno != 0) {
38 perror("failed to read directory entry");
39 return 3;
40 }
41
42 closedir(dirp);
43
44 return 0;
45 }