Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 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.         char strFgets[BUFFER], wordsPath[BUFFER], temp[BUFFER], text[BUFFER];
  17.         char file[BUFFER];
  18.         int pid,fd[2];
  19.  
  20.         sprintf(file, "%s/words.txt", argv[1]);
  21.  
  22.         sprintf(temp, "%s/temp%s.txt", argv[1], argv[2]);
  23.  
  24.         char *number = malloc(strlen(argv[2]) + 1);
  25.  
  26.         if (number == NULL){
  27.                 exit(1);
  28.         }
  29.         strcpy(number, argv[2]);
  30.  
  31.         char *dot = strrchr(number, '.');
  32.         *dot = '\0';
  33.  
  34.         int linenumber;
  35.         int myPipe[2];
  36.  
  37.         //Open words files
  38.         FILE *words;
  39.         words = fopen(file, "r");
  40.  
  41.         //check if file exist
  42.         if (words == NULL){
  43.                 perror("words.txt");
  44.                 exit(1);
  45.         }
  46.  
  47.         fclose(words);
  48.  
  49.         char* outputFilename = malloc(strlen(argv[2]) + 1);
  50.         strcat(outputFilename, number);
  51.         strcat(outputFilename, ".sw");
  52.  
  53.         FILE * tempFile = fopen(outputFilename, "w");
  54.  
  55.         if (tempFile == NULL){
  56.                 perror("temp.txt");
  57.                 exit(1);
  58.         }
  59.  
  60.         //creating pipes
  61.         if (pipe(myPipe) == -1)
  62.         {
  63.                 fprintf(stderr, "Error creating pipes.\n");
  64.                 exit(1);
  65.         }
  66.  
  67.         if (pid > 0)
  68.         {
  69.                 close(myPipe[WRITE]);
  70.                 wait(NULL);
  71.  
  72.                 while ((fgets(text, BUFFER, words)) != NULL)
  73.                 {
  74.                         char* line = readline(myPipe[READ]);
  75.  
  76.                         while (line != '\0')
  77.                         {
  78.                                 if (sscanf(line, "%d:%s", &linenumber, text) > 0)
  79.                                 {
  80.                                         fprintf(tempFile, "%s : %s-%d\n", text, number, linenumber);
  81.                                 }
  82.  
  83.                                 line = readline(fd[READ]);
  84.                         }
  85.                 }
  86.  
  87.                 close(myPipe[READ]);
  88.         }
  89.         else if (pid == 0)
  90.         {
  91.                 dup2(myPipe[WRITE], STDOUT_FILENO);
  92.                 close(myPipe[READ]);
  93.                 execlp("grep", "grep", "-w", "-o", "-n", "-f", file, temp, NULL);
  94.                 perror("grep");
  95.                 exit(1);
  96.         }
  97.         else // fork failed
  98.         {
  99.  
  100.         }
  101.         fclose(tempFile);
  102.  
  103.         return 0;
  104. }
  105.  
  106. char* readline(int fd)
  107. {
  108.         char string[1024];
  109.         char currentByte;
  110.         int i = 0;
  111.  
  112.         while (read(fd, &currentByte, 1) > 0)
  113.         {
  114.                 if (currentByte == '\n' || currentByte == '\0')
  115.                 {
  116.                         break;
  117.                 }
  118.  
  119.                 string[i++] = currentByte;
  120.         }
  121.  
  122.         string[i] = '\0';
  123.  
  124.         return string;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement