Skip to content

Commit 473cb3e

Browse files
authored
Fixed some libc.jl issues on windows. (#10)
1 parent 354be6c commit 473cb3e

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

src/libc.jl

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ elseif is_linux()
2222
pw_dir::Cstring
2323
pw_shell::Cstring
2424
end
25+
else
26+
immutable Cpasswd
27+
pw_name::Cstring
28+
pw_uid::Cint
29+
pw_gid::Cint
30+
pw_dir::Cstring
31+
pw_shell::Cstring
32+
end
33+
34+
Cpasswd() = Cpasswd(pointer("NA"), 0, 0, pointer("NA"), pointer("NA"))
2535
end
2636

2737
immutable Cgroup
@@ -30,16 +40,16 @@ immutable Cgroup
3040
gr_gid::Cint
3141
end
3242

43+
Cgroup() = Cgroup(pointer("NA"), pointer("NA"), 0)
44+
3345
immutable User
3446
name::String
3547
uid::UInt64
3648
gid::UInt64
3749
dir::String
3850
shell::String
3951

40-
function User(passwd::Ptr{Cpasswd})
41-
ps = unsafe_load(passwd)
42-
52+
function User(ps::Cpasswd)
4353
new(
4454
unsafe_string(ps.pw_name),
4555
UInt64(ps.pw_uid),
@@ -50,54 +60,71 @@ immutable User
5060
end
5161
end
5262

63+
User(passwd::Ptr{Cpasswd}) = User(unsafe_load(passwd))
64+
5365
function Base.show(io::IO, user::User)
5466
print(io, "$(user.uid) ($(user.name))")
5567
end
5668

5769
function User(name::String)
58-
ps = ccall((:getpwnam, "libc"), Ptr{Cpasswd}, (Ptr{UInt8},), name)
70+
ps = @static if is_unix()
71+
ccall((:getpwnam, "libc"), Ptr{Cpasswd}, (Ptr{UInt8},), name)
72+
else
73+
Cpasswd()
74+
end
75+
5976
User(ps)
6077
end
6178

6279
function User(uid::UInt64)
63-
ps = ccall((:getpwuid, "libc"), Ptr{Cpasswd}, (UInt64,), uid)
80+
ps = @static if is_unix()
81+
ccall((:getpwuid, "libc"), Ptr{Cpasswd}, (UInt64,), uid)
82+
else
83+
Cpasswd()
84+
end
85+
6486
User(ps)
6587
end
6688

6789
function User()
68-
uid = ccall((:geteuid, "libc"), Cint, ())
90+
uid = @static is_unix() ? ccall((:geteuid, "libc"), Cint, ()) : 0
6991
User(UInt64(uid))
7092
end
7193

7294
immutable Group
7395
name::String
7496
gid::UInt64
7597

76-
function Group(group::Ptr{Cgroup})
77-
gr = unsafe_load(group)
78-
79-
new(
80-
unsafe_string(gr.gr_name),
81-
UInt64(gr.gr_gid)
82-
)
83-
end
98+
Group(gr::Cgroup) = new(unsafe_string(gr.gr_name), UInt64(gr.gr_gid))
8499
end
85100

101+
Group(group::Ptr{Cgroup}) = Group(unsafe_load(group))
102+
86103
function Base.show(io::IO, group::Group)
87104
print(io, "$(group.gid) ($(group.name))")
88105
end
89106

90107
function Group(name::String)
91-
ps = ccall((:getgrnam, "libc"), Ptr{Cgroup}, (Ptr{UInt8},), name)
108+
ps = @static if is_unix()
109+
ccall((:getgrnam, "libc"), Ptr{Cgroup}, (Ptr{UInt8},), name)
110+
else
111+
Cgroup()
112+
end
113+
92114
Group(ps)
93115
end
94116

95117
function Group(gid::UInt64)
96-
gr = ccall((:getgrgid, "libc"), Ptr{Cgroup}, (UInt64,), gid)
118+
gr = @static if is_unix()
119+
ccall((:getgrgid, "libc"), Ptr{Cgroup}, (UInt64,), gid)
120+
else
121+
Cgroup()
122+
end
123+
97124
Group(gr)
98125
end
99126

100127
function Group()
101-
gid = ccall((:getegid, "libc"), Cint, ())
128+
gid = @static is_unix() ? ccall((:getegid, "libc"), Cint, ()) : 0
102129
Group(UInt64(gid))
103130
end

0 commit comments

Comments
 (0)