From ea23115af449e086ba05c9757ad22108944f6ec2 Mon Sep 17 00:00:00 2001
From: Bert <ber.t@gmx.com>
Date: Sun, 29 May 2011 11:45:58 +0200
Subject: [PATCH] Use getline instead of readline

---
 Makefile  |  2 +-
 main.c    | 25 +++++++++++++++----------
 options.c |  2 +-
 options.h |  2 +-
 util.c    | 38 +-------------------------------------
 util.h    |  2 --
 6 files changed, 19 insertions(+), 52 deletions(-)

diff --git a/Makefile b/Makefile
index 24574cc..edbb949 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 all: sxiv
 
-VERSION=git-20110525
+VERSION=git-20110529
 
 CC?=gcc
 DESTDIR?=
diff --git a/main.c b/main.c
index 344d55a..9a43fe7 100644
--- a/main.c
+++ b/main.c
@@ -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) {
diff --git a/options.c b/options.c
index b2b3a14..7bc92b8 100644
--- a/options.c
+++ b/options.c
@@ -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;
diff --git a/options.h b/options.h
index 0c62986..4438fd0 100644
--- a/options.h
+++ b/options.h
@@ -22,7 +22,7 @@
 #include "image.h"
 
 typedef struct {
-	const char **filenames;
+	char **filenames;
 	unsigned char from_stdin;
 	int filecnt;
 	int startnum;
diff --git a/util.c b/util.c
index e16de94..3957629 100644
--- a/util.c
+++ b/util.c
@@ -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;
-}
diff --git a/util.h b/util.h
index 8e8a20d..c1e08a6 100644
--- a/util.h
+++ b/util.h
@@ -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 */