From: Michael Gernoth Date: Wed, 4 Feb 2009 22:55:49 +0000 (+0100) Subject: use unified write_file function everywhere... X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/rsbs2/commitdiff_plain/3de486aeb82f17284ba67e8a3ab40db20813b5da?hp=be101700bb10bac9a432c153490e9db9031a5d61 use unified write_file function everywhere... --- diff --git a/filesystem.c b/filesystem.c index 175cbc4..d4b207e 100644 --- a/filesystem.c +++ b/filesystem.c @@ -86,11 +86,11 @@ void extract_files(unsigned char *fw, int len) fent = get_next_file(fw, len); while (fent) { - printf("%s: unknown: 0x%02x, length: %d", + printf("%s: unknown: 0x%02x, length: %d, ", fent->name, fent->unknown, fent->length); if (fent->length > 0) { - write_file(fent->name, fent->start, fent->length); + write_file(extracted_file(fent->name), fent->start, fent->length); if (*((unsigned int*)fent->start) == LZ_MAGIC) { char *lzname; @@ -105,7 +105,7 @@ void extract_files(unsigned char *fw, int len) } lzname[strlen(lzname) - 3] = 0x00; - printf("%s: packed file found", lzname); + printf("%s: packed file found, ", lzname); extract_lz_file(fent->start, (unsigned char*)lzname, 0); free(lzname); @@ -120,7 +120,7 @@ void extract_files(unsigned char *fw, int len) memcpy(lzname + strlen(lzname), lzpos - 4, 4); lzpos += 4; if (*((unsigned int*)(lzpos)) == LZ_MAGIC) { - printf("%s: compressed firmware part found", lzname); + printf("%s: compressed firmware part found, ", lzname); extract_lz_file(lzpos, (unsigned char*)lzname, 1); } } @@ -174,16 +174,12 @@ void mkdir_p(char *dir) void write_file(char *fname, unsigned char *buf, int len) { - char filename[PATH_MAX]; char *filename_c, *dirn; int fd; int remaining; int ret; - strcpy(filename, "extracted/"); - strcat(filename, fname); - - if ((filename_c = strdup(filename)) == NULL) { + if ((filename_c = strdup(fname)) == NULL) { perror("strdup"); exit(1); } @@ -191,8 +187,8 @@ void write_file(char *fname, unsigned char *buf, int len) mkdir_p(dirn); free(filename_c); - if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) { - fprintf(stderr, "%s: ", filename); + if ((fd = open(fname, O_WRONLY|O_CREAT, 0644)) == -1) { + fprintf(stderr, "%s: ", fname); perror("open"); exit(1); } @@ -208,7 +204,17 @@ void write_file(char *fname, unsigned char *buf, int len) remaining -= ret; } - printf(", %s written.\n", filename); + printf("%s written.\n", fname); close(fd); } + +char *extracted_file(char *fname) +{ + static char filename[PATH_MAX]; + + strcpy(filename, "extracted/"); + strcat(filename, fname); + + return filename; +} diff --git a/filesystem.h b/filesystem.h index d4b91c7..bf9a4c8 100644 --- a/filesystem.h +++ b/filesystem.h @@ -10,3 +10,4 @@ void extract_files(unsigned char *fw, int len); void replace_add_file(unsigned char *fw, int len, char *fwname, char *lname); void list_files(unsigned char *fw, int len); void write_file(char *fname, unsigned char *buf, int len); +char *extracted_file(char *fname); diff --git a/firmware.c b/firmware.c index 80e12e5..b575012 100644 --- a/firmware.c +++ b/firmware.c @@ -519,23 +519,8 @@ int main(int argc, char **argv) strcpy(newfile, file); strcat(newfile, ".patched"); - printf("Writing %s\n", newfile); - if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) { - fprintf(stderr,"%s: ", newfile); - perror("open"); - exit(1); - } - - remaining = statbuf.st_size; - - while(remaining) { - if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) { - perror("write"); - exit(1); - } - remaining -= ret; - } - close(fd); + printf("Firmware "); + write_file(newfile, fw, statbuf.st_size); } else { fprintf(stderr,"Can't set correct checksum, aborting...\n"); } diff --git a/rsb-lz.c b/rsb-lz.c index 2e50252..c771b89 100644 --- a/rsb-lz.c +++ b/rsb-lz.c @@ -312,8 +312,7 @@ void extract_lz_file(unsigned char *inbuf, unsigned char *name, unsigned char ch err_exit(__func__); } } - - write_file((char*)name, outbuf, len); + write_file(extracted_file((char*)name), outbuf, len); free(outbuf); }