[comp.os.minix] putenv

tholm@uvicctr.UUCP (Terrence W. Holm) (05/05/88)

EFTH Minix report #2  - May 1988 -  putenv(3)


This is an implementation of putenv(3) that we
wrote for Minix. Please consider this a public
domain program.

We have included all of our "man" pages that
mention the environment. So even if you don't
want the putenv(3) routine, you might find the
man pages useful.

[Our man pages are completely written by us.
Though they do have the "look and feel" of
real Unix (tm) man pages.]


echo x - putenv.c
gres '^X' '' > putenv.c << '/'
X/****************************************************************/
X/*								*/
X/*	putenv(3)						*/
X/*								*/
X/*		Change or add an environment entry		*/
X/*								*/
X/****************************************************************/
X/*   origination        1987-Oct-7               T. Holm	*/
X/****************************************************************/
X
X
X#include <stdio.h>
X
X
X#define  PSIZE  sizeof(char *)
X
X
Xextern  char  **environ;
X
X
Xchar  *index();
Xchar  *malloc();
X
X
X/****************************************************************/
X/*								*/
X/*	putenv( entry )						*/
X/*								*/
X/*		The "entry" should follow the form 		*/
X/*		"NAME=VALUE". This routine will search the 	*/
X/*		user environment for "NAME" and replace its 	*/
X/*		value with "VALUE".				*/
X/*								*/
X/*		Note that "entry" is not copied, it is used 	*/
X/*		as the environment entry. This means that it 	*/
X/*		must not be unallocated or otherwise modifed 	*/
X/*		by the caller, unless it is replaced by a 	*/
X/*		subsequent putenv().				*/
X/*								*/
X/*		If the name is not found in the environment, 	*/
X/*		then a new vector of pointers is allocated, 	*/
X/*		"entry" is put at the end and the global 	*/
X/*		variable "environ" is updated.			*/
X/*								*/
X/*		This function normally returns NULL, but -1	*/
X/*		is returned if it can not allocate enough 	*/
X/*		space using malloc(3), or "entry" does not	*/
X/*		contain a '='.					*/
X/*								*/
X/****************************************************************/
X
X
Xputenv( entry )
X  char *entry;
X
X  {
X  unsigned length;
X  unsigned size;
X  char     **p;
X  char     **new_environ;
X
X  /*  Find the length of the "NAME="  */
X
X  if ( (length=(unsigned) index(entry,'=')) == NULL )
X    return( -1 );
X
X  length = length - (unsigned) entry + 1;
X
X
X  /*  Scan through the environment looking for "NAME="  */
X
X  for ( p=environ; *p != 0 ; p++ )
X    if ( strncmp( entry, *p, length ) == 0 )
X      {
X      *p = entry;
X      return( NULL );
X      }
X
X
X  /*  The name was not found, build a bigger environment  */
X
X  size = p - environ;
X
X  new_environ = (char **) malloc( (size+2)*PSIZE );
X
X  if ( new_environ == NULL )
X    return( -1 );
X
X  bcopy( (char *) environ, (char *) new_environ, size*PSIZE );
X
X  new_environ[size]   = entry;
X  new_environ[size+1] = NULL;
X
X  environ = new_environ;
X
X  return(NULL);
X  }
/
echo x - printenv.1
gres '^X' '' > printenv.1 << '/'
XNAME
X    printenv(1)       	- print the environment
X
XSYNOPSIS
X    printenv
X
XDESCRIPTION
X    All of the entries in the user environment are printed on the
X    standard output stream.
X
XSEE ALSO
X    getenv(3), putenv(3), environ(4)
/
echo x - execve.2
gres '^X' '' > execve.2 << '/'
XNAME
X    execve(2)     	- execute a file as a process
X
XSYNOPSIS
X    execve( name, argv, envp )
X      char *name, *argv[], *envp[];
X
XDESCRIPTION
X    "name" is the path name of the file which contains the core
X    image. "argv" points to a vector of pointers to strings which
X    are to be passed as arguments, the first argument is generally
X    the same the last component in "name". "envp" points to the
X    environment, see environ(4).
X
X    The new process begins with the following, where "argc" is the
X    number of arguments in "argv". The global variable "environ" is
X    set to "envp".
X
X        main( argc, argv, envp )
X          int  argc;
X          char **argv, **envp;
X
XSEE ALSO
X    execle(3), environ(4)
/
echo x - execle.3
gres '^X' '' > execle.3 << '/'
XNAME
X    execle(3)    	- execute a file as a process
X
XSYNOPSIS
X    execle( name, arg0, arg1, ..., argn, 0, envp )
X      char *name, *arg0, *arg1, ..., *argn, *envp[];
X
X    execl( name, arg0, arg1, ..., argn, 0 )
X      char *name, *arg0, *arg1, ..., *argn;
X
X    execv( name, argv )
X      char *name, *argv[];
X
X    execn( name )
X      char *name;
X
XDESCRIPTION
X    These calls are simpler calls to execve(2). The first two
X    routines allow the "argv" vector to be given as a list
X    of strings, terminated by a zero. The execl(3) and execv(3)
X    routines are like execle(3) and execve(2) respectively, except 
X    that no user environment is passed.
X
X    For the special case of no argument list and no environment
X    the execn(3) call is useful.
X
XSEE ALSO
X    execve(2), environ(4)
X
XBUGS
X    The calls execl() and execv() don't pass the environment, I think
X    they should.
/
echo x - getenv.3
gres '^X' '' > getenv.3 << '/'
XNAME
X    getenv(3)     	- get environment entry
X
XSYNOPSIS
X    char *getenv( name )
X      char *name;
X
XDESCRIPTION
X    If the entry "name=value" exists in the user environment, then
X    a pointer to "value" is returned. Otherwise NULL is returned.
X
XSEE ALSO
X    execve(2), putenv(3), environ(4)
/
echo x - putenv.3
gres '^X' '' > putenv.3 << '/'
XNAME
X    putenv(3)    	- change or add an environment entry
X
XSYNOPSIS
X    putenv( entry )
X      char *entry;
X
XDESCRIPTION
X    The "entry" should follow the form "NAME=VALUE". This routine will
X    search the user environment for "NAME" and replace its value with
X    "VALUE".
X
X    Note that "entry" is not copied, it is used as the environment
X    entry. This means that it must not be unallocated or otherwise 
X    modifed by the caller, unless it is replaced by a subsequent 
X    putenv().
X
X    If the name is not found in the environment, then a new vector
X    of pointers is allocated, "entry" is put at the end and the
X    global variable "environ" is updated.
X
X    This function normally returns NULL, but -1 is returned if it
X    can not allocate enough space using malloc(3), or "entry" does
X    not contain a '='.
X
XSEE ALSO
X    execve(2), getenv(3), environ(4)
/
echo x - environ.4
gres '^X' '' > environ.4 << '/'
XNAME
X    environ(4)     	- description of the user environment
X
XSYNOPSIS
X    extern  char  **environ;
X
XDESCRIPTION
X    Using execve() a list of strings may be passed to a new process.
X    The new process may use either the global variable "environ", or
X    the third parameter in "main(argc,argv,envp)" to point to a list
X    of (char *) pointers. These pointers each refer to a string of
X    characters, terminated by a '\0'. The last pointer is NULL. By
X    convention the strings are of the form: "NAME=VALUE".
X
X    Entries can be looked up using getenv(3). Entries can be changed
X    or added using putenv(3).
X
X    The environment may be changed from within the shell. (Note that
X    the "export" command flags names which should be put in the user
X    environment.) The environment may be looked at using printenv(1),
X    or a $NAME escape to the shell. The more notable names are,
X
X    PATH    Where to search for commands. Set by a login shell to
X            ":/bin:/usr/bin". May be reset in the ".profile" file.
X
X    HOME    Used by the "cd" command in "sh". Set by "login" and "su"
X            to the entry given in the password file. 
X
X    SHELL   Set by "login" and "su" to the entry given in the password
X            file (default is "/bin/sh"). 
X
X    TERM
X    TERMCAP Used by the termcap routines to determine the terminal's
X            type.
X
X    IFS
X    PS1
X    PS2     Used by "sh". These are not automatically exported.
X
XFILES
X    ~/.profile
X    /etc/passwd
X
XSEE ALSO
X    login(1), printenv(1), sh(1), su(1), tset(1), execve(2)
X    getenv(3), putenv(3), termcap(3), termcap(4)
/
--------------------------------------------------------------------
               Edwin L. Froese
                  uw-beaver!ubc-vision!mprg!handel!froese

               Terrence W. Holm
                  uw-beaver!uvicctr!sirius!tholm