2 /* UNIX group file access module */
10 static PyStructSequence_Field struct_group_type_fields
[] = {
11 {"gr_name", "group name"},
12 {"gr_passwd", "password"},
13 {"gr_gid", "group id"},
14 {"gr_mem", "group memebers"},
18 PyDoc_STRVAR(struct_group__doc__
,
19 "grp.struct_group: Results from getgr*() routines.\n\n\
20 This object may be accessed either as a tuple of\n\
21 (gr_name,gr_passwd,gr_gid,gr_mem)\n\
22 or via the object attributes as named in the above tuple.\n");
24 static PyStructSequence_Desc struct_group_type_desc
= {
27 struct_group_type_fields
,
32 static int initialized
;
33 static PyTypeObject StructGrpType
;
36 mkgrent(struct group
*p
)
39 PyObject
*v
= PyStructSequence_New(&StructGrpType
), *w
;
45 if ((w
= PyList_New(0)) == NULL
) {
49 #define FSDECODE(val) PyUnicode_Decode(val, strlen(val),\
50 Py_FileSystemDefaultEncoding,\
52 for (member
= p
->gr_mem
; *member
!= NULL
; member
++) {
53 PyObject
*x
= FSDECODE(*member
);
54 if (x
== NULL
|| PyList_Append(w
, x
) != 0) {
63 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
64 SET(setIndex
++, FSDECODE(p
->gr_name
));
66 SET(setIndex
++, Py_None
);
70 SET(setIndex
++, FSDECODE(p
->gr_passwd
));
72 SET(setIndex
++, Py_None
);
76 SET(setIndex
++, PyLong_FromLong((long) p
->gr_gid
));
80 if (PyErr_Occurred()) {
89 grp_getgrgid(PyObject
*self
, PyObject
*pyo_id
)
95 py_int_id
= PyNumber_Long(pyo_id
);
98 gid
= PyLong_AS_LONG(py_int_id
);
101 if ((p
= getgrgid(gid
)) == NULL
) {
102 PyErr_Format(PyExc_KeyError
, "getgrgid(): gid not found: %d", gid
);
109 grp_getgrnam(PyObject
*self
, PyObject
*args
)
113 PyObject
*arg
, *bytes
, *retval
= NULL
;
115 if (!PyArg_ParseTuple(args
, "U:getgrnam", &arg
))
117 if ((bytes
= PyUnicode_AsEncodedString(arg
, Py_FileSystemDefaultEncoding
,
118 "surrogateescape")) == NULL
)
120 if (PyBytes_AsStringAndSize(bytes
, &name
, NULL
) == -1)
123 if ((p
= getgrnam(name
)) == NULL
) {
124 PyErr_Format(PyExc_KeyError
, "getgrnam(): name not found: %s", name
);
134 grp_getgrall(PyObject
*self
, PyObject
*ignore
)
139 if ((d
= PyList_New(0)) == NULL
)
142 while ((p
= getgrent()) != NULL
) {
143 PyObject
*v
= mkgrent(p
);
144 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
156 static PyMethodDef grp_methods
[] = {
157 {"getgrgid", grp_getgrgid
, METH_O
,
158 "getgrgid(id) -> tuple\n\
159 Return the group database entry for the given numeric group ID. If\n\
160 id is not valid, raise KeyError."},
161 {"getgrnam", grp_getgrnam
, METH_VARARGS
,
162 "getgrnam(name) -> tuple\n\
163 Return the group database entry for the given group name. If\n\
164 name is not valid, raise KeyError."},
165 {"getgrall", grp_getgrall
, METH_NOARGS
,
166 "getgrall() -> list of tuples\n\
167 Return a list of all available group entries, in arbitrary order."},
168 {NULL
, NULL
} /* sentinel */
171 PyDoc_STRVAR(grp__doc__
,
172 "Access to the Unix group database.\n\
174 Group entries are reported as 4-tuples containing the following fields\n\
175 from the group database, in order:\n\
177 name - name of the group\n\
178 passwd - group password (encrypted); often empty\n\
179 gid - numeric ID of the group\n\
180 mem - list of members\n\
182 The gid is an integer, name and password are strings. (Note that most\n\
183 users are not explicitly listed as members of the groups they are in\n\
184 according to the password database. Check both databases to get\n\
185 complete membership information.)");
189 static struct PyModuleDef grpmodule
= {
190 PyModuleDef_HEAD_INIT
,
205 m
= PyModule_Create(&grpmodule
);
208 d
= PyModule_GetDict(m
);
210 PyStructSequence_InitType(&StructGrpType
, &struct_group_type_desc
);
211 PyDict_SetItemString(d
, "struct_group", (PyObject
*) &StructGrpType
);