From 2afb989cc415419229d6f6ded02d59ee109ac796 Mon Sep 17 00:00:00 2001 From: Bert Date: Wed, 2 Feb 2011 09:01:05 +0100 Subject: [PATCH] Skeleton for -r --- Makefile | 2 +- main.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++------ options.c | 1 + options.h | 1 + 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 4c78d88..5ef4755 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: sxiv -VERSION=0.4 +VERSION=git-20110202 CC?=gcc PREFIX?=/usr/local diff --git a/main.c b/main.c index 96cf10e..766312d 100644 --- a/main.c +++ b/main.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,9 @@ #include "image.h" #include "window.h" +void* s_malloc(size_t); +void* s_realloc(void*, size_t); + void on_keypress(XEvent*); void on_buttonpress(XEvent*); void on_buttonrelease(XEvent*); @@ -35,6 +39,7 @@ void on_motionnotify(XEvent*); void on_configurenotify(XEvent*); void update_title(); +void read_dir(const char*); static void (*handler[LASTEvent])(XEvent*) = { [KeyPress] = on_keypress, @@ -47,6 +52,8 @@ static void (*handler[LASTEvent])(XEvent*) = { img_t img; win_t win; +#define DNAME_CNT 512 +#define FNAME_CNT 4096 const char **filenames; int filecnt, fileidx; @@ -87,6 +94,8 @@ void run() { int main(int argc, char **argv) { int i; + const char *filename; + struct stat fstats; parse_options(argc, argv); @@ -95,17 +104,36 @@ int main(int argc, char **argv) { exit(1); } - if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*)))) - DIE("could not allocate memory"); - + if (options->recursive) + filecnt = FNAME_CNT; + else + filecnt = options->filecnt; + + filenames = (const char**) s_malloc(filecnt * sizeof(const char*)); fileidx = 0; - filecnt = 0; for (i = 0; i < options->filecnt; ++i) { - if (img_check(options->filenames[i])) - filenames[filecnt++] = options->filenames[i]; + filename = options->filenames[i]; + if (stat(filename, &fstats)) { + WARN("could not stat file: %s", filename); + } else if (S_ISDIR(fstats.st_mode)) { + if (options->recursive) + read_dir(filename); + else + WARN("ignoring directory: %s", filename); + } else if (img_check(filename)) { + if (fileidx == filecnt) { + filecnt *= 2; + filenames = (const char**) s_realloc(filenames, + filecnt * sizeof(const char*)); + } + filenames[fileidx++] = filename; + } } + filecnt = fileidx; + fileidx = 0; + if (!filecnt) { fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); exit(1); @@ -355,3 +383,20 @@ void update_title() { win_set_title(&win, win_title); } + +void read_dir(const char *dir) { +} + +void* s_malloc(size_t size) { + void *ptr; + + if (!(ptr = malloc(size))) + DIE("could not allocate memory"); + return ptr; +} + +void* s_realloc(void *ptr, size_t size) { + if (!(ptr = realloc(ptr, size))) + DIE("could not allocate memory"); + return ptr; +} diff --git a/options.c b/options.c index 38bc59e..bb608e5 100644 --- a/options.c +++ b/options.c @@ -50,6 +50,7 @@ void parse_options(int argc, char **argv) { _options.geometry = NULL; _options.quiet = 0; + _options.recursive = 0; while ((opt = getopt(argc, argv, "dFfg:hpqsvZz:")) != -1) { switch (opt) { diff --git a/options.h b/options.h index 19a0c6b..3cb4b38 100644 --- a/options.h +++ b/options.h @@ -34,6 +34,7 @@ typedef struct options_s { char *geometry; unsigned char quiet; + unsigned char recursive; } options_t; extern const options_t *options;