From f36156d44a73cc91a39840051859f8a233760c94 Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Sun, 16 Dec 2018 16:00:16 +0100 Subject: [PATCH] Fix splitting bug --- cpair/cpair.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/cpair/cpair.c b/cpair/cpair.c index 4a548f0..4ef5384 100644 --- a/cpair/cpair.c +++ b/cpair/cpair.c @@ -134,12 +134,12 @@ int main(int argc, char *argv[]) { close(in_pipe_a[1]); close(STDIN_FILENO); if(dup2(in_pipe_a[0], STDIN_FILENO) != STDIN_FILENO) { - fprintf(stderr, "ERROR: Failed duplicating STDIN"); + fprintf(stderr, "ERROR: Failed duplicating STDIN\n"); exit(EXIT_FAILURE); } execlp(argv[0], argv[0], NULL); - fprintf(stderr, "ERROR: Failed to load program into child"); + fprintf(stderr, "ERROR: Failed to load program into child\n"); exit(EXIT_FAILURE); default: /// Parent execution @@ -154,12 +154,12 @@ int main(int argc, char *argv[]) { close(in_pipe_b[1]); close(STDIN_FILENO); if(dup2(in_pipe_b[0], STDIN_FILENO) != STDIN_FILENO) { - fprintf(stderr, "ERROR: Failed duplicating STDIN"); + fprintf(stderr, "ERROR: Failed duplicating STDIN\n"); exit(EXIT_FAILURE); } execlp(argv[0], argv[0], NULL); - fprintf(stderr, "ERROR: Failed to load program into child"); + fprintf(stderr, "ERROR: Failed to load program into child\n"); exit(EXIT_FAILURE); default: /// Parent execution @@ -180,14 +180,16 @@ int main(int argc, char *argv[]) { /// Separate the lists based on the mean current = head; - while(current != NULL) { + while(current -> next != NULL) { if(current -> points[0] <= x_mean) { first_part_buff -> points[0] = current -> points[0]; + first_part_buff -> points[1] = current -> points[1]; first_part_buff -> next = malloc(sizeof(node_t)); first_part_buff = first_part_buff -> next; first_part_len ++; } else { second_part_buff -> points[0] = current -> points[0]; + second_part_buff -> points[1] = current -> points[1]; second_part_buff -> next = malloc(sizeof(node_t)); second_part_buff = second_part_buff -> next; second_part_len ++; @@ -200,22 +202,22 @@ int main(int argc, char *argv[]) { * Send first half to first child * **/ - if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) { - fprintf(stderr, "ERROR: Failed duplicating STDIN"); - exit(EXIT_FAILURE); - } - - /// Setup string for pipe + /// Setup string for first child pipe current = first_part; - char first_part_string[first_part_len + 1][__UINT8_MAX__]; + char first_part_string[first_part_len][__UINT8_MAX__]; int i = 0; - while(current != NULL) { + while(current -> next != NULL) { sprintf(first_part_string[i], "%f %f\n", current -> points[0], current -> points[1]); i++; current = current -> next; } first_part_string[first_part_len][0] = EOF; + if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) { + fprintf(stderr, "ERROR: Failed duplicating STDIN\n"); + exit(EXIT_FAILURE); + } + /// Write to pipe if (write(in_pipe_a[1], first_part_string, first_part_len * __UINT8_MAX__) < 0) { fprintf(stderr, "ERROR: Failed writing to child\n"); @@ -228,22 +230,22 @@ int main(int argc, char *argv[]) { * Send second half to second child * **/ - if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) { - fprintf(stderr, "ERROR: Failed duplicating STDIN\n"); - exit(EXIT_FAILURE); - } - - /// Setup string for pipe + /// Setup string for second child pipe current = second_part; char second_part_string[second_part_len + 1][__UINT8_MAX__]; i = 0; - while(current != NULL) { + while(current -> next != NULL) { sprintf(second_part_string[i], "%f %f\n", current -> points[0], current -> points[1]); i++; current = current -> next; } second_part_string[second_part_len][0] = EOF; + if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) { + fprintf(stderr, "ERROR: Failed duplicating STDIN\n"); + exit(EXIT_FAILURE); + } + /// Write to pipe if (write(in_pipe_b[1], second_part_string, second_part_len * __UINT8_MAX__) < 0) { fprintf(stderr, "ERROR: Failed writing to child\n");