static PyObject * pwd_getpwall(PyObject *self) { PyObject *d; struct passwd *p; if ((d = PyList_New(0)) == NULL) return NULL; #if defined(PYOS_OS2) && defined(PYCC_GCC) if ((p = getpwuid(0)) != NULL) { #else setpwent(); while ((p = getpwent()) != NULL) { #endif PyObject *v = mkpwent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); endpwent(); return NULL; } Py_DECREF(v); } endpwent(); return d; } #endif static PyMethodDef pwd_methods[] = { {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, #ifdef HAVE_GETPWENT {"getpwall", (PyCFunction)pwd_getpwall, METH_NOARGS, pwd_getpwall__doc__}, #endif {NULL, NULL} /* sentinel */ }; PyMODINIT_FUNC initpwd(void) { PyObject *m; m = Py_InitModule3("pwd", pwd_methods, pwd__doc__); if (m == NULL) return; if (!initialized) PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc); Py_INCREF((PyObject *) &StructPwdType); PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); /* And for b/w compatibility (this was defined by mistake): */ Py_INCREF((PyObject *) &StructPwdType); PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType); initialized = 1; }
static PyObject * pwd_getpwnam(PyObject *self, PyObject *args) { char *name; struct passwd *p; if (!PyArg_ParseTuple(args, "s:getpwnam", &name)) return NULL; if ((p = getpwnam(name)) == NULL) { PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found"); return NULL; } return mkpwent(p); }
static PyObject * pwd_getpwuid(PyObject *self, PyObject *args) { int uid; struct passwd *p; if (!PyArg_ParseTuple(args, "i:getpwuid", &uid)) return NULL; if ((p = getpwuid(uid)) == NULL) { PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found"); return NULL; } return mkpwent(p); }
static PyObject * pwd_getpwuid(PyObject *self, PyObject *args) { unsigned int uid; struct passwd *p; if (!PyArg_ParseTuple(args, "I:getpwuid", &uid)) return NULL; if ((p = getpwuid(uid)) == NULL) { PyErr_Format(PyExc_KeyError, "getpwuid(): uid not found: %d", uid); return NULL; } return mkpwent(p); }
static PyObject * pwd_getpwall_impl(PyModuleDef *module) /*[clinic end generated code: output=ab30e37bf26d431d input=d7ecebfd90219b85]*/ { PyObject *d; struct passwd *p; if ((d = PyList_New(0)) == NULL) return NULL; setpwent(); while ((p = getpwent()) != NULL) { PyObject *v = mkpwent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); endpwent(); return NULL; } Py_DECREF(v); } endpwent(); return d; }
static PyObject * pwd_getpwnam_impl(PyModuleDef *module, PyObject *arg) /*[clinic end generated code: output=66848d42d386fca3 input=d5f7e700919b02d3]*/ { char *name; struct passwd *p; PyObject *bytes, *retval = NULL; if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) return NULL; if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) goto out; if ((p = getpwnam(name)) == NULL) { PyErr_Format(PyExc_KeyError, "getpwnam(): name not found: %s", name); goto out; } retval = mkpwent(p); out: Py_DECREF(bytes); return retval; }
static PyObject * pwd_getpwuid(PyModuleDef *module, PyObject *uidobj) /*[clinic end generated code: output=cba29ae4c2bcb8e1 input=ae64d507a1c6d3e8]*/ { uid_t uid; struct passwd *p; if (!_Py_Uid_Converter(uidobj, &uid)) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) PyErr_Format(PyExc_KeyError, "getpwuid(): uid not found"); return NULL; } if ((p = getpwuid(uid)) == NULL) { PyObject *uid_obj = _PyLong_FromUid(uid); if (uid_obj == NULL) return NULL; PyErr_Format(PyExc_KeyError, "getpwuid(): uid not found: %S", uid_obj); Py_DECREF(uid_obj); return NULL; } return mkpwent(p); }
static PyObject * pwd_getpwall(PyObject *self) { PyObject *d; struct passwd *p; if ((d = PyList_New(0)) == NULL) return NULL; #if defined(PYOS_OS2) && defined(PYCC_GCC) if ((p = getpwuid(0)) != NULL) { #elif !defined(AMITCP) && !defined(INET225) setpwent(); while ((p = getpwent()) != NULL) { PyObject *v = mkpwent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); return NULL; } Py_DECREF(v); } return d; #else #ifdef AMITCP setpwent(); #else setpwent(1); /* INET225 wants argument XXX correct? - I.J. */ #endif while ((p = getpwent()) != NULL) { PyObject *v = mkpwent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); endpwent(); return NULL; } Py_DECREF(v); } endpwent(); return d; #endif /* AMITCP or INET225 */ } #endif static PyMethodDef pwd_methods[] = { {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, #ifdef HAVE_GETPWENT {"getpwall", (PyCFunction)pwd_getpwall, METH_NOARGS, pwd_getpwall__doc__}, #endif {NULL, NULL} /* sentinel */ }; PyMODINIT_FUNC initpwd(void) { PyObject *m; m = Py_InitModule3("pwd", pwd_methods, pwd__doc__); PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc); Py_INCREF((PyObject *) &StructPwdType); PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType); }