[systemd-devel] [RFC 13/25] shared/missing.h: check for missing strndupa

Emil Renner Berthing systemd at esmil.dk
Thu Sep 18 06:24:49 PDT 2014


---
 configure.ac         |  3 ++-
 src/shared/missing.h | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 1f2bbd0..3db0e24 100644
--- a/configure.ac
+++ b/configure.ac
@@ -301,9 +301,10 @@ LIBS="$save_LIBS"
 
 AC_CHECK_FUNCS([fanotify_init fanotify_mark])
 AC_CHECK_FUNCS([__secure_getenv secure_getenv])
-AC_CHECK_DECLS([gettid, pivot_root, canonicalize_file_name, name_to_handle_at, setns, LO_FLAGS_PARTSCAN],
+AC_CHECK_DECLS([gettid, pivot_root, canonicalize_file_name, strndupa, name_to_handle_at, setns, LO_FLAGS_PARTSCAN],
                [], [], [[
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/mount.h>
diff --git a/src/shared/missing.h b/src/shared/missing.h
index 2379950..55e61f6 100644
--- a/src/shared/missing.h
+++ b/src/shared/missing.h
@@ -136,6 +136,17 @@ static inline char *canonicalize_file_name(const char *path) {
 }
 #endif
 
+#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
+
 #ifdef __x86_64__
 #  ifndef __NR_fanotify_init
 #    define __NR_fanotify_init 300
-- 
2.1.0



More information about the systemd-devel mailing list