2 // Mono.Unix/UnixUser.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004 Jonathan Pryor
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
36 public sealed class UnixUser
38 private UnixUser () {}
40 public static uint GetUserId (string user)
42 return new UnixUserInfo (user).UserId;
45 public static uint GetCurrentUser ()
47 return Syscall.getuid ();
50 public static string GetCurrentUserName ()
52 return GetName (GetCurrentUser());
55 // I would hope that this is the same as GetCurrentUserName, but it is a
56 // different syscall, so who knows.
57 public static string GetLogin ()
59 StringBuilder buf = new StringBuilder (4);
63 r = Syscall.getlogin_r (buf, (ulong) buf.Capacity);
64 } while (r == (-1) && Syscall.GetLastError() == Error.ERANGE);
65 UnixMarshal.ThrowExceptionForLastErrorIf (r);
66 return buf.ToString ();
69 public static uint GetGroupId (string user)
71 return new UnixUserInfo (user).GroupId;
74 public static uint GetGroupId (uint user)
76 return new UnixUserInfo (user).GroupId;
79 public static string GetRealName (string user)
81 return new UnixUserInfo (user).RealName;
84 public static string GetRealName (uint user)
86 return new UnixUserInfo (user).RealName;
89 public static string GetHomeDirectory (string user)
91 return new UnixUserInfo (user).HomeDirectory;
94 public static string GetHomeDirectory (uint user)
96 return new UnixUserInfo (user).HomeDirectory;
99 public static string GetName (uint user)
101 return new UnixUserInfo (user).UserName;
104 public static string GetPassword (string user)
106 return new UnixUserInfo (user).Password;
109 public static string GetPassword (uint user)
111 return new UnixUserInfo (user).Password;
114 public static string GetShellProgram (string user)
116 return new UnixUserInfo (user).ShellProgram;
119 public static string GetShellProgram (uint user)
121 return new UnixUserInfo (user).ShellProgram;
124 public static UnixUserInfo[] GetLocalUsers ()
126 Syscall.SetLastError ((Error) 0);
128 if (Syscall.GetLastError () != (Error) 0) {
129 UnixMarshal.ThrowExceptionForLastError ();
131 ArrayList entries = new ArrayList ();
134 while ((p = Syscall.getpwent()) != null)
135 entries.Add (new UnixUserInfo (p));
136 if (Syscall.GetLastError () != (Error) 0)
137 UnixMarshal.ThrowExceptionForLastError ();
142 return (UnixUserInfo[]) entries.ToArray (typeof(UnixUserInfo));