]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Support both getline and fgetln
authorStéphane Graber <stgraber@ubuntu.com>
Thu, 20 Dec 2012 12:46:57 +0000 (13:46 +0100)
committerStéphane Graber <stgraber@ubuntu.com>
Wed, 9 Jan 2013 15:09:36 +0000 (10:09 -0500)
Some libc implementations don't have the getline function but instead
have an equivalent fgetln function.

Add code to detect both and use whatever is available.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
configure.ac
src/include/getline.c [new file with mode: 0644]
src/include/getline.h [new file with mode: 0644]
src/lxc/Makefile.am
src/lxc/attach.c
src/lxc/parse.c

index 564df0ecd54347e2eeee2f419fbaa2a5514b0573..4793573140f2437b4e651b30e2c434cba9796eec 100644 (file)
@@ -216,6 +216,15 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP], [], [], [#include <sys/prctl.h>])
 # Check for optional headers
 AC_CHECK_HEADERS([sys/signalfd.h])
 
+AC_CHECK_FUNCS([getline],
+       AM_CONDITIONAL(HAVE_GETLINE, true)
+       AC_DEFINE(HAVE_GETLINE,1,[Have getline]),
+       AM_CONDITIONAL(HAVE_GETLINE, false))
+AC_CHECK_FUNCS([fgetln],
+       AM_CONDITIONAL(HAVE_FGETLN, true)
+       AC_DEFINE(HAVE_FGETLN,1,[Have fgetln]),
+       AM_CONDITIONAL(HAVE_FGETLN, false))
+
 # Check for some standard binaries
 AC_PROG_GCC_TRADITIONAL
 AC_PROG_SED
diff --git a/src/include/getline.c b/src/include/getline.c
new file mode 100644 (file)
index 0000000..d4117cb
--- /dev/null
@@ -0,0 +1,31 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Emulate glibc getline() via BSD fgetln().
+ * Note that outsize is not changed unless memory is allocated.
+ */
+ssize_t
+getline(char **outbuf, size_t *outsize, FILE *fp)
+{
+    size_t len;
+    char *buf;
+    buf = fgetln(fp, &len);
+
+    if (buf == NULL)
+        return (-1);
+
+    /* Assumes realloc() accepts NULL for ptr (C99) */
+    if (*outbuf == NULL || *outsize < len + 1) {
+        void *tmp = realloc(*outbuf, len + 1);
+        if (tmp == NULL)
+            return (-1);
+        *outbuf = tmp;
+        *outsize = len + 1;
+    }
+    memcpy(*outbuf, buf, len);
+    (*outbuf)[len] = '\0';
+    return (len);
+}
diff --git a/src/include/getline.h b/src/include/getline.h
new file mode 100644 (file)
index 0000000..b030d7a
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef _getline_h
+#define _getline_h
+
+extern ssize_t getline(char **outbuf, size_t *outsize, FILE *fp);
+
+#endif
index bf675f9d0078356cec3878513967e6b25567f342..ef02b6dd1322022758388faf04fe81e0188221fc 100644 (file)
@@ -17,6 +17,12 @@ pkginclude_HEADERS = \
                lxccontainer.h \
                lxclock.h
 
+if !HAVE_GETLINE
+if HAVE_FGETLN
+pkginclude_HEADERS += ../include/getline.h
+endif
+endif
+
 sodir=$(libdir)
 # use PROGRAMS to avoid complains from automake
 so_PROGRAMS = liblxc.so
@@ -61,6 +67,12 @@ liblxc_so_SOURCES = \
        lxclock.h lxclock.c \
        lxccontainer.c lxccontainer.h
 
+if !HAVE_GETLINE
+if HAVE_FGETLN
+liblxc_so_SOURCES += ../include/getline.c ../include/getline.h
+endif
+endif
+
 AM_CFLAGS=-I$(top_srcdir)/src \
        -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
        -DLXCPATH=\"$(LXCPATH)\" \
index ec0e08300e28af9148ad90ae89f53a51393708b8..9b7efbc0b7f4bb798e530af4ef3a60bd6c65a428 100644 (file)
@@ -31,6 +31,7 @@
 #include <sys/param.h>
 #include <sys/prctl.h>
 #include <sys/mount.h>
+#include <sys/syscall.h>
 #include <linux/unistd.h>
 
 #if !HAVE_DECL_PR_CAPBSET_DROP
@@ -56,6 +57,13 @@ int setns(int fd, int nstype)
 #endif
 }
 
+/* Define getline() if missing from the C library */
+#ifndef HAVE_GETLINE
+#ifdef HAVE_FGETLN
+#include <../include/getline.h>
+#endif
+#endif
+
 struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
 {
        struct lxc_proc_context_info *info = calloc(1, sizeof(*info));
index 10510c9d951ae958853b2efc6ac0f42e7a46be08..b074b047b484ebb6f976f0d345a1d9732e90417e 100644 (file)
 #include <dirent.h>
 
 #include "parse.h"
+#include "config.h"
 #include <lxc/log.h>
 
+/* Define getline() if missing from the C library */
+#ifndef HAVE_GETLINE
+#ifdef HAVE_FGETLN
+#include <../include/getline.h>
+#endif
+#endif
+
 lxc_log_define(lxc_parse, lxc);
 
 static int dir_filter(const struct dirent *dirent)