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
));
62 SET(setIndex
++, PyString_FromString(p
->gr_passwd
));
64 SET(setIndex
++, Py_None
);
67 SET(setIndex
++, PyInt_FromLong((long) p
->gr_gid
));
71 if (PyErr_Occurred()) {
81 grp_getgrgid(PyObject
*self
, PyObject
*args
)
85 if (!PyArg_ParseTuple(args
, "i:getgrgid", &gid
))
87 if ((p
= getgrgid(gid
)) == NULL
) {
88 PyErr_SetString(PyExc_KeyError
, "getgrgid(): gid not found");
95 grp_getgrnam(PyObject
*self
, PyObject
*args
)
99 if (!PyArg_ParseTuple(args
, "s:getgrnam", &name
))
101 if ((p
= getgrnam(name
)) == NULL
) {
102 PyErr_SetString(PyExc_KeyError
, "getgrnam(): name not found");
109 grp_getgrall(PyObject
*self
, PyObject
*args
)
114 if (!PyArg_ParseTuple(args
, ":getgrall"))
116 if ((d
= PyList_New(0)) == NULL
)
119 while ((p
= getgrent()) != NULL
) {
120 PyObject
*v
= mkgrent(p
);
121 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
132 static PyMethodDef grp_methods
[] = {
133 {"getgrgid", grp_getgrgid
, METH_VARARGS
,
134 "getgrgid(id) -> tuple\n\
135 Return the group database entry for the given numeric group ID. If\n\
136 id is not valid, raise KeyError."},
137 {"getgrnam", grp_getgrnam
, METH_VARARGS
,
138 "getgrnam(name) -> tuple\n\
139 Return the group database entry for the given group name. If\n\
140 name is not valid, raise KeyError."},
141 {"getgrall", grp_getgrall
, METH_VARARGS
,
142 "getgrall() -> list of tuples\n\
143 Return a list of all available group entries, in arbitrary order."},
144 {NULL
, NULL
} /* sentinel */
147 PyDoc_STRVAR(grp__doc__
,
148 "Access to the Unix group database.\n\
150 Group entries are reported as 4-tuples containing the following fields\n\
151 from the group database, in order:\n\
153 name - name of the group\n\
154 passwd - group password (encrypted); often empty\n\
155 gid - numeric ID of the group\n\
156 mem - list of members\n\
158 The gid is an integer, name and password are strings. (Note that most\n\
159 users are not explicitly listed as members of the groups they are in\n\
160 according to the password database. Check both databases to get\n\
161 complete membership information.)");
168 m
= Py_InitModule3("grp", grp_methods
, grp__doc__
);
169 d
= PyModule_GetDict(m
);
170 PyStructSequence_InitType(&StructGrpType
, &struct_group_type_desc
);
171 PyDict_SetItemString(d
, "struct_group", (PyObject
*) &StructGrpType
);