Skip to content

Commit 82e9360

Browse files
committed
unixlauncher: Handle argv[0] as path or basename separately
1 parent 9535434 commit 82e9360

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

unixlauncher.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <errno.h>
22
#include <libgen.h>
3+
#include <stdbool.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
@@ -81,9 +82,20 @@ int unixlauncher_run(int argc, char *argv[], char *envp[]) {
8182

8283
// Find ourselves
8384
char *original_self = argv[0];
84-
char *self_path = realpath(original_self, NULL);
85+
char *self_path;
86+
87+
// Detect whether argv[0] contains forward slashes
88+
bool self_is_path = false;
89+
for (size_t i = 0; original_self[i]; i++) {
90+
if (original_self[i] == '/') {
91+
self_is_path = true;
92+
break;
93+
}
94+
}
8595

86-
if (self_path == NULL) {
96+
if (self_is_path) { // argv[0] is a path to an executable
97+
self_path = realpath(original_self, NULL);
98+
} else { // argv[0] is basename of executable
8799
// Iterate through PATH to find script
88100
self_path = which(argv[0]);
89101

0 commit comments

Comments
 (0)