forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrp.cs
More file actions
146 lines (111 loc) · 4.86 KB
/
grp.cs
File metadata and controls
146 lines (111 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#if FEATURE_NATIVE
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using System.Numerics;
[assembly: PythonModule("grp", typeof(IronPython.Modules.PythonGrp), PlatformsAttribute.PlatformFamily.Unix)]
namespace IronPython.Modules {
public static class PythonGrp {
public const string __doc__ = @"Access to the Unix group database.
Group entries are reported as 4-tuples containing the following fields
from the group database, in order:
gr_name - name of the group
gr_passwd - group password (encrypted); often empty
gr_gid - numeric ID of the group
gr_mem - list of members
The gid is an integer, name and password are strings. (Note that most
users are not explicitly listed as members of the groups they are in
according to the password database. Check both databases to get
complete membership information.)";
[StructLayout(LayoutKind.Sequential)]
private struct group {
[MarshalAs(UnmanagedType.LPStr)]
public string gr_name;
[MarshalAs(UnmanagedType.LPStr)]
public string gr_passwd;
public int gr_gid;
public IntPtr gr_mem;
};
[PythonType("struct_group")]
[Documentation(@"grp.struct_group: Results from getgr*() routines.
This object may be accessed either as a tuple of
(gr_name,gr_passwd,gr_gid,gr_mem)
or via the object attributes as named in the above tuple.
")]
public class struct_group : PythonTuple {
internal struct_group(string gr_name, string gr_passwd, int gr_gid, PythonList gr_mem) :
base(new object[] { gr_name, gr_passwd, gr_gid, gr_mem }) {
}
[Documentation("group name")]
public string gr_name => (string)_data[0];
[Documentation("password")]
public string gr_passwd => (string)_data[1];
[Documentation("group id")]
public int gr_gid => (int)_data[2];
[Documentation("group members")]
public PythonList gr_mem => (PythonList)_data[3];
public override string/*!*/ __repr__(CodeContext/*!*/ context) {
return $"grp.struct_group(gr_name='{gr_name}', gr_passwd='{gr_passwd}', gr_gid={gr_gid}, gr_mem={gr_mem.__repr__(context)})";
}
}
private static struct_group Make(IntPtr pwd) {
group g = (group)Marshal.PtrToStructure(pwd, typeof(group));
return new struct_group(g.gr_name, g.gr_passwd, g.gr_gid, new PythonList(MarshalStringArray(g.gr_mem)));
}
private static IEnumerable<string> MarshalStringArray(IntPtr arrayPtr)
{
if (arrayPtr != IntPtr.Zero)
{
IntPtr ptr = Marshal.ReadIntPtr(arrayPtr);
while (ptr != IntPtr.Zero)
{
string key = Marshal.PtrToStringAnsi(ptr);
yield return key;
arrayPtr = new IntPtr(arrayPtr.ToInt64() + IntPtr.Size);
ptr = Marshal.ReadIntPtr(arrayPtr);
}
}
}
public static struct_group getgrgid(int gid) {
var grp = _getgrgid(gid);
if(grp == IntPtr.Zero) {
throw PythonOps.KeyError($"getgrgid(): gid not found: {gid}");
}
return Make(grp);
}
public static struct_group getgrnam(string name) {
var grp = _getgrnam(name);
if(grp == IntPtr.Zero) {
throw PythonOps.KeyError($"getgrnam()): name not found: {name}");
}
return Make(grp);
}
public static PythonList getgrall() {
var res = new PythonList();
setgrent();
IntPtr val = getgrent();
while(val != IntPtr.Zero) {
res.Add(Make(val));
val = getgrent();
}
return res;
}
#region P/Invoke Declarations
[DllImport("libc", EntryPoint="getgrgid", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr _getgrgid(int uid);
[DllImport("libc", EntryPoint="getgrnam", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr _getgrnam([MarshalAs(UnmanagedType.LPStr)] string name);
[DllImport("libc", CallingConvention=CallingConvention.Cdecl)]
private static extern void setgrent();
[DllImport("libc", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr getgrent();
#endregion
}
}
#endif