From 2a0c0dd24bf3bf9f9e8a0d6979cb1bffefa56ff3 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Mon, 13 May 2024 15:21:48 +0200 Subject: [PATCH] lib/string/strdup/: XSTRNDUP(), STRNDUPA(): Add macros Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 4 ++++ lib/string/strdup/strndupa.c | 7 +++++++ lib/string/strdup/strndupa.h | 25 +++++++++++++++++++++++++ lib/string/strdup/xstrndup.c | 7 +++++++ lib/string/strdup/xstrndup.h | 25 +++++++++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 lib/string/strdup/strndupa.c create mode 100644 lib/string/strdup/strndupa.h create mode 100644 lib/string/strdup/xstrndup.c create mode 100644 lib/string/strdup/xstrndup.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 21900733e..42ff20413 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 index 000000000..aa67fa276 --- /dev/null +++ b/lib/string/strdup/strndupa.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strdup/strndupa.h" diff --git a/lib/string/strdup/strndupa.h b/lib/string/strdup/strndupa.h new file mode 100644 index 000000000..cefc29820 --- /dev/null +++ b/lib/string/strdup/strndupa.h @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_STRNDUPA_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRDUP_STRNDUPA_H_ + + +#include + +#include +#include + +#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 index 000000000..e659073f5 --- /dev/null +++ b/lib/string/strdup/xstrndup.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strdup/xstrndup.h" diff --git a/lib/string/strdup/xstrndup.h b/lib/string/strdup/xstrndup.h new file mode 100644 index 000000000..77fd5206a --- /dev/null +++ b/lib/string/strdup/xstrndup.h @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRNDUP_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRNDUP_H_ + + +#include + +#include + +#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 -- 2.47.3