diff options
author | Ross Lagerwall <rosslagerwall@gmail.com> | 2011-06-10 05:30:30 (GMT) |
---|---|---|
committer | Ross Lagerwall <rosslagerwall@gmail.com> | 2011-06-10 05:30:30 (GMT) |
commit | b0ae53d8a09731a51be48f9e84a71d09d0f90657 (patch) | |
tree | c4aed090ed5d29da17d008e3aa8d47b076146f1b /Modules | |
parent | 10c30d676432b995d19308855a5ed40f87353074 (diff) | |
download | cpython-b0ae53d8a09731a51be48f9e84a71d09d0f90657.zip cpython-b0ae53d8a09731a51be48f9e84a71d09d0f90657.tar.gz cpython-b0ae53d8a09731a51be48f9e84a71d09d0f90657.tar.bz2 |
Issue #9344: Add os.getgrouplist().
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index bfdf5cd..2d26a0e 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4672,6 +4672,70 @@ posix_getpid(PyObject *self, PyObject *noargs) return PyLong_FromPid(getpid()); } +#ifdef HAVE_GETGROUPLIST +PyDoc_STRVAR(posix_getgrouplist__doc__, +"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ +Returns a list of groups to which a user belongs.\n\n\ + user: username to lookup\n\ + group: base group id of the user"); + +static PyObject * +posix_getgrouplist(PyObject *self, PyObject *args) +{ +#ifdef NGROUPS_MAX +#define MAX_GROUPS NGROUPS_MAX +#else + /* defined to be 16 on Solaris7, so this should be a small number */ +#define MAX_GROUPS 64 +#endif + + const char *user; + int i, ngroups; + PyObject *list; +#ifdef __APPLE__ + int *groups, basegid; +#else + gid_t *groups, basegid; +#endif + ngroups = MAX_GROUPS; + + if (!PyArg_ParseTuple(args, "si", &user, &basegid)) + return NULL; + +#ifdef __APPLE__ + groups = PyMem_Malloc(ngroups * sizeof(int)); +#else + groups = PyMem_Malloc(ngroups * sizeof(gid_t)); +#endif + if (groups == NULL) + return PyErr_NoMemory(); + + if (getgrouplist(user, basegid, groups, &ngroups) == -1) { + PyMem_Del(groups); + return posix_error(); + } + + list = PyList_New(ngroups); + if (list == NULL) { + PyMem_Del(groups); + return NULL; + } + + for (i = 0; i < ngroups; i++) { + PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); + if (o == NULL) { + Py_DECREF(list); + PyMem_Del(groups); + return NULL; + } + PyList_SET_ITEM(list, i, o); + } + + PyMem_Del(groups); + + return list; +} +#endif #ifdef HAVE_GETGROUPS PyDoc_STRVAR(posix_getgroups__doc__, @@ -9383,6 +9447,9 @@ static PyMethodDef posix_methods[] = { #ifdef HAVE_GETGID {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, #endif /* HAVE_GETGID */ +#ifdef HAVE_GETGROUPLIST + {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, +#endif #ifdef HAVE_GETGROUPS {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, #endif |