Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <string.h>
  6. #include <sys/wait.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <errno.h>
  10.  
  11. #define BUFFER 1500
  12. #define READ 0
  13. #define WRITE 1
  14.  
  15. int main(int argc, char* argv[]){
  16.  
  17.  
  18.     //verificacao do numero de argumentos
  19.     if (argc != 3) {
  20.         printf("Numero errado de argumentos.\n");
  21.         printf("Usage: %s <directory> <filename>\n", argv[0]);
  22.         exit(1);
  23.     }
  24.  
  25.     char strFgets[BUFFER], wordsPath[BUFFER], temp[BUFFER], text[BUFFER];
  26.    
  27.     char file[BUFFER];
  28.     sprintf(file, "%s/words.txt", argv[1]);
  29.  
  30.     sprintf(temp, "%s/temp%s.txt", argv[1], argv[2]);
  31.    
  32.     char *number = malloc(strlen(argv[2]) + 1);
  33.  
  34.     if(number == NULL){
  35.        exit(1);
  36.     }
  37.     strcpy(number, argv[2]);
  38.  
  39.     char *dot = strrchr(number,'.');
  40.     *dot = '\0';
  41.  
  42.  
  43.     int myPipe[2];
  44.  
  45.     //abrir o ficheiro das palavras a procurar
  46.     FILE *words;
  47.     words = fopen(file,"r");
  48.  
  49.    
  50.     //verifica se o ficheiro existe
  51.     if(words == NULL){
  52.         perror("words.txt");
  53.         exit(1);
  54.         }
  55.        
  56.     fclose(words);
  57.  
  58.     char* outputFilename = malloc(strlen(argv[2]) + 1);
  59.     strcat(outputFilename, number);
  60.     strcat(outputFilename, ".sw");
  61.    
  62.     FILE * tempFile = fopen(outputFilename, "w");
  63.  
  64.     if(tempFile == NULL){
  65.         perror("temp.txt");
  66.         exit(1);
  67.         }
  68.  
  69.     //criar pipes
  70.     if (pipe (mypipe) == -1){
  71.         fprintf(stderr, "Erro ao criar pipes.\n");
  72.         exit(1);
  73.     }
  74.  
  75.     //pai
  76. if(pid > 0){
  77.        
  78.     close(mypipe[WRITE]);
  79.        
  80. //enquanto o ficheiro onde se encontram as palavras a procurar nao termina,            
  81. //continua a procura as palavras no ficheiro de texto passado como 2ΒΊ argumento
  82.     int read=1;
  83.        
  84.     while( (fgets(text, BUFFER, words))!= NULL){
  85.     text[strlen(text)-1]='\0';
  86.    
  87.     char buff[1024];
  88.     read =read(mypipe[READ],buff,1023);
  89.        
  90.         while(read != 0){
  91.        
  92.             while(buff[read] != '\0'){
  93.        
  94.             char *doubleDots = strchr(text, ':');
  95.          
  96.               if (doubleDots != NULL){
  97.                 char* line =readline(mypipe(read));
  98.                 fprintf(tempFile, "%s : %s-%s\n", doubleDots + 1, number, text);
  99.               }
  100.              
  101.             }
  102.         }
  103.        
  104.     }
  105.         //close(mypipe[READ]);
  106.     wait(NULL);
  107. }
  108.  
  109. else{
  110.     dup2(mypipe[WRITE], STDOUT_FILENO);
  111.     close(mypipe[READ]);
  112.     execlp("grep", "grep",  "-w", "-o", "-n", "-f", file, temp, NULL);
  113.     perror("grep");
  114.     exit(1);
  115. }
  116.  
  117. fclose(tempFile);
  118.  
  119.     return 0;
  120. }
  121.  
  122. char* readline(int fd){
  123.     char string[1024];
  124.     char currentByte;
  125.     int i = 0;
  126.  
  127.     while(read(fd, &currentByte, 1) > 0){
  128.         if(currentByte == '\n' || currentByte == '\0'){
  129.             break;
  130.         }
  131.  
  132.         string[i++] = currentByte;
  133.     }
  134.     string[i] = '\0';
  135.  
  136.     return string;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement