From c64b35d2843e9d2d03775aee59c1b17a05b1d3ad Mon Sep 17 00:00:00 2001 From: pgen Date: Mon, 12 Jun 2023 23:25:04 +0200 Subject: Rework xstrdup and xstrndup smenu can now be compiled without warning when using the following options: -std=c99 -Werror=implicit-function-declaration -Werror=implicit-int -Werror=int-conversion -Werror=old-style-definition --- config.h.in | 6 ------ configure | 12 ------------ configure.ac | 2 +- xmalloc.c | 39 ++++++++++++++++----------------------- 4 files changed, 17 insertions(+), 42 deletions(-) diff --git a/config.h.in b/config.h.in index c249090..47cb3d3 100644 --- a/config.h.in +++ b/config.h.in @@ -62,18 +62,12 @@ /* 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 header file. */ #undef HAVE_STRINGS_H /* Define to 1 if you have the 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 diff --git a/configure b/configure index 0fbc221..417f75d 100755 --- a/configure +++ b/configure @@ -6270,18 +6270,6 @@ then : printf "%s\n" "#define HAVE_STRCASECMP 1" >>confdefs.h fi -ac_fn_c_check_func "$LINENO" "strdup" "ac_cv_func_strdup" -if test "x$ac_cv_func_strdup" = xyes -then : - printf "%s\n" "#define HAVE_STRDUP 1" >>confdefs.h - -fi -ac_fn_c_check_func "$LINENO" "strndup" "ac_cv_func_strndup" -if test "x$ac_cv_func_strndup" = xyes -then : - printf "%s\n" "#define HAVE_STRNDUP 1" >>confdefs.h - -fi diff --git a/configure.ac b/configure.ac index 1455f30..2444308 100644 --- a/configure.ac +++ b/configure.ac @@ -42,7 +42,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 strdup strndup]) +AC_CHECK_FUNCS([strchr strrchr strspn strcasecmp]) AC_CANONICAL_HOST # OS-specific tests diff --git a/xmalloc.c b/xmalloc.c index 2f148f5..cd2ffd6 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -126,25 +126,21 @@ xrealloc(void * p, size_t size) /* Displays an error message and exits gracefully if an error occurs. */ /* ================================================================== */ char * -xstrdup(const char * p) +xstrdup(const char * str) { - char * allocated; + char * p; -#ifdef HAVE_STRDUP - allocated = strdup(p); + p = malloc(strlen(str) + 1); - if (allocated == NULL) + if (p == NULL) { - fprintf(stderr, "Error: Insufficient memory for strdup.\n"); - + fprintf(stderr, "Error: Insufficient memory for xstrdup.\n"); exit(EXIT_FAILURE); } -#else - allocated = xmalloc(strlen(p) + 1); - strcpy(allocated, p); -#endif - return allocated; + strcpy(p, str); + + return p; } /* ================================================================== */ @@ -156,26 +152,23 @@ char * xstrndup(const char * str, size_t len) { char * p; + size_t l; -#ifdef HAVE_STRNDUP - p = strndup(str, len); + p = memchr(str, '\0', len); + + if (p) + len = p - str; + + p = malloc(len + 1); if (p == NULL) { - fprintf(stderr, "Error: Insufficient memory for strndup.\n"); - + fprintf(stderr, "Error: Insufficient memory for xstrndup.\n"); exit(EXIT_FAILURE); } -#else - p = memchr(str, '\0', len); - if (p) - len = p - str; - - p = xmalloc(len + 1); memcpy(p, str, len); p[len] = '\0'; -#endif return p; } -- cgit v1.2.3