Skip to content

Commit

Permalink
lib/string/strdup/strndupa.h: STRNDUPA(): Simplify implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 19, 2025
1 parent 0e95f99 commit 9227a34
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions lib/string/strdup/strndupa.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


Expand All @@ -12,19 +12,15 @@
#include <string.h>

#include "sizeof.h"
#include "string/strcpy/strncat.h"


#ifndef strndupa
# define strndupa(s, n) strncat(strcpy(alloca(n + 1), ""), s, n)
#endif


// Similar to strndupa(3), but ensure that 's' is an array.
#define STRNDUPA(s) \
( \
STRNCAT(strcpy(alloca(strnlen(s, NITEMS(s)) + 1), ""), s) \
)
// strndupa(3), but ensure that 's' is an array.
#define STRNDUPA(s) strndupa(s, NITEMS(s))


#endif // include guard

0 comments on commit 9227a34

Please sign in to comment.