Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Read filenames from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert committed Feb 14, 2011
1 parent ea65610 commit 26cc5af
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: sxiv

VERSION=git-20110209
VERSION=git-20110214

CC?=gcc
PREFIX?=/usr/local
Expand Down
27 changes: 17 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,30 @@ int main(int argc, char **argv) {
exit(1);
}

if (options->recursive)
if (options->recursive || options->from_stdin)
filecnt = FNAME_CNT;
else
filecnt = options->filecnt;

filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
fileidx = 0;

for (i = 0; i < options->filecnt; ++i) {
filename = options->filenames[i];
if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
if (options->recursive)
read_dir_rec(filename);
else
warn("ignoring directory: %s", filename);
} else {
check_append(filename);
if (options->from_stdin) {
while ((filename = readline(stdin))) {
if (!check_append(filename))
free((void*) filename);
}
} else {
for (i = 0; i < options->filecnt; ++i) {
filename = options->filenames[i];
if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
if (options->recursive)
read_dir_rec(filename);
else
warn("ignoring directory: %s", filename);
} else {
check_append(filename);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define _XOPEN_SOURCE

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

Expand Down Expand Up @@ -106,4 +107,6 @@ void parse_options(int argc, char **argv) {

_options.filenames = (const char**) argv + optind;
_options.filecnt = argc - optind;
_options.from_stdin = _options.filecnt == 1 &&
strcmp(_options.filenames[0], "-") == 0;
}
1 change: 1 addition & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
typedef struct options_s {
const char **filenames;
int filecnt;
unsigned char from_stdin;

scalemode_t scalemode;
float zoom;
Expand Down
40 changes: 39 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "options.h"
#include "util.h"

#define FNAME_LEN 10

void cleanup();

void* s_malloc(size_t size) {
Expand Down Expand Up @@ -75,3 +77,39 @@ void size_readable(float *size, const char **unit) {
*size /= 1024;
*unit = units[MIN(i, LEN(units) - 1)];
}

char* readline(FILE *stream) {
size_t len;
char *buf, *s, *end;

if (!stream || feof(stream) || ferror(stream))
return NULL;

len = FNAME_LEN;
s = buf = (char*) s_malloc(len * sizeof(char));

do {
*s = '\0';
fgets(s, len - (s - buf), stream);
if ((end = strchr(s, '\n'))) {
*end = '\0';
} else if (strlen(s) + 1 == len - (s - buf)) {
buf = (char*) s_realloc(buf, 2 * len * sizeof(char));
s = buf + len - 1;
len *= 2;
} else {
s += strlen(s);
}
} while (!end && !feof(stream) && !ferror(stream));

if (!ferror(stream) && *buf) {
s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char));
strcpy(s, buf);
} else {
s = NULL;
}

free(buf);

return s;
}
3 changes: 3 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef UTIL_H
#define UTIL_H

#include <stdio.h>
#include <stdarg.h>

#define ABS(a) ((a) < 0 ? (-(a)) : (a))
Expand All @@ -34,4 +35,6 @@ void die(const char*, ...);

void size_readable(float*, const char**);

char* readline(FILE*);

#endif /* UTIL_H */

0 comments on commit 26cc5af

Please sign in to comment.