Skip to content

Commit

Permalink
Not all systems provide memrchr, example are some MacOS/OSX variants.
Browse files Browse the repository at this point in the history
  • Loading branch information
mywave82 committed Dec 29, 2024
1 parent 617c3aa commit 447a598
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@

#undef HAVE_MEMMEM

#undef HAVE_MEMRCHR

#undef HAVE_QSORT

#undef HAVE_STRUPR
Expand Down
7 changes: 7 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -8226,6 +8226,13 @@ then :

fi

ac_fn_c_check_func "$LINENO" "memrchr" "ac_cv_func_memrchr"
if test "x$ac_cv_func_memrchr" = xyes
then :
printf "%s\n" "#define HAVE_MEMRCHR 1" >>confdefs.h

fi

ac_fn_c_check_func "$LINENO" "getopt" "ac_cv_func_getopt"
if test "x$ac_cv_func_getopt" = xyes
then :
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ AC_LINK_IFELSE(
)
AC_CHECK_FUNCS([memmem])
AC_CHECK_FUNCS([memrchr])
AC_CHECK_FUNCS([getopt])
AC_CHECK_FUNCS([getpwuid])
AC_CHECK_FUNCS([popen])
Expand Down
1 change: 1 addition & 0 deletions stuff/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ file.o: file.c \
../config.h \
../types.h \
../boot/psetting.h \
../stuff/compat.h \
../stuff/file.h
$(CC) $< -o $@ -c

Expand Down
33 changes: 33 additions & 0 deletions stuff/compat-test.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "config.h"
#include "types.h"

#undef HAVE_MEMRCHR
#define memrchr ocp_memrchr

#include "compat.h"

#include "compat.c"

#undef memrchr

#include <string.h>
#include <unistd.h>

Expand Down Expand Up @@ -276,6 +281,32 @@ static int test_splitpath42 (void)
return failed;
}

int test_memrchr (const char *s, const char c, size_t n, const char *e, const char *d)
{
char *r = ocp_memrchr (s, c, n);
printf("%s: %s\n", d, (r != e) ? "failed" : "ok");
return r != e;
}

int do_test_memchr (void)
{
const char *ooo = "ooo";
const char *ofo = "ofo";

const char *oooo = "oooo";
const char *foobarfoo = "foobarfoo";

int retval = 0;
retval |= test_memrchr ("foo", 'a', 3, 0, "memrchr(\"foo\", 'a', 3) => NULL");
retval |= test_memrchr (ooo+1, 'o', 0, 0, "memrchr(\"ooo\" + 1, 'o', 0) => NULL");
retval |= test_memrchr (ooo+1, 'o', 1, ooo+1, "memrchr(\"ooo\" + 1, 'o') => \"ooo\" + 1");
retval |= test_memrchr (ofo+1, 'o', 1, 0, "memrchr(\"ofo\" + 1, 'o') => NULL");
retval |= test_memrchr (oooo+1, 'o', 2, oooo+2, "memrchr(\"oooo\" + 1, 'o') => \"oooo\" + 2");
retval |= test_memrchr (foobarfoo, 'r', 9, foobarfoo + 5, "memrchr(\"foobarfoo\", 'r') => \"rfoo\"");

return retval;
}

int main(int argc, char *argv[])
{
int retval = 0;
Expand All @@ -284,6 +315,8 @@ int main(int argc, char *argv[])

retval |= test_splitpath42 ();

retval |= do_test_memchr ();

if (retval)
{
printf ("%sSomething failed%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
Expand Down
15 changes: 15 additions & 0 deletions stuff/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ void getext_malloc (const char *src, char **ext);
extern int splitpath4_malloc(const char *src, char **drive, char **path, char **file, char **ext); /* returns non-zero on errors */
extern int splitpath_malloc(const char *src, char **drive, char **path, char **filename); /* returns non-zero on errors */

#ifndef HAVE_MEMRCHR
static inline void *memrchr(const void *s, int c, size_t n)
{
char c2 = c;
char *s2 = (char *)s + n - 1;
while (n--)
{
if (*s2 == c2) return s2;
s2--;
}

return 0;
}
#endif

#ifndef HAVE_STRUPR
#include <ctype.h>
static inline char *strupr(char *src)
Expand Down
1 change: 1 addition & 0 deletions stuff/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "types.h"

#include "boot/psetting.h"
#include "stuff/compat.h"
#include "stuff/file.h"

struct osfile_cacheline_t
Expand Down

0 comments on commit 447a598

Please sign in to comment.