22/* UNIX password file access module */
33
44#include "Python.h"
5+ #include "structseq.h"
56
67#include <sys/types.h>
78#include <pwd.h>
89
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 }
19+ };
20+
21+ static char 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.\n" ;
26+
27+ static PyStructSequence_Desc struct_pwd_type_desc = {
28+ "pwd.struct_passwd" ,
29+ struct_passwd__doc__ ,
30+ struct_pwd_type_fields ,
31+ 7 ,
32+ };
33+
934static char pwd__doc__ [] = "\
1035This module provides access to the Unix password database.\n\
1136It is available on all Unix versions.\n\
@@ -17,22 +42,40 @@ The uid and gid items are integers, all others are strings. An\n\
1742exception is raised if the entry asked for cannot be found." ;
1843
1944
45+ static PyTypeObject StructPwdType ;
46+
2047static PyObject *
2148mkpwent (struct passwd * p )
2249{
23- return Py_BuildValue (
24- "(ssllsss)" ,
25- p -> pw_name ,
26- p -> pw_passwd ,
27- (long )p -> pw_uid ,
28- (long )p -> pw_gid ,
29- p -> pw_gecos ,
30- p -> pw_dir ,
31- p -> pw_shell );
50+ int setIndex = 0 ;
51+ PyObject * v = PyStructSequence_New (& StructPwdType );
52+ if (v == NULL )
53+ return NULL ;
54+
55+ #define SETI (i ,val ) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
56+ #define SETS (i ,val ) PyStructSequence_SET_ITEM(v, i, PyString_FromString(val))
57+
58+ SETS (setIndex ++ , p -> pw_name );
59+ SETS (setIndex ++ , p -> pw_passwd );
60+ SETI (setIndex ++ , p -> pw_uid );
61+ SETI (setIndex ++ , p -> pw_gid );
62+ SETS (setIndex ++ , p -> pw_gecos );
63+ SETS (setIndex ++ , p -> pw_dir );
64+ SETS (setIndex ++ , p -> pw_shell );
65+
66+ #undef SETS
67+ #undef SETI
68+
69+ if (PyErr_Occurred ()) {
70+ Py_XDECREF (v );
71+ return NULL ;
72+ }
73+
74+ return v ;
3275}
3376
3477static char pwd_getpwuid__doc__ [] = "\
35- getpwuid(uid) -> entry \n\
78+ getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell) \n\
3679Return the password database entry for the given numeric user ID.\n\
3780See pwd.__doc__ for more on password database entries." ;
3881
@@ -51,7 +94,7 @@ pwd_getpwuid(PyObject *self, PyObject *args)
5194}
5295
5396static char pwd_getpwnam__doc__ [] = "\
54- getpwnam(name) -> entry \n\
97+ getpwnam(name) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell) \n\
5598Return the password database entry for the given user name.\n\
5699See pwd.__doc__ for more on password database entries." ;
57100
@@ -112,6 +155,10 @@ static PyMethodDef pwd_methods[] = {
112155DL_EXPORT (void )
113156initpwd (void )
114157{
115- Py_InitModule4 ("pwd" , pwd_methods , pwd__doc__ ,
158+ PyObject * m , * d ;
159+ m = Py_InitModule4 ("pwd" , pwd_methods , pwd__doc__ ,
116160 (PyObject * )NULL , PYTHON_API_VERSION );
161+ d = PyModule_GetDict (m );
162+ PyStructSequence_InitType (& StructPwdType , & struct_pwd_type_desc );
163+ PyDict_SetItemString (d , "struct_pwent" , (PyObject * ) & StructPwdType );
117164}
0 commit comments