// Output the name of each file in the current directory where: // The file has public read bit set // The file is a regular file // The file is not empty // The file does not contain the byte 0xFE // No need to scan sub directories // Assume only regular files and directories #include #include #include #include #include #include // Get used to using command line man for exam // Do incrementally // Useful to check return values even if question does not ask you to // Easier to debug - catch bugs as you go #define SEARCH_BYTE 0xFE int contains_byte(char *path) { FILE *stream = fopen(path, "r"); if(stream == NULL) { perror(NULL); return 0; } int value; while ((value = fgetc(stream)) != EOF) { if (value == SEARCH_BYTE) { fclose(stream); return 1; } } fclose(stream); return 0; } int main(void) { // Open current directory DIR *dirp = opendir("."); if (dirp == NULL) { perror("opendir . "); return 1; } struct dirent *entry; while ((entry = readdir(dirp)) != NULL){ struct stat s; if (stat(entry->d_name, &s) != 0){ continue; } // The file is a regular file if (!S_ISREG(s.st_mode)){ continue; } // The file has public read bit set if (!(s.st_mode & S_IROTH)) { continue; } // The file is not empty if (s.st_size == 0){ continue; } // The file does not contain the byte 0xFE if (contains_byte(entry->d_name)) { continue; } printf("%s\n", entry->d_name); } closedir(dirp); return 0; }