PageRenderTime 364ms CodeModel.GetById 241ms app.highlight 12ms RepoModel.GetById 109ms app.codeStats 0ms

/Modules/grpmodule.c

http://unladen-swallow.googlecode.com/
C | 194 lines | 166 code | 27 blank | 1 comment | 30 complexity | 96611645c05a2e5b88a9fe35879e33c7 MD5 | raw file
  1
  2/* UNIX group file access module */
  3
  4#include "Python.h"
  5#include "structseq.h"
  6
  7#include <sys/types.h>
  8#include <grp.h>
  9
 10static 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}
 16};
 17
 18PyDoc_STRVAR(struct_group__doc__,
 19"grp.struct_group: Results from getgr*() routines.\n\n\
 20This object may be accessed either as a tuple of\n\
 21  (gr_name,gr_passwd,gr_gid,gr_mem)\n\
 22or via the object attributes as named in the above tuple.\n");
 23
 24static PyStructSequence_Desc struct_group_type_desc = {
 25   "grp.struct_group",
 26   struct_group__doc__,
 27   struct_group_type_fields,
 28   4,
 29};
 30
 31
 32static int initialized;
 33static PyTypeObject StructGrpType;
 34
 35static PyObject *
 36mkgrent(struct group *p)
 37{
 38    int setIndex = 0;
 39    PyObject *v = PyStructSequence_New(&StructGrpType), *w;
 40    char **member;
 41
 42    if (v == NULL)
 43        return NULL;
 44
 45    if ((w = PyList_New(0)) == NULL) {
 46        Py_DECREF(v);
 47        return NULL;
 48    }
 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;
 56        }
 57        Py_DECREF(x);
 58    }
 59
 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);
 71    }
 72#endif
 73    SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
 74    SET(setIndex++, w);
 75#undef SET
 76
 77    if (PyErr_Occurred()) {
 78        Py_DECREF(v);
 79        return NULL;
 80    }
 81
 82    return v;
 83}
 84
 85static PyObject *
 86grp_getgrgid(PyObject *self, PyObject *pyo_id)
 87{
 88    PyObject *py_int_id;
 89    unsigned int gid;
 90    struct group *p;
 91
 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);
 97
 98    if ((p = getgrgid(gid)) == NULL) {
 99	PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
100        return NULL;
101    }
102    return mkgrent(p);
103}
104
105static PyObject *
106grp_getgrnam(PyObject *self, PyObject *pyo_name)
107{
108    PyObject *py_str_name;
109    char *name;
110    struct group *p;
111
112    py_str_name = PyObject_Str(pyo_name);
113    if (!py_str_name)
114	    return NULL;
115    name = PyString_AS_STRING(py_str_name);
116    
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;
121    }
122
123    Py_DECREF(py_str_name);
124    return mkgrent(p);
125}
126
127static PyObject *
128grp_getgrall(PyObject *self, PyObject *ignore)
129{
130    PyObject *d;
131    struct group *p;
132
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;
143        }
144        Py_DECREF(v);
145    }
146    endgrent();
147    return d;
148}
149
150static PyMethodDef grp_methods[] = {
151    {"getgrgid",	grp_getgrgid,	METH_O,
152     "getgrgid(id) -> tuple\n\
153Return the group database entry for the given numeric group ID.  If\n\
154id is not valid, raise KeyError."},
155    {"getgrnam",	grp_getgrnam,	METH_O,
156     "getgrnam(name) -> tuple\n\
157Return the group database entry for the given group name.  If\n\
158name is not valid, raise KeyError."},
159    {"getgrall",	grp_getgrall,	METH_NOARGS,
160     "getgrall() -> list of tuples\n\
161Return a list of all available group entries, in arbitrary order."},
162    {NULL,		NULL}		/* sentinel */
163};
164
165PyDoc_STRVAR(grp__doc__,
166"Access to the Unix group database.\n\
167\n\
168Group entries are reported as 4-tuples containing the following fields\n\
169from the group database, in order:\n\
170\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\
175\n\
176The gid is an integer, name and password are strings.  (Note that most\n\
177users are not explicitly listed as members of the groups they are in\n\
178according to the password database.  Check both databases to get\n\
179complete membership information.)");
180
181
182PyMODINIT_FUNC
183initgrp(void)
184{
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;
194}