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
29 #define PSIZE sizeof(char *)
31 extern char **environ;
36 /****************************************************************/
41 /* The "entry" should follow the form */
42 /* "NAME=VALUE". This routine will search the */
43 /* user environment for "NAME" and replace its */
44 /* value with "VALUE". */
46 /* Note that "entry" is not copied, it is used */
47 /* as the environment entry. This means that it */
48 /* must not be unallocated or otherwise modifed */
49 /* by the caller, unless it is replaced by a */
50 /* subsequent putenv(). */
52 /* If the name is not found in the environment, */
53 /* then a new vector of pointers is allocated, */
54 /* "entry" is put at the end and the global */
55 /* variable "environ" is updated. */
57 /* This function normally returns NULL, but -1 */
58 /* is returned if it can not allocate enough */
59 /* space using malloc(3), or "entry" does not */
62 /****************************************************************/
75 /* Find the length of the "NAME=" */
77 temp = index(entry,'=');
81 length = (unsigned) (temp - entry + 1);
84 /* Scan through the environment looking for "NAME=" */
86 for ( p=environ; *p != 0 ; p++ )
87 if ( strncmp( entry, *p, length ) == 0 )
94 /* The name was not found, build a bigger environment */
98 new_environ = (char **) malloc( (size+2)*PSIZE );
100 if ( new_environ == (char **) NULL )
103 memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
105 new_environ[size] = entry;
106 new_environ[size+1] = NULL;
108 environ = new_environ;