- Got rid of newmodule.c
[python/dscho.git] / Modules / grpmodule.c
blobc8340099404701728951b62126d2a781cae981bb
2 /* UNIX group file access module */
4 #include "Python.h"
5 #include "structseq.h"
7 #include <sys/types.h>
8 #include <grp.h>
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"},
15 {0}
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 = {
25 "grp.struct_group",
26 struct_group__doc__,
27 struct_group_type_fields,
32 static PyTypeObject StructGrpType;
34 static PyObject *
35 mkgrent(struct group *p)
37 int setIndex = 0;
38 PyObject *v = PyStructSequence_New(&StructGrpType), *w;
39 char **member;
41 if (v == NULL)
42 return NULL;
44 if ((w = PyList_New(0)) == NULL) {
45 Py_DECREF(v);
46 return 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) {
51 Py_XDECREF(x);
52 Py_DECREF(w);
53 Py_DECREF(v);
54 return NULL;
56 Py_DECREF(x);
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));
63 SET(setIndex++, w);
64 #undef SET
66 if (PyErr_Occurred()) {
67 Py_DECREF(v);
68 Py_DECREF(w);
69 return NULL;
72 return v;
75 static PyObject *
76 grp_getgrgid(PyObject *self, PyObject *args)
78 int gid;
79 struct group *p;
80 if (!PyArg_ParseTuple(args, "i:getgrgid", &gid))
81 return NULL;
82 if ((p = getgrgid(gid)) == NULL) {
83 PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
84 return NULL;
86 return mkgrent(p);
89 static PyObject *
90 grp_getgrnam(PyObject *self, PyObject *args)
92 char *name;
93 struct group *p;
94 if (!PyArg_ParseTuple(args, "s:getgrnam", &name))
95 return NULL;
96 if ((p = getgrnam(name)) == NULL) {
97 PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
98 return NULL;
100 return mkgrent(p);
103 static PyObject *
104 grp_getgrall(PyObject *self, PyObject *args)
106 PyObject *d;
107 struct group *p;
109 if (!PyArg_ParseTuple(args, ":getgrall"))
110 return NULL;
111 if ((d = PyList_New(0)) == NULL)
112 return NULL;
113 setgrent();
114 while ((p = getgrent()) != NULL) {
115 PyObject *v = mkgrent(p);
116 if (v == NULL || PyList_Append(d, v) != 0) {
117 Py_XDECREF(v);
118 Py_DECREF(d);
119 return NULL;
121 Py_DECREF(v);
123 endgrent();
124 return d;
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.)");
159 DL_EXPORT(void)
160 initgrp(void)
162 PyObject *m, *d;
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);