Skip to content

Commit e1c0688

Browse files
davvidgitster
authored andcommitted
compat: add a basename() compatibility function
Some systems such as Windows lack libgen.h so provide a basename() implementation for cross-platform use. This introduces the NO_LIBGEN_H construct to the Makefile and autoconf scripts. Signed-off-by: David Aguilar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0620b39 commit e1c0688

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ all::
5454
#
5555
# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
5656
#
57+
# Define NO_LIBGEN_H if you don't have libgen.h.
58+
#
5759
# Define NO_SYS_SELECT_H if you don't have sys/select.h.
5860
#
5961
# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -834,6 +836,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
834836
NO_PREAD = YesPlease
835837
NO_OPENSSL = YesPlease
836838
NO_CURL = YesPlease
839+
NO_LIBGEN_H = YesPlease
837840
NO_SYMLINK_HEAD = YesPlease
838841
NO_IPV6 = YesPlease
839842
NO_SETENV = YesPlease
@@ -899,6 +902,11 @@ ifndef CC_LD_DYNPATH
899902
endif
900903
endif
901904

905+
ifdef NO_LIBGEN_H
906+
COMPAT_CFLAGS += -DNO_LIBGEN_H
907+
COMPAT_OBJS += compat/basename.o
908+
endif
909+
902910
ifdef NO_CURL
903911
BASIC_CFLAGS += -DNO_CURL
904912
else

compat/basename.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "../git-compat-util.h"
2+
3+
/* Adapted from libiberty's basename.c. */
4+
char *gitbasename (char *path)
5+
{
6+
const char *base;
7+
/* Skip over the disk name in MSDOS pathnames. */
8+
if (has_dos_drive_prefix(path))
9+
path += 2;
10+
for (base = path; *path; path++) {
11+
if (is_dir_sep(*path))
12+
base = path + 1;
13+
}
14+
return (char *)base;
15+
}

config.mak.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ NEEDS_SSL_WITH_CRYPTO=@NEEDS_SSL_WITH_CRYPTO@
3030
NO_OPENSSL=@NO_OPENSSL@
3131
NO_CURL=@NO_CURL@
3232
NO_EXPAT=@NO_EXPAT@
33+
NO_LIBGEN_H=@NO_LIBGEN_H@
3334
NEEDS_LIBICONV=@NEEDS_LIBICONV@
3435
NEEDS_SOCKET=@NEEDS_SOCKET@
3536
NO_SYS_SELECT_H=@NO_SYS_SELECT_H@

configure.ac

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ AC_SUBST(SNPRINTF_RETURNS_BOGUS)
627627
## (in default C library and libraries checked by AC_CHECK_LIB)
628628
AC_MSG_NOTICE([CHECKS for library functions])
629629
#
630+
# Define NO_LIBGEN_H if you don't have libgen.h.
631+
AC_CHECK_HEADER([libgen.h],
632+
[NO_LIBGEN_H=],
633+
[NO_LIBGEN_H=YesPlease])
634+
AC_SUBST(NO_LIBGEN_H)
635+
#
630636
# Define NO_STRCASESTR if you don't have strcasestr.
631637
GIT_CHECK_FUNC(strcasestr,
632638
[NO_STRCASESTR=],

git-compat-util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@
9797
#include "compat/mingw.h"
9898
#endif /* __MINGW32__ */
9999

100+
#ifndef NO_LIBGEN_H
101+
#include <libgen.h>
102+
#else
103+
#define basename gitbasename
104+
extern char *gitbasename(char *);
105+
#endif
106+
100107
#ifndef NO_ICONV
101108
#include <iconv.h>
102109
#endif

0 commit comments

Comments
 (0)