xref: /minix3/minix/lib/libsys/env_get_prm.c (revision 7c48de6cc4c6d56f2277d378dba01dbac8a8c3b9)
1433d6423SLionel Sambuc #include "sysutil.h"
2433d6423SLionel Sambuc #include <string.h>
3433d6423SLionel Sambuc 
4433d6423SLionel Sambuc int env_argc = 0;
5433d6423SLionel Sambuc char **env_argv = NULL;
6433d6423SLionel Sambuc 
7*7c48de6cSDavid van Moolenbroek static const char *find_key(const char *params, const char *key);
8433d6423SLionel Sambuc 
9433d6423SLionel Sambuc /*===========================================================================*
10433d6423SLionel Sambuc  *				env_setargs				     *
11433d6423SLionel Sambuc  *===========================================================================*/
env_setargs(int arg_c,char * arg_v[])12433d6423SLionel Sambuc void env_setargs(int arg_c, char *arg_v[])
13433d6423SLionel Sambuc {
14433d6423SLionel Sambuc 	env_argc= arg_c;
15433d6423SLionel Sambuc 	env_argv= arg_v;
16433d6423SLionel Sambuc }
17433d6423SLionel Sambuc 
18433d6423SLionel Sambuc /*===========================================================================*
19433d6423SLionel Sambuc  *				env_get_param				     *
20433d6423SLionel Sambuc  *===========================================================================*/
env_get_param(const char * key,char * value,int max_len)21433d6423SLionel Sambuc int env_get_param(const char *key, char *value, int max_len)
22433d6423SLionel Sambuc {
23433d6423SLionel Sambuc   message m;
24433d6423SLionel Sambuc   static char mon_params[MULTIBOOT_PARAM_BUF_SIZE]; /* copy parameters here */
25*7c48de6cSDavid van Moolenbroek   const char *key_value;
26433d6423SLionel Sambuc   int i, s;
27433d6423SLionel Sambuc   size_t keylen;
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc   if (key == NULL)
30433d6423SLionel Sambuc   	return EINVAL;
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc   keylen= strlen(key);
33433d6423SLionel Sambuc   for (i= 1; i<env_argc; i++)
34433d6423SLionel Sambuc   {
35433d6423SLionel Sambuc   	if (strncmp(env_argv[i], key, keylen) != 0)
36433d6423SLionel Sambuc   		continue;
37433d6423SLionel Sambuc 	if (strlen(env_argv[i]) <= keylen)
38433d6423SLionel Sambuc 		continue;
39433d6423SLionel Sambuc 	if (env_argv[i][keylen] != '=')
40433d6423SLionel Sambuc 		continue;
41433d6423SLionel Sambuc 	key_value= env_argv[i]+keylen+1;
42433d6423SLionel Sambuc 	if (strlen(key_value)+1 > (size_t) max_len)
43433d6423SLionel Sambuc 	      return(E2BIG);
44433d6423SLionel Sambuc 	strcpy(value, key_value);
45433d6423SLionel Sambuc 	return OK;
46433d6423SLionel Sambuc   }
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc   /* Get copy of boot monitor parameters. */
49433d6423SLionel Sambuc   m.m_type = SYS_GETINFO;
50433d6423SLionel Sambuc   m.m_lsys_krn_sys_getinfo.request = GET_MONPARAMS;
51433d6423SLionel Sambuc   m.m_lsys_krn_sys_getinfo.endpt = SELF;
52433d6423SLionel Sambuc   m.m_lsys_krn_sys_getinfo.val_len = sizeof(mon_params);
53685aa793SDavid van Moolenbroek   m.m_lsys_krn_sys_getinfo.val_ptr = (vir_bytes)mon_params;
54433d6423SLionel Sambuc   if ((s=_kernel_call(SYS_GETINFO, &m)) != OK) {
55d91890d2SEmmanuel Blot 	printf("SYS_GETINFO: %d (size %zu)\n", s, sizeof(mon_params));
56433d6423SLionel Sambuc 	return(s);
57433d6423SLionel Sambuc   }
58433d6423SLionel Sambuc 
59433d6423SLionel Sambuc   /* We got a copy, now search requested key. */
60433d6423SLionel Sambuc   if ((key_value = find_key(mon_params, key)) == NULL)
61433d6423SLionel Sambuc 	return(ESRCH);
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc   /* Value found, see if it fits in the client's buffer. Callers assume that
64433d6423SLionel Sambuc    * their buffer is unchanged on error, so don't make a partial copy.
65433d6423SLionel Sambuc    */
66433d6423SLionel Sambuc   if ((strlen(key_value)+1) > (size_t) max_len) return(E2BIG);
67433d6423SLionel Sambuc 
68433d6423SLionel Sambuc   /* Make the actual copy. */
69433d6423SLionel Sambuc   strcpy(value, key_value);
70433d6423SLionel Sambuc 
71433d6423SLionel Sambuc   return(OK);
72433d6423SLionel Sambuc }
73433d6423SLionel Sambuc 
74433d6423SLionel Sambuc 
75433d6423SLionel Sambuc /*==========================================================================*
76433d6423SLionel Sambuc  *				find_key				    *
77433d6423SLionel Sambuc  *==========================================================================*/
find_key(const char * params,const char * name)78*7c48de6cSDavid van Moolenbroek static const char *find_key(const char *params, const char *name)
79433d6423SLionel Sambuc {
80*7c48de6cSDavid van Moolenbroek   const char *namep, *envp;
81433d6423SLionel Sambuc 
82*7c48de6cSDavid van Moolenbroek   for (envp = params; *envp != 0;) {
83433d6423SLionel Sambuc 	for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
84433d6423SLionel Sambuc 		;
85433d6423SLionel Sambuc 	if (*namep == '\0' && *envp == '=')
86433d6423SLionel Sambuc 		return(envp + 1);
87433d6423SLionel Sambuc 	while (*envp++ != 0)
88433d6423SLionel Sambuc 		;
89433d6423SLionel Sambuc   }
90433d6423SLionel Sambuc   return(NULL);
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc 
93