Skip to content

Commit 9d85a5f

Browse files
committed
More libc cleanup.
1 parent 69c8f94 commit 9d85a5f

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

src/libc.jl

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,36 @@ function User(ps::Cpasswd)
6161
end
6262

6363
User(passwd::Ptr{Cpasswd}) = User(unsafe_load(passwd))
64+
Base.show(io::IO, user::User) = print(io, "$(user.uid) ($(user.name))")
6465

65-
function Base.show(io::IO, user::User)
66-
print(io, "$(user.uid) ($(user.name))")
67-
end
66+
@static if Sys.isunix()
67+
function User(name::String)
68+
Libc.errno(0)
69+
ps = ccall(:getpwnam, Ptr{Cpasswd}, (Ptr{UInt8},), name)
70+
ret = Libc.errno()
71+
72+
systemerror(:getpwnam, !iszero(ret))
73+
ps == C_NULL && throw(ArgumentError("User $name not found."))
6874

69-
function User(name::String)
70-
ps = @static if Sys.isunix()
71-
ccall(:getpwnam, Ptr{Cpasswd}, (Ptr{UInt8},), name)
72-
else
73-
Cpasswd()
75+
return User(ps)
7476
end
7577

76-
systemerror(:getpwnam, ps == C_NULL)
77-
User(ps)
78-
end
78+
function User(uid::UInt)
79+
Libc.errno(0)
80+
ps = ccall(:getpwuid, Ptr{Cpasswd}, (UInt64,), uid)
81+
ret = Libc.errno()
7982

80-
function User(uid::UInt)
81-
ps = @static if Sys.isunix()
82-
ccall(:getpwuid, Ptr{Cpasswd}, (UInt64,), uid)
83-
else
84-
Cpasswd()
85-
end
83+
systemerror(:getpwuid, !iszero(ret))
84+
ps == C_NULL && throw(ArgumentError("User $uid not found."))
8685

87-
systemerror(:getpwuid, ps == C_NULL)
88-
User(ps)
89-
end
86+
return User(ps)
87+
end
9088

91-
function User()
92-
uid = @static Sys.isunix() ? ccall(:geteuid, Cint, ()) : 0
93-
User(UInt64(uid))
89+
User() = User(UInt64(ccall(:geteuid, Cint, ())))
90+
else
91+
User(name::String) = User(Cpasswd())
92+
User(uid::UInt) = User(Cpasswd())
93+
User() = User(UInt64(0))
9494
end
9595

9696
struct Group
@@ -100,34 +100,32 @@ end
100100

101101
Group(gr::Cgroup) = Group(unsafe_string(gr.gr_name), UInt64(gr.gr_gid))
102102
Group(group::Ptr{Cgroup}) = Group(unsafe_load(group))
103+
Base.show(io::IO, group::Group) = print(io, "$(group.gid) ($(group.name))")
103104

104-
function Base.show(io::IO, group::Group)
105-
print(io, "$(group.gid) ($(group.name))")
106-
end
105+
@static if Sys.isunix()
106+
function Group(name::String)
107+
Libc.errno(0)
108+
gr = ccall(:getgrnam, Ptr{Cgroup}, (Ptr{UInt8},), name)
109+
ret = Libc.errno()
107110

108-
function Group(name::String)
109-
ps = @static if Sys.isunix()
110-
ccall(:getgrnam, Ptr{Cgroup}, (Ptr{UInt8},), name)
111-
else
112-
Cgroup()
111+
systemerror(:getgrnam, !iszero(ret))
112+
gr == C_NULL && throw(ArgumentError("Group $name not found."))
113+
return Group(gr)
113114
end
114115

115-
systemerror(:getgrnam, ps == C_NULL)
116-
Group(ps)
117-
end
116+
function Group(gid::UInt)
117+
Libc.errno(0)
118+
gr = ccall(:getgrgid, Ptr{Cgroup}, (UInt64,), gid)
119+
ret = Libc.errno()
118120

119-
function Group(gid::UInt)
120-
gr = @static if Sys.isunix()
121-
ccall(:getgrgid, Ptr{Cgroup}, (UInt64,), gid)
122-
else
123-
Cgroup()
121+
systemerror(:getgrgid, !iszero(ret))
122+
gr == C_NULL && throw(ArgumentError("Group $gid not found."))
123+
return Group(gr)
124124
end
125125

126-
systemerror(:getgrgid, gr == C_NULL)
127-
Group(gr)
128-
end
129-
130-
function Group()
131-
gid = @static Sys.isunix() ? ccall(:getegid, Cint, ()) : 0
132-
Group(UInt64(gid))
126+
Group() = Group(UInt64(ccall(:getegid, Cint, ())))
127+
else
128+
Group(name::String) = Group(Cgroup())
129+
Group(uid::UInt) = Group(Cgroup())
130+
Group() = Group(UInt64(0))
133131
end

0 commit comments

Comments
 (0)