home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / OS2 / Modules / pwdmodule.c < prev    next >
C/C++ Source or Header  |  1994-06-02  |  5KB  |  206 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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 not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Passwd/group file access module */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29.  
  30. #include <sys/types.h>
  31. #include <pwd.h>
  32. #ifdef HAVE_GRP_H
  33. #include <grp.h>
  34. #endif
  35.  
  36.  
  37. /* Module pwd */
  38.  
  39.  
  40. static object *mkpwent(p)
  41.     struct passwd *p;
  42. {
  43.     return mkvalue("(ssllsss)",
  44.                p->pw_name,
  45.                p->pw_passwd,
  46.                (long)p->pw_uid,
  47.                (long)p->pw_gid,
  48.                p->pw_gecos,
  49.                p->pw_dir,
  50.                p->pw_shell);
  51. }
  52.  
  53. static object *pwd_getpwuid(self, args)
  54.     object *self, *args;
  55. {
  56.     int uid;
  57.     struct passwd *p;
  58.     if (!getintarg(args, &uid))
  59.         return NULL;
  60.     if ((p = getpwuid(uid)) == NULL) {
  61.         err_setstr(KeyError, "getpwuid(): uid not found");
  62.         return NULL;
  63.     }
  64.     return mkpwent(p);
  65. }
  66.  
  67. static object *pwd_getpwnam(self, args)
  68.     object *self, *args;
  69. {
  70.     char *name;
  71.     struct passwd *p;
  72.     if (!getstrarg(args, &name))
  73.         return NULL;
  74.     if ((p = getpwnam(name)) == NULL) {
  75.         err_setstr(KeyError, "getpwnam(): name not found");
  76.         return NULL;
  77.     }
  78.     return mkpwent(p);
  79. }
  80.  
  81. static object *pwd_getpwall(self, args)
  82.     object *self, *args;
  83. {
  84.     object *d;
  85.     struct passwd *p;
  86.     if (!getnoarg(args))
  87.         return NULL;
  88.     if ((d = newlistobject(0)) == NULL)
  89.         return NULL;
  90.     setpwent();
  91.     while ((p = getpwent()) != NULL) {
  92.         object *v = mkpwent(p);
  93.         if (v == NULL || addlistitem(d, v) != 0) {
  94.             XDECREF(v);
  95.             DECREF(d);
  96.             return NULL;
  97.         }
  98.     }
  99.     return d;
  100. }
  101.  
  102. static struct methodlist pwd_methods[] = {
  103.     {"getpwuid",    pwd_getpwuid},
  104.     {"getpwnam",    pwd_getpwnam},
  105.     {"getpwall",    pwd_getpwall},
  106.     {NULL,        NULL}        /* sentinel */
  107. };
  108.  
  109. void
  110. initpwd()
  111. {
  112.     initmodule("pwd", pwd_methods);
  113. }
  114.  
  115.  
  116. /* Module grp */
  117.  
  118. #ifdef HAVE_GRP_H
  119. static object *mkgrent(p)
  120.     struct group *p;
  121. {
  122.     object *v, *w;
  123.     char **member;
  124.     if ((w = newlistobject(0)) == NULL) {
  125.         return NULL;
  126.     }
  127.     for (member = p->gr_mem; *member != NULL; member++) {
  128.         object *x = newstringobject(*member);
  129.         if (x == NULL || addlistitem(w, x) != 0) {
  130.             XDECREF(x);
  131.             DECREF(w);
  132.             return NULL;
  133.         }
  134.     }
  135.     v = mkvalue("(sslO)",
  136.                p->gr_name,
  137.                p->gr_passwd,
  138.                (long)p->gr_gid,
  139.                w);
  140.     DECREF(w);
  141.     return v;
  142. }
  143.  
  144. static object *grp_getgrgid(self, args)
  145.     object *self, *args;
  146. {
  147.     int gid;
  148.     struct group *p;
  149.     if (!getintarg(args, &gid))
  150.         return NULL;
  151.     if ((p = getgrgid(gid)) == NULL) {
  152.         err_setstr(KeyError, "getgrgid(): gid not found");
  153.         return NULL;
  154.     }
  155.     return mkgrent(p);
  156. }
  157.  
  158. static object *grp_getgrnam(self, args)
  159.     object *self, *args;
  160. {
  161.     char *name;
  162.     struct group *p;
  163.     if (!getstrarg(args, &name))
  164.         return NULL;
  165.     if ((p = getgrnam(name)) == NULL) {
  166.         err_setstr(KeyError, "getgrnam(): name not found");
  167.         return NULL;
  168.     }
  169.     return mkgrent(p);
  170. }
  171.  
  172. static object *grp_getgrall(self, args)
  173.     object *self, *args;
  174. {
  175.     object *d;
  176.     struct group *p;
  177.     if (!getnoarg(args))
  178.         return NULL;
  179.     if ((d = newlistobject(0)) == NULL)
  180.         return NULL;
  181.     setgrent();
  182.     while ((p = getgrent()) != NULL) {
  183.         object *v = mkgrent(p);
  184.         if (v == NULL || addlistitem(d, v) != 0) {
  185.             XDECREF(v);
  186.             DECREF(d);
  187.             return NULL;
  188.         }
  189.     }
  190.     return d;
  191. }
  192.  
  193. static struct methodlist grp_methods[] = {
  194.     {"getgrgid",    grp_getgrgid},
  195.     {"getgrnam",    grp_getgrnam},
  196.     {"getgrall",    grp_getgrall},
  197.     {NULL,        NULL}        /* sentinel */
  198. };
  199.  
  200. void
  201. initgrp()
  202. {
  203.     initmodule("grp", grp_methods);
  204. }
  205. #endif
  206.