Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8.  
  9. int notAVowel(char c) {
  10.   char vowels[10] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
  11.   int index = 0;
  12.   while (index < 10) {
  13.     if (c == vowels[index])
  14.       return 0;
  15.     index++;
  16.   }
  17.   return 1;
  18. }
  19. int main(int argc, char **argv) {
  20.   int pipeUno[2];
  21.   pid_t pid1, pid2;
  22.   if (argc != 2)
  23.     perror("Invalid argument number(inset one)"), exit(-1);
  24.  
  25.   if (pipe(pipeUno) < 0)
  26.     perror("Pipe"), exit(-1);
  27.  
  28.   if ((pid1 = fork()) < 0)
  29.     perror("Fork"), exit(-1);
  30.   if (pid1 == 0) { /*figlio 1*/
  31.     char buffer[1];
  32.     int fileDesc;
  33.     close(pipeUno[1]);
  34.     if ((fileDesc = open(argv[1], O_WRONLY | O_APPEND)) < 0)
  35.       perror("Can't open file (writing)"), exit(-1);
  36.     while (read(pipeUno[0], buffer, 1)) {
  37.       if (notAVowel(buffer[0])) {
  38.         write(fileDesc, buffer, 1);
  39.       }
  40.     }
  41.     close(pipeUno[1]);
  42.     close(fileDesc);
  43.   } else {
  44.     if ((pid2 = fork()) < 0)
  45.       perror("Fork 2"), exit(-1);
  46.     if (pid2 == 0) { /* figlio 2*/
  47.       int fileDescriptor;
  48.       char buffer[1];
  49.       if ((fileDescriptor = open(argv[1], O_RDONLY)) < 0)
  50.         perror("Can't open file"), exit(-1);
  51.       close(pipeUno[0]);
  52.       while (read(fileDescriptor, buffer, 1)) {
  53.         write(pipeUno[1], buffer, 1);
  54.       }
  55.       close(pipeUno[1]);
  56.       close(fileDescriptor);
  57.     }
  58.   }
  59.   return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement