forked from oracle/truffleruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_launcher_darwin.c
65 lines (54 loc) · 1.6 KB
/
native_launcher_darwin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0, or
* GNU General Public License version 2, or
* GNU Lesser General Public License version 2.1.
*/
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <mach-o/dyld.h>
int main(int argc, char* argv[], char* envp[]) {
if (argc < 1) {
fprintf(stderr, "argc < 1\n");
return EXIT_FAILURE;
}
char *self = "";
uint32_t bufsize = 0;
/* Get the buffer size */
int ret = _NSGetExecutablePath(self, &bufsize);
if (bufsize == 0) {
fprintf(stderr, "_NSGetExecutablePath failed to give the buffer size\n");
return EXIT_FAILURE;
}
/* Get the path of the current executable */
self = malloc(bufsize);
ret = _NSGetExecutablePath(self, &bufsize);
if (ret != 0) {
fprintf(stderr, "_NSGetExecutablePath returned %d\n", ret);
return EXIT_FAILURE;
}
char exec[PATH_MAX+3];
if (realpath(self, exec) == NULL) {
perror("realpath");
fprintf(stderr, "self=%s\n", self);
return EXIT_FAILURE;
}
size_t len = strlen(exec);
exec[len++] = '.';
exec[len++] = 's';
exec[len++] = 'h';
exec[len] = '\0';
char** args = argv;
args[0] = exec;
execve(exec, args, envp);
/* execve only returns on failure */
perror("execve");
fprintf(stderr, "%s\n", exec);
return EXIT_FAILURE;
}