Skip to content

Commit

Permalink
compat: add a basename() compatibility function
Browse files Browse the repository at this point in the history
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 <davvid@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
David Aguilar authored and Junio C Hamano committed Jun 1, 2009
1 parent 0620b39 commit e1c0688
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ all::
#
# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
#
# Define NO_LIBGEN_H if you don't have libgen.h.
#
# Define NO_SYS_SELECT_H if you don't have sys/select.h.
#
# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
Expand Down Expand Up @@ -834,6 +836,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
NO_PREAD = YesPlease
NO_OPENSSL = YesPlease
NO_CURL = YesPlease
NO_LIBGEN_H = YesPlease
NO_SYMLINK_HEAD = YesPlease
NO_IPV6 = YesPlease
NO_SETENV = YesPlease
Expand Down Expand Up @@ -899,6 +902,11 @@ ifndef CC_LD_DYNPATH
endif
endif

ifdef NO_LIBGEN_H
COMPAT_CFLAGS += -DNO_LIBGEN_H
COMPAT_OBJS += compat/basename.o
endif

ifdef NO_CURL
BASIC_CFLAGS += -DNO_CURL
else
Expand Down
15 changes: 15 additions & 0 deletions compat/basename.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "../git-compat-util.h"

/* Adapted from libiberty's basename.c. */
char *gitbasename (char *path)
{
const char *base;
/* Skip over the disk name in MSDOS pathnames. */
if (has_dos_drive_prefix(path))
path += 2;
for (base = path; *path; path++) {
if (is_dir_sep(*path))
base = path + 1;
}
return (char *)base;
}
1 change: 1 addition & 0 deletions config.mak.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ NEEDS_SSL_WITH_CRYPTO=@NEEDS_SSL_WITH_CRYPTO@
NO_OPENSSL=@NO_OPENSSL@
NO_CURL=@NO_CURL@
NO_EXPAT=@NO_EXPAT@
NO_LIBGEN_H=@NO_LIBGEN_H@
NEEDS_LIBICONV=@NEEDS_LIBICONV@
NEEDS_SOCKET=@NEEDS_SOCKET@
NO_SYS_SELECT_H=@NO_SYS_SELECT_H@
Expand Down
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,12 @@ AC_SUBST(SNPRINTF_RETURNS_BOGUS)
## (in default C library and libraries checked by AC_CHECK_LIB)
AC_MSG_NOTICE([CHECKS for library functions])
#
# Define NO_LIBGEN_H if you don't have libgen.h.
AC_CHECK_HEADER([libgen.h],
[NO_LIBGEN_H=],
[NO_LIBGEN_H=YesPlease])
AC_SUBST(NO_LIBGEN_H)
#
# Define NO_STRCASESTR if you don't have strcasestr.
GIT_CHECK_FUNC(strcasestr,
[NO_STRCASESTR=],
Expand Down
7 changes: 7 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
#include "compat/mingw.h"
#endif /* __MINGW32__ */

#ifndef NO_LIBGEN_H
#include <libgen.h>
#else
#define basename gitbasename
extern char *gitbasename(char *);
#endif

#ifndef NO_ICONV
#include <iconv.h>
#endif
Expand Down

0 comments on commit e1c0688

Please sign in to comment.