console: only color outputs if stdout isa TTY
[nit.git] / lib / core / 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 text
21
22 in "C Header" `{
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <pwd.h>
26 #include <grp.h>
27 `}
28
29 redef class Sys
30 # Set the real current user id of this process
31 fun uid=(uid: Int): Bool `{ return setuid(uid); `}
32
33 # Current real user id of this process
34 fun uid: Int `{ return getuid(); `}
35
36 # Set the current real group id of this process
37 fun gid=(gid: Int): Bool `{ return setgid(gid); `}
38
39 # Current real group id of this process
40 fun gid: Int `{ return getgid(); `}
41
42 # Set the effective user id of this process
43 fun euid=(uid: Int): Bool `{ return seteuid(uid); `}
44
45 # The effective user id of this process
46 fun euid: Int `{ return geteuid(); `}
47
48 # Set the effective group id of this process
49 fun egid=(gid: Int): Bool `{ return setegid(gid); `}
50
51 # The effective group id of this process
52 fun egid: Int `{ return getegid(); `}
53 end
54
55 # Information on a user account
56 extern class Passwd `{struct passwd*`}
57 # Get the `Passwd` of the user with the `uid`
58 new from_uid(uid: Int) `{ return getpwuid(uid); `}
59
60 # Get the `Passwd` of the user with the `name`
61 new from_name(name: String) import String.to_cstring `{ return getpwnam( String_to_cstring(name) ); `}
62
63 # Username
64 fun name: String import CString.to_s `{ return CString_to_s(self->pw_name); `}
65
66 # User password
67 fun passwd: String import CString.to_s `{ return CString_to_s(self->pw_passwd); `}
68
69 # User ID
70 fun uid: Int `{ return self->pw_uid; `}
71
72 # Group ID
73 fun gid: Int `{ return self->pw_gid; `}
74
75 # Home directory
76 fun dir: String import CString.to_s `{ return CString_to_s(self->pw_dir); `}
77
78 # Shell program
79 fun shell: String import CString.to_s `{ return CString_to_s(self->pw_shell); `}
80 end
81
82 # Information on a user group
83 extern class Group `{struct group*`}
84 # Get a group from its id
85 new from_gid(gid: Int) `{ return getgrgid(gid); `}
86
87 # Get a group from its name
88 new from_name(name: String) import String.to_cstring `{ return getgrnam( String_to_cstring(name) ); `}
89
90 # Name of this ground
91 fun name: String import CString.to_s `{ return CString_to_s(self->gr_name); `}
92
93 # Encrypted password of this group
94 fun passwd: String import CString.to_s `{ return CString_to_s(self->gr_passwd); `}
95
96 # Id of this group
97 fun gid: Int `{ return self->gr_gid; `}
98
99 # List of the members of the group
100 fun mem: Array[String] import Array[String], Array[String].add, CString.to_s `{
101 char **mem;
102 int m;
103 Array_of_String ret;
104
105 mem = self->gr_mem;
106 ret = new_Array_of_String();
107
108 for (m = 0; mem[m] != NULL; m++)
109 Array_of_String_add(ret, CString_to_s(mem[m]));
110
111 return ret;
112 `}
113 end
114
115 redef class Int
116 # Does the file descriptor `self` refer to a terminal?
117 fun isatty: Bool `{ return isatty(self); `}
118 end