]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strdup/: XSTRNDUP(), STRNDUPA(): Add macros
authorAlejandro Colomar <alx@kernel.org>
Mon, 13 May 2024 13:21:48 +0000 (15:21 +0200)
committerSerge Hallyn <serge@hallyn.com>
Tue, 2 Jul 2024 02:40:11 +0000 (21:40 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strdup/strndupa.c [new file with mode: 0644]
lib/string/strdup/strndupa.h [new file with mode: 0644]
lib/string/strdup/xstrndup.c [new file with mode: 0644]
lib/string/strdup/xstrndup.h [new file with mode: 0644]

index 21900733e3e62d787e54ab57ff6d6a44127628cb..42ff20413d4bc0c80452e9a219e39f43dba47d80 100644 (file)
@@ -160,6 +160,10 @@ libshadow_la_SOURCES = \
        string/strcpy/strtcpy.h \
        string/strcpy/zustr2stp.c \
        string/strcpy/zustr2stp.h \
+       string/strdup/strndupa.c \
+       string/strdup/strndupa.h \
+       string/strdup/xstrndup.c \
+       string/strdup/xstrndup.h \
        string/strftime.c \
        string/strftime.h \
        strtoday.c \
diff --git a/lib/string/strdup/strndupa.c b/lib/string/strdup/strndupa.c
new file mode 100644 (file)
index 0000000..aa67fa2
--- /dev/null
@@ -0,0 +1,7 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/strdup/strndupa.h"
diff --git a/lib/string/strdup/strndupa.h b/lib/string/strdup/strndupa.h
new file mode 100644 (file)
index 0000000..cefc298
--- /dev/null
@@ -0,0 +1,25 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_STRNDUPA_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRDUP_STRNDUPA_H_
+
+
+#include <config.h>
+
+#include <alloca.h>
+#include <string.h>
+
+#include "sizeof.h"
+#include "string/strcpy/strncat.h"
+
+
+// Similar to strndupa(3), but ensure that 's' is an array.
+#define STRNDUPA(s)                                                           \
+(                                                                             \
+       STRNCAT(strcpy(alloca(strnlen(s, NITEMS(s)) + 1), ""), s)             \
+)
+
+
+#endif  // include guard
diff --git a/lib/string/strdup/xstrndup.c b/lib/string/strdup/xstrndup.c
new file mode 100644 (file)
index 0000000..e659073
--- /dev/null
@@ -0,0 +1,7 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/strdup/xstrndup.h"
diff --git a/lib/string/strdup/xstrndup.h b/lib/string/strdup/xstrndup.h
new file mode 100644 (file)
index 0000000..77fd520
--- /dev/null
@@ -0,0 +1,25 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRNDUP_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRNDUP_H_
+
+
+#include <config.h>
+
+#include <string.h>
+
+#include "alloc.h"
+#include "sizeof.h"
+#include "string/strcpy/strncat.h"
+
+
+// Similar to strndup(3), but ensure that 's' is an array, and exit on ENOMEM.
+#define XSTRNDUP(s)                                                           \
+(                                                                             \
+       STRNCAT(strcpy(XMALLOC(strnlen(s, NITEMS(s)) + 1, char), ""), s)      \
+)
+
+
+#endif  // include guard