Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dirent.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- void check_file(const char *path) {
- FILE *file = fopen(path, "rb");
- if(!file)
- return;
- fseek(file, 0, SEEK_END);
- int size = ftell(file);
- rewind(file);
- unsigned char *buffer = (char*)malloc (sizeof(char)*size);
- if(!buffer)
- goto error;
- int result = fread(buffer, 1, size, file);
- if(result != size)
- goto error;
- int bad = buffer[14] || buffer[15];
- if(bad) {
- printf("Fixing bad header: %s\n", path);
- for(int i=0; i<15; i++)
- printf("%.2X ", buffer[i]);
- putchar('\n');
- fclose(file);
- file = fopen(path, "wb");
- if(!file) {
- puts("Couldn't open the file for writing to fix it though");
- goto error;
- }
- for(int i=7; i<16; i++) // clear out the last 9 bytes of the header
- buffer[i] = 0;
- fwrite(buffer, sizeof(char), size, file);
- }
- error:
- free(buffer);
- if(file)
- fclose(file);
- }
- int is_directory(const char *path) {
- struct stat statbuf;
- if (stat(path, &statbuf) != 0)
- return 0;
- return S_ISDIR(statbuf.st_mode);
- }
- void scan(const char *name) {
- DIR *d;
- struct dirent *dir;
- d = opendir(name);
- if(d) {
- while ((dir = readdir(d)) != NULL) {
- if(dir->d_name[0] == '.')
- continue;
- char full_path[500];
- sprintf(full_path, "%s/%s", name, dir->d_name);
- if(is_directory(full_path)) {
- scan(full_path);
- continue;
- }
- char *extension = strrchr(dir->d_name, '.');
- if(!extension)
- continue;
- if(strcasecmp(extension+1, "nes"))
- continue;
- check_file(full_path);
- }
- closedir(d);
- }
- }
- int main(int argc, char *argv[]) {
- scan(".");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement