use bools
[python.git] / Modules / grpmodule.c
blobffb451e1744388bd7f82fb88f304617c7d4499c1
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 int initialized;
33 static PyTypeObject StructGrpType;
35 static PyObject *
36 mkgrent(struct group *p)
38 int setIndex = 0;
39 PyObject *v = PyStructSequence_New(&StructGrpType), *w;
40 char **member;
42 if (v == NULL)
43 return NULL;
45 if ((w = PyList_New(0)) == NULL) {
46 Py_DECREF(v);
47 return 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) {
52 Py_XDECREF(x);
53 Py_DECREF(w);
54 Py_DECREF(v);
55 return NULL;
57 Py_DECREF(x);
60 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
61 SET(setIndex++, PyString_FromString(p->gr_name));
62 #ifdef __VMS
63 SET(setIndex++, Py_None);
64 Py_INCREF(Py_None);
65 #else
66 if (p->gr_passwd)
67 SET(setIndex++, PyString_FromString(p->gr_passwd));
68 else {
69 SET(setIndex++, Py_None);
70 Py_INCREF(Py_None);
72 #endif
73 SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
74 SET(setIndex++, w);
75 #undef SET
77 if (PyErr_Occurred()) {
78 Py_DECREF(v);
79 return NULL;
82 return v;
85 static PyObject *
86 grp_getgrgid(PyObject *self, PyObject *pyo_id)
88 PyObject *py_int_id;
89 unsigned int gid;
90 struct group *p;
92 py_int_id = PyNumber_Int(pyo_id);
93 if (!py_int_id)
94 return NULL;
95 gid = PyInt_AS_LONG(py_int_id);
96 Py_DECREF(py_int_id);
98 if ((p = getgrgid(gid)) == NULL) {
99 PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
100 return NULL;
102 return mkgrent(p);
105 static PyObject *
106 grp_getgrnam(PyObject *self, PyObject *pyo_name)
108 PyObject *py_str_name;
109 char *name;
110 struct group *p;
112 py_str_name = PyObject_Str(pyo_name);
113 if (!py_str_name)
114 return NULL;
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);
120 return NULL;
123 Py_DECREF(py_str_name);
124 return mkgrent(p);
127 static PyObject *
128 grp_getgrall(PyObject *self, PyObject *ignore)
130 PyObject *d;
131 struct group *p;
133 if ((d = PyList_New(0)) == NULL)
134 return NULL;
135 setgrent();
136 while ((p = getgrent()) != NULL) {
137 PyObject *v = mkgrent(p);
138 if (v == NULL || PyList_Append(d, v) != 0) {
139 Py_XDECREF(v);
140 Py_DECREF(d);
141 endgrent();
142 return NULL;
144 Py_DECREF(v);
146 endgrent();
147 return d;
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.)");
182 PyMODINIT_FUNC
183 initgrp(void)
185 PyObject *m, *d;
186 m = Py_InitModule3("grp", grp_methods, grp__doc__);
187 if (m == NULL)
188 return;
189 d = PyModule_GetDict(m);
190 if (!initialized)
191 PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
192 PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
193 initialized = 1;