Use py_resource module
[python/dscho.git] / Modules / grpmodule.c
blobcff9d83f35a5ab482b3d05f19c1fa032fbfef180
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* UNIX group file access module */
27 #include "allobjects.h"
28 #include "modsupport.h"
30 #include <sys/types.h>
31 #include <grp.h>
33 static object *mkgrent(p)
34 struct group *p;
36 object *v, *w;
37 char **member;
38 if ((w = newlistobject(0)) == NULL) {
39 return NULL;
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;
49 v = mkvalue("(sslO)",
50 p->gr_name,
51 p->gr_passwd,
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
57 (long)p->gr_gid,
58 #endif
59 w);
60 DECREF(w);
61 return v;
64 static object *grp_getgrgid(self, args)
65 object *self, *args;
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;
75 return mkgrent(p);
78 static object *grp_getgrnam(self, args)
79 object *self, *args;
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;
89 return mkgrent(p);
92 static object *grp_getgrall(self, args)
93 object *self, *args;
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;
110 return d;
113 static struct methodlist grp_methods[] = {
114 {"getgrgid", grp_getgrgid},
115 {"getgrnam", grp_getgrnam},
116 {"getgrall", grp_getgrall},
117 {NULL, NULL} /* sentinel */
120 void
121 initgrp()
123 initmodule("grp", grp_methods);