Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include <sys/types.h>
  8. int main(int argc, char *argv[]){
  9. char * x = argv[1];
  10. char * show;
  11. char * show2;
  12. char * first;
  13. char * second;
  14. int status;
  15. int status2;
  16. int proc_f;
  17. int proc_s;
  18. int counter = 0;
  19. if(20 <= strlen(argv[1]) || argc != 2){
  20. printf("Nieprawidlowe wywolanie\n");
  21. return 1;
  22. }
  23. else if(strlen(argv[1]) == 1){
  24. return x[0];
  25. }
  26. if((strlen(argv[1]) % 2) == 0){
  27. int half = strlen(argv[1])/2;
  28. show = malloc(sizeof(char)*21);
  29. first = malloc(sizeof(char)*(half+1));
  30. second = malloc(sizeof(char)*(half+1));
  31. for(int i = 0; i<half ; i++){
  32. first[i] = x[i];
  33. }
  34. for(int i = half ; i<strlen(argv[1]) ; i++){
  35. second[counter] = x[i];
  36. counter++;
  37. }
  38. }
  39. else{
  40. int half = (int)strlen(argv[1])/2;
  41. first = malloc(sizeof(char)*(half+1));
  42. second = malloc(sizeof(char)*(half+2));
  43. show = malloc(sizeof(char)*21);
  44. for(int i = 0; i<half ; i++){
  45. first[i] = x[i];
  46. }
  47. counter = 0;
  48. for(int i = half ; i<strlen(argv[1]) ; i++){
  49. second[counter] = x[i];
  50. counter++;
  51. }
  52. }
  53.  
  54.  
  55. proc_f = fork();
  56. if(proc_f == 0){
  57. show = first;
  58. status = execlp("./skrypt", "./skrypt", show, NULL);
  59. fprintf(stderr, "exec failed: %s\n" , strerror(errno));
  60. return 1;
  61. }
  62. if(proc_f > 0){
  63. proc_s = fork();
  64. if(proc_s == 0){
  65. show2 = second;
  66. status = execlp("./skrypt","./skrypt", show2, NULL);
  67. fprintf(stderr, "exec failed: %s\n" , strerror(errno));
  68. return 1;
  69. }
  70. else if(proc_s > 0){
  71. show = first;
  72. show2 = second;
  73.  
  74. int child = waitpid(proc_f,&status,0);
  75. printf("%d %d %s %c\n",getpid(), child ,show, (char)WEXITSTATUS(status));
  76. int child2 = waitpid(proc_s,&status2,0);
  77.  
  78.  
  79.  
  80. printf("%d %d %s %c\n",getpid(), child2 ,show2, (char)WEXITSTATUS(status2));
  81. return (char)WEXITSTATUS(status2) ;
  82. }
  83.  
  84. }
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement