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

  1.  
  2. /* UNIX group file access module */
  3.  
  4. #include "Python.h"
  5.  
  6. #include <sys/types.h>
  7. #include <grp.h>
  8.  
  9. #ifdef AMITCP
  10. #include <proto/usergroup.h>
  11. #endif
  12. #ifdef INET225
  13. #include <proto/socket.h>
  14. #endif
  15.  
  16.  
  17. static PyObject *
  18. mkgrent(struct group *p)
  19. {
  20.     PyObject *v, *w;
  21.     char **member;
  22.     if ((w = PyList_New(0)) == NULL) {
  23.         return NULL;
  24.     }
  25.     for (member = p->gr_mem; *member != NULL; member++) {
  26.         PyObject *x = PyString_FromString(*member);
  27.         if (x == NULL || PyList_Append(w, x) != 0) {
  28.             Py_XDECREF(x);
  29.             Py_DECREF(w);
  30.             return NULL;
  31.         }
  32.         Py_DECREF(x);
  33.     }
  34.     v = Py_BuildValue("(sslO)",
  35.                       p->gr_name,
  36.                       p->gr_passwd,
  37. #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
  38. /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
  39.    for later versions you may have to remove this */
  40.                       (long)p->gr_short_pad, /* ugh-NeXT broke the padding */
  41. #else
  42.                       (long)p->gr_gid,
  43. #endif
  44.                       w);
  45.     Py_DECREF(w);
  46.     return v;
  47. }
  48.  
  49. static PyObject *
  50. grp_getgrgid(PyObject *self, PyObject *args)
  51. {
  52.     int gid;
  53.     struct group *p;
  54.     if (!PyArg_ParseTuple(args, "i:getgrgid", &gid))
  55.         return NULL;
  56.     if ((p = getgrgid(gid)) == NULL) {
  57.         PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
  58.         return NULL;
  59.     }
  60.     return mkgrent(p);
  61. }
  62.  
  63. static PyObject *
  64. grp_getgrnam(PyObject *self, PyObject *args)
  65. {
  66.     char *name;
  67.     struct group *p;
  68.     if (!PyArg_ParseTuple(args, "s:getgrnam", &name))
  69.         return NULL;
  70.     if ((p = getgrnam(name)) == NULL) {
  71.         PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
  72.         return NULL;
  73.     }
  74.     return mkgrent(p);
  75. }
  76.  
  77. static PyObject *
  78. grp_getgrall(PyObject *self, PyObject *args)
  79. {
  80.     PyObject *d;
  81.     struct group *p;
  82.  
  83.     if (!PyArg_ParseTuple(args, ":getgrall"))
  84.         return NULL;
  85.     if ((d = PyList_New(0)) == NULL)
  86.         return NULL;
  87. #if !defined(AMITCP) && !defined(INET225)
  88.     setgrent();
  89.     while ((p = getgrent()) != NULL) {
  90.         PyObject *v = mkgrent(p);
  91.         if (v == NULL || PyList_Append(d, v) != 0) {
  92.             Py_XDECREF(v);
  93.             Py_DECREF(d);
  94.             return NULL;
  95.         }
  96.         Py_DECREF(v);
  97.     }
  98.     return d;
  99. #else
  100.  #ifdef AMITCP
  101.     setgrent();
  102.  #else
  103.     setgrent(1); /* INET225 wants argument XXX correct? */
  104.  #endif
  105.     while ((p = getgrent()) != NULL) {
  106.         PyObject *v = mkgrent(p);
  107.         if (v == NULL || PyList_Append(d, v) != 0) {
  108.             Py_XDECREF(v);
  109.             Py_DECREF(d);
  110.             endgrent();
  111.             return NULL;
  112.         }
  113.         Py_DECREF(v);
  114.     }
  115.     endgrent();
  116.     return d;
  117. #endif /* AMITCP or INET225 */
  118. }
  119.  
  120. static PyMethodDef grp_methods[] = {
  121.     {"getgrgid",    grp_getgrgid,    METH_VARARGS,
  122.      "getgrgid(id) -> tuple\n\
  123. Return the group database entry for the given numeric group ID.  If\n\
  124. id is not valid, raise KeyError."},
  125.     {"getgrnam",    grp_getgrnam,    METH_VARARGS,
  126.      "getgrnam(name) -> tuple\n\
  127. Return the group database entry for the given group name.  If\n\
  128. name is not valid, raise KeyError."},
  129.     {"getgrall",    grp_getgrall,    METH_VARARGS,
  130.      "getgrall() -> list of tuples\n\
  131. Return a list of all available group entries, in arbitrary order."},
  132.     {NULL,        NULL}        /* sentinel */
  133. };
  134.  
  135. static char grp__doc__[] =
  136. "Access to the Unix group database.\n\
  137. \n\
  138. Group entries are reported as 4-tuples containing the following fields\n\
  139. from the group database, in order:\n\
  140. \n\
  141.   name   - name of the group\n\
  142.   passwd - group password (encrypted); often empty\n\
  143.   gid    - numeric ID of the group\n\
  144.   mem    - list of members\n\
  145. \n\
  146. The gid is an integer, name and password are strings.  (Note that most\n\
  147. users are not explicitly listed as members of the groups they are in\n\
  148. according to the password database.  Check both databases to get\n\
  149. complete membership information.)";
  150.  
  151.  
  152. DL_EXPORT(void)
  153. initgrp(void)
  154. {
  155.     Py_InitModule3("grp", grp_methods, grp__doc__);
  156. }
  157.