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: tholm@uvicctr.UUCP (Terrence W. Holm) | |
14 | Newsgroups: comp.os.minix | |
15 | Subject: putenv(3) | |
16 | Message-ID: <395@uvicctr.UUCP> | |
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> | |
ba47c66a | 28 | #include <string.h> |
b6a3c755 | 29 | |
b6a3c755 JK |
30 | #define PSIZE sizeof(char *) |
31 | ||
b6a3c755 JK |
32 | extern char **environ; |
33 | ||
b38f304c | 34 | char *strchr(); |
b6a3c755 JK |
35 | char *malloc(); |
36 | ||
b6a3c755 JK |
37 | /****************************************************************/ |
38 | /* */ | |
7d9884b9 | 39 | /* int */ |
b6a3c755 JK |
40 | /* putenv( entry ) */ |
41 | /* */ | |
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". */ | |
46 | /* */ | |
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(). */ | |
52 | /* */ | |
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. */ | |
57 | /* */ | |
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 */ | |
61 | /* contain a '='. */ | |
62 | /* */ | |
63 | /****************************************************************/ | |
64 | ||
65 | ||
7d9884b9 | 66 | int |
b6a3c755 JK |
67 | putenv( entry ) |
68 | char *entry; | |
7d9884b9 | 69 | { |
b6a3c755 JK |
70 | unsigned length; |
71 | unsigned size; | |
c55e6167 | 72 | char *temp; |
b6a3c755 JK |
73 | char **p; |
74 | char **new_environ; | |
75 | ||
76 | /* Find the length of the "NAME=" */ | |
77 | ||
b38f304c | 78 | temp = strchr(entry,'='); |
c55e6167 | 79 | if ( temp == 0 ) |
b6a3c755 JK |
80 | return( -1 ); |
81 | ||
c55e6167 | 82 | length = (unsigned) (temp - entry + 1); |
b6a3c755 JK |
83 | |
84 | ||
85 | /* Scan through the environment looking for "NAME=" */ | |
86 | ||
87 | for ( p=environ; *p != 0 ; p++ ) | |
88 | if ( strncmp( entry, *p, length ) == 0 ) | |
89 | { | |
90 | *p = entry; | |
619fd145 | 91 | return( 0 ); |
b6a3c755 JK |
92 | } |
93 | ||
94 | ||
95 | /* The name was not found, build a bigger environment */ | |
96 | ||
97 | size = p - environ; | |
98 | ||
99 | new_environ = (char **) malloc( (size+2)*PSIZE ); | |
100 | ||
c55e6167 | 101 | if ( new_environ == (char **) NULL ) |
b6a3c755 JK |
102 | return( -1 ); |
103 | ||
4ed3a9ea | 104 | memcpy ((char *) new_environ, (char *) environ, size*PSIZE ); |
b6a3c755 JK |
105 | |
106 | new_environ[size] = entry; | |
107 | new_environ[size+1] = NULL; | |
108 | ||
109 | environ = new_environ; | |
110 | ||
619fd145 | 111 | return(0); |
7d9884b9 | 112 | } |