append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Modules / pwdmodule.c
blob9134edc1b17965e7a50bb26e0671ec2e95ecaee4
2 /* UNIX password file access module */
4 #include "Python.h"
5 #include "structseq.h"
7 #include <sys/types.h>
8 #include <pwd.h>
10 static PyStructSequence_Field struct_pwd_type_fields[] = {
11 {"pw_name", "user name"},
12 {"pw_passwd", "password"},
13 {"pw_uid", "user id"},
14 {"pw_gid", "group id"},
15 {"pw_gecos", "real name"},
16 {"pw_dir", "home directory"},
17 {"pw_shell", "shell program"},
18 {0}
21 PyDoc_STRVAR(struct_passwd__doc__,
22 "pwd.struct_passwd: Results from getpw*() routines.\n\n\
23 This object may be accessed either as a tuple of\n\
24 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
25 or via the object attributes as named in the above tuple.");
27 static PyStructSequence_Desc struct_pwd_type_desc = {
28 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
34 PyDoc_STRVAR(pwd__doc__,
35 "This module provides access to the Unix password database.\n\
36 It is available on all Unix versions.\n\
37 \n\
38 Password database entries are reported as 7-tuples containing the following\n\
39 items from the password database (see `<pwd.h>'), in order:\n\
40 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
41 The uid and gid items are integers, all others are strings. An\n\
42 exception is raised if the entry asked for cannot be found.");
45 static PyTypeObject StructPwdType;
47 static void
48 sets(PyObject *v, int i, char* val)
50 if (val)
51 PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
52 else {
53 PyStructSequence_SET_ITEM(v, i, Py_None);
54 Py_INCREF(Py_None);
58 static PyObject *
59 mkpwent(struct passwd *p)
61 int setIndex = 0;
62 PyObject *v = PyStructSequence_New(&StructPwdType);
63 if (v == NULL)
64 return NULL;
66 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
67 #define SETS(i,val) sets(v, i, val)
69 SETS(setIndex++, p->pw_name);
70 SETS(setIndex++, p->pw_passwd);
71 SETI(setIndex++, p->pw_uid);
72 SETI(setIndex++, p->pw_gid);
73 SETS(setIndex++, p->pw_gecos);
74 SETS(setIndex++, p->pw_dir);
75 SETS(setIndex++, p->pw_shell);
77 #undef SETS
78 #undef SETI
80 if (PyErr_Occurred()) {
81 Py_XDECREF(v);
82 return NULL;
85 return v;
88 PyDoc_STRVAR(pwd_getpwuid__doc__,
89 "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
90 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
91 Return the password database entry for the given numeric user ID.\n\
92 See pwd.__doc__ for more on password database entries.");
94 static PyObject *
95 pwd_getpwuid(PyObject *self, PyObject *args)
97 int uid;
98 struct passwd *p;
99 if (!PyArg_ParseTuple(args, "i:getpwuid", &uid))
100 return NULL;
101 if ((p = getpwuid(uid)) == NULL) {
102 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
103 return NULL;
105 return mkpwent(p);
108 PyDoc_STRVAR(pwd_getpwnam__doc__,
109 "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
110 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
111 Return the password database entry for the given user name.\n\
112 See pwd.__doc__ for more on password database entries.");
114 static PyObject *
115 pwd_getpwnam(PyObject *self, PyObject *args)
117 char *name;
118 struct passwd *p;
119 if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
120 return NULL;
121 if ((p = getpwnam(name)) == NULL) {
122 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
123 return NULL;
125 return mkpwent(p);
128 #ifdef HAVE_GETPWENT
129 PyDoc_STRVAR(pwd_getpwall__doc__,
130 "getpwall() -> list_of_entries\n\
131 Return a list of all available password database entries, \
132 in arbitrary order.\n\
133 See pwd.__doc__ for more on password database entries.");
135 static PyObject *
136 pwd_getpwall(PyObject *self)
138 PyObject *d;
139 struct passwd *p;
140 if ((d = PyList_New(0)) == NULL)
141 return NULL;
142 #if defined(PYOS_OS2) && defined(PYCC_GCC)
143 if ((p = getpwuid(0)) != NULL) {
144 #else
145 setpwent();
146 while ((p = getpwent()) != NULL) {
147 #endif
148 PyObject *v = mkpwent(p);
149 if (v == NULL || PyList_Append(d, v) != 0) {
150 Py_XDECREF(v);
151 Py_DECREF(d);
152 return NULL;
154 Py_DECREF(v);
156 endpwent();
157 return d;
159 #endif
161 static PyMethodDef pwd_methods[] = {
162 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
163 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
164 #ifdef HAVE_GETPWENT
165 {"getpwall", (PyCFunction)pwd_getpwall,
166 METH_NOARGS, pwd_getpwall__doc__},
167 #endif
168 {NULL, NULL} /* sentinel */
171 PyMODINIT_FUNC
172 initpwd(void)
174 PyObject *m;
175 m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
177 PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
178 Py_INCREF((PyObject *) &StructPwdType);
179 PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);