From 399fd3eda3045636a70da438a0fd1406cc332ed1 Mon Sep 17 00:00:00 2001
From: Chen Qi <Qi.Chen@windriver.com>
Date: Mon, 25 Feb 2019 14:18:21 +0800
Subject: [PATCH 05/24] src/basic/missing.h: check for missing strndupa

include missing.h  for definition of strndupa

Upstream-Status: Inappropriate [musl specific]

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
---
 meson.build                |  1 +
 src/basic/missing_stdlib.h | 12 ++++++++++++
 src/basic/mkdir.c          |  1 +
 src/basic/parse-util.c     |  1 +
 src/basic/procfs-util.c    |  1 +
 src/shared/pager.c         |  1 +
 src/shared/uid-range.c     |  1 +
 7 files changed, 18 insertions(+)

diff --git a/meson.build b/meson.build
index 44b24ee..70fb218 100644
--- a/meson.build
+++ b/meson.build
@@ -558,6 +558,7 @@ foreach ident : [
                                  #include <unistd.h>'''],
         ['explicit_bzero' ,   '''#include <string.h>'''],
         ['reallocarray',      '''#include <malloc.h>'''],
+        ['strndupa' ,         '''#include <string.h>'''],
 ]
 
         have = cc.has_function(ident[0], prefix : ident[1], args : '-D_GNU_SOURCE')
diff --git a/src/basic/missing_stdlib.h b/src/basic/missing_stdlib.h
index c0ffe86..d50274b 100644
--- a/src/basic/missing_stdlib.h
+++ b/src/basic/missing_stdlib.h
@@ -11,3 +11,15 @@
 #    define secure_getenv getenv
 #  endif
 #endif
+
+/* string.h */
+#if ! HAVE_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/basic/mkdir.c b/src/basic/mkdir.c
index 4bb65d5..3038ab9 100644
--- a/src/basic/mkdir.c
+++ b/src/basic/mkdir.c
@@ -13,6 +13,7 @@
 #include "stat-util.h"
 #include "stdio-util.h"
 #include "user-util.h"
+#include "missing.h"
 
 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) {
         struct stat st;
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 87724af..d9c53bc 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -19,6 +19,7 @@
 #include "process-util.h"
 #include "stat-util.h"
 #include "string-util.h"
+#include "missing.h"
 
 int parse_boolean(const char *v) {
         if (!v)
diff --git a/src/basic/procfs-util.c b/src/basic/procfs-util.c
index 7aaf95b..42ce53d 100644
--- a/src/basic/procfs-util.c
+++ b/src/basic/procfs-util.c
@@ -11,6 +11,7 @@
 #include "procfs-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
+#include "missing.h"
 
 int procfs_tasks_get_limit(uint64_t *ret) {
         _cleanup_free_ char *value = NULL;
diff --git a/src/shared/pager.c b/src/shared/pager.c
index bf2597e..fa9e321 100644
--- a/src/shared/pager.c
+++ b/src/shared/pager.c
@@ -24,6 +24,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "missing.h"
 
 static pid_t pager_pid = 0;
 
diff --git a/src/shared/uid-range.c b/src/shared/uid-range.c
index 5fa7bd2..9c63b9e 100644
--- a/src/shared/uid-range.c
+++ b/src/shared/uid-range.c
@@ -9,6 +9,7 @@
 #include "uid-range.h"
 #include "user-util.h"
 #include "util.h"
+#include "missing.h"
 
 static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
         assert(range);
-- 
2.7.4

