mirror of
https://git.freebsd.org/src.git
synced 2026-01-11 19:57:22 +00:00
libc/string: add strdupa(3) and strndupa(3)
Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54066
This commit is contained in:
parent
98b7cca018
commit
a98e5d7850
3 changed files with 64 additions and 2 deletions
|
|
@ -154,6 +154,34 @@ void swab(const void * __restrict, void * __restrict, ssize_t);
|
|||
|
||||
int timingsafe_bcmp(const void *, const void *, size_t);
|
||||
int timingsafe_memcmp(const void *, const void *, size_t);
|
||||
|
||||
#if __has_builtin(__builtin_alloca)
|
||||
#define strdupa(_Str) (__extension__({ \
|
||||
const char *_Str1; \
|
||||
size_t _Len; \
|
||||
char *_Copy; \
|
||||
\
|
||||
_Str1 = (_Str); \
|
||||
_Len = strlen(_Str1) + 1; \
|
||||
_Copy = (char *)__builtin_alloca(_Len); \
|
||||
memcpy(_Copy, _Str1, _Len); \
|
||||
_Copy; \
|
||||
}))
|
||||
|
||||
#define strndupa(_Str, _Maxlen) (__extension__({ \
|
||||
const char *_Str1; \
|
||||
char *_Copy; \
|
||||
size_t _Len; \
|
||||
\
|
||||
_Str1 = (_Str); \
|
||||
_Len = strnlen((_Str1), (_Maxlen)); \
|
||||
_Copy = __builtin_alloca(_Len + 1); \
|
||||
(void)memcpy(_Copy, _Str1, _Len); \
|
||||
_Copy[_Len] = '\0'; \
|
||||
_Copy; \
|
||||
}))
|
||||
#endif
|
||||
|
||||
#endif /* __BSD_VISIBLE */
|
||||
|
||||
#if __POSIX_VISIBLE >= 200112 || defined(_XLOCALE_H_)
|
||||
|
|
|
|||
|
|
@ -174,7 +174,9 @@ MLINKS+=strcoll.3 strcoll_l.3
|
|||
MLINKS+=strcpy.3 stpcpy.3 \
|
||||
strcpy.3 stpncpy.3 \
|
||||
strcpy.3 strncpy.3
|
||||
MLINKS+=strdup.3 strndup.3
|
||||
MLINKS+=strdup.3 strndup.3 \
|
||||
strdup.3 strdupa.3 \
|
||||
strdup.3 strndupa.3
|
||||
MLINKS+=strerror.3 perror.3 \
|
||||
strerror.3 strerror_l.3 \
|
||||
strerror.3 strerror_r.3 \
|
||||
|
|
|
|||
|
|
@ -30,7 +30,9 @@
|
|||
.Os
|
||||
.Sh NAME
|
||||
.Nm strdup ,
|
||||
.Nm strndup
|
||||
.Nm strdupa ,
|
||||
.Nm strndup ,
|
||||
.Nm strndupa
|
||||
.Nd save a copy of a string
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
|
|
@ -39,7 +41,11 @@
|
|||
.Ft char *
|
||||
.Fn strdup "const char *str"
|
||||
.Ft char *
|
||||
.Fn strdupa "const char *str"
|
||||
.Ft char *
|
||||
.Fn strndup "const char *str" "size_t len"
|
||||
.Ft char *
|
||||
.Fn strndupa "const char *str" "size_t len"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn strdup
|
||||
|
|
@ -63,6 +69,19 @@ characters from the string
|
|||
always
|
||||
.Dv NUL
|
||||
terminating the copied string.
|
||||
.Pp
|
||||
The
|
||||
.Fn strdupa
|
||||
function is identical to
|
||||
.Fn strdup
|
||||
but allocates the memory with
|
||||
.Xr alloca 3 .
|
||||
Similarly, the
|
||||
.Fn strndupa
|
||||
function is identical to
|
||||
.Fn strndup ,
|
||||
but allocates the memory with
|
||||
.Xr alloca 3 .
|
||||
.Sh RETURN VALUES
|
||||
If insufficient memory is available, NULL is returned and
|
||||
.Va errno
|
||||
|
|
@ -72,6 +91,7 @@ Otherwise, the
|
|||
.Fn strdup
|
||||
family of functions return a pointer to the copied string.
|
||||
.Sh SEE ALSO
|
||||
.Xr alloca 3 ,
|
||||
.Xr free 3 ,
|
||||
.Xr malloc 3 ,
|
||||
.Xr wcsdup 3
|
||||
|
|
@ -84,6 +104,12 @@ The
|
|||
.Fn strndup
|
||||
function is specified by
|
||||
.St -p1003.1-2008 .
|
||||
The
|
||||
.Fn strdupa
|
||||
and
|
||||
.Fn strndupa
|
||||
functions are extensions,
|
||||
taken from glibc.
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn strdup
|
||||
|
|
@ -93,3 +119,9 @@ The
|
|||
.Fn strndup
|
||||
function was added in
|
||||
.Fx 7.2 .
|
||||
The
|
||||
.Fn strdupa
|
||||
and
|
||||
.Fn strndupa
|
||||
functions were added in
|
||||
.Fx 15.1 .
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue