Krolyxon
Commit e999688

Branches

Refs →

nsxive999688

Moved read_dir_rec into util.c

Author
Bert
Date
2011-04-07 19:15 +0200
Parent
7d40fae
Commit
e9996882cb55c5b6974a3448f29bda32d6aa373d

+91 −76

3 files changed

  1. M main.c +10 −76
  2. M util.c +79 −0
  3. M util.h +2 −0
M main.c View
@@ -19,7 +19,6 @@
1919 #include <stdlib.h>
2020 #include <stdio.h>
2121 #include <string.h>
22−#include <dirent.h>
2322 #include <sys/select.h>
2423 #include <sys/stat.h>
2524 #include <sys/time.h>
@@ -48,7 +47,6 @@ typedef enum {
4847
4948 void update_title();
5049 int check_append(const char*);
51−void read_dir_rec(const char*);
5250 void run();
5351
5452 appmode_t mode;
@@ -56,7 +54,6 @@ img_t img;
5654 tns_t tns;
5755 win_t win;
5856
59−#define DNAME_CNT 512
6057 #define FNAME_CNT 1024
6158 const char **filenames;
6259 int filecnt, fileidx;
@@ -96,8 +93,9 @@ int load_image(int new) {
9693 }
9794
9895 int main(int argc, char **argv) {
99− int i;
96+ int i, j;
10097 const char *filename;
98+ char **fnames;
10199 struct stat fstats;
102100
103101 parse_options(argc, argv);
@@ -124,10 +122,15 @@ int main(int argc, char **argv) {
124122 for (i = 0; i < options->filecnt; ++i) {
125123 filename = options->filenames[i];
126124 if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
127− if (options->recursive)
128− read_dir_rec(filename);
129− else
125+ if (options->recursive && (fnames = read_dir_rec(filename))) {
126+ for (j = 0; fnames[j]; ++j) {
127+ if (!check_append(fnames[j]))
128+ free(fnames[j]);
129+ }
130+ free(fnames);
131+ } else {
130132 warn("ignoring directory: %s", filename);
133+ }
131134 } else {
132135 check_append(filename);
133136 }
@@ -215,75 +218,6 @@ int check_append(const char *filename) {
215218 }
216219 }
217220
218−int fncmp(const void *a, const void *b) {
219− return strcoll(*((char* const*) a), *((char* const*) b));
220−}
221−
222−void read_dir_rec(const char *dirname) {
223− char *filename;
224− const char **dirnames;
225− int dircnt, diridx;
226− int fcnt, fstart;
227− unsigned char first;
228− size_t len;
229− DIR *dir;
230− struct dirent *dentry;
231− struct stat fstats;
232−
233− if (!dirname)
234− return;
235−
236− dircnt = DNAME_CNT;
237− diridx = first = 1;
238− dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
239− dirnames[0] = dirname;
240−
241− fcnt = 0;
242− fstart = fileidx;
243−
244− while (diridx > 0) {
245− dirname = dirnames[--diridx];
246− if (!(dir = opendir(dirname))) {
247− warn("could not open directory: %s", dirname);
248− } else {
249− while ((dentry = readdir(dir))) {
250− if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
251− continue;
252−
253− len = strlen(dirname) + strlen(dentry->d_name) + 2;
254− filename = (char*) s_malloc(len * sizeof(char));
255− snprintf(filename, len, "%s%s%s", dirname,
256− dirname[strlen(dirname)-1] == '/' ? "" : "/", dentry->d_name);
257−
258− if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
259− if (diridx == dircnt) {
260− dircnt *= 2;
261− dirnames = (const char**) s_realloc(dirnames,
262− dircnt * sizeof(const char*));
263− }
264− dirnames[diridx++] = filename;
265− } else {
266− if (check_append(filename))
267− ++fcnt;
268− else
269− free(filename);
270− }
271− }
272− closedir(dir);
273− }
274−
275− if (!first)
276− free((void*) dirname);
277− else
278− first = 0;
279− }
280−
281− if (fcnt > 1)
282− qsort(filenames + fstart, fcnt, sizeof(char*), fncmp);
283−
284− free(dirnames);
285−}
286−
287221 #ifdef EXT_COMMANDS
288222 int run_command(const char *cline, Bool reload) {
289223 int fncnt, fnlen;
M util.c View
@@ -18,6 +18,7 @@
1818
1919 #include <stdlib.h>
2020 #include <string.h>
21+#include <dirent.h>
2122 #include <sys/types.h>
2223 #include <sys/stat.h>
2324 #include <unistd.h>
@@ -26,6 +27,8 @@
2627 #include "options.h"
2728 #include "util.h"
2829
30+#define DNAME_CNT 512
31+#define FNAME_CNT 1024
2932 #define FNAME_LEN 1024
3033
3134 void cleanup();
@@ -199,6 +202,82 @@ int create_dir_rec(const char *path) {
199202 return err;
200203 }
201204
205+int fncmp(const void *a, const void *b) {
206+ return strcoll(*((char* const*) a), *((char* const*) b));
207+}
208+
209+char** read_dir_rec(const char *dirname) {
210+ int dcnt, didx, fcnt, fidx;
211+ char **dnames, **fnames, *filename;
212+ unsigned char first;
213+ size_t len;
214+ DIR *dir;
215+ struct dirent *dentry;
216+ struct stat fstats;
217+
218+ if (!dirname)
219+ return NULL;
220+
221+ dcnt = DNAME_CNT;
222+ didx = first = 1;
223+ dnames = (char**) s_malloc(dcnt * sizeof(char*));
224+ dnames[0] = (char*) dirname;
225+
226+ fcnt = FNAME_CNT;
227+ fidx = 0;
228+ fnames = (char**) s_malloc(fcnt * sizeof(char*));
229+
230+ while (didx > 0) {
231+ dirname = dnames[--didx];
232+ if (!(dir = opendir(dirname))) {
233+ warn("could not open directory: %s", dirname);
234+ } else {
235+ while ((dentry = readdir(dir))) {
236+ if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
237+ continue;
238+
239+ len = strlen(dirname) + strlen(dentry->d_name) + 2;
240+ filename = (char*) s_malloc(len * sizeof(char));
241+ snprintf(filename, len, "%s%s%s", dirname,
242+ dirname[strlen(dirname)-1] == '/' ? "" : "/", dentry->d_name);
243+
244+ if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
245+ if (didx == dcnt) {
246+ dcnt *= 2;
247+ dnames = (char**) s_realloc(dnames, dcnt * sizeof(char*));
248+ }
249+ dnames[didx++] = filename;
250+ } else {
251+ if (fidx + 1 == fcnt) {
252+ fcnt *= 2;
253+ fnames = (char**) s_realloc(fnames, fcnt * sizeof(char*));
254+ }
255+ fnames[fidx++] = filename;
256+ }
257+ }
258+ closedir(dir);
259+ }
260+
261+ if (!first)
262+ free((void*) dirname);
263+ else
264+ first = 0;
265+ }
266+
267+ if (!fidx) {
268+ free(fnames);
269+ fnames = NULL;
270+ } else {
271+ if (fidx > 1)
272+ qsort(fnames, fidx, sizeof(char*), fncmp);
273+ fnames[fidx] = NULL;
274+ }
275+
276+ free(dnames);
277+
278+ return fnames;
279+}
280+
202281 char* readline(FILE *stream) {
203282 size_t len;
204283 char *buf, *s, *end;
M util.h View
@@ -44,7 +44,9 @@ void die(const char*, ...);
4444 void size_readable(float*, const char**);
4545
4646 char* absolute_path(const char*);
47+
4748 int create_dir_rec(const char*);
49+char** read_dir_rec(const char*);
4850
4951 char* readline(FILE*);
5052