Use getline instead of readline

This commit is contained in:
Bert 2011-05-29 11:45:58 +02:00
parent 2252a0148d
commit ea23115af4
6 changed files with 19 additions and 52 deletions

View File

@ -1,6 +1,6 @@
all: sxiv
VERSION=git-20110525
VERSION=git-20110529
CC?=gcc
DESTDIR?=

25
main.c
View File

@ -16,6 +16,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -59,7 +61,7 @@ img_t img;
tns_t tns;
win_t win;
const char **filenames;
char **filenames;
int filecnt, fileidx;
size_t filesize;
@ -88,7 +90,7 @@ void remove_file(int n, unsigned char silent) {
if (n + 1 < filecnt)
memmove(filenames + n, filenames + n + 1, (filecnt - n - 1) *
sizeof(const char*));
sizeof(char*));
if (n + 1 < tns.cnt) {
memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
sizeof(thumb_t));
@ -150,7 +152,7 @@ void update_title() {
win_set_title(&win, win_title);
}
int check_append(const char *filename) {
int check_append(char *filename) {
if (!filename)
return 0;
@ -160,8 +162,7 @@ int check_append(const char *filename) {
} else {
if (fileidx == filecnt) {
filecnt *= 2;
filenames = (const char**) s_realloc(filenames,
filecnt * sizeof(const char*));
filenames = (char**) s_realloc(filenames, filecnt * sizeof(char*));
}
filenames[fileidx++] = filename;
return 1;
@ -173,8 +174,9 @@ int fncmp(const void *a, const void *b) {
}
int main(int argc, char **argv) {
int i, start;
const char *filename;
int i, len, start;
size_t n;
char *filename = NULL;
struct stat fstats;
r_dir_t dir;
@ -196,13 +198,16 @@ int main(int argc, char **argv) {
else
filecnt = options->filecnt;
filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
filenames = (char**) s_malloc(filecnt * sizeof(char*));
fileidx = 0;
if (options->from_stdin) {
while ((filename = readline(stdin))) {
while ((len = getline(&filename, &n, stdin)) > 0) {
if (filename[len-1] == '\n')
filename[len-1] = '\0';
if (!*filename || !check_append(filename))
free((void*) filename);
free(filename);
filename = NULL;
}
} else {
for (i = 0; i < options->filecnt; ++i) {

View File

@ -124,7 +124,7 @@ void parse_options(int argc, char **argv) {
}
}
_options.filenames = (const char**) argv + optind;
_options.filenames = argv + optind;
_options.filecnt = argc - optind;
_options.from_stdin = _options.filecnt == 1 &&
strcmp(_options.filenames[0], "-") == 0;

View File

@ -22,7 +22,7 @@
#include "image.h"
typedef struct {
const char **filenames;
char **filenames;
unsigned char from_stdin;
int filecnt;
int startnum;

38
util.c
View File

@ -140,7 +140,7 @@ char* absolute_path(const char *filename) {
path = (char*) s_malloc(len);
snprintf(path, len, "%s/%s", dir, basename);
goto end;
goto end;
error:
if (path) {
@ -297,39 +297,3 @@ int r_mkdir(const char *path) {
return err;
}
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)) {
s = NULL;
} else {
s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char));
strcpy(s, buf);
}
free(buf);
return s;
}

2
util.h
View File

@ -61,6 +61,4 @@ int r_closedir(r_dir_t*);
char* r_readdir(r_dir_t*);
int r_mkdir(const char *);
char* readline(FILE*);
#endif /* UTIL_H */