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
+40 −36
1 file changed
- M main.c +40 −36
| @@ -121,7 +121,12 @@ static bool xgetline(char **lineptr, size_t *n) | |||
| 121 | 121 | return len > 0; | |
| 122 | 122 | } | |
| 123 | 123 | ||
| 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) | |
| 125 | 130 | { | |
| 126 | 131 | char *path; | |
| 127 | 132 | ||
| @@ -149,6 +154,35 @@ static void check_add_file(char *filename, bool given) | |||
| 149 | 154 | fileidx++; | |
| 150 | 155 | } | |
| 151 | 156 | ||
| 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 | + | ||
| 152 | 186 | void remove_file(int n, bool manual) | |
| 153 | 187 | { | |
| 154 | 188 | if (n < 0 || n >= filecnt) | |
| @@ -804,11 +838,6 @@ static void run(void) | |||
| 804 | 838 | } | |
| 805 | 839 | } | |
| 806 | 840 | ||
| 807 | − | static int fncmp(const void *a, const void *b) | |
| 808 | − | { | |
| 809 | − | return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name); | |
| 810 | − | } | |
| 811 | − | ||
| 812 | 841 | static void sigchld(int sig) | |
| 813 | 842 | { | |
| 814 | 843 | while (waitpid(-1, NULL, WNOHANG) > 0); | |
| @@ -827,12 +856,9 @@ static void setup_signal(int sig, void (*handler)(int sig)) | |||
| 827 | 856 | ||
| 828 | 857 | int main(int argc, char *argv[]) | |
| 829 | 858 | { | |
| 830 | − | int i, start; | |
| 859 | + | int i; | |
| 831 | 860 | size_t n; | |
| 832 | − | char *filename; | |
| 833 | 861 | const char *homedir, *dsuffix = ""; | |
| 834 | − | struct stat fstats; | |
| 835 | − | r_dir_t dir; | |
| 836 | 862 | ||
| 837 | 863 | setup_signal(SIGCHLD, sigchld); | |
| 838 | 864 | setup_signal(SIGPIPE, SIG_IGN); | |
| @@ -861,37 +887,15 @@ int main(int argc, char *argv[]) | |||
| 861 | 887 | fileidx = 0; | |
| 862 | 888 | ||
| 863 | 889 | if (options->from_stdin) { | |
| 890 | + | char *filename = NULL; | |
| 864 | 891 | n = 0; | |
| 865 | − | filename = NULL; | |
| 866 | 892 | while (xgetline(&filename, &n)) | |
| 867 | − | check_add_file(filename, true); | |
| 893 | + | add_entry(filename); | |
| 868 | 894 | free(filename); | |
| 869 | 895 | } | |
| 870 | 896 | ||
| 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]); | |
| 895 | 899 | ||
| 896 | 900 | if (fileidx == 0) | |
| 897 | 901 | error(EXIT_FAILURE, 0, "No valid image file given, aborting"); | |