Krolyxon
Commit 26cc5af

Branches

Refs →

nsxiv26cc5af

Read filenames from stdin

Author
Bert
Date
2011-02-14 17:51 +0100
Parent
ea65610
Commit
26cc5aff69856caf01a925ee1357b3650d806d67

+64 −12

6 files changed

  1. M Makefile +1 −1
  2. M main.c +17 −10
  3. M options.c +3 −0
  4. M options.h +1 −0
  5. M util.c +39 −1
  6. M util.h +3 −0
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=git-20110209
3+VERSION=git-20110214
44
55 CC?=gcc
66 PREFIX?=/usr/local
M main.c View
@@ -81,7 +81,7 @@ int main(int argc, char **argv) {
8181 exit(1);
8282 }
8383
84− if (options->recursive)
84+ if (options->recursive || options->from_stdin)
8585 filecnt = FNAME_CNT;
8686 else
8787 filecnt = options->filecnt;
@@ -89,15 +89,22 @@ int main(int argc, char **argv) {
8989 filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
9090 fileidx = 0;
9191
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+ }
101108 }
102109 }
103110
M options.c View
@@ -19,6 +19,7 @@
1919 #define _XOPEN_SOURCE
2020
2121 #include <stdlib.h>
22+#include <string.h>
2223 #include <stdio.h>
2324 #include <unistd.h>
2425
@@ -106,4 +107,6 @@ void parse_options(int argc, char **argv) {
106107
107108 _options.filenames = (const char**) argv + optind;
108109 _options.filecnt = argc - optind;
110+ _options.from_stdin = _options.filecnt == 1 &&
111+ strcmp(_options.filenames[0], "-") == 0;
109112 }
M options.h View
@@ -24,6 +24,7 @@
2424 typedef struct options_s {
2525 const char **filenames;
2626 int filecnt;
27+ unsigned char from_stdin;
2728
2829 scalemode_t scalemode;
2930 float zoom;
M util.c View
@@ -17,11 +17,13 @@
1717 */
1818
1919 #include <stdlib.h>
20−#include <stdio.h>
20+#include <string.h>
2121
2222 #include "options.h"
2323 #include "util.h"
2424
25+#define FNAME_LEN 10
26+
2527 void cleanup();
2628
2729 void* s_malloc(size_t size) {
@@ -75,3 +77,39 @@ void size_readable(float *size, const char **unit) {
7577 *size /= 1024;
7678 *unit = units[MIN(i, LEN(units) - 1)];
7779 }
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+}
M util.h View
@@ -19,6 +19,7 @@
1919 #ifndef UTIL_H
2020 #define UTIL_H
2121
22+#include <stdio.h>
2223 #include <stdarg.h>
2324
2425 #define ABS(a) ((a) < 0 ? (-(a)) : (a))
@@ -34,4 +35,6 @@ void die(const char*, ...);
3435
3536 void size_readable(float*, const char**);
3637
38+char* readline(FILE*);
39+
3740 #endif /* UTIL_H */