Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
macOS: Add an entry point to use that works even if run via a symlink
Fixes #1539
- Loading branch information
1 parent
268e5ce
commit bdeec61
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* symlink-deref.c | ||
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net> | ||
* | ||
* Distributed under terms of the GPL3 license. | ||
*/ | ||
|
||
|
||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
#include <mach-o/dyld.h> | ||
#include <sys/syslimits.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <libgen.h> | ||
#include <unistd.h> | ||
|
||
static inline bool | ||
safe_realpath(const char* src, char *buf, size_t buf_sz) { | ||
char* ans = realpath(src, NULL); | ||
if (ans == NULL) return false; | ||
snprintf(buf, buf_sz, "%s", ans); | ||
free(ans); | ||
return true; | ||
} | ||
|
||
static inline bool | ||
read_exe_path(char *exe, size_t buf_sz) { | ||
(void)buf_sz; | ||
uint32_t size = PATH_MAX; | ||
char apple[PATH_MAX+1] = {0}; | ||
if (_NSGetExecutablePath(apple, &size) != 0) { fprintf(stderr, "Failed to get path to executable\n"); return false; } | ||
if (!safe_realpath(apple, exe, buf_sz)) { fprintf(stderr, "realpath() failed on the executable's path\n"); return false; } | ||
return true; | ||
} | ||
|
||
|
||
int | ||
main(int argc, char *argv[]) { | ||
char exe[PATH_MAX+1] = {0}; | ||
char real_exe[PATH_MAX+1] = {0}; | ||
if (!read_exe_path(exe, sizeof(exe))) return 1; | ||
snprintf(real_exe, sizeof(real_exe), "%s/kitty", dirname(exe)); | ||
return execv(real_exe, argv); | ||
} |