Web lists-archives.com

Re: Help with C clearenv and setenv




On 05/30/2018 09:48 AM, Sam Habiel wrote:
I have code for a database I am porting to Cygwin.

Part of that code is a clearenv() then a couple of setenvs. There is
an ifdef for Cygwin, as it doesn't implement clearenv.

It wouldn't be hard to implement clearenv() for a future release of Cygwin, at least for a version that leaves environ pointing to a 1-element array containing NULL. A bit more effort and we could probably also support glibc's notion of environ==NULL being shorthand for an empty environment; in fact, POSIX states:

"After assigning a new value to environ, applications should not rely on the new environment strings remaining part of the environment, as a call to getenv(), [XSI] [Option Start] putenv(), [Option End] setenv(), unsetenv(), or any function that is dependent on an environment variable may, on noticing that environ has changed, copy the environment strings to a new array and assign environ to point to it."

where we could check for environ==NULL on entry to any of those functions as one of the times that we reassign environ back to a non-NULL array with one NULL entry.

Note, however, that POSIX says that it is not portable to exec processes with a completely empty environment. In particular, Cygwin is rather fragile if some things like PATH are not defined in the environment. Having a temporarily empty environ may work if you then add back enough state before Cygwin has to look something up from the environment. The POSIX recommendation is that you should always have at least the equivalent of:

env -i $(getconf V7_ENV) PATH="$(getconf PATH)" command

in your environment, at least when exec'ing processes.

It just sets
environ = NULL. Well--that really breaks setenv! It returns a "Bad
Poniter" error (-1).

What it SHOULD do is set environ to a one-element array containing NULL, at least until someone submits a patch adding the glibc extension of clearenv() to Cygwin.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple