home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / pwdmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  3.5 KB  |  151 lines

  1.  
  2. /* UNIX password file access module */
  3.  
  4. #include "Python.h"
  5.  
  6. #include <sys/types.h>
  7. #include <pwd.h>
  8.  
  9. #ifdef AMITCP
  10. #include <proto/usergroup.h>
  11. #endif
  12. #ifdef INET225
  13. #include <proto/socket.h>
  14. #endif
  15.  
  16. static char pwd__doc__ [] = "\
  17. This module provides access to the Unix password database.\n\
  18. It is available on all Unix versions.\n\
  19. \n\
  20. Password database entries are reported as 7-tuples containing the following\n\
  21. items from the password database (see `<pwd.h>'), in order:\n\
  22. pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
  23. The uid and gid items are integers, all others are strings. An\n\
  24. exception is raised if the entry asked for cannot be found.";
  25.  
  26.       
  27. static PyObject *
  28. mkpwent(struct passwd *p)
  29. {
  30.     return Py_BuildValue(
  31.         "(ssllsss)",
  32.         p->pw_name,
  33.         p->pw_passwd,
  34. #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
  35. /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
  36.    for later versions you may have to remove this */
  37.         (long)p->pw_short_pad1,         /* ugh-NeXT broke the padding */
  38.         (long)p->pw_short_pad2,
  39. #else
  40.         (long)p->pw_uid,
  41.         (long)p->pw_gid,
  42. #endif
  43.         p->pw_gecos,
  44.         p->pw_dir,
  45.         p->pw_shell);
  46. }
  47.  
  48. static char pwd_getpwuid__doc__[] = "\
  49. getpwuid(uid) -> entry\n\
  50. Return the password database entry for the given numeric user ID.\n\
  51. See pwd.__doc__ for more on password database entries.";
  52.  
  53. static PyObject *
  54. pwd_getpwuid(PyObject *self, PyObject *args)
  55. {
  56.     int uid;
  57.     struct passwd *p;
  58.     if (!PyArg_Parse(args, "i", &uid))
  59.         return NULL;
  60.     if ((p = getpwuid(uid)) == NULL) {
  61.         PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
  62.         return NULL;
  63.     }
  64.     return mkpwent(p);
  65. }
  66.  
  67. static char pwd_getpwnam__doc__[] = "\
  68. getpwnam(name) -> entry\n\
  69. Return the password database entry for the given user name.\n\
  70. See pwd.__doc__ for more on password database entries.";
  71.  
  72. static PyObject *
  73. pwd_getpwnam(PyObject *self, PyObject *args)
  74. {
  75.     char *name;
  76.     struct passwd *p;
  77.     if (!PyArg_Parse(args, "s", &name))
  78.         return NULL;
  79.     if ((p = getpwnam(name)) == NULL) {
  80.         PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
  81.         return NULL;
  82.     }
  83.     return mkpwent(p);
  84. }
  85.  
  86. #ifdef HAVE_GETPWENT
  87. static char pwd_getpwall__doc__[] = "\
  88. getpwall() -> list_of_entries\n\
  89. Return a list of all available password database entries, \
  90. in arbitrary order.\n\
  91. See pwd.__doc__ for more on password database entries.";
  92.  
  93. static PyObject *
  94. pwd_getpwall(PyObject *self, PyObject *args)
  95. {
  96.     PyObject *d;
  97.     struct passwd *p;
  98.     if (!PyArg_NoArgs(args))
  99.         return NULL;
  100.     if ((d = PyList_New(0)) == NULL)
  101.         return NULL;
  102. #if !defined(AMITCP) && !defined(INET225)
  103.     setpwent();
  104.     while ((p = getpwent()) != NULL) {
  105.         PyObject *v = mkpwent(p);
  106.         if (v == NULL || PyList_Append(d, v) != 0) {
  107.             Py_XDECREF(v);
  108.             Py_DECREF(d);
  109.             return NULL;
  110.         }
  111.         Py_DECREF(v);
  112.     }
  113.     return d;
  114. #else
  115.  #ifdef AMITCP
  116.     setpwent();
  117.  #else 
  118.     setpwent(1); /* INET225 wants argument XXX correct? - I.J. */
  119.  #endif
  120.     while ((p = getpwent()) != NULL) {
  121.         PyObject *v = mkpwent(p);
  122.         if (v == NULL || PyList_Append(d, v) != 0) {
  123.             Py_XDECREF(v);
  124.             Py_DECREF(d);
  125.             endpwent();
  126.             return NULL;
  127.         }
  128.         Py_DECREF(v);
  129.     }
  130.     endpwent();
  131.     return d;
  132. #endif /* AMITCP or INET225 */
  133. }
  134. #endif
  135.  
  136. static PyMethodDef pwd_methods[] = {
  137.     {"getpwuid",    pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
  138.     {"getpwnam",    pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
  139. #ifdef HAVE_GETPWENT
  140.     {"getpwall",    pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
  141. #endif
  142.     {NULL,        NULL}        /* sentinel */
  143. };
  144.  
  145. DL_EXPORT(void)
  146. initpwd(void)
  147. {
  148.     Py_InitModule4("pwd", pwd_methods, pwd__doc__,
  149.                        (PyObject *)NULL, PYTHON_API_VERSION);
  150. }
  151.