posix: move `posix` out of `core` as its own package
[nit.git] / lib / posix / posix.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Services conforming to POSIX
16 module posix
17
18 in "C Header" `{
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <pwd.h>
22 #include <grp.h>
23 `}
24
25 redef class Sys
26 # Set the real current user id of this process
27 fun uid=(uid: Int): Bool `{ return setuid(uid); `}
28
29 # Current real user id of this process
30 fun uid: Int `{ return getuid(); `}
31
32 # Set the current real group id of this process
33 fun gid=(gid: Int): Bool `{ return setgid(gid); `}
34
35 # Current real group id of this process
36 fun gid: Int `{ return getgid(); `}
37
38 # Set the effective user id of this process
39 fun euid=(uid: Int): Bool `{ return seteuid(uid); `}
40
41 # The effective user id of this process
42 fun euid: Int `{ return geteuid(); `}
43
44 # Set the effective group id of this process
45 fun egid=(gid: Int): Bool `{ return setegid(gid); `}
46
47 # The effective group id of this process
48 fun egid: Int `{ return getegid(); `}
49 end
50
51 # Information on a user account
52 extern class Passwd `{struct passwd*`}
53 # Get the `Passwd` of the user with the `uid`
54 new from_uid(uid: Int) `{ return getpwuid(uid); `}
55
56 # Get the `Passwd` of the user with the `name`
57 new from_name(name: String) import String.to_cstring `{ return getpwnam( String_to_cstring(name) ); `}
58
59 # Username
60 fun name: String import CString.to_s `{ return CString_to_s(self->pw_name); `}
61
62 # User password
63 fun passwd: String import CString.to_s `{ return CString_to_s(self->pw_passwd); `}
64
65 # User ID
66 fun uid: Int `{ return self->pw_uid; `}
67
68 # Group ID
69 fun gid: Int `{ return self->pw_gid; `}
70
71 # Home directory
72 fun dir: String import CString.to_s `{ return CString_to_s(self->pw_dir); `}
73
74 # Shell program
75 fun shell: String import CString.to_s `{ return CString_to_s(self->pw_shell); `}
76 end
77
78 # Information on a user group
79 extern class Group `{struct group*`}
80 # Get a group from its id
81 new from_gid(gid: Int) `{ return getgrgid(gid); `}
82
83 # Get a group from its name
84 new from_name(name: String) import String.to_cstring `{ return getgrnam( String_to_cstring(name) ); `}
85
86 # Name of this ground
87 fun name: String import CString.to_s `{ return CString_to_s(self->gr_name); `}
88
89 # Encrypted password of this group
90 fun passwd: String import CString.to_s `{ return CString_to_s(self->gr_passwd); `}
91
92 # Id of this group
93 fun gid: Int `{ return self->gr_gid; `}
94
95 # List of the members of the group
96 fun mem: Array[String] import Array[String], Array[String].add, CString.to_s `{
97 char **mem;
98 int m;
99 Array_of_String ret;
100
101 mem = self->gr_mem;
102 ret = new_Array_of_String();
103
104 for (m = 0; mem[m] != NULL; m++)
105 Array_of_String_add(ret, CString_to_s(mem[m]));
106
107 return ret;
108 `}
109 end