env.c revision 3e14f97f673e8a630f076077de35afdd43dc1587
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1982-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypedef struct _env_
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define ENV_PMALLOC 1 /* set if Evar_t->un.ptr *s malloced */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Compares the name portion of name=... only.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int compare(Dt_t *dt, Void_t* key1, Void_t* key2, Dtdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int c,d;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(c=='=')
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(c-d);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sizeof(char*),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * return a pointer to the environment in sorted order
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * NULL is returned if there if there is nospace
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(ep->env = (char**)malloc(sizeof(char*)*(ep->count+1))))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for(vp=(Evar_t*)dtfirst(ep->dt);vp; vp=(Evar_t*)dtnext(ep->dt,vp))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin vp->index = (n<<ENV_BITS) | (vp->index&((1<<ENV_BITS)-1));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * add name=value pair given by <str> to <ep>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * if malloced is set, the variable will be freed when reassigned
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The environment list may become invalidated
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Returns 1 for success, 0 for failure
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * delete name from <ep>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The environment list may become invalidated
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Returns 1 for success, 0 for if name is not present
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * open up a structure to support environment variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * initialize with environment give by <envp>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If <extra> > 0, <extra> slots will be left at beginning of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * environment list when env_get() is involed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If <extra>==ENV_USABLE, then the original environ can be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * used and returned. Otherwise, a new one will be returned
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while(*env++);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while(--n>0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * close <ep> and free up all space used by it