diff --git a/configure.ac b/configure.ac index 62e8889..9735db9 100644 --- a/configure.ac +++ b/configure.ac @@ -112,6 +112,7 @@ AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h netdb.h netinet/in.h \ AC_C_INLINE AC_CHECK_MEMBERS([struct stat.st_rdev],, AC_MSG_ERROR([no st_rdev member in struct stat])) +AC_CHECK_DECLS([strndupa]) AC_TYPE_SIZE_T AC_TYPE_UID_T m4_ifdef([AC_TYPE_UINT16_T], [AC_TYPE_UINT16_T]) diff --git a/include/strndupa.h b/include/strndupa.h new file mode 100644 index 0000000..fbeba68 --- /dev/null +++ b/include/strndupa.h @@ -0,0 +1,16 @@ +#pragma once + +/** + * Reimplementation of glibc strndupa + * http://lists.freedesktop.org/archives/systemd-devel/2014-September/023190.html + */ +#if !HAVE_DECL_STRNDUPA +#define strndupa(s, n) \ + ({ \ + const char *__old = (s); \ + size_t __len = strnlen(__old, (n)); \ + char *__new = (char *)alloca(__len + 1); \ + __new[__len] = '\0'; \ + (char *)memcpy(__new, __old, __len); \ + }) +#endif diff --git a/src/lib/net.c b/src/lib/net.c index 6ac3008..d7f2078 100644 --- a/src/lib/net.c +++ b/src/lib/net.c @@ -41,6 +41,7 @@ #include "script.h" #include "util.h" #include "vps_configure.h" +#include "strndupa.h" char *find_ip(list_head_t *ip_h, const char *ipaddr) diff --git a/src/lib/util.c b/src/lib/util.c index 9712333..29c981a 100644 --- a/src/lib/util.c +++ b/src/lib/util.c @@ -33,6 +33,7 @@ #include "util.h" #include "logger.h" #include "fs.h" +#include "strndupa.h" #ifndef NR_OPEN #define NR_OPEN 1024