-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearcher.c
executable file
·68 lines (48 loc) · 1.59 KB
/
searcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "searcher.h"
#include <string.h>
#include <dirent.h>
#include "myFunctionalLib.h"
/*
* Browse the entire directory recursively to add each file's pointer in a list except "." and "..".
*/
int finderRecursive(listOfFiles* list){
//========== SET the complete Path ===========
char* completePATH = malloc(sizeof(char)*(strlen(list-> myFile -> myName) + strlen(list -> myFile -> myPath) + 2) );
strcpy(completePATH,list -> myFile -> myPath);
slashItCorrectly(&completePATH);
myStrCat(&completePATH, list-> myFile -> myName);
//========== TEST the disponibility of this Directory ===========
DIR* currDir;
currDir = opendir(completePATH);
if(currDir==NULL)
return 1; //Not allowed to visit it.
//========== SKIM the Folder ===========
struct dirent* dir;
listOfFiles* nextList;
while((dir =readdir(currDir))!=NULL){
if(dir -> d_type == DT_DIR){
//--- THIS IS A FOLDER ---
if (strcmp(dir -> d_name,"..")==0 ||strcmp(dir -> d_name,".")==0){
//Nothing Happen...
}else{
insertFile( create_File(dir -> d_name, completePATH, 0), list);
nextList = malloc(sizeof(listOfFiles));
myListOfFilesPtrCpy(&nextList, list);
while(nextList -> next != NULL)
myListOfFilesPtrCpy(&nextList, nextList -> next);
if(finderRecursive(nextList))
return 1; //A Folder was not available.
}
}else{
//--- THIS IS A FILE ---
insertFile( create_File(dir -> d_name, completePATH, 1), list);
}
}
closedir(currDir);
return 0;
}