From 5cfe1ae2ffb0d7bf7f3d5122271fa90c11ed1c91 Mon Sep 17 00:00:00 2001 From: Freddy DISSAUX Date: Sat, 13 Oct 2018 19:47:47 +0200 Subject: fix strndup --- openbsd-compat/Makefile.am | 8 ++++++++ openbsd-compat/openbsd-compat.h | 7 +++++++ openbsd-compat/strndup.c | 39 +++++++++++++++++++++++++++++++++++++++ openbsd-compat/strnlen.c | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 openbsd-compat/strndup.c create mode 100644 openbsd-compat/strnlen.c (limited to 'openbsd-compat') diff --git a/openbsd-compat/Makefile.am b/openbsd-compat/Makefile.am index d58f901d..a43dff55 100644 --- a/openbsd-compat/Makefile.am +++ b/openbsd-compat/Makefile.am @@ -83,6 +83,14 @@ if !SUPPORT_INET_NET_PTON libopenbsd_compat_a_SOURCES += inet_net_pton.c endif +if !SUPPORT_STRNDUP +libopenbsd_compat_a_SOURCES += strndup.c +endif + +if !SUPPORT_STRNLEN +libopenbsd_compat_a_SOURCES += strnlen.c +endif + EXTRA_DIST = base64.h EXTRA_DIST += bsd-misc.h EXTRA_DIST += bsd-waitpid.h diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h index 57125fb7..7dde2fe1 100644 --- a/openbsd-compat/openbsd-compat.h +++ b/openbsd-compat/openbsd-compat.h @@ -265,5 +265,12 @@ ssize_t getline(char **, size_t *, FILE *); int crypt_checkpass(const char *, const char *); #endif +#ifndef HAVE_STRNDUP +char * strndup(const char *, size_t); +#endif + +#ifndef HAVE_STRNLEN +char * strnlen(const char *, size_t); +#endif #endif /* _OPENBSD_COMPAT_H */ diff --git a/openbsd-compat/strndup.c b/openbsd-compat/strndup.c new file mode 100644 index 00000000..f43ba659 --- /dev/null +++ b/openbsd-compat/strndup.c @@ -0,0 +1,39 @@ +/* $OpenBSD: strndup.c,v 1.2 2015/08/31 02:53:57 guenther Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller + * + * 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 + +#include +#include +#include + +char * +strndup(const char *str, size_t maxlen) +{ + char *copy; + size_t len; + + len = strnlen(str, maxlen); + copy = malloc(len + 1); + if (copy != NULL) { + (void)memcpy(copy, str, len); + copy[len] = '\0'; + } + + return copy; +} diff --git a/openbsd-compat/strnlen.c b/openbsd-compat/strnlen.c new file mode 100644 index 00000000..db809756 --- /dev/null +++ b/openbsd-compat/strnlen.c @@ -0,0 +1,33 @@ +/* $OpenBSD: strnlen.c,v 1.8 2016/10/16 17:37:39 dtucker Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller + * + * 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 + +#include + +size_t +strnlen(const char *str, size_t maxlen) +{ + const char *cp; + + for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) + ; + + return (size_t)(cp - str); +} +DEF_WEAK(strnlen); -- cgit v1.2.3-59-g8ed1b From 9e3f626edb4c1237ab9819540dece002a311b28b Mon Sep 17 00:00:00 2001 From: Freddy DISSAUX Date: Sat, 13 Oct 2018 19:49:40 +0200 Subject: fix strnlen --- openbsd-compat/strnlen.c | 1 - 1 file changed, 1 deletion(-) (limited to 'openbsd-compat') diff --git a/openbsd-compat/strnlen.c b/openbsd-compat/strnlen.c index db809756..a2017e19 100644 --- a/openbsd-compat/strnlen.c +++ b/openbsd-compat/strnlen.c @@ -30,4 +30,3 @@ strnlen(const char *str, size_t maxlen) return (size_t)(cp - str); } -DEF_WEAK(strnlen); -- cgit v1.2.3-59-g8ed1b