summaryrefslogtreecommitdiff
path: root/src/search.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.c')
-rw-r--r--src/search.c64
1 files changed, 44 insertions, 20 deletions
diff --git a/src/search.c b/src/search.c
index 3488136..00ad141 100644
--- a/src/search.c
+++ b/src/search.c
@@ -99,37 +99,61 @@ struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dire
99 return start; 99 return start;
100} 100}
101 101
102int main(void) { 102struct nodelist* scanfiles(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)) {
103 struct nodelist *folders = NULL; 103 struct nodelist *folders = NULL, *files = NULL, *start = files;
104 folders = scanfolders("./", alphasort); 104 folders = scanfolders(STARTPATH, cmp);
105 105
106 struct dirent **nodes; 106 struct dirent **nodes;
107 int n; 107 int n = 0;
108 108
109 char *restoredir = NULL; 109 char *restoredir = NULL;
110 restoredir = get_current_dir_name(); 110 restoredir = get_current_dir_name();
111 111 if(restoredir == NULL) {
112 char *actualpath = NULL; 112 error(0, errno, "Could not get path to current working dir");
113 nodelist_delete(folders);
114 return NULL;
115 }
113 116
114 for(struct nodelist *p = folders; p != NULL; p = p->next) { 117 for(struct nodelist *p = folders; p != NULL; p = p->next) {
115 chdir(p->fullpath); 118 if(chdir(p->fullpath) < 0) {
116 n = scandir(p->fullpath, &nodes, filesort, alphasort); 119 error(0, errno, "Couldn't enter dir \"%s\", skipping...", p->fullpath);
120 continue;
121 }
122
123 n = scandir(p->fullpath, &nodes, filesort, cmp);
117 if(n >= 0) { 124 if(n >= 0) {
118 int cnt; 125 for(int i = 0; i < n; i++) {
119 for(cnt = 0; cnt < n; ++cnt) { 126 if(files == NULL) {
120 actualpath = realpath(nodes[cnt]->d_name, NULL); 127 files = nodelist_init(NULL);
121 puts(actualpath); 128 start = files;
122 free(actualpath); 129 }
123 }
124 130
125 } else 131 files->fullpath = realpath(nodes[i]->d_name, NULL);
126 perror("Couldn't open the directory"); 132
133 printf("%s\n", files->fullpath);
134
135 files->next = nodelist_init(NULL);
136 files = files->next;
137 }
138 } else {
139 error(0, errno, "Could not scan dir \"%s\", skipping...", p->fullpath);
140 continue;
141 }
127 } 142 }
128 //*/
129 143
130 nodelist_delete(folders); 144 return start;
145}
131 146
132 chdir(restoredir); 147/*
148int main(void) {
149 struct nodelist *files = scanfiles("./", alphasort);
150
151 printf("\nafter scan\n");
152 for(struct nodelist *p = files; p != NULL; p = p->next) {
153 if(p->fullpath != NULL) // Annoying extra "null" entry at the end of the list
154 printf("%s\n", p->fullpath);
155 }
133 156
134 return 0; 157 return 0;
135} 158}
159*/ \ No newline at end of file