-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsys_ops.c
More file actions
47 lines (37 loc) · 1.35 KB
/
sys_ops.c
File metadata and controls
47 lines (37 loc) · 1.35 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
#include "quickjs-libc.h"
#include <sys/mount.h>
#include <unistd.h>
#include <errno.h>
#ifndef countof
#define countof(x) (sizeof(x) / sizeof((x)[0]))
#endif
// Helper for error reporting
static JSValue js_get_err(JSContext *ctx) {
return JS_NewInt32(ctx, -errno);
}
// C Implementation of mount()
static JSValue js_mount(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
const char *source = JS_ToCString(ctx, argv[0]);
const char *target = JS_ToCString(ctx, argv[1]);
const char *type = JS_ToCString(ctx, argv[2]);
// flags=0, data=NULL for a basic mount
int res = mount(source, target, type, 0, NULL);
JS_FreeCString(ctx, source);
JS_FreeCString(ctx, target);
JS_FreeCString(ctx, type);
if (res < 0) return js_get_err(ctx);
return JS_NewInt32(ctx, 0);
}
static const JSCFunctionListEntry js_sys_ops_funcs[] = {
JS_CFUNC_DEF("mount", 3, js_mount),
};
static int js_sys_ops_init(JSContext *ctx, JSModuleDef *m) {
return JS_SetModuleExportList(ctx, m, js_sys_ops_funcs, countof(js_sys_ops_funcs));
}
JSModuleDef *js_init_module_js_init_module_sys_ops(JSContext *ctx, const char *module_name) {
JSModuleDef *m;
m = JS_NewCModule(ctx, module_name, js_sys_ops_init);
if (!m) return NULL;
JS_AddModuleExportList(ctx, m, js_sys_ops_funcs, countof(js_sys_ops_funcs));
return m;
}