From: Eric Bollengier Date: Wed, 14 Sep 2022 13:36:36 +0000 (+0200) Subject: Add bstrcasestr() for non GNU platforms X-Git-Tag: Beta-15.0.0~509 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ddbb791d2716cdabace476e9c28fa8482cd774b8;p=thirdparty%2Fbacula.git Add bstrcasestr() for non GNU platforms --- diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index 2b0a17d69..c42cb4ee4 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -1879,6 +1879,29 @@ int get_home_directories(const char *grpname, alist *dirs) return (dirs->size() > 0) ? 0 : -1; } +/* Implementation of strcasestr() for non-gnu platforms */ +char *bstrcasestr(const char *haystack, const char *needle) +{ + int needle_len; /* Length of needle. */ + int haystack_len; /* Known minimum length of haystack. */ + + if (!needle || needle[0] == '\0') { + return (char *)haystack; + } + needle_len = strlen (needle); + haystack_len = strlen (haystack); + + const char *p = haystack; + while (p && *p && haystack_len >= needle_len) { + if (strncasecmp(needle, p, needle_len) == 0) { + return (char *)p; + } + p++; + haystack_len--; + } + return NULL; +} + #ifdef TEST_PROGRAM #include "unittests.h" @@ -2136,6 +2159,17 @@ int main(int argc, char **argv) i++; } + // Test bstrcasestr() + { + const char *p1 = "This is a test"; + ok(bstrcasestr(p1, "toto") == NULL, "Test with non existing string"); + ok(bstrcasestr(p1, NULL) == p1, "Test with NULL string"); + ok(bstrcasestr(p1, "") == p1, "Test with empty string"); + is(bstrcasestr(p1, "TEST"), "test", "Test with upper string at the end"); + is(bstrcasestr(p1, "this"), p1, "Test with lower string at the start"); + ok(bstrcasestr(p1, "xxxxxxxxxxxxxxxxxxxxxxxxx") == NULL, "Test with non existing string"); + is(bstrcasestr(p1, " iS"), " is a test", "Test with a middle string"); + } return report(); } #endif diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index 4348a2fac..2e5e26ed8 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -74,6 +74,7 @@ const char *get_timezone(); int get_user_home_directory(const char *user, POOLMEM *&home); int get_home_directories(const char *grpname, alist *dirs); char *ucfirst(char *dest, const char *src, int len); +char *bstrcasestr(char *haystack, char *needle); typedef enum { WAIT_READ = 1, WAIT_WRITE = 2