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 int initialized
;
46 static PyTypeObject StructPwdType
;
49 sets(PyObject
*v
, int i
, const char* val
)
52 PyObject
*o
= PyUnicode_Decode(val
, strlen(val
),
53 Py_FileSystemDefaultEncoding
,
55 PyStructSequence_SET_ITEM(v
, i
, o
);
58 PyStructSequence_SET_ITEM(v
, i
, Py_None
);
64 mkpwent(struct passwd
*p
)
67 PyObject
*v
= PyStructSequence_New(&StructPwdType
);
71 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
72 #define SETS(i,val) sets(v, i, val)
74 SETS(setIndex
++, p
->pw_name
);
78 SETS(setIndex
++, p
->pw_passwd
);
80 SETI(setIndex
++, p
->pw_uid
);
81 SETI(setIndex
++, p
->pw_gid
);
85 SETS(setIndex
++, p
->pw_gecos
);
87 SETS(setIndex
++, p
->pw_dir
);
88 SETS(setIndex
++, p
->pw_shell
);
93 if (PyErr_Occurred()) {
101 PyDoc_STRVAR(pwd_getpwuid__doc__
,
102 "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
103 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
104 Return the password database entry for the given numeric user ID.\n\
105 See pwd.__doc__ for more on password database entries.");
108 pwd_getpwuid(PyObject
*self
, PyObject
*args
)
112 if (!PyArg_ParseTuple(args
, "I:getpwuid", &uid
))
114 if ((p
= getpwuid(uid
)) == NULL
) {
115 PyErr_Format(PyExc_KeyError
,
116 "getpwuid(): uid not found: %d", uid
);
122 PyDoc_STRVAR(pwd_getpwnam__doc__
,
123 "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
124 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
125 Return the password database entry for the given user name.\n\
126 See pwd.__doc__ for more on password database entries.");
129 pwd_getpwnam(PyObject
*self
, PyObject
*args
)
133 PyObject
*arg
, *bytes
, *retval
= NULL
;
135 if (!PyArg_ParseTuple(args
, "U:getpwnam", &arg
))
137 if ((bytes
= PyUnicode_AsEncodedString(arg
,
138 Py_FileSystemDefaultEncoding
,
139 "surrogateescape")) == NULL
)
141 if (PyBytes_AsStringAndSize(bytes
, &name
, NULL
) == -1)
143 if ((p
= getpwnam(name
)) == NULL
) {
144 PyErr_Format(PyExc_KeyError
,
145 "getpwnam(): name not found: %s", name
);
155 PyDoc_STRVAR(pwd_getpwall__doc__
,
156 "getpwall() -> list_of_entries\n\
157 Return a list of all available password database entries, \
158 in arbitrary order.\n\
159 See pwd.__doc__ for more on password database entries.");
162 pwd_getpwall(PyObject
*self
)
166 if ((d
= PyList_New(0)) == NULL
)
168 #if defined(PYOS_OS2) && defined(PYCC_GCC)
169 if ((p
= getpwuid(0)) != NULL
) {
172 while ((p
= getpwent()) != NULL
) {
174 PyObject
*v
= mkpwent(p
);
175 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
188 static PyMethodDef pwd_methods
[] = {
189 {"getpwuid", pwd_getpwuid
, METH_VARARGS
, pwd_getpwuid__doc__
},
190 {"getpwnam", pwd_getpwnam
, METH_VARARGS
, pwd_getpwnam__doc__
},
192 {"getpwall", (PyCFunction
)pwd_getpwall
,
193 METH_NOARGS
, pwd_getpwall__doc__
},
195 {NULL
, NULL
} /* sentinel */
198 static struct PyModuleDef pwdmodule
= {
199 PyModuleDef_HEAD_INIT
,
215 m
= PyModule_Create(&pwdmodule
);
220 PyStructSequence_InitType(&StructPwdType
,
221 &struct_pwd_type_desc
);
224 Py_INCREF((PyObject
*) &StructPwdType
);
225 PyModule_AddObject(m
, "struct_passwd", (PyObject
*) &StructPwdType
);