Advertisement
clucasg

main.c

Apr 1st, 2021
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7.  
  8. bool isFilePath(char *string){
  9.     return access(string, F_OK) == 0 ? true : false;
  10. }
  11.  
  12.  
  13. int main(int argc, char **argv){
  14.     char filepath[20];
  15.    
  16.     bool isFile = false;
  17.     bool hasC = false;
  18.     bool hasI = false;
  19.  
  20.     int searchedWordIndex;
  21.     for (int i = 1; i < argc; i++) {
  22.         // printf("arg: %s\n",argv[i]);
  23.         // printf("i: %d\n",i);
  24.         if(strcmp("-c",argv[i]) == 0){
  25.             // puts("entrou no primeiro");
  26.             hasC = true;
  27.             continue;
  28.         }
  29.         if(strcmp("-i",argv[i]) == 0){
  30.             // puts("entrou no segundo");
  31.             hasI = true;
  32.             continue;
  33.         }
  34.         if(isFilePath(argv[i])){
  35.             // puts("entrou no terceiro");
  36.             isFile = true;
  37.             strcpy(filepath, argv[i]);
  38.             continue;
  39.         }
  40.         // puts("chegou no quarto");
  41.         searchedWordIndex = i;
  42.     }
  43.  
  44.     int searchedWordLength = strlen(argv[searchedWordIndex]);
  45.     char searchedWord[searchedWordLength];
  46.  
  47.     strcpy(searchedWord,argv[searchedWordIndex]);
  48.     // printf("Palava procurada é %s na posição %d e tem tamanho %d\n",searchedWord,searchedWordIndex,searchedWordLength);
  49.  
  50.     int count = 0;
  51.     FILE *input = stdin;
  52.     if (isFile)
  53.     {
  54.         input = fopen(filepath, "r");
  55.         if(input==NULL){
  56.             printf("[ERRO AO ABRIR O ARQUIVO]\n");
  57.             exit(1);
  58.         }
  59.     }
  60.     char line[256];
  61.     char loweredLine[256];
  62.     fgets(line, sizeof(line), input);
  63.     while (fgets(line, sizeof(line), input)) {
  64.         strcpy(loweredLine,line);
  65.         if (hasI)
  66.             for (int i = 0; i < strlen(line); i++)
  67.                 loweredLine[i]=tolower(loweredLine[i]);
  68.         // printf("Procurando em: %s",loweredLine);
  69.         if(strstr(loweredLine, searchedWord)){
  70.             count++;
  71.             if(!hasC)
  72.                 printf("%s",line);
  73.         }
  74.     }
  75.  
  76.     if(hasC)
  77.         printf("%d\n",count);
  78.  
  79.     if(isFile)
  80.         fclose(input);
  81.     return 0;
  82. }
  83.  
  84. char *lowerString(char* text, int text_size){
  85.     char *lowered;
  86.    
  87.     for(int i = 0; i < text_size; i++){
  88.         lowered[i] = tolower(text[i]);
  89.     }
  90.  
  91.     return lowered;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement