www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/05/14/02:59:56

Date: Mon, 14 May 2001 09:59:25 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Rafal Maj <maj_rafal AT poczta DOT onet DOT pl>
cc: djgpp AT delorie DOT com
Subject: Re: getenv() question
In-Reply-To: <9dnc8b$agu$1@info.cyf-kr.edu.pl>
Message-ID: <Pine.SUN.3.91.1010514095853.24166I@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, Rafal Maj wrote:

> how to check ammount of variables in environment, and get all of them ? Like
> getenv() but when I don't now environment variable name.

Use the `environ' array.  It's NULL-terminated, like `argv', so you
can walk it with a simple loop.

As an example, here's the source of DJGPP's implementation of
`getenv':

/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdlib.h>
#include <string.h>

extern char **environ;

char *
getenv(const char *name)
{
  int i;

  if (environ == 0)
    return 0;

  for (i=0; environ[i]; i++)
  {
    char *ep = environ[i];
    const char *np = name;
    while (*ep && *np && *ep == *np && *np != '=')
      ep++, np++;
    if (*ep == '=' && *np == 0)
      return ep+1;
  }
  return 0;
}

- Raw text -


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