PatchworkOS  10941b4
A non-POSIX operating system.
Loading...
Searching...
No Matches
env.c
Go to the documentation of this file.
1#include <kernel/proc/env.h>
2#include <kernel/sync/mutex.h>
3
4#include <errno.h>
5#include <stdlib.h>
6#include <string.h>
7
8void env_init(env_t* env)
9{
10 env->vars = NULL;
11 env->count = 0;
12 mutex_init(&env->mutex);
13}
14
15void env_deinit(env_t* env)
16{
17 mutex_deinit(&env->mutex);
18 for (size_t i = 0; i < env->count; i++)
19 {
20 free(env->vars[i].key);
21 free(env->vars[i].value);
22 }
23 free(env->vars);
24}
25
27{
28 if (dest == NULL || src == NULL)
29 {
30 errno = EINVAL;
31 return ERR;
32 }
33
34 MUTEX_SCOPE(&src->mutex);
35
36 for (size_t i = 0; i < src->count; i++)
37 {
38 if (env_set(dest, src->vars[i].key, src->vars[i].value) == ERR)
39 {
40 return ERR;
41 }
42 }
43
44 return 0;
45}
46
47const char* env_get(env_t* env, const char* key)
48{
49 if (env == NULL || key == NULL)
50 {
51 return NULL;
52 }
53
54 MUTEX_SCOPE(&env->mutex);
55
56 for (size_t i = 0; i < env->count; i++)
57 {
58 if (strcmp(env->vars[i].key, key) == 0)
59 {
60 return env->vars[i].value;
61 }
62 }
63
64 return NULL;
65}
66
67uint64_t env_set(env_t* env, const char* key, const char* value)
68{
69 if (env == NULL || key == NULL || value == NULL)
70 {
71 errno = EINVAL;
72 return ERR;
73 }
74
75 MUTEX_SCOPE(&env->mutex);
76
77 for (size_t i = 0; i < env->count; i++)
78 {
79 if (strcmp(env->vars[i].key, key) != 0)
80 {
81 continue;
82 }
83
84 char* newValue = strdup(value);
85 if (newValue == NULL)
86 {
87 errno = ENOMEM;
88 return ERR;
89 }
90 free(env->vars[i].value);
91 env->vars[i].value = newValue;
92 return 0;
93 }
94
95 char* newKey = strdup(key);
96 if (newKey == NULL)
97 {
98 errno = ENOMEM;
99 return ERR;
100 }
101
102 char* newValue = strdup(value);
103 if (newValue == NULL)
104 {
105 free(newKey);
106 errno = ENOMEM;
107 return ERR;
108 }
109
110 env_var_t* newVars = realloc(env->vars, sizeof(env_var_t) * (env->count + 1));
111 if (newVars == NULL)
112 {
113 free(newKey);
114 free(newValue);
115 errno = ENOMEM;
116 return ERR;
117 }
118 env->vars = newVars;
119
120 env->vars[env->count].key = newKey;
121 env->vars[env->count].value = newValue;
122 env->count++;
123
124 return 0;
125}
126
127uint64_t env_unset(env_t* env, const char* key)
128{
129 if (env == NULL || key == NULL)
130 {
131 errno = EINVAL;
132 return ERR;
133 }
134
135 MUTEX_SCOPE(&env->mutex);
136
137 for (size_t i = 0; i < env->count; i++)
138 {
139 if (strcmp(env->vars[i].key, key) != 0)
140 {
141 continue;
142 }
143
144 free(env->vars[i].key);
145 free(env->vars[i].value);
146
147 for (size_t j = i; j < env->count - 1; j++)
148 {
149 env->vars[j] = env->vars[j + 1];
150 }
151
152 env->count--;
153 if (env->count == 0)
154 {
155 free(env->vars);
156 env->vars = NULL;
157 }
158 else
159 {
160 env_var_t* newVars = realloc(env->vars, sizeof(env_var_t) * env->count);
161 if (newVars != NULL)
162 {
163 env->vars = newVars;
164 }
165 }
166
167 return 0;
168 }
169
170 return 0;
171}
int64_t y
Definition main.c:153
const char * env_get(env_t *env, const char *key)
Get the value of an environment variable.
Definition env.c:47
uint64_t env_set(env_t *env, const char *key, const char *value)
Set the value of an environment variable.
Definition env.c:67
void env_init(env_t *env)
Initialize the environment.
Definition env.c:8
uint64_t env_copy(env_t *dest, env_t *src)
Copy environment variables from one environment to another.
Definition env.c:26
uint64_t env_unset(env_t *env, const char *key)
Unset an environment variable.
Definition env.c:127
void env_deinit(env_t *env)
Deinitialize the environment.
Definition env.c:15
void mutex_deinit(mutex_t *mtx)
Deinitializes a mutex.
Definition mutex.c:22
void mutex_init(mutex_t *mtx)
Initializes a mutex.
Definition mutex.c:14
#define MUTEX_SCOPE(mutex)
Acquires a mutex for the reminder of the current scope.
Definition mutex.h:23
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOMEM
Out of memory.
Definition errno.h:92
#define errno
Error number variable.
Definition errno.h:27
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * realloc(void *ptr, size_t size)
Definition realloc.c:13
_PUBLIC void free(void *ptr)
Definition free.c:11
char * strdup(const char *src)
Definition strdup.c:5
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
Environment structure.
Definition env.h:33
mutex_t mutex
Definition env.h:36
env_var_t * vars
Definition env.h:34
size_t count
Definition env.h:35
Environment variable structure.
Definition env.h:21
char * value
Definition env.h:23
char * key
Definition env.h:22