Files
ww/test/wcc/974_getenv_run.c
Hojun-Cho 4ab530c24d rt+lib/os+test: capture envp; add os.getenv
Capture envp from the kernel-supplied stack into a DATAW slot during
_start's prologue (before CALL main), and expose it via a `rt_envp`
TEXT getter. lib/os.getenv binds the getter as `@symbol("rt_envp")
fn rtenvp() **u8` — the getter-fn pattern works around @symbol-on-let
not being supported by the compiler yet (silent miscompile otherwise).

`os.getenv(name: str) (str | void)` matches Hare's os::getenv surface:
walks the NUL-terminated envp table, "name=" prefix-matches with an
explicit `=` boundary check so prefixes don't false-match longer
names, returns the value as a borrowed str view. Empty value (env
"FOO=") returns len=0 str, not void — void is reserved for "name
not present at all".

Cohort coverage in lib/os/ostest.ww + test/wcc/974_getenv_run.c:
set / empty / unset / prefix-no-match (4 @test fns).
2026-05-15 17:13:32 +09:00

66 lines
1.8 KiB
C

/*
* 974_getenv_run — execute the lib/os getenv smoke fixture under the
* C-side `ww run` driver and assert exit 0.
*
* Pre-arranges the environment that lib/os/ostest.ww asserts against:
*
* WW_TEST_GETENV = "hello-world" (set, non-empty)
* WW_TEST_EMPTY = "" (set, empty value)
* WW_TEST_NOT_SET unset (unsetenv-cleared)
*
* The child `ww run` process inherits this env, so ostest.ww's
* `os.getenv` calls see exactly the state we configured here. This
* is the "C-side env arrangement" pattern (parent sets, child reads)
* — proper POSIX shape for stdlib testing without a `setenv` ww
* primitive (deferred per drew/rob).
*
* Same wrapper shape as 970_fmt_run / 971_log_run / 972_fnmatch_run
* / 973_shlex_run.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* Pre-arrange the env state ostest.ww asserts against. */
setenv("WW_TEST_GETENV", "hello-world", 1);
setenv("WW_TEST_EMPTY", "", 1);
unsetenv("WW_TEST_NOT_SET");
const char *src = "lib/os/ostest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "getenv_run FAIL: %s exited %d\n", src, rc);
return 1;
}
printf("getenv_run: %s ok\n", src);
return 0;
}