/* Simple recurive open test * * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public Licence * as published by the Free Software Foundation; either version * 2 of the Licence, or (at your option) any later version. */ #define _GNU_SOURCE #include #include #include #include #include #include void process(int dirfd, const char *filename); void process_dir(int dirfd, const char *filename) { struct dirent *de; DIR *dir; int fd; dir = fdopendir(dirfd); if (dir) { while (de = readdir(dir)) { if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || de->d_name[1] == '.' && de->d_name[2] == '\0')) continue; process(dirfd, de->d_name); } closedir(dir); } } void process(int dirfd, const char *filename) { struct stat st; int fd; if (fstatat(dirfd, filename, &st, AT_SYMLINK_NOFOLLOW) >= 0) { if (S_ISREG(st.st_mode)) { fd = openat(dirfd, filename, O_RDONLY); if (fd >= 0) close(fd); } else if (S_ISDIR(st.st_mode)) { fd = openat(dirfd, filename, O_RDONLY | O_DIRECTORY); if (fd >= 0) process_dir(fd, filename); } } } int main(int argc, char **argv) { for (argv++; *argv; argv++) process(AT_FDCWD, *argv); return 0; }