Skip to content

Commit

Permalink
back out dash_l insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdomino committed Dec 3, 2023
1 parent eddc8c7 commit 0bb5bcd
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions ape/ape-m1.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ int main(int argc, char **argv, char **envp) {
long *sp, *sp2, *auxv;
union ElfEhdrBuf *ebuf;
int c, islogin, n, fd, rc;
char *p, *pe, *dash_l, *exe, *prog, *shell, *execfn;
char *p, *pe, *exe, *prog, *shell, *execfn;

/* allocate loader memory in program's arg block */
n = sizeof(struct ApeLoader);
Expand Down Expand Up @@ -971,29 +971,17 @@ int main(int argc, char **argv, char **envp) {
system v abi aligns this on a 16-byte boundary
grows down the alloc by poking the guard pages */

if (islogin) {
dash_l = __builtin_alloca(3);
}
n = (auxv - sp + islogin + AUXV_WORDS + 1) * sizeof(long);
n = (auxv - sp + AUXV_WORDS + 1) * sizeof(long);
sp2 = (long *)__builtin_alloca(n);
if ((long)sp2 & 15) ++sp2;
for (; n > 0; n -= pagesz) {
((char *)sp2)[n - 1] = 0;
}
if (islogin) {
memmove(sp2, sp, 2 * sizeof(long));
memmove(dash_l, "-l", 3);
*((char **)sp2 + 2) = dash_l;
memmove(sp2 + 3, sp + 2, (auxv - sp - 2) * sizeof(long));
++argc;
sp2[0] = argc;
} else {
memmove(sp2, sp, (auxv - sp) * sizeof(long));
}
memmove(sp2, sp, (auxv - sp) * sizeof(long));

argv = (char **)(sp2 + 1);
envp = (char **)(sp2 + 1 + argc + 1);
auxv = sp2 + (auxv - sp) + 1;
auxv = sp2 + (auxv - sp);
sp = sp2;

/* interpret command line arguments */
Expand All @@ -1008,13 +996,9 @@ int main(int argc, char **argv, char **envp) {
but it will if you say:
ln -sf /usr/local/bin/ape /opt/cosmos/bin/bash.ape
and then use #!/opt/cosmos/bin/bash.ape instead. */
prog = (char *)sp[1];
prog = islogin ? shell : (char *)sp[1];
argc = sp[0];
argv = (char **)(sp + 1);
if (islogin) {
++argv[0];
prog = shell;
}
} else if ((M->ps.literally = argc >= 3 && !StrCmp(argv[1], "-"))) {
/* if the first argument is a hyphen then we give the user the
power to change argv[0] or omit it entirely. most operating
Expand Down Expand Up @@ -1056,9 +1040,13 @@ int main(int argc, char **argv, char **envp) {
}
pe = ebuf->buf + rc;

/* resolve argv[0] to reflect path search */
if (argc > 0 && ((*prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) ||
M->ps.indirect || !StrCmp(BaseName(prog), argv[0]))) {
if (islogin) {
/* TODO: this does not work: setenv("_", exe, 1); */
argv[0] = exe;
} else if (argc > 0 &&
((*prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) ||
!StrCmp(BaseName(prog), argv[0]))) {
/* resolve argv[0] to reflect path search */
argv[0] = exe;
}

Expand Down

0 comments on commit 0bb5bcd

Please sign in to comment.