From a17c7e61ba8953301c28e94890e459e76f532687 Mon Sep 17 00:00:00 2001 From: jakob Date: Thu, 19 Jan 2017 18:54:37 -0500 Subject: Implemented the ability to specify an output path,. --- src/write.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/write.c') diff --git a/src/write.c b/src/write.c index b9efafb..5d9cbad 100644 --- a/src/write.c +++ b/src/write.c @@ -90,6 +90,18 @@ void write_files(file_node *file_root, elif_node *elif_root, FILE *archive) { if (file_name == NULL) continue; + /* strcpy is used rather than strcat to prevent the file + name from being appended after garbage data. */ + size_t file_name_size = strlen(file_name); + size_t path_size = strlen(arguments.output); + char *output_path = malloc(path_size + file_name_size + 1); + if (output_path == NULL) { + fprintf(stderr, "Insufficient memory.\n"); + return; + } + strcpy(output_path, arguments.output); + strcpy(output_path + path_size, file_name); + if (!arguments.quiet) { printf("Inflating %s (%" PRIu64 " bytes)\n", file_name, current->file_size); @@ -127,14 +139,15 @@ void write_files(file_node *file_root, elif_node *elif_root, FILE *archive) { encryption_key, current->key); } - make_paths(file_name); - FILE *output = fopen(file_name, "wb+"); + make_paths(output_path); + FILE *output = fopen(output_path, "wb+"); if (output == NULL) { - perror(file_name); + perror(output_path); break; } fwrite(out_start, current->file_size, 1, output); fclose(output); + free(output_path); free(file_name); free(out_start); } -- cgit v1.3