Krolyxon
Commit bbd7b7d

Branches

Refs →

nsxivbbd7b7d

New option: -r, open all images in directories

Author
Bert
Date
2011-02-02 10:34 +0100
Parent
2afb989
Commit
bbd7b7d595e41535e7152d3941f63e56e57b5641

+79 −14

5 files changed

  1. M README.md +1 −0
  2. M TODO +0 −1
  3. M main.c +69 −10
  4. M options.c +5 −2
  5. M sxiv.1 +4 −1
M README.md View
@@ -37,6 +37,7 @@ sxiv supports the following command-line options:
3737 (see section GEOMETRY SPECIFICATIONS of X(7))
3838 -p Pixelize, i.e. turn off image anti-aliasing
3939 -q Be quiet, disable warnings
40+ -r Search given directories recursively for images
4041 -s Scale all images to fit into window
4142 -v Print version information and exit
4243 -Z Same as `-z 100'
M TODO View
@@ -1,3 +1,2 @@
11 - key mappings for fit image to window and vice versa
22 - update filelist if image is removed or renamed while running
3−- view all images in directories (recursive mode)
M main.c View
@@ -18,6 +18,8 @@
1818
1919 #include <stdlib.h>
2020 #include <stdio.h>
21+#include <string.h>
22+#include <dirent.h>
2123 #include <sys/select.h>
2224 #include <sys/stat.h>
2325
@@ -39,7 +41,8 @@ void on_motionnotify(XEvent*);
3941 void on_configurenotify(XEvent*);
4042
4143 void update_title();
42−void read_dir(const char*);
44+void check_append(const char*);
45+void read_dir_rec(const char*);
4346
4447 static void (*handler[LASTEvent])(XEvent*) = {
4548 [KeyPress] = on_keypress,
@@ -118,16 +121,11 @@ int main(int argc, char **argv) {
118121 WARN("could not stat file: %s", filename);
119122 } else if (S_ISDIR(fstats.st_mode)) {
120123 if (options->recursive)
121− read_dir(filename);
124+ read_dir_rec(filename);
122125 else
123126 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;
127+ } else {
128+ check_append(filename);
131129 }
132130 }
133131
@@ -384,7 +382,68 @@ void update_title() {
384382 win_set_title(&win, win_title);
385383 }
386384
387−void read_dir(const char *dir) {
385+void check_append(const char *filename) {
386+ if (!filename)
387+ return;
388+
389+ if (img_check(filename)) {
390+ if (fileidx == filecnt) {
391+ filecnt *= 2;
392+ filenames = (const char**) s_realloc(filenames,
393+ filecnt * sizeof(const char*));
394+ }
395+ filenames[fileidx++] = filename;
396+ }
397+}
398+
399+void read_dir_rec(const char *dirname) {
400+ char *filename;
401+ const char **dirnames;
402+ int dircnt, diridx;
403+ unsigned char first;
404+ size_t len;
405+ DIR *dir;
406+ struct dirent *dentry;
407+ struct stat fstats;
408+
409+ if (!dirname)
410+ return;
411+
412+ dircnt = DNAME_CNT;
413+ diridx = first = 1;
414+ dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
415+ dirnames[0] = dirname;
416+
417+ while (diridx > 0) {
418+ dirname = dirnames[--diridx];
419+ if (!(dir = opendir(dirname)))
420+ DIE("could not open directory: %s", dirname);
421+ while ((dentry = readdir(dir))) {
422+ if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
423+ continue;
424+ len = strlen(dirname) + strlen(dentry->d_name) + 2;
425+ filename = (char*) s_malloc(len * sizeof(char));
426+ snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
427+ if (stat(filename, &fstats)) {
428+ WARN("could not stat file: %s", filename);
429+ free(filename);
430+ } else if (S_ISDIR(fstats.st_mode)) {
431+ if (diridx == dircnt) {
432+ dircnt *= 2;
433+ dirnames = (const char**) s_realloc(dirnames,
434+ dircnt * sizeof(const char*));
435+ }
436+ dirnames[diridx++] = filename;
437+ } else {
438+ check_append(filename);
439+ }
440+ }
441+ closedir(dir);
442+ if (!first)
443+ free((void*) dirname);
444+ else
445+ first = 0;
446+ }
388447 }
389448
390449 void* s_malloc(size_t size) {
M options.c View
@@ -29,7 +29,7 @@ options_t _options;
2929 const options_t *options = (const options_t*) &_options;
3030
3131 void print_usage() {
32− printf("usage: sxiv [-dFfhpqsvZ] [-g GEOMETRY] [-z ZOOM] FILES...\n");
32+ printf("usage: sxiv [-dFfhpqrsvZ] [-g GEOMETRY] [-z ZOOM] FILES...\n");
3333 }
3434
3535 void print_version() {
@@ -52,7 +52,7 @@ void parse_options(int argc, char **argv) {
5252 _options.quiet = 0;
5353 _options.recursive = 0;
5454
55− while ((opt = getopt(argc, argv, "dFfg:hpqsvZz:")) != -1) {
55+ while ((opt = getopt(argc, argv, "dFfg:hpqrsvZz:")) != -1) {
5656 switch (opt) {
5757 case '?':
5858 print_usage();
@@ -78,6 +78,9 @@ void parse_options(int argc, char **argv) {
7878 case 'q':
7979 _options.quiet = 1;
8080 break;
81+ case 'r':
82+ _options.recursive = 1;
83+ break;
8184 case 's':
8285 _options.scalemode = SCALE_FIT;
8386 break;
M sxiv.1 View
@@ -3,7 +3,7 @@
33 sxiv \- Simple (or small or suckless) X Image Viewer
44 .SH SYNOPSIS
55 .B sxiv
6−.RB [ \-dFfhpqsvZ ]
6+.RB [ \-dFfhpqrsvZ ]
77 .RB [ \-g
88 .IR GEOMETRY ]
99 .RB [ \-z
@@ -41,6 +41,9 @@ Pixelize images, i.e. turn off anti-aliasing.
4141 .B \-q
4242 Be quiet, disable warnings to standard error stream.
4343 .TP
44+.B \-r
45+Search the given directories recursively for images to view.
46+.TP
4447 .B \-s
4548 Scale all images to fit into window.
4649 .TP