1 /* environ.c -- library for manipulating environments for GNU.
2 Copyright (C) 1986 Free Software Foundation, Inc.
6 BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
7 NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
8 WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
9 RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
10 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
11 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
12 FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
13 AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
14 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
17 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
18 STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
19 WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
20 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
21 OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
22 USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
23 DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
24 A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
25 PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
28 GENERAL PUBLIC LICENSE TO COPY
30 1. You may copy and distribute verbatim copies of this source file
31 as you receive it, in any medium, provided that you conspicuously and
32 appropriately publish on each copy a valid copyright notice "Copyright
33 (C) 1986 Free Software Foundation, Inc."; and include following the
34 copyright notice a verbatim copy of the above disclaimer of warranty
35 and of this License. You may charge a distribution fee for the
36 physical act of transferring a copy.
38 2. You may modify your copy or copies of this source file or
39 any portion of it, and copy and distribute such modifications under
40 the terms of Paragraph 1 above, provided that you also do the following:
42 a) cause the modified files to carry prominent notices stating
43 that you changed the files and the date of any change; and
45 b) cause the whole of any work that you distribute or publish,
46 that in whole or in part contains or is a derivative of this
47 program or any part thereof, to be licensed at no charge to all
48 third parties on terms identical to those contained in this
49 License Agreement (except that you may choose to grant more extensive
50 warranty protection to some or all third parties, at your option).
52 c) You may charge a distribution fee for the physical act of
53 transferring a copy, and you may at your option offer warranty
54 protection in exchange for a fee.
56 Mere aggregation of another unrelated program with this program (or its
57 derivative) on a volume of a storage or distribution medium does not bring
58 the other program under the scope of these terms.
60 3. You may copy and distribute this program (or a portion or derivative
61 of it, under Paragraph 2) in object code or executable form under the terms
62 of Paragraphs 1 and 2 above provided that you also do one of the following:
64 a) accompany it with the complete corresponding machine-readable
65 source code, which must be distributed under the terms of
66 Paragraphs 1 and 2 above; or,
68 b) accompany it with a written offer, valid for at least three
69 years, to give any third party free (except for a nominal
70 shipping charge) a complete machine-readable copy of the
71 corresponding source code, to be distributed under the terms of
72 Paragraphs 1 and 2 above; or,
74 c) accompany it with the information you received as to where the
75 corresponding source code may be obtained. (This alternative is
76 allowed only for noncommercial distribution and only if you
77 received the program in object code or executable form alone.)
79 For an executable file, complete source code means all the source code for
80 all modules it contains; but, as a special exception, it need not include
81 source code for modules which are standard libraries that accompany the
82 operating system on which the executable file runs.
84 4. You may not copy, sublicense, distribute or transfer this program
85 except as expressly provided under this License Agreement. Any attempt
86 otherwise to copy, sublicense, distribute or transfer this program is void and
87 your rights to use the program under this License agreement shall be
88 automatically terminated. However, parties who have received computer
89 software programs from you with this License Agreement will not have
90 their licenses terminated so long as such parties remain in full compliance.
92 5. If you wish to incorporate parts of this program into other free
93 programs whose distribution conditions are different, write to the Free
94 Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
95 worked out a simple rule that can be stated here, but we will often permit
96 this. We will be guided by the two goals of preserving the free status of
97 all derivatives of our free software and of promoting the sharing and reuse of
100 In other words, feel free to share this program, but don't try to
101 stop anyone else from sharing it. */
103 #define min(a, b) ((a) < (b) ? (a) : (b))
104 #define max(a, b) ((a) > (b) ? (a) : (b))
108 /* Return a new environment object. */
113 register struct environ *e;
115 e = (struct environ *) xmalloc (sizeof (struct environ));
118 e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
123 /* Free an environment and all the strings in it. */
127 register struct environ *e;
129 register char **vector = e->vector;
137 /* Copy the environment given to this process into E.
138 Also copies all the strings in it, so we can be sure
139 that all strings in these environments are safe to free. */
143 register struct environ *e;
145 extern char **environ;
148 for (i = 0; environ[i]; i++);
150 if (e->allocated < i)
152 e->allocated = max (i, e->allocated + 10);
153 e->vector = (char **) xrealloc (e->vector,
154 (e->allocated + 1) * sizeof (char *));
157 bcopy (environ, e->vector, (i + 1) * sizeof (char *));
161 register int len = strlen (e->vector[i]);
162 register char *new = (char *) xmalloc (len + 1);
163 bcopy (e->vector[i], new, len);
168 /* Return the vector of environment E.
169 This is used to get something to pass to execve. */
178 /* Return the value in environment E of variable VAR. */
181 get_in_environ (e, var)
185 register int len = strlen (var);
186 register char **vector = e->vector;
189 for (; s = *vector; vector++)
190 if (!strncmp (s, var, len)
197 /* Store the value in E of VAR as VALUE. */
200 set_in_environ (e, var, value)
206 register int len = strlen (var);
207 register char **vector = e->vector;
210 for (i = 0; s = vector[i]; i++)
211 if (!strncmp (s, var, len)
217 if (i == e->allocated)
220 vector = (char **) xrealloc (vector,
221 (e->allocated + 1) * sizeof (char *));
229 s = (char *) xmalloc (len + strlen (value) + 2);
237 /* Remove the setting for variable VAR from environment E. */
240 unset_in_environ (e, var)
244 register int len = strlen (var);
245 register char **vector = e->vector;
248 for (; s = *vector; vector++)
249 if (!strncmp (s, var, len)
253 bcopy (vector + 1, vector,
254 (e->allocated - (vector - e->vector)) * sizeof (char *));
255 e->vector[e->allocated - 1] = 0;