public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: cygwin@cygwin.com, sam.habiel@gmail.com
Subject: Re: Help with C clearenv and setenv
Date: Thu, 31 May 2018 17:57:00 -0000	[thread overview]
Message-ID: <7df53a72-cf45-a5df-45dd-6ea2b7d8e0ed@redhat.com> (raw)
In-Reply-To: <CABHT962nB5jK2sNP9jGaVEbWGrvD72hBVN_q6xX9TKuq3yQ0Gg@mail.gmail.com>

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

  parent reply	other threads:[~2018-05-31 13:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-30 16:55 Sam Habiel
2018-05-31  3:56 ` Keith Christian
2018-05-31  8:58   ` Sam Habiel
2018-05-31 21:53     ` Keith Christian
2018-05-31  9:38 ` Marco Atzeri
2018-05-31 13:22   ` Eliot Moss
2018-05-31 17:57 ` Eric Blake [this message]
2018-06-04 19:39   ` Ken Brown
2018-06-04 20:22     ` Sam Habiel
2018-06-06 13:02       ` Sam Habiel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7df53a72-cf45-a5df-45dd-6ea2b7d8e0ed@redhat.com \
    --to=eblake@redhat.com \
    --cc=cygwin@cygwin.com \
    --cc=sam.habiel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).