From 1ba0013f21e1953ae4a59cdf61562fbe843077e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Graber?= Date: Thu, 20 Dec 2012 13:46:57 +0100 Subject: [PATCH] Support both getline and fgetln MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Acked-by: Serge E. Hallyn --- configure.ac | 9 +++++++++ src/include/getline.c | 31 +++++++++++++++++++++++++++++++ src/include/getline.h | 6 ++++++ src/lxc/Makefile.am | 12 ++++++++++++ src/lxc/attach.c | 8 ++++++++ src/lxc/parse.c | 8 ++++++++ 6 files changed, 74 insertions(+) create mode 100644 src/include/getline.c create mode 100644 src/include/getline.h diff --git a/configure.ac b/configure.ac index 564df0ecd..479357314 100644 --- a/configure.ac +++ b/configure.ac @@ -216,6 +216,15 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP], [], [], [#include ]) # 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 index 000000000..d4117cbba --- /dev/null +++ b/src/include/getline.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +/* + * 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 index 000000000..b030d7a1c --- /dev/null +++ b/src/include/getline.h @@ -0,0 +1,6 @@ +#ifndef _getline_h +#define _getline_h + +extern ssize_t getline(char **outbuf, size_t *outsize, FILE *fp); + +#endif diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index bf675f9d0..ef02b6dd1 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -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)\" \ diff --git a/src/lxc/attach.c b/src/lxc/attach.c index ec0e08300..9b7efbc0b 100644 --- a/src/lxc/attach.c +++ b/src/lxc/attach.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #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)); diff --git a/src/lxc/parse.c b/src/lxc/parse.c index 10510c9d9..b074b047b 100644 --- a/src/lxc/parse.c +++ b/src/lxc/parse.c @@ -29,8 +29,16 @@ #include #include "parse.h" +#include "config.h" #include +/* 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) -- 2.47.2