bsdstubs: implement strndup() stub implementation if none exists

feature/tap-sh
William Pitcock 2012-04-30 18:11:51 +00:00
parent feb26b8168
commit c212908c44
2 changed files with 42 additions and 0 deletions

View File

@ -96,3 +96,40 @@ strlcat(char *dst, const char *src, size_t siz)
} }
#endif #endif
/*
* Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
*
* 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

View File

@ -30,3 +30,8 @@ extern size_t strlcpy(char *dst, const char *src, size_t siz);
#ifndef HAVE_STRLCAT #ifndef HAVE_STRLCAT
extern size_t strlcat(char *dst, const char *src, size_t siz); extern size_t strlcat(char *dst, const char *src, size_t siz);
#endif #endif
#ifndef HAVE_STRNDUP
extern char *strndup(const char *src, size_t len);
#endif