feat: add cwd tracking to kernel, remove shell hack, update system utilities to use getcwd, fix tcc libc stub, fix nonexistent directory issue

This commit is contained in:
2026-03-25 15:45:22 +01:00
parent 38ac914de6
commit 1c0db774f6
22 changed files with 389 additions and 265 deletions
+13 -65
View File
@@ -36,81 +36,29 @@ static void build_cwd_path(const char* name, char* out, int outMax) {
scat(out, name, outMax);
}
// ---- Resolve arguments: expand relative file paths against CWD ----
static void resolve_args(const char* args, char* out, int outMax) {
if (!args || !args[0]) { out[0] = '\0'; return; }
int o = 0;
const char* p = args;
while (*p && o < outMax - 1) {
while (*p == ' ' && o < outMax - 1) { out[o++] = *p++; }
if (!*p) break;
const char* tokStart = p;
int tokLen = 0;
while (p[tokLen] && p[tokLen] != ' ') tokLen++;
bool resolve = (cwd[0] || current_drive != 0) && !has_drive_prefix(tokStart) && tokStart[0] != '-';
if (resolve) {
char candidate[256];
int r = 0;
if (current_drive >= 10) candidate[r++] = '0' + current_drive / 10;
candidate[r++] = '0' + current_drive % 10;
candidate[r++] = ':'; candidate[r++] = '/';
int j = 0;
while (cwd[j] && r < 255) candidate[r++] = cwd[j++];
if (cwd[0] && r < 255) candidate[r++] = '/';
// Strip leading "./" from token
int skip = 0;
if (tokLen >= 2 && tokStart[0] == '.' && tokStart[1] == '/') skip = 2;
for (int k = skip; k < tokLen && r < 255; k++) candidate[r++] = tokStart[k];
candidate[r] = '\0';
int h = montauk::open(candidate);
if (h >= 0) {
montauk::close(h);
for (int k = 0; k < r && o < outMax - 1; k++) out[o++] = candidate[k];
} else {
bool looksLikeFile = false;
for (int k = 0; k < tokLen; k++) {
if (tokStart[k] == '.') { looksLikeFile = true; break; }
}
if (looksLikeFile) {
for (int k = 0; k < r && o < outMax - 1; k++) out[o++] = candidate[k];
} else {
for (int k = 0; k < tokLen && o < outMax - 1; k++) out[o++] = tokStart[k];
}
}
} else {
for (int k = 0; k < tokLen && o < outMax - 1; k++) out[o++] = tokStart[k];
}
p = tokStart + tokLen;
}
out[o] = '\0';
}
// ---- Search and execute an external command ----
int exec_external(const char* cmd, const char* args) {
char path[256];
char resolvedArgs[512];
resolve_args(args, resolvedArgs, sizeof(resolvedArgs));
const char* finalArgs = resolvedArgs[0] ? resolvedArgs : nullptr;
const char* finalArgs = (args && args[0]) ? args : nullptr;
// Strip leading "./" from command name
const char* name = cmd;
if (name[0] == '.' && name[1] == '/') name += 2;
// 0. If cmd has a drive prefix (absolute path), try it directly
if (has_drive_prefix(cmd)) {
// 0. Direct paths are resolved by the kernel against the process CWD.
bool hasSlash = false;
for (int i = 0; cmd[i]; i++) {
if (cmd[i] == '/') {
hasSlash = true;
break;
}
}
if (has_drive_prefix(cmd) || cmd[0] == '/' || cmd[0] == '.' || hasSlash) {
if (try_exec(cmd, finalArgs)) return 0;
scopy(path, cmd, sizeof(path));
scat(path, ".elf", sizeof(path));
if (try_exec(path, finalArgs)) return 0;
montauk::print(cmd);
montauk::print(": not found\n");
return 127;