1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/**
* @file environment.c
*/
#include "spm.h"
/**
*
* @param env
*/
void runtime_export(char **env) {
char *borne[] = {
"bash",
"dash",
"zsh",
NULL,
};
char *unborne[] = {
"csh"
"tcsh",
NULL,
};
char output[BUFSIZ];
char export_command[7]; // export=6 and setenv=6... convenient
char *_sh = getenv("SHELL");
char *sh = basename(_sh);
if (sh == NULL) {
fprintf(stderr, "echo SHELL environment variable is not defined");
exit(1);
}
for (size_t i = 0; borne[i] != NULL; i++) {
if (strcmp(sh, borne[i]) == 0) {
strcpy(export_command, "export");
break;
}
}
for (size_t i = 0; unborne[i] != NULL; i++) {
if (strcmp(sh, unborne[i]) == 0) {
strcpy(export_command, "setenv");
break;
}
}
for (size_t i = 0; env[i] != NULL; i++) {
char **pair = split(env[i], "=");
sprintf(output, "%s %s=\"%s\"", export_command, pair[0], pair[1] ? pair[1] : "");
puts(output);
split_free(pair);
}
}
/**
*
* @param env
* @return
*/
char **runtime_copy(char **env) {
char **envp = NULL;
size_t env_count;
for (env_count = 0; env[env_count] != NULL; env_count++);
envp = (char **)calloc(env_count + 1, sizeof(char *));
for (size_t i = 0; i < env_count; i++) {
size_t len = strlen(env[i]);
envp[i] = (char *)calloc(len + 1, sizeof(char));
memcpy(envp[i], env[i], len);
}
return envp;
}
void runtime_free(char **env) {
for (size_t i = 0; env[i] != NULL; i++) {
free(env[i]);
}
free(env);
}
|