diff --git a/setup.py b/setup.py index 567773fcd..c44067ba9 100755 --- a/setup.py +++ b/setup.py @@ -744,6 +744,10 @@ Categories=System;TerminalEmulator; os.rename('../lib', 'Frameworks') if not os.path.exists(logo_dir): raise SystemExit('The kitty logo has not been generated, you need to run logo/make.py') + cmd = [env.cc] + ['-Wall', '-Werror'] + [ + 'symlink-deref.c', '-o', os.path.join('MacOS', 'kitty-deref-symlink')] + run_tool(cmd) + subprocess.check_call([ 'iconutil', '-c', 'icns', logo_dir, '-o', os.path.join('Resources', os.path.basename(logo_dir).partition('.')[0] + '.icns') diff --git a/symlink-deref.c b/symlink-deref.c new file mode 100644 index 000000000..1bdf75e9f --- /dev/null +++ b/symlink-deref.c @@ -0,0 +1,47 @@ +/* + * symlink-deref.c + * Copyright (C) 2019 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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); +}