Merged release21-maint changes.
[python/dscho.git] / Modules / grpmodule.c
blob5dbcf9862bc8f49f2b52fceb420693f6d37826be
2 /* UNIX group file access module */
4 #include "Python.h"
6 #include <sys/types.h>
7 #include <grp.h>
10 static PyObject *
11 mkgrent(struct group *p)
13 PyObject *v, *w;
14 char **member;
15 if ((w = PyList_New(0)) == NULL) {
16 return 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) {
21 Py_XDECREF(x);
22 Py_DECREF(w);
23 return NULL;
25 Py_DECREF(x);
27 v = Py_BuildValue("(sslO)",
28 p->gr_name,
29 p->gr_passwd,
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 */
34 #else
35 (long)p->gr_gid,
36 #endif
37 w);
38 Py_DECREF(w);
39 return v;
42 static PyObject *
43 grp_getgrgid(PyObject *self, PyObject *args)
45 int gid;
46 struct group *p;
47 if (!PyArg_ParseTuple(args, "i:getgrgid", &gid))
48 return NULL;
49 if ((p = getgrgid(gid)) == NULL) {
50 PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
51 return NULL;
53 return mkgrent(p);
56 static PyObject *
57 grp_getgrnam(PyObject *self, PyObject *args)
59 char *name;
60 struct group *p;
61 if (!PyArg_ParseTuple(args, "s:getgrnam", &name))
62 return NULL;
63 if ((p = getgrnam(name)) == NULL) {
64 PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
65 return NULL;
67 return mkgrent(p);
70 static PyObject *
71 grp_getgrall(PyObject *self, PyObject *args)
73 PyObject *d;
74 struct group *p;
76 if (!PyArg_ParseTuple(args, ":getgrall"))
77 return NULL;
78 if ((d = PyList_New(0)) == NULL)
79 return NULL;
80 setgrent();
81 while ((p = getgrent()) != NULL) {
82 PyObject *v = mkgrent(p);
83 if (v == NULL || PyList_Append(d, v) != 0) {
84 Py_XDECREF(v);
85 Py_DECREF(d);
86 return NULL;
88 Py_DECREF(v);
90 endgrent();
91 return d;
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.)";
126 DL_EXPORT(void)
127 initgrp(void)
129 Py_InitModule3("grp", grp_methods, grp__doc__);