delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/27/20:56:34

Message-Id: <3.0.32.19970624032057.00689bbc@mail.geocities.com>
Date: Tue, 24 Jun 1997 04:01:45 -0300
To: djgpp AT delorie DOT com
From: Guilherme Silveira <thedarkage AT mail DOT geocities DOT com>
Subject: Re: How to read/modify environment variables?
Mime-Version: 1.0

The fast way to read and modify is with putenv and getenv
It have their version on Clipper (at least version 5.2) and Visual Basic.
There's _environ variable too.

Here's a "little" help to these 3 functions.
And at the end is a example using these 3.

_ENVIRON
Syntax
extern char ** _environ;
Header File
dos.h
Description
_environ is an array of pointers to strings; it is used to access and alter
the operating system environment variables. Each string is of the form:
envvar = varvalue
where envvar is the name of an environment variable (such as PATH), and
varvalue is the string value to which envvar is set (such as
C:\BIN;C:\DOS). The string varvalue can be empty.
When a program begins execution, the operating system environment settings
are passed directly to the program. Note that env, the third argument to
main, is equal to the initial setting of _environ.
The _environ array can be accessed by getenv; however, the putenv function
is the only routine that should be used to add, change or delete the
environ array entries. This is because modification can resize and relocate
the process environment array, but environ is automatically adjusted so
that it always points to the array.

PUTENV
Syntax
#include <stdlib.h>
int putenv(const char *name);
Description
Adds string to current _environment.
putenv accepts the string name and adds it to the _environment of the
current process. For example,
   putenv("PATH=C:\\BC");
putenv can also be used to modify an existing name. On DOS and OS/2, name
must be uppercase. On other systems, name can be either uppercase or
lowercase. name must not include the equal sign (=). You can set a variable
to an empty value by specifying an empty string on the right side of the
'=' sign.
putenv can be used only to modify the current program's _environment. Once
the program ends, the old _environment is restored. The _environment of the
current process is passed to child processes, including any changes made by
putenv.
Note that the string given to putenv must be static or global.
Unpredictable results will occur if a local or dynamic string given to
putenv is used after the string memory is released.
Return Value
On success, putenv returns 0; on failure, -1.

GETENV
Syntax
#include <stdlib.h>
char *getenv(const char *name);
Description
Gets a string from _environment.
getenv returns the value of a specified variable. On DOS and OS/2, name
must be uppercase. On other systems, name can be either uppercase or
lowercase. name must not include the equal sign (=). If the specified
_environment variable does not exist, getenv returns a NULL pointer.
To delete the variable from the _environment, use getenv("name=").
Note:	_environment entries must not be changed directly. If you want to
change an _environment value, you must use putenv.
Return Value
On success, getenv returns the value associated with name. 
If the specified name is not defined in the _environment, getenv returns a
NULL pointer.

EXAMPLE
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
#include <dos.h>
int main(void)
{
   char *path, *ptr;
   int i = 0;
   /* get the current path environment */
   ptr = getenv("PATH");
   /* set up new path */
   path = (char *) malloc(strlen(ptr)+15);
   strcpy(path,"PATH=");
   strcat(path,ptr);
   strcat(path,";c:\\temp");
   /* replace the current path and display current environment */
   putenv(path);
   while (_environ[i])
       printf("%s\n",_environ[i++]);
   return 0;
}
 


Guilherme

- Raw text -


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