Back to index

tetex-bin  3.0
putenv.c
Go to the documentation of this file.
00001 /****************************************************************/
00002 /*                                                      */
00003 /*     putenv(3)                                        */
00004 /*                                                      */
00005 /*            Change or add an environment entry        */
00006 /*                                                      */
00007 /****************************************************************/
00008 /*   origination        1987-Oct-7               T. Holm       */
00009 /****************************************************************/
00010 
00011 /*
00012 Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
00013 From: tholm@uvicctr.UUCP (Terrence W. Holm)
00014 Newsgroups: comp.os.minix
00015 Subject: putenv(3)
00016 Message-ID: <395@uvicctr.UUCP>
00017 Date: 5 May 88 06:40:52 GMT
00018 Organization: University of Victoria, Victoria B.C. Canada
00019 
00020 EFTH Minix report #2  - May 1988 -  putenv(3)
00021 
00022 This is an implementation of putenv(3) that we
00023 wrote for Minix. Please consider this a public
00024 domain program.
00025 */
00026 
00027 #define NULL 0
00028 #define  PSIZE  sizeof(char *)
00029 
00030 extern  char  **environ;
00031 
00032 char  *strchr();
00033 char  *malloc();
00034 
00035 /****************************************************************/
00036 /*                                                      */
00037 /*      int                                             */
00038 /*     putenv( entry )                                         */
00039 /*                                                      */
00040 /*            The "entry" should follow the form               */
00041 /*            "NAME=VALUE". This routine will search the       */
00042 /*            user environment for "NAME" and replace its      */
00043 /*            value with "VALUE".                       */
00044 /*                                                      */
00045 /*            Note that "entry" is not copied, it is used      */
00046 /*            as the environment entry. This means that it     */
00047 /*            must not be unallocated or otherwise modifed     */
00048 /*            by the caller, unless it is replaced by a        */
00049 /*            subsequent putenv().                      */
00050 /*                                                      */
00051 /*            If the name is not found in the environment,     */
00052 /*            then a new vector of pointers is allocated,      */
00053 /*            "entry" is put at the end and the global  */
00054 /*            variable "environ" is updated.                   */
00055 /*                                                      */
00056 /*            This function normally returns 0, but -1  */
00057 /*            is returned if it can not allocate enough        */
00058 /*            space using malloc(3), or "entry" does not       */
00059 /*            contain a '='.                                   */
00060 /*                                                      */
00061 /****************************************************************/
00062 
00063 
00064 int
00065 putenv( entry )
00066   char *entry;
00067 {
00068   unsigned length;
00069   unsigned size;
00070   char     *temp;
00071   char     **p;
00072   char     **new_environ;
00073 
00074   /*  Find the length of the "NAME="  */
00075 
00076   temp = strchr(entry,'=');
00077   if ( temp == 0 )
00078     return( -1 );
00079 
00080   length = (unsigned) (temp - entry + 1);
00081 
00082 
00083   /*  Scan through the environment looking for "NAME="  */
00084 
00085   for ( p=environ; *p != 0 ; p++ )
00086     if ( strncmp( entry, *p, length ) == 0 )
00087       {
00088       *p = entry;
00089       return( 0 );
00090       }
00091 
00092 
00093   /*  The name was not found, build a bigger environment  */
00094 
00095   size = p - environ;
00096 
00097   new_environ = (char **) malloc( (size+2)*PSIZE );
00098 
00099   if ( new_environ == (char **) NULL )
00100     return( -1 );
00101 
00102   memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
00103 
00104   new_environ[size]   = entry;
00105   new_environ[size+1] = NULL;
00106 
00107   environ = new_environ;
00108 
00109   return(0);
00110 }