Пример #1
0
static PyObject *
grp_getgrgid(PyObject *self, PyObject *pyo_id)
{
    PyObject *py_int_id;
    gid_t gid;
    struct group *p;

    py_int_id = PyNumber_Int(pyo_id);
    if (!py_int_id)
            return NULL;
    if (!_Py_Gid_Converter(py_int_id, &gid)) {
        Py_DECREF(py_int_id);
        return NULL;
    }
    Py_DECREF(py_int_id);

    if ((p = getgrgid(gid)) == NULL) {
        if (gid < 0)
            PyErr_Format(PyExc_KeyError,
                         "getgrgid(): gid not found: %ld", (long)gid);
        else
            PyErr_Format(PyExc_KeyError,
                         "getgrgid(): gid not found: %lu", (unsigned long)gid);
        return NULL;
    }
    return mkgrent(p);
}
Пример #2
0
static PyObject *
grp_getgrgid(PyObject *self, PyObject *pyo_id)
{
    PyObject *py_int_id;
    unsigned int gid;
    struct group *p;

    py_int_id = PyNumber_Long(pyo_id);
    if (!py_int_id)
            return NULL;
    gid = PyLong_AS_LONG(py_int_id);
    Py_DECREF(py_int_id);

    if ((p = getgrgid(gid)) == NULL) {
        PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
        return NULL;
    }
    return mkgrent(p);
}
Пример #3
0
static PyObject *
grp_getgrall(PyObject *self, PyObject *ignore)
{
    PyObject *d;
    struct group *p;

    if ((d = PyList_New(0)) == NULL)
        return NULL;
    setgrent();
    while ((p = getgrent()) != NULL) {
        PyObject *v = mkgrent(p);
        if (v == NULL || PyList_Append(d, v) != 0) {
            Py_XDECREF(v);
            Py_DECREF(d);
            return NULL;
        }
        Py_DECREF(v);
    }
    endgrent();
    return d;
}
Пример #4
0
static PyObject *
grp_getgrnam(PyObject *self, PyObject *pyo_name)
{
    PyObject *py_str_name;
    char *name;
    struct group *p;

    py_str_name = PyObject_Str(pyo_name);
    if (!py_str_name)
	    return NULL;
    name = PyString_AS_STRING(py_str_name);
    
    if ((p = getgrnam(name)) == NULL) {
	PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
	Py_DECREF(py_str_name);
        return NULL;
    }

    Py_DECREF(py_str_name);
    return mkgrent(p);
}
Пример #5
0
static PyObject *
grp_getgrnam_impl(PyModuleDef *module, PyObject *name)
/*[clinic end generated code: output=cd47511f4854da8e input=08ded29affa3c863]*/
{
    char *name_chars;
    struct group *p;
    PyObject *bytes, *retval = NULL;

    if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
        return NULL;
    if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
        goto out;

    if ((p = getgrnam(name_chars)) == NULL) {
        PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
        goto out;
    }
    retval = mkgrent(p);
out:
    Py_DECREF(bytes);
    return retval;
}
Пример #6
0
static PyObject *
grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
/*[clinic end generated code: output=8a11f5fdeb8c78a0 input=15fa0e2ccf5cda25]*/
{
    PyObject *py_int_id;
    gid_t gid;
    struct group *p;

    if (!_Py_Gid_Converter(id, &gid)) {
        if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
            return NULL;
        }
        PyErr_Clear();
        if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
                             "group id must be int, not %.200",
                             id->ob_type->tp_name) < 0) {
            return NULL;
        }
        py_int_id = PyNumber_Long(id);
        if (!py_int_id)
            return NULL;
        if (!_Py_Gid_Converter(py_int_id, &gid)) {
            Py_DECREF(py_int_id);
            return NULL;
        }
        Py_DECREF(py_int_id);
    }

    if ((p = getgrgid(gid)) == NULL) {
        PyObject *gid_obj = _PyLong_FromGid(gid);
        if (gid_obj == NULL)
            return NULL;
        PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj);
        Py_DECREF(gid_obj);
        return NULL;
    }
    return mkgrent(p);
}
Пример #7
0
static PyObject *
grp_getgrall_impl(PyModuleDef *module)
/*[clinic end generated code: output=add9037a20c202de input=d7df76c825c367df]*/
{
    PyObject *d;
    struct group *p;

    if ((d = PyList_New(0)) == NULL)
        return NULL;
    setgrent();
    while ((p = getgrent()) != NULL) {
        PyObject *v = mkgrent(p);
        if (v == NULL || PyList_Append(d, v) != 0) {
            Py_XDECREF(v);
            Py_DECREF(d);
            endgrent();
            return NULL;
        }
        Py_DECREF(v);
    }
    endgrent();
    return d;
}
Пример #8
0
static PyObject *
grp_getgrnam(PyObject *self, PyObject *args)
{
    char *name;
    struct group *p;
    PyObject *arg, *bytes, *retval = NULL;

    if (!PyArg_ParseTuple(args, "U:getgrnam", &arg))
        return NULL;
    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
        return NULL;
    if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
        goto out;

    if ((p = getgrnam(name)) == NULL) {
        PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
        goto out;
    }
    retval = mkgrent(p);
out:
    Py_DECREF(bytes);
    return retval;
}