Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F168274877
D54066.id167540.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
8 KB
Referenced Files
None
Subscribers
None
D54066.id167540.diff
View Options
diff --git a/include/string.h b/include/string.h
--- a/include/string.h
+++ b/include/string.h
@@ -154,6 +154,10 @@
int timingsafe_bcmp(const void *, const void *, size_t);
int timingsafe_memcmp(const void *, const void *, size_t);
+
+char *strdupa(const char *);
+char *strndupa(const char *, size_t);
+
#endif /* __BSD_VISIBLE */
#if __POSIX_VISIBLE >= 200112 || defined(_XLOCALE_H_)
diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc
--- a/lib/libc/string/Makefile.inc
+++ b/lib/libc/string/Makefile.inc
@@ -7,26 +7,95 @@
CFLAGS+= -I${LIBC_SRCTOP}/locale
# machine-independent string sources
-MISRCS+=bcmp.c bcopy.c bzero.c explicit_bzero.c \
- ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \
- memccpy.c memchr.c memrchr.c memcmp.c \
- memcpy.c memmem.c memmove.c mempcpy.c memset.c memset_s.c \
+MISRCS+=bcmp.c \
+ bcopy.c \
+ bzero.c \
+ explicit_bzero.c \
+ ffs.c \
+ ffsl.c \
+ ffsll.c \
+ fls.c \
+ flsl.c \
+ flsll.c \
+ memccpy.c \
+ memchr.c \
+ memrchr.c \
+ memcmp.c \
+ memcpy.c \
+ memmem.c \
+ memmove.c \
+ mempcpy.c \
+ memset.c \
+ memset_s.c \
memset_explicit.c \
- stpcpy.c stpncpy.c strcasecmp.c \
- strcat.c strcasestr.c strchr.c strchrnul.c strcmp.c strcoll.c strcpy.c\
- strcspn.c strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c \
- strncat.c strncmp.c strncpy.c strndup.c strnlen.c strnstr.c \
- strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \
- strverscmp.c strxfrm.c swab.c \
+ stpcpy.c \
+ stpncpy.c \
+ strcasecmp.c \
+ strcat.c \
+ strcasestr.c \
+ strchr.c \
+ strchrnul.c \
+ strcmp.c \
+ strcoll.c \
+ strcpy.c\
+ strcspn.c \
+ strdup.c \
+ strdupa.c \
+ strerror.c \
+ strlcat.c \
+ strlcpy.c \
+ strlen.c \
+ strmode.c \
+ strncat.c \
+ strncmp.c \
+ strncpy.c \
+ strndup.c \
+ strndupa.c \
+ strnlen.c \
+ strnstr.c \
+ strpbrk.c \
+ strrchr.c \
+ strsep.c \
+ strsignal.c \
+ strspn.c \
+ strstr.c \
+ strtok.c \
+ strverscmp.c \
+ strxfrm.c \
+ swab.c \
timingsafe_bcmp.c \
timingsafe_memcmp.c \
- wcpcpy.c wcpncpy.c wcscasecmp.c wcscat.c \
- wcschr.c wcscmp.c wcscoll.c wcscpy.c wcscspn.c wcsdup.c \
- wcslcat.c wcslcpy.c wcslen.c wcsncasecmp.c wcsncat.c wcsncmp.c \
- wcsncpy.c wcsnlen.c wcspbrk.c \
- wcsrchr.c wcsspn.c wcsstr.c wcstok.c wcswidth.c wcsxfrm.c wmemchr.c \
+ wcpcpy.c \
+ wcpncpy.c \
+ wcscasecmp.c \
+ wcscat.c \
+ wcschr.c \
+ wcscmp.c \
+ wcscoll.c \
+ wcscpy.c \
+ wcscspn.c \
+ wcsdup.c \
+ wcslcat.c \
+ wcslcpy.c \
+ wcslen.c \
+ wcsncasecmp.c \
+ wcsncat.c \
+ wcsncmp.c \
+ wcsncpy.c \
+ wcsnlen.c \
+ wcspbrk.c \
+ wcsrchr.c \
+ wcsspn.c \
+ wcsstr.c \
+ wcstok.c \
+ wcswidth.c \
+ wcsxfrm.c \
+ wmemchr.c \
wmemcmp.c \
- wmemcpy.c wmemmove.c wmempcpy.c wmemset.c
+ wmemcpy.c \
+ wmemmove.c \
+ wmempcpy.c \
+ wmemset.c
SYM_MAPS+= ${LIBC_SRCTOP}/string/Symbol.map
@@ -45,14 +114,45 @@
.sinclude "${LIBC_SRCTOP}/${LIBC_ARCH}/string/Makefile.inc"
.endif
-MAN+= bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \
- memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \
- strchr.3 strcmp.3 strcoll.3 strcpy.3 strdup.3 strerror.3 \
- string.3 strlcpy.3 strlen.3 strmode.3 strpbrk.3 strsep.3 \
- strspn.3 strstr.3 strtok.3 strverscmp.3 strxfrm.3 swab.3 \
+MAN+= bcmp.3 \
+ bcopy.3 \
+ bstring.3 \
+ bzero.3 \
+ ffs.3 \
+ index.3 \
+ memccpy.3 \
+ memchr.3 \
+ memcmp.3 \
+ memcpy.3 \
+ memmem.3 \
+ memmove.3 \
+ memset.3 \
+ strcasecmp.3 \
+ strcat.3 \
+ strchr.3 \
+ strcmp.3 \
+ strcoll.3 \
+ strcpy.3 \
+ strdup.3 \
+ strerror.3 \
+ string.3 \
+ strlcpy.3 \
+ strlen.3 \
+ strmode.3 \
+ strpbrk.3 \
+ strsep.3 \
+ strspn.3 \
+ strstr.3 \
+ strtok.3 \
+ strverscmp.3 \
+ strxfrm.3 \
+ swab.3 \
timingsafe_bcmp.3 \
- wcscoll.3 wcstok.3 \
- wcswidth.3 wcsxfrm.3 wmemchr.3
+ wcscoll.3 \
+ wcstok.3 \
+ wcswidth.3 \
+ wcsxfrm.3 \
+ wmemchr.3
MLINKS+=bzero.3 explicit_bzero.3
MLINKS+=ffs.3 ffsl.3 \
@@ -76,7 +176,9 @@
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 \
diff --git a/lib/libc/string/Symbol.map b/lib/libc/string/Symbol.map
--- a/lib/libc/string/Symbol.map
+++ b/lib/libc/string/Symbol.map
@@ -120,6 +120,11 @@
memset_explicit;
};
+FBSD_1.9 {
+ strdupa;
+ strndupa;
+};
+
FBSDprivate_1.0 {
__strtok_r;
};
diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3
--- a/lib/libc/string/strdup.3
+++ b/lib/libc/string/strdup.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 @@
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 @@
.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 @@
.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 @@
.Fn strndup
function was added in
.Fx 7.2 .
+The
+.Fn strdupa
+and
+.Fn strndupa
+functions were added in
+.Fx 15.1 .
diff --git a/lib/libc/string/strdupa.c b/lib/libc/string/strdupa.c
new file mode 100644
--- /dev/null
+++ b/lib/libc/string/strdupa.c
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strdupa(const char *str)
+{
+ size_t len;
+ char *copy;
+
+ len = strlen(str) + 1;
+ copy = alloca(len);
+ memcpy(copy, str, len);
+ return (copy);
+}
diff --git a/lib/libc/string/strndupa.c b/lib/libc/string/strndupa.c
new file mode 100644
--- /dev/null
+++ b/lib/libc/string/strndupa.c
@@ -0,0 +1,35 @@
+/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
+
+/*
+ * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strndupa(const char *str, size_t maxlen)
+{
+ char *copy;
+ size_t len;
+
+ len = strnlen(str, maxlen);
+ copy = alloca(len + 1);
+ (void)memcpy(copy, str, len);
+ copy[len] = '\0';
+
+ return (copy);
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Aug 28, 8:16 AM (12 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
37444843
Default Alt Text
D54066.id167540.diff (8 KB)
Attached To
Mode
D54066: libc: add strdupa(3) and strndupa(3)
Attached
Detach File
Event Timeline
Log In to Comment