This commit was manufactured by cvs2svn to create tag 'r23a1-fork'.
[python/dscho.git] / Modules / grpmodule.c
blob2882fabd13308f8eb01bccaf19da22ae87cca51a
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 if (p->gr_passwd)
62 SET(setIndex++, PyString_FromString(p->gr_passwd));
63 else {
64 SET(setIndex++, Py_None);
65 Py_INCREF(Py_None);
67 SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
68 SET(setIndex++, w);
69 #undef SET
71 if (PyErr_Occurred()) {
72 Py_DECREF(v);
73 Py_DECREF(w);
74 return NULL;
77 return v;
80 static PyObject *
81 grp_getgrgid(PyObject *self, PyObject *args)
83 int gid;
84 struct group *p;
85 if (!PyArg_ParseTuple(args, "i:getgrgid", &gid))
86 return NULL;
87 if ((p = getgrgid(gid)) == NULL) {
88 PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
89 return NULL;
91 return mkgrent(p);
94 static PyObject *
95 grp_getgrnam(PyObject *self, PyObject *args)
97 char *name;
98 struct group *p;
99 if (!PyArg_ParseTuple(args, "s:getgrnam", &name))
100 return NULL;
101 if ((p = getgrnam(name)) == NULL) {
102 PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
103 return NULL;
105 return mkgrent(p);
108 static PyObject *
109 grp_getgrall(PyObject *self, PyObject *args)
111 PyObject *d;
112 struct group *p;
114 if (!PyArg_ParseTuple(args, ":getgrall"))
115 return NULL;
116 if ((d = PyList_New(0)) == NULL)
117 return NULL;
118 setgrent();
119 while ((p = getgrent()) != NULL) {
120 PyObject *v = mkgrent(p);
121 if (v == NULL || PyList_Append(d, v) != 0) {
122 Py_XDECREF(v);
123 Py_DECREF(d);
124 return NULL;
126 Py_DECREF(v);
128 endgrent();
129 return d;
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.)");
164 PyMODINIT_FUNC
165 initgrp(void)
167 PyObject *m, *d;
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);