diff options
-rw-r--r-- | config.h.in | 6 | ||||
-rwxr-xr-x | configure | 2 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | xmalloc.c | 23 |
4 files changed, 31 insertions, 2 deletions
diff --git a/config.h.in b/config.h.in index 71054f3..7d1a5e0 100644 --- a/config.h.in +++ b/config.h.in @@ -56,12 +56,18 @@ /* Define to 1 if you have the `strchr' function. */ #undef HAVE_STRCHR +/* Define to 1 if you have the `strdup' function. */ +#undef HAVE_STRDUP + /* Define to 1 if you have the <strings.h> header file. */ #undef HAVE_STRINGS_H /* Define to 1 if you have the <string.h> header file. */ #undef HAVE_STRING_H +/* Define to 1 if you have the `strndup' function. */ +#undef HAVE_STRNDUP + /* Define to 1 if you have the `strrchr' function. */ #undef HAVE_STRRCHR @@ -5082,7 +5082,7 @@ _ACEOF fi done -for ac_func in strchr strrchr strspn strcasecmp +for ac_func in strchr strrchr strspn strcasecmp strdup strndup do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/configure.ac b/configure.ac index 7a92b8f..0a85042 100644 --- a/configure.ac +++ b/configure.ac @@ -35,7 +35,7 @@ AC_TYPE_SIZE_T AC_FUNC_MALLOC AC_FUNC_REALLOC AC_CHECK_FUNCS([mblen memset nl_langinfo pathconf regcomp setlocale]) -AC_CHECK_FUNCS([strchr strrchr strspn strcasecmp]) +AC_CHECK_FUNCS([strchr strrchr strspn strcasecmp strdup strndup]) AC_CANONICAL_HOST # OS-specific tests @@ -16,6 +16,7 @@ #include <string.h> #include <errno.h> +#include "config.h" #include "xmalloc.h" /* ================== */ @@ -93,8 +94,19 @@ xstrdup(const char * p) { char * allocated; +#ifdef HAVE_STRDUP + allocated = strdup(p); + + if (allocated == NULL) + { + fprintf(stderr, "Error: Insufficient memory for strdup.\n"); + + exit(EXIT_FAILURE); + } +#else allocated = xmalloc(strlen(p) + 1); strcpy(allocated, p); +#endif return allocated; } @@ -108,6 +120,16 @@ xstrndup(const char * str, size_t len) { char * p; +#ifdef HAVE_STRNDUP + p = strndup(str, len); + + if (p == NULL) + { + fprintf(stderr, "Error: Insufficient memory for strndup.\n"); + + exit(EXIT_FAILURE); + } +#else p = memchr(str, '\0', len); if (p) @@ -116,6 +138,7 @@ xstrndup(const char * str, size_t len) p = xmalloc(len + 1); memcpy(p, str, len); p[len] = '\0'; +#endif return p; } |