Deleted Added
full compact
environment.c (38452) environment.c (39665)
1/*
2 * Copyright (c) 1998 Michael Smith.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*
2 * Copyright (c) 1998 Michael Smith.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id$
26 * $Id: environment.c,v 1.1.1.1 1998/08/20 08:19:55 msmith Exp $
27 *
28 */
29
30/*
31 * Manage an environment-like space in which string variables may be stored.
32 * Provide support for some method-like operations for setting/retrieving
33 * variables in order to allow some type strength.
34 */

--- 30 unchanged lines hidden (view full) ---

65 */
66int
67env_setenv(const char *name, int flags, void *value, ev_sethook_t sethook,
68 ev_unsethook_t unsethook)
69{
70 struct env_var *ev, *curr, *last;
71
72 if ((ev = env_getenv(name)) != NULL) {
27 *
28 */
29
30/*
31 * Manage an environment-like space in which string variables may be stored.
32 * Provide support for some method-like operations for setting/retrieving
33 * variables in order to allow some type strength.
34 */

--- 30 unchanged lines hidden (view full) ---

65 */
66int
67env_setenv(const char *name, int flags, void *value, ev_sethook_t sethook,
68 ev_unsethook_t unsethook)
69{
70 struct env_var *ev, *curr, *last;
71
72 if ((ev = env_getenv(name)) != NULL) {
73
74 /*
75 * If there's a set hook, let it do the work (unless we are working
76 * for one already.
77 */
78 if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
79 return(ev->ev_sethook(ev, flags, value));
80 } else {
73 /*
74 * If there's a set hook, let it do the work (unless we are working
75 * for one already.
76 */
77 if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
78 return(ev->ev_sethook(ev, flags, value));
79 } else {
80
81 /*
82 * New variable; create and sort into list
83 */
81 ev = malloc(sizeof(struct env_var));
82 ev->ev_name = strdup(name);
83 ev->ev_value = NULL;
84 /* hooks can only be set when the variable is instantiated */
85 ev->ev_sethook = sethook;
86 ev->ev_unsethook = unsethook;
84 ev = malloc(sizeof(struct env_var));
85 ev->ev_name = strdup(name);
86 ev->ev_value = NULL;
87 /* hooks can only be set when the variable is instantiated */
88 ev->ev_sethook = sethook;
89 ev->ev_unsethook = unsethook;
90
91 /* Sort into list */
92 ev->ev_prev = NULL;
93 ev->ev_next = NULL;
94 /* Search for the record to insert before */
95 for (last = NULL, curr = environ;
96 curr != NULL;
97 last = curr, curr = curr->ev_next) {
98
99 if (strcmp(ev->ev_name, curr->ev_name) < 0) {
100 if (curr->ev_prev) {
101 curr->ev_prev->ev_next = ev;
102 } else {
103 environ = ev;
104 }
105 ev->ev_next = curr;
106 ev->ev_prev = curr->ev_prev;
107 curr->ev_prev = ev;
108 break;
109 }
110 }
111 if (curr == NULL) {
112 if (last == NULL) {
113 environ = ev;
114 } else {
115 last->ev_next = ev;
116 ev->ev_prev = last;
117 }
118 }
87 }
88
89 /* If there is data in the variable, discard it */
90 if (ev->ev_value != NULL)
91 free(ev->ev_value);
92
93 /* If we have a new value, use it */
94 if (flags & EV_VOLATILE) {
95 ev->ev_value = strdup(value);
96 } else {
97 ev->ev_value = value;
98 }
99
100 /* Keep the flag components that are relevant */
101 ev->ev_flags = flags & (EV_DYNAMIC);
102
119 }
120
121 /* If there is data in the variable, discard it */
122 if (ev->ev_value != NULL)
123 free(ev->ev_value);
124
125 /* If we have a new value, use it */
126 if (flags & EV_VOLATILE) {
127 ev->ev_value = strdup(value);
128 } else {
129 ev->ev_value = value;
130 }
131
132 /* Keep the flag components that are relevant */
133 ev->ev_flags = flags & (EV_DYNAMIC);
134
103 /* Sort into list */
104 ev->ev_prev = NULL;
105 ev->ev_next = NULL;
106
107 /* Search for the record to insert before */
108 for (last = NULL, curr = environ;
109 curr != NULL;
110 last = curr, curr = curr->ev_next) {
111
112 if (strcmp(ev->ev_name, curr->ev_name) < 0) {
113 if (curr->ev_prev) {
114 curr->ev_prev->ev_next = ev;
115 } else {
116 environ = ev;
117 }
118 ev->ev_next = curr;
119 ev->ev_prev = curr->ev_prev;
120 curr->ev_prev = ev;
121 break;
122 }
123 }
124 if (curr == NULL) {
125 if (last == NULL) {
126 environ = ev;
127 } else {
128 last->ev_next = ev;
129 ev->ev_prev = last;
130 }
131 }
132 return(0);
133}
134
135char *
136getenv(const char *name)
137{
138 struct env_var *ev;
139

--- 13 unchanged lines hidden (view full) ---

153 if (overwrite || (env_getenv(name) == NULL))
154 return(env_setenv(name, EV_VOLATILE, value, NULL, NULL));
155 return(0);
156}
157
158int
159putenv(const char *string)
160{
135 return(0);
136}
137
138char *
139getenv(const char *name)
140{
141 struct env_var *ev;
142

--- 13 unchanged lines hidden (view full) ---

156 if (overwrite || (env_getenv(name) == NULL))
157 return(env_setenv(name, EV_VOLATILE, value, NULL, NULL));
158 return(0);
159}
160
161int
162putenv(const char *string)
163{
161 char *value;
164 char *value, *copy;
165 int result;
162
166
163 if ((value = strchr(string, '=')) != NULL)
167 copy = strdup(string);
168 if ((value = strchr(copy, '=')) != NULL)
164 *(value++) = 0;
169 *(value++) = 0;
165 return(setenv(string, value, 1));
170 result = setenv(copy, value, 1);
171 free(copy);
172 return(result);
166}
167
168int
169unsetenv(const char *name)
170{
171 struct env_var *ev;
172 int err;
173

--- 39 unchanged lines hidden ---
173}
174
175int
176unsetenv(const char *name)
177{
178 struct env_var *ev;
179 int err;
180

--- 39 unchanged lines hidden ---