-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
354 lines (301 loc) · 8.71 KB
/
main.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
File: main.c
Date: 10th November, 2019
Authors:
Pradyumna Y M PES1201700986
Anush V Kini PES1201701646
Sushanth Jain PES1201700992
*/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct Node
{
char *name;
struct Node **children;
int numents;
int type;
struct timespec last_mod;
} Node;
/*
Description: concatenate two strings in a new string, with new memory (To append two paths to get the absolute path)
Parameters: s1: string - string1 to be concatenated
s2: string - string2 to be concatenated
Returns: Char pointer - a pointer to the new string, which is a concatenation of the two strings
Assumptions: N/A
*/
char *concat(char *s1, char *s2)
{
char *ns = malloc(strlen(s1) + strlen(s2) + 1);
ns[0] = '\0';
strcat(ns, s1);
strcat(ns, s2);
return ns;
}
/*
Description: creates a copy of a string
Parameters: s1: string - string to be hard copied
Returns: Char pointer - pointer to the new string
Assumptions: N/A
*/
char *getcopy(char *s1)
{
char *ns = malloc(strlen(s1) + 1);
ns[0] = '\0';
strcat(ns, s1);
return ns;
}
/*
Description: allocate memory for a new node and initialize values, and returns a pointer.
Parameters: name: string - Name of the node (file or directory),
numents: integer - number of entries in the children array (The number of subfiles and directories),
type: integer - type of the node - Can take 3 values: (0: Directory, 1: File, 2: Symbolic Link)
Returns: Node pointer - pointer to the new node created
Assumptions: N/A
*/
Node *getNode(char *name, int numents, int type)
{
//allocate memory to the new node
Node *newnode = malloc(sizeof(Node));
//reduce the numents to remove the trivial nodes(. and ..)
numents -= 2;
//allocate pointers to the children
newnode->children = malloc(sizeof(Node *) * numents);
//make the child pointers null
for (int i = 0; i < numents; i++)
newnode->children[i] = 0;
//initialise values of the node
newnode->name = getcopy(name);
//printf("in getnode %s\n", name);
newnode->numents = numents;
newnode->type = type;
newnode->numents = newnode->numents < 0 ? 0 : newnode->numents;
return newnode;
}
/*
Description: Given a path to a directory, find the number of entries in the directory
(The number of entries refers to subfiles or subdirectories in a given directory)
Parameters: path: string - path to the directory
Returns: integer - number of entries in the directory
Assumptions: N/A
*/
int numberofentries(char *path)
{
//open the directory
DIR *dp = opendir(path); //path here later
struct dirent *d;
int nument = 0;
//increment the number of entries for each non-null node in the directory
while ((d = readdir(dp)) != NULL)
{
nument++;
}
//close the directory and return the number of entries
closedir(dp);
return nument;
}
/*
Description: comparator function, compares two nodes based on various conditions
Parameters:
n1: Node* pointer - points to the first node
n2- Node* pointere - points to the second node
order - an integer which defines the order in which the list should be sorted
Returns: 1 if n1>n2 else -1
Assumptions: N/A
*/
int compare(Node *n1, Node *n2, int order)
{
//self explanatory code..
switch (order)
{
case 0:
if (n1->last_mod.tv_nsec >= n2->last_mod.tv_nsec)
return 1;
else
return -1;
break;
case 1:
if (n1->last_mod.tv_nsec <= n2->last_mod.tv_nsec)
return 1;
else
return -1;
break;
case 2:
if (strcmp(n1->name, n2->name) >= 0)
return 1;
else
return -1;
break;
case 3:
if (strcmp(n1->name, n2->name) <= 0)
return 1;
else
return -1;
break;
}
}
/*
Description: populates the tree based on the directory structure
Parameters:
path: string - contains the path of the directory
node: Node* pointer - a pointer to the Node in the directory tree
order: defines the ordering to be followed while enumerating the list
Returns: N/A
Assumptions: N/A
*/
void populate(char *path, Node *node, int order)
{
//variable declarations
int i = 0;
DIR *dp = opendir(path); //open the directory at path
struct dirent *d;
struct stat statistics;
Node *temp;
//loop through the contents of the directory
while ((d = readdir(dp)) != NULL)
{
//skip the trivial contents of the directory
if ((strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0))
continue;
//define the path to the child node
char *newpath = concat(path, concat("/", d->d_name));
//check if it is a directory, if yes, create a new node and perform a recursive call
if (d->d_type == DT_DIR)
{
node->children[i] = getNode(d->d_name, numberofentries(newpath), 0);
populate(newpath, node->children[i], order);
//printf("here %s\n", newpath);
fflush(stdout);
}
else if (d->d_type == DT_LNK)
node->children[i] = getNode(d->d_name, 0, 2);
else
node->children[i] = getNode(d->d_name, 0, 1);
//get the stats of the node
stat(newpath, &statistics);
//record the last modified time of the node
node->children[i]->last_mod = statistics.st_mtim;
i++;
}
//sort the children nodes based on a particular order
for (i = 0; i < node->numents; i++)
{
for (int j = 0; j < node->numents - i - 1; j++)
{
if (compare(node->children[j], node->children[j + 1], order) == 1)
{
temp = node->children[j];
node->children[j] = node->children[j + 1];
node->children[j + 1] = temp;
}
}
}
closedir(dp);
}
/*
Description: prints the name of a node in the appropriate color based on the type of node(dir, file, symlink)
Parameters:
node: Node* pointer - a pointer to the node in the directory tree
Returns: N/A
Assumptions: N/A
*/
void print(Node *node)
{
//check the type of node and print it in the correct format
switch (node->type)
{
case 2:
//flag for printf to print it in a particular color
printf("\033[1;31m");
printf("%s\n", node->name);
//flag to reset the output format to normal
printf("\033[0m");
break;
case 0:
//flag for printf to print it in a particular color
printf("\033[01;33m");
printf("%s\n", node->name);
printf("\033[0m");
break;
case 1:
//flag for printf to print it in a particular color
printf("\033[1;32m");
printf("%s\n", node->name);
printf("\033[0m");
break;
}
}
/*
Description: Prints the directory tree
Parameters:
node: Node* pointer, points to the root of the directory tree
depth: integer pointer used for formatting the output
printcount: integer pointer used to control the output
Returns: N/A
Assumptions: N/A
*/
void inorder_print(Node *node, int *depth, int *printcount)
{
//if the node is a NULL return control
if (node == NULL)
return;
//stop printing if n lines are already printed.
if (*printcount == 10)
{
char c = getchar();
if (c == 'q')
exit(0);
*printcount = 0;
}
//print \t's for formatting the output
for (int i = 0; i < *depth - 1; i++)
{
printf("\t");
}
if (*depth != 0)
printf("|-------");
(*printcount)++;
//call the print function to print it in the appropriate format
print(node);
fflush(stdout);
//print the child nodes
for (int i = 0; i < node->numents; i++)
{
(*depth)++;
inorder_print(node->children[i], depth, printcount);
(*depth)--;
}
}
int main(int argc, char **argv)
{
int i = 0;
int depth = 0, printcount = 0;
char path[2000];
int order = 0;
if (argc == 2)
{
if (!strcmp(argv[1], "ma"))
order = 0;
else if (!strcmp(argv[1], "md"))
order = 1;
else if (!strcmp(argv[1], "na"))
order = 2;
else if (!strcmp(argv[1], "na"))
order = 3;
else
{
printf("Please check the paramters (na - name asc, nd - name desc, ma - last modified asc, md - last modified dsc)\n");
exit(0);
}
}
scanf("%s", path);
getchar();
Node *a = getNode(path, numberofentries(path), 0);
populate(path, a, order);
inorder_print(a, &depth, &printcount);
}