1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-06-11 05:29:33 +00:00

Change to option_open_file(), allow any file mode

This commit is contained in:
Peter Evans 2018-03-09 16:44:20 -06:00
parent 1235197418
commit 267171ea4a
3 changed files with 12 additions and 12 deletions

View File

@ -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 *);

View File

@ -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;
}

View File

@ -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);