summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorBert <ber.t@gmx.com>2011-02-14 17:51:04 +0100
committerBert <ber.t@gmx.com>2011-02-14 17:51:04 +0100
commit26cc5aff69856caf01a925ee1357b3650d806d67 (patch)
treee0ecd554c2a910f896e082271d8bc50905e51e68 /util.c
parentea65610747bf324c98f390fba91b8a7c8818006a (diff)
downloadnsxiv-26cc5aff69856caf01a925ee1357b3650d806d67.tar.zst
Read filenames from stdin
Diffstat (limited to 'util.c')
-rw-r--r--util.c40
1 files changed, 39 insertions, 1 deletions
diff --git a/util.c b/util.c
index ac81190..c23d821 100644
--- a/util.c
+++ b/util.c
@@ -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) {
@@ -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;
+}