Merge branch 'nd/index-pack-l10n-buf-overflow' into maint-1.8.1
[git/jnareb-git.git] / compat / fopen.c
blobb5ca142fedf2ac0e0cedde1011ab385f65010fdf
1 /*
2 * The order of the following two lines is important.
4 * FREAD_READS_DIRECTORIES is undefined before including git-compat-util.h
5 * to avoid the redefinition of fopen within git-compat-util.h. This is
6 * necessary since fopen is a macro on some platforms which may be set
7 * based on compiler options. For example, on AIX fopen is set to fopen64
8 * when _LARGE_FILES is defined. The previous technique of merely undefining
9 * fopen after including git-compat-util.h is inadequate in this case.
11 #undef FREAD_READS_DIRECTORIES
12 #include "../git-compat-util.h"
14 FILE *git_fopen(const char *path, const char *mode)
16 FILE *fp;
17 struct stat st;
19 if (mode[0] == 'w' || mode[0] == 'a')
20 return fopen(path, mode);
22 if (!(fp = fopen(path, mode)))
23 return NULL;
25 if (fstat(fileno(fp), &st)) {
26 fclose(fp);
27 return NULL;
30 if (S_ISDIR(st.st_mode)) {
31 fclose(fp);
32 errno = EISDIR;
33 return NULL;
36 return fp;