2 /* UNIX password file access module */
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"},
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
= {
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\
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
;
48 sets(PyObject
*v
, int i
, char* val
)
51 PyStructSequence_SET_ITEM(v
, i
, PyString_FromString(val
));
53 PyStructSequence_SET_ITEM(v
, i
, Py_None
);
59 mkpwent(struct passwd
*p
)
62 PyObject
*v
= PyStructSequence_New(&StructPwdType
);
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
);
73 SETS(setIndex
++, p
->pw_passwd
);
75 SETI(setIndex
++, p
->pw_uid
);
76 SETI(setIndex
++, p
->pw_gid
);
80 SETS(setIndex
++, p
->pw_gecos
);
82 SETS(setIndex
++, p
->pw_dir
);
83 SETS(setIndex
++, p
->pw_shell
);
88 if (PyErr_Occurred()) {
96 PyDoc_STRVAR(pwd_getpwuid__doc__
,
97 "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
98 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
99 Return the password database entry for the given numeric user ID.\n\
100 See pwd.__doc__ for more on password database entries.");
103 pwd_getpwuid(PyObject
*self
, PyObject
*args
)
107 if (!PyArg_ParseTuple(args
, "I:getpwuid", &uid
))
109 if ((p
= getpwuid(uid
)) == NULL
) {
110 PyErr_Format(PyExc_KeyError
,
111 "getpwuid(): uid not found: %d", uid
);
117 PyDoc_STRVAR(pwd_getpwnam__doc__
,
118 "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
119 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
120 Return the password database entry for the given user name.\n\
121 See pwd.__doc__ for more on password database entries.");
124 pwd_getpwnam(PyObject
*self
, PyObject
*args
)
128 if (!PyArg_ParseTuple(args
, "s:getpwnam", &name
))
130 if ((p
= getpwnam(name
)) == NULL
) {
131 PyErr_Format(PyExc_KeyError
,
132 "getpwnam(): name not found: %s", name
);
139 PyDoc_STRVAR(pwd_getpwall__doc__
,
140 "getpwall() -> list_of_entries\n\
141 Return a list of all available password database entries, \
142 in arbitrary order.\n\
143 See pwd.__doc__ for more on password database entries.");
146 pwd_getpwall(PyObject
*self
)
150 if ((d
= PyList_New(0)) == NULL
)
152 #if defined(PYOS_OS2) && defined(PYCC_GCC)
153 if ((p
= getpwuid(0)) != NULL
) {
156 while ((p
= getpwent()) != NULL
) {
158 PyObject
*v
= mkpwent(p
);
159 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
171 static PyMethodDef pwd_methods
[] = {
172 {"getpwuid", pwd_getpwuid
, METH_VARARGS
, pwd_getpwuid__doc__
},
173 {"getpwnam", pwd_getpwnam
, METH_VARARGS
, pwd_getpwnam__doc__
},
175 {"getpwall", (PyCFunction
)pwd_getpwall
,
176 METH_NOARGS
, pwd_getpwall__doc__
},
178 {NULL
, NULL
} /* sentinel */
185 m
= Py_InitModule3("pwd", pwd_methods
, pwd__doc__
);
189 PyStructSequence_InitType(&StructPwdType
, &struct_pwd_type_desc
);
190 Py_INCREF((PyObject
*) &StructPwdType
);
191 PyModule_AddObject(m
, "struct_passwd", (PyObject
*) &StructPwdType
);
192 /* And for b/w compatibility (this was defined by mistake): */
193 PyModule_AddObject(m
, "struct_pwent", (PyObject
*) &StructPwdType
);