]>
Commit | Line | Data |
---|---|---|
b6a3c755 JK |
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 | /* | |
b6a3c755 JK |
12 | Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm |
13 | From: [email protected] (Terrence W. Holm) | |
14 | Newsgroups: comp.os.minix | |
15 | Subject: putenv(3) | |
16 | Message-ID: <[email protected]> | |
17 | Date: 5 May 88 06:40:52 GMT | |
b6a3c755 | 18 | Organization: University of Victoria, Victoria B.C. Canada |
b6a3c755 JK |
19 | |
20 | EFTH Minix report #2 - May 1988 - putenv(3) | |
21 | ||
b6a3c755 JK |
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 | #include <stdio.h> | |
28 | ||
b6a3c755 JK |
29 | #define PSIZE sizeof(char *) |
30 | ||
b6a3c755 JK |
31 | extern char **environ; |
32 | ||
b38f304c | 33 | char *strchr(); |
b6a3c755 JK |
34 | char *malloc(); |
35 | ||
b6a3c755 JK |
36 | /****************************************************************/ |
37 | /* */ | |
7d9884b9 | 38 | /* int */ |
b6a3c755 JK |
39 | /* putenv( entry ) */ |
40 | /* */ | |
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". */ | |
45 | /* */ | |
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(). */ | |
51 | /* */ | |
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. */ | |
56 | /* */ | |
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 */ | |
60 | /* contain a '='. */ | |
61 | /* */ | |
62 | /****************************************************************/ | |
63 | ||
64 | ||
7d9884b9 | 65 | int |
b6a3c755 JK |
66 | putenv( entry ) |
67 | char *entry; | |
7d9884b9 | 68 | { |
b6a3c755 JK |
69 | unsigned length; |
70 | unsigned size; | |
c55e6167 | 71 | char *temp; |
b6a3c755 JK |
72 | char **p; |
73 | char **new_environ; | |
74 | ||
75 | /* Find the length of the "NAME=" */ | |
76 | ||
b38f304c | 77 | temp = strchr(entry,'='); |
c55e6167 | 78 | if ( temp == 0 ) |
b6a3c755 JK |
79 | return( -1 ); |
80 | ||
c55e6167 | 81 | length = (unsigned) (temp - entry + 1); |
b6a3c755 JK |
82 | |
83 | ||
84 | /* Scan through the environment looking for "NAME=" */ | |
85 | ||
86 | for ( p=environ; *p != 0 ; p++ ) | |
87 | if ( strncmp( entry, *p, length ) == 0 ) | |
88 | { | |
89 | *p = entry; | |
619fd145 | 90 | return( 0 ); |
b6a3c755 JK |
91 | } |
92 | ||
93 | ||
94 | /* The name was not found, build a bigger environment */ | |
95 | ||
96 | size = p - environ; | |
97 | ||
98 | new_environ = (char **) malloc( (size+2)*PSIZE ); | |
99 | ||
c55e6167 | 100 | if ( new_environ == (char **) NULL ) |
b6a3c755 JK |
101 | return( -1 ); |
102 | ||
4ed3a9ea | 103 | memcpy ((char *) new_environ, (char *) environ, size*PSIZE ); |
b6a3c755 JK |
104 | |
105 | new_environ[size] = entry; | |
106 | new_environ[size+1] = NULL; | |
107 | ||
108 | environ = new_environ; | |
109 | ||
619fd145 | 110 | return(0); |
7d9884b9 | 111 | } |