VM: full munmap
[minix.git] / lib / libsys / env_get_prm.c
blob24e577a7cdcc132e768fc016fff2444160f22559
1 #include "sysutil.h"
2 #include <string.h>
4 int env_argc = 0;
5 char **env_argv = NULL;
7 static char *find_key(const char *params, const char *key);
9 /*===========================================================================*
10 * env_setargs *
11 *===========================================================================*/
12 void env_setargs(arg_c, arg_v)
13 int arg_c;
14 char *arg_v[];
16 env_argc= arg_c;
17 env_argv= arg_v;
20 /*===========================================================================*
21 * env_get_param *
22 *===========================================================================*/
23 int env_get_param(key, value, max_len)
24 char *key; /* which key to look up */
25 char *value; /* where to store value */
26 int max_len; /* maximum length of value */
28 message m;
29 static char mon_params[MULTIBOOT_PARAM_BUF_SIZE]; /* copy parameters here */
30 char *key_value;
31 int i, s;
32 size_t keylen;
34 if (key == NULL)
35 return EINVAL;
37 keylen= strlen(key);
38 for (i= 1; i<env_argc; i++)
40 if (strncmp(env_argv[i], key, keylen) != 0)
41 continue;
42 if (strlen(env_argv[i]) <= keylen)
43 continue;
44 if (env_argv[i][keylen] != '=')
45 continue;
46 key_value= env_argv[i]+keylen+1;
47 if (strlen(key_value)+1 > (size_t) max_len)
48 return(E2BIG);
49 strcpy(value, key_value);
50 return OK;
53 /* Get copy of boot monitor parameters. */
54 m.m_type = SYS_GETINFO;
55 m.I_REQUEST = GET_MONPARAMS;
56 m.I_ENDPT = SELF;
57 m.I_VAL_LEN = sizeof(mon_params);
58 m.I_VAL_PTR = mon_params;
59 if ((s=_kernel_call(SYS_GETINFO, &m)) != OK) {
60 printf("SYS_GETINFO: %d (size %u)\n", s, sizeof(mon_params));
61 return(s);
64 /* We got a copy, now search requested key. */
65 if ((key_value = find_key(mon_params, key)) == NULL)
66 return(ESRCH);
68 /* Value found, see if it fits in the client's buffer. Callers assume that
69 * their buffer is unchanged on error, so don't make a partial copy.
71 if ((strlen(key_value)+1) > (size_t) max_len) return(E2BIG);
73 /* Make the actual copy. */
74 strcpy(value, key_value);
76 return(OK);
80 /*==========================================================================*
81 * find_key *
82 *==========================================================================*/
83 static char *find_key(params,name)
84 const char *params;
85 const char *name;
87 register const char *namep;
88 register char *envp;
90 for (envp = (char *) params; *envp != 0;) {
91 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
93 if (*namep == '\0' && *envp == '=')
94 return(envp + 1);
95 while (*envp++ != 0)
98 return(NULL);