Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- int main(int argc, char *argv[]){
- char * x = argv[1];
- char * show;
- char * show2;
- char * first;
- char * second;
- int status;
- int status2;
- int proc_f;
- int proc_s;
- int counter = 0;
- if(20 <= strlen(argv[1]) || argc != 2){
- printf("Nieprawidlowe wywolanie\n");
- return 1;
- }
- else if(strlen(argv[1]) == 1){
- return x[0];
- }
- if((strlen(argv[1]) % 2) == 0){
- int half = strlen(argv[1])/2;
- show = malloc(sizeof(char)*21);
- first = malloc(sizeof(char)*(half+1));
- second = malloc(sizeof(char)*(half+1));
- for(int i = 0; i<half ; i++){
- first[i] = x[i];
- }
- for(int i = half ; i<strlen(argv[1]) ; i++){
- second[counter] = x[i];
- counter++;
- }
- }
- else{
- int half = (int)strlen(argv[1])/2;
- first = malloc(sizeof(char)*(half+1));
- second = malloc(sizeof(char)*(half+2));
- show = malloc(sizeof(char)*21);
- for(int i = 0; i<half ; i++){
- first[i] = x[i];
- }
- counter = 0;
- for(int i = half ; i<strlen(argv[1]) ; i++){
- second[counter] = x[i];
- counter++;
- }
- }
- proc_f = fork();
- if(proc_f == 0){
- show = first;
- status = execlp("./skrypt", "./skrypt", show, NULL);
- fprintf(stderr, "exec failed: %s\n" , strerror(errno));
- return 1;
- }
- if(proc_f > 0){
- proc_s = fork();
- if(proc_s == 0){
- show2 = second;
- status = execlp("./skrypt","./skrypt", show2, NULL);
- fprintf(stderr, "exec failed: %s\n" , strerror(errno));
- return 1;
- }
- else if(proc_s > 0){
- show = first;
- show2 = second;
- int child = waitpid(proc_f,&status,0);
- printf("%d %d %s %c\n",getpid(), child ,show, (char)WEXITSTATUS(status));
- int child2 = waitpid(proc_s,&status2,0);
- printf("%d %d %s %c\n",getpid(), child2 ,show2, (char)WEXITSTATUS(status2));
- return (char)WEXITSTATUS(status2) ;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement