diff options
author | Freddy DISSAUX <dsx@bsdsx.fr> | 2018-10-13 19:47:47 +0200 |
---|---|---|
committer | Freddy DISSAUX <dsx@bsdsx.fr> | 2018-10-13 19:47:47 +0200 |
commit | 5cfe1ae2ffb0d7bf7f3d5122271fa90c11ed1c91 (patch) | |
tree | 5cb1cd12729524378e11e243d498744a4d2627d6 /openbsd-compat | |
parent | Update README.md (diff) | |
download | OpenSMTPD-5cfe1ae2ffb0d7bf7f3d5122271fa90c11ed1c91.tar.xz OpenSMTPD-5cfe1ae2ffb0d7bf7f3d5122271fa90c11ed1c91.zip |
fix strndup
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/Makefile.am | 8 | ||||
-rw-r--r-- | openbsd-compat/openbsd-compat.h | 7 | ||||
-rw-r--r-- | openbsd-compat/strndup.c | 39 | ||||
-rw-r--r-- | openbsd-compat/strnlen.c | 33 |
4 files changed, 87 insertions, 0 deletions
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 <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 <sys/types.h> + +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +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 <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 <sys/types.h> + +#include <string.h> + +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); |