Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide clearenv? #481

Closed
shahn opened this issue Dec 2, 2016 · 6 comments · Fixed by #1185
Closed

Provide clearenv? #481

shahn opened this issue Dec 2, 2016 · 6 comments · Fixed by #1185

Comments

@shahn
Copy link

shahn commented Dec 2, 2016

Can clearenv() be supported, with a workaround of environ = NULL where it's not supported?

@fiveop
Copy link
Contributor

fiveop commented Dec 2, 2016

Certainly (at least for the first part). You would need to add bindings for clearenv to the project libc and then add a wrapper to nix.

As for the latter part, you would need to figure out (for each platform) how to implement clearenv given the capabilities provided by libc.

@Susurrus
Copy link
Contributor

Susurrus commented Jun 5, 2017

clearenv is already in libc, so this just needs a safe wrapper.

@wginolas
Copy link
Contributor

wginolas commented Nov 5, 2017

I am thinking about implementing this feature, but I am not sure where to add the cleanenv () implementation. Are there any other functions concerning environment variables?

@Susurrus
Copy link
Contributor

Susurrus commented Nov 5, 2017

Cool, it'd be great to implement this! If you look at the man page, you'll see that clearenv(3) is a Linux-only API, but it has a few sibling functions that should also be implemented and are POSIX: getenv(3), putenv(3), setenv(3), and unsetenv(3).

As to where to put these functions, we normally put functions into files following their location within libc (you can use the manpages to figure out which files functions are in). But for stdlib.h we've been splitting up the stdlib.h header into separate files, and I think it makes sense to continue this. I'd suggest you put all these functions into a src/env.rs file.

And if you get stuck while writing code, I'd suggest submitting a draft PR (put [WIP] in the title if you don't want us to comment yet and are just running CI or the like) and we'll review it rather than continuing discussion here.

@jorickert
Copy link
Contributor

Hello im planning to work on this functions. getenv(3), putenv(3), setenv(3), and unsetenv(3) seem to be easy, but i have a questions regarding clearenv(3).
This functions returns a success with 0, error with something else, but it does not set errno. What is the best way to return this in rust? Option<()>, Result<()> or a bool ?

@asomers
Copy link
Member

asomers commented Aug 7, 2018

It can return an error, but doesn't set errno? Sounds a little useless. How would you diagnose the problem? Ahh, I see. It never actually returns anything but 0.
https://github.com/lattera/glibc/blob/a2f34833b1042d5d8eeb263b4cf4caaea138c4ad/stdlib/setenv.c#L354
https://github.com/kraj/uClibc/blob/ca1c74d67dd115d059a875150e10b8560a9c35a8/libc/stdlib/setenv.c#L166

Nonetheless, Rust prefers using Result over bool anytime errors are in play. It's more self-documenting, for one thing. Also, Result is a must_use type, so you'll get a compiler warning if you don't check the return value. I would use Result<(), ()>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants