From 48032aed00246327132bc4262b8d9e22680778ba Mon Sep 17 00:00:00 2001 From: Andreas Steffen Date: Fri, 9 Jan 2009 01:19:45 +0000 Subject: [PATCH] add a compatible memrchr() function if the platform does not support it (e.g. old glibc). Patch courtesy to Thomas Jarosch --- configure.in | 1 + src/libstrongswan/Makefile.am | 1 + src/libstrongswan/utils/lexparser.c | 3 ++ src/libstrongswan/utils/memrchr.c | 45 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 src/libstrongswan/utils/memrchr.c diff --git a/configure.in b/configure.in index ff1c25ee8..45dc82936 100644 --- a/configure.in +++ b/configure.in @@ -707,6 +707,7 @@ dnl ========================================== AC_HAVE_LIBRARY(dl) AC_CHECK_FUNCS(backtrace) AC_CHECK_FUNCS(dladdr) +AC_REPLACE_FUNCS(memrchr) AC_MSG_CHECKING([for gcc atomic operations]) AC_TRY_RUN( diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 1463d415f..aa0e1d83e 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -47,6 +47,7 @@ utils/identification.c utils/identification.h \ utils/iterator.h \ utils/lexparser.c utils/lexparser.h \ utils/linked_list.c utils/linked_list.h \ +utils/memrchr.c \ utils/hashtable.c utils/hashtable.h \ utils/enumerator.c utils/enumerator.h \ utils/optionsfrom.c utils/optionsfrom.h \ diff --git a/src/libstrongswan/utils/lexparser.c b/src/libstrongswan/utils/lexparser.c index 34eb340a5..c351e7e24 100644 --- a/src/libstrongswan/utils/lexparser.c +++ b/src/libstrongswan/utils/lexparser.c @@ -20,6 +20,9 @@ #include "lexparser.h" +#ifndef HAVE_MEMRCHR +void *memrchr(const void *s, int c, size_t n); +#endif /** * eat whitespace diff --git a/src/libstrongswan/utils/memrchr.c b/src/libstrongswan/utils/memrchr.c new file mode 100644 index 000000000..efcd03aa0 --- /dev/null +++ b/src/libstrongswan/utils/memrchr.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2008 Thomas Jarosch + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef HAVE_MEMRCHR + +#include + +void *memrchr(const void *s, int c, size_t n) +{ + unsigned char *reverse_search; + + if (s == NULL || n == 0) + { + return NULL; + } + + reverse_search = s + n; + + for (;;) + { + if (*reverse_search == (unsigned char)c) + { + return reverse_search; + } + else if (reverse_search == s) + { + break; + } + reverse_search--; + } + return NULL; +} + +#endif