#include #include #include #include #include #include int contains_lowercase(char *path); int traverse(char *directory_path); // Try with // dir_search . // dir_search examples int main(int argc, char *argv[]) { if (argc <= 1) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } int count = traverse(argv[1]); printf("Total count %d\n", count); return 0; } // Count the number of files in the given directory that: // Are regular files // publicly readable // contains at least one byte that could represent a lower case letter // You do not need to recursively search sub directories // Assume there are no symbolic links and just directories and regular files int traverse(char *directory_path) { int count = 0; DIR *dirp = opendir(directory_path); if (dirp == NULL) { perror(""); exit(1); } char fullpath[PATH_MAX]; struct dirent *entry; while ((entry = readdir(dirp)) != NULL){ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 ) { continue; } snprintf(fullpath, PATH_MAX, "%s/%s", directory_path, entry->d_name); struct stat s; if (stat(fullpath, &s) == 0){ if (S_ISDIR(s.st_mode)) { count += traverse(fullpath); } else if (S_ISREG(s.st_mode) && (s.st_mode & S_IROTH) && contains_lowercase(fullpath) != -1){ printf("%s\n", fullpath); count++; } } } closedir(dirp); return count; } int contains_lowercase(char *path) { FILE *f = fopen(path, "r"); if (f == NULL) { perror(""); return -1; } int curr_byte; while ((curr_byte = fgetc(f)) != EOF){ if (curr_byte >= 'a' && curr_byte <= 'z') { fclose(f); return curr_byte; } } fclose(f); return -1; }