Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include<sys/types.h>
  5. #include<string.h>
  6. #include<sys/wait.h>
  7.  
  8. char* delete_tabs(char* new_str,char* text) {
  9.     int j = 0;
  10.     for (int i = 0; i<strlen(text); ++i) {
  11.         if (text[i]=='\t') {
  12.             for (int k = 0; k<3; ++k) {
  13.                 new_str[j]=' ';
  14.                 ++j;
  15.             }
  16.         } else {
  17.             new_str[j]=text[i];
  18.             ++j;
  19.         }
  20.     }
  21.            printf("%s\n",new_str);
  22.  
  23.     return new_str;
  24. }
  25.  
  26. int main()
  27. {
  28.  
  29.     int fd1[2];  
  30.     int fd2[2];  
  31.     char result_str[100];
  32.     char input_str[100];
  33.     pid_t p;
  34.  
  35.     if (pipe(fd1)==-1)
  36.     {
  37.         fprintf(stderr, "Pipe Failed" );
  38.         return 1;
  39.     }
  40.     if (pipe(fd2)==-1)
  41.     {
  42.         fprintf(stderr, "Pipe Failed" );
  43.         return 1;
  44.     }
  45.  
  46.     p = fork();
  47.  
  48.     if (p < 0)
  49.     {
  50.         fprintf(stderr, "fork Failed" );
  51.         return 1;
  52.     }
  53.  
  54.     // Parent process
  55.     else if (p > 0)
  56.     {
  57.         scanf ("%[^\n]%*c", input_str);
  58.  
  59.         close(fd1[0]);  // Close reading end of first pipe
  60.  
  61.         // Write input string and close writing end of first
  62.         // pipe.
  63.         write(fd1[1], input_str, strlen(input_str)+1);
  64.         close(fd1[1]);
  65.  
  66.         // Wait for child to send a string
  67.         wait(NULL);
  68.  
  69.         close(fd2[1]); // Close writing end of second pipe
  70.  
  71.         // Read string from child, print it and close
  72.         // reading end.
  73.         read(fd2[0], result_str, 100);
  74.         printf("%s\n",result_str);
  75.         close(fd2[0]);
  76.     }
  77.  
  78.     // child process
  79.     else
  80.     {
  81.         close(fd1[1]);  // Close writing end of first pipe
  82.  
  83.         // Read a string using first pipe
  84.         char get_str[100];
  85.         read(fd1[0], get_str, 100);
  86.        char* new_str;
  87.         delete_tabs(new_str,get_str);
  88.  
  89.        for (int i = 0; i<strlen(new_str); ++i) {
  90.            if (new_str[i]==' ') {
  91.                new_str[i]='_';
  92.            }
  93.        }
  94.         // Concatenate a fixed string with it
  95.        
  96.  
  97.         // Close both reading ends
  98.         close(fd1[0]);
  99.         close(fd2[0]);
  100.  
  101.         // Write concatenated string and close writing end
  102.         write(fd2[1], new_str, strlen(new_str)+1);
  103.         close(fd2[1]);
  104.  
  105.         exit(0);
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement