]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libiberty, gcc: Add memrchr to libiberty and use it [PR119283].
authorIain Sandoe <iain@sandoe.co.uk>
Sat, 15 Mar 2025 22:30:20 +0000 (22:30 +0000)
committerIain Sandoe <iain@sandoe.co.uk>
Sat, 29 Mar 2025 13:53:22 +0000 (13:53 +0000)
This adds an implementation of memrchr to libiberty and arranges
to configure gcc to use it, if the host does not have it.

PR cobol/119283

gcc/ChangeLog:

* config.in: Regenerate.
* configure: Regenerate.
* configure.ac: Check for host memrchr.

include/ChangeLog:

* libiberty.h (memrchr): New.

libiberty/ChangeLog:

* Makefile.in: Add memrchr build rules.
* config.in: Regenerate.
* configure: Regenerate.
* configure.ac: Check for memrchr.
* functions.texi: Document memrchr.
* memrchr.c: New file.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
gcc/config.in
gcc/configure
gcc/configure.ac
include/libiberty.h
libiberty/Makefile.in
libiberty/config.in
libiberty/configure
libiberty/configure.ac
libiberty/functions.texi
libiberty/memrchr.c [new file with mode: 0644]

index 0d8a6ba18089de08d5440ca8411d0be32c369d12..7c89cab7717fc059e224307fdf87191077e129d3 100644 (file)
 #endif
 
 
+/* Define to 1 if you have the `memrchr' function. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_MEMRCHR
+#endif
+
+
 /* Define to 1 if you have the `mmap' function. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_MMAP
index 063b9ce6701171328de474a24ae26e310c244a7a..ab6bec1f0ae781d667f70db24b3b000cf772bb04 100755 (executable)
@@ -10640,7 +10640,7 @@ for ac_func in times clock kill getrlimit setrlimit atoq \
        popen sysconf strsignal getrusage nl_langinfo \
        gettimeofday mbstowcs wcswidth mmap posix_fallocate setlocale \
        clearerr_unlocked feof_unlocked   ferror_unlocked fflush_unlocked fgetc_unlocked fgets_unlocked   fileno_unlocked fprintf_unlocked fputc_unlocked fputs_unlocked   fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked   putchar_unlocked putc_unlocked madvise mallinfo mallinfo2 fstatat getauxval \
-       clock_gettime munmap msync get_current_dir_name
+       clock_gettime munmap msync get_current_dir_name memrchr
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_cxx_check_func "$LINENO" "$ac_func" "$as_ac_var"
index 3243472680c3ae71dd42c25b68aeb8c1fa6cec29..fca0579574fa617c7a46511cc621ea0f1ab01d1a 100644 (file)
@@ -1574,7 +1574,7 @@ AC_CHECK_FUNCS(times clock kill getrlimit setrlimit atoq \
        popen sysconf strsignal getrusage nl_langinfo \
        gettimeofday mbstowcs wcswidth mmap posix_fallocate setlocale \
        gcc_UNLOCKED_FUNCS madvise mallinfo mallinfo2 fstatat getauxval \
-       clock_gettime munmap msync get_current_dir_name)
+       clock_gettime munmap msync get_current_dir_name memrchr)
 
 # At least for glibc, clock_gettime is in librt.  But don't pull that
 # in if it still doesn't give us the function we want.
index f2e763a306a170b8f032b75465d3b9aba99fc637..d4e8791b14bafa2233d3dca671ea7d50e8bca78b 100644 (file)
@@ -215,6 +215,16 @@ extern int ffs(int);
 extern int mkstemps(char *, int);
 #endif
 
+#if defined (HAVE_DECL_MKSTEMPS) && !HAVE_DECL_MKSTEMPS
+extern int mkstemps(char *, int);
+#endif
+
+/* Make memrchr available on systems that do not have it.  */
+#if !defined (__GNU_LIBRARY__ ) && !defined (__linux__) && \
+    !defined (HAVE_MEMRCHR)
+extern void *memrchr(const void *, int, size_t);
+#endif
+
 /* Get the working directory.  The result is cached, so don't call
    chdir() between calls to getpwd().  */
 
