From 267171ea4acd586bbd32815640ede233ef2ab99f Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Fri, 9 Mar 2018 16:44:20 -0600 Subject: [PATCH] Change to option_open_file(), allow any file mode --- include/option.h | 2 +- src/option.c | 16 ++++++++-------- tests/option.c | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/option.h b/include/option.h index e51cd63..31f2b27 100644 --- a/include/option.h +++ b/include/option.h @@ -18,7 +18,7 @@ extern bool option_flag(int); extern FILE *option_get_input(int); extern const char *option_get_error(); extern int option_parse(int, char **); -extern int option_read_file(FILE **, const char *); +extern int option_open_file(FILE **, const char *, const char *); extern int option_set_size(const char *); extern void option_print_help(); extern void option_set_error(const char *); diff --git a/src/option.c b/src/option.c index 77f7c27..30a6d57 100644 --- a/src/option.c +++ b/src/option.c @@ -123,7 +123,7 @@ option_parse(int argc, char **argv) switch (opt) { case DISASSEMBLE: flags |= OPTION_DISASSEMBLE; - if (!option_read_file(&disasm_log, optarg)) { + if (!option_open_file(&disasm_log, optarg, "w")) { return 0; } @@ -131,7 +131,7 @@ option_parse(int argc, char **argv) break; case DISK1: - if (!option_read_file(&input1, optarg)) { + if (!option_open_file(&input1, optarg, "r+")) { return 0; } @@ -139,7 +139,7 @@ option_parse(int argc, char **argv) break; case DISK2: - if (!option_read_file(&input2, optarg)) { + if (!option_open_file(&input2, optarg, "r+")) { return 0; } @@ -174,21 +174,21 @@ option_parse(int argc, char **argv) * 0 if not. */ int -option_read_file(FILE **stream, const char *file) +option_open_file(FILE **stream, const char *file, const char *opts) { if (!file) { snprintf(error_buffer, ERRBUF_SIZE, - "No file given for --diskN\n"); + "No file given\n"); return 0; } - *stream = fopen(file, "r+"); + *stream = fopen(file, opts); if (*stream == NULL) { snprintf(error_buffer, ERRBUF_SIZE, - "--diskN: %s", - strerror(errno)); + "open file for %s: %s", + file, strerror(errno)); return 0; } diff --git a/tests/option.c b/tests/option.c index c66451e..068e5d8 100644 --- a/tests/option.c +++ b/tests/option.c @@ -41,8 +41,8 @@ Test(option, read_file) FILE *stream_b; // Maybe we should use sterror(ENOENT)? - cr_assert_eq(option_read_file(&stream_a, bad_file), 0); - cr_assert_str_eq(option_get_error(), "--diskN: No such file or directory"); + cr_assert_eq(option_open_file(&stream_a, bad_file, "r"), 0); + cr_assert_str_eq(option_get_error(), "open file for /tmp/BLEH: No such file or directory"); option_set_error(""); @@ -51,7 +51,7 @@ Test(option, read_file) fwrite(str, sizeof(char), strlen(str), stream_a); fclose(stream_a); - option_read_file(&stream_b, file); + option_open_file(&stream_b, file, "r"); fread(buf, sizeof(char), 255, stream_b); cr_assert_str_eq(buf, str); fclose(stream_b);