Advertisement
Guest User

Untitled

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