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 PyTypeObject StructGrpType
;
35 mkgrent(struct group
*p
)
38 PyObject
*v
= PyStructSequence_New(&StructGrpType
), *w
;
44 if ((w
= PyList_New(0)) == NULL
) {
48 for (member
= p
->gr_mem
; *member
!= NULL
; member
++) {
49 PyObject
*x
= PyString_FromString(*member
);
50 if (x
== NULL
|| PyList_Append(w
, x
) != 0) {
59 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
60 SET(setIndex
++, PyString_FromString(p
->gr_name
));
61 SET(setIndex
++, PyString_FromString(p
->gr_passwd
));
62 SET(setIndex
++, PyInt_FromLong((long) p
->gr_gid
));
66 if (PyErr_Occurred()) {
76 grp_getgrgid(PyObject
*self
, PyObject
*args
)
80 if (!PyArg_ParseTuple(args
, "i:getgrgid", &gid
))
82 if ((p
= getgrgid(gid
)) == NULL
) {
83 PyErr_SetString(PyExc_KeyError
, "getgrgid(): gid not found");
90 grp_getgrnam(PyObject
*self
, PyObject
*args
)
94 if (!PyArg_ParseTuple(args
, "s:getgrnam", &name
))
96 if ((p
= getgrnam(name
)) == NULL
) {
97 PyErr_SetString(PyExc_KeyError
, "getgrnam(): name not found");
104 grp_getgrall(PyObject
*self
, PyObject
*args
)
109 if (!PyArg_ParseTuple(args
, ":getgrall"))
111 if ((d
= PyList_New(0)) == NULL
)
114 while ((p
= getgrent()) != NULL
) {
115 PyObject
*v
= mkgrent(p
);
116 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
127 static PyMethodDef grp_methods
[] = {
128 {"getgrgid", grp_getgrgid
, METH_VARARGS
,
129 "getgrgid(id) -> tuple\n\
130 Return the group database entry for the given numeric group ID. If\n\
131 id is not valid, raise KeyError."},
132 {"getgrnam", grp_getgrnam
, METH_VARARGS
,
133 "getgrnam(name) -> tuple\n\
134 Return the group database entry for the given group name. If\n\
135 name is not valid, raise KeyError."},
136 {"getgrall", grp_getgrall
, METH_VARARGS
,
137 "getgrall() -> list of tuples\n\
138 Return a list of all available group entries, in arbitrary order."},
139 {NULL
, NULL
} /* sentinel */
142 PyDoc_STRVAR(grp__doc__
,
143 "Access to the Unix group database.\n\
145 Group entries are reported as 4-tuples containing the following fields\n\
146 from the group database, in order:\n\
148 name - name of the group\n\
149 passwd - group password (encrypted); often empty\n\
150 gid - numeric ID of the group\n\
151 mem - list of members\n\
153 The gid is an integer, name and password are strings. (Note that most\n\
154 users are not explicitly listed as members of the groups they are in\n\
155 according to the password database. Check both databases to get\n\
156 complete membership information.)");
163 m
= Py_InitModule3("grp", grp_methods
, grp__doc__
);
164 d
= PyModule_GetDict(m
);
165 PyStructSequence_InitType(&StructGrpType
, &struct_group_type_desc
);
166 PyDict_SetItemString(d
, "struct_group", (PyObject
*) &StructGrpType
);