This commit was manufactured by cvs2svn to create tag 'release101'.
[python/dscho.git] / Modules / pwdmodule.c
blobf4b621d8a75ed9632b5dd17ca1664772207cced6
1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, 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 /* Passwd/group file access module */
27 #include "allobjects.h"
28 #include "modsupport.h"
30 #include <sys/types.h>
31 #include <pwd.h>
32 #include <grp.h>
35 /* Module pwd */
38 static object *mkpwent(p)
39 struct passwd *p;
41 return mkvalue("(ssllsss)",
42 p->pw_name,
43 p->pw_passwd,
44 (long)p->pw_uid,
45 (long)p->pw_gid,
46 p->pw_gecos,
47 p->pw_dir,
48 p->pw_shell);
51 static object *pwd_getpwuid(self, args)
52 object *self, *args;
54 int uid;
55 struct passwd *p;
56 if (!getintarg(args, &uid))
57 return NULL;
58 if ((p = getpwuid(uid)) == NULL) {
59 err_setstr(KeyError, "getpwuid(): uid not found");
60 return NULL;
62 return mkpwent(p);
65 static object *pwd_getpwnam(self, args)
66 object *self, *args;
68 char *name;
69 struct passwd *p;
70 if (!getstrarg(args, &name))
71 return NULL;
72 if ((p = getpwnam(name)) == NULL) {
73 err_setstr(KeyError, "getpwnam(): name not found");
74 return NULL;
76 return mkpwent(p);
79 static object *pwd_getpwall(self, args)
80 object *self, *args;
82 object *d;
83 struct passwd *p;
84 if (!getnoarg(args))
85 return NULL;
86 if ((d = newlistobject(0)) == NULL)
87 return NULL;
88 setpwent();
89 while ((p = getpwent()) != NULL) {
90 object *v = mkpwent(p);
91 if (v == NULL || addlistitem(d, v) != 0) {
92 XDECREF(v);
93 DECREF(d);
94 return NULL;
97 return d;
100 static struct methodlist pwd_methods[] = {
101 {"getpwuid", pwd_getpwuid},
102 {"getpwnam", pwd_getpwnam},
103 {"getpwall", pwd_getpwall},
104 {NULL, NULL} /* sentinel */
107 void
108 initpwd()
110 initmodule("pwd", pwd_methods);
114 /* Module grp */
117 static object *mkgrent(p)
118 struct group *p;
120 object *v, *w;
121 char **member;
122 if ((w = newlistobject(0)) == NULL) {
123 return NULL;
125 for (member = p->gr_mem; *member != NULL; member++) {
126 object *x = newstringobject(*member);
127 if (x == NULL || addlistitem(w, x) != 0) {
128 XDECREF(x);
129 DECREF(w);
130 return NULL;
133 v = mkvalue("(sslO)",
134 p->gr_name,
135 p->gr_passwd,
136 (long)p->gr_gid,
138 DECREF(w);
139 return v;
142 static object *grp_getgrgid(self, args)
143 object *self, *args;
145 int gid;
146 struct group *p;
147 if (!getintarg(args, &gid))
148 return NULL;
149 if ((p = getgrgid(gid)) == NULL) {
150 err_setstr(KeyError, "getgrgid(): gid not found");
151 return NULL;
153 return mkgrent(p);
156 static object *grp_getgrnam(self, args)
157 object *self, *args;
159 char *name;
160 struct group *p;
161 if (!getstrarg(args, &name))
162 return NULL;
163 if ((p = getgrnam(name)) == NULL) {
164 err_setstr(KeyError, "getgrnam(): name not found");
165 return NULL;
167 return mkgrent(p);
170 static object *grp_getgrall(self, args)
171 object *self, *args;
173 object *d;
174 struct group *p;
175 if (!getnoarg(args))
176 return NULL;
177 if ((d = newlistobject(0)) == NULL)
178 return NULL;
179 setgrent();
180 while ((p = getgrent()) != NULL) {
181 object *v = mkgrent(p);
182 if (v == NULL || addlistitem(d, v) != 0) {
183 XDECREF(v);
184 DECREF(d);
185 return NULL;
188 return d;
191 static struct methodlist grp_methods[] = {
192 {"getgrgid", grp_getgrgid},
193 {"getgrnam", grp_getgrnam},
194 {"getgrall", grp_getgrall},
195 {NULL, NULL} /* sentinel */
198 void
199 initgrp()
201 initmodule("grp", grp_methods);