summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ll.c1
-rw-r--r--src/ll.h1
-rw-r--r--src/search.c84
3 files changed, 81 insertions, 5 deletions
diff --git a/src/ll.c b/src/ll.c
index 1a0426b..21e413e 100644
--- a/src/ll.c
+++ b/src/ll.c
@@ -18,7 +18,6 @@ struct nodelist* nodelist_init(struct nodelist *start) {
18 18
19 start->fullpath = NULL; 19 start->fullpath = NULL;
20 start->next = NULL; 20 start->next = NULL;
21 start->type = NODELIST_TYPE__UNDEF;
22 21
23 return start; 22 return start;
24} 23}
diff --git a/src/ll.h b/src/ll.h
index 9470ea6..72d6593 100644
--- a/src/ll.h
+++ b/src/ll.h
@@ -7,7 +7,6 @@
7struct nodelist { 7struct nodelist {
8 struct nodelist *next; 8 struct nodelist *next;
9 char *fullpath; 9 char *fullpath;
10 int type;
11}; 10};
12 11
13// Initializes a nodelist object. Returns a pointer to START on success, and NULL on error. If START is null, malloc is called to populate it 12// Initializes a nodelist object. Returns a pointer to START on success, and NULL on error. If START is null, malloc is called to populate it
diff --git a/src/search.c b/src/search.c
index 070801a..eb39e08 100644
--- a/src/search.c
+++ b/src/search.c
@@ -47,7 +47,7 @@ int S_ISDIR_WRAPPER(mode_t mode) {
47} 47}
48 48
49int S_ISREG_WRAPPER(mode_t mode) { 49int S_ISREG_WRAPPER(mode_t mode) {
50 return S_ISREG(mode); 50 return (S_ISREG(mode) && !S_ISDIR(mode));
51} 51}
52 52
53int foldersort(const struct dirent *node) { 53int foldersort(const struct dirent *node) {
@@ -58,6 +58,46 @@ int filesort(const struct dirent *node) {
58 return nodesort(node, S_ISREG_WRAPPER); 58 return nodesort(node, S_ISREG_WRAPPER);
59} 59}
60 60
61struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)) {
62 struct dirent **nodes = NULL;
63
64 char *restoredir = NULL;
65 restoredir = get_current_dir_name();
66 if(restoredir == NULL) {
67 error(0, errno, "Couldn't get the current working dir to restore from later");
68 return NULL;
69 }
70
71 struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner;
72 scanner->fullpath = realpath(STARTPATH, NULL);
73
74 for(; scanner != NULL; scanner = scanner->next) {
75 if(chdir(scanner->fullpath) < 0) {
76 error(0, errno, "Couldn't change to dir \"%s\". Skipping scan of that folder", scanner->fullpath);
77 continue;
78 }
79
80 int n = scandir(scanner->fullpath, &nodes, foldersort, cmp);
81 if(n >= 0) {
82 for(int i = 0; i < n; i++) {
83 holder = nodelist_init(NULL);
84 holder->fullpath = realpath(nodes[i]->d_name, NULL);
85 nodelist_append(scanner, holder); // Yeah this is slow but idk rn
86 }
87 } else {
88 error(0, errno, "Couldn't open dir \"%s\"", scanner->fullpath);
89 return NULL;
90 }
91
92 }
93
94 if(chdir(restoredir) < 0) {
95 error(0, errno, "Couldn't restore to proper working dir");
96 return NULL;
97 }
98
99 return start;
100}
61 101
62int main(void) { 102int main(void) {
63 /* 103 /*
@@ -78,11 +118,11 @@ int main(void) {
78 perror("Couldn't open the directory"); 118 perror("Couldn't open the directory");
79 //*/ 119 //*/
80 120
121 /*
81 struct dirent **nodes = NULL; 122 struct dirent **nodes = NULL;
82 123
83 struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; 124 struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner;
84 scanner->fullpath = realpath("./", NULL); 125 scanner->fullpath = realpath("./", NULL);
85 scanner->type = NODELIST_TYPE__FOLDER;
86 126
87 for(; scanner != NULL; scanner = scanner->next) { 127 for(; scanner != NULL; scanner = scanner->next) {
88 if(chdir(scanner->fullpath) < 0) { 128 if(chdir(scanner->fullpath) < 0) {
@@ -95,7 +135,6 @@ int main(void) {
95 for(int i = 0; i < n; i++) { 135 for(int i = 0; i < n; i++) {
96 holder = nodelist_init(NULL); 136 holder = nodelist_init(NULL);
97 holder->fullpath = realpath(nodes[i]->d_name, NULL); 137 holder->fullpath = realpath(nodes[i]->d_name, NULL);
98 holder->type = NODELIST_TYPE__FOLDER;
99 nodelist_append(scanner, holder); // Yeah this is slow but idk rn 138 nodelist_append(scanner, holder); // Yeah this is slow but idk rn
100 } 139 }
101 } else 140 } else
@@ -109,6 +148,45 @@ int main(void) {
109 } 148 }
110 149
111 nodelist_delete(start); 150 nodelist_delete(start);
151 //*/
152
153 struct nodelist *folders = NULL;
154
155 folders = scanfolders("./", alphasort);
156
157 /*
158 for(struct nodelist *p = folders; p != NULL; p = p->next)
159 printf("%s\n", p->fullpath);
160 */
161
162 //*
163 struct dirent **nodes;
164 int n;
165
166 char *restoredir = NULL;
167 restoredir = get_current_dir_name();
168
169 char *actualpath = NULL;
170
171 for(struct nodelist *p = folders; p != NULL; p = p->next) {
172 chdir(p->fullpath);
173 n = scandir(p->fullpath, &nodes, filesort, alphasort);
174 if(n >= 0) {
175 int cnt;
176 for(cnt = 0; cnt < n; ++cnt) {
177 actualpath = realpath(nodes[cnt]->d_name, NULL);
178 puts(actualpath);
179 free(actualpath);
180 }
181
182 } else
183 perror("Couldn't open the directory");
184 }
185 //*/
186
187 nodelist_delete(folders);
188
189 chdir(restoredir);
112 190
113 return 0; 191 return 0;
114} 192}