[PATCH v2] fix build on systems without memrchr(3)

Martijn Dekker <[email protected]>
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
Op 22-06-2024 om 15:25 schreef Martijn Dekker:
> memrchr(3) is non-standard, and has been ported from glibc to FreeBSD, NetSBD 
> and OpenBSD, but not to macOS, at least as of 12.7.5. So we need a test for 
> it. As far as I can tell, *name is a zero-terminated C string, so it should 
> work to use strrchr(3) as a fallback.

Reading the code more closely, that's nonsense, because 'p' does not point to 
the end of the string if metacharacters are found.

Guess the best we can do is provide a simple local fallback implementation of 
memrchr(3). Patch v2 attached.

-- 
||	modernish -- harness the shell
||	https://github.com/modernish/modernish
||
||	KornShell lives!
||	https://github.com/ksh93/ksh
memrchr.patch (text/plain, 978 B)
diff --git a/configure.ac b/configure.ac
index 338d5bd..ba4856a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -87,7 +87,7 @@ AC_CHECK_DECL([PRIdMAX],,
 
 dnl Checks for library functions.
 AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit isalpha killpg \
-	       memfd_create mempcpy \
+	       memfd_create memrchr mempcpy \
 	       sigsetmask stpcpy strchrnul strsignal strtod strtoimax \
 	       strtoumax sysconf tee)
 
diff --git a/src/expand.c b/src/expand.c
index 6912e39..e3056a0 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -1633,6 +1633,17 @@ static char *expmeta_rmescapes(char *enddir, const char *name)
 	return enddir - 1;
 }
 
+#ifndef HAVE_MEMRCHR
+static void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *str = (const unsigned char *)s;
+	for (const unsigned char *cp = str + n - 1; cp >= str; cp--)
+		if (*cp == c)
+			return (void *)cp;
+	return NULL;
+}
+#endif
+
 /*
  * Do metacharacter (i.e. *, ?, [...]) expansion.
  */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.