Add strdup and strndup to compat
This commit is contained in:
52
compat/strdup.c
Normal file
52
compat/strdup.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#ifdef STDC_HEADERS
|
||||
# include <stdlib.h>
|
||||
# include <stddef.h>
|
||||
#else
|
||||
# ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
# endif
|
||||
#endif /* STDC_HEADERS */
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#else
|
||||
# ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
# endif
|
||||
#endif /* HAVE_STRING_H */
|
||||
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
|
||||
# include <malloc.h>
|
||||
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
|
||||
|
||||
#include "compat.h"
|
||||
|
||||
char *
|
||||
strdup(const char *src)
|
||||
{
|
||||
char *dst = NULL;
|
||||
size_t len = strlen(src);
|
||||
|
||||
dst = (char *) malloc(len + 1);
|
||||
(void) memcpy(dst, src, len);
|
||||
dst[len] = '\0';
|
||||
|
||||
return dst;
|
||||
}
|
55
compat/strndup.c
Normal file
55
compat/strndup.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#ifdef STDC_HEADERS
|
||||
# include <stdlib.h>
|
||||
# include <stddef.h>
|
||||
#else
|
||||
# ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
# endif
|
||||
#endif /* STDC_HEADERS */
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#else
|
||||
# ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
# endif
|
||||
#endif /* HAVE_STRING_H */
|
||||
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
|
||||
# include <malloc.h>
|
||||
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
|
||||
|
||||
#include "compat.h"
|
||||
|
||||
char *
|
||||
strndup(const char *src, size_t maxlen)
|
||||
{
|
||||
char *dst = NULL;
|
||||
size_t len = strlen(src);
|
||||
|
||||
if (len > maxlen)
|
||||
len = maxlen;
|
||||
dst = (char *) malloc(len + 1);
|
||||
(void) memcpy(dst, src, len);
|
||||
dst[len] = '\0';
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
4
configure
vendored
4
configure
vendored
@@ -19280,7 +19280,9 @@ esac
|
||||
|
||||
|
||||
|
||||
for ac_func in memrchr strerror strcasecmp sigaction strlcpy strlcat
|
||||
|
||||
|
||||
for ac_func in memrchr strerror strcasecmp sigaction strlcpy strlcat strdup strndup
|
||||
do
|
||||
as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
|
||||
{ echo "$as_me:$LINENO: checking for $ac_func" >&5
|
||||
|
@@ -1934,7 +1934,7 @@ AC_CHECK_FUNCS(utimes, [AC_CHECK_FUNCS(futimes futimesat, [break])], [AC_CHECK_F
|
||||
AC_CHECK_FUNCS(killpg, [], [AC_LIBOBJ(killpg)])
|
||||
SUDO_FUNC_FNMATCH([AC_DEFINE(HAVE_FNMATCH)], [AC_LIBOBJ(fnmatch)])
|
||||
SUDO_FUNC_ISBLANK
|
||||
AC_REPLACE_FUNCS(memrchr strerror strcasecmp sigaction strlcpy strlcat)
|
||||
AC_REPLACE_FUNCS(memrchr strerror strcasecmp sigaction strlcpy strlcat strdup strndup)
|
||||
AC_CHECK_FUNCS(nanosleep, [], [
|
||||
# On Solaris, nanosleep is in librt
|
||||
AC_CHECK_LIB(rt, nanosleep, [LIBS="${LIBS} -lrt"], [AC_LIBOBJ(nanosleep)])
|
||||
|
@@ -113,6 +113,10 @@ $(LIBOBJDIR)snprintf.lo: $(compat)/snprintf.c $(incdir)/compat.h $(top_builddir)
|
||||
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/snprintf.c
|
||||
$(LIBOBJDIR)strcasecmp.lo: $(compat)/strcasecmp.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strcasecmp.c
|
||||
$(LIBOBJDIR)strdup.lo: $(compat)/strdup.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strdup.c
|
||||
$(LIBOBJDIR)strndup.lo: $(compat)/strdup.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strndup.c
|
||||
$(LIBOBJDIR)strerror.lo: $(compat)/strerror.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strerror.c
|
||||
$(LIBOBJDIR)strlcat.lo: $(compat)/strlcat.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
|
@@ -179,6 +179,10 @@ $(LIBOBJDIR)snprintf.o: $(compat)/snprintf.c $(incdir)/compat.h $(top_builddir)/
|
||||
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/snprintf.c
|
||||
$(LIBOBJDIR)strcasecmp.o: $(compat)/strcasecmp.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strcasecmp.c
|
||||
$(LIBOBJDIR)strdup.o: $(compat)/strdup.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strdup.c
|
||||
$(LIBOBJDIR)strndup.o: $(compat)/strndup.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strndup.c
|
||||
$(LIBOBJDIR)strerror.o: $(compat)/strerror.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(compat)/strerror.c
|
||||
$(LIBOBJDIR)strlcat.o: $(compat)/strlcat.c $(incdir)/compat.h $(top_builddir)/config.h
|
||||
|
Reference in New Issue
Block a user