blob: 07d12e74e6d893acedb33e3c7d59d54a188740d2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum775f4da1993-01-09 17:18:52 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* POSIX module implementation */
26
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000027#ifdef AMOEBA
28#define NO_LSTAT
29#define SYSV
30#endif
31
Guido van Rossumc2670a01992-09-13 20:07:29 +000032#ifdef __sgi
33#define DO_PG
34#endif
35
Guido van Rossumc70b61f1993-11-01 16:23:18 +000036#ifdef _NEXT_SOURCE
37#define mode_t int
38#define NO_UNAME
39#endif
40
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041#include <signal.h>
42#include <string.h>
43#include <setjmp.h>
44#include <sys/types.h>
45#include <sys/stat.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000046
Guido van Rossum22db57e1992-04-05 14:25:30 +000047#ifdef DO_TIMES
48#include <sys/times.h>
49#include <sys/param.h>
50#include <errno.h>
51#endif
52
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053#ifdef SYSV
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000054
Guido van Rossumb376a4a1993-11-23 17:53:17 +000055#define UTIME_STRUCT 1
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056#include <dirent.h>
57#define direct dirent
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000058#ifdef i386
59#define mode_t int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000061
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000062#else /* !SYSV */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000063
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000064#include <sys/dir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000065
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000066#endif /* !SYSV */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067
Guido van Rossum50e61dc1992-03-27 17:22:31 +000068#ifndef NO_UNISTD
Guido van Rossum22db57e1992-04-05 14:25:30 +000069#include <unistd.h> /* Take this out and hope the best if it doesn't exist */
Guido van Rossum50e61dc1992-03-27 17:22:31 +000070#endif
71
Guido van Rossum3f5da241990-12-20 15:06:42 +000072#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000074#include "ceval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000076#ifdef _SEQUENT_
77#include <unistd.h>
78#else /* _SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000079/* XXX Aren't these always declared in unistd.h? */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000080extern int mkdir PROTO((const char *, mode_t));
81extern int chdir PROTO((const char *));
Guido van Rossume22e6441993-07-09 10:51:31 +000082extern int rmdir PROTO((const char *));
83extern int chmod PROTO((const char *, mode_t));
Guido van Rossumb376a4a1993-11-23 17:53:17 +000084extern char *getcwd(); /* No PROTO((char *, int)) -- non portable */
Guido van Rossume22e6441993-07-09 10:51:31 +000085extern char *strerror PROTO((int));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000086extern int link PROTO((const char *, const char *));
87extern int rename PROTO((const char *, const char *));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000088extern int stat PROTO((const char *, struct stat *));
89extern int unlink PROTO((const char *));
90extern int pclose PROTO((FILE *));
Guido van Rossuma3309961993-07-28 09:05:47 +000091#endif /* !_SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000092#ifdef NO_LSTAT
93#define lstat stat
94#else
95extern int lstat PROTO((const char *, struct stat *));
96extern int symlink PROTO((const char *, const char *));
97#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099
100/* Return a dictionary corresponding to the POSIX environment table */
101
102extern char **environ;
103
104static object *
105convertenviron()
106{
107 object *d;
108 char **e;
109 d = newdictobject();
110 if (d == NULL)
111 return NULL;
112 if (environ == NULL)
113 return d;
114 /* XXX This part ignores errors */
115 for (e = environ; *e != NULL; e++) {
116 object *v;
117 char *p = strchr(*e, '=');
118 if (p == NULL)
119 continue;
120 v = newstringobject(p+1);
121 if (v == NULL)
122 continue;
123 *p = '\0';
124 (void) dictinsert(d, *e, v);
125 *p = '=';
126 DECREF(v);
127 }
128 return d;
129}
130
131
132static object *PosixError; /* Exception posix.error */
133
134/* Set a POSIX-specific error from errno, and return NULL */
135
Guido van Rossum687dd131993-05-17 08:34:16 +0000136static object * posix_error() { return err_errno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137}
138
139
140/* POSIX generic methods */
141
142static object *
143posix_1str(args, func)
144 object *args;
145 int (*func) FPROTO((const char *));
146{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000147 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000148 int res;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000149 if (!getargs(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000151 BGN_SAVE
152 res = (*func)(path1);
153 END_SAVE
154 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return posix_error();
156 INCREF(None);
157 return None;
158}
159
160static object *
161posix_2str(args, func)
162 object *args;
163 int (*func) FPROTO((const char *, const char *));
164{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000165 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000166 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000167 if (!getargs(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000169 BGN_SAVE
170 res = (*func)(path1, path2);
171 END_SAVE
172 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 return posix_error();
174 INCREF(None);
175 return None;
176}
177
178static object *
179posix_strint(args, func)
180 object *args;
181 int (*func) FPROTO((const char *, int));
182{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000183 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000185 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000186 if (!getargs(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000188 BGN_SAVE
189 res = (*func)(path, i);
190 END_SAVE
191 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192 return posix_error();
193 INCREF(None);
194 return None;
195}
196
197static object *
198posix_do_stat(self, args, statfunc)
199 object *self;
200 object *args;
201 int (*statfunc) FPROTO((const char *, struct stat *));
202{
203 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000204 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000205 int res;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000206 if (!getargs(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000208 BGN_SAVE
209 res = (*statfunc)(path, &st);
210 END_SAVE
211 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212 return posix_error();
Guido van Rossum687dd131993-05-17 08:34:16 +0000213 return mkvalue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000214 (long)st.st_mode,
215 (long)st.st_ino,
216 (long)st.st_dev,
217 (long)st.st_nlink,
218 (long)st.st_uid,
219 (long)st.st_gid,
220 (long)st.st_size,
221 (long)st.st_atime,
222 (long)st.st_mtime,
223 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224}
225
226
227/* POSIX methods */
228
229static object *
230posix_chdir(self, args)
231 object *self;
232 object *args;
233{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234 return posix_1str(args, chdir);
235}
236
237static object *
238posix_chmod(self, args)
239 object *self;
240 object *args;
241{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242 return posix_strint(args, chmod);
243}
244
245static object *
246posix_getcwd(self, args)
247 object *self;
248 object *args;
249{
250 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000251 char *res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 if (!getnoarg(args))
253 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000254 BGN_SAVE
255 res = getcwd(buf, sizeof buf);
256 END_SAVE
257 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258 return posix_error();
259 return newstringobject(buf);
260}
261
262static object *
263posix_link(self, args)
264 object *self;
265 object *args;
266{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 return posix_2str(args, link);
268}
269
270static object *
271posix_listdir(self, args)
272 object *self;
273 object *args;
274{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000275 char *name;
276 object *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277 DIR *dirp;
278 struct direct *ep;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000279 if (!getargs(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281 BGN_SAVE
282 if ((dirp = opendir(name)) == NULL) {
283 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 if ((d = newlistobject(0)) == NULL) {
287 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000288 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 return NULL;
290 }
291 while ((ep = readdir(dirp)) != NULL) {
292 v = newstringobject(ep->d_name);
293 if (v == NULL) {
294 DECREF(d);
295 d = NULL;
296 break;
297 }
298 if (addlistitem(d, v) != 0) {
299 DECREF(v);
300 DECREF(d);
301 d = NULL;
302 break;
303 }
304 DECREF(v);
305 }
306 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000307 END_SAVE
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000308
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309 return d;
310}
311
312static object *
313posix_mkdir(self, args)
314 object *self;
315 object *args;
316{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317 return posix_strint(args, mkdir);
318}
319
Guido van Rossum775f4da1993-01-09 17:18:52 +0000320static object *
321posix_nice(self, args)
322 object *self;
323 object *args;
324{
325 int increment, value;
326
327 if (!getargs(args, "i", &increment))
328 return NULL;
329 value = nice(increment);
330 if (value == -1)
331 return posix_error();
332 return newintobject((long) value);
333}
Guido van Rossum775f4da1993-01-09 17:18:52 +0000334
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000335#if i386 && ! _SEQUENT_
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000336int
337rename(from, to)
338 char *from;
339 char *to;
340{
341 int status;
342 /* XXX Shouldn't this unlink the destination first? */
343 status = link(from, to);
344 if (status != 0)
345 return status;
346 return unlink(from);
347}
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000348#endif /* i386 && ! _SEQUENT_ */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000349
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350static object *
351posix_rename(self, args)
352 object *self;
353 object *args;
354{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 return posix_2str(args, rename);
356}
357
358static object *
359posix_rmdir(self, args)
360 object *self;
361 object *args;
362{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 return posix_1str(args, rmdir);
364}
365
366static object *
367posix_stat(self, args)
368 object *self;
369 object *args;
370{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371 return posix_do_stat(self, args, stat);
372}
373
374static object *
375posix_system(self, args)
376 object *self;
377 object *args;
378{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000379 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000380 long sts;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000381 if (!getargs(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000383 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000384 sts = system(command);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000385 END_SAVE
386 return newintobject(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
388
389static object *
390posix_umask(self, args)
391 object *self;
392 object *args;
393{
394 int i;
395 if (!getintarg(args, &i))
396 return NULL;
397 i = umask(i);
398 if (i < 0)
399 return posix_error();
400 return newintobject((long)i);
401}
402
403static object *
404posix_unlink(self, args)
405 object *self;
406 object *args;
407{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408 return posix_1str(args, unlink);
409}
410
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000411#ifndef NO_UNAME
412#include <sys/utsname.h>
413
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000414extern int uname PROTO((struct utsname *));
415
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000416static object *
417posix_uname(self, args)
418 object *self;
419 object *args;
420{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000421 struct utsname u;
422 object *v;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000423 int res;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000424 if (!getnoarg(args))
425 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000426 BGN_SAVE
427 res = uname(&u);
428 END_SAVE
429 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000430 return posix_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000431 return mkvalue("(sssss)",
432 u.sysname,
433 u.nodename,
434 u.release,
435 u.version,
436 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000437}
438#endif /* NO_UNAME */
439
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000440#ifdef UTIME_STRUCT
441#include <utime.h>
442#endif
443
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000445posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 object *self;
447 object *args;
448{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000449 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000450 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000451
452#ifdef UTIME_STRUCT
453 struct utimbuf buf;
454#define ATIME buf.actime
455#define MTIME buf.modtime
456#define UTIME_ARG &buf
457
458#else
459 time_t buf[2];
460#define ATIME buf[0]
461#define MTIME buf[1]
462#define UTIME_ARG buf
463#endif
464
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000465 if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 BGN_SAVE
468 res = utime(path, UTIME_ARG);
469 END_SAVE
470 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471 return posix_error();
472 INCREF(None);
473 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000474#undef UTIME_ARG
475#undef ATIME
476#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
Guido van Rossum85e3b011991-06-03 12:42:10 +0000479
Guido van Rossum3b066191991-06-04 19:40:25 +0000480/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000481
482static object *
483posix__exit(self, args)
484 object *self;
485 object *args;
486{
487 int sts;
488 if (!getintarg(args, &sts))
489 return NULL;
490 _exit(sts);
491 /* NOTREACHED */
492}
493
Guido van Rossum85e3b011991-06-03 12:42:10 +0000494static object *
Guido van Rossum89b33251993-10-22 14:26:06 +0000495posix_execv(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000496 object *self;
497 object *args;
498{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000499 char *path;
500 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000501 char **argvlist;
502 int i, argc;
503 object *(*getitem) PROTO((object *, int));
504
Guido van Rossum89b33251993-10-22 14:26:06 +0000505 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000506 argv is a list or tuple of strings. */
507
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000508 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000509 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000510 if (is_listobject(argv)) {
511 argc = getlistsize(argv);
512 getitem = getlistitem;
513 }
514 else if (is_tupleobject(argv)) {
515 argc = gettuplesize(argv);
516 getitem = gettupleitem;
517 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000518 else {
519 badarg:
520 err_badarg();
521 return NULL;
522 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000523
524 argvlist = NEW(char *, argc+1);
525 if (argvlist == NULL)
526 return NULL;
527 for (i = 0; i < argc; i++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000528 if (!getargs((*getitem)(argv, i), "s", &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000529 DEL(argvlist);
530 goto badarg;
531 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000532 }
533 argvlist[argc] = NULL;
534
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000535 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000536
537 /* If we get here it's definitely an error */
538
539 DEL(argvlist);
540 return posix_error();
541}
542
543static object *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000544posix_execve(self, args)
545 object *self;
546 object *args;
547{
548 char *path;
549 object *argv, *env;
550 char **argvlist;
551 char **envlist;
552 object *key, *val;
553 int i, pos, argc, envc;
554 object *(*getitem) PROTO((object *, int));
555
556 /* execve has three arguments: (path, argv, env), where
557 argv is a list or tuple of strings and env is a dictionary
558 like posix.environ. */
559
560 if (!getargs(args, "(sOO)", &path, &argv, &env))
561 return NULL;
562 if (is_listobject(argv)) {
563 argc = getlistsize(argv);
564 getitem = getlistitem;
565 }
566 else if (is_tupleobject(argv)) {
567 argc = gettuplesize(argv);
568 getitem = gettupleitem;
569 }
570 else {
571 err_setstr(TypeError, "argv must be tuple or list");
572 return NULL;
573 }
574 if (!is_dictobject(env)) {
575 err_setstr(TypeError, "env must be dictionary");
576 return NULL;
577 }
578
579 argvlist = NEW(char *, argc+1);
580 if (argvlist == NULL) {
581 err_nomem();
582 return NULL;
583 }
584 for (i = 0; i < argc; i++) {
585 if (!getargs((*getitem)(argv, i),
586 "s;argv must be list of strings",
587 &argvlist[i])) {
588 goto fail_1;
589 }
590 }
591 argvlist[argc] = NULL;
592
593 i = getmappingsize(env);
594 envlist = NEW(char *, i + 1);
595 if (envlist == NULL) {
596 err_nomem();
597 goto fail_1;
598 }
599 pos = 0;
600 envc = 0;
601 while (mappinggetnext(env, &pos, &key, &val)) {
602 char *p, *k, *v;
603 if (!getargs(key, "s;non-string key in env", &k) ||
604 !getargs(val, "s;non-string value in env", &v)) {
605 goto fail_2;
606 }
607 p = NEW(char, getstringsize(key) + getstringsize(val) + 2);
608 if (p == NULL) {
609 err_nomem();
610 goto fail_2;
611 }
612 sprintf(p, "%s=%s", k, v);
613 envlist[envc++] = p;
614 }
615 envlist[envc] = 0;
616
617 execve(path, argvlist, envlist);
618
619 /* If we get here it's definitely an error */
620
621 (void) posix_error();
622
623 fail_2:
624 while (--envc >= 0)
625 DEL(envlist[envc]);
626 DEL(envlist);
627 fail_1:
628 DEL(argvlist);
629
630 return NULL;
631}
632
633static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000634posix_fork(self, args)
635 object *self;
636 object *args;
637{
638 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000639 if (!getnoarg(args))
640 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000641 pid = fork();
642 if (pid == -1)
643 return posix_error();
644 return newintobject((long)pid);
645}
646
647static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000648posix_getegid(self, args)
649 object *self;
650 object *args;
651{
652 if (!getnoarg(args))
653 return NULL;
654 return newintobject((long)getegid());
655}
656
657static object *
658posix_geteuid(self, args)
659 object *self;
660 object *args;
661{
662 if (!getnoarg(args))
663 return NULL;
664 return newintobject((long)geteuid());
665}
666
667static object *
668posix_getgid(self, args)
669 object *self;
670 object *args;
671{
672 if (!getnoarg(args))
673 return NULL;
674 return newintobject((long)getgid());
675}
676
677static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000678posix_getpid(self, args)
679 object *self;
680 object *args;
681{
Guido van Rossum04814471991-06-04 20:23:49 +0000682 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000683 return NULL;
684 return newintobject((long)getpid());
685}
686
687static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000688posix_getpgrp(self, args)
689 object *self;
690 object *args;
691{
692 if (!getnoarg(args))
693 return NULL;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000694#ifdef SYSV
695 return newintobject((long)getpgrp());
696#else
Guido van Rossum971443b1991-06-07 13:59:29 +0000697 return newintobject((long)getpgrp(0));
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000698#endif
Guido van Rossum04814471991-06-04 20:23:49 +0000699}
700
701static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +0000702posix_setpgrp(self, args)
703 object *self;
704 object *args;
705{
706 if (!getnoarg(args))
707 return NULL;
708#ifdef SYSV
709 if (setpgrp() < 0)
710#else
711 if (setpgrp(0, 0) < 0)
712#endif
Guido van Rossum687dd131993-05-17 08:34:16 +0000713 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000714 INCREF(None);
715 return None;
716}
717
718static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000719posix_getppid(self, args)
720 object *self;
721 object *args;
722{
Guido van Rossum04814471991-06-04 20:23:49 +0000723 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000724 return NULL;
725 return newintobject((long)getppid());
726}
727
728static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000729posix_getuid(self, args)
730 object *self;
731 object *args;
732{
733 if (!getnoarg(args))
734 return NULL;
735 return newintobject((long)getuid());
736}
737
738static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000739posix_kill(self, args)
740 object *self;
741 object *args;
742{
743 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +0000744 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000745 return NULL;
746 if (kill(pid, sig) == -1)
747 return posix_error();
748 INCREF(None);
749 return None;
750}
751
752static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000753posix_popen(self, args)
754 object *self;
755 object *args;
756{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000757 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000758 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000759 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000760 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000761 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000762 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000763 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +0000764 if (fp == NULL)
765 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000766 /* From now on, ignore SIGPIPE and let the error checking
767 do the work. */
768 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000769 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000770}
771
772static object *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +0000773posix_setuid(self, args)
774 object *self;
775 object *args;
776{
777 int uid;
778 if (!getargs(args, "i", &uid))
779 return NULL;
780 if (setuid(uid) < 0)
781 return posix_error();
782 INCREF(None);
783 return None;
784}
785
786static object *
787posix_setgid(self, args)
788 object *self;
789 object *args;
790{
791 int gid;
792 if (!getargs(args, "i", &gid))
793 return NULL;
794 if (setgid(gid) < 0)
795 return posix_error();
796 INCREF(None);
797 return None;
798}
799
800static object *
Guido van Rossum21803b81992-08-09 12:55:27 +0000801posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000802 object *self;
803 object *args;
804{
Guido van Rossum85e3b011991-06-03 12:42:10 +0000805#ifdef NO_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +0000806 err_setstr(PosixError,
807 "posix.waitpid() not supported on this system");
808 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000809#else
Guido van Rossum21803b81992-08-09 12:55:27 +0000810 int pid, options, sts;
811 if (!getargs(args, "(ii)", &pid, &options))
812 return NULL;
813 BGN_SAVE
814 pid = waitpid(pid, &sts, options);
815 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +0000816 if (pid == -1)
817 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +0000818 else
819 return mkvalue("ii", pid, sts);
820#endif
821}
822
823static object *
824posix_wait(self, args)
825 object *self;
826 object *args;
827{
828 int pid, sts;
829 if (args != NULL)
830 return posix_waitpid(self, args); /* BW compat */
831 BGN_SAVE
832 pid = wait(&sts);
833 END_SAVE
834 if (pid == -1)
835 return posix_error();
836 else
837 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000838}
839
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000840static object *
841posix_lstat(self, args)
842 object *self;
843 object *args;
844{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000845 return posix_do_stat(self, args, lstat);
846}
847
848static object *
849posix_readlink(self, args)
850 object *self;
851 object *args;
852{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000853#ifdef NO_LSTAT
854 err_setstr(PosixError, "readlink not implemented on this system");
855 return NULL;
856#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000858 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 int n;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000860 if (!getargs(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000862 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000863 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000864 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865 if (n < 0)
866 return posix_error();
867 return newsizedstringobject(buf, n);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000868#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869}
870
871static object *
872posix_symlink(self, args)
873 object *self;
874 object *args;
875{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000876#ifdef NO_LSTAT
877 err_setstr(PosixError, "symlink not implemented on this system");
878 return NULL;
879#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000880 return posix_2str(args, symlink);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000881#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000882}
883
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000884
Guido van Rossum22db57e1992-04-05 14:25:30 +0000885#ifdef DO_TIMES
886
887static object *
888posix_times(self, args)
889 object *self;
890 object *args;
891{
892 struct tms t;
893 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +0000894 if (!getnoarg(args))
895 return NULL;
896 errno = 0;
897 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +0000898 if (c == (clock_t) -1)
899 return posix_error();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000900 return mkvalue("dddd",
901 (double)t.tms_utime / HZ,
902 (double)t.tms_stime / HZ,
903 (double)t.tms_cutime / HZ,
904 (double)t.tms_cstime / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +0000905}
906
Guido van Rossumc2670a01992-09-13 20:07:29 +0000907#endif /* DO_TIMES */
908
909#ifdef DO_PG
910
911static object *
912posix_setsid(self, args)
913 object *self;
914 object *args;
915{
916 if (!getnoarg(args))
917 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000918 if (setsid() < 0)
919 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000920 INCREF(None);
921 return None;
922}
923
924static object *
925posix_setpgid(self, args)
926 object *self;
927 object *args;
928{
929 int pid, pgrp;
930 if (!getargs(args, "(ii)", &pid, &pgrp))
931 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000932 if (setpgid(pid, pgrp) < 0)
933 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000934 INCREF(None);
935 return None;
936}
937
Guido van Rossum7066dd71992-09-17 17:54:56 +0000938static object *
939posix_tcgetpgrp(self, args)
940 object *self;
941 object *args;
942{
943 int fd, pgid;
944 if (!getargs(args, "i", &fd))
945 return NULL;
946 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +0000947 if (pgid < 0)
948 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000949 return newintobject((long)pgid);
950}
951
952static object *
953posix_tcsetpgrp(self, args)
954 object *self;
955 object *args;
956{
957 int fd, pgid;
958 if (!getargs(args, "(ii)", &fd, &pgid))
959 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000960 if (tcsetpgrp(fd, pgid) < 0)
961 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000962 INCREF(None);
963 return None;
964}
965
Guido van Rossumc2670a01992-09-13 20:07:29 +0000966#endif /* DO_PG */
Guido van Rossum22db57e1992-04-05 14:25:30 +0000967
Guido van Rossum687dd131993-05-17 08:34:16 +0000968/* Functions acting on file descriptors */
969
Guido van Rossum234f9421993-06-17 12:35:49 +0000970static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000971posix_open(self, args)
972 object *self;
973 object *args;
974{
975 char *file;
976 int flag;
977 int mode = 0777;
978 int fd;
979 if (!getargs(args, "(si)", &file, &flag)) {
980 err_clear();
981 if (!getargs(args, "(sii)", &file, &flag, &mode))
982 return NULL;
983 }
984 BGN_SAVE
985 fd = open(file, flag, mode);
986 END_SAVE
987 if (fd < 0)
988 return posix_error();
989 return newintobject((long)fd);
990}
991
Guido van Rossum234f9421993-06-17 12:35:49 +0000992static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000993posix_close(self, args)
994 object *self;
995 object *args;
996{
997 int fd, res;
998 if (!getargs(args, "i", &fd))
999 return NULL;
1000 BGN_SAVE
1001 res = close(fd);
1002 END_SAVE
1003 if (res < 0)
1004 return posix_error();
1005 INCREF(None);
1006 return None;
1007}
1008
Guido van Rossum234f9421993-06-17 12:35:49 +00001009static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001010posix_dup(self, args)
1011 object *self;
1012 object *args;
1013{
1014 int fd;
1015 if (!getargs(args, "i", &fd))
1016 return NULL;
1017 BGN_SAVE
1018 fd = dup(fd);
1019 END_SAVE
1020 if (fd < 0)
1021 return posix_error();
1022 return newintobject((long)fd);
1023}
1024
Guido van Rossum234f9421993-06-17 12:35:49 +00001025static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001026posix_dup2(self, args)
1027 object *self;
1028 object *args;
1029{
1030 int fd, fd2, res;
1031 if (!getargs(args, "(ii)", &fd, &fd2))
1032 return NULL;
1033 BGN_SAVE
1034 res = dup2(fd, fd2);
1035 END_SAVE
1036 if (res < 0)
1037 return posix_error();
1038 INCREF(None);
1039 return None;
1040}
1041
Guido van Rossum234f9421993-06-17 12:35:49 +00001042static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001043posix_lseek(self, args)
1044 object *self;
1045 object *args;
1046{
1047 int fd, how;
1048 long pos, res;
1049 if (!getargs(args, "(ili)", &fd, &pos, &how))
1050 return NULL;
1051#ifdef SEEK_SET
1052 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1053 switch (how) {
1054 case 0: how = SEEK_SET; break;
1055 case 1: how = SEEK_CUR; break;
1056 case 2: how = SEEK_END; break;
1057 }
1058#endif
1059 BGN_SAVE
1060 res = lseek(fd, pos, how);
1061 END_SAVE
1062 if (res < 0)
1063 return posix_error();
1064 return newintobject(res);
1065}
1066
Guido van Rossum234f9421993-06-17 12:35:49 +00001067static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001068posix_read(self, args)
1069 object *self;
1070 object *args;
1071{
1072 int fd, size;
1073 object *buffer;
1074 if (!getargs(args, "(ii)", &fd, &size))
1075 return NULL;
1076 buffer = newsizedstringobject((char *)NULL, size);
1077 if (buffer == NULL)
1078 return NULL;
1079 BGN_SAVE
1080 size = read(fd, getstringvalue(buffer), size);
1081 END_SAVE
1082 if (size < 0) {
1083 DECREF(buffer);
1084 return posix_error();
1085 }
1086 resizestring(&buffer, size);
1087 return buffer;
1088}
1089
Guido van Rossum234f9421993-06-17 12:35:49 +00001090static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001091posix_write(self, args)
1092 object *self;
1093 object *args;
1094{
1095 int fd, size;
1096 char *buffer;
1097 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1098 return NULL;
1099 BGN_SAVE
1100 size = write(fd, buffer, size);
1101 END_SAVE
1102 if (size < 0)
1103 return posix_error();
1104 return newintobject((long)size);
1105}
1106
Guido van Rossum234f9421993-06-17 12:35:49 +00001107static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001108posix_fstat(self, args)
1109 object *self;
1110 object *args;
1111{
1112 int fd;
1113 struct stat st;
1114 int res;
1115 if (!getargs(args, "i", &fd))
1116 return NULL;
1117 BGN_SAVE
1118 res = fstat(fd, &st);
1119 END_SAVE
1120 if (res != 0)
1121 return posix_error();
1122 return mkvalue("(llllllllll)",
1123 (long)st.st_mode,
1124 (long)st.st_ino,
1125 (long)st.st_dev,
1126 (long)st.st_nlink,
1127 (long)st.st_uid,
1128 (long)st.st_gid,
1129 (long)st.st_size,
1130 (long)st.st_atime,
1131 (long)st.st_mtime,
1132 (long)st.st_ctime);
1133}
1134
1135static object *
1136posix_fdopen(self, args)
1137 object *self;
1138 object *args;
1139{
1140 extern int fclose PROTO((FILE *));
1141 int fd;
1142 char *mode;
1143 FILE *fp;
1144 if (!getargs(args, "(is)", &fd, &mode))
1145 return NULL;
1146 BGN_SAVE
1147 fp = fdopen(fd, mode);
1148 END_SAVE
1149 if (fp == NULL)
1150 return posix_error();
1151 /* From now on, ignore SIGPIPE and let the error checking
1152 do the work. */
1153 (void) signal(SIGPIPE, SIG_IGN);
1154 return newopenfileobject(fp, "(fdopen)", mode, fclose);
1155}
1156
Guido van Rossum234f9421993-06-17 12:35:49 +00001157static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001158posix_pipe(self, args)
1159 object *self;
1160 object *args;
1161{
1162 int fds[2];
1163 int res;
1164 if (!getargs(args, ""))
1165 return NULL;
1166 BGN_SAVE
1167 res = pipe(fds);
1168 END_SAVE
1169 if (res != 0)
1170 return posix_error();
1171 return mkvalue("(ii)", fds[0], fds[1]);
1172}
Guido van Rossum22db57e1992-04-05 14:25:30 +00001173
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001174static struct methodlist posix_methods[] = {
1175 {"chdir", posix_chdir},
1176 {"chmod", posix_chmod},
1177 {"getcwd", posix_getcwd},
1178 {"link", posix_link},
1179 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001180 {"lstat", posix_lstat},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001181 {"mkdir", posix_mkdir},
Guido van Rossum775f4da1993-01-09 17:18:52 +00001182 {"nice", posix_nice},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001183 {"readlink", posix_readlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001184 {"rename", posix_rename},
1185 {"rmdir", posix_rmdir},
1186 {"stat", posix_stat},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001187 {"symlink", posix_symlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001188 {"system", posix_system},
1189 {"umask", posix_umask},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001190#ifndef NO_UNAME
1191 {"uname", posix_uname},
1192#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001193 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001194 {"utime", posix_utime},
Guido van Rossum22db57e1992-04-05 14:25:30 +00001195#ifdef DO_TIMES
1196 {"times", posix_times},
1197#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001198 {"_exit", posix__exit},
Guido van Rossum89b33251993-10-22 14:26:06 +00001199 {"execv", posix_execv},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001200 {"execve", posix_execve},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001201 {"fork", posix_fork},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001202 {"getegid", posix_getegid},
1203 {"geteuid", posix_geteuid},
1204 {"getgid", posix_getgid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001205 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +00001206 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001207 {"getppid", posix_getppid},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001208 {"getuid", posix_getuid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001209 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +00001210 {"popen", posix_popen},
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001211 {"setuid", posix_setuid},
1212 {"setgid", posix_setgid},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001213 {"setpgrp", posix_setpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001214 {"wait", posix_wait},
Guido van Rossum21803b81992-08-09 12:55:27 +00001215 {"waitpid", posix_waitpid},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001216#ifdef DO_PG
1217 {"setsid", posix_setsid},
1218 {"setpgid", posix_setpgid},
Guido van Rossum7066dd71992-09-17 17:54:56 +00001219 {"tcgetpgrp", posix_tcgetpgrp},
1220 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001221#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00001222 {"open", posix_open},
1223 {"close", posix_close},
1224 {"dup", posix_dup},
1225 {"dup2", posix_dup2},
1226 {"lseek", posix_lseek},
1227 {"read", posix_read},
1228 {"write", posix_write},
1229 {"fstat", posix_fstat},
1230 {"fdopen", posix_fdopen},
Guido van Rossum687dd131993-05-17 08:34:16 +00001231 {"pipe", posix_pipe},
Guido van Rossum687dd131993-05-17 08:34:16 +00001232
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001233 {NULL, NULL} /* Sentinel */
1234};
1235
1236
1237void
1238initposix()
1239{
1240 object *m, *d, *v;
1241
1242 m = initmodule("posix", posix_methods);
1243 d = getmoduledict(m);
1244
1245 /* Initialize posix.environ dictionary */
1246 v = convertenviron();
1247 if (v == NULL || dictinsert(d, "environ", v) != 0)
1248 fatal("can't define posix.environ");
1249 DECREF(v);
1250
1251 /* Initialize posix.error exception */
1252 PosixError = newstringobject("posix.error");
1253 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1254 fatal("can't define posix.error");
1255}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001256
Guido van Rossum3b066191991-06-04 19:40:25 +00001257
1258/* Function used elsewhere to get a file's modification time */
1259
1260long
1261getmtime(path)
1262 char *path;
1263{
1264 struct stat st;
1265 if (stat(path, &st) != 0)
1266 return -1;
1267 else
1268 return st.st_mtime;
1269}