index 4870fa95f2f374d00645e1656bd680e331841aae..ce54d88278d2eb56166ea26557efc684cce6391b 100644 (file)
@@ -139,8 +139,8 @@ CFILES = alloca.c argv.c asprintf.c atexit.c                                \
        ldirname.c                                                      \
        lrealpath.c                                                     \
        make-relative-prefix.c                                          \
-       make-temp-file.c md5.c memchr.c memcmp.c memcpy.c memmem.c      \
-        memmove.c mempcpy.c memset.c mkstemps.c                        \
+       make-temp-file.c md5.c memchr.c memrchr.c memcmp.c memcpy.c     \
+       memmem.c memmove.c mempcpy.c memset.c mkstemps.c                \
        objalloc.c obstack.c                                            \
        partition.c pexecute.c                                          \
         pex-common.c pex-djgpp.c pex-msdos.c pex-one.c                 \
@@ -213,8 +213,8 @@ CONFIGURED_OFILES = ./asprintf.$(objext) ./atexit.$(objext)         \
        ./getcwd.$(objext) ./getpagesize.$(objext)                      \
         ./gettimeofday.$(objext)                                       \
        ./index.$(objext) ./insque.$(objext)                            \
-       ./memchr.$(objext) ./memcmp.$(objext) ./memcpy.$(objext)        \
-       ./memmem.$(objext) ./memmove.$(objext)                          \
+       ./memchr.$(objext) ./memrchr.$(objext) ./memcmp.$(objext)       \
+       ./memcpy.$(objext) ./memmem.$(objext) ./memmove.$(objext)       \
         ./mempcpy.$(objext) ./memset.$(objext) ./mkstemps.$(objext)    \
        ./pex-djgpp.$(objext) ./pex-msdos.$(objext)                     \
         ./pex-unix.$(objext) ./pex-win32.$(objext)                     \
@@ -1025,6 +1025,15 @@ $(CONFIGURED_OFILES): stamp-picdir stamp-noasandir
        else true; fi
        $(COMPILE.c) $(srcdir)/memchr.c $(OUTPUT_OPTION)
 
+./memrchr.$(objext): $(srcdir)/memrchr.c $(INCDIR)/ansidecl.h
+       if [ x"$(PICFLAG)" != x ]; then \
+         $(COMPILE.c) $(PICFLAG) $(srcdir)/memrchr.c -o pic/$@; \
+       else true; fi
+       if [ x"$(NOASANFLAG)" != x ]; then \
+         $(COMPILE.c) $(PICFLAG) $(NOASANFLAG) $(srcdir)/memrchr.c -o noasan/$@; \
+       else true; fi
+       $(COMPILE.c) $(srcdir)/memrchr.c $(OUTPUT_OPTION)
+
 ./memcmp.$(objext): $(srcdir)/memcmp.c $(INCDIR)/ansidecl.h
        if [ x"$(PICFLAG)" != x ]; then \
          $(COMPILE.c) $(PICFLAG) $(srcdir)/memcmp.c -o pic/$@; \
index b055150961c5166cc87476163d05f9a99ec8e4ad..3b8716511f009a3874a5073f0b853558f1cd3687 100644 (file)
 /* Define to 1 if you have the <memory.h> header file. */
 #undef HAVE_MEMORY_H
 
+/* Define to 1 if you have the `memrchr' function. */
+#undef HAVE_MEMRCHR
+
 /* Define to 1 if you have the `memset' function. */
 #undef HAVE_MEMSET
 
index 347719c6262366ee2dd729fc7ad1fd25404694f4..02fbaabe89e792c02b377687deed133c1847d8be 100755 (executable)
@@ -6210,6 +6210,7 @@ funcs="$funcs gettimeofday"
 funcs="$funcs index"
 funcs="$funcs insque"
 funcs="$funcs memchr"
+funcs="$funcs memrchr"
 funcs="$funcs memcmp"
 funcs="$funcs memcpy"
 funcs="$funcs memmem"
@@ -6276,7 +6277,7 @@ if test "x" = "y"; then
     ffs __fsetlocking \
     getcwd getpagesize getrlimit getrusage getsysinfo gettimeofday \
     index insque \
-    memchr memcmp memcpy memmem memmove memset mkstemps \
+    memchr memrchr memcmp memcpy memmem memmove memset mkstemps \
     on_exit \
     pipe2 posix_spawn posix_spawnp psignal \
     pstat_getdynamic pstat_getstatic putenv \
