2 /* UNIX group file access module */
11 mkgrent(struct group
*p
)
15 if ((w
= PyList_New(0)) == NULL
) {
18 for (member
= p
->gr_mem
; *member
!= NULL
; member
++) {
19 PyObject
*x
= PyString_FromString(*member
);
20 if (x
== NULL
|| PyList_Append(w
, x
) != 0) {
27 v
= Py_BuildValue("(sslO)",
30 #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
31 /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
32 for later versions you may have to remove this */
33 (long)p
->gr_short_pad
, /* ugh-NeXT broke the padding */
43 grp_getgrgid(PyObject
*self
, PyObject
*args
)
47 if (!PyArg_ParseTuple(args
, "i:getgrgid", &gid
))
49 if ((p
= getgrgid(gid
)) == NULL
) {
50 PyErr_SetString(PyExc_KeyError
, "getgrgid(): gid not found");
57 grp_getgrnam(PyObject
*self
, PyObject
*args
)
61 if (!PyArg_ParseTuple(args
, "s:getgrnam", &name
))
63 if ((p
= getgrnam(name
)) == NULL
) {
64 PyErr_SetString(PyExc_KeyError
, "getgrnam(): name not found");
71 grp_getgrall(PyObject
*self
, PyObject
*args
)
76 if (!PyArg_ParseTuple(args
, ":getgrall"))
78 if ((d
= PyList_New(0)) == NULL
)
81 while ((p
= getgrent()) != NULL
) {
82 PyObject
*v
= mkgrent(p
);
83 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
94 static PyMethodDef grp_methods
[] = {
95 {"getgrgid", grp_getgrgid
, METH_VARARGS
,
96 "getgrgid(id) -> tuple\n\
97 Return the group database entry for the given numeric group ID. If\n\
98 id is not valid, raise KeyError."},
99 {"getgrnam", grp_getgrnam
, METH_VARARGS
,
100 "getgrnam(name) -> tuple\n\
101 Return the group database entry for the given group name. If\n\
102 name is not valid, raise KeyError."},
103 {"getgrall", grp_getgrall
, METH_VARARGS
,
104 "getgrall() -> list of tuples\n\
105 Return a list of all available group entries, in arbitrary order."},
106 {NULL
, NULL
} /* sentinel */
109 static char grp__doc__
[] =
110 "Access to the Unix group database.\n\
112 Group entries are reported as 4-tuples containing the following fields\n\
113 from the group database, in order:\n\
115 name - name of the group\n\
116 passwd - group password (encrypted); often empty\n\
117 gid - numeric ID of the group\n\
118 mem - list of members\n\
120 The gid is an integer, name and password are strings. (Note that most\n\
121 users are not explicitly listed as members of the groups they are in\n\
122 according to the password database. Check both databases to get\n\
123 complete membership information.)";
129 Py_InitModule3("grp", grp_methods
, grp__doc__
);