From c212908c441045943b959e92c6ba4c3e591c6585 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 30 Apr 2012 18:11:51 +0000 Subject: [PATCH] bsdstubs: implement strndup() stub implementation if none exists --- bsdstubs.c | 37 +++++++++++++++++++++++++++++++++++++ bsdstubs.h | 5 +++++ 2 files changed, 42 insertions(+) diff --git a/bsdstubs.c b/bsdstubs.c index bb56a4f..3986443 100644 --- a/bsdstubs.c +++ b/bsdstubs.c @@ -96,3 +96,40 @@ strlcat(char *dst, const char *src, size_t siz) } #endif + +/* + * Copyright (c) 2012 William Pitcock . + * + * Permission to use, copy, modify, and/or 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. + */ + +#ifndef HAVE_STRNDUP + +/* + * Creates a memory buffer and copies at most 'len' characters to it. + * If 'len' is less than the length of the source string, truncation occured. + */ +char *strndup(const char *src, size_t len) +{ + char *out = malloc(len + 1); + + strlcpy(out, src, len + 1); + + return out; +} + +#endif diff --git a/bsdstubs.h b/bsdstubs.h index 53b6af6..7361af8 100644 --- a/bsdstubs.h +++ b/bsdstubs.h @@ -30,3 +30,8 @@ extern size_t strlcpy(char *dst, const char *src, size_t siz); #ifndef HAVE_STRLCAT extern size_t strlcat(char *dst, const char *src, size_t siz); #endif + +#ifndef HAVE_STRNDUP +extern char *strndup(const char *src, size_t len); +#endif +