diff mbox series

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

Message ID a64ad0a4-ea0f-4de3-aa2b-e377d3bef85d@inlv.org (mailing list archive)
State Under Review
Delegated to: Herbert Xu
Headers show
Series [v2] fix build on systems without memrchr(3) | expand

Commit Message

Martijn Dekker June 24, 2024, 4:23 p.m. UTC
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.
diff mbox series

Patch

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.
  */