From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9407 invoked by alias); 31 May 2018 13:43:34 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 9397 invoked by uid 89); 31 May 2018 13:43:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Principal, xsi, virtualization, Virtualization X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 31 May 2018 13:43:32 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 54AA840201A0; Thu, 31 May 2018 13:43:31 +0000 (UTC) Received: from [10.10.125.63] (ovpn-125-63.rdu2.redhat.com [10.10.125.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1E741134CA2; Thu, 31 May 2018 13:43:30 +0000 (UTC) Subject: Re: Help with C clearenv and setenv To: cygwin@cygwin.com, sam.habiel@gmail.com References: From: Eric Blake Message-ID: <7df53a72-cf45-a5df-45dd-6ea2b7d8e0ed@redhat.com> Date: Thu, 31 May 2018 17:57:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2018-05/txt/msg00336.txt.bz2 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