@@ -6691,7 +6692,7 @@ esac
 
     for f in atexit basename bcmp bcopy bsearch bzero calloc clock ffs \
              getcwd getpagesize getrusage gettimeofday \
-             index insque memchr memcmp memcpy memmove memset psignal \
+             index insque memchr memrchr memcmp memcpy memmove memset psignal \
              putenv random rename rindex sbrk setenv stpcpy strcasecmp \
              strchr strdup strerror strncasecmp strrchr strstr strtod \
              strtol strtoul sysconf times tmpnam vfprintf vprintf \
index 5bad0f936920d0fccdfb05251792049150a882de..3de5eca0df2b6b66c6559b11d6cc1fb528813bad 100644 (file)
@@ -370,6 +370,7 @@ funcs="$funcs gettimeofday"
 funcs="$funcs index"
 funcs="$funcs insque"
 funcs="$funcs memchr"
+funcs="$funcs memrchr"
 funcs="$funcs memcmp"
 funcs="$funcs memcpy"
 funcs="$funcs memmem"
@@ -436,7 +437,7 @@ if test "x" = "y"; then
     ffs __fsetlocking \
     getcwd getpagesize getrlimit getrusage getsysinfo gettimeofday \
     index insque \
-    memchr memcmp memcpy memmem memmove memset mkstemps \
+    memchr memrchr memcmp memcpy memmem memmove memset mkstemps \
     on_exit \
     pipe2 posix_spawn posix_spawnp psignal \
     pstat_getdynamic pstat_getstatic putenv \
@@ -555,7 +556,7 @@ if test -n "${with_target_subdir}"; then
 
     for f in atexit basename bcmp bcopy bsearch bzero calloc clock ffs \
              getcwd getpagesize getrusage gettimeofday \
-             index insque memchr memcmp memcpy memmove memset psignal \
+             index insque memchr memrchr memcmp memcpy memmove memset psignal \
              putenv random rename rindex sbrk setenv stpcpy strcasecmp \
              strchr strdup strerror strncasecmp strrchr strstr strtod \
              strtol strtoul sysconf times tmpnam vfprintf vprintf \
index b56b02e06862a5204fd5763a46223df94bfe2680..7c7da1ba296671e272b8f2f8c1095f01913fd635 100644 (file)
@@ -749,6 +749,20 @@ returned.
 
 @end deftypefn
 
+@c memrchr.c:3
+@deftypefn Supplemental void* memrchr (const void *@var{s}, int @var{c}, @
+  size_t @var{n})
+
+This function searches memory for the character @var{c} in reverse order,
+starting at @code{*@var{s}+@var{n}-1} .  The search only ends with
+the first occurrence of @var{c}, or when the start us reached; in particular,
+a null character does not terminate the search.  If the character @var{c} is
+found within @var{length} characters of @code{*@var{s}}, a pointer
+to the character is returned.  If @var{c} is not found, then @code{NULL} is
+returned.
+
+@end deftypefn
+
 @c memcmp.c:6
 @deftypefn Supplemental int memcmp (const void *@var{x}, const void *@var{y}, @
   size_t @var{count})
diff --git a/libiberty/memrchr.c b/libiberty/memrchr.c
new file mode 100644 (file)
index 0000000..fe7713e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+
+@deftypefn Supplemental void* memrchr (const void *@var{s}, int @var{c}, @
+  size_t @var{n})
+
+This function searches memory for the character @var{c} in reverse order,
+starting at @code{*@var{s}+@var{n}-1} .  The search only ends with
+the first occurrence of @var{c}, or when the start us reached; in particular,
+a null character does not terminate the search.  If the character @var{c} is
+found within @var{length} characters of @code{*@var{s}}, a pointer
+to the character is returned.  If @var{c} is not found, then @code{NULL} is
+returned.
+
+@end deftypefn
+
+*/
+
+#include <ansidecl.h>
+#include <stddef.h>
+
+void *
+memrchr (const void *src_void, int c, size_t length)
+{
+  if (length == 0)
+    return NULL;
+
+  const unsigned char *p = (const unsigned char*)src_void;
+  p += length;
+  while (*--p != (unsigned char)c)
+    if (src_void == p)
+      return NULL;
+  return (void *)p;
+}