Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 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, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 52 | #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__) |
| 53 | /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3; |
| 54 | for later versions you may have to remove this */ |
| 55 | (long)p->gr_short_pad, /* ugh-NeXT broke the padding */ |
| 56 | #else |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 57 | (long)p->gr_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 58 | #endif |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 59 | w); |
| 60 | DECREF(w); |
| 61 | return v; |
| 62 | } |
| 63 | |
| 64 | static object *grp_getgrgid(self, args) |
| 65 | object *self, *args; |
| 66 | { |
| 67 | int gid; |
| 68 | struct group *p; |
| 69 | if (!getintarg(args, &gid)) |
| 70 | return NULL; |
| 71 | if ((p = getgrgid(gid)) == NULL) { |
| 72 | err_setstr(KeyError, "getgrgid(): gid not found"); |
| 73 | return NULL; |
| 74 | } |
| 75 | return mkgrent(p); |
| 76 | } |
| 77 | |
| 78 | static object *grp_getgrnam(self, args) |
| 79 | object *self, *args; |
| 80 | { |
| 81 | char *name; |
| 82 | struct group *p; |
| 83 | if (!getstrarg(args, &name)) |
| 84 | return NULL; |
| 85 | if ((p = getgrnam(name)) == NULL) { |
| 86 | err_setstr(KeyError, "getgrnam(): name not found"); |
| 87 | return NULL; |
| 88 | } |
| 89 | return mkgrent(p); |
| 90 | } |
| 91 | |
| 92 | static object *grp_getgrall(self, args) |
| 93 | object *self, *args; |
| 94 | { |
| 95 | object *d; |
| 96 | struct group *p; |
| 97 | if (!getnoarg(args)) |
| 98 | return NULL; |
| 99 | if ((d = newlistobject(0)) == NULL) |
| 100 | return NULL; |
| 101 | setgrent(); |
| 102 | while ((p = getgrent()) != NULL) { |
| 103 | object *v = mkgrent(p); |
| 104 | if (v == NULL || addlistitem(d, v) != 0) { |
| 105 | XDECREF(v); |
| 106 | DECREF(d); |
| 107 | return NULL; |
| 108 | } |
| 109 | } |
| 110 | return d; |
| 111 | } |
| 112 | |
| 113 | static struct methodlist grp_methods[] = { |
| 114 | {"getgrgid", grp_getgrgid}, |
| 115 | {"getgrnam", grp_getgrnam}, |
| 116 | {"getgrall", grp_getgrall}, |
| 117 | {NULL, NULL} /* sentinel */ |
| 118 | }; |
| 119 | |
| 120 | void |
| 121 | initgrp() |
| 122 | { |
| 123 | initmodule("grp", grp_methods); |
| 124 | } |