From ce15b42a3385cb50dab7f8a58bf310ba5a57958e Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Wed, 22 Nov 2006 22:33:10 -0800 Subject: [PATCH 1/1] Initial commit --- Makefile | 6 +++++ scandir.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 Makefile create mode 100644 scandir.cc diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d2221fb --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +CXXFLAGS=-O -Wall + +OBJS=scandir.o + +scandir : $(OBJS) + $(CXX) $(LDFLAGS) -o $@ $^ diff --git a/scandir.cc b/scandir.cc new file mode 100644 index 0000000..7e02f27 --- /dev/null +++ b/scandir.cc @@ -0,0 +1,75 @@ +/* Recursively descend the filesystem and visit each file. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using std::string; + +void scandir(const string& path); + +void scanfile(const string& path) +{ + struct stat stat_buf; + lstat(path.c_str(), &stat_buf); + + printf("%s:\n", path.c_str()); + printf(" ino=%Ld, perm=%04o, uid=%d, gid=%d, nlink=%d, blksize=%d, size=%Ld\n", + (int64_t)stat_buf.st_ino, stat_buf.st_mode & 07777, + stat_buf.st_uid, stat_buf.st_gid, stat_buf.st_nlink, + (int)stat_buf.st_blksize, (int64_t)stat_buf.st_size); + + switch (stat_buf.st_mode & S_IFMT) { + case S_IFIFO: + case S_IFSOCK: + case S_IFCHR: + case S_IFBLK: + case S_IFLNK: + printf(" special file\n"); + break; + case S_IFREG: + printf(" regular file\n"); + break; + case S_IFDIR: + printf(" directory\n"); + scandir(path); + break; + } +} + +void scandir(const string& path) +{ + printf("Scan directory: %s\n", path.c_str()); + + DIR *dir = opendir(path.c_str()); + + if (dir == NULL) { + printf("Error: %m\n"); + return; + } + + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + string filename(ent->d_name); + if (filename == "." || filename == "..") + continue; + printf(" d_name = '%s'\n", filename.c_str()); + scanfile(path + "/" + filename); + } + + closedir(dir); +} + +int main(int argc, char *argv[]) +{ + scandir("."); + + return 0; +} -- 2.20.1