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
13 From: tholm@uvicctr.UUCP (Terrence W. Holm)
14 Newsgroups: comp.os.minix
16 Message-ID: <395@uvicctr.UUCP>
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
28 #define PSIZE sizeof(char *)
30 extern char **environ;
35 /****************************************************************/
40 /* The "entry" should follow the form */
41 /* "NAME=VALUE". This routine will search the */
42 /* user environment for "NAME" and replace its */
43 /* value with "VALUE". */
45 /* Note that "entry" is not copied, it is used */
46 /* as the environment entry. This means that it */
47 /* must not be unallocated or otherwise modifed */
48 /* by the caller, unless it is replaced by a */
49 /* subsequent putenv(). */
51 /* If the name is not found in the environment, */
52 /* then a new vector of pointers is allocated, */
53 /* "entry" is put at the end and the global */
54 /* variable "environ" is updated. */
56 /* This function normally returns 0, but -1 */
57 /* is returned if it can not allocate enough */
58 /* space using malloc(3), or "entry" does not */
61 /****************************************************************/
74 /* Find the length of the "NAME=" */
76 temp = strchr(entry,'=');
80 length = (unsigned) (temp - entry + 1);
83 /* Scan through the environment looking for "NAME=" */
85 for ( p=environ; *p != 0 ; p++ )
86 if ( strncmp( entry, *p, length ) == 0 )
93 /* The name was not found, build a bigger environment */
97 new_environ = (char **) malloc( (size+2)*PSIZE );
99 if ( new_environ == (char **) NULL )
102 memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
104 new_environ[size] = entry;
105 new_environ[size+1] = NULL;
107 environ = new_environ;