Skip to content

Commit

Permalink
Fix schweikert#164: Ensure all JSON output goes to stdout
Browse files Browse the repository at this point in the history
- Modified json.c to consistently use stdout for all JSON output
- Updated add_name in fping.c to use stdout for error JSON output
- Simplified error JSON output in add_name to match other JSON output
- All tests now pass with consistent stdout/stderr behavior
  • Loading branch information
JoshIPT committed Feb 12, 2025
1 parent 22d5784 commit 0b9f531
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
15 changes: 2 additions & 13 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -2798,19 +2798,8 @@ void add_name(char *name)

// Handle JSON output for invalid hosts
if (output_json_flag) {
static int first_invalid = 1;
if (first_invalid) {
print_json_start();
print_json_keyvalue("host", name, 1);
print_json_keyvalue("error", gai_strerror(ret_ga), 0);
print_json_end(0);
first_invalid = 0;
} else {
print_json_next();
print_json_keyvalue("host", name, 1);
print_json_keyvalue("error", gai_strerror(ret_ga), 0);
print_json_end(0);
}
fprintf(stdout, "{\"host\": \"%s\",\"error\": \"%s\"}", name, gai_strerror(ret_ga));
return;
}

num_noaddress++;
Expand Down
40 changes: 20 additions & 20 deletions src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ static int current_indent = 0; /* Current indentation level */
static void print_indent(void) {
if (json_pretty_print > 0) {
for (int i = 0; i < current_indent * json_pretty_print; i++) {
fprintf(stderr, " ");
fprintf(stdout, " ");
}
}
}

static void print_newline(void) {
if (json_pretty_print > 0) {
fprintf(stderr, "\n");
fprintf(stdout, "\n");
}
}

Expand Down Expand Up @@ -71,57 +71,57 @@ char* json_int_to_string(int input) {

void print_json_start(void) {
print_indent();
fprintf(stderr, "{");
fprintf(stdout, "{");
increase_indent();
}

void print_json_next(void) {
fprintf(stderr, ",");
fprintf(stdout, ",");
print_newline();
print_indent();
fprintf(stderr, "{");
fprintf(stdout, "{");
increase_indent();
}

void print_json_end(int json_last_end) {
decrease_indent();
print_newline();
print_indent();
fprintf(stderr, "}");
fprintf(stdout, "}");
if (json_last_end > 0 && json_pretty_print > 0) {
print_newline();
}
}

void print_json_start_array(const char *json_key, int json_first_record) {
if (json_key == NULL) {
fprintf(stderr, "Error: NULL pointer provided\n");
fprintf(stdout, "Error: NULL pointer provided\n");
return;
}

size_t key_len = strlen(json_key);
char *safe_key = malloc(key_len + 1);

if (safe_key == NULL) {
fprintf(stderr, "Error: Memory allocation failed\n");
fprintf(stdout, "Error: Memory allocation failed\n");
free(safe_key);
return;
}

strcpy(safe_key, json_key);

if (json_first_record == 0) {
fprintf(stderr, ",");
fprintf(stdout, ",");
print_newline();
} else {
print_newline();
}

print_indent();
if (json_pretty_print > 0) {
fprintf(stderr, "\"%s\": [", safe_key);
fprintf(stdout, "\"%s\": [", safe_key);
} else {
fprintf(stderr, "\"%s\":[", safe_key);
fprintf(stdout, "\"%s\":[", safe_key);
}

if (json_first_record > 0) {
Expand All @@ -137,12 +137,12 @@ void print_json_end_array(void) {
decrease_indent();
print_newline();
print_indent();
fprintf(stderr, "]");
fprintf(stdout, "]");
}

void print_json_keyvalue(const char *json_key, const char *json_value, int json_first_record) {
if (json_key == NULL || json_value == NULL) {
fprintf(stderr, "Error: NULL pointer provided\n");
fprintf(stdout, "Error: NULL pointer provided\n");
return;
}

Expand All @@ -153,7 +153,7 @@ void print_json_keyvalue(const char *json_key, const char *json_value, int json_
char *safe_value = malloc(value_len + 1);

if (safe_key == NULL || safe_value == NULL) {
fprintf(stderr, "Error: Memory allocation failed\n");
fprintf(stdout, "Error: Memory allocation failed\n");
free(safe_key);
free(safe_value);
return;
Expand All @@ -163,44 +163,44 @@ void print_json_keyvalue(const char *json_key, const char *json_value, int json_
strcpy(safe_value, json_value);

if (json_first_record == 0) {
fprintf(stderr, ",");
fprintf(stdout, ",");
print_newline();
} else {
print_newline();
}

print_indent();
fprintf(stderr, "\"%s\": \"%s\"", safe_key, safe_value);
fprintf(stdout, "\"%s\": \"%s\"", safe_key, safe_value);

free(safe_key);
free(safe_value);
}

void print_json_nokeyvalue(const char *json_value, int json_first_record) {
if (json_value == NULL) {
fprintf(stderr, "Error: NULL pointer provided\n");
fprintf(stdout, "Error: NULL pointer provided\n");
return;
}

size_t value_len = strlen(json_value);
char *safe_value = malloc(value_len + 1);

if (safe_value == NULL) {
fprintf(stderr, "Error: Memory allocation failed\n");
fprintf(stdout, "Error: Memory allocation failed\n");
free(safe_value);
return;
}

strcpy(safe_value, json_value);

if (json_first_record == 0) {
fprintf(stderr, ",");
fprintf(stdout, ",");
print_newline();
} else if (json_first_record == 1) {
print_newline();
}
print_indent();
fprintf(stderr, "\"%s\"", safe_value);
fprintf(stdout, "\"%s\"", safe_value);

free(safe_value);
}

0 comments on commit 0b9f531

Please sign in to comment.