From: Stéphane Graber Date: Thu, 20 Dec 2012 12:46:57 +0000 (+0100) Subject: Support both getline and fgetln X-Git-Tag: lxc-0.9.0.alpha3~1^2~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ba0013f21e1953ae4a59cdf61562fbe843077e9;p=thirdparty%2Flxc.git Support both getline and fgetln 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 --- 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)