-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
134 lines (103 loc) · 2.83 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "flags/flags.h"
#include "flags/setFlags.h"
#include "file/checkFileType.h"
#include "file/fileListing.h"
#include "get/getDir.h"
#include "get/getFile.h"
#include "print/printListing.h"
#include "queue/dirQueue.h"
#include "sort/sortArray.h"
#include "get/recurDir.h"
#ifdef DEBUG_MODE_ENABLED
#include "flags/checkFlags.h"
#endif
// initialize global flags variable
uint32_t app_flags = 0;
int main(int argc, char *argv[]) {
Queue q;
initQueue(&q);
char *path;
struct fileListing *fl_arr;
fl_arr = malloc(sizeof(struct fileListing));
int file_count = 0;
int filetype = 0;
/* This function will check if the effective user Id is 0 and
* will set the A_FLAG if true
*/
setEuidFlags(geteuid());
if (argc == 1) {
if (app_flags & d_FLAG) {
if (getFile(".", &fl_arr, &file_count) != 0) {
perror("error getting file");
}
} else {
//getDir(".");
enqueue(&q, ".");
}
} else if (argc > 1) {
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
setFlags(argv[i]);
if (argc == 2) {
if (app_flags & d_FLAG) {
if (getFile(".", &fl_arr, &file_count) != 0) {
perror("error getting file");
}
} else {
enqueue(&q, ".");
//getDir(".");
}
}
continue;
}
if ((filetype = checkFileType(argv[i])) == -1) {
perror("Couldn't load file: ");
char msg[275];
snprintf(msg, sizeof(msg), "Could not load %s: ", argv[i]);
perror(msg);
}
if (filetype == S_IFDIR) {
if (app_flags & d_FLAG) {
if (getFile(argv[i], &fl_arr, &file_count) != 0) {
perror("error getting file");
}
} else {
//getDir(argv[i]);
enqueue(&q, argv[i]);
}
} else if (filetype == S_IFREG) {
if (getFile(argv[i], &fl_arr, &file_count) != 0) {
perror("error getting file");
}
}
else if(filetype == S_IFLNK){
if(getFile(argv[i], &fl_arr, &file_count) != 0){
perror("error getting symlink file: ");
}
}
}
}
sortArray(&fl_arr, file_count);
printListing(fl_arr, file_count);
free(fl_arr);
while ((path = dequeue(&q)) != NULL){
if(app_flags & R_FLAG){
recurDir(path);
} else{
struct fileListing *queue_fl_arr = malloc(sizeof(struct fileListing));
int queue_file_count = 0;
getDir(path, &queue_fl_arr, &queue_file_count);
sortArray(&queue_fl_arr, queue_file_count);
printListing(queue_fl_arr, queue_file_count);
free(path);
free(queue_fl_arr);
}
}
return 0;
}