lib/sorter: rename Sorter to Comparator
[nit.git] / lib / standard / posix.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Services conforming to POSIX
18 module posix
19
20 import string
21
22 `{
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <pwd.h>
26 #include <grp.h>
27 `}
28
29 redef class Sys
30 fun uid=(uid: Int): Bool `{ return setuid(uid); `}
31 fun uid: Int `{ return getuid(); `}
32
33 fun gid=(gid: Int): Bool `{ return setgid(gid); `}
34 fun gid: Int `{ return getgid(); `}
35
36 fun euid=(uid: Int): Bool `{ return seteuid(uid); `}
37 fun euid: Int `{ return geteuid(); `}
38
39 fun egid=(gid: Int): Bool `{ return setegid(gid); `}
40 fun egid: Int `{ return getegid(); `}
41 end
42
43 extern class Passwd `{struct passwd*`}
44 # Get the `Passwd` of the user with the `uid`
45 new from_uid(uid: Int) `{ return getpwuid(uid); `}
46
47 # Get the `Passwd` of the user with the `name`
48 new from_name(name: String) import String.to_cstring `{ return getpwnam( String_to_cstring(name) ); `}
49
50 # Username
51 fun name: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_name); `}
52
53 # User password
54 fun passwd: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_passwd); `}
55
56 # User ID
57 fun uid: Int `{ return recv->pw_uid; `}
58
59 # Group ID
60 fun gid: Int `{ return recv->pw_gid; `}
61
62 # Home directory
63 fun dir: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_dir); `}
64
65 # Shell program
66 fun shell: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_shell); `}
67 end
68
69 extern class Group `{struct group*`}
70 new from_gid(gid: Int) `{ return getgrgid(gid); `}
71 new from_name(name: String) import String.to_cstring `{ return getgrnam( String_to_cstring(name) ); `}
72
73 fun name: String import NativeString.to_s `{ return NativeString_to_s(recv->gr_name); `}
74 fun passwd: String import NativeString.to_s `{ return NativeString_to_s(recv->gr_passwd); `}
75 fun gid: Int `{ return recv->gr_gid; `}
76 fun mem: Array[String] import Array[String], Array[String].add, NativeString.to_s `{
77 char **mem;
78 int m;
79 Array_of_String ret;
80
81 mem = recv->gr_mem;
82 ret = new_Array_of_String();
83
84 for (m = 0; mem[m] != NULL; m++)
85 Array_of_String_add(ret, NativeString_to_s(mem[m]));
86
87 return ret;
88 `}
89 end