Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/other.zig
1067 views
1
const c = @import("c.zig");
2
const node = @import("node.zig");
3
const std = @import("std");
4
const builtin = @import("builtin");
5
const util = @import("util.zig");
6
7
pub fn register(env: c.napi_env, exports: c.napi_value) !void {
8
if (builtin.target.os.tag == .linux) {
9
try node.registerFunction(env, exports, "login_tty", login_tty);
10
}
11
try node.registerFunction(env, exports, "_statvfs", statvfs_impl);
12
try node.registerFunction(env, exports, "_fstatvfs", fstatvfs);
13
try node.registerFunction(env, exports, "ctermid", ctermid);
14
}
15
16
// int login_tty(int fd);
17
fn login_tty(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
18
if (builtin.target.os.tag == .linux) {
19
const utmp = @cImport(@cInclude("utmp.h"));
20
const argv = node.getArgv(env, info, 1) catch return null;
21
const fd = node.i32FromValue(env, argv[0], "fd") catch return null;
22
if (utmp.login_tty(fd) == -1) {
23
node.throwErrno(env, "error in login_tty");
24
return null;
25
}
26
return null;
27
} else {
28
node.throwError(env, "login_tty not supported on this platform");
29
}
30
}
31
32
const statvfs = @cImport({
33
@cDefine("struct_statvfs", "struct statvfs");
34
@cInclude("sys/statvfs.h");
35
});
36
// int statvfs(const char *restrict path, struct statvfs *restrict buf);
37
38
fn statvfs_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
39
const argv = node.getArgv(env, info, 1) catch return null;
40
var path: [1024]u8 = undefined;
41
node.stringFromValue(env, argv[0], "path", 1024, &path) catch return null;
42
var buf: statvfs.struct_statvfs = undefined;
43
if (statvfs.statvfs(&path, &buf) == -1) {
44
node.throwErrno(env, "statsvfs failed -- invalid input");
45
return null;
46
}
47
const s = util.structToNullTerminatedJsonString(statvfs.struct_statvfs, buf) catch {
48
node.throwError(env, "statsvfs failed -- problem converting output");
49
return null;
50
};
51
defer std.c.free(s);
52
return node.createStringFromPtr(env, s, "statsvfs") catch return null;
53
}
54
55
// int fstatvfs(int fd, struct statvfs *buf);
56
fn fstatvfs(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
57
const argv = node.getArgv(env, info, 1) catch return null;
58
const fd = node.i32FromValue(env, argv[0], "fd") catch return null;
59
var buf: statvfs.struct_statvfs = undefined;
60
if (statvfs.fstatvfs(fd, &buf) == -1) {
61
node.throwErrno(env, "fstatsvfs failed -- invalid input");
62
return null;
63
}
64
const s = util.structToNullTerminatedJsonString(statvfs.struct_statvfs, buf) catch {
65
node.throwError(env, "fstatsvfs failed -- problem converting output");
66
return null;
67
};
68
defer std.c.free(s);
69
return node.createStringFromPtr(env, s, "fstatsvfs") catch return null;
70
}
71
72
// stdio.h
73
// char *ctermid(char *s);
74
const stdio = @cImport(@cInclude("stdio.h"));
75
fn ctermid(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
76
_ = info;
77
const s = stdio.ctermid(null) orelse {
78
node.throwError(env, "failed to get ctermid");
79
return null;
80
};
81
return node.createStringFromPtr(env, s, "ctermid") catch return null;
82
}
83
84
85