summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDenys Vlasenko <dvlasenk@redhat.com>2010-10-25 14:20:05 +0200
committerDenys Vlasenko <dvlasenk@redhat.com>2010-10-25 14:20:05 +0200
commitd50f3cd388ebc2c8772d874aae0340489957fbcb (patch)
tree7e5bec7ba1f3c1ea010a5f7e544990ceaf830b6a /lib
parent44a0c7b2f92024120f4be376b5f0f44712a3670f (diff)
downloadabrt-d50f3cd388ebc2c8772d874aae0340489957fbcb.tar.gz
abrt-d50f3cd388ebc2c8772d874aae0340489957fbcb.tar.xz
abrt-d50f3cd388ebc2c8772d874aae0340489957fbcb.zip
firgot to "git add stdio_helpers.c"
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/utils/Makefile.am1
-rw-r--r--lib/utils/stdio_helpers.c69
2 files changed, 70 insertions, 0 deletions
diff --git a/lib/utils/Makefile.am b/lib/utils/Makefile.am
index 00ec6872..8ceebbe6 100644
--- a/lib/utils/Makefile.am
+++ b/lib/utils/Makefile.am
@@ -13,6 +13,7 @@ libABRTUtils_la_SOURCES = \
concat_path_file.c \
append_to_malloced_string.c \
encbase64.c \
+ stdio_helpers.c \
hash_md5.c hash_md5.h \
hash_sha1.c hash_sha1.h \
read_write.c read_write.h \
diff --git a/lib/utils/stdio_helpers.c b/lib/utils/stdio_helpers.c
new file mode 100644
index 00000000..81cf5d75
--- /dev/null
+++ b/lib/utils/stdio_helpers.c
@@ -0,0 +1,69 @@
+/*
+ * Utility routines.
+ *
+ * Copyright (C) 2001 Matt Krai
+ * Copyright (C) 2004 Erik Andersen <andersen@codepoet.org>
+ * Copyright (C) 2005, 2006 Rob Landley <rob@landley.net>
+ * Copyright (C) 2010 ABRT Team
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+#include "abrtlib.h"
+
+//TODO: add sanitizing upper limit (e.g 64K, 1M, or configurable).
+//This is why we don't use GNU's getline: it doesn't have
+//any upper sanity bound on line size.
+
+static char *xmalloc_fgets_internal(FILE *file, int *sizep)
+{
+ unsigned idx = 0;
+ char *linebuf = NULL;
+
+ while (1) {
+ char *r;
+
+ linebuf = xrealloc(linebuf, idx + 0x100);
+ r = fgets(&linebuf[idx], 0x100, file);
+ if (!r) {
+ /* need to terminate the line */
+ linebuf[idx] = '\0';
+ break;
+ }
+
+ /* stupid. fgets knows the len, it should report it somehow */
+ unsigned len = strlen(&linebuf[idx]);
+
+ idx += len;
+ if (len < 0xff || linebuf[idx - 1] == '\n')
+ break; /* we found \n or EOF */
+ }
+
+ *sizep = idx;
+
+ if (!idx) {
+ /* The very first fgets returned NULL. It's EOF (or error) */
+ free(linebuf);
+ linebuf = NULL;
+ }
+ return linebuf;
+}
+
+char *xmalloc_fgets(FILE *file)
+{
+ int sz;
+ char *r = xmalloc_fgets_internal(file, &sz);
+ if (!r)
+ return r;
+ return xrealloc(r, sz + 1);
+}
+
+char *xmalloc_fgetline(FILE *file)
+{
+ int sz;
+ char *r = xmalloc_fgets_internal(file, &sz);
+ if (!r)
+ return r;
+ if (r[sz - 1] == '\n')
+ r[--sz] = '\0';
+ return xrealloc(r, sz + 1);
+}