Преглед на файлове

Read filenames from stdin

master
Bert преди 14 години
родител
ревизия
26cc5aff69
променени са 6 файла, в които са добавени 64 реда и са изтрити 12 реда
  1. +1
    -1
      Makefile
  2. +17
    -10
      main.c
  3. +3
    -0
      options.c
  4. +1
    -0
      options.h
  5. +39
    -1
      util.c
  6. +3
    -0
      util.h

+ 1
- 1
Makefile Целия файл

@@ -1,6 +1,6 @@
all: sxiv all: sxiv


VERSION=git-20110209 VERSION=git-20110214


CC?=gcc CC?=gcc
PREFIX?=/usr/local PREFIX?=/usr/local


+ 17
- 10
main.c Целия файл

@@ -81,7 +81,7 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }


if (options->recursive) if (options->recursive || options->from_stdin)
filecnt = FNAME_CNT; filecnt = FNAME_CNT;
else else
filecnt = options->filecnt; filecnt = options->filecnt;
@@ -89,15 +89,22 @@ int main(int argc, char **argv) {
filenames = (const char**) s_malloc(filecnt * sizeof(const char*)); filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
fileidx = 0; fileidx = 0;


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




+ 3
- 0
options.c Целия файл

@@ -19,6 +19,7 @@
#define _XOPEN_SOURCE #define _XOPEN_SOURCE


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


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


_options.filenames = (const char**) argv + optind; _options.filenames = (const char**) argv + optind;
_options.filecnt = argc - optind; _options.filecnt = argc - optind;
_options.from_stdin = _options.filecnt == 1 &&
strcmp(_options.filenames[0], "-") == 0;
} }

+ 1
- 0
options.h Целия файл

@@ -24,6 +24,7 @@
typedef struct options_s { typedef struct options_s {
const char **filenames; const char **filenames;
int filecnt; int filecnt;
unsigned char from_stdin;


scalemode_t scalemode; scalemode_t scalemode;
float zoom; float zoom;


+ 39
- 1
util.c Целия файл

@@ -17,11 +17,13 @@
*/ */


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


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


#define FNAME_LEN 10

void cleanup(); void cleanup();


void* s_malloc(size_t size) { void* s_malloc(size_t size) {
@@ -75,3 +77,39 @@ void size_readable(float *size, const char **unit) {
*size /= 1024; *size /= 1024;
*unit = units[MIN(i, LEN(units) - 1)]; *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
- 0
util.h Целия файл

@@ -19,6 +19,7 @@
#ifndef UTIL_H #ifndef UTIL_H
#define UTIL_H #define UTIL_H


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


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


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


char* readline(FILE*);

#endif /* UTIL_H */ #endif /* UTIL_H */

||||||
x
 
000:0
Loading…
Отказ
Запис