3 ms·
> As far as I'm concerned, the environment is a read-only input parameter set on process creation like argv. This holds for a lot of programs, but what if you'
by stouset 2y ago
> As far as I'm concerned, the environment is a read-only input parameter set on process creation like argv.
This holds for a lot of programs, but what if you're writing a shell?
- tsimionescu 2y agoEven then, you could maintain a separate copy of the environment that you control and freely mutate. Basically, during startup, you create a copy of the env you received. Any setenv primitive you expose to users will modify this copy (that you can sync properly yourself). When you want to launch a process, you explicitly provide the internal copy of the env to that process, you don't rely on libc providing its own copy. Of course, this means you won't see any changes to env vars from libraries you may use that call setenv(), but you also shouldn't need, or want, that in a shell. I still think having a proper synchronous thread safe setenv()/getenv() in libc is the better choice.
- GoblinSlayer 2y agoIt doesn't look like there's any incentive to change it, e.g. getenv_r is an unpopular function.
- dzaima 2y agoIf you're writing a shell, you can spend the 15 minutes to write a custom mutable data structure for your envvars; no need to significantly worsen the entire ecosystem to reduce the size of shells by a couple dozen lines (or, rather, move those lines into libc..)
- harrall 2y agoI’ve written a lot of subprocess runners and environmental variables passed to a sub-process is just data at that point and you store it in your own variable like you would store someone’s name or someone’s age.