-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
275 lines (249 loc) · 6.6 KB
/
main.cpp
File metadata and controls
275 lines (249 loc) · 6.6 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <infos.h>
// global counters for number of files within tree, and number of directories within tree
int filecount;
int dircount;
/**
* checks for the location of the character within a 'string'
* @param string : array of characters
* @param check : value we are searching character array for
* @return integer value demsontrating the position of the character in the 'string'
*/
int contains (const char *string,char check){
int count=0;
while(*string) {
count++;
if (*string==check){
return count;
}
}
return 0;
}
/**
* Checks if filename follows the rules required by the regex pattern and hence should be opened and printed
* @param filename :name of file which we are checking against the regex
* @param pattern : regex pattern that describes the filenames which we are allowed to display
* @return returns true if file follows the regex rules and should be printed and/or opened, returns false if not
*/
bool follows_rules (const char *filename, const char *pattern){
int length = strlen(pattern);
if (strlen(filename)>strlen(pattern)){
length = strlen(filename);
}
for (int i =0; i<length;i++)
{
if (pattern[i]=='(') {
while (pattern[i]!=')'){
if (pattern[i]=='-'){
}
else{
}
i++;
}
}
else if (pattern[i]!=filename[i] && (pattern[i+1]!='*'|| pattern[i+1]!='?')){
return false;
}
else if (pattern[i+1]=='?'){
if (i==0){
i++;
}
else if (filename[i] != pattern[i] && (((pattern[i-1]!='*')||(pattern[i-1]!='?')) &&pattern[i-1]!=filename[i])){
return false;
}
}
else if (pattern[i+1]=='*'){
if (i==0){
int j = i;
while (filename[i]==pattern[j]){
i++;
}
i++;
}
else if ((filename[i] == pattern[i] || filename[i] == pattern[i-1])){
int j = i;
while (filename[i]==pattern[j]){
i++;
}
i++;
}
else{
return false;
}
}
}
return true;
}
/**
* checks if the file is the last in the directory
* @param path_check : path we are searching through in order
* @param name : name of file we are attempting to locate the location of
* @return returns true if the file is the final in the directory, false if not
*
*/
bool checkLastEntry(const char* path_check, const char*name){
HDIR dir = opendir(path_check, 0);
if (is_error(dir)) {
return false;
}
int check = 0;
struct dirent de;
while(readdir(dir, &de)) {
if (strcmp(de.name, name)==0){
check = 1;
}
else{
check=0;
}
}
if (check == 1) {
return true;
}
return false;
}
/**
* prints preceding spaces or lines, demonstrating files structure with indentation.
*@param path: path which we are starting from
*@param originalPath: path which we are stopping at, dont want to look further back than.(e.g /usr)
*/
void add_spaces(const char *path, const char *originalPath)
{
char path_check[strlen(path)];
char name[strlen(path)];
char spaces[100];
int count=0;
for (int i = strlen(path); i >=strlen(originalPath); i--){
if (path[i]=='/') {
for(int x =0;x<strlen(path);x++){
path_check[x]=NULL;
name[x]=NULL;
}
for (int j = 0;j<i;j++){
path_check[j]=path[j];
}
for (int q = i+1; q<=strlen(path);q++){
if (path[q] == '/'){
break;
}
else{
name[q-(i+1)]=path[q];
}
}
if (checkLastEntry(path_check,name)){
for (int i =0;i<3;i++){
spaces[count]=' ';
count++;
}
}
else
{
for (int i =0;i<2;i++){
spaces[count]=' ';
count++;
}
spaces[count]='|';
count++;
}
}
}
for(int i =count-1;i>=0;i--){
printf("%c",spaces[i]);
}
}
/**
* main recursive function for accessing and printing all required files and directories
* @param path: current directory path which we are checking and exploring through
* @param pattern: pattern of regex if applicable
* @param originalPath: path from which this function is first called, and is called within the cmdline
* @param countcheck: integer value to demonstrate when function has been called for the last time recursively.
*/
void print_directory (const char *path, const char *pattern, const char *originalPath,int countcheck)
{
HDIR dir = opendir(path, 0);
if (is_error(dir)) {
filecount++;
//move back a recursive step as the file is unopenable hence not a directory
return;
}
countcheck++;
dircount++;
struct dirent de;
while(readdir(dir, &de)) {
if (follows_rules(de.name, pattern) || pattern==""){
add_spaces(path,originalPath);
printf("|--- %s \n", de.name);
//string comprehension for appending new filename onto current path for next recursive call
char next_path[strlen(path)+strlen(de.name)+2];
for (int i = 0; i < strlen(path); i++) {
next_path[i]=path[i];
}
next_path[strlen(path)]='/';
for (int j = strlen(path)+1; j < strlen(de.name)+strlen(path)+1; j++) {
next_path[j]=de.name[j-strlen(path)-1];
}
next_path[strlen(path)+strlen(de.name)+2] ='\0';
print_directory(next_path, pattern, originalPath,countcheck);
}
}
countcheck--;
if (countcheck==0){
printf("%d directories, %d files \n",dircount-1,filecount);
}
return;
}
int main(const char *cmdline)
{
const char *path;
if (!cmdline || strlen(cmdline) == 0) {
path = "/usr";
}
else {
path = cmdline;
char new_path[strlen(cmdline)];
const char* patternstart = "-P";
char printpath[strlen(patternstart)];
for (int i = 0; i < strlen(path); i++) {
printpath[0]=path[i];
printpath[1]=path[i+1];
//check if pattern has occured
if (strcmp(printpath,patternstart)==0) {
for (int q = 0; q < i-1; q++) {
new_path[q] = cmdline[q]; //setting directory path to be character array before '-P' occurance
}
if (strlen(new_path)<1) {
path="/usr";
}
else {
path = new_path;
}
//setting regex pattern to be characters in array after '-P' occurance
char pattern[strlen(cmdline) - strlen(patternstart) - i];
for (int j = strlen(patternstart)+i; j <= strlen(cmdline); j++) {
pattern[j-strlen(patternstart)-i]=cmdline[j+1];
}
HDIR dir = opendir(path, 0);
if (is_error(dir)) {
printf("Unable to open directory '%s' for reading.\n", path);
return 1;
}
printf("Directory Listing of '%s':\n", path);
printf("Using Pattern '%s':\n", pattern);
print_directory(path, pattern,path,0);
closedir(dir);
return 0;
}
else {
printpath[strlen(path)]={0};
}
}
}
HDIR dir = opendir(path, 0);
if (is_error(dir)) {
printf("Unable to open directory '%s' for reading.\n", path);
return 1;
}
printf("Directory Listing of '%s':\n", path);
//no pattern so recursion must be called as such
print_directory(path, "",path,0);
closedir(dir);
return 0;
}