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