daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Feb 20th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #define BUFF_SIZE 255
  10.  
  11. int main(int ac, char* av[])
  12. {  
  13.     if(ac <3)
  14.     {
  15.         printf("Please enter all required arguments!n");  
  16.         exit(0);    
  17.     }
  18.  
  19.     int pfd[2];
  20.     int pipeCreated;
  21.  
  22.     char readFile[50];
  23.     char writePipe[20];
  24.  
  25.     pid_t child_pid_read;
  26.     pid_t child_pid_write;
  27.  
  28.     pipeCreated = pipe(pfd);
  29.  
  30.     if(pipeCreated == -1)
  31.     {
  32.         printf("An error occurred when trying to create a pipen");
  33.         exit(0);
  34.     }
  35.  
  36.     strcpy(readFile, av[1]);
  37.     sprintf(writePipe,"%d", pfd[1]);
  38.  
  39.     child_pid_read = fork();
  40.  
  41.     char writeFile[50];
  42.     char readPipe[20];
  43.  
  44.     //Handling the read()
  45.     switch(child_pid_read)
  46.     {
  47.         //Error in case forfk() failed
  48.         case -1:
  49.             perror("fork failed");
  50.             return 1;
  51.  
  52.             //Handle child processes
  53.         case 0:
  54.              if(close(pfd[0]) == -1)
  55.              {
  56.                  printf("An error occurred while closing the pipen");
  57.                  exit(0);
  58.              }
  59.              if(execle("./read.out", "./read.out", readFile, writePipe, (char*)0, NULL) == -1)
  60.              {
  61.                  printf("Child: Error creating read.n");
  62.                  exit(0);
  63.              }
  64.  
  65.         default:
  66.             wait(&child_pid_read);
  67.  
  68.             strcpy(writeFile, av[2]);
  69.             sprintf(readPipe,"%d", pfd[0]);
  70.  
  71.             child_pid_write = fork();
  72.             break;
  73.     }
  74.  
  75.     //Handling the write
  76.     switch(child_pid_write)
  77.     {
  78.         //Error in case fork() failed
  79.         case -1:
  80.             perror("fork failed");
  81.             return 1;
  82.  
  83.         //Handle child processes
  84.         case 0:
  85.             if(close(pfd[1]) == -1)
  86.             {
  87.                 printf("An error occurred while closing the pipen");
  88.                 exit(0);
  89.             }
  90.  
  91.             if(execle("./write.out", "./write.out", writeFile, readPipe, (char*)0, NULL) == -1)
  92.             {
  93.                 printf("Child: Error creating read.n");
  94.                 exit(-1);
  95.             }
  96.             break;
  97.  
  98.         default:
  99.             wait(&child_pid_write);
  100.             break;
  101.     }
  102.     printf("Write completed!");
  103.     return 0;
  104. }
  105.    
  106. #include <unistd.h>
  107. #include <stdio.h>
  108. #include <stdlib.h>
  109. #include <fcntl.h>
  110.  
  111. #define BUFF_SIZE 16
  112.  
  113. int main(int ac, char* av[])
  114. {  
  115.     char buffer[BUFF_SIZE];
  116.     int fd;
  117.     int pid;
  118.  
  119.     if(ac > 1)
  120.     {
  121.         fd = open(av[1], O_RDONLY);
  122.         if(fd == -1)
  123.         {
  124.             printf("error: Could Not Open Filen");
  125.             exit(0);
  126.         }
  127.  
  128.         pid = atoi(av[2]);
  129.  
  130.     }
  131.  
  132.     int num_read = 1;
  133.  
  134.     while(1)
  135.     {
  136.         num_read = read(fd, buffer, BUFF_SIZE);
  137.  
  138.         if(num_read == -1)
  139.         {
  140.             printf("Error reading filen");
  141.             exit(0);
  142.         }
  143.  
  144.         if(num_read == 0)
  145.         {
  146.             break;
  147.         }
  148.  
  149.         if(write(pid, buffer, num_read) != num_read)
  150.         {
  151.             printf("Error writing to pipen");
  152.             break;
  153.         }
  154.     }
  155.     close(fd);
  156.     return 1;
  157. }
  158.    
  159. #include <unistd.h>
  160. #include <stdio.h>
  161. #include <stdlib.h>
  162. #include <fcntl.h>
  163.  
  164. #define BUFF_SIZE 1
  165.  
  166. int main(int ac, char* av[])
  167. {
  168.     char buffer[BUFF_SIZE];
  169.  
  170.         int fd = open(av[1], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  171.  
  172.         int pid = atoi(av[2]);
  173.  
  174.         int num_read = 1;
  175.  
  176.     while(1)
  177.     {
  178.             num_read = read(pid, buffer, BUFF_SIZE);
  179.  
  180.             printf("num_read: %dn", num_read);
  181.  
  182.             if(num_read == -1)
  183.             {
  184.                 printf("Error reading pipen");
  185.                 break;
  186.             }
  187.  
  188.             if(write(fd, buffer, num_read) != num_read)
  189.             {
  190.                 printf("Error writing to filen");
  191.                 break;
  192.             }
  193.  
  194.             if(num_read == EOF)
  195.             {
  196.                 break;
  197.             }
  198.     }
  199.  
  200.         close(fd);
  201.         return 1;
  202. }
  203.    
  204. default:
  205.     if(close(pfd[0]) == -1)
  206.      {
  207.          printf("An error occurred while closing the pipen");
  208.          exit(0);
  209.      }
  210.     wait(&child_pid_write);
  211.     break;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top