Skip to content

Commit 35eeef4

Browse files
Johannes Sixtgitster
authored andcommitted
Windows: Make sure argv[0] has a path
Since the exec-path on Windows is derived from the program invocation path, we must ensure that argv[0] always has a path. Unfortunately, if a program is invoked from CMD, argv[0] has no path. But on the other hand, the C runtime offers a global variable, _pgmptr, that always has the full path to the program. We hook into main() with a preprocessor macro, where we replace argv[0]. Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf74a88 commit 35eeef4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

compat/mingw.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,15 @@ void mingw_open_html(const char *path);
223223
char **copy_environ(void);
224224
void free_environ(char **env);
225225
char **env_setenv(char **env, const char *name);
226+
227+
/*
228+
* A replacement of main() that ensures that argv[0] has a path
229+
*/
230+
231+
#define main(c,v) main(int argc, const char **argv) \
232+
{ \
233+
static int mingw_main(); \
234+
argv[0] = xstrdup(_pgmptr); \
235+
return mingw_main(argc, argv); \
236+
} \
237+
static int mingw_main(c,v)

0 commit comments

Comments
 (0)