-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
libc.j
39 lines (30 loc) · 954 Bytes
/
libc.j
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
# libc = dlopen("libc")
## time-related functions ##
sleep(s::Real) = ccall(dlsym(libc, :usleep), Uint32, (Uint32,), uint32(round(s*1e6)))
unixtime() = ccall(dlsym(libc, :time), Uint32, (Ptr{Uint32},), C_NULL)
## process-related functions ##
system(cmd::String) =
ccall(dlsym(libc, :system), Int32, (Ptr{Uint8},), cstring(cmd))
## network functions ##
function gethostname()
hn = Array(Uint8, 128)
ccall(dlsym(libc,:gethostname), Int32, (Ptr{Uint8}, Ulong),
hn, ulong(length(hn)))
string(convert(Ptr{Uint8},hn))
end
## file and directory ##
function getcwd()
b = Array(Uint8,1024)
p = ccall(dlsym(libc, :getcwd), Ptr{Uint8}, (Ptr{Uint8}, Ulong),
b, ulong(length(b)))
if p == C_NULL
error("path too long")
end
string(p)
end
function setcwd(p::String)
if ccall(dlsym(libc, :chdir), Int32, (Ptr{Uint8},), p) == -1
throw(SystemError("setcwd"))
end
getcwd()
end