source: trunk/minix/lib/sysutil/env_get_prm.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.5 KB
RevLine 
[9]1#include "sysutil.h"
2#include <minix/config.h>
3#include <string.h>
4
5PRIVATE int argc = 0;
6PRIVATE char **argv = NULL;
7
8FORWARD _PROTOTYPE( char *find_key, (const char *params, const char *key));
9
10/*===========================================================================*
11 * env_setargs *
12 *===========================================================================*/
13PUBLIC void env_setargs(arg_c, arg_v)
14int arg_c;
15char *arg_v[];
16{
17 argc= arg_c;
18 argv= arg_v;
19}
20
21/*===========================================================================*
22 * env_get_param *
23 *===========================================================================*/
24PUBLIC int env_get_param(key, value, max_len)
25char *key; /* which key to look up */
26char *value; /* where to store value */
27int max_len; /* maximum length of value */
28{
29 message m;
30 static char mon_params[128*sizeof(char *)]; /* copy parameters here */
31 char *key_value;
32 int i, s, keylen;
33
34 if (key == NULL)
35 return EINVAL;
36
37 keylen= strlen(key);
38 for (i= 1; i<argc; i++)
39 {
40 if (strncmp(argv[i], key, keylen) != 0)
41 continue;
42 if (strlen(argv[i]) <= keylen)
43 continue;
44 if (argv[i][keylen] != '=')
45 continue;
46 key_value= argv[i]+keylen+1;
47 if (strlen(key_value)+1 > EP_BUF_SIZE)
48 return(E2BIG);
49 strcpy(value, key_value);
50 return OK;
51 }
52
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=_taskcall(SYSTASK, SYS_GETINFO, &m)) != OK) {
60 printf("SYS_GETINFO: %d (size %u)\n", s, sizeof(mon_params));
61 return(s);
62 }
63
64 /* We got a copy, now search requested key. */
65 if ((key_value = find_key(mon_params, key)) == NULL)
66 return(ESRCH);
67
68 /* Value found, make the actual copy (as far as possible). */
69 strncpy(value, key_value, max_len);
70
71 /* See if it fits in the client's buffer. */
72 if ((strlen(key_value)+1) > max_len) return(E2BIG);
73 return(OK);
74}
75
76
77/*==========================================================================*
78 * find_key *
79 *==========================================================================*/
80PRIVATE char *find_key(params,name)
81const char *params;
82const char *name;
83{
84 register const char *namep;
85 register char *envp;
86
87 for (envp = (char *) params; *envp != 0;) {
88 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
89 ;
90 if (*namep == '\0' && *envp == '=')
91 return(envp + 1);
92 while (*envp++ != 0)
93 ;
94 }
95 return(NULL);
96}
97
Note: See TracBrowser for help on using the repository browser.