Add strchrnul()
As suggested by Pierre Habouzit, add strchrnul(). It's a useful GNU extension and can simplify string parser code. There are several places in git that can be converted to strchrnul(); as a trivial example, this patch introduces its usage to builtin-fetch--tool.c. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Nov 9, 2007 at 01:49 UTC
659c69cfef984e7416decc78841877207e9d5914
4 files changed
+28
-6
Makefile
+13
@@ -30,6 +30,8 @@ all::
30
#
31
# Define NO_MEMMEM if you don't have memmem.
32
#
33
+# Define NO_STRCHRNUL if you don't have strchrnul.
34
+#
35
# Define NO_STRLCPY if you don't have strlcpy.
36
#
37
# Define NO_STRTOUMAX if you don't have strtoumax in the C library.
@@ -406,6 +408,7 @@ ifeq ($(uname_S),Darwin)
408
OLD_ICONV = UnfortunatelyYes
409
NO_STRLCPY = YesPlease
410
NO_MEMMEM = YesPlease
411
+ NO_STRCHRNUL = YesPlease
412
endif
413
ifeq ($(uname_S),SunOS)
414
NEEDS_SOCKET = YesPlease
@@ -413,6 +416,7 @@ ifeq ($(uname_S),SunOS)
416
SHELL_PATH = /bin/bash
417
NO_STRCASESTR = YesPlease
418
NO_MEMMEM = YesPlease
419
+ NO_STRCHRNUL = YesPlease
420
NO_HSTRERROR = YesPlease
421
ifeq ($(uname_R),5.8)
422
NEEDS_LIBICONV = YesPlease
@@ -438,6 +442,7 @@ ifeq ($(uname_O),Cygwin)
442
NO_D_INO_IN_DIRENT = YesPlease
443
NO_STRCASESTR = YesPlease
444
NO_MEMMEM = YesPlease
445
+ NO_STRCHRNUL = YesPlease
446
NO_SYMLINK_HEAD = YesPlease
447
NEEDS_LIBICONV = YesPlease
448
NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@@ -452,12 +457,14 @@ endif
457
ifeq ($(uname_S),FreeBSD)
458
NEEDS_LIBICONV = YesPlease
459
NO_MEMMEM = YesPlease
460
+ NO_STRCHRNUL = YesPlease
461
BASIC_CFLAGS += -I/usr/local/include
462
BASIC_LDFLAGS += -L/usr/local/lib
463
endif
464
ifeq ($(uname_S),OpenBSD)
465
NO_STRCASESTR = YesPlease
466
NO_MEMMEM = YesPlease
467
+ NO_STRCHRNUL = YesPlease
468
NEEDS_LIBICONV = YesPlease
469
BASIC_CFLAGS += -I/usr/local/include
470
BASIC_LDFLAGS += -L/usr/local/lib
@@ -473,6 +480,7 @@ endif
480
ifeq ($(uname_S),AIX)
481
NO_STRCASESTR=YesPlease
482
NO_MEMMEM = YesPlease
483
+ NO_STRCHRNUL = YesPlease
484
NO_STRLCPY = YesPlease
485
NEEDS_LIBICONV=YesPlease
486
endif
@@ -485,6 +493,7 @@ ifeq ($(uname_S),IRIX64)
493
NO_SETENV=YesPlease
494
NO_STRCASESTR=YesPlease
495
NO_MEMMEM = YesPlease
496
+ NO_STRCHRNUL = YesPlease
497
NO_STRLCPY = YesPlease
498
NO_SOCKADDR_STORAGE=YesPlease
499
SHELL_PATH=/usr/gnu/bin/bash
@@ -695,6 +704,10 @@ ifdef NO_MEMMEM
704
COMPAT_CFLAGS += -DNO_MEMMEM
705
COMPAT_OBJS += compat/memmem.o
706
endif
707
+ifdef NO_STRCHRNUL
708
+ COMPAT_CFLAGS += -DNO_STRCHRNUL
709
+ COMPAT_OBJS += compat/strchrnul.o
710
+endif
711
712
ifdef THREADED_DELTA_SEARCH
713
BASIC_CFLAGS += -DTHREADED_DELTA_SEARCH
builtin-fetch--tool.c
+2
-6
@@ -435,9 +435,7 @@ static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_resu
435
cp++;
436
if (!*cp)
437
break;
438
- np = strchr(cp, '\n');
439
- if (!np)
440
- np = cp + strlen(cp);
438
+ np = strchrnul(cp, '\n');
439
if (pass) {
440
lrr_list[i].line = cp;
441
lrr_list[i].name = cp + 41;
@@ -461,9 +459,7 @@ static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_resu
459
rref++;
460
if (!*rref)
461
break;
464
- next = strchr(rref, '\n');
465
- if (!next)
466
- next = rref + strlen(rref);
462
+ next = strchrnul(rref, '\n');
463
rreflen = next - rref;
464
465
for (i = 0; i < lrr_count; i++) {
compat/strchrnul.c
new
+8
@@ -0,0 +1,8 @@
1
+#include "../git-compat-util.h"
2
+
3
+char *gitstrchrnul(const char *s, int c)
4
+{
5
+ while (*s && *s != c)
6
+ s++;
7
+ return (char *)s;
8
+}
git-compat-util.h
+5
@@ -183,6 +183,11 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
183
const void *needle, size_t needlelen);
184
#endif
185
186
+#ifdef NO_STRCHRNUL
187
+#define strchrnul gitstrchrnul
188
+char *gitstrchrnul(const char *s, int c);
189
+#endif
190
+
191
extern void release_pack_memory(size_t, int);
192
193
static inline char* xstrdup(const char *str)