diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index f7d46cd655..59f00800f3 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -442,7 +442,8 @@ int main(int argc, char **argv) { /* Loop over all records of all input files */ int previous_index = -1; - while (opt_input_loop(o, optargs_file_format_text)) { + bool gotError = false; + while (opt_input_loop(o, optargs_file_format_text, &gotError)) { int err; char *bufptr = fgets(buf, BUFFER_SIZE - 1, o->input); if (opt_eof(o)) { @@ -547,7 +548,7 @@ int main(int argc, char **argv) { fclose(fout); free(o); free(buf); - return 0; + return gotError ? 1 : 0; } /* return a pointer to the n'th column of buf */ diff --git a/src/apps/optargpm.h b/src/apps/optargpm.h index 8d0f1ea536..2835819c62 100644 --- a/src/apps/optargpm.h +++ b/src/apps/optargpm.h @@ -75,7 +75,7 @@ handle open/close of a sequence of input files: enum OPTARGS_FILE_MODE: indicates whether to read operands in text (0) or binary (1) mode -opt_input_loop (o, mode): +opt_input_loop (o, mode, &gotError): When used as condition in a while loop, traverses all operands, giving the impression of reading just a single input file. @@ -129,7 +129,8 @@ to "o" internally P = proj_create_argv (0, o->pargc, o->pargv); // Loop over all lines of all input files - while (opt_input_loop (o, optargs_file_format_text)) { + bool gotError = false; + while (opt_input_loop (o, optargs_file_format_text, &gotError)) { char buf[1000]; int ret = fgets (buf, 1000, o->input); if (opt_eof (o)) { @@ -186,6 +187,7 @@ Thomas Knudsen, thokn@sdfe.dk, 2016-05-25/2017-09-10 #include #include #include +#include #include #include #include @@ -201,7 +203,7 @@ enum OPTARGS_FILE_FORMAT { char *opt_filename(OPTARGS *opt); static int opt_eof(OPTARGS *opt); int opt_record(OPTARGS *opt); -int opt_input_loop(OPTARGS *opt, int binary); +int opt_input_loop(OPTARGS *opt, int binary, bool *gotError); static int opt_is_flag(OPTARGS *opt, int ordinal); static int opt_raise_flag(OPTARGS *opt, int ordinal); static int opt_ordinal(OPTARGS *opt, const char *option); @@ -261,7 +263,9 @@ int opt_record(OPTARGS *opt) { } /* handle closing/opening of a "stream-of-streams" */ -int opt_input_loop(OPTARGS *opt, int binary) { +int opt_input_loop(OPTARGS *opt, int binary, bool *gotError) { + if (gotError) + *gotError = false; if (nullptr == opt) return 0; @@ -291,12 +295,16 @@ int opt_input_loop(OPTARGS *opt, int binary) { return 0; /* otherwise, open next input file */ - opt->input = fopen(opt->fargv[opt->input_index++], binary ? "rb" : "rt"); - if (nullptr != opt->input) - return 1; + const char *filename = opt->fargv[opt->input_index++]; + opt->input = fopen(filename, binary ? "rb" : "rt"); + if (nullptr == opt->input) { + fprintf(stderr, "Cannot open file %s\n", filename); + if (gotError) + *gotError = true; + return 0; + } - /* ignore non-existing files - go on! */ - return opt_input_loop(opt, binary); + return 1; } /* return true if option with given ordinal is a flag, false if undefined or diff --git a/test/cli/test_cct.yaml b/test/cli/test_cct.yaml index 52ef796b9d..c932bf47d9 100644 --- a/test/cli/test_cct.yaml +++ b/test/cli/test_cct.yaml @@ -151,3 +151,7 @@ tests: args: +proj=noop input_file1_with_utf8_bom.txt # no BOM with output out: " 0.0000 3.0000 0.0000 inf" +- comment: Test cct with non existing input file + args: +proj=noop i_do_not_exist.txt + stderr: "Cannot open file i_do_not_exist.txt" + exitcode: 1