blob: 107b3e8182fd4ab13006404cba05f7b52f6c9c54 [file] [log] [blame]
Brandon Caseyeb120e62008-05-07 12:34:18 -05001/*
2 * The order of the following two lines is important.
3 *
Ramsay Jonesb0a642a2017-05-08 21:45:56 +01004 * SUPPRESS_FOPEN_REDEFINITION is defined before including git-compat-util.h
Brandon Caseyeb120e62008-05-07 12:34:18 -05005 * 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.
10 */
Ramsay Jonesb0a642a2017-05-08 21:45:56 +010011#define SUPPRESS_FOPEN_REDEFINITION
Brandon Caseycba22522008-02-08 20:32:47 -060012#include "../git-compat-util.h"
Brandon Caseyeb120e62008-05-07 12:34:18 -050013
Brandon Caseycba22522008-02-08 20:32:47 -060014FILE *git_fopen(const char *path, const char *mode)
15{
16 FILE *fp;
17 struct stat st;
18
19 if (mode[0] == 'w' || mode[0] == 'a')
20 return fopen(path, mode);
21
22 if (!(fp = fopen(path, mode)))
23 return NULL;
24
25 if (fstat(fileno(fp), &st)) {
26 fclose(fp);
27 return NULL;
28 }
29
30 if (S_ISDIR(st.st_mode)) {
31 fclose(fp);
32 errno = EISDIR;
33 return NULL;
34 }
35
36 return fp;
37}