diff options
author | Nicolas Williams <nico@cryptonector.com> | 2015-04-28 17:27:12 -0500 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2015-05-11 21:36:26 -0500 |
commit | e3cb1f76cd3e2e55455757217be15662d3c75236 (patch) | |
tree | 013a8165b6911f91e4237c5deb4aeaa488c0c8c7 | |
parent | ef12efbf8c67e79e21bcbdc2dad516f902da53da (diff) |
Report read errors too (and fix #772)
-rw-r--r-- | jq.h | 2 | ||||
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | util.c | 21 |
3 files changed, 17 insertions, 10 deletions
@@ -43,7 +43,7 @@ jq_util_input_state jq_util_input_init(jq_msg_cb, void *); void jq_util_input_set_parser(jq_util_input_state, jv_parser *, int); void jq_util_input_free(jq_util_input_state *); void jq_util_input_add_input(jq_util_input_state, jv); -int jq_util_input_open_errors(jq_util_input_state); +int jq_util_input_errors(jq_util_input_state); jv jq_util_input_next_input(jq_util_input_state); jv jq_util_input_next_input_cb(jq_state *, void *); @@ -456,7 +456,7 @@ int main(int argc, char* argv[]) { ret = process(jq, jv_null(), jq_flags, dumpopts); } else { jv value; - while (jq_util_input_open_errors(input_state) == 0 && + while (jq_util_input_errors(input_state) == 0 && (jv_is_valid((value = jq_util_input_next_input(input_state))) || jv_invalid_has_msg(jv_copy(value)))) { if (jv_is_valid(value)) { ret = process(jq, value, jq_flags, dumpopts); @@ -477,7 +477,7 @@ int main(int argc, char* argv[]) { } } - if (jq_util_input_open_errors(input_state) != 0) + if (jq_util_input_errors(input_state) != 0) ret = 2; out: @@ -151,7 +151,7 @@ struct jq_util_input_state { jv_parser *parser; FILE* current_input; jv files; - int open_failures; + int failures; jv slurped; char buf[4096]; size_t buf_valid_len; @@ -211,8 +211,8 @@ void jq_util_input_add_input(jq_util_input_state state, jv fname) { state->files = jv_array_append(state->files, fname); } -int jq_util_input_open_errors(jq_util_input_state state) { - return state->open_failures; +int jq_util_input_errors(jq_util_input_state state) { + return state->failures; } static jv next_file(jq_util_input_state state) { @@ -246,7 +246,7 @@ static int jq_util_input_read_more(jq_util_input_state state) { state->current_input = fopen(jv_string_value(f), "r"); if (!state->current_input) { state->err_cb(state->err_cb_data, jv_copy(f)); - state->open_failures++; + state->failures++; } } jv_free(f); @@ -256,9 +256,16 @@ static int jq_util_input_read_more(jq_util_input_state state) { state->buf[0] = 0; state->buf_valid_len = 0; if (state->current_input) { + char *res; memset(state->buf, 0, sizeof(state->buf)); - if (!fgets(state->buf, sizeof(state->buf), state->current_input)) { + + while (!(res = fgets(state->buf, sizeof(state->buf), state->current_input)) && + ferror(state->current_input) && errno == EINTR) + clearerr(state->current_input); + if (res == NULL) { state->buf[0] = 0; + if (ferror(state->current_input)) + state->failures++; } else { const char *p = memchr(state->buf, '\n', sizeof(state->buf)); @@ -276,8 +283,8 @@ static int jq_util_input_read_more(jq_util_input_state state) { * * We can't use getline() because there need not be any newlines * in the input. The only entirely correct choices are: use - * fgetc() or read(), and of those the latter will be the - * best-performing. + * fgetc() or fread(). Using fread() will complicate buffer + * management here. * * For now we guess how much fgets() read. */ |