-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.vek
More file actions
74 lines (66 loc) · 2.08 KB
/
Copy pathenv.vek
File metadata and controls
74 lines (66 loc) · 2.08 KB
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
native fn __rt_env_has(name: cstr) -> bool;
native fn __rt_env_get_or_empty(name: cstr) -> cstr;
native fn __rt_env_set(name: cstr, value: cstr) -> void;
native fn __rt_env_unset(name: cstr) -> void;
native fn __rt_env_cwd() -> string;
native fn __rt_env_argc() -> usize;
native fn __rt_env_arg(index: usize) -> cstr;
native fn __rt_env_count() -> usize;
native fn __rt_env_pair(index: usize) -> cstr;
native fn __rt_env_chdir(path: cstr) -> i32;
native fn __rt_env_strerror(code: i32) -> cstr;
pub fn args() -> string[] {
let mut result: string[] = [];
let count: usize = __rt_env_argc() ;
let mut i: usize = 0;
while i < count {
result.push(string.from_cstr(__rt_env_arg(i)));
i += 1;
}
return result;
}
pub fn vars() -> (string, string)[] {
let mut result: (string, string)[] = [];
let count: usize = __rt_env_count();
let mut i: usize = 0;
while i < count {
// Each entry is "KEY=VALUE"; split on the first '='. A malformed entry with
// no '=' (rare, but permitted by POSIX) is a bare key with an empty value.
let entry: string = string.from_cstr(__rt_env_pair(i)) ;
let eq: isize? = entry.find("=");
if eq == null {
result.push((entry, ""));
} else {
let at: isize = eq;
result.push((entry.slice(0, at), entry.slice(at + 1, entry.length())));
}
i += 1;
}
return result;
}
pub fn var(name: string) -> string? {
let n: cstr = name.to_cstr();
if __rt_env_has(n) {
string.from_cstr(__rt_env_get_or_empty(n))
} else {
null
}
}
pub inline fn set_var(name: string, value: string) -> void {
__rt_env_set(name.to_cstr(), value.to_cstr());
}
pub inline fn remove_var(name: string) -> void {
__rt_env_unset(name.to_cstr());
}
pub inline fn current_dir() -> string {
__rt_env_cwd()
}
pub inline fn set_current_dir(path: string) -> Result<void, string> {
let code: i32 = __rt_env_chdir(path.to_cstr()) ;
if code == 0 {
return Ok();
}
return Err(string.from_cstr(__rt_env_strerror(code)));
}
// `exit` and `abort` moved to std:process — they act on the process, not on the
// environment it was given.