public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Help with C clearenv and setenv
@ 2018-05-30 16:55 Sam Habiel
  2018-05-31  3:56 ` Keith Christian
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Sam Habiel @ 2018-05-30 16:55 UTC (permalink / raw)
  To: cygwin

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 just sets
environ = NULL. Well--that really breaks setenv! It returns a "Bad
Poniter" error (-1).

What is the correct way to clear environment variables in Cygwin?

--Sam
(About me: http://smh101.com/)

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-30 16:55 Help with C clearenv and setenv Sam Habiel
@ 2018-05-31  3:56 ` Keith Christian
  2018-05-31  8:58   ` Sam Habiel
  2018-05-31  9:38 ` Marco Atzeri
  2018-05-31 17:57 ` Eric Blake
  2 siblings, 1 reply; 10+ messages in thread
From: Keith Christian @ 2018-05-31  3:56 UTC (permalink / raw)
  To: cygwin

Sam,

Here is a short demonstration of how to detect unset (possibly null,
too?) variables in BASH.  Not sure if this is exactly what you are
looking for but presented for info.

set -x;A_VAR="${RANDOM}";echo "1. ${A_VAR}";echo "2.
${A_VAR:?IS_NOT_SET}";unset A_VAR;set +x
+ set -x
+ A_VAR=28641
+ echo '1. 28641'
1. 28641
+ echo '2. 28641'
2. 28641
+ unset A_VAR
-bash: A_VAR: IS_NOT_SET


Documentation for is in the BASH texinfo docs, read it in a Cygwin
terminal by typing "info bash" and go to this section:


3.5.3 Shell Parameter Expansion
-------------------------------
(( lines deleted ))  (( lines deleted ))  (( lines deleted ))
'${PARAMETER:-WORD}'
     If PARAMETER is unset or null, the expansion of WORD is
     substituted.  Otherwise, the value of PARAMETER is substituted.

'${PARAMETER:=WORD}'
     If PARAMETER is unset or null, the expansion of WORD is assigned to
     PARAMETER.  The value of PARAMETER is then substituted.  Positional
     parameters and special parameters may not be assigned to in this
     way.

'${PARAMETER:?WORD}'
     If PARAMETER is null or unset, the expansion of WORD (or a message
     to that effect if WORD is not present) is written to the standard
     error and the shell, if it is not interactive, exits.  Otherwise,
     the value of PARAMETER is substituted.

'${PARAMETER:+WORD}'
     If PARAMETER is null or unset, nothing is substituted, otherwise
     the expansion of WORD is substituted.
(( lines deleted ))  (( lines deleted ))  (( lines deleted ))

Keith

On Wed, May 30, 2018 at 8:48 AM, Sam Habiel <sam.habiel@gmail.com> 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 just sets
> environ = NULL. Well--that really breaks setenv! It returns a "Bad
> Poniter" error (-1).
>
> What is the correct way to clear environment variables in Cygwin?
>
> --Sam
> (About me: http://smh101.com/)
>
> --
> 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
>

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-31  3:56 ` Keith Christian
@ 2018-05-31  8:58   ` Sam Habiel
  2018-05-31 21:53     ` Keith Christian
  0 siblings, 1 reply; 10+ messages in thread
From: Sam Habiel @ 2018-05-31  8:58 UTC (permalink / raw)
  To: cygwin

Thank you Keith for you reply; but I am actually looking for the C APIs to
manipulate the environment; esp. to clear it.

On Wed, May 30, 2018 at 6:05 PM, Keith Christian <keith1christian@gmail.com>
wrote:

> Sam,
>
> Here is a short demonstration of how to detect unset (possibly null,
> too?) variables in BASH.  Not sure if this is exactly what you are
> looking for but presented for info.
>
> set -x;A_VAR="${RANDOM}";echo "1. ${A_VAR}";echo "2.
> ${A_VAR:?IS_NOT_SET}";unset A_VAR;set +x
> + set -x
> + A_VAR=28641
> + echo '1. 28641'
> 1. 28641
> + echo '2. 28641'
> 2. 28641
> + unset A_VAR
> -bash: A_VAR: IS_NOT_SET
>
>
> Documentation for is in the BASH texinfo docs, read it in a Cygwin
> terminal by typing "info bash" and go to this section:
>
>
> 3.5.3 Shell Parameter Expansion
> -------------------------------
> (( lines deleted ))  (( lines deleted ))  (( lines deleted ))
> '${PARAMETER:-WORD}'
>      If PARAMETER is unset or null, the expansion of WORD is
>      substituted.  Otherwise, the value of PARAMETER is substituted.
>
> '${PARAMETER:=WORD}'
>      If PARAMETER is unset or null, the expansion of WORD is assigned to
>      PARAMETER.  The value of PARAMETER is then substituted.  Positional
>      parameters and special parameters may not be assigned to in this
>      way.
>
> '${PARAMETER:?WORD}'
>      If PARAMETER is null or unset, the expansion of WORD (or a message
>      to that effect if WORD is not present) is written to the standard
>      error and the shell, if it is not interactive, exits.  Otherwise,
>      the value of PARAMETER is substituted.
>
> '${PARAMETER:+WORD}'
>      If PARAMETER is null or unset, nothing is substituted, otherwise
>      the expansion of WORD is substituted.
> (( lines deleted ))  (( lines deleted ))  (( lines deleted ))
>
> Keith
>
> On Wed, May 30, 2018 at 8:48 AM, Sam Habiel <sam.habiel@gmail.com> 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 just sets
> > environ = NULL. Well--that really breaks setenv! It returns a "Bad
> > Poniter" error (-1).
> >
> > What is the correct way to clear environment variables in Cygwin?
> >
> > --Sam
> > (About me: http://smh101.com/)
> >
> > --
> > 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
> >
>
> --
> 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
>
>

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-30 16:55 Help with C clearenv and setenv Sam Habiel
  2018-05-31  3:56 ` Keith Christian
@ 2018-05-31  9:38 ` Marco Atzeri
  2018-05-31 13:22   ` Eliot Moss
  2018-05-31 17:57 ` Eric Blake
  2 siblings, 1 reply; 10+ messages in thread
From: Marco Atzeri @ 2018-05-31  9:38 UTC (permalink / raw)
  To: cygwin

On 5/30/2018 4:48 PM, 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 just sets
> environ = NULL. Well--that really breaks setenv! It returns a "Bad
> Poniter" error (-1).
> 
> What is the correct way to clear environment variables in Cygwin?
> 
> --Sam


not tested but it seems Cygwin is not the only platform
with problem on environ = NULL

environ = calloc(1, sizeof(*environ))

see
https://github.com/samba-team/samba/blob/master/source3/client/smbspool_krb5_wrapper.c

https://github.com/dovecot/core/blob/master/src/lib/env-util.c


--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-31  9:38 ` Marco Atzeri
@ 2018-05-31 13:22   ` Eliot Moss
  0 siblings, 0 replies; 10+ messages in thread
From: Eliot Moss @ 2018-05-31 13:22 UTC (permalink / raw)
  To: cygwin

On 5/30/2018 11:56 PM, Marco Atzeri wrote:
 > On 5/30/2018 4:48 PM, Sam Habiel wrote:

 > environ = calloc(1, sizeof(*environ))

Indeed, this does not surprise me since environ is supposed
to be a pointer to an array of char *, whose last entry is
NULL.  So the above line is the one needed.  It *might*
also work to do something like:

char *dummy = NULL;
environ = &dummy;

This avoids an allocation call.

-- Eliot Moss

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-30 16:55 Help with C clearenv and setenv Sam Habiel
  2018-05-31  3:56 ` Keith Christian
  2018-05-31  9:38 ` Marco Atzeri
@ 2018-05-31 17:57 ` Eric Blake
  2018-06-04 19:39   ` Ken Brown
  2 siblings, 1 reply; 10+ messages in thread
From: Eric Blake @ 2018-05-31 17:57 UTC (permalink / raw)
  To: cygwin, sam.habiel

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-31  8:58   ` Sam Habiel
@ 2018-05-31 21:53     ` Keith Christian
  0 siblings, 0 replies; 10+ messages in thread
From: Keith Christian @ 2018-05-31 21:53 UTC (permalink / raw)
  To: cygwin

You're welcome, Sam.

Keith

On Wed, May 30, 2018 at 8:22 PM, Sam Habiel <sam.habiel@gmail.com> wrote:
> Thank you Keith for you reply; but I am actually looking for the C APIs to
> manipulate the environment; esp. to clear it.
>
> On Wed, May 30, 2018 at 6:05 PM, Keith Christian <keith1christian@gmail.com>
> wrote:
>
>> Sam,
>>
>> Here is a short demonstration of how to detect unset (possibly null,
>> too?) variables in BASH.  Not sure if this is exactly what you are
>> looking for but presented for info.
>>
>> set -x;A_VAR="${RANDOM}";echo "1. ${A_VAR}";echo "2.
>> ${A_VAR:?IS_NOT_SET}";unset A_VAR;set +x
>> + set -x
>> + A_VAR=28641
>> + echo '1. 28641'
>> 1. 28641
>> + echo '2. 28641'
>> 2. 28641
>> + unset A_VAR
>> -bash: A_VAR: IS_NOT_SET
>>
>>
>> Documentation for is in the BASH texinfo docs, read it in a Cygwin
>> terminal by typing "info bash" and go to this section:
>>
>>
>> 3.5.3 Shell Parameter Expansion
>> -------------------------------
>> (( lines deleted ))  (( lines deleted ))  (( lines deleted ))
>> '${PARAMETER:-WORD}'
>>      If PARAMETER is unset or null, the expansion of WORD is
>>      substituted.  Otherwise, the value of PARAMETER is substituted.
>>
>> '${PARAMETER:=WORD}'
>>      If PARAMETER is unset or null, the expansion of WORD is assigned to
>>      PARAMETER.  The value of PARAMETER is then substituted.  Positional
>>      parameters and special parameters may not be assigned to in this
>>      way.
>>
>> '${PARAMETER:?WORD}'
>>      If PARAMETER is null or unset, the expansion of WORD (or a message
>>      to that effect if WORD is not present) is written to the standard
>>      error and the shell, if it is not interactive, exits.  Otherwise,
>>      the value of PARAMETER is substituted.
>>
>> '${PARAMETER:+WORD}'
>>      If PARAMETER is null or unset, nothing is substituted, otherwise
>>      the expansion of WORD is substituted.
>> (( lines deleted ))  (( lines deleted ))  (( lines deleted ))
>>
>> Keith
>>
>> On Wed, May 30, 2018 at 8:48 AM, Sam Habiel <sam.habiel@gmail.com> 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 just sets
>> > environ = NULL. Well--that really breaks setenv! It returns a "Bad
>> > Poniter" error (-1).
>> >
>> > What is the correct way to clear environment variables in Cygwin?
>> >
>> > --Sam
>> > (About me: http://smh101.com/)
>> >
>> > --
>> > 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
>> >
>>
>> --
>> 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
>>
>>
>
> --
> 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
>

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-05-31 17:57 ` Eric Blake
@ 2018-06-04 19:39   ` Ken Brown
  2018-06-04 20:22     ` Sam Habiel
  0 siblings, 1 reply; 10+ messages in thread
From: Ken Brown @ 2018-06-04 19:39 UTC (permalink / raw)
  To: cygwin; +Cc: Eric Blake, sam.habiel

On 5/31/2018 9:43 AM, Eric Blake wrote:
> 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;

I've attempted to do this:

   https://cygwin.com/ml/cygwin-patches/2018-q2/msg00024.html

Ken

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-06-04 19:39   ` Ken Brown
@ 2018-06-04 20:22     ` Sam Habiel
  2018-06-06 13:02       ` Sam Habiel
  0 siblings, 1 reply; 10+ messages in thread
From: Sam Habiel @ 2018-06-04 20:22 UTC (permalink / raw)
  To: Ken Brown; +Cc: cygwin, Eric Blake

Thank you!

On Mon, Jun 4, 2018 at 3:39 PM, Ken Brown <kbrown@cornell.edu> wrote:
> On 5/31/2018 9:43 AM, Eric Blake wrote:
>>
>> 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;
>
>
> I've attempted to do this:
>
>   https://cygwin.com/ml/cygwin-patches/2018-q2/msg00024.html
>
> Ken

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Help with C clearenv and setenv
  2018-06-04 20:22     ` Sam Habiel
@ 2018-06-06 13:02       ` Sam Habiel
  0 siblings, 0 replies; 10+ messages in thread
From: Sam Habiel @ 2018-06-06 13:02 UTC (permalink / raw)
  To: Ken Brown; +Cc: cygwin, Eric Blake

Eliot Moss,

This worked perfectly:

char *dummy = NULL;
environ = &dummy;

Thank you so much.

--Sam

On Mon, Jun 4, 2018 at 4:22 PM, Sam Habiel <sam.habiel@gmail.com> wrote:
> Thank you!
>
> On Mon, Jun 4, 2018 at 3:39 PM, Ken Brown <kbrown@cornell.edu> wrote:
>> On 5/31/2018 9:43 AM, Eric Blake wrote:
>>>
>>> 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;
>>
>>
>> I've attempted to do this:
>>
>>   https://cygwin.com/ml/cygwin-patches/2018-q2/msg00024.html
>>
>> Ken

--
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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-06-06 13:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-30 16:55 Help with C clearenv and setenv 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
2018-06-04 19:39   ` Ken Brown
2018-06-04 20:22     ` Sam Habiel
2018-06-06 13:02       ` Sam Habiel

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).