Advertisement
Guest User

another-grep

a guest
Jan 29th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>
  10. #include <sys/wait.h>
  11. #include <sys/stat.h>
  12. #define WORDSIZE 64
  13. #define BUFFSIZE 2048
  14. #define SIZE 128
  15. #define MSGSIZE sizeof(msg)-sizeof(long)
  16. #define WRITER 2
  17. #define DIM 100
  18. /* struttura dei messaggi nella coda*/
  19. typedef struct {
  20.     long type;
  21.     char stop;
  22.     char riga[BUFFSIZE];
  23. } msg;
  24. /* reader legge il contenuto del file indicato e lo passa al padre
  25.  * tramite una pipe unidirezionale */
  26. void reader(int pipefd, char *url) {
  27.     char buffer[BUFFSIZE];
  28.     struct stat statbuf;
  29.     FILE *input_fs, *output_fs;
  30.     if( (input_fs = fopen(url, "r")) == NULL) {
  31.         perror("fopen");
  32.         exit(1);
  33.     }
  34.     if( (stat(url, &statbuf)==-1) || (!S_ISREG(statbuf.st_mode)) ){
  35.         perror("stat");
  36.         exit(1);
  37.     }
  38.     /*inserire qui controllo file*/
  39.     if( (output_fs = fdopen(pipefd, "w")) == NULL) {
  40.         perror("pipe");
  41.         exit(1);
  42.     }
  43.     while(fgets(buffer, BUFFSIZE, input_fs)!=NULL) {
  44.         fputs(buffer, output_fs);
  45.     }
  46.     fclose(input_fs);
  47.     exit(0);
  48. }
  49. /* writer visualizza a video le righe ricevute dal padre */
  50. void writer(int qid) {
  51.     msg messaggio;
  52.     fprintf(stdout, "W> Sono il writer.\n");
  53.     while(1) {
  54.         msgrcv(qid, &messaggio, MSGSIZE, WRITER, 0);
  55.         if(messaggio.stop == 1)
  56.             break;
  57.         fprintf(stdout, "W> %s\n", messaggio.riga);
  58.     }
  59.     fprintf(stdout, "END WRITER\n");
  60.     exit(0);
  61. }
  62. /* ricerca una parola all'interno di una stringa, restituisce 0 se contenuta nella riga */
  63. int ricerca(char *key, char *stringa) {
  64.     char * ret;
  65.     if((ret = strstr(stringa, key))==NULL)
  66.         return 1;
  67.     return 0;
  68. }
  69. /* parent legge il contenuto ricevuto riga per riga e seleziona solo le
  70.  * righe contenenti la parola indicata, passandole poi una alla volta al
  71.  * writer tramite coda di messaggi */
  72. int main(int argc, char **argv) {
  73.     /* dichiarazione delle variabili */
  74.     char key[WORDSIZE]; //la parola da ricercare
  75.     int qid;
  76.     int pipefd[2];//0 lettura 1 scrittura
  77.     int a = 0;
  78.     msg messaggio;
  79.     FILE *input_fs;
  80.     char buffer[BUFFSIZE];
  81.     messaggio.stop = 0;
  82.     /* controllo sui parametri */
  83.     if(argc!=3) {
  84.         fprintf(stderr, "P> Utilizzo: %s  <parola>  <file-di-testo>\n", argv[0]);
  85.         exit(1);
  86.     }
  87.     strcpy(key, argv[1]); //memorizzo la parola
  88.     /* pipe unidirezionale*/
  89.     if(pipe(pipefd)==-1) {
  90.         perror("pipe");
  91.         exit(1);
  92.     }
  93.     /* coda di messaggi */
  94.     if((qid = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600))==-1) {
  95.         perror("msgget");
  96.         exit(1);
  97.     }
  98.     /* creazione dei processi figlio */
  99.     if(fork()==0) {
  100.         close(pipefd[0]);//chiude canale input sulla pipe nel re!der
  101.         reader(pipefd[1], argv[2]);
  102.     }
  103.     if(fork()==0)
  104.         writer(qid);
  105.     /* lettura ed invio delle righe esatte */
  106.     close(pipefd[1]);
  107.     if( (input_fs = fdopen(pipefd[0], "r")) == NULL) {
  108.         perror("pipe");
  109.         exit(1);
  110.     }
  111.     while(1) {
  112.         fgets(buffer, BUFFSIZE, input_fs);
  113.         if(buffer == NULL || strcmp(buffer, "") == 0){
  114.             printf("SANTI\n");
  115.             break;
  116.         }
  117.         if( ricerca(argv[1], buffer) == 0) {
  118.             strcpy(messaggio.riga, buffer);
  119.             messaggio.type = WRITER;
  120.             msgsnd(qid, &messaggio, MSGSIZE, 0);
  121.         }
  122.     }
  123.     messaggio.stop = 1;
  124.     messaggio.type = WRITER;
  125.     msgsnd(qid, &messaggio, MSGSIZE, 0);
  126.     /* chiusura delle strutture */
  127.     wait(NULL);
  128.     //wait(NULL);
  129.     //msgctl(qid, IPC_RMID, NULL);
  130.     fprintf(stdout, "P> Coda rimossa.\n");
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement