git-compat-util: work around fileno(fp) that is a macro

On various BSD's, fileno(fp) is implemented as a macro that directly accesses the fields in the FILE * object, which breaks a function that accepts a "void *fp" parameter and calls fileno(fp) and expect it to work. Work it around by adding a compile-time knob FILENO_IS_A_MACRO that inserts a real helper function in the middle of the callchain. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Duy Nguyen committed Feb 12, 2019 at 21:14 UTC 18a4f6be6b4cfc34de6f80c36ab3ef951a0f7164
4 files changed +24
Makefile
+7
@@ -427,6 +427,8 @@ all::
427 #
428 # Define HAVE_GETDELIM if your system has the getdelim() function.
429 #
430 +# Define FILENO_IS_A_MACRO if fileno() is a macro, not a real function.
431 +#
432 # Define PAGER_ENV to a SP separated VAR=VAL pairs to define
433 # default environment variables to be passed when a pager is spawned, e.g.
434 #
@@ -1773,6 +1775,11 @@ ifdef HAVE_WPGMPTR
1775 BASIC_CFLAGS += -DHAVE_WPGMPTR
1776 endif
1777
1778 +ifdef FILENO_IS_A_MACRO
1779 + COMPAT_CFLAGS += -DFILENO_IS_A_MACRO
1780 + COMPAT_OBJS += compat/fileno.o
1781 +endif
1782 +
1783 ifeq ($(TCLTK_PATH),)
1784 NO_TCLTK = NoThanks
1785 endif
compat/fileno.c new
+7
@@ -0,0 +1,7 @@
1 +#define COMPAT_CODE
2 +#include "../git-compat-util.h"
3 +
4 +int git_fileno(FILE *stream)
5 +{
6 + return fileno(stream);
7 +}
config.mak.uname
+2
@@ -221,6 +221,7 @@ ifeq ($(uname_S),FreeBSD)
221 HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
222 PAGER_ENV = LESS=FRX LV=-c MORE=FRX
223 FREAD_READS_DIRECTORIES = UnfortunatelyYes
224 + FILENO_IS_A_MACRO = UnfortunatelyYes
225 endif
226 ifeq ($(uname_S),OpenBSD)
227 NO_STRCASESTR = YesPlease
@@ -233,6 +234,7 @@ ifeq ($(uname_S),OpenBSD)
234 HAVE_BSD_SYSCTL = YesPlease
235 HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
236 PROCFS_EXECUTABLE_PATH = /proc/curproc/file
237 + FILENO_IS_A_MACRO = UnfortunatelyYes
238 endif
239 ifeq ($(uname_S),MirBSD)
240 NO_STRCASESTR = YesPlease
git-compat-util.h
+8
@@ -1220,6 +1220,14 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
1220 #define getc_unlocked(fh) getc(fh)
1221 #endif
1222
1223 +#ifdef FILENO_IS_A_MACRO
1224 +int git_fileno(FILE *stream);
1225 +# ifndef COMPAT_CODE
1226 +# undef fileno
1227 +# define fileno(p) git_fileno(p)
1228 +# endif
1229 +#endif
1230 +
1231 /*
1232 * Our code often opens a path to an optional file, to work on its
1233 * contents when we can successfully open it. We can ignore a failure