1*4a5d661aSToomas Soome /* 2*4a5d661aSToomas Soome * Copyright (c) 1998 Michael Smith. 3*4a5d661aSToomas Soome * All rights reserved. 4*4a5d661aSToomas Soome * 5*4a5d661aSToomas Soome * Redistribution and use in source and binary forms, with or without 6*4a5d661aSToomas Soome * modification, are permitted provided that the following conditions 7*4a5d661aSToomas Soome * are met: 8*4a5d661aSToomas Soome * 1. Redistributions of source code must retain the above copyright 9*4a5d661aSToomas Soome * notice, this list of conditions and the following disclaimer. 10*4a5d661aSToomas Soome * 2. Redistributions in binary form must reproduce the above copyright 11*4a5d661aSToomas Soome * notice, this list of conditions and the following disclaimer in the 12*4a5d661aSToomas Soome * documentation and/or other materials provided with the distribution. 13*4a5d661aSToomas Soome * 14*4a5d661aSToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*4a5d661aSToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*4a5d661aSToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*4a5d661aSToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*4a5d661aSToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*4a5d661aSToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*4a5d661aSToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*4a5d661aSToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*4a5d661aSToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*4a5d661aSToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*4a5d661aSToomas Soome * SUCH DAMAGE. 25*4a5d661aSToomas Soome */ 26*4a5d661aSToomas Soome 27*4a5d661aSToomas Soome #include <sys/cdefs.h> 28*4a5d661aSToomas Soome __FBSDID("$FreeBSD$"); 29*4a5d661aSToomas Soome 30*4a5d661aSToomas Soome /* 31*4a5d661aSToomas Soome * Manage an environment-like space in which string variables may be stored. 32*4a5d661aSToomas Soome * Provide support for some method-like operations for setting/retrieving 33*4a5d661aSToomas Soome * variables in order to allow some type strength. 34*4a5d661aSToomas Soome */ 35*4a5d661aSToomas Soome 36*4a5d661aSToomas Soome #include "stand.h" 37*4a5d661aSToomas Soome 38*4a5d661aSToomas Soome #include <string.h> 39*4a5d661aSToomas Soome 40*4a5d661aSToomas Soome static void env_discard(struct env_var *ev); 41*4a5d661aSToomas Soome 42*4a5d661aSToomas Soome struct env_var *environ = NULL; 43*4a5d661aSToomas Soome 44*4a5d661aSToomas Soome /* 45*4a5d661aSToomas Soome * Look up (name) and return it's env_var structure. 46*4a5d661aSToomas Soome */ 47*4a5d661aSToomas Soome struct env_var * 48*4a5d661aSToomas Soome env_getenv(const char *name) 49*4a5d661aSToomas Soome { 50*4a5d661aSToomas Soome struct env_var *ev; 51*4a5d661aSToomas Soome 52*4a5d661aSToomas Soome for (ev = environ; ev != NULL; ev = ev->ev_next) 53*4a5d661aSToomas Soome if (!strcmp(ev->ev_name, name)) 54*4a5d661aSToomas Soome break; 55*4a5d661aSToomas Soome return(ev); 56*4a5d661aSToomas Soome } 57*4a5d661aSToomas Soome 58*4a5d661aSToomas Soome /* 59*4a5d661aSToomas Soome * Some notes: 60*4a5d661aSToomas Soome * 61*4a5d661aSToomas Soome * If the EV_VOLATILE flag is set, a copy of the variable is made. 62*4a5d661aSToomas Soome * If EV_DYNAMIC is set, the variable has been allocated with 63*4a5d661aSToomas Soome * malloc and ownership transferred to the environment. 64*4a5d661aSToomas Soome * If (value) is NULL, the variable is set but has no value. 65*4a5d661aSToomas Soome */ 66*4a5d661aSToomas Soome int 67*4a5d661aSToomas Soome env_setenv(const char *name, int flags, const void *value, 68*4a5d661aSToomas Soome ev_sethook_t sethook, ev_unsethook_t unsethook) 69*4a5d661aSToomas Soome { 70*4a5d661aSToomas Soome struct env_var *ev, *curr, *last; 71*4a5d661aSToomas Soome 72*4a5d661aSToomas Soome if ((ev = env_getenv(name)) != NULL) { 73*4a5d661aSToomas Soome /* 74*4a5d661aSToomas Soome * If there's a set hook, let it do the work (unless we are working 75*4a5d661aSToomas Soome * for one already. 76*4a5d661aSToomas Soome */ 77*4a5d661aSToomas Soome if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK)) 78*4a5d661aSToomas Soome return (ev->ev_sethook(ev, flags, value)); 79*4a5d661aSToomas Soome 80*4a5d661aSToomas Soome /* If there is data in the variable, discard it. */ 81*4a5d661aSToomas Soome if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0) 82*4a5d661aSToomas Soome free(ev->ev_value); 83*4a5d661aSToomas Soome ev->ev_value = NULL; 84*4a5d661aSToomas Soome ev->ev_flags &= ~EV_DYNAMIC; 85*4a5d661aSToomas Soome 86*4a5d661aSToomas Soome } else { 87*4a5d661aSToomas Soome 88*4a5d661aSToomas Soome /* 89*4a5d661aSToomas Soome * New variable; create and sort into list 90*4a5d661aSToomas Soome */ 91*4a5d661aSToomas Soome ev = malloc(sizeof(struct env_var)); 92*4a5d661aSToomas Soome ev->ev_name = strdup(name); 93*4a5d661aSToomas Soome ev->ev_value = NULL; 94*4a5d661aSToomas Soome ev->ev_flags = 0; 95*4a5d661aSToomas Soome /* hooks can only be set when the variable is instantiated */ 96*4a5d661aSToomas Soome ev->ev_sethook = sethook; 97*4a5d661aSToomas Soome ev->ev_unsethook = unsethook; 98*4a5d661aSToomas Soome 99*4a5d661aSToomas Soome /* Sort into list */ 100*4a5d661aSToomas Soome ev->ev_prev = NULL; 101*4a5d661aSToomas Soome ev->ev_next = NULL; 102*4a5d661aSToomas Soome /* Search for the record to insert before */ 103*4a5d661aSToomas Soome for (last = NULL, curr = environ; 104*4a5d661aSToomas Soome curr != NULL; 105*4a5d661aSToomas Soome last = curr, curr = curr->ev_next) { 106*4a5d661aSToomas Soome 107*4a5d661aSToomas Soome if (strcmp(ev->ev_name, curr->ev_name) < 0) { 108*4a5d661aSToomas Soome if (curr->ev_prev) { 109*4a5d661aSToomas Soome curr->ev_prev->ev_next = ev; 110*4a5d661aSToomas Soome } else { 111*4a5d661aSToomas Soome environ = ev; 112*4a5d661aSToomas Soome } 113*4a5d661aSToomas Soome ev->ev_next = curr; 114*4a5d661aSToomas Soome ev->ev_prev = curr->ev_prev; 115*4a5d661aSToomas Soome curr->ev_prev = ev; 116*4a5d661aSToomas Soome break; 117*4a5d661aSToomas Soome } 118*4a5d661aSToomas Soome } 119*4a5d661aSToomas Soome if (curr == NULL) { 120*4a5d661aSToomas Soome if (last == NULL) { 121*4a5d661aSToomas Soome environ = ev; 122*4a5d661aSToomas Soome } else { 123*4a5d661aSToomas Soome last->ev_next = ev; 124*4a5d661aSToomas Soome ev->ev_prev = last; 125*4a5d661aSToomas Soome } 126*4a5d661aSToomas Soome } 127*4a5d661aSToomas Soome } 128*4a5d661aSToomas Soome 129*4a5d661aSToomas Soome /* If we have a new value, use it */ 130*4a5d661aSToomas Soome if (flags & EV_VOLATILE) { 131*4a5d661aSToomas Soome ev->ev_value = strdup(value); 132*4a5d661aSToomas Soome ev->ev_flags |= EV_DYNAMIC; 133*4a5d661aSToomas Soome } else { 134*4a5d661aSToomas Soome ev->ev_value = (char *)value; 135*4a5d661aSToomas Soome ev->ev_flags |= flags & EV_DYNAMIC; 136*4a5d661aSToomas Soome } 137*4a5d661aSToomas Soome 138*4a5d661aSToomas Soome return(0); 139*4a5d661aSToomas Soome } 140*4a5d661aSToomas Soome 141*4a5d661aSToomas Soome char * 142*4a5d661aSToomas Soome getenv(const char *name) 143*4a5d661aSToomas Soome { 144*4a5d661aSToomas Soome struct env_var *ev; 145*4a5d661aSToomas Soome 146*4a5d661aSToomas Soome /* Set but no value gives empty string */ 147*4a5d661aSToomas Soome if ((ev = env_getenv(name)) != NULL) { 148*4a5d661aSToomas Soome if (ev->ev_value != NULL) 149*4a5d661aSToomas Soome return(ev->ev_value); 150*4a5d661aSToomas Soome return(""); 151*4a5d661aSToomas Soome } 152*4a5d661aSToomas Soome return(NULL); 153*4a5d661aSToomas Soome } 154*4a5d661aSToomas Soome 155*4a5d661aSToomas Soome int 156*4a5d661aSToomas Soome setenv(const char *name, const char *value, int overwrite) 157*4a5d661aSToomas Soome { 158*4a5d661aSToomas Soome /* No guarantees about state, always assume volatile */ 159*4a5d661aSToomas Soome if (overwrite || (env_getenv(name) == NULL)) 160*4a5d661aSToomas Soome return(env_setenv(name, EV_VOLATILE, value, NULL, NULL)); 161*4a5d661aSToomas Soome return(0); 162*4a5d661aSToomas Soome } 163*4a5d661aSToomas Soome 164*4a5d661aSToomas Soome int 165*4a5d661aSToomas Soome putenv(const char *string) 166*4a5d661aSToomas Soome { 167*4a5d661aSToomas Soome char *value, *copy; 168*4a5d661aSToomas Soome int result; 169*4a5d661aSToomas Soome 170*4a5d661aSToomas Soome copy = strdup(string); 171*4a5d661aSToomas Soome if ((value = strchr(copy, '=')) != NULL) 172*4a5d661aSToomas Soome *(value++) = 0; 173*4a5d661aSToomas Soome result = setenv(copy, value, 1); 174*4a5d661aSToomas Soome free(copy); 175*4a5d661aSToomas Soome return(result); 176*4a5d661aSToomas Soome } 177*4a5d661aSToomas Soome 178*4a5d661aSToomas Soome int 179*4a5d661aSToomas Soome unsetenv(const char *name) 180*4a5d661aSToomas Soome { 181*4a5d661aSToomas Soome struct env_var *ev; 182*4a5d661aSToomas Soome int err; 183*4a5d661aSToomas Soome 184*4a5d661aSToomas Soome err = 0; 185*4a5d661aSToomas Soome if ((ev = env_getenv(name)) == NULL) { 186*4a5d661aSToomas Soome err = ENOENT; 187*4a5d661aSToomas Soome } else { 188*4a5d661aSToomas Soome if (ev->ev_unsethook != NULL) 189*4a5d661aSToomas Soome err = ev->ev_unsethook(ev); 190*4a5d661aSToomas Soome if (err == 0) { 191*4a5d661aSToomas Soome env_discard(ev); 192*4a5d661aSToomas Soome } 193*4a5d661aSToomas Soome } 194*4a5d661aSToomas Soome return(err); 195*4a5d661aSToomas Soome } 196*4a5d661aSToomas Soome 197*4a5d661aSToomas Soome static void 198*4a5d661aSToomas Soome env_discard(struct env_var *ev) 199*4a5d661aSToomas Soome { 200*4a5d661aSToomas Soome if (ev->ev_prev) 201*4a5d661aSToomas Soome ev->ev_prev->ev_next = ev->ev_next; 202*4a5d661aSToomas Soome if (ev->ev_next) 203*4a5d661aSToomas Soome ev->ev_next->ev_prev = ev->ev_prev; 204*4a5d661aSToomas Soome if (environ == ev) 205*4a5d661aSToomas Soome environ = ev->ev_next; 206*4a5d661aSToomas Soome free(ev->ev_name); 207*4a5d661aSToomas Soome if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0) 208*4a5d661aSToomas Soome free(ev->ev_value); 209*4a5d661aSToomas Soome free(ev); 210*4a5d661aSToomas Soome } 211*4a5d661aSToomas Soome 212*4a5d661aSToomas Soome int 213*4a5d661aSToomas Soome env_noset(struct env_var *ev __unused, int flags __unused, 214*4a5d661aSToomas Soome const void *value __unused) 215*4a5d661aSToomas Soome { 216*4a5d661aSToomas Soome return(EPERM); 217*4a5d661aSToomas Soome } 218*4a5d661aSToomas Soome 219*4a5d661aSToomas Soome int 220*4a5d661aSToomas Soome env_nounset(struct env_var *ev __unused) 221*4a5d661aSToomas Soome { 222*4a5d661aSToomas Soome return(EPERM); 223*4a5d661aSToomas Soome } 224