1
2 #include "lib.h"
3 #include "bootenv.h"
4
5 #define MAXENVVARS 64
6 #define MAXENVLEN 64
7 static char *initenv[MAXENVVARS] = {
8 "boot=",
9 "console=3",
10 "haltaction=h",
11 "testaction=q",
12 "systype=0x82020101",
13 NULL
14 };
15
16 static char bootenv[MAXENVVARS][MAXENVLEN];
17
18 void
initbootenv(void)19 initbootenv (void)
20 {
21 char **p = initenv;
22 int i = 0;
23 while (*p != NULL)
24 {
25 strcpy (bootenv[i], *p);
26 p++;
27 i++;
28 }
29 for (; i < MAXENVVARS; ++i)
30 {
31 bootenv[i][0] = '\0';
32 }
33 }
34
35 void
printenv(void)36 printenv (void)
37 {
38 int i;
39 for (i = 0; i < MAXENVVARS; ++i)
40 {
41 if (bootenv[i][0] == '\0')
42 {
43 return;
44 }
45 puts (bootenv[i]);
46 }
47 }
48
49 char *
getenv(const char * name)50 getenv (const char *name)
51 {
52 int namelen = strlen (name);
53 int i;
54 for (i = 0; i < MAXENVVARS; ++i)
55 {
56 char *s = bootenv[i];
57 if ((strncmp (s, name, namelen) == 0) && (s[namelen] == '='))
58 {
59 return &s[namelen + 1];
60 }
61 }
62 return NULL;
63 }
64
65 void
setenv(const char * name,const char * value)66 setenv (const char *name, const char *value)
67 {
68 int namelen = strlen (name);
69 int valuelen = strlen (value);
70 int i;
71 if (namelen + valuelen + 1 > MAXENVLEN)
72 {
73 puts ("Variable/value too long.");
74 return;
75 }
76 for (i = 0; i < MAXENVVARS; ++i)
77 {
78 char *s = bootenv[i];
79 if (s[0] == '\0')
80 {
81 break;
82 }
83 if ((strncmp (s, name, namelen) == 0) && (s[namelen] == '='))
84 {
85 strncpy (&s[namelen + 1], value, MAXENVLEN - namelen - 1);
86 return;
87 }
88 }
89 /* if we got here, it's a new variable; put it at position i
90 * if i is not greater than MAXENVVARS-1
91 */
92 if (i < MAXENVVARS)
93 {
94 char *s = bootenv[i];
95 strncpy (s, name, MAXENVLEN - 1);
96 s[namelen] = '=';
97 strncpy (&s[namelen + 1], value, MAXENVLEN - namelen - 1);
98 }
99 else
100 {
101 puts ("Out of environment space.");
102 }
103 }
104
105 void
unsetenv(const char * name)106 unsetenv (const char *name)
107 {
108 int namelen = strlen (name);
109 int i, j;
110 for (i = 0; i < MAXENVVARS; ++i)
111 {
112 char *s = bootenv[i];
113 if ((strncmp (s, name, namelen) == 0) && (s[namelen] == '='))
114 {
115 /* move the rest of the environment up */
116 for (j = i + 1; j < MAXENVVARS; ++j)
117 {
118 strcpy (bootenv[j - 1], bootenv[j]);
119 if (bootenv[j][0] == '\0')
120 {
121 return;
122 }
123 }
124 }
125 }
126 puts ("Variable not found.");
127 return;
128 }
129