stdlib/strings: Moved Substring to Buffer since it was in no way similar to the Strin...
[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 new from_uid(uid: Int) `{ return getpwuid(uid); `}
45 new from_name(name: String) import String.to_cstring `{ return getpwnam( String_to_cstring(name) ); `}
46
47 fun name: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_name); `}
48 fun passwd: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_passwd); `}
49 fun uid: Int `{ return recv->pw_uid; `}
50 fun gid: Int `{ return recv->pw_gid; `}
51 fun gecos: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_gecos); `}
52 fun dir: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_dir); `}
53 fun shell: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_shell); `}
54 end
55
56 extern class Group `{struct group*`}
57 new from_gid(gid: Int) `{ return getgrgid(gid); `}
58 new from_name(name: String) import String.to_cstring `{ return getgrnam( String_to_cstring(name) ); `}
59
60 fun name: String import NativeString.to_s `{ return NativeString_to_s(recv->gr_name); `}
61 fun passwd: String import NativeString.to_s `{ return NativeString_to_s(recv->gr_passwd); `}
62 fun gid: Int `{ return recv->gr_gid; `}
63 fun mem: Array[String] import Array[String], Array[String].add, NativeString.to_s `{
64 char **mem;
65 int m;
66 Array_of_String ret;
67
68 mem = recv->gr_mem;
69 ret = new_Array_of_String();
70
71 for (m = 0; mem[m] != NULL; m++)
72 Array_of_String_add(ret, NativeString_to_s(mem[m]));
73
74 return ret;
75 `}
76 end