Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <dirent.h>
- #define START_FOLDER "./labyrinth"
- void minoPasses(char** filenames, char* Pass, char* wordToSearch, int* counter){
- DIR* curDir = opendir(Pass);
- struct dirent* curFile = readdir(curDir);
- while(curFile){
- if (strcmp(curFile->d_name, ".") == 0 || strcmp(curFile->d_name, "..") == 0) {
- curFile = readdir(curDir);
- continue;
- }
- else if(strstr(curFile->d_name, ".txt") == NULL){
- char* newPass = calloc(strlen(Pass)+strlen(curFile->d_name)+2, sizeof(char));
- strcat(newPass, Pass);
- strcat(newPass, "/");
- strcat(newPass, curFile->d_name);
- minoPasses(filenames, newPass, wordToSearch, counter);
- free(newPass);
- }
- else{
- char buf[100];
- char* newPass = calloc(strlen(Pass)+strlen(curFile->d_name)+2, sizeof(char));
- strcat(newPass, Pass);
- strcat(newPass, "/");
- strcat(newPass, curFile->d_name);
- FILE* fp = fopen(newPass, "r");
- while(fgets(buf, 100, fp)){
- if((strstr(buf, wordToSearch) != NULL) && (strstr(buf, "Deadlock") == NULL)){
- if (strcmp(curFile->d_name, "file.txt") != 0) {
- minoPasses(filenames, START_FOLDER, curFile->d_name, counter);
- }
- filenames[*counter] = newPass;
- (*counter)++;
- }
- }
- fclose(fp);
- }
- curFile = readdir(curDir);
- }
- closedir(curDir);
- }
- int main()
- {
- char **filenames = calloc(3000, sizeof(char *));
- int counter = 0;
- minoPasses(filenames, START_FOLDER, "Minotaur", &counter);
- FILE *resultFile = fopen("./result.txt", "w");
- int j = 0;
- int i = 0;
- for (i; i < counter; i++){
- fprintf(resultFile, "%s\n", filenames[i]);
- }
- for(j; j<counter; j++){
- free(filenames[j]);
- }
- fclose(resultFile);
- free(filenames);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement