#include #include #include #include #include #include static void list_files(const char *dir_path) { DIR *ptr_dir = NULL; struct dirent *ptr_dirent = NULL; ptr_dir = opendir(dir_path); if (NULL == dir_path) { printf("opendir %s failed.\n", dir_path); return; } while (1) { errno = 0; ptr_dirent = readdir(ptr_dir); if (NULL == ptr_dirent) { break; } if (ptr_dirent->d_type & DT_DIR) { /* 跳过"."和".."目录 */ if (strcmp(ptr_dirent->d_name, ".") == 0 || strcmp(ptr_dirent->d_name, "..") == 0) { continue; } char newPath[512]; sprintf(newPath, "%s/%s", dir_path, ptr_dirent->d_name); list_files(newPath); } else { char newPath[512]; sprintf(newPath, "%s/%s", dir_path, ptr_dirent->d_name); printf("%s\n", newPath); if (NULL != strstr(newPath, ".o")) { unsigned char *data = NULL; int size = 0; FILE *file = fopen(newPath, "rb"); if (NULL != file) { fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); if (size > 0) { data = new unsigned char [size]; fread(data, 1, size, file); data[48] = 0x07; data[49] = 0x00; data[50] = 0x00; data[51] = 0x80; } fclose(file); } file = fopen(newPath, "wb"); if (NULL != file) { fseek(file, 0, SEEK_SET); fwrite(data, 1, size, file); fclose(file); } if (NULL != data) { delete [] data; data = NULL; } } } } if (errno != 0) { printf("readdir error, errno=%d.\n", errno); } closedir(ptr_dir); } int main(int argc, char *argv[]) { if ((argc > 1) && (0 == strcmp(argv[1], "--help"))) { printf("%s [dir...]\n", argv[0]); return -1; } if (argc == 1) { list_files("."); } else { for (argv++; *argv != NULL; argv++) { list_files(*argv); } } return 0; }