2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
12 #include <sys/types.h>
18 extern char **environ ;
19 env_h std_env ; /* created from environ */
21 status_e initenv(void)
23 std_env = env_make( environ ) ;
24 return( ( std_env == NULL ) ? FAILED : OK ) ;
27 static status_e make_env_with_strings(struct environment *, env_h, pset_h);
28 static status_e make_env_from_vars(struct environment *, env_h, pset_h) ;
29 static status_e update_env_with_strings(env_h, pset_h) ;
32 status_e setup_environ( struct service_config *scp, struct service_config *def )
34 struct environment *ep = SC_ENV( scp ) ;
36 if ( ! SC_SPECIFIED( scp, A_PASSENV ) )
38 if ( ! SC_SPECIFIED( def, A_PASSENV ) )
40 if ( ! SC_SPECIFIED( scp, A_ENV ) )
42 ep->env_type = STD_ENV ;
43 ep->env_handle = std_env ;
48 make_env_with_strings( ep, std_env, SC_ENV_VAR_DEFS(scp) ) ) ;
50 else /* SC_SPECIFIED( def, A_PASSENV ) */
52 struct environment *dep = SC_ENV( def ) ;
54 if ( dep->env_type == NO_ENV &&
55 make_env_from_vars( dep, std_env,
56 SC_PASS_ENV_VARS(def) ) == FAILED )
59 if ( ! SC_SPECIFIED( scp, A_ENV ) )
61 ep->env_type = DEF_ENV ;
62 ep->env_handle = dep->env_handle ;
66 return( make_env_with_strings( ep,
67 dep->env_handle, SC_ENV_VAR_DEFS(scp) ) ) ;
70 else /* SC_SPECIFIED( scp, A_PASSENV ) */
72 if ( make_env_from_vars( ep, std_env, SC_PASS_ENV_VARS(scp) ) == FAILED )
75 if ( ! SC_SPECIFIED( scp, A_ENV ) )
79 if ( update_env_with_strings(
80 ep->env_handle, SC_ENV_VAR_DEFS(scp) ) == FAILED )
82 env_destroy( ep->env_handle ) ;
92 * Create a new environment from environ and env_strings
93 * env_strings contains strings of the form "var=value"
95 static status_e make_env_with_strings( struct environment *ep,
100 const char *func = "make_env_with_strings" ;
102 if ( ( new_env = env_create( env ) ) == ENV_NULL )
104 out_of_memory( func ) ;
108 if ( update_env_with_strings( new_env, env_strings ) == FAILED )
110 env_destroy( new_env ) ;
114 ep->env_type = CUSTOM_ENV ;
115 ep->env_handle = new_env ;
120 static status_e make_env_from_vars( struct environment *ep,
127 const char *func = "make_env_from_vars" ;
129 if ( ( new_env = env_create( ENV_NULL ) ) == ENV_NULL )
131 out_of_memory( func ) ;
135 for ( u = 0 ; u < pset_count( vars ) ; u++ )
137 varname = (char *) pset_pointer( vars, u ) ;
138 if ( env_addvar( new_env, env, varname ) == ENV_ERR )
142 msg( LOG_ERR, func, "Unknown variable %s", varname ) ;
146 out_of_memory( func ) ;
147 env_destroy( new_env ) ;
152 ep->env_type = CUSTOM_ENV ;
153 ep->env_handle = new_env ;
158 static status_e update_env_with_strings( env_h env, pset_h strings )
161 const char *func = "update_env_with_strings" ;
163 for ( u = 0 ; u < pset_count( strings ) ; u++ )
165 char *p = (char *) pset_pointer( strings, u ) ;
167 if ( env_addstr( env, p ) == ENV_ERR )
171 out_of_memory( func ) ;
175 msg( LOG_ERR, func, "Bad environment string: %s", p ) ;