Skip to content

Commit cec4c04

Browse files
authored
Remove .com logic from GetProgramExecutableName (#1134)
The `com` parameter to `TryPath` was always 1, so there was no reason to have it. This patch changes the logic to be as though `com` was 0, which provides a possible answer to the TODO question -- the answer is no. If we never care about appending `.com`, then `CopyWithCwd` doesn't need to return anything beyond a boolean success value.
1 parent f8c0186 commit cec4c04

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

libc/calls/getprogramexecutablename.greg.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int OldApeLoader(char *s) {
9393
(!strncmp((b = basename(s)), ".ape-", 5) && AllNumDot(b + 5));
9494
}
9595

96-
static char *CopyWithCwd(const char *q, char *p, char *e) {
96+
static int CopyWithCwd(const char *q, char *p, char *e) {
9797
char c;
9898
if (*q != '/') {
9999
if (q[0] == '.' && q[1] == '/') {
@@ -109,29 +109,19 @@ static char *CopyWithCwd(const char *q, char *p, char *e) {
109109
if (p + 1 /* nul */ < e) {
110110
*p++ = c;
111111
} else {
112-
return NULL;
112+
return 0;
113113
}
114114
}
115115
*p = 0;
116-
return p;
116+
return 1;
117117
}
118118

119-
// if q exists then turn it into an absolute path. we also try adding
120-
// a .com suffix since the ape auto-appends it when resolving
121-
//
122-
// TODO(jart): is this still relevant?
123-
static int TryPath(const char *q, int com) {
124-
char *p;
125-
if (!(p = CopyWithCwd(q, g_prog.u.buf,
126-
g_prog.u.buf + sizeof(g_prog.u.buf) - com * 4))) {
119+
// if q exists then turn it into an absolute path.
120+
static int TryPath(const char *q) {
121+
if (!CopyWithCwd(q, g_prog.u.buf, g_prog.u.buf + sizeof(g_prog.u.buf))) {
127122
return 0;
128123
}
129-
if (!sys_faccessat(AT_FDCWD, g_prog.u.buf, F_OK, 0)) return 1;
130-
if (!com) return 0;
131-
p = WRITE32LE(p, READ32LE(".com"));
132-
*p = 0;
133-
if (!sys_faccessat(AT_FDCWD, g_prog.u.buf, F_OK, 0)) return 1;
134-
return 0;
124+
return !sys_faccessat(AT_FDCWD, g_prog.u.buf, F_OK, 0);
135125
}
136126

137127
// if the loader passed a relative path, prepend cwd to it.
@@ -221,13 +211,13 @@ static inline void InitProgramExecutableNameImpl(void) {
221211
}
222212
}
223213

224-
// don't trust argument parsing if set-id.
214+
// don't trust argv or envp if set-id.
225215
if (issetugid()) {
226216
goto UseEmpty;
227217
}
228218

229-
// try argv[0], then argv[0].com, then $_, then $_.com.
230-
if (TryPath(__argv[0], 1) || TryPath(__getenv(__envp, "_").s, 1)) {
219+
// try argv[0], then then $_.
220+
if (TryPath(__argv[0]) || TryPath(__getenv(__envp, "_").s)) {
231221
goto UseBuf;
232222
}
233223

0 commit comments

Comments
 (0)