nsxiv26cc5af
Read filenames from stdin
+64 −12
6 files changed
- M Makefile +1 −1
- M main.c +17 −10
- M options.c +3 −0
- M options.h +1 −0
- M util.c +39 −1
- M util.h +3 −0
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | all: sxiv | |
| 2 | 2 | ||
| 3 | − | VERSION=git-20110209 | |
| 3 | + | VERSION=git-20110214 | |
| 4 | 4 | ||
| 5 | 5 | CC?=gcc | |
| 6 | 6 | PREFIX?=/usr/local | |
| @@ -81,7 +81,7 @@ int main(int argc, char **argv) { | |||
| 81 | 81 | exit(1); | |
| 82 | 82 | } | |
| 83 | 83 | ||
| 84 | − | if (options->recursive) | |
| 84 | + | if (options->recursive || options->from_stdin) | |
| 85 | 85 | filecnt = FNAME_CNT; | |
| 86 | 86 | else | |
| 87 | 87 | filecnt = options->filecnt; | |
| @@ -89,15 +89,22 @@ int main(int argc, char **argv) { | |||
| 89 | 89 | filenames = (const char**) s_malloc(filecnt * sizeof(const char*)); | |
| 90 | 90 | fileidx = 0; | |
| 91 | 91 | ||
| 92 | − | for (i = 0; i < options->filecnt; ++i) { | |
| 93 | − | filename = options->filenames[i]; | |
| 94 | − | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { | |
| 95 | − | if (options->recursive) | |
| 96 | − | read_dir_rec(filename); | |
| 97 | − | else | |
| 98 | − | warn("ignoring directory: %s", filename); | |
| 99 | − | } else { | |
| 100 | − | check_append(filename); | |
| 92 | + | if (options->from_stdin) { | |
| 93 | + | while ((filename = readline(stdin))) { | |
| 94 | + | if (!check_append(filename)) | |
| 95 | + | free((void*) filename); | |
| 96 | + | } | |
| 97 | + | } else { | |
| 98 | + | for (i = 0; i < options->filecnt; ++i) { | |
| 99 | + | filename = options->filenames[i]; | |
| 100 | + | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { | |
| 101 | + | if (options->recursive) | |
| 102 | + | read_dir_rec(filename); | |
| 103 | + | else | |
| 104 | + | warn("ignoring directory: %s", filename); | |
| 105 | + | } else { | |
| 106 | + | check_append(filename); | |
| 107 | + | } | |
| 101 | 108 | } | |
| 102 | 109 | } | |
| 103 | 110 | ||
| @@ -19,6 +19,7 @@ | |||
| 19 | 19 | #define _XOPEN_SOURCE | |
| 20 | 20 | ||
| 21 | 21 | #include <stdlib.h> | |
| 22 | + | #include <string.h> | |
| 22 | 23 | #include <stdio.h> | |
| 23 | 24 | #include <unistd.h> | |
| 24 | 25 | ||
| @@ -106,4 +107,6 @@ void parse_options(int argc, char **argv) { | |||
| 106 | 107 | ||
| 107 | 108 | _options.filenames = (const char**) argv + optind; | |
| 108 | 109 | _options.filecnt = argc - optind; | |
| 110 | + | _options.from_stdin = _options.filecnt == 1 && | |
| 111 | + | strcmp(_options.filenames[0], "-") == 0; | |
| 109 | 112 | } | |
| @@ -24,6 +24,7 @@ | |||
| 24 | 24 | typedef struct options_s { | |
| 25 | 25 | const char **filenames; | |
| 26 | 26 | int filecnt; | |
| 27 | + | unsigned char from_stdin; | |
| 27 | 28 | ||
| 28 | 29 | scalemode_t scalemode; | |
| 29 | 30 | float zoom; | |
| @@ -17,11 +17,13 @@ | |||
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | 19 | #include <stdlib.h> | |
| 20 | − | #include <stdio.h> | |
| 20 | + | #include <string.h> | |
| 21 | 21 | ||
| 22 | 22 | #include "options.h" | |
| 23 | 23 | #include "util.h" | |
| 24 | 24 | ||
| 25 | + | #define FNAME_LEN 10 | |
| 26 | + | ||
| 25 | 27 | void cleanup(); | |
| 26 | 28 | ||
| 27 | 29 | void* s_malloc(size_t size) { | |
| @@ -75,3 +77,39 @@ void size_readable(float *size, const char **unit) { | |||
| 75 | 77 | *size /= 1024; | |
| 76 | 78 | *unit = units[MIN(i, LEN(units) - 1)]; | |
| 77 | 79 | } | |
| 80 | + | ||
| 81 | + | char* readline(FILE *stream) { | |
| 82 | + | size_t len; | |
| 83 | + | char *buf, *s, *end; | |
| 84 | + | ||
| 85 | + | if (!stream || feof(stream) || ferror(stream)) | |
| 86 | + | return NULL; | |
| 87 | + | ||
| 88 | + | len = FNAME_LEN; | |
| 89 | + | s = buf = (char*) s_malloc(len * sizeof(char)); | |
| 90 | + | ||
| 91 | + | do { | |
| 92 | + | *s = '\0'; | |
| 93 | + | fgets(s, len - (s - buf), stream); | |
| 94 | + | if ((end = strchr(s, '\n'))) { | |
| 95 | + | *end = '\0'; | |
| 96 | + | } else if (strlen(s) + 1 == len - (s - buf)) { | |
| 97 | + | buf = (char*) s_realloc(buf, 2 * len * sizeof(char)); | |
| 98 | + | s = buf + len - 1; | |
| 99 | + | len *= 2; | |
| 100 | + | } else { | |
| 101 | + | s += strlen(s); | |
| 102 | + | } | |
| 103 | + | } while (!end && !feof(stream) && !ferror(stream)); | |
| 104 | + | ||
| 105 | + | if (!ferror(stream) && *buf) { | |
| 106 | + | s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char)); | |
| 107 | + | strcpy(s, buf); | |
| 108 | + | } else { | |
| 109 | + | s = NULL; | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | free(buf); | |
| 113 | + | ||
| 114 | + | return s; | |
| 115 | + | } | |
| @@ -19,6 +19,7 @@ | |||
| 19 | 19 | #ifndef UTIL_H | |
| 20 | 20 | #define UTIL_H | |
| 21 | 21 | ||
| 22 | + | #include <stdio.h> | |
| 22 | 23 | #include <stdarg.h> | |
| 23 | 24 | ||
| 24 | 25 | #define ABS(a) ((a) < 0 ? (-(a)) : (a)) | |
| @@ -34,4 +35,6 @@ void die(const char*, ...); | |||
| 34 | 35 | ||
| 35 | 36 | void size_readable(float*, const char**); | |
| 36 | 37 | ||
| 38 | + | char* readline(FILE*); | |
| 39 | + | ||
| 37 | 40 | #endif /* UTIL_H */ | |