Skip to content

Commit ef34af2

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
[PATCH] strcasestr compatibility replacement
Some C libraries lack strcasestr(); add a stupid replacement to help folks with such. [jc: original Linus posting, updated with his "also need <ctype.h>", updated further with a fix from Joachim B Haga <cjhaga@fys.uio.no>"] Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 7271328 commit ef34af2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# Define NO_CURL if you do not have curl installed. git-http-pull is not
1010
# built, and you cannot use http:// and https:// transports.
1111
#
12+
# Define NO_STRCASESTR if you don't have strcasestr.
13+
#
1214
# Define PPC_SHA1 environment variable when running make to make use of
1315
# a bundled SHA1 routine optimized for PowerPC.
1416
#
@@ -203,6 +205,10 @@ ifdef NEEDS_NSL
203205
LIBS += -lnsl
204206
SIMPLE_LIB += -lnsl
205207
endif
208+
ifdef NO_STRCASESTR
209+
DEFINES += -Dstrcasestr=gitstrcasestr
210+
LIB_OBJS += compat/strcasestr.o
211+
endif
206212

207213
DEFINES += '-DSHA1_HEADER=$(SHA1_HEADER)'
208214

compat/strcasestr.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <string.h>
2+
#include <ctype.h>
3+
4+
char *gitstrcasestr(const char *haystack, const char *needle)
5+
{
6+
int nlen = strlen(needle);
7+
int hlen = strlen(haystack) - nlen + 1;
8+
int i;
9+
10+
for (i = 0; i < hlen; i++) {
11+
int j;
12+
for (j = 0; j < nlen; j++) {
13+
unsigned char c1 = haystack[i+j];
14+
unsigned char c2 = needle[j];
15+
if (toupper(c1) != toupper(c2))
16+
goto next;
17+
}
18+
return (char *) haystack + i;
19+
next:
20+
;
21+
}
22+
return NULL;
23+
}

0 commit comments

Comments
 (0)