Krolyxon
Commit 045abbd

Branches

Refs →

nsxiv045abbd

accept directory via stdin (-i) (#383)

this basically just extracts the logic that was previously inside
`main()` into a seperate function `add_entry()` so that it can be used
for accepting entries form stdin as well.

Closes: https://codeberg.org/nsxiv/nsxiv/issues/382
Author
NRK
Date
2022-10-30 19:18 +0100
Parent
3140638
Commit
045abbd117dc327a8636a5afd19252c53d6880aa

+40 −36

1 file changed

  1. M main.c +40 −36
M main.c View
@@ -121,7 +121,12 @@ static bool xgetline(char **lineptr, size_t *n)
121121 return len > 0;
122122 }
123123
124−static void check_add_file(char *filename, bool given)
124+static int fncmp(const void *a, const void *b)
125+{
126+ return strcoll(((fileinfo_t *)a)->name, ((fileinfo_t *)b)->name);
127+}
128+
129+static void check_add_file(const char *filename, bool given)
125130 {
126131 char *path;
127132
@@ -149,6 +154,35 @@ static void check_add_file(char *filename, bool given)
149154 fileidx++;
150155 }
151156
157+static void add_entry(const char *entry_name)
158+{
159+ int start;
160+ char *filename;
161+ struct stat fstats;
162+ r_dir_t dir;
163+
164+ if (stat(entry_name, &fstats) < 0) {
165+ error(0, errno, "%s", entry_name);
166+ return;
167+ }
168+ if (!S_ISDIR(fstats.st_mode)) {
169+ check_add_file(entry_name, true);
170+ } else {
171+ if (r_opendir(&dir, entry_name, options->recursive) < 0) {
172+ error(0, errno, "%s", entry_name);
173+ return;
174+ }
175+ start = fileidx;
176+ while ((filename = r_readdir(&dir, true)) != NULL) {
177+ check_add_file(filename, false);
178+ free(filename);
179+ }
180+ r_closedir(&dir);
181+ if (fileidx - start > 1)
182+ qsort(files + start, fileidx - start, sizeof(*files), fncmp);
183+ }
184+}
185+
152186 void remove_file(int n, bool manual)
153187 {
154188 if (n < 0 || n >= filecnt)
@@ -804,11 +838,6 @@ static void run(void)
804838 }
805839 }
806840
807−static int fncmp(const void *a, const void *b)
808−{
809− return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
810−}
811−
812841 static void sigchld(int sig)
813842 {
814843 while (waitpid(-1, NULL, WNOHANG) > 0);
@@ -827,12 +856,9 @@ static void setup_signal(int sig, void (*handler)(int sig))
827856
828857 int main(int argc, char *argv[])
829858 {
830− int i, start;
859+ int i;
831860 size_t n;
832− char *filename;
833861 const char *homedir, *dsuffix = "";
834− struct stat fstats;
835− r_dir_t dir;
836862
837863 setup_signal(SIGCHLD, sigchld);
838864 setup_signal(SIGPIPE, SIG_IGN);
@@ -861,37 +887,15 @@ int main(int argc, char *argv[])
861887 fileidx = 0;
862888
863889 if (options->from_stdin) {
890+ char *filename = NULL;
864891 n = 0;
865− filename = NULL;
866892 while (xgetline(&filename, &n))
867− check_add_file(filename, true);
893+ add_entry(filename);
868894 free(filename);
869895 }
870896
871− for (i = 0; i < options->filecnt; i++) {
872− filename = options->filenames[i];
873−
874− if (stat(filename, &fstats) < 0) {
875− error(0, errno, "%s", filename);
876− continue;
877− }
878− if (!S_ISDIR(fstats.st_mode)) {
879− check_add_file(filename, true);
880− } else {
881− if (r_opendir(&dir, filename, options->recursive) < 0) {
882− error(0, errno, "%s", filename);
883− continue;
884− }
885− start = fileidx;
886− while ((filename = r_readdir(&dir, true)) != NULL) {
887− check_add_file(filename, false);
888− free((void*) filename);
889− }
890− r_closedir(&dir);
891− if (fileidx - start > 1)
892− qsort(files + start, fileidx - start, sizeof(*files), fncmp);
893− }
894− }
897+ for (i = 0; i < options->filecnt; i++)
898+ add_entry(options->filenames[i]);
895899
896900 if (fileidx == 0)
897901 error(EXIT_FAILURE, 0, "No valid image file given, aborting");