Skip to content

Commit

Permalink
Support portable boot files on more platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
ur4t committed Oct 19, 2023
1 parent a55ce4f commit ea56574
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
58 changes: 55 additions & 3 deletions c/scheme.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include <fcntl.h>
#include <stddef.h>

#if defined(__APPLE__) && defined(__MACH__)
#include <mach-o/dyld.h>
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif /* O_BINARY */
Expand Down Expand Up @@ -542,10 +546,10 @@ static IBOOL next_path(char *path, const char *name, const char *ext,
case '%':
s += 1;
switch (*s) {
#ifdef WIN32
case 'x': {
wchar_t exepath[BOOT_PATH_MAX]; DWORD n;
s += 1;
#ifdef WIN32
wchar_t exepath[BOOT_PATH_MAX]; DWORD n;
n = GetModuleFileNameW(NULL, exepath, BOOT_PATH_MAX);
if (n == 0 || (n == BOOT_PATH_MAX && GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
fprintf(stderr, "warning: executable path is too long; ignoring %%x\n");
Expand All @@ -559,9 +563,57 @@ static IBOOL next_path(char *path, const char *name, const char *ext,
while (t != tend) setp(*t++);
free(tstart);
}
#else
#if defined(__APPLE__) && defined(__MACH__)
char exepath[BOOT_PATH_MAX]; uint32_t n;
if (_NSGetExecutablePath(exepath, &n) != 0) {
fprintf(stderr, "warning: executable path is too long; ignoring %%x\n");
break;
}
#elif defined(__sun__) && defined(__svr4__)
char exepath[BOOT_PATH_MAX]; ssize_t n;
int fd = OPEN("/proc/self/execname", O_RDONLY);
if (fd == -1) {
fprintf(stderr, "warning: failed to open execname (%s); ignoring %%x\n", strerror(errno));
break;
}
n = READ(fd, exepath, BOOT_PATH_MAX);
CLOSE(fd);
if (n < 0) {
fprintf(stderr, "warning: failed to read execname (%s); ignoring %%x\n", strerror(errno));
break;
}
if (n == BOOT_PATH_MAX) {
fprintf(stderr, "warning: executable path is too long; ignoring %%x\n");
break;
}
exepath[n] = '\0';
#elif defined(__linux__) || defined(__gnu_hurd__)
char *exepath = "/proc/self/exe";
#elif defined(__FreeBSD_kernel__) || defined(__NetBSD__)
char *exepath = "/proc/curproc/file";
#else
char *exepath;
fprintf(stderr, "warning: %%x is not supported; ignoring %%x\n");
break;
}
#endif
char tstart[BOOT_PATH_MAX];
if (realpath(exepath, tstart) == NULL) {
if (errno == ENAMETOOLONG) {
fprintf(stderr, "warning: executable path is too long; ignoring %%x\n");
} else {
fprintf(stderr, "warning: failed to get executable path (%s); ignoring %%x\n", strerror(errno));
}
} else {
const char *tend;
t = tstart;
tend = path_last(t);
if (tend != t) tend -= 1; /* back up to directory separator */
while (t != tend) setp(*t++);
}
#endif
break;
}
case 'm':
s += 1;
t = MACHINE_TYPE;
Expand Down
6 changes: 3 additions & 3 deletions csug/use.stex
Original file line number Diff line number Diff line change
Expand Up @@ -1844,16 +1844,16 @@ the order in which they should be searched.
Within each directory, the two-character escape sequence ``\scheme{%v}''
is replaced by the current version, and the two-character escape sequence
``\scheme{%m}'' is replaced by the machine type.
On supported platforms, the two-character escape sequence ``\scheme{%x}''
is replaced by the directory in which the executable file resides.
A percent followed by any other character is replaced by the second
character; in particular, ``\scheme{%%}'' is replaced by ``\scheme{%}'', and
``\scheme{%:}'' is replaced by ``\scheme{:}''.
If \scheme{SCHEMEHEAPDIRS} ends in a non-escaped colon, the default directories are
searched after those in \scheme{SCHEMEHEAPDIRS}; otherwise, only those listed in
\scheme{SCHEMEHEAPDIRS} are searched.

Under Windows, semi-colons are used in place of colons, and one additional
escape is recognized: ``\scheme{%x},'' which is replaced by the directory in
which the executable file resides.
Under Windows, semi-colons are used in place of colons.
The default search path under Windows consists of ``\scheme{%x}''
and ``\scheme{%x\..\..\boot\%m}.''
The registry key \scheme{HeapSearchPath} in
Expand Down

0 comments on commit ea56574

Please sign in to comment.