home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / putenv.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  3KB  |  117 lines

  1. /****************************************************************/
  2. /*                                */
  3. /*    putenv(3)                        */
  4. /*                                */
  5. /*        Change or add an environment entry        */
  6. /*                                */
  7. /****************************************************************/
  8. /*   origination        1987-Oct-7               T. Holm    */
  9. /****************************************************************/
  10.  
  11. /*
  12. Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
  13. From: tholm@uvicctr.UUCP (Terrence W. Holm)
  14. Newsgroups: comp.os.minix
  15. Subject: putenv(3)
  16. Message-ID: <395@uvicctr.UUCP>
  17. Date: 5 May 88 06:40:52 GMT
  18. Organization: University of Victoria, Victoria B.C. Canada
  19.  
  20. EFTH Minix report #2  - May 1988 -  putenv(3)
  21.  
  22. This is an implementation of putenv(3) that we
  23. wrote for Minix. Please consider this a public
  24. domain program.
  25. */
  26.  
  27. /* Modified by Klaus Gebhardt, 1998-1999 */
  28.  
  29. #define NULL 0
  30. #define  PSIZE  sizeof(char *)
  31.  
  32. extern  char  **environ;
  33.  
  34. char  *strchr();
  35. char  *malloc();
  36.  
  37. #ifdef __EMX__
  38. int strncmp (const char *, const char *, int);
  39. #endif
  40.  
  41. /****************************************************************/
  42. /*                                */
  43. /*      int                            */
  44. /*    putenv( entry )                        */
  45. /*                                */
  46. /*        The "entry" should follow the form         */
  47. /*        "NAME=VALUE". This routine will search the     */
  48. /*        user environment for "NAME" and replace its     */
  49. /*        value with "VALUE".                */
  50. /*                                */
  51. /*        Note that "entry" is not copied, it is used     */
  52. /*        as the environment entry. This means that it     */
  53. /*        must not be unallocated or otherwise modifed     */
  54. /*        by the caller, unless it is replaced by a     */
  55. /*        subsequent putenv().                */
  56. /*                                */
  57. /*        If the name is not found in the environment,     */
  58. /*        then a new vector of pointers is allocated,     */
  59. /*        "entry" is put at the end and the global     */
  60. /*        variable "environ" is updated.            */
  61. /*                                */
  62. /*        This function normally returns 0, but -1    */
  63. /*        is returned if it can not allocate enough     */
  64. /*        space using malloc(3), or "entry" does not    */
  65. /*        contain a '='.                    */
  66. /*                                */
  67. /****************************************************************/
  68.  
  69.  
  70. int
  71. putenv( entry )
  72.   char *entry;
  73. {
  74.   unsigned length;
  75.   unsigned size;
  76.   char     *temp;
  77.   char     **p;
  78.   char     **new_environ;
  79.  
  80.   /*  Find the length of the "NAME="  */
  81.  
  82.   temp = strchr(entry,'=');
  83.   if ( temp == 0 )
  84.     return( -1 );
  85.  
  86.   length = (unsigned) (temp - entry + 1);
  87.  
  88.  
  89.   /*  Scan through the environment looking for "NAME="  */
  90.  
  91.   for ( p=environ; *p != 0 ; p++ )
  92.     if ( strncmp( entry, *p, length ) == 0 )
  93.       {
  94.       *p = entry;
  95.       return( 0 );
  96.       }
  97.  
  98.  
  99.   /*  The name was not found, build a bigger environment  */
  100.  
  101.   size = p - environ;
  102.  
  103.   new_environ = (char **) malloc( (size+2)*PSIZE );
  104.  
  105.   if ( new_environ == (char **) NULL )
  106.     return( -1 );
  107.  
  108.   memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
  109.  
  110.   new_environ[size]   = entry;
  111.   new_environ[size+1] = NULL;
  112.  
  113.   environ = new_environ;
  114.  
  115.   return(0);
  116. }
  117.