summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-12-24 16:07:36 -0600
committerNicolas Williams <nico@cryptonector.com>2014-12-24 16:07:36 -0600
commita55085b6620eaadd4c25681c3b744137ba8d30c2 (patch)
tree5f86ab1485ad7e102170eb2e8454fd9269558326
parente0f9b6a5cd8de846e6aac53f356ecb56ad6cd2a7 (diff)
Fix EOF handling; fix #656
-rw-r--r--main.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/main.c b/main.c
index a43f8156..0d32cb0b 100644
--- a/main.c
+++ b/main.c
@@ -153,36 +153,33 @@ FILE* current_input;
const char** input_filenames = NULL;
int ninput_files;
int next_input_idx;
-static int read_more(char* buf, size_t size, int* islast) {
+static int read_more(char* buf, size_t size) {
while (!current_input || feof(current_input)) {
if (current_input) {
if (current_input == stdin) {
- clearerr(stdin); // perhaps we can read again
+ clearerr(stdin); // perhaps we can read again; anyways, we don't fclose(stdin)
} else {
fclose(current_input);
}
current_input = 0;
}
- if (next_input_idx == ninput_files) {
- assert(*islast == 1);
- return 0;
- }
- *islast = 0;
- if (!strcmp(input_filenames[next_input_idx], "-")) {
- current_input = stdin;
- } else {
- current_input = fopen(input_filenames[next_input_idx], "r");
- }
- if (!current_input) {
- fprintf(stderr, "%s: %s: %s\n", progname, input_filenames[next_input_idx], strerror(errno));
+ if (next_input_idx < ninput_files) {
+ if (!strcmp(input_filenames[next_input_idx], "-")) {
+ current_input = stdin;
+ } else {
+ current_input = fopen(input_filenames[next_input_idx], "r");
+ if (!current_input)
+ fprintf(stderr, "%s: %s: %s\n", progname, input_filenames[next_input_idx], strerror(errno));
+ }
+ next_input_idx++;
}
- next_input_idx++;
- *islast = (next_input_idx == ninput_files);
}
- if (!fgets(buf, size, current_input)) buf[0] = 0;
- if (!current_input || feof(current_input)) *islast = 1;
- return 1;
+ if (current_input) {
+ if (!fgets(buf, size, current_input))
+ buf[0] = 0;
+ }
+ return next_input_idx == ninput_files && (!current_input || feof(current_input));
}
int main(int argc, char* argv[]) {
@@ -493,7 +490,8 @@ int main(int argc, char* argv[]) {
struct jv_parser* parser = jv_parser_new((options & SEQ) ? JV_PARSE_SEQ : 0);
char buf[4096];
int is_last = 0;
- while (read_more(buf, sizeof(buf), &is_last)) {
+ do {
+ is_last = read_more(buf, sizeof(buf));
if (options & RAW_INPUT) {
int len = strlen(buf);
if (len > 0) {
@@ -530,7 +528,7 @@ int main(int argc, char* argv[]) {
}
}
}
- }
+ } while (!is_last);
jv_parser_free(parser);
if (ret != 0)
goto out;