59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
int my_grep(char *filename, char *expr){
|
|
|
|
FILE *file = fopen(filename, "r");
|
|
if(!file) return 2;
|
|
|
|
char buffer[2048];
|
|
int found = 0;
|
|
while(fgets(buffer, sizeof(buffer), file)){
|
|
|
|
if(strstr(buffer, expr) != NULL){
|
|
printf("%s", buffer);
|
|
|
|
if(buffer[strlen(buffer) - 1] != '\n') printf("\n");
|
|
|
|
found ++;
|
|
}
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
if (found != 0) return 1;
|
|
return 0;
|
|
}
|
|
/*
|
|
int main()
|
|
{
|
|
printf("\n%d\n", my_grep("example.txt", "word"));
|
|
// Expected:
|
|
//This line contains words.
|
|
//And "words" contains "word".
|
|
//word
|
|
//1
|
|
|
|
|
|
putchar('\n');
|
|
|
|
printf("%d\n", my_grep("example.txt", "eqlijgber")); // Returns 0
|
|
printf("%d\n", my_grep("empty_file.txt", "word")); // Returns 0
|
|
printf("%d\n", my_grep("inexisting_file.txt", "word")); // Returns 2
|
|
|
|
putchar('\n');
|
|
|
|
// FLAG
|
|
//Try "grepping" the flag.csv file, by the credit card numbers you found in
|
|
//the previous flag.
|
|
//Then, only keep those which booked several entries.
|
|
|
|
my_grep("flag.csv", "Put here the credit card numbers");
|
|
my_grep("flag.csv", "Put here the credit card numbers");
|
|
my_grep("flag.csv", "Put here the credit card numbers");
|
|
my_grep("flag.csv", "Put here the credit card numbers");
|
|
my_grep("flag.csv", "Put here the credit card numbers");
|
|
|
|
return 0;
|
|
}
|
|
*/
|