-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos.c
111 lines (96 loc) · 2.89 KB
/
os.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* This Source Code Form is subject to the terms of the Mozilla Public
* * License, v. 2.0. If a copy of the MPL was not distributed with this
* * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Copyright (c) 2017, Yuxuan Shui <[email protected]> */
#include <dirent.h>
#include <string.h>
#include <sys/utsname.h>
#include <deai/deai.h>
#include <deai/helper.h>
#include <deai/object.h>
#include "common.h" // IWYU pragma: keep
static struct di_variant di_env_get(struct di_module *m, di_string name_) {
struct di_variant ret = {
.type = DI_LAST_TYPE,
.value = NULL,
};
if (!name_.data) {
return ret;
}
scopedp(char) *name = di_string_to_chars_alloc(name_);
const char *str = getenv(name);
if (str) {
ret.type = DI_TYPE_STRING_LITERAL;
ret.value = malloc(sizeof(void *));
di_copy_value(ret.type, ret.value, &str);
}
return ret;
}
static void di_env_set(struct di_module *m, di_string key_, di_string val_) {
if (!key_.data || !val_.data) {
return;
}
scopedp(char) *key = di_string_to_chars_alloc(key_);
scopedp(char) *val = di_string_to_chars_alloc(val_);
setenv(key, val, 1);
}
static void di_env_unset(struct di_module *m, di_string key_) {
if (!key_.data) {
return;
}
scopedp(char) *key = di_string_to_chars_alloc(key_);
unsetenv(key);
}
static const char *di_get_hostname(di_object *p) {
struct utsname buf;
if (uname(&buf) != 0) {
return NULL;
}
return strdup(buf.nodename);
}
static di_array di_listdir(di_object *o unused, di_string path) {
int capacity = 0;
di_array ret = {.arr = NULL, .length = 0, .elem_type = DI_TYPE_STRING};
scopedp(char) *c_path = di_string_to_chars_alloc(path);
DIR *dir = opendir(c_path);
if (!dir) {
return ret;
}
struct dirent *ent = NULL;
while ((ent = readdir(dir))) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
if (capacity == ret.length) {
capacity = capacity * 2 + 1;
ret.arr = realloc(ret.arr, capacity * sizeof(di_string));
}
((di_string *)ret.arr)[ret.length] = di_string_dup(ent->d_name);
ret.length++;
}
closedir(dir);
return ret;
}
/// EXPORT: os: deai:module
///
/// OS environment
///
/// EXPORT: os.env: deai.builtin.os:Env
///
/// Environment variables
///
/// This object exposes environment variables as its members. You can get/set environment
/// variables by reading/changing its member. This will affect subsequently spawned
/// processes.
void di_init_os(di_object *di) {
struct di_module *m = di_new_module(di);
di_object *o = di_new_object_with_type(di_object);
di_set_type(o, "deai.builtin.os:Env");
di_method(o, "__get", di_env_get, di_string);
di_method(o, "__set", di_env_set, di_string, di_string);
di_method(o, "__delete", di_env_unset, di_string);
di_member(m, "env", o);
di_getter(m, hostname, di_get_hostname);
di_method(m, "listdir", di_listdir, di_string);
di_register_module(di, di_string_borrow_literal("os"), &m);
}