Doh!
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixUser.cs
1 //
2 // Mono.Unix/UnixUser.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2004 Jonathan Pryor
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Text;
32 using Mono.Unix;
33
34 namespace Mono.Unix {
35
36         public sealed class UnixUser
37         {
38                 private UnixUser () {}
39
40                 public static uint GetUserId (string user)
41                 {
42                         return new UnixUserInfo (user).UserId;
43                 }
44
45                 public static uint GetCurrentUserId ()
46                 {
47                         return Syscall.getuid ();
48                 }
49
50                 public static string GetCurrentUserName ()
51                 {
52                         return GetName (GetCurrentUserId ());
53                 }
54
55                 public static UnixUserInfo GetCurrentUser ()
56                 {
57                         return new UnixUserInfo (GetCurrentUserId ());
58                 }
59
60                 // I would hope that this is the same as GetCurrentUserName, but it is a
61                 // different syscall, so who knows.
62                 public static string GetLogin ()
63                 {
64                         StringBuilder buf = new StringBuilder (4);
65                         int r;
66                         do {
67                                 buf.Capacity *= 2;
68                                 r = Syscall.getlogin_r (buf, (ulong) buf.Capacity);
69                         } while (r == (-1) && Syscall.GetLastError() == Error.ERANGE);
70                         UnixMarshal.ThrowExceptionForLastErrorIf (r);
71                         return buf.ToString ();
72                 }
73
74                 public static uint GetGroupId (string user)
75                 {
76                         return new UnixUserInfo (user).GroupId;
77                 }
78
79                 public static uint GetGroupId (uint user)
80                 {
81                         return new UnixUserInfo (user).GroupId;
82                 }
83
84                 public static string GetRealName (string user)
85                 {
86                         return new UnixUserInfo (user).RealName;
87                 }
88
89                 public static string GetRealName (uint user)
90                 {
91                         return new UnixUserInfo (user).RealName;
92                 }
93
94                 public static string GetHomeDirectory (string user)
95                 {
96                         return new UnixUserInfo (user).HomeDirectory;
97                 }
98
99                 public static string GetHomeDirectory (uint user)
100                 {
101                         return new UnixUserInfo (user).HomeDirectory;
102                 }
103
104                 [Obsolete ("Use GetUserName")]
105                 public static string GetName (uint user)
106                 {
107                         return new UnixUserInfo (user).UserName;
108                 }
109
110                 public static string GetUserName (uint user)
111                 {
112                         return new UnixUserInfo (user).UserName;
113                 }
114
115                 public static string GetPassword (string user)
116                 {
117                         return new UnixUserInfo (user).Password;
118                 }
119
120                 public static string GetPassword (uint user)
121                 {
122                         return new UnixUserInfo (user).Password;
123                 }
124
125                 public static string GetShellProgram (string user)
126                 {
127                         return new UnixUserInfo (user).ShellProgram;
128                 }
129
130                 public static string GetShellProgram (uint user)
131                 {
132                         return new UnixUserInfo (user).ShellProgram;
133                 }
134
135                 public static UnixUserInfo[] GetLocalUsers ()
136                 {
137                         ArrayList entries = new ArrayList ();
138                         Syscall.SetLastError ((Error) 0);
139                         lock (Syscall.pwd_lock) {
140                                 Syscall.setpwent ();
141                                 if (Syscall.GetLastError () != (Error) 0) {
142                                         UnixMarshal.ThrowExceptionForLastError ();
143                                 }
144                                 try {
145                                         Passwd p;
146                                         while ((p = Syscall.getpwent()) != null)
147                                                 entries.Add (new UnixUserInfo (p));
148                                         if (Syscall.GetLastError () != (Error) 0)
149                                                 UnixMarshal.ThrowExceptionForLastError ();
150                                 }
151                                 finally {
152                                         Syscall.endpwent ();
153                                 }
154                         }
155                         return (UnixUserInfo[]) entries.ToArray (typeof(UnixUserInfo));
156                 }
157         }
158 }
159
160 // vim: noexpandtab