-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseek.c
More file actions
132 lines (104 loc) · 3.31 KB
/
seek.c
File metadata and controls
132 lines (104 loc) · 3.31 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "headers.h"
char* nametarget;
char* sdirectory;
int fflag;
int dflag;
int eflag;
int filecount;
char firstfile[MAX_FILE_NAME];
//DIDNT COLOR
int FileFind(const char* path, const struct stat * filestat, int typeflag, struct FTW * nftstruct)
{
//printf("In filefind path: %s\n", path);
int mydirlen = strlen(sdirectory);
//printf("strcmp: %d\n", strncmp(nametarget, path + nftstruct->base, sizeof(nametarget)));
if (strncmp(nametarget, path + nftstruct->base, strlen(nametarget)) == 0)
{
//printf("%s\n", path + nftstruct->base);
// printf("%s\n", nametarget);
// printf("name length: %ld\n", strlen(nametarget));
int curlen = strlen(path);
// printf("mydirlen: %d\n", mydirlen);
// printf("curlen: %d\n", curlen);
if (filecount == 0)
{
firstfile[0] = '\0';
strcat(firstfile, path);
//printf("first files path: %s\n", firstfilefound);
}
filecount++;
if(!S_ISDIR(filestat->st_mode) && dflag == 0) //file, check d flag 0 COLOR GREEN
{
printf(GREEN"."reset);
printf(GREEN"%s"reset, (path)+mydirlen);
printf(GREEN"\n"reset);
}
if(S_ISDIR(filestat->st_mode) && fflag == 0 && nftstruct->level != 0) //dir, check f flag 0 COLOR BLUE
{
printf(BLUE"."reset);
printf(BLUE"%s"reset, (path)+mydirlen);
printf(BLUE"\n"reset);
}
}
return 0;
}
void Seek(Command cmd, char* target_name, char* searchdirectory, char* invokeddir, char** pwd, int f_flag, int d_flag, int e_flag)
{
nametarget = target_name;
sdirectory = searchdirectory;
fflag = f_flag;
dflag = d_flag;
eflag = e_flag;
filecount = 0;
if (nftw(searchdirectory, FileFind, 10, FTW_PHYS | FTW_ACTIONRETVAL) == -1)
{
perror(RED"nftw"reset);
}
if(filecount == 0)
{
fprintf(stderr, RED"No match found!\n"reset);
return;
}
//printf("%d\n", filecount);
if(filecount == 1 && eflag == 1)
{
struct stat* fstat = malloc(sizeof(struct stat));
stat(firstfile, fstat);
if(!S_ISDIR(fstat->st_mode) && dflag == 1) //file, check d flag 0 COLOR GREEN
{
fprintf(stderr, RED"No matches found!\n"reset);
return;
}
else if(S_ISREG(fstat->st_mode)) //execute file
{
//printf("HI\n");
FILE* fileptr = fopen(firstfile, "r");
if(fileptr == NULL)
{
fprintf(stderr, RED"Missing permissions for task!\n"reset);
return;
}
char *line = NULL;
size_t len = 0;
ssize_t read;
while(read = getline(&line, &len, fileptr) != -1)
{
printf("%s", line);
}
}
else if(S_ISDIR(fstat->st_mode) && fflag == 1)
{
fprintf(stderr, RED"No matches found!\n"reset);
return;
}
else if(S_ISDIR(fstat->st_mode)) //chdir
{
*pwd = getcwd(NULL, MAX_PATH_LEN);
if(chdir(firstfile) != 0)
{
perror(RED"chdir"reset);
return;
}
}
}
}