Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- char *program_name;
- struct parts
- {
- char *first_half;
- char *second_half;
- };
- void usage(char *message)
- {
- fprintf(stderr, "%s : %s\n", program_name, message);
- exit(EXIT_FAILURE);
- }
- char * getLine()
- {
- char *line = NULL;
- size_t len = 0;
- getline(&line, &len, stdin);
- line[strcspn(line, "\n")] = '\0';
- return line;
- }
- void split(char *input, struct parts * p)
- {
- int len = strlen(input);
- int len1 = len/2;
- int len2 = len - len1;
- char *s1 = malloc(len1+1);
- memcpy(s1, input, len1);
- s1[len1] = '\0';
- char *s2 = malloc(len2+1);
- memcpy(s2, input+len1, len2);
- s2[len2] = '\0';
- p -> first_half = s1;
- p -> second_half = s2;
- }
- void redirectPipes(pid_t pid, int *pipe1, int *pipe2)
- {
- if (pid == -1)
- {
- exit(EXIT_FAILURE);
- } else if (pid == 0)
- {
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- dup2(pipe2[0],STDIN_FILENO);
- close(pipe2[1]);
- close(pipe2[0]);
- dup2(pipe1[1],STDOUT_FILENO);
- close(pipe1[1]);
- close(pipe1[0]);
- if((execlp(program_name,program_name,(char *)NULL)) == -1)
- {
- exit(EXIT_FAILURE);
- }
- }
- }
- int main(int argc, char *argv[])
- {
- program_name = argv[0];
- int status = 0;
- char *input;
- input = getLine();
- int input_length = strlen(input);
- if((input_length/2)%2 == 1 && input_length > 2)
- {
- usage("input must have even length");
- }
- if (input_length == 1)
- {
- fprintf(stdout, "%s", input);
- }else
- {
- struct parts p1;
- split(input, &p1);
- //================================================== Pipe and Fork ============================================================//
- // pipe - 1st child
- int pipeEndsFirstChild1[2];
- int pipeEndsFirstChild2[2];
- if(pipe(pipeEndsFirstChild1) == -1)
- {
- exit(EXIT_FAILURE);
- }
- if(pipe(pipeEndsFirstChild2) == -1)
- {
- exit(EXIT_FAILURE);
- }
- // fork - 1st child
- pid_t pid1 = fork();
- redirectPipes(pid1, pipeEndsFirstChild1, pipeEndsFirstChild2);
- // close unnecessary
- if(close(pipeEndsFirstChild1[1]) == -1)
- {
- exit(EXIT_FAILURE);
- }
- if(close(pipeEndsFirstChild2[0]) == -1)
- {
- exit(EXIT_FAILURE);
- }
- // pipe - 2nd child
- int pipeEndsSecondChild1[2];
- int pipeEndsSecondChild2[2];
- if(pipe(pipeEndsSecondChild1) == -1)
- {
- exit(EXIT_FAILURE);
- }
- if(pipe(pipeEndsSecondChild2) == -1)
- {
- exit(EXIT_FAILURE);
- }
- // fork - 2nd child
- pid_t pid2 = fork();
- redirectPipes(pid2, pipeEndsSecondChild1, pipeEndsSecondChild2);
- // close unnecessary
- if(close(pipeEndsSecondChild1[1]) == -1)
- {
- exit(EXIT_FAILURE);
- }
- if(close(pipeEndsSecondChild2[0]) == -1)
- {
- exit(EXIT_FAILURE);
- }
- // write to 1st and 2nd child
- write(pipeEndsFirstChild2[1], p1.first_half, strlen(p1.first_half));
- write(pipeEndsSecondChild2[1], p1.second_half, strlen(p1.second_half));
- // close pipes
- if(close(pipeEndsFirstChild2[1]) == -1){
- exit(EXIT_FAILURE);
- }
- if(close(pipeEndsSecondChild2[1]) == -1){
- exit(EXIT_FAILURE);
- }
- // reading from 1st and 2nd child
- char readBufFirstChild[128];
- char readBufSecondChild[128];
- // open output fd of 1st child
- FILE *filePointer1 = fdopen(pipeEndsFirstChild1[0], "r");
- if(filePointer1 == NULL)
- {
- exit(EXIT_FAILURE);
- }
- // put output into readBufFirstChild
- fgets(readBufFirstChild,sizeof(readBufFirstChild),filePointer1);
- // open output fd of 2nd child
- FILE *filePointer2 = fdopen(pipeEndsSecondChild1[0], "r");
- if(filePointer2 == NULL){
- exit(EXIT_FAILURE);
- }
- // open output fd of 2st child
- fgets(readBufSecondChild,sizeof(readBufSecondChild),filePointer2);
- //concat results
- char *result = malloc(strlen(readBufFirstChild) + strlen(readBufSecondChild) + 1);
- strcpy(result, readBufFirstChild);
- strcat(result, readBufSecondChild);
- fprintf(stderr, "result ->\n%s\n", result);
- if(wait(&status) == -1){
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement