summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2023-06-12 23:25:04 +0200
committerpgen <p.gen.progs@gmail.com>2023-06-14 00:24:05 +0200
commitc64b35d2843e9d2d03775aee59c1b17a05b1d3ad (patch)
tree667b4d20d08e2fc5ee52176e155cf6030f56668a
parent131b94c135b9e0a72b482d365bca1b533c7e5c2a (diff)
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
-rw-r--r--config.h.in6
-rwxr-xr-xconfigure12
-rw-r--r--configure.ac2
-rw-r--r--xmalloc.c39
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 <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
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;
}