Krolyxon
Commit 2afb989

Branches

Refs →

nsxiv2afb989

Skeleton for -r

Author
Bert
Date
2011-02-02 09:01 +0100
Parent
e510eda
Commit
2afb989cc415419229d6f6ded02d59ee109ac796

+54 −7

4 files changed

  1. M Makefile +1 −1
  2. M main.c +51 −6
  3. M options.c +1 −0
  4. M options.h +1 −0
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=0.4
3+VERSION=git-20110202
44
55 CC?=gcc
66 PREFIX?=/usr/local
M main.c View
@@ -19,6 +19,7 @@
1919 #include <stdlib.h>
2020 #include <stdio.h>
2121 #include <sys/select.h>
22+#include <sys/stat.h>
2223
2324 #include <X11/Xlib.h>
2425 #include <X11/Xutil.h>
@@ -28,6 +29,9 @@
2829 #include "image.h"
2930 #include "window.h"
3031
32+void* s_malloc(size_t);
33+void* s_realloc(void*, size_t);
34+
3135 void on_keypress(XEvent*);
3236 void on_buttonpress(XEvent*);
3337 void on_buttonrelease(XEvent*);
@@ -35,6 +39,7 @@ void on_motionnotify(XEvent*);
3539 void on_configurenotify(XEvent*);
3640
3741 void update_title();
42+void read_dir(const char*);
3843
3944 static void (*handler[LASTEvent])(XEvent*) = {
4045 [KeyPress] = on_keypress,
@@ -47,6 +52,8 @@ static void (*handler[LASTEvent])(XEvent*) = {
4752 img_t img;
4853 win_t win;
4954
55+#define DNAME_CNT 512
56+#define FNAME_CNT 4096
5057 const char **filenames;
5158 int filecnt, fileidx;
5259
@@ -87,6 +94,8 @@ void run() {
8794
8895 int main(int argc, char **argv) {
8996 int i;
97+ const char *filename;
98+ struct stat fstats;
9099
91100 parse_options(argc, argv);
92101
@@ -95,17 +104,36 @@ int main(int argc, char **argv) {
95104 exit(1);
96105 }
97106
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*));
101113 fileidx = 0;
102− filecnt = 0;
103114
104115 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+ }
107132 }
108133
134+ filecnt = fileidx;
135+ fileidx = 0;
136+
109137 if (!filecnt) {
110138 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
111139 exit(1);
@@ -355,3 +383,20 @@ void update_title() {
355383
356384 win_set_title(&win, win_title);
357385 }
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+}
M options.c View
@@ -50,6 +50,7 @@ void parse_options(int argc, char **argv) {
5050 _options.geometry = NULL;
5151
5252 _options.quiet = 0;
53+ _options.recursive = 0;
5354
5455 while ((opt = getopt(argc, argv, "dFfg:hpqsvZz:")) != -1) {
5556 switch (opt) {
M options.h View
@@ -34,6 +34,7 @@ typedef struct options_s {
3434 char *geometry;
3535
3636 unsigned char quiet;
37+ unsigned char recursive;
3738 } options_t;
3839
3940 extern const options_t *options;