delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/05/14/08:02:11

Date: Mon, 14 May 2001 14:34:33 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Waldemar Schultz <schultz AT ma DOT tum DOT de>
cc: djgpp AT delorie DOT com
Subject: Re: getenv() question
In-Reply-To: <3AFFB47E.C82A6B3D@ma.tum.de>
Message-ID: <Pine.SUN.3.91.1010514142758.26350B@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Mon, 14 May 2001, Waldemar Schultz wrote:

> Eli Zaretskii schrieb:
> > 
> > On Mon, 14 May 2001, Waldemar Schultz wrote:
> > 
> > > Thanks, and is there also a possibility of obtaining the
> > > available free space in environ (allocated size of environ)?
> > 
> > I'm not sure what are you asking, exactly.  There's space that is
> > allocate for `environ', which is an array of pointers, and there's
> > additional space allocated for each "VAR=VALUE" string.  Which one of
> > these are you interested in?
> 
> In simple words: can I write a (DOS) C-program that tells me
> how much environment space is free/occupied ?

Sorry, I still don't follow ;-)

First, the size of environ[] array is dynamically changed as needed, 
since a program can use putenv or setenv to insert additional variables.  
Second, each of the elements of the environ[] array is a pointer to 
malloc'ed string "VAR=VALUE", and each string has a different size, which 
is also dynamically allocated and reallocated if the program modifies the 
environment.  (For example, if you change the value of PATH, its string 
might grow or shrink as appropriate.)

See the source of the putenv library function for the gory details.

Given this, what meaning can you assign to ``how much of the environment 
space is free/occupied''?  About the only meaningful answer would be to 
count the number of free (i.e. NULL) elements in the environ[] array, 
with a loop similar to what I posted.  But this result can change as soon 
as you call putenv, and it hardly says anything about the total storage 
used by environment variables themselves.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019