Flush (oops).
[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 GetCurrentUser ()
46                 {
47                         return Syscall.getuid ();
48                 }
49
50                 public static string GetCurrentUserName ()
51                 {
52                         return GetName (GetCurrentUser());
53                 }
54
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 ()
58                 {
59                         StringBuilder buf = new StringBuilder (4);
60                         int r;
61                         do {
62                                 buf.Capacity *= 2;
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 ();
67                 }
68
69                 public static uint GetGroupId (string user)
70                 {
71                         return new UnixUserInfo (user).GroupId;
72                 }
73
74                 public static uint GetGroupId (uint user)
75                 {
76                         return new UnixUserInfo (user).GroupId;
77                 }
78
79                 public static string GetRealName (string user)
80                 {
81                         return new UnixUserInfo (user).RealName;
82                 }
83
84                 public static string GetRealName (uint user)
85                 {
86                         return new UnixUserInfo (user).RealName;
87                 }
88
89                 public static string GetHomeDirectory (string user)
90                 {
91                         return new UnixUserInfo (user).HomeDirectory;
92                 }
93
94                 public static string GetHomeDirectory (uint user)
95                 {
96                         return new UnixUserInfo (user).HomeDirectory;
97                 }
98
99                 public static string GetName (uint user)
100                 {
101                         return new UnixUserInfo (user).UserName;
102                 }
103
104                 public static string GetPassword (string user)
105                 {
106                         return new UnixUserInfo (user).Password;
107                 }
108
109                 public static string GetPassword (uint user)
110                 {
111                         return new UnixUserInfo (user).Password;
112                 }
113
114                 public static string GetShellProgram (string user)
115                 {
116                         return new UnixUserInfo (user).ShellProgram;
117                 }
118
119                 public static string GetShellProgram (uint user)
120                 {
121                         return new UnixUserInfo (user).ShellProgram;
122                 }
123
124                 public static UnixUserInfo[] GetLocalUsers ()
125                 {
126                         Syscall.SetLastError ((Error) 0);
127                         Syscall.setpwent ();
128                         if (Syscall.GetLastError () != (Error) 0) {
129                                 UnixMarshal.ThrowExceptionForLastError ();
130                         }
131                         ArrayList entries = new ArrayList ();
132                         try {
133                                 Passwd p;
134                                 while ((p = Syscall.getpwent()) != null)
135                                         entries.Add (new UnixUserInfo (p));
136                                 if (Syscall.GetLastError () != (Error) 0)
137                                         UnixMarshal.ThrowExceptionForLastError ();
138                         }
139                         finally {
140                                 Syscall.endpwent ();
141                         }
142                         return (UnixUserInfo[]) entries.ToArray (typeof(UnixUserInfo));
143                 }
144         }
145 }
146
147 // vim: noexpandtab