blob: ed1c3e6730eca1748edfd58850264f7093604986 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
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 Rossum0ee42cd1991-04-08 21:01:03 +000032#ifdef MSDOS
33#define NO_LSTAT
34#endif
35
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036#include <signal.h>
37#include <string.h>
38#include <setjmp.h>
39#include <sys/types.h>
40#include <sys/stat.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000041
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042#ifdef SYSV
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000043
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000044#define UTIME_STRUCT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045#include <dirent.h>
46#define direct dirent
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000047#ifdef i386
48#define mode_t int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000050
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000051#else /* !SYSV */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000052
53#ifndef MSDOS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000054#include <sys/dir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000055#endif
56
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000057#endif /* !SYSV */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058
Guido van Rossum3f5da241990-12-20 15:06:42 +000059#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060#include "modsupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061
Guido van Rossum7f77e2d1990-10-30 13:34:38 +000062extern char *strerror PROTO((int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064
65/* Return a dictionary corresponding to the POSIX environment table */
66
67extern char **environ;
68
69static object *
70convertenviron()
71{
72 object *d;
73 char **e;
74 d = newdictobject();
75 if (d == NULL)
76 return NULL;
77 if (environ == NULL)
78 return d;
79 /* XXX This part ignores errors */
80 for (e = environ; *e != NULL; e++) {
81 object *v;
82 char *p = strchr(*e, '=');
83 if (p == NULL)
84 continue;
85 v = newstringobject(p+1);
86 if (v == NULL)
87 continue;
88 *p = '\0';
89 (void) dictinsert(d, *e, v);
90 *p = '=';
91 DECREF(v);
92 }
93 return d;
94}
95
96
97static object *PosixError; /* Exception posix.error */
98
99/* Set a POSIX-specific error from errno, and return NULL */
100
Guido van Rossum9a1581c1990-10-21 13:12:47 +0000101static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102posix_error()
103{
Guido van Rossume8f305a1990-10-14 20:04:28 +0000104 return err_errno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105}
106
107
108/* POSIX generic methods */
109
110static object *
111posix_1str(args, func)
112 object *args;
113 int (*func) FPROTO((const char *));
114{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000115 char *path1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 if (!getstrarg(args, &path1))
117 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000118 if ((*func)(path1) < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 return posix_error();
120 INCREF(None);
121 return None;
122}
123
124static object *
125posix_2str(args, func)
126 object *args;
127 int (*func) FPROTO((const char *, const char *));
128{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000129 char *path1, *path2;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 if (!getstrstrarg(args, &path1, &path2))
131 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000132 if ((*func)(path1, path2) < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133 return posix_error();
134 INCREF(None);
135 return None;
136}
137
138static object *
139posix_strint(args, func)
140 object *args;
141 int (*func) FPROTO((const char *, int));
142{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000143 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 int i;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000145 if (!getstrintarg(args, &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000147 if ((*func)(path, i) < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return posix_error();
149 INCREF(None);
150 return None;
151}
152
153static object *
154posix_do_stat(self, args, statfunc)
155 object *self;
156 object *args;
157 int (*statfunc) FPROTO((const char *, struct stat *));
158{
159 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000160 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 object *v;
162 if (!getstrarg(args, &path))
163 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000164 if ((*statfunc)(path, &st) != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 return posix_error();
166 v = newtupleobject(10);
167 if (v == NULL)
168 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169#define SET(i, st_member) settupleitem(v, i, newintobject((long)st.st_member))
170 SET(0, st_mode);
171 SET(1, st_ino);
172 SET(2, st_dev);
173 SET(3, st_nlink);
174 SET(4, st_uid);
175 SET(5, st_gid);
176 SET(6, st_size);
177 SET(7, st_atime);
178 SET(8, st_mtime);
179 SET(9, st_ctime);
180#undef SET
Guido van Rossum3f5da241990-12-20 15:06:42 +0000181 if (err_occurred()) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182 DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000183 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184 }
185 return v;
186}
187
188
189/* POSIX methods */
190
191static object *
192posix_chdir(self, args)
193 object *self;
194 object *args;
195{
196 extern int chdir PROTO((const char *));
197 return posix_1str(args, chdir);
198}
199
200static object *
201posix_chmod(self, args)
202 object *self;
203 object *args;
204{
205 extern int chmod PROTO((const char *, mode_t));
206 return posix_strint(args, chmod);
207}
208
209static object *
210posix_getcwd(self, args)
211 object *self;
212 object *args;
213{
214 char buf[1026];
215 extern char *getcwd PROTO((char *, int));
216 if (!getnoarg(args))
217 return NULL;
218 if (getcwd(buf, sizeof buf) == NULL)
219 return posix_error();
220 return newstringobject(buf);
221}
222
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000223#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224static object *
225posix_link(self, args)
226 object *self;
227 object *args;
228{
229 extern int link PROTO((const char *, const char *));
230 return posix_2str(args, link);
231}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000232#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233
234static object *
235posix_listdir(self, args)
236 object *self;
237 object *args;
238{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000239 char *name;
240 object *d, *v;
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000241
242#ifdef MSDOS
243 struct ffblk ep;
244 int rv;
245 if (!getstrarg(args, &name))
246 return NULL;
247
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000248 if (findfirst(name, &ep, 0) == -1)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000249 return posix_error();
250 if ((d = newlistobject(0)) == NULL)
251 return NULL;
252 do {
253 v = newstringobject(ep.ff_name);
254 if (v == NULL) {
255 DECREF(d);
256 d = NULL;
257 break;
258 }
259 if (addlistitem(d, v) != 0) {
260 DECREF(v);
261 DECREF(d);
262 d = NULL;
263 break;
264 }
265 DECREF(v);
266 } while ((rv = findnext(&ep)) == 0);
267#else /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 DIR *dirp;
269 struct direct *ep;
270 if (!getstrarg(args, &name))
271 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000272 if ((dirp = opendir(name)) == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273 return posix_error();
274 if ((d = newlistobject(0)) == NULL) {
275 closedir(dirp);
276 return NULL;
277 }
278 while ((ep = readdir(dirp)) != NULL) {
279 v = newstringobject(ep->d_name);
280 if (v == NULL) {
281 DECREF(d);
282 d = NULL;
283 break;
284 }
285 if (addlistitem(d, v) != 0) {
286 DECREF(v);
287 DECREF(d);
288 d = NULL;
289 break;
290 }
291 DECREF(v);
292 }
293 closedir(dirp);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000294#endif /* !MSDOS */
295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 return d;
297}
298
299static object *
300posix_mkdir(self, args)
301 object *self;
302 object *args;
303{
304 extern int mkdir PROTO((const char *, mode_t));
305 return posix_strint(args, mkdir);
306}
307
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000308#ifdef i386
309int
310rename(from, to)
311 char *from;
312 char *to;
313{
314 int status;
315 /* XXX Shouldn't this unlink the destination first? */
316 status = link(from, to);
317 if (status != 0)
318 return status;
319 return unlink(from);
320}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000321#endif /* i386 */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000322
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323static object *
324posix_rename(self, args)
325 object *self;
326 object *args;
327{
328 extern int rename PROTO((const char *, const char *));
329 return posix_2str(args, rename);
330}
331
332static object *
333posix_rmdir(self, args)
334 object *self;
335 object *args;
336{
337 extern int rmdir PROTO((const char *));
338 return posix_1str(args, rmdir);
339}
340
341static object *
342posix_stat(self, args)
343 object *self;
344 object *args;
345{
346 extern int stat PROTO((const char *, struct stat *));
347 return posix_do_stat(self, args, stat);
348}
349
350static object *
351posix_system(self, args)
352 object *self;
353 object *args;
354{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000355 char *command;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356 int sts;
357 if (!getstrarg(args, &command))
358 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000359 sts = system(command);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360 return newintobject((long)sts);
361}
362
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000363#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364static object *
365posix_umask(self, args)
366 object *self;
367 object *args;
368{
369 int i;
370 if (!getintarg(args, &i))
371 return NULL;
372 i = umask(i);
373 if (i < 0)
374 return posix_error();
375 return newintobject((long)i);
376}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000377#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378
379static object *
380posix_unlink(self, args)
381 object *self;
382 object *args;
383{
384 extern int unlink PROTO((const char *));
385 return posix_1str(args, unlink);
386}
387
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000388#ifdef UTIME_STRUCT
389#include <utime.h>
390#endif
391
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000393posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 object *self;
395 object *args;
396{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000397 char *path;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000398
399#ifdef UTIME_STRUCT
400 struct utimbuf buf;
401#define ATIME buf.actime
402#define MTIME buf.modtime
403#define UTIME_ARG &buf
404
405#else
406 time_t buf[2];
407#define ATIME buf[0]
408#define MTIME buf[1]
409#define UTIME_ARG buf
410#endif
411
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000412 if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000414 if (utime(path, UTIME_ARG) < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415 return posix_error();
416 INCREF(None);
417 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000418#undef UTIME_ARG
419#undef ATIME
420#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421}
422
Guido van Rossum85e3b011991-06-03 12:42:10 +0000423
424#ifndef MSDOS
425
Guido van Rossum3b066191991-06-04 19:40:25 +0000426/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000427
428static object *
429posix__exit(self, args)
430 object *self;
431 object *args;
432{
433 int sts;
434 if (!getintarg(args, &sts))
435 return NULL;
436 _exit(sts);
437 /* NOTREACHED */
438}
439
440/* XXX To do: exece, execp */
441
442static object *
443posix_exec(self, args)
444 object *self;
445 object *args;
446{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000447 char *path;
448 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000449 char **argvlist;
450 int i, argc;
451 object *(*getitem) PROTO((object *, int));
452
453 /* exec has two arguments: (path, argv), where
454 argv is a list or tuple of strings. */
455
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000456 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000457 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000458 if (is_listobject(argv)) {
459 argc = getlistsize(argv);
460 getitem = getlistitem;
461 }
462 else if (is_tupleobject(argv)) {
463 argc = gettuplesize(argv);
464 getitem = gettupleitem;
465 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000466 else {
467 badarg:
468 err_badarg();
469 return NULL;
470 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000471
472 argvlist = NEW(char *, argc+1);
473 if (argvlist == NULL)
474 return NULL;
475 for (i = 0; i < argc; i++) {
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000476 if (!getstrarg((*getitem)(argv, i), &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000477 DEL(argvlist);
478 goto badarg;
479 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000480 }
481 argvlist[argc] = NULL;
482
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000483 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000484
485 /* If we get here it's definitely an error */
486
487 DEL(argvlist);
488 return posix_error();
489}
490
491static object *
492posix_fork(self, args)
493 object *self;
494 object *args;
495{
496 int pid;
497 pid = fork();
498 if (pid == -1)
499 return posix_error();
500 return newintobject((long)pid);
501}
502
503static object *
504posix_getpid(self, args)
505 object *self;
506 object *args;
507{
Guido van Rossum04814471991-06-04 20:23:49 +0000508 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000509 return NULL;
510 return newintobject((long)getpid());
511}
512
513static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000514posix_getpgrp(self, args)
515 object *self;
516 object *args;
517{
518 if (!getnoarg(args))
519 return NULL;
Guido van Rossum971443b1991-06-07 13:59:29 +0000520 return newintobject((long)getpgrp(0));
Guido van Rossum04814471991-06-04 20:23:49 +0000521}
522
523static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000524posix_getppid(self, args)
525 object *self;
526 object *args;
527{
Guido van Rossum04814471991-06-04 20:23:49 +0000528 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000529 return NULL;
530 return newintobject((long)getppid());
531}
532
533static object *
534posix_kill(self, args)
535 object *self;
536 object *args;
537{
538 int pid, sig;
539 if (!getintintarg(args, &pid, &sig))
540 return NULL;
541 if (kill(pid, sig) == -1)
542 return posix_error();
543 INCREF(None);
544 return None;
545}
546
547static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000548posix_popen(self, args)
549 object *self;
550 object *args;
551{
552 extern int pclose PROTO((FILE *));
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000553 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000554 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000555 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000556 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000557 fp = popen(name, mode);
Guido van Rossum3b066191991-06-04 19:40:25 +0000558 if (fp == NULL)
559 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000560 /* From now on, ignore SIGPIPE and let the error checking
561 do the work. */
562 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000563 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000564}
565
566static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000567posix_wait(self, args) /* Also waitpid() */
568 object *self;
569 object *args;
570{
571 object *v;
572 int pid, sts;
573 if (args == NULL)
574 pid = wait(&sts);
575 else {
576#ifdef NO_WAITPID
577 err_setstr(RuntimeError,
578 "posix.wait(pid, options) not supported on this system");
579#else
580 int options;
581 if (!getintintarg(args, &pid, &options))
582 return NULL;
583 pid = waitpid(pid, &sts, options);
584#endif
585 }
586 if (pid == -1)
587 return posix_error();
588 v = newtupleobject(2);
589 if (v != NULL) {
590 settupleitem(v, 0, newintobject((long)pid));
591 settupleitem(v, 1, newintobject((long)sts));
592 if (err_occurred()) {
593 DECREF(v);
594 v = NULL;
595 }
596 }
597 return v;
598}
599
600#endif /* MSDOS */
601
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602#ifndef NO_LSTAT
603
604static object *
605posix_lstat(self, args)
606 object *self;
607 object *args;
608{
609 extern int lstat PROTO((const char *, struct stat *));
610 return posix_do_stat(self, args, lstat);
611}
612
613static object *
614posix_readlink(self, args)
615 object *self;
616 object *args;
617{
618 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000619 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620 int n;
621 if (!getstrarg(args, &path))
622 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000623 n = readlink(path, buf, sizeof buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624 if (n < 0)
625 return posix_error();
626 return newsizedstringobject(buf, n);
627}
628
629static object *
630posix_symlink(self, args)
631 object *self;
632 object *args;
633{
634 extern int symlink PROTO((const char *, const char *));
635 return posix_2str(args, symlink);
636}
637
638#endif /* NO_LSTAT */
639
640
641static struct methodlist posix_methods[] = {
642 {"chdir", posix_chdir},
643 {"chmod", posix_chmod},
644 {"getcwd", posix_getcwd},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000645#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646 {"link", posix_link},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000647#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648 {"listdir", posix_listdir},
649 {"mkdir", posix_mkdir},
650 {"rename", posix_rename},
651 {"rmdir", posix_rmdir},
652 {"stat", posix_stat},
653 {"system", posix_system},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000654#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000655 {"umask", posix_umask},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000656#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000657 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000658 {"utime", posix_utime},
Guido van Rossum85e3b011991-06-03 12:42:10 +0000659#ifndef MSDOS
660 {"_exit", posix__exit},
661 {"exec", posix_exec},
662 {"fork", posix_fork},
663 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +0000664 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +0000665 {"getppid", posix_getppid},
666 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +0000667 {"popen", posix_popen},
Guido van Rossum85e3b011991-06-03 12:42:10 +0000668 {"wait", posix_wait},
669#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670#ifndef NO_LSTAT
671 {"lstat", posix_lstat},
672 {"readlink", posix_readlink},
673 {"symlink", posix_symlink},
674#endif
675 {NULL, NULL} /* Sentinel */
676};
677
678
679void
680initposix()
681{
682 object *m, *d, *v;
683
684 m = initmodule("posix", posix_methods);
685 d = getmoduledict(m);
686
687 /* Initialize posix.environ dictionary */
688 v = convertenviron();
689 if (v == NULL || dictinsert(d, "environ", v) != 0)
690 fatal("can't define posix.environ");
691 DECREF(v);
692
693 /* Initialize posix.error exception */
694 PosixError = newstringobject("posix.error");
695 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
696 fatal("can't define posix.error");
697}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000698
Guido van Rossum3b066191991-06-04 19:40:25 +0000699
700/* Function used elsewhere to get a file's modification time */
701
702long
703getmtime(path)
704 char *path;
705{
706 struct stat st;
707 if (stat(path, &st) != 0)
708 return -1;
709 else
710 return st.st_mtime;
711}
712
713
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000714#ifdef MSDOS
715
716/* A small "compatibility library" for TurboC under MS-DOS */
717
718#include <sir.h>
719#include <io.h>
720#include <dos.h>
721#include <fcntl.h>
722
723int
724chmod(path, mode)
725 char *path;
726 int mode;
727{
728 return _chmod(path, 1, mode);
729}
730
731int
732utime(path, times)
733 char *path;
734 time_t times[2];
735{
736 struct date dt;
737 struct time tm;
738 struct ftime dft;
739 int fh;
740 unixtodos(tv[0].tv_sec,&dt,&tm);
741 dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min;
742 dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day;
743 dft.ft_month = dt.da_mon;
744 dft.ft_year = (dt.da_year - 1980); /* this is for TC library */
745
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000746 if ((fh = open(path,O_RDWR)) < 0)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000747 return posix_error(); /* can't open file to set time */
748 if (setftime(fh,&dft) < 0)
749 {
750 close(fh);
751 return posix_error();
752 }
753 close(fh); /* close the temp handle */
754}
755
756#endif /* MSDOS */