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 for (member
= p
->gr_mem
; *member
!= NULL
; member
++) {
50 PyObject
*x
= PyString_FromString(*member
);
51 if (x
== NULL
|| PyList_Append(w
, x
) != 0) {
60 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
61 SET(setIndex
++, PyString_FromString(p
->gr_name
));
63 SET(setIndex
++, Py_None
);
67 SET(setIndex
++, PyString_FromString(p
->gr_passwd
));
69 SET(setIndex
++, Py_None
);
73 SET(setIndex
++, PyInt_FromLong((long) p
->gr_gid
));
77 if (PyErr_Occurred()) {
86 grp_getgrgid(PyObject
*self
, PyObject
*pyo_id
)
92 py_int_id
= PyNumber_Int(pyo_id
);
95 gid
= PyInt_AS_LONG(py_int_id
);
98 if ((p
= getgrgid(gid
)) == NULL
) {
99 PyErr_Format(PyExc_KeyError
, "getgrgid(): gid not found: %d", gid
);
106 grp_getgrnam(PyObject
*self
, PyObject
*pyo_name
)
108 PyObject
*py_str_name
;
112 py_str_name
= PyObject_Str(pyo_name
);
115 name
= PyString_AS_STRING(py_str_name
);
117 if ((p
= getgrnam(name
)) == NULL
) {
118 PyErr_Format(PyExc_KeyError
, "getgrnam(): name not found: %s", name
);
119 Py_DECREF(py_str_name
);
123 Py_DECREF(py_str_name
);
128 grp_getgrall(PyObject
*self
, PyObject
*ignore
)
133 if ((d
= PyList_New(0)) == NULL
)
136 while ((p
= getgrent()) != NULL
) {
137 PyObject
*v
= mkgrent(p
);
138 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
150 static PyMethodDef grp_methods
[] = {
151 {"getgrgid", grp_getgrgid
, METH_O
,
152 "getgrgid(id) -> tuple\n\
153 Return the group database entry for the given numeric group ID. If\n\
154 id is not valid, raise KeyError."},
155 {"getgrnam", grp_getgrnam
, METH_O
,
156 "getgrnam(name) -> tuple\n\
157 Return the group database entry for the given group name. If\n\
158 name is not valid, raise KeyError."},
159 {"getgrall", grp_getgrall
, METH_NOARGS
,
160 "getgrall() -> list of tuples\n\
161 Return a list of all available group entries, in arbitrary order."},
162 {NULL
, NULL
} /* sentinel */
165 PyDoc_STRVAR(grp__doc__
,
166 "Access to the Unix group database.\n\
168 Group entries are reported as 4-tuples containing the following fields\n\
169 from the group database, in order:\n\
171 name - name of the group\n\
172 passwd - group password (encrypted); often empty\n\
173 gid - numeric ID of the group\n\
174 mem - list of members\n\
176 The gid is an integer, name and password are strings. (Note that most\n\
177 users are not explicitly listed as members of the groups they are in\n\
178 according to the password database. Check both databases to get\n\
179 complete membership information.)");
186 m
= Py_InitModule3("grp", grp_methods
, grp__doc__
);
189 d
= PyModule_GetDict(m
);
191 PyStructSequence_InitType(&StructGrpType
, &struct_group_type_desc
);
192 PyDict_SetItemString(d
, "struct_group", (PyObject
*) &StructGrpType
);