1268
|
1 /****************************************************************/ |
|
2 /* */ |
|
3 /* putenv(3) */ |
|
4 /* */ |
|
5 /* Change or add an environment entry */ |
|
6 /* */ |
|
7 /****************************************************************/ |
|
8 /* origination 1987-Oct-7 T. Holm */ |
|
9 /* (slightly modified by karl@cs.umb.edu for kpathsea.) */ |
|
10 /****************************************************************/ |
|
11 |
|
12 /* for HAVE_PUTENV and const -- need nothing else. */ |
|
13 #include <kpathsea/c-auto.h> |
|
14 |
|
15 #ifndef HAVE_PUTENV /* whole file */ |
|
16 |
|
17 /* |
|
18 Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm |
|
19 From: tholm@uvicctr.UUCP (Terrence W. Holm) |
|
20 Newsgroups: comp.os.minix |
|
21 Subject: putenv(3) |
|
22 Message-ID: <395@uvicctr.UUCP> |
|
23 Date: 5 May 88 06:40:52 GMT |
|
24 Organization: University of Victoria, Victoria B.C. Canada |
|
25 |
|
26 EFTH Minix report #2 - May 1988 - putenv(3) |
|
27 |
|
28 This is an implementation of putenv(3) that we |
|
29 wrote for Minix. Please consider this a public |
|
30 domain program. |
|
31 */ |
|
32 |
|
33 #define NULL 0 |
|
34 #define PSIZE sizeof(char *) |
|
35 |
|
36 extern char **environ; |
|
37 |
|
38 char *strchr(); |
|
39 char *malloc(); |
|
40 |
|
41 /****************************************************************/ |
|
42 /* */ |
|
43 /* int */ |
|
44 /* putenv( entry ) */ |
|
45 /* */ |
|
46 /* The "entry" should follow the form */ |
|
47 /* "NAME=VALUE". This routine will search the */ |
|
48 /* user environment for "NAME" and replace its */ |
|
49 /* value with "VALUE". */ |
|
50 /* */ |
|
51 /* Note that "entry" is not copied, it is used */ |
|
52 /* as the environment entry. This means that it */ |
|
53 /* must not be unallocated or otherwise modifed */ |
|
54 /* by the caller, unless it is replaced by a */ |
|
55 /* subsequent putenv(). */ |
|
56 /* */ |
|
57 /* If the name is not found in the environment, */ |
|
58 /* then a new vector of pointers is allocated, */ |
|
59 /* "entry" is put at the end and the global */ |
|
60 /* variable "environ" is updated. */ |
|
61 /* */ |
|
62 /* This function normally returns 0, but -1 */ |
|
63 /* is returned if it can not allocate enough */ |
|
64 /* space using malloc(3), or "entry" does not */ |
|
65 /* contain a '='. */ |
|
66 /* */ |
|
67 /****************************************************************/ |
|
68 |
|
69 |
|
70 int |
|
71 putenv( entry ) |
|
72 char *entry; |
|
73 { |
|
74 unsigned length; |
|
75 unsigned size; |
|
76 char *temp; |
|
77 char **p; |
|
78 char **new_environ; |
|
79 |
|
80 /* Find the length of the "NAME=" */ |
|
81 |
|
82 temp = strchr(entry,'='); |
|
83 if ( temp == 0 ) |
|
84 return( -1 ); |
|
85 |
|
86 length = (unsigned) (temp - entry + 1); |
|
87 |
|
88 |
|
89 /* Scan through the environment looking for "NAME=" */ |
|
90 |
|
91 for ( p=environ; *p != 0 ; p++ ) |
|
92 if ( strncmp( entry, *p, length ) == 0 ) |
|
93 { |
|
94 *p = entry; |
|
95 return( 0 ); |
|
96 } |
|
97 |
|
98 |
|
99 /* The name was not found, build a bigger environment */ |
|
100 |
|
101 size = p - environ; |
|
102 |
|
103 new_environ = (char **) malloc( (size+2)*PSIZE ); |
|
104 |
|
105 if ( new_environ == (char **) NULL ) |
|
106 return( -1 ); |
|
107 |
|
108 memcpy ((char *) new_environ, (const char *) environ, size*PSIZE ); |
|
109 |
|
110 new_environ[size] = entry; |
|
111 new_environ[size+1] = NULL; |
|
112 |
|
113 environ = new_environ; |
|
114 |
|
115 return(0); |
|
116 } |
|
117 |
|
118 #endif /* not HAVE_PUTENV */ |