1 /****************************************************************/
5 /* Change or add an environment entry */
7 /****************************************************************/
8 /* origination 1987-Oct-7 T. Holm */
9 /****************************************************************/
12 Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
14 Newsgroups: comp.os.minix
17 Date: 5 May 88 06:40:52 GMT
18 Organization: University of Victoria, Victoria B.C. Canada
20 EFTH Minix report #2 - May 1988 - putenv(3)
22 This is an implementation of putenv(3) that we
23 wrote for Minix. Please consider this a public
30 #define PSIZE sizeof(char *)
32 extern char **environ;
37 /****************************************************************/
42 /* The "entry" should follow the form */
43 /* "NAME=VALUE". This routine will search the */
44 /* user environment for "NAME" and replace its */
45 /* value with "VALUE". */
47 /* Note that "entry" is not copied, it is used */
48 /* as the environment entry. This means that it */
49 /* must not be unallocated or otherwise modifed */
50 /* by the caller, unless it is replaced by a */
51 /* subsequent putenv(). */
53 /* If the name is not found in the environment, */
54 /* then a new vector of pointers is allocated, */
55 /* "entry" is put at the end and the global */
56 /* variable "environ" is updated. */
58 /* This function normally returns NULL, but -1 */
59 /* is returned if it can not allocate enough */
60 /* space using malloc(3), or "entry" does not */
63 /****************************************************************/
76 /* Find the length of the "NAME=" */
78 temp = strchr(entry,'=');
82 length = (unsigned) (temp - entry + 1);
85 /* Scan through the environment looking for "NAME=" */
87 for ( p=environ; *p != 0 ; p++ )
88 if ( strncmp( entry, *p, length ) == 0 )
95 /* The name was not found, build a bigger environment */
99 new_environ = (char **) malloc( (size+2)*PSIZE );
101 if ( new_environ == (char **) NULL )
104 memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
106 new_environ[size] = entry;
107 new_environ[size+1] = NULL;
109 environ = new_environ;