Skip to content

Commit

Permalink
Merge from oconfigure.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristaps committed Mar 6, 2018
1 parent e84a6a6 commit 9d3ae0e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
73 changes: 73 additions & 0 deletions compats.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,79 @@ strlcpy(char *dst, const char *src, size_t siz)
return(s - src - 1); /* count does not include NUL */
}
#endif /* !HAVE_STRLCPY */
#if !HAVE_STRNDUP
/* $OpenBSD$ */
/*
* 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;
}
#endif /* !HAVE_STRNDUP */
#if !HAVE_STRNLEN
/* $OpenBSD$ */

/*
* 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);
}
#endif /* !HAVE_STRNLEN */
#if !HAVE_STRTONUM
/*
* Copyright (c) 2004 Ted Unangst and Todd Miller
Expand Down
16 changes: 15 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ runtest seccomp-filter SECCOMP_FILTER || true
runtest SOCK_NONBLOCK SOCK_NONBLOCK || true
runtest strlcat STRLCAT || true
runtest strlcpy STRLCPY || true
runtest strndup STRNDUP || true
runtest strnlen STRNLEN || true
runtest strtonum STRTONUM || true
runtest systrace SYSTRACE || true
runtest zlib ZLIB "" "-lz" || true
Expand Down Expand Up @@ -306,7 +308,9 @@ __HEREDOC__
${HAVE_REALLOCARRAY} -eq 0 -o \
${HAVE_RECALLOCARRAY} -eq 0 -o \
${HAVE_STRLCAT} -eq 0 -o \
${HAVE_STRLCPY} -eq 0 ] \
${HAVE_STRLCPY} -eq 0 -o \
${HAVE_STRNDUP} -eq 0 -o \
${HAVE_STRNLEN} -eq 0 ] \
&& echo "#include <sys/types.h>"

# Now we handle our HAVE_xxxx values.
Expand Down Expand Up @@ -339,6 +343,8 @@ cat << __HEREDOC__
#define HAVE_SOCK_NONBLOCK ${HAVE_SOCK_NONBLOCK}
#define HAVE_STRLCAT ${HAVE_STRLCAT}
#define HAVE_STRLCPY ${HAVE_STRLCPY}
#define HAVE_STRNDUP ${HAVE_STRNDUP}
#define HAVE_STRNLEN ${HAVE_STRNLEN}
#define HAVE_STRTONUM ${HAVE_STRTONUM}
#define HAVE_SYSTRACE ${HAVE_SYSTRACE}
#define HAVE_ZLIB ${HAVE_ZLIB}
Expand Down Expand Up @@ -420,6 +426,14 @@ if [ ${HAVE_STRLCPY} -eq 0 ]; then
echo "extern size_t strlcpy(char *, const char *, size_t);"
fi

if [ ${HAVE_STRNDUP} -eq 0 ]; then
echo "extern char *strndup(const char *, size_t);"
fi

if [ ${HAVE_STRNLEN} -eq 0 ]; then
echo "extern size_t strnlen(const char *, size_t);"
fi

if [ ${HAVE_STRTONUM} -eq 0 ]; then
echo "extern long long strtonum(const char *, long long, long long, const char **);"
fi
Expand Down
26 changes: 26 additions & 0 deletions tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,32 @@ main(void)
buf[0] == 'a' && buf[1] == '\0');
}
#endif /* TEST_STRLCPY */
#if TEST_STRNDUP
#include <string.h>

int
main(void)
{
const char *foo = "bar";
char *baz;

baz = strndup(foo, 1);
return(0 != strcmp(baz, "b"));
}
#endif /* TEST_STRNDUP */
#if TEST_STRNLEN
#include <string.h>

int
main(void)
{
const char *foo = "bar";
size_t sz;

sz = strnlen(foo, 1);
return(1 != sz);
}
#endif /* TEST_STRNLEN */
#if TEST_STRTONUM
/*
* Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
Expand Down

0 comments on commit 9d3ae0e

Please sign in to comment.