From: Nicolas Williams Date: Tue, 28 Apr 2015 22:27:12 +0000 (-0500) Subject: Report read errors too (and fix #772) X-Git-Tag: jq-1.5rc2~105 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e3cb1f76cd3e2e55455757217be15662d3c75236;p=jq Report read errors too (and fix #772) --- diff --git a/jq.h b/jq.h index ab6e5d5..111b1bc 100644 --- a/jq.h +++ b/jq.h @@ -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 *); diff --git a/main.c b/main.c index 9791b22..31dbc82 100644 --- a/main.c +++ b/main.c @@ -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: diff --git a/util.c b/util.c index f5e61ff..1d9b0f2 100644 --- a/util.c +++ b/util.c @@ -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. */