Debian Codegraph @

bypy/macos/placeholder.c

raw
1 /*
2 * placeholder.c
3 * Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
4 *
5 * Distributed under terms of the GPL3 license.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <libproc.h>
13 #include <unistd.h>
14
15
16 int
17 main(int argc, char *const *argv, const char **envp) {
18 int ret;
19 pid_t pid;
20 char pathbuf[PROC_PIDPATHINFO_MAXSIZE], realpath_buf[PROC_PIDPATHINFO_MAXSIZE * 5];
21
22 pid = getpid();
23 ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
24 if (ret <= 0) {
25 perror("failed to get executable path for current pid with error");
26 return 1;
27 }
28 char *path = realpath(pathbuf, realpath_buf);
29 if (path == NULL) {
30 perror("failed to get realpath for executable path with error");
31 return 1;
32 }
33 char *t = rindex(path, '/');
34 if (t == NULL) {
35 fprintf(stderr, "No / in executable path: %s\n", path);
36 return 1;
37 }
38 *(t + 1) = 0;
39 snprintf(t + 1, sizeof(realpath_buf) - strlen(path), "%s/%s", REL_PATH, EXE_NAME);
40 execv(path, argv);
41 }