Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 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 | /* UNIX group file access module */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | #include "modsupport.h" |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <grp.h> |
| 32 | |
| 33 | static object *mkgrent(p) |
| 34 | struct group *p; |
| 35 | { |
| 36 | object *v, *w; |
| 37 | char **member; |
| 38 | if ((w = newlistobject(0)) == NULL) { |
| 39 | return NULL; |
| 40 | } |
| 41 | for (member = p->gr_mem; *member != NULL; member++) { |
| 42 | object *x = newstringobject(*member); |
| 43 | if (x == NULL || addlistitem(w, x) != 0) { |
| 44 | XDECREF(x); |
| 45 | DECREF(w); |
| 46 | return NULL; |
| 47 | } |
| 48 | } |
| 49 | v = mkvalue("(sslO)", |
| 50 | p->gr_name, |
| 51 | p->gr_passwd, |
| 52 | (long)p->gr_gid, |
| 53 | w); |
| 54 | DECREF(w); |
| 55 | return v; |
| 56 | } |
| 57 | |
| 58 | static object *grp_getgrgid(self, args) |
| 59 | object *self, *args; |
| 60 | { |
| 61 | int gid; |
| 62 | struct group *p; |
| 63 | if (!getintarg(args, &gid)) |
| 64 | return NULL; |
| 65 | if ((p = getgrgid(gid)) == NULL) { |
| 66 | err_setstr(KeyError, "getgrgid(): gid not found"); |
| 67 | return NULL; |
| 68 | } |
| 69 | return mkgrent(p); |
| 70 | } |
| 71 | |
| 72 | static object *grp_getgrnam(self, args) |
| 73 | object *self, *args; |
| 74 | { |
| 75 | char *name; |
| 76 | struct group *p; |
| 77 | if (!getstrarg(args, &name)) |
| 78 | return NULL; |
| 79 | if ((p = getgrnam(name)) == NULL) { |
| 80 | err_setstr(KeyError, "getgrnam(): name not found"); |
| 81 | return NULL; |
| 82 | } |
| 83 | return mkgrent(p); |
| 84 | } |
| 85 | |
| 86 | static object *grp_getgrall(self, args) |
| 87 | object *self, *args; |
| 88 | { |
| 89 | object *d; |
| 90 | struct group *p; |
| 91 | if (!getnoarg(args)) |
| 92 | return NULL; |
| 93 | if ((d = newlistobject(0)) == NULL) |
| 94 | return NULL; |
| 95 | setgrent(); |
| 96 | while ((p = getgrent()) != NULL) { |
| 97 | object *v = mkgrent(p); |
| 98 | if (v == NULL || addlistitem(d, v) != 0) { |
| 99 | XDECREF(v); |
| 100 | DECREF(d); |
| 101 | return NULL; |
| 102 | } |
| 103 | } |
| 104 | return d; |
| 105 | } |
| 106 | |
| 107 | static struct methodlist grp_methods[] = { |
| 108 | {"getgrgid", grp_getgrgid}, |
| 109 | {"getgrnam", grp_getgrnam}, |
| 110 | {"getgrall", grp_getgrall}, |
| 111 | {NULL, NULL} /* sentinel */ |
| 112 | }; |
| 113 | |
| 114 | void |
| 115 | initgrp() |
| 116 | { |
| 117 | initmodule("grp", grp_methods); |
| 118 | } |