| @@ -19,6 +19,7 @@ |
| 19 | 19 | | #include <stdlib.h> |
| 20 | 20 | | #include <stdio.h> |
| 21 | 21 | | #include <sys/select.h> |
| 22 | + | #include <sys/stat.h> |
| 22 | 23 | | |
| 23 | 24 | | #include <X11/Xlib.h> |
| 24 | 25 | | #include <X11/Xutil.h> |
| @@ -28,6 +29,9 @@ |
| 28 | 29 | | #include "image.h" |
| 29 | 30 | | #include "window.h" |
| 30 | 31 | | |
| 32 | + | void* s_malloc(size_t); |
| 33 | + | void* s_realloc(void*, size_t); |
| 34 | + | |
| 31 | 35 | | void on_keypress(XEvent*); |
| 32 | 36 | | void on_buttonpress(XEvent*); |
| 33 | 37 | | void on_buttonrelease(XEvent*); |
| @@ -35,6 +39,7 @@ void on_motionnotify(XEvent*); |
| 35 | 39 | | void on_configurenotify(XEvent*); |
| 36 | 40 | | |
| 37 | 41 | | void update_title(); |
| 42 | + | void read_dir(const char*); |
| 38 | 43 | | |
| 39 | 44 | | static void (*handler[LASTEvent])(XEvent*) = { |
| 40 | 45 | | [KeyPress] = on_keypress, |
| @@ -47,6 +52,8 @@ static void (*handler[LASTEvent])(XEvent*) = { |
| 47 | 52 | | img_t img; |
| 48 | 53 | | win_t win; |
| 49 | 54 | | |
| 55 | + | #define DNAME_CNT 512 |
| 56 | + | #define FNAME_CNT 4096 |
| 50 | 57 | | const char **filenames; |
| 51 | 58 | | int filecnt, fileidx; |
| 52 | 59 | | |
| @@ -87,6 +94,8 @@ void run() { |
| 87 | 94 | | |
| 88 | 95 | | int main(int argc, char **argv) { |
| 89 | 96 | | int i; |
| 97 | + | const char *filename; |
| 98 | + | struct stat fstats; |
| 90 | 99 | | |
| 91 | 100 | | parse_options(argc, argv); |
| 92 | 101 | | |
| @@ -95,17 +104,36 @@ int main(int argc, char **argv) { |
| 95 | 104 | | exit(1); |
| 96 | 105 | | } |
| 97 | 106 | | |
| 98 | | − | if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*)))) |
| 99 | | − | DIE("could not allocate memory"); |
| 100 | | − | |
| 107 | + | if (options->recursive) |
| 108 | + | filecnt = FNAME_CNT; |
| 109 | + | else |
| 110 | + | filecnt = options->filecnt; |
| 111 | + | |
| 112 | + | filenames = (const char**) s_malloc(filecnt * sizeof(const char*)); |
| 101 | 113 | | fileidx = 0; |
| 102 | | − | filecnt = 0; |
| 103 | 114 | | |
| 104 | 115 | | for (i = 0; i < options->filecnt; ++i) { |
| 105 | | − | if (img_check(options->filenames[i])) |
| 106 | | − | filenames[filecnt++] = options->filenames[i]; |
| 116 | + | filename = options->filenames[i]; |
| 117 | + | if (stat(filename, &fstats)) { |
| 118 | + | WARN("could not stat file: %s", filename); |
| 119 | + | } else if (S_ISDIR(fstats.st_mode)) { |
| 120 | + | if (options->recursive) |
| 121 | + | read_dir(filename); |
| 122 | + | else |
| 123 | + | WARN("ignoring directory: %s", filename); |
| 124 | + | } else if (img_check(filename)) { |
| 125 | + | if (fileidx == filecnt) { |
| 126 | + | filecnt *= 2; |
| 127 | + | filenames = (const char**) s_realloc(filenames, |
| 128 | + | filecnt * sizeof(const char*)); |
| 129 | + | } |
| 130 | + | filenames[fileidx++] = filename; |
| 131 | + | } |
| 107 | 132 | | } |
| 108 | 133 | | |
| 134 | + | filecnt = fileidx; |
| 135 | + | fileidx = 0; |
| 136 | + | |
| 109 | 137 | | if (!filecnt) { |
| 110 | 138 | | fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); |
| 111 | 139 | | exit(1); |
| @@ -355,3 +383,20 @@ void update_title() { |
| 355 | 383 | | |
| 356 | 384 | | win_set_title(&win, win_title); |
| 357 | 385 | | } |
| 386 | + | |
| 387 | + | void read_dir(const char *dir) { |
| 388 | + | } |
| 389 | + | |
| 390 | + | void* s_malloc(size_t size) { |
| 391 | + | void *ptr; |
| 392 | + | |
| 393 | + | if (!(ptr = malloc(size))) |
| 394 | + | DIE("could not allocate memory"); |
| 395 | + | return ptr; |
| 396 | + | } |
| 397 | + | |
| 398 | + | void* s_realloc(void *ptr, size_t size) { |
| 399 | + | if (!(ptr = realloc(ptr, size))) |
| 400 | + | DIE("could not allocate memory"); |
| 401 | + | return ptr; |
| 402 | + | } |