Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #define MAX_DEPTH 4
- void print_regular_files(const char* dir_path, int depth, off_t max_size) {
- DIR* dir = opendir(dir_path);
- if (dir == NULL) {
- return;
- }
- struct dirent* entry;
- while ((entry = readdir(dir)) != NULL) {
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
- continue;
- }
- char path[PATH_MAX];
- snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);
- struct stat st;
- if (lstat(path, &st) == -1) {
- continue;
- }
- if (S_ISREG(st.st_mode) && access(path, R_OK) != -1 && st.st_size <= max_size) {
- printf("%s\n", entry->d_name);
- } else if (S_ISDIR(st.st_mode) && depth < MAX_DEPTH) {
- print_regular_files(path, depth + 1, max_size);
- }
- }
- closedir(dir);
- }
- int main(int argc, char* argv[]) {
- if (argc < 3) {
- printf("Usage: ./yourprog directory max_size\n");
- return 0;
- }
- const char* dir_path = argv[1];
- off_t max_size = atoll(argv[2]);
- print_regular_files(dir_path, 0, max_size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement