From 57b8f15fbd67709055c245222b0c20cd27d3c3a1 Mon Sep 17 00:00:00 2001
From: Bert Gijsbers <gijsbers@science.uva.nl>
Date: Mon, 31 Jul 2017 21:30:20 +0200
Subject: [PATCH] Changes for ALTLinux which has its own prototypes for
 strlcpy/strlcat. Also cleanup obsolete AC_FUNC_*s and sort -u the
 CHECK_FUNCs. Add two AC_RUN_IFELSE to detect correct operations of
 strlcpy/strcat. Define HAVE_STRLCPY / HAVE_STRLCAT if they do. Don't include
 our own strlcpy/strlcat if these are defined.

---
 configure.ac       | 49 ++++++++++++++++++++++++++++++++++++++++---------
 src/CMakeLists.txt | 19 +++++++------------
 src/base.h         |  5 +++++
 src/misc.cc        |  4 ++++
 4 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/configure.ac b/configure.ac
index fe9176ea..e00cb147 100644
--- a/configure.ac
+++ b/configure.ac
@@ -110,21 +110,52 @@ AC_CHECK_SIZEOF(long, 4)
 AC_TYPE_SIGNAL
 
 # Checks for library functions.
-AC_FUNC_STRFTIME
-AC_FUNC_VPRINTF
-AC_FUNC_ERROR_AT_LINE
 if test x$cross_compling != xyes ; then :; AC_FUNC_FORK fi
-AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
 if test x$cross_compling != xyes ; then :; AC_FUNC_MALLOC fi
-AC_FUNC_MMAP
 if test x$cross_compling != xyes ; then :; AC_FUNC_REALLOC fi
-AC_FUNC_STRNLEN
-AC_CHECK_FUNCS([dup2 getcwd gethostbyname gethostname gettimeofday mblen memchr memmove memset mkdir nl_langinfo putenv select setlocale socket strcasecmp strchr strcspn strdup strerror strncasecmp strrchr strspn strstr strtol sysinfo uname abort wordexp])
-AC_CHECK_FUNCS([gettimeofday putenv select socket strtol strtoul basename])
-AC_CHECK_FUNCS([sysctlbyname asprintf])
+AC_CHECK_FUNCS([abort basename dup2])
+AC_CHECK_FUNCS([gethostbyname gethostname gettimeofday])
+AC_CHECK_FUNCS([mblen memchr memmove memset mkdir nl_langinfo])
+AC_CHECK_FUNCS([select setlocale socket])
+AC_CHECK_FUNCS([strcasecmp strchr strcspn strdup strerror])
+AC_CHECK_FUNCS([strncasecmp strrchr strspn strstr strtol strtoul])
+AC_CHECK_FUNCS([sysctlbyname sysinfo uname wordexp])
 AC_CHECK_FUNC([getloadavg],[AC_DEFINE([HAVE_GETLOADAVG2], 1, [getloadavg() is available])])
 AC_FUNC_SELECT_ARGTYPES
 
+AC_MSG_CHECKING([for strlcpy])
+AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <string.h>]],[[
+               char a[2];
+               if (strlcpy(a, "yes", 2) != 3)
+                   return 1;]])],
+              [
+               AC_MSG_RESULT([yes])
+               AC_DEFINE_UNQUOTED([HAVE_STRLCPY], 1,
+                                  [Define to 1 if you have the strlcpy function.])
+              ],
+              [
+               AC_MSG_RESULT([no])
+               AC_DEFINE_UNQUOTED([HAVE_STRLCPY], 0,
+                                  [Define to 1 if you have the strlcpy function.])
+              ])
+
+AC_MSG_CHECKING([for strlcat])
+AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <string.h>]],[[
+               char a[4] = "no";
+               if (strlcat(a, "yes", 4) != 5)
+                   return 1;]])],
+              [
+               AC_MSG_RESULT([yes])
+               AC_DEFINE_UNQUOTED([HAVE_STRLCAT], 1,
+                                  [Define to 1 if you have the strlcat function.])
+              ],
+              [
+               AC_MSG_RESULT([no])
+               AC_DEFINE_UNQUOTED([HAVE_STRLCAT], 0,
+                                  [Define to 1 if you have the strlcat function.])
+              ])
+
+
 PKG_CHECK_MODULES([CORE],[fontconfig xext x11])
 AC_SUBST([CORE_CFLAGS])
 AC_SUBST([CORE_LIBS])
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index da476a0b..6f694c18 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -59,10 +59,12 @@ endif()
 # for x in `cat funclist` ; do grep $x src/* lib/* && echo $x >> exlist ; done 
 # perl -e 'print "CHECK_FUNCTION_EXISTS($_ HAVE_".uc($_).")\n" for @ARGV' `cat exlist`
 # perl -e 'print "#cmakedefine HAVE_".uc($_)."\n" for @ARGV' `cat exlist`
+CHECK_FUNCTION_EXISTS(abort HAVE_ABORT)
+CHECK_FUNCTION_EXISTS(basename HAVE_BASENAME)
 CHECK_FUNCTION_EXISTS(dup2 HAVE_DUP2)
-CHECK_FUNCTION_EXISTS(getcwd HAVE_GETCWD)
 CHECK_FUNCTION_EXISTS(gethostbyname HAVE_GETHOSTBYNAME)
 CHECK_FUNCTION_EXISTS(gethostname HAVE_GETHOSTNAME)
+CHECK_FUNCTION_EXISTS(getloadavg HAVE_GETLOADAVG)
 CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
 CHECK_FUNCTION_EXISTS(mblen HAVE_MBLEN)
 CHECK_FUNCTION_EXISTS(memchr HAVE_MEMCHR)
@@ -70,7 +72,6 @@ CHECK_FUNCTION_EXISTS(memmove HAVE_MEMMOVE)
 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
 CHECK_FUNCTION_EXISTS(mkdir HAVE_MKDIR)
 CHECK_FUNCTION_EXISTS(nl_langinfo HAVE_NL_LANGINFO)
-CHECK_FUNCTION_EXISTS(putenv HAVE_PUTENV)
 CHECK_FUNCTION_EXISTS(select HAVE_SELECT)
 CHECK_FUNCTION_EXISTS(setlocale HAVE_SETLOCALE)
 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
@@ -79,23 +80,17 @@ CHECK_FUNCTION_EXISTS(strchr HAVE_STRCHR)
 CHECK_FUNCTION_EXISTS(strcspn HAVE_STRCSPN)
 CHECK_FUNCTION_EXISTS(strdup HAVE_STRDUP)
 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
+CHECK_FUNCTION_EXISTS(strlcpy HAVE_STRLCPY)
+CHECK_FUNCTION_EXISTS(strlcat HAVE_STRLCAT)
 CHECK_FUNCTION_EXISTS(strncasecmp HAVE_STRNCASECMP)
 CHECK_FUNCTION_EXISTS(strrchr HAVE_STRRCHR)
 CHECK_FUNCTION_EXISTS(strspn HAVE_STRSPN)
 CHECK_FUNCTION_EXISTS(strstr HAVE_STRSTR)
 CHECK_FUNCTION_EXISTS(strtol HAVE_STRTOL)
+CHECK_FUNCTION_EXISTS(strtoul HAVE_STRTOL)
+CHECK_FUNCTION_EXISTS(sysctlbyname HAVE_SYSCTLBYNAME)
 CHECK_FUNCTION_EXISTS(sysinfo HAVE_SYSINFO)
 CHECK_FUNCTION_EXISTS(uname HAVE_UNAME)
-CHECK_FUNCTION_EXISTS(abort HAVE_ABORT)
-CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
-CHECK_FUNCTION_EXISTS(putenv HAVE_PUTENV)
-CHECK_FUNCTION_EXISTS(select HAVE_SELECT)
-CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
-CHECK_FUNCTION_EXISTS(strtol HAVE_STRTOL)
-CHECK_FUNCTION_EXISTS(basename HAVE_BASENAME)
-CHECK_FUNCTION_EXISTS(sysctlbyname HAVE_SYSCTLBYNAME)
-CHECK_FUNCTION_EXISTS(getloadavg HAVE_GETLOADAVG)
-CHECK_FUNCTION_EXISTS(asprintf HAVE_ASPRINTF)
 CHECK_FUNCTION_EXISTS(wordexp HAVE_WORDEXP)
 
 CHECK_LIBRARY_EXISTS (X11 XInternAtoms "" HAVE_XINTERNATOMS)
diff --git a/src/base.h b/src/base.h
index 0df34ccb..2083ca6f 100644
--- a/src/base.h
+++ b/src/base.h
@@ -49,9 +49,14 @@ inline T abs(T v) {
 /*** String Functions *********************************************************/
 
 /* Prefer this as a safer alternative over strcpy. Return strlen(from). */
+#ifndef HAVE_STRLCPY
 size_t strlcpy(char *dest, const char *from, size_t dest_size);
+#endif
+
 /* Prefer this over strcat. Return strlen(dest) + strlen(from). */
+#ifndef HAVE_STRLCAT
 size_t strlcat(char *dest, const char *from, size_t dest_size);
+#endif
 
 char *newstr(char const *str);
 char *newstr(char const *str, int len);
diff --git a/src/misc.cc b/src/misc.cc
index b0ac1c95..97638495 100644
--- a/src/misc.cc
+++ b/src/misc.cc
@@ -448,6 +448,7 @@ void operator delete[](void *p) {
 #endif
 
 /* Prefer this as a safer alternative over strcpy. Return strlen(from). */
+#ifndef HAVE_STRLCPY
 size_t strlcpy(char *dest, const char *from, size_t dest_size)
 {
     const char *in = from;
@@ -461,8 +462,10 @@ size_t strlcpy(char *dest, const char *from, size_t dest_size)
     while (*in) ++in;
     return in - from;
 }
+#endif
 
 /* Prefer this over strcat. Return strlen(dest) + strlen(from). */
+#ifndef HAVE_STRLCAT
 size_t strlcat(char *dest, const char *from, size_t dest_size)
 {
     char *to = dest;
@@ -470,6 +473,7 @@ size_t strlcat(char *dest, const char *from, size_t dest_size)
     while (to < stop && *to) ++to;
     return to - dest + strlcpy(to, from, dest_size - (to - dest));
 }
+#endif
 
 char *newstr(char const *str) {
     return (str != NULL ? newstr(str, strlen(str)) : NULL);
-- 
2.15.0