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