#define _XOPEN_SOURCE 500
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <ftw.h>
int showfile(const char *file, const struct stat *stat, int type, struct FTW *ftwbuf) { switch(type) { case FTW_F: switch (stat->st_mode & S_IFMT) { case S_IFREG: printf("%s -- regular file\n", file); break; case S_IFCHR: printf("%s -- character special file\n", file); break; case S_IFBLK: printf("%s -- block special file\n", file); break; case S_IFIFO: printf("%s -- pipe or FIFO\n", file); break;
/* 当为符号链接时,这里判断不出来,不甚明白 */ case S_IFLNK: printf("%s -- symbolic link\n", file); break; case S_IFSOCK: printf("%s -- socket\n", file); break; case S_IFDIR: printf("for S_IFDIR for %s: %m\n", file); break; } break; case FTW_D: printf("%s -- directory file\n", file); break; case FTW_DNR: printf("%s -- can't read directory\n", file); break; case FTW_NS: printf("%s -- stat error: %m\n", file); break;
/* 当为符号链接时,用这个条件才能判断出来 */ case FTW_SL: printf("%s -- symbolic link by FTW_PHYS\n", file); break; case FTW_SLN: printf("%s -- symbolic link pointing to a nonexistent file\n", file); break; default: printf("unknown type %d for file: %s\n", type, file); } return 0; }
/* Simple example of usage of ftw function */ int main(int argc, char *argv[]) { assert(argc == 2);
if (nftw(argv[1], showfile, 512, FTW_PHYS) == -1) perror("ftw error");
exit(0); }
|