1 /****************************************************************/
5 /* Change or add an environment entry */
7 /****************************************************************/
8 /* origination 1987-Oct-7 T. Holm */
9 /****************************************************************/
12 From pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm Wed May 4 23:40:52 1988
13 Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
15 Newsgroups: comp.os.minix
18 Date: 5 May 88 06:40:52 GMT
20 Organization: University of Victoria, Victoria B.C. Canada
24 EFTH Minix report #2 - May 1988 - putenv(3)
27 This is an implementation of putenv(3) that we
28 wrote for Minix. Please consider this a public
35 #define PSIZE sizeof(char *)
38 extern char **environ;
45 /****************************************************************/
49 /* The "entry" should follow the form */
50 /* "NAME=VALUE". This routine will search the */
51 /* user environment for "NAME" and replace its */
52 /* value with "VALUE". */
54 /* Note that "entry" is not copied, it is used */
55 /* as the environment entry. This means that it */
56 /* must not be unallocated or otherwise modifed */
57 /* by the caller, unless it is replaced by a */
58 /* subsequent putenv(). */
60 /* If the name is not found in the environment, */
61 /* then a new vector of pointers is allocated, */
62 /* "entry" is put at the end and the global */
63 /* variable "environ" is updated. */
65 /* This function normally returns NULL, but -1 */
66 /* is returned if it can not allocate enough */
67 /* space using malloc(3), or "entry" does not */
70 /****************************************************************/
82 /* Find the length of the "NAME=" */
84 if ( (length=(unsigned) index(entry,'=')) == NULL )
87 length = length - (unsigned) entry + 1;
90 /* Scan through the environment looking for "NAME=" */
92 for ( p=environ; *p != 0 ; p++ )
93 if ( strncmp( entry, *p, length ) == 0 )
100 /* The name was not found, build a bigger environment */
104 new_environ = (char **) malloc( (size+2)*PSIZE );
106 if ( new_environ == NULL )
109 bcopy( (char *) environ, (char *) new_environ, size*PSIZE );
111 new_environ[size] = entry;
112 new_environ[size+1] = NULL;
114 environ = new_environ;