Advertisement
razvanth21

Untitled

Jan 14th, 2022
1,045
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 <fcntl.h>
  5.  
  6. int main(int argc, char *argv[]) {
  7.     if (argc != 4) {
  8.         fprintf(stderr, "Utilizare: %s <fisier> <string-tipar-1> <string-tipar-2>\n", argv[0]);
  9.         exit(-1);
  10.     }
  11.  
  12.     pid_t pid[3];
  13.     pid[0] = fork();
  14.  
  15.     int pipe1[2];
  16.     int pipe2[2];
  17.     int pipe3[2];
  18.  
  19.     if (pipe(pipe1) < 0) {
  20.         fprintf(stderr, "Eroare pipe().\n");
  21.         exit(-1);
  22.     }
  23.  
  24.     if (pipe(pipe2) < 0) {
  25.         fprintf(stderr, "Eroare pipe().\n");
  26.         exit(-1);
  27.     }
  28.  
  29.     if (pipe(pipe3) < 0) {
  30.         fprintf(stderr, "Eroare pipe().\n");
  31.         exit(-1);
  32.     }
  33.  
  34.     if (pid[0] < 0) {
  35.         fprintf(stderr, "Eroare fork() cititor.\n");
  36.         exit(-1);
  37.     }
  38.  
  39.     if (pid[0] == 0) {
  40.         // cod cititor
  41.         close(pipe1[0]);
  42.         dup2(pipe1[1], 1); // redirectioneaza iesirea standard catre pipe
  43.         close(pipe1[1]);
  44.         close(pipe2[0]);
  45.         close(pipe2[1]);
  46.         close(pipe3[0]);
  47.         close(pipe3[1]);
  48.  
  49.         execlp("cat", "cat", argv[1], NULL);
  50.         fprintf(stderr, "Eroare execlp().\n");
  51.         exit(-1);
  52.     }
  53.  
  54.     pid[1] = fork();
  55.     if (pid[1] < 0) {
  56.         fprintf(stderr, "Eroare fork() filtrator-1.\n");
  57.         exit(-1);
  58.     }
  59.  
  60.     if (pid[1] == 0) {
  61.         // cod filtrator-1
  62.         close(pipe1[1]);
  63.         close(pipe2[0]);
  64.         close(pipe3[0]);
  65.         close(pipe3[1]);
  66.         dup2(pipe1[0], 0);
  67.         dup2(pipe2[1], 1);
  68.         close(pipe1[0]);
  69.         close(pipe2[1]);
  70.  
  71.         char regex[64];
  72.         int n = sprintf(regex, ".*%s.*", argv[2]);
  73.         regex[n] = '\0';
  74.  
  75.         execlp("grep", "grep", "-E", regex, NULL);
  76.         fprintf(stderr, "Eroare execlp().\n");
  77.         exit(-1);
  78.     }
  79.  
  80.     pid[2] = fork();
  81.     if (pid[2] < 0) {
  82.         fprintf(stderr, "Eroare fork() filtrator-2.\n");
  83.         exit(-1);
  84.     }
  85.  
  86.     if (pid[2] == 0) {
  87.         // cod filtrator-2
  88.         close(pipe1[0]);
  89.         close(pipe1[1]);
  90.         close(pipe2[1]);
  91.         close(pipe3[0]);
  92.         dup2(pipe2[0], 0);
  93.         dup2(pipe3[1], 1);
  94.         close(pipe2[0]);
  95.         close(pipe3[1]);
  96.  
  97.         char regex[64];
  98.         int n = sprintf(regex, ".*%s.*", argv[3]);
  99.         regex[n] = '\0';
  100.  
  101.         execlp("grep", "grep", "-E", regex, NULL);
  102.         fprintf(stderr, "Eroare execlp().\n");
  103.         exit(-1);
  104.     }
  105.  
  106.     // cod parinte
  107.  
  108.     close(pipe1[0]);
  109.     close(pipe1[1]);
  110.     close(pipe2[0]);
  111.     close(pipe2[1]);
  112.     close(pipe3[1]);
  113.  
  114.     char buffer[512];
  115.     int r;
  116.     int k = 0;
  117.  
  118.     while ((r = read(pipe3[0], buffer, sizeof(buffer)))) {
  119.         buffer[r] = '\0';
  120.  
  121.         for (int i = 0; i < r; i++) {
  122.             if (buffer[i] == '\n') {
  123.                 k++;
  124.             }
  125.         }
  126.     }
  127.  
  128.     close(pipe3[0]);
  129.     fprintf(stdout, "%d\n", k);
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement