|
|
1.1 root 1: /* environ.c -- library for manipulating environments for GNU.
2: Copyright (C) 1986 Free Software Foundation, Inc.
3:
4: NO WARRANTY
5:
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
15: CORRECTION.
16:
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.
27:
28: GENERAL PUBLIC LICENSE TO COPY
29:
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.
37:
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:
41:
42: a) cause the modified files to carry prominent notices stating
43: that you changed the files and the date of any change; and
44:
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).
51:
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.
55:
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.
59:
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:
63:
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,
67:
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,
73:
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.)
78:
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.
83:
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.
91:
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
98: software.
99:
100: In other words, feel free to share this program, but don't try to
101: stop anyone else from sharing it. */
102:
103: #define min(a, b) ((a) < (b) ? (a) : (b))
104: #define max(a, b) ((a) > (b) ? (a) : (b))
105:
106: #include "environ.h"
107:
108: /* Return a new environment object. */
109:
110: struct environ *
111: make_environ ()
112: {
113: register struct environ *e;
114:
115: e = (struct environ *) xmalloc (sizeof (struct environ));
116:
117: e->allocated = 10;
118: e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
119: e->vector[0] = 0;
120: return e;
121: }
122:
123: /* Free an environment and all the strings in it. */
124:
125: void
126: free_environ (e)
127: register struct environ *e;
128: {
129: register char **vector = e->vector;
130:
131: while (*vector)
132: free (*vector++);
133:
134: free (e);
135: }
136:
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. */
140:
141: void
142: init_environ (e)
143: register struct environ *e;
144: {
145: extern char **environ;
146: register int i;
147:
148: for (i = 0; environ[i]; i++);
149:
150: if (e->allocated < i)
151: {
152: e->allocated = max (i, e->allocated + 10);
153: e->vector = (char **) xrealloc (e->vector,
154: (e->allocated + 1) * sizeof (char *));
155: }
156:
157: bcopy (environ, e->vector, (i + 1) * sizeof (char *));
158:
159: while (--i >= 0)
160: {
161: register int len = strlen (e->vector[i]);
162: register char *new = (char *) xmalloc (len + 1);
163: bcopy (e->vector[i], new, len);
164: e->vector[i] = new;
165: }
166: }
167:
168: /* Return the vector of environment E.
169: This is used to get something to pass to execve. */
170:
171: char **
172: environ_vector (e)
173: struct environ *e;
174: {
175: return e->vector;
176: }
177:
178: /* Return the value in environment E of variable VAR. */
179:
180: char *
181: get_in_environ (e, var)
182: struct environ *e;
183: char *var;
184: {
185: register int len = strlen (var);
186: register char **vector = e->vector;
187: register char *s;
188:
189: for (; s = *vector; vector++)
190: if (!strncmp (s, var, len)
191: && s[len] == '=')
192: return &s[len + 1];
193:
194: return 0;
195: }
196:
197: /* Store the value in E of VAR as VALUE. */
198:
199: void
200: set_in_environ (e, var, value)
201: struct environ *e;
202: char *var;
203: char *value;
204: {
205: register int i;
206: register int len = strlen (var);
207: register char **vector = e->vector;
208: register char *s;
209:
210: for (i = 0; s = vector[i]; i++)
211: if (!strncmp (s, var, len)
212: && s[len] == '=')
213: break;
214:
215: if (s == 0)
216: {
217: if (i == e->allocated)
218: {
219: e->allocated += 10;
220: vector = (char **) xrealloc (vector,
221: (e->allocated + 1) * sizeof (char *));
222: e->vector = vector;
223: }
224: vector[i + 1] = 0;
225: }
226: else
227: free (s);
228:
229: s = (char *) xmalloc (len + strlen (value) + 2);
230: strcpy (s, var);
231: strcat (s, "=");
232: strcat (s, value);
233: vector[i] = s;
234: return;
235: }
236:
237: /* Remove the setting for variable VAR from environment E. */
238:
239: void
240: unset_in_environ (e, var)
241: struct environ *e;
242: char *var;
243: {
244: register int len = strlen (var);
245: register char **vector = e->vector;
246: register char *s;
247:
248: for (; s = *vector; vector++)
249: if (!strncmp (s, var, len)
250: && s[len] == '=')
251: {
252: free (s);
253: bcopy (vector + 1, vector,
254: (e->allocated - (vector - e->vector)) * sizeof (char *));
255: e->vector[e->allocated - 1] = 0;
256: return;
257: }
258: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.