home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / pwdmodule.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  5KB  |  173 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* UNIX password file access module */
  33.  
  34. #include "Python.h"
  35.  
  36. #include <sys/types.h>
  37. #include <pwd.h>
  38.  
  39. static char pwd__doc__ [] = "\
  40. This module provides access to the Unix password database.\n\
  41. It is available on all Unix versions.\n\
  42. \n\
  43. Password database entries are reported as 7-tuples containing the following\n\
  44. items from the password database (see `<pwd.h>'), in order:\n\
  45. pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
  46. The uid and gid items are integers, all others are strings. An\n\
  47. exception is raised if the entry asked for cannot be found.";
  48.  
  49.       
  50. static PyObject *
  51. mkpwent(p)
  52.     struct passwd *p;
  53. {
  54. #ifdef __BEOS__
  55.     /* For faking the GECOS field. - [cjh] */
  56.     char *be_user = NULL;
  57.  
  58.     be_user = getenv( "USER" );
  59. #endif
  60.  
  61.     return Py_BuildValue(
  62.         "(ssllsss)",
  63.         p->pw_name,
  64.         p->pw_passwd,
  65. #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
  66. /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
  67.    for later versions you may have to remove this */
  68.         (long)p->pw_short_pad1,         /* ugh-NeXT broke the padding */
  69.         (long)p->pw_short_pad2,
  70. #else
  71.         (long)p->pw_uid,
  72.         (long)p->pw_gid,
  73. #endif
  74. #ifdef __BEOS__
  75. /* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
  76.         be_user ? be_user : "baron",
  77. #else
  78.         p->pw_gecos,
  79. #endif
  80.         p->pw_dir,
  81.         p->pw_shell);
  82. }
  83.  
  84. static char pwd_getpwuid__doc__[] = "\
  85. getpwuid(uid) -> entry\n\
  86. Return the password database entry for the given numeric user ID.\n\
  87. See pwd.__doc__ for more on password database entries.";
  88.  
  89. static PyObject *
  90. pwd_getpwuid(self, args)
  91.     PyObject *self;
  92.     PyObject *args;
  93. {
  94.     int uid;
  95.     struct passwd *p;
  96.     if (!PyArg_Parse(args, "i", &uid))
  97.         return NULL;
  98.     if ((p = getpwuid(uid)) == NULL) {
  99.         PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
  100.         return NULL;
  101.     }
  102.     return mkpwent(p);
  103. }
  104.  
  105. static char pwd_getpwnam__doc__[] = "\
  106. getpwnam(name) -> entry\n\
  107. Return the password database entry for the given user name.\n\
  108. See pwd.__doc__ for more on password database entries.";
  109.  
  110. static PyObject *
  111. pwd_getpwnam(self, args)
  112.     PyObject *self;
  113.     PyObject *args;
  114. {
  115.     char *name;
  116.     struct passwd *p;
  117.     if (!PyArg_Parse(args, "s", &name))
  118.         return NULL;
  119.     if ((p = getpwnam(name)) == NULL) {
  120.         PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
  121.         return NULL;
  122.     }
  123.     return mkpwent(p);
  124. }
  125.  
  126. #ifdef HAVE_GETPWENT
  127. static char pwd_getpwall__doc__[] = "\
  128. getpwall() -> list_of_entries\n\
  129. Return a list of all available password database entries, \
  130. in arbitrary order.\n\
  131. See pwd.__doc__ for more on password database entries.";
  132.  
  133. static PyObject *
  134. pwd_getpwall(self, args)
  135.     PyObject *self;
  136.     PyObject *args;
  137. {
  138.     PyObject *d;
  139.     struct passwd *p;
  140.     if (!PyArg_NoArgs(args))
  141.         return NULL;
  142.     if ((d = PyList_New(0)) == NULL)
  143.         return NULL;
  144.     setpwent();
  145.     while ((p = getpwent()) != NULL) {
  146.         PyObject *v = mkpwent(p);
  147.         if (v == NULL || PyList_Append(d, v) != 0) {
  148.             Py_XDECREF(v);
  149.             Py_DECREF(d);
  150.             return NULL;
  151.         }
  152.         Py_DECREF(v);
  153.     }
  154.     return d;
  155. }
  156. #endif
  157.  
  158. static PyMethodDef pwd_methods[] = {
  159.     {"getpwuid",    pwd_getpwuid, 0, pwd_getpwuid__doc__},
  160.     {"getpwnam",    pwd_getpwnam, 0, pwd_getpwnam__doc__},
  161. #ifdef HAVE_GETPWENT
  162.     {"getpwall",    pwd_getpwall, 0, pwd_getpwall__doc__},
  163. #endif
  164.     {NULL,        NULL}        /* sentinel */
  165. };
  166.  
  167. DL_EXPORT(void)
  168. initpwd()
  169. {
  170.     Py_InitModule4("pwd", pwd_methods, pwd__doc__,
  171.                        (PyObject *)NULL, PYTHON_API_VERSION);
  172. }
  173.