blob: 04c17f08a400a552d7649f903ad784eefae952c2 [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 Rossum0ee42cd1991-04-08 21:01:03 +000032#ifdef MSDOS
33#define NO_LSTAT
Guido van Rossumc39de5f1992-02-05 11:15:54 +000034#define NO_UNAME
Guido van Rossume22e6441993-07-09 10:51:31 +000035#include <dos.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000036#endif
37
Guido van Rossumc2670a01992-09-13 20:07:29 +000038#ifdef __sgi
39#define DO_PG
40#endif
41
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042#include <signal.h>
43#include <string.h>
44#include <setjmp.h>
45#include <sys/types.h>
46#include <sys/stat.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000047
Guido van Rossum22db57e1992-04-05 14:25:30 +000048#ifdef DO_TIMES
49#include <sys/times.h>
50#include <sys/param.h>
51#include <errno.h>
52#endif
53
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054#ifdef SYSV
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000055
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000056#define UTIME_STRUCT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057#include <dirent.h>
58#define direct dirent
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000059#ifdef i386
60#define mode_t int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000062
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000063#else /* !SYSV */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000064
65#ifndef MSDOS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000066#include <sys/dir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000067#endif
68
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000069#endif /* !SYSV */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070
Guido van Rossum50e61dc1992-03-27 17:22:31 +000071#ifndef NO_UNISTD
Guido van Rossum22db57e1992-04-05 14:25:30 +000072#include <unistd.h> /* Take this out and hope the best if it doesn't exist */
Guido van Rossum50e61dc1992-03-27 17:22:31 +000073#endif
74
Guido van Rossum3f5da241990-12-20 15:06:42 +000075#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000077#include "ceval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000079#ifdef _SEQUENT_
80#include <unistd.h>
81#else /* _SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000082/* XXX Aren't these always declared in unistd.h? */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000083extern int mkdir PROTO((const char *, mode_t));
84extern int chdir PROTO((const char *));
Guido van Rossume22e6441993-07-09 10:51:31 +000085extern int rmdir PROTO((const char *));
86extern int chmod PROTO((const char *, mode_t));
87extern char *getcwd PROTO((char *, int)); /* XXX or size_t? */
Guido van Rossuma3309961993-07-28 09:05:47 +000088#ifndef MSDOS
Guido van Rossume22e6441993-07-09 10:51:31 +000089extern char *strerror PROTO((int));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000090extern int link PROTO((const char *, const char *));
91extern int rename PROTO((const char *, const char *));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000092extern int stat PROTO((const char *, struct stat *));
93extern int unlink PROTO((const char *));
94extern int pclose PROTO((FILE *));
Guido van Rossuma3309961993-07-28 09:05:47 +000095#endif /* !MSDOS */
96#endif /* !_SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000097#ifdef NO_LSTAT
98#define lstat stat
99#else
100extern int lstat PROTO((const char *, struct stat *));
101extern int symlink PROTO((const char *, const char *));
102#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104
105/* Return a dictionary corresponding to the POSIX environment table */
106
107extern char **environ;
108
109static object *
110convertenviron()
111{
112 object *d;
113 char **e;
114 d = newdictobject();
115 if (d == NULL)
116 return NULL;
117 if (environ == NULL)
118 return d;
119 /* XXX This part ignores errors */
120 for (e = environ; *e != NULL; e++) {
121 object *v;
122 char *p = strchr(*e, '=');
123 if (p == NULL)
124 continue;
125 v = newstringobject(p+1);
126 if (v == NULL)
127 continue;
128 *p = '\0';
129 (void) dictinsert(d, *e, v);
130 *p = '=';
131 DECREF(v);
132 }
133 return d;
134}
135
136
137static object *PosixError; /* Exception posix.error */
138
139/* Set a POSIX-specific error from errno, and return NULL */
140
Guido van Rossum687dd131993-05-17 08:34:16 +0000141static object * posix_error() { return err_errno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
143
144
145/* POSIX generic methods */
146
147static object *
148posix_1str(args, func)
149 object *args;
150 int (*func) FPROTO((const char *));
151{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000152 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000153 int res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154 if (!getstrarg(args, &path1))
155 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000156 BGN_SAVE
157 res = (*func)(path1);
158 END_SAVE
159 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 return posix_error();
161 INCREF(None);
162 return None;
163}
164
165static object *
166posix_2str(args, func)
167 object *args;
168 int (*func) FPROTO((const char *, const char *));
169{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000170 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000171 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000172 if (!getargs(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000174 BGN_SAVE
175 res = (*func)(path1, path2);
176 END_SAVE
177 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178 return posix_error();
179 INCREF(None);
180 return None;
181}
182
183static object *
184posix_strint(args, func)
185 object *args;
186 int (*func) FPROTO((const char *, int));
187{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000188 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000190 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000191 if (!getargs(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000193 BGN_SAVE
194 res = (*func)(path, i);
195 END_SAVE
196 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 return posix_error();
198 INCREF(None);
199 return None;
200}
201
202static object *
203posix_do_stat(self, args, statfunc)
204 object *self;
205 object *args;
206 int (*statfunc) FPROTO((const char *, struct stat *));
207{
208 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000209 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210 int res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211 if (!getstrarg(args, &path))
212 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000213 BGN_SAVE
214 res = (*statfunc)(path, &st);
215 END_SAVE
216 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217 return posix_error();
Guido van Rossum687dd131993-05-17 08:34:16 +0000218 return mkvalue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000219 (long)st.st_mode,
220 (long)st.st_ino,
221 (long)st.st_dev,
222 (long)st.st_nlink,
223 (long)st.st_uid,
224 (long)st.st_gid,
225 (long)st.st_size,
226 (long)st.st_atime,
227 (long)st.st_mtime,
228 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
231
232/* POSIX methods */
233
234static object *
235posix_chdir(self, args)
236 object *self;
237 object *args;
238{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 return posix_1str(args, chdir);
240}
241
242static object *
243posix_chmod(self, args)
244 object *self;
245 object *args;
246{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 return posix_strint(args, chmod);
248}
249
250static object *
251posix_getcwd(self, args)
252 object *self;
253 object *args;
254{
255 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000256 char *res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 if (!getnoarg(args))
258 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000259 BGN_SAVE
260 res = getcwd(buf, sizeof buf);
261 END_SAVE
262 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 return posix_error();
264 return newstringobject(buf);
265}
266
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000267#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268static object *
269posix_link(self, args)
270 object *self;
271 object *args;
272{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273 return posix_2str(args, link);
274}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000275#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276
277static object *
278posix_listdir(self, args)
279 object *self;
280 object *args;
281{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000282 char *name;
283 object *d, *v;
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000284
Guido van Rossume22e6441993-07-09 10:51:31 +0000285#ifdef TURBO_C
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000286 struct ffblk ep;
287 int rv;
288 if (!getstrarg(args, &name))
289 return NULL;
290
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000291 if (findfirst(name, &ep, 0) == -1)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000292 return posix_error();
293 if ((d = newlistobject(0)) == NULL)
294 return NULL;
295 do {
296 v = newstringobject(ep.ff_name);
297 if (v == NULL) {
298 DECREF(d);
299 d = NULL;
300 break;
301 }
302 if (addlistitem(d, v) != 0) {
303 DECREF(v);
304 DECREF(d);
305 d = NULL;
306 break;
307 }
308 DECREF(v);
309 } while ((rv = findnext(&ep)) == 0);
Guido van Rossume22e6441993-07-09 10:51:31 +0000310#endif /* TURBO_C */
311#ifdef MSDOS
312 struct find_t ep;
313 int rv;
314 char _name[100];
315 int attrib;
316 int num= 0;
317
318 if (!getstrarg(args, &name))
319 return NULL;
320 strcpy( _name, name );
321
322again:
323 if ((d = newlistobject(0)) == NULL)
324 return NULL;
325
326 if( _name[strlen( _name )-1]=='/' )
327 strcat( _name, "*.*" );
328
329 if (_dos_findfirst(_name, _A_NORMAL|_A_SUBDIR, &ep) == -1)
330 return posix_error();
331 attrib= ep.attrib;
332 do {
333 v = newstringobject(ep.name);
334 if (v == NULL) {
335 DECREF(d);
336 d = NULL;
337 break;
338 }
339 if (addlistitem(d, v) != 0) {
340 DECREF(v);
341 DECREF(d);
342 d = NULL;
343 break;
344 }
345 num++;
346 DECREF(v);
347 } while ((rv = _dos_findnext(&ep)) == 0);
348
349 if( attrib&_A_SUBDIR && num==1 )
350 {
351 DECREF( d );
352 strcat( _name, "/*.*" );
Guido van Rossum06191531993-10-26 15:22:37 +0000353 /* This comment is here to help the DEC alpha OSF/1 cpp
354 (which scans for comments but not for strings in
355 code that is #ifdef'ed out...) */
Guido van Rossume22e6441993-07-09 10:51:31 +0000356 goto again;
357 }
358
359#endif /* MSDOS */
360#ifdef unix
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 DIR *dirp;
362 struct direct *ep;
363 if (!getstrarg(args, &name))
364 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000365 BGN_SAVE
366 if ((dirp = opendir(name)) == NULL) {
367 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000369 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370 if ((d = newlistobject(0)) == NULL) {
371 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000372 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373 return NULL;
374 }
375 while ((ep = readdir(dirp)) != NULL) {
376 v = newstringobject(ep->d_name);
377 if (v == NULL) {
378 DECREF(d);
379 d = NULL;
380 break;
381 }
382 if (addlistitem(d, v) != 0) {
383 DECREF(v);
384 DECREF(d);
385 d = NULL;
386 break;
387 }
388 DECREF(v);
389 }
390 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000391 END_SAVE
Guido van Rossume22e6441993-07-09 10:51:31 +0000392#endif /* unix */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000393
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 return d;
395}
396
397static object *
398posix_mkdir(self, args)
399 object *self;
400 object *args;
401{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 return posix_strint(args, mkdir);
403}
404
Guido van Rossum775f4da1993-01-09 17:18:52 +0000405#ifndef MSDOS
406static object *
407posix_nice(self, args)
408 object *self;
409 object *args;
410{
411 int increment, value;
412
413 if (!getargs(args, "i", &increment))
414 return NULL;
415 value = nice(increment);
416 if (value == -1)
417 return posix_error();
418 return newintobject((long) value);
419}
420#endif
421
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000422#if i386 && ! _SEQUENT_
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000423int
424rename(from, to)
425 char *from;
426 char *to;
427{
428 int status;
429 /* XXX Shouldn't this unlink the destination first? */
430 status = link(from, to);
431 if (status != 0)
432 return status;
433 return unlink(from);
434}
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000435#endif /* i386 && ! _SEQUENT_ */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000436
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437static object *
438posix_rename(self, args)
439 object *self;
440 object *args;
441{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442 return posix_2str(args, rename);
443}
444
445static object *
446posix_rmdir(self, args)
447 object *self;
448 object *args;
449{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 return posix_1str(args, rmdir);
451}
452
453static object *
454posix_stat(self, args)
455 object *self;
456 object *args;
457{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458 return posix_do_stat(self, args, stat);
459}
460
461static object *
462posix_system(self, args)
463 object *self;
464 object *args;
465{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000466 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 long sts;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 if (!getstrarg(args, &command))
469 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000470 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000471 sts = system(command);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000472 END_SAVE
473 return newintobject(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474}
475
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000476#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477static object *
478posix_umask(self, args)
479 object *self;
480 object *args;
481{
482 int i;
483 if (!getintarg(args, &i))
484 return NULL;
485 i = umask(i);
486 if (i < 0)
487 return posix_error();
488 return newintobject((long)i);
489}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000490#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491
492static object *
493posix_unlink(self, args)
494 object *self;
495 object *args;
496{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497 return posix_1str(args, unlink);
498}
499
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000500#ifndef NO_UNAME
501#include <sys/utsname.h>
502
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000503extern int uname PROTO((struct utsname *));
504
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000505static object *
506posix_uname(self, args)
507 object *self;
508 object *args;
509{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000510 struct utsname u;
511 object *v;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000512 int res;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000513 if (!getnoarg(args))
514 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000515 BGN_SAVE
516 res = uname(&u);
517 END_SAVE
518 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000519 return posix_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000520 return mkvalue("(sssss)",
521 u.sysname,
522 u.nodename,
523 u.release,
524 u.version,
525 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000526}
527#endif /* NO_UNAME */
528
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000529#ifdef UTIME_STRUCT
530#include <utime.h>
531#endif
532
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000534posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535 object *self;
536 object *args;
537{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000538 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000539 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000540
541#ifdef UTIME_STRUCT
542 struct utimbuf buf;
543#define ATIME buf.actime
544#define MTIME buf.modtime
545#define UTIME_ARG &buf
546
547#else
548 time_t buf[2];
549#define ATIME buf[0]
550#define MTIME buf[1]
551#define UTIME_ARG buf
552#endif
553
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000554 if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000556 BGN_SAVE
557 res = utime(path, UTIME_ARG);
558 END_SAVE
559 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 return posix_error();
561 INCREF(None);
562 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000563#undef UTIME_ARG
564#undef ATIME
565#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566}
567
Guido van Rossum85e3b011991-06-03 12:42:10 +0000568
569#ifndef MSDOS
570
Guido van Rossum3b066191991-06-04 19:40:25 +0000571/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000572
573static object *
574posix__exit(self, args)
575 object *self;
576 object *args;
577{
578 int sts;
579 if (!getintarg(args, &sts))
580 return NULL;
581 _exit(sts);
582 /* NOTREACHED */
583}
584
585/* XXX To do: exece, execp */
586
587static object *
Guido van Rossum89b33251993-10-22 14:26:06 +0000588posix_execv(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000589 object *self;
590 object *args;
591{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000592 char *path;
593 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000594 char **argvlist;
595 int i, argc;
596 object *(*getitem) PROTO((object *, int));
597
Guido van Rossum89b33251993-10-22 14:26:06 +0000598 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000599 argv is a list or tuple of strings. */
600
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000601 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000602 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000603 if (is_listobject(argv)) {
604 argc = getlistsize(argv);
605 getitem = getlistitem;
606 }
607 else if (is_tupleobject(argv)) {
608 argc = gettuplesize(argv);
609 getitem = gettupleitem;
610 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000611 else {
612 badarg:
613 err_badarg();
614 return NULL;
615 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000616
617 argvlist = NEW(char *, argc+1);
618 if (argvlist == NULL)
619 return NULL;
620 for (i = 0; i < argc; i++) {
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000621 if (!getstrarg((*getitem)(argv, i), &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000622 DEL(argvlist);
623 goto badarg;
624 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000625 }
626 argvlist[argc] = NULL;
627
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000628 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000629
630 /* If we get here it's definitely an error */
631
632 DEL(argvlist);
633 return posix_error();
634}
635
636static object *
637posix_fork(self, args)
638 object *self;
639 object *args;
640{
641 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000642 if (!getnoarg(args))
643 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000644 pid = fork();
645 if (pid == -1)
646 return posix_error();
647 return newintobject((long)pid);
648}
649
650static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000651posix_getegid(self, args)
652 object *self;
653 object *args;
654{
655 if (!getnoarg(args))
656 return NULL;
657 return newintobject((long)getegid());
658}
659
660static object *
661posix_geteuid(self, args)
662 object *self;
663 object *args;
664{
665 if (!getnoarg(args))
666 return NULL;
667 return newintobject((long)geteuid());
668}
669
670static object *
671posix_getgid(self, args)
672 object *self;
673 object *args;
674{
675 if (!getnoarg(args))
676 return NULL;
677 return newintobject((long)getgid());
678}
679
680static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000681posix_getpid(self, args)
682 object *self;
683 object *args;
684{
Guido van Rossum04814471991-06-04 20:23:49 +0000685 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000686 return NULL;
687 return newintobject((long)getpid());
688}
689
690static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000691posix_getpgrp(self, args)
692 object *self;
693 object *args;
694{
695 if (!getnoarg(args))
696 return NULL;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000697#ifdef SYSV
698 return newintobject((long)getpgrp());
699#else
Guido van Rossum971443b1991-06-07 13:59:29 +0000700 return newintobject((long)getpgrp(0));
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000701#endif
Guido van Rossum04814471991-06-04 20:23:49 +0000702}
703
704static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +0000705posix_setpgrp(self, args)
706 object *self;
707 object *args;
708{
709 if (!getnoarg(args))
710 return NULL;
711#ifdef SYSV
712 if (setpgrp() < 0)
713#else
714 if (setpgrp(0, 0) < 0)
715#endif
Guido van Rossum687dd131993-05-17 08:34:16 +0000716 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000717 INCREF(None);
718 return None;
719}
720
721static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000722posix_getppid(self, args)
723 object *self;
724 object *args;
725{
Guido van Rossum04814471991-06-04 20:23:49 +0000726 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000727 return NULL;
728 return newintobject((long)getppid());
729}
730
731static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000732posix_getuid(self, args)
733 object *self;
734 object *args;
735{
736 if (!getnoarg(args))
737 return NULL;
738 return newintobject((long)getuid());
739}
740
741static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000742posix_kill(self, args)
743 object *self;
744 object *args;
745{
746 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +0000747 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000748 return NULL;
749 if (kill(pid, sig) == -1)
750 return posix_error();
751 INCREF(None);
752 return None;
753}
754
755static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000756posix_popen(self, args)
757 object *self;
758 object *args;
759{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000760 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000761 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000762 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000763 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000764 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000765 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000766 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +0000767 if (fp == NULL)
768 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000769 /* From now on, ignore SIGPIPE and let the error checking
770 do the work. */
771 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000772 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000773}
774
775static object *
Guido van Rossum21803b81992-08-09 12:55:27 +0000776posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000777 object *self;
778 object *args;
779{
Guido van Rossum85e3b011991-06-03 12:42:10 +0000780#ifdef NO_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +0000781 err_setstr(PosixError,
782 "posix.waitpid() not supported on this system");
783 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000784#else
Guido van Rossum21803b81992-08-09 12:55:27 +0000785 int pid, options, sts;
786 if (!getargs(args, "(ii)", &pid, &options))
787 return NULL;
788 BGN_SAVE
789 pid = waitpid(pid, &sts, options);
790 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +0000791 if (pid == -1)
792 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +0000793 else
794 return mkvalue("ii", pid, sts);
795#endif
796}
797
798static object *
799posix_wait(self, args)
800 object *self;
801 object *args;
802{
803 int pid, sts;
804 if (args != NULL)
805 return posix_waitpid(self, args); /* BW compat */
806 BGN_SAVE
807 pid = wait(&sts);
808 END_SAVE
809 if (pid == -1)
810 return posix_error();
811 else
812 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000813}
814
815#endif /* MSDOS */
816
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817static object *
818posix_lstat(self, args)
819 object *self;
820 object *args;
821{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822 return posix_do_stat(self, args, lstat);
823}
824
825static object *
826posix_readlink(self, args)
827 object *self;
828 object *args;
829{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000830#ifdef NO_LSTAT
831 err_setstr(PosixError, "readlink not implemented on this system");
832 return NULL;
833#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000835 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836 int n;
837 if (!getstrarg(args, &path))
838 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000839 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000840 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000841 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842 if (n < 0)
843 return posix_error();
844 return newsizedstringobject(buf, n);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000845#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000846}
847
848static object *
849posix_symlink(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, "symlink not implemented on this system");
855 return NULL;
856#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857 return posix_2str(args, symlink);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000858#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861
Guido van Rossum22db57e1992-04-05 14:25:30 +0000862#ifdef DO_TIMES
863
864static object *
865posix_times(self, args)
866 object *self;
867 object *args;
868{
869 struct tms t;
870 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +0000871 if (!getnoarg(args))
872 return NULL;
873 errno = 0;
874 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +0000875 if (c == (clock_t) -1)
876 return posix_error();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000877 return mkvalue("dddd",
878 (double)t.tms_utime / HZ,
879 (double)t.tms_stime / HZ,
880 (double)t.tms_cutime / HZ,
881 (double)t.tms_cstime / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +0000882}
883
Guido van Rossumc2670a01992-09-13 20:07:29 +0000884#endif /* DO_TIMES */
885
886#ifdef DO_PG
887
888static object *
889posix_setsid(self, args)
890 object *self;
891 object *args;
892{
893 if (!getnoarg(args))
894 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000895 if (setsid() < 0)
896 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000897 INCREF(None);
898 return None;
899}
900
901static object *
902posix_setpgid(self, args)
903 object *self;
904 object *args;
905{
906 int pid, pgrp;
907 if (!getargs(args, "(ii)", &pid, &pgrp))
908 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000909 if (setpgid(pid, pgrp) < 0)
910 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000911 INCREF(None);
912 return None;
913}
914
Guido van Rossum7066dd71992-09-17 17:54:56 +0000915static object *
916posix_tcgetpgrp(self, args)
917 object *self;
918 object *args;
919{
920 int fd, pgid;
921 if (!getargs(args, "i", &fd))
922 return NULL;
923 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +0000924 if (pgid < 0)
925 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000926 return newintobject((long)pgid);
927}
928
929static object *
930posix_tcsetpgrp(self, args)
931 object *self;
932 object *args;
933{
934 int fd, pgid;
935 if (!getargs(args, "(ii)", &fd, &pgid))
936 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000937 if (tcsetpgrp(fd, pgid) < 0)
938 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000939 INCREF(None);
940 return None;
941}
942
Guido van Rossumc2670a01992-09-13 20:07:29 +0000943#endif /* DO_PG */
Guido van Rossum22db57e1992-04-05 14:25:30 +0000944
Guido van Rossum687dd131993-05-17 08:34:16 +0000945/* Functions acting on file descriptors */
946
Guido van Rossum234f9421993-06-17 12:35:49 +0000947static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000948posix_open(self, args)
949 object *self;
950 object *args;
951{
952 char *file;
953 int flag;
954 int mode = 0777;
955 int fd;
956 if (!getargs(args, "(si)", &file, &flag)) {
957 err_clear();
958 if (!getargs(args, "(sii)", &file, &flag, &mode))
959 return NULL;
960 }
961 BGN_SAVE
962 fd = open(file, flag, mode);
963 END_SAVE
964 if (fd < 0)
965 return posix_error();
966 return newintobject((long)fd);
967}
968
Guido van Rossum234f9421993-06-17 12:35:49 +0000969static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000970posix_close(self, args)
971 object *self;
972 object *args;
973{
974 int fd, res;
975 if (!getargs(args, "i", &fd))
976 return NULL;
977 BGN_SAVE
978 res = close(fd);
979 END_SAVE
980 if (res < 0)
981 return posix_error();
982 INCREF(None);
983 return None;
984}
985
Guido van Rossum234f9421993-06-17 12:35:49 +0000986static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000987posix_dup(self, args)
988 object *self;
989 object *args;
990{
991 int fd;
992 if (!getargs(args, "i", &fd))
993 return NULL;
994 BGN_SAVE
995 fd = dup(fd);
996 END_SAVE
997 if (fd < 0)
998 return posix_error();
999 return newintobject((long)fd);
1000}
1001
Guido van Rossum234f9421993-06-17 12:35:49 +00001002static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001003posix_dup2(self, args)
1004 object *self;
1005 object *args;
1006{
1007 int fd, fd2, res;
1008 if (!getargs(args, "(ii)", &fd, &fd2))
1009 return NULL;
1010 BGN_SAVE
1011 res = dup2(fd, fd2);
1012 END_SAVE
1013 if (res < 0)
1014 return posix_error();
1015 INCREF(None);
1016 return None;
1017}
1018
Guido van Rossum234f9421993-06-17 12:35:49 +00001019static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001020posix_lseek(self, args)
1021 object *self;
1022 object *args;
1023{
1024 int fd, how;
1025 long pos, res;
1026 if (!getargs(args, "(ili)", &fd, &pos, &how))
1027 return NULL;
1028#ifdef SEEK_SET
1029 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1030 switch (how) {
1031 case 0: how = SEEK_SET; break;
1032 case 1: how = SEEK_CUR; break;
1033 case 2: how = SEEK_END; break;
1034 }
1035#endif
1036 BGN_SAVE
1037 res = lseek(fd, pos, how);
1038 END_SAVE
1039 if (res < 0)
1040 return posix_error();
1041 return newintobject(res);
1042}
1043
Guido van Rossum234f9421993-06-17 12:35:49 +00001044static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001045posix_read(self, args)
1046 object *self;
1047 object *args;
1048{
1049 int fd, size;
1050 object *buffer;
1051 if (!getargs(args, "(ii)", &fd, &size))
1052 return NULL;
1053 buffer = newsizedstringobject((char *)NULL, size);
1054 if (buffer == NULL)
1055 return NULL;
1056 BGN_SAVE
1057 size = read(fd, getstringvalue(buffer), size);
1058 END_SAVE
1059 if (size < 0) {
1060 DECREF(buffer);
1061 return posix_error();
1062 }
1063 resizestring(&buffer, size);
1064 return buffer;
1065}
1066
Guido van Rossum234f9421993-06-17 12:35:49 +00001067static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001068posix_write(self, args)
1069 object *self;
1070 object *args;
1071{
1072 int fd, size;
1073 char *buffer;
1074 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1075 return NULL;
1076 BGN_SAVE
1077 size = write(fd, buffer, size);
1078 END_SAVE
1079 if (size < 0)
1080 return posix_error();
1081 return newintobject((long)size);
1082}
1083
Guido van Rossum234f9421993-06-17 12:35:49 +00001084static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001085posix_fstat(self, args)
1086 object *self;
1087 object *args;
1088{
1089 int fd;
1090 struct stat st;
1091 int res;
1092 if (!getargs(args, "i", &fd))
1093 return NULL;
1094 BGN_SAVE
1095 res = fstat(fd, &st);
1096 END_SAVE
1097 if (res != 0)
1098 return posix_error();
1099 return mkvalue("(llllllllll)",
1100 (long)st.st_mode,
1101 (long)st.st_ino,
1102 (long)st.st_dev,
1103 (long)st.st_nlink,
1104 (long)st.st_uid,
1105 (long)st.st_gid,
1106 (long)st.st_size,
1107 (long)st.st_atime,
1108 (long)st.st_mtime,
1109 (long)st.st_ctime);
1110}
1111
1112static object *
1113posix_fdopen(self, args)
1114 object *self;
1115 object *args;
1116{
1117 extern int fclose PROTO((FILE *));
1118 int fd;
1119 char *mode;
1120 FILE *fp;
1121 if (!getargs(args, "(is)", &fd, &mode))
1122 return NULL;
1123 BGN_SAVE
1124 fp = fdopen(fd, mode);
1125 END_SAVE
1126 if (fp == NULL)
1127 return posix_error();
1128 /* From now on, ignore SIGPIPE and let the error checking
1129 do the work. */
1130 (void) signal(SIGPIPE, SIG_IGN);
1131 return newopenfileobject(fp, "(fdopen)", mode, fclose);
1132}
1133
1134#ifndef MSDOS
Guido van Rossum234f9421993-06-17 12:35:49 +00001135static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001136posix_pipe(self, args)
1137 object *self;
1138 object *args;
1139{
1140 int fds[2];
1141 int res;
1142 if (!getargs(args, ""))
1143 return NULL;
1144 BGN_SAVE
1145 res = pipe(fds);
1146 END_SAVE
1147 if (res != 0)
1148 return posix_error();
1149 return mkvalue("(ii)", fds[0], fds[1]);
1150}
1151#endif /* MSDOS */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001152
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001153static struct methodlist posix_methods[] = {
1154 {"chdir", posix_chdir},
1155 {"chmod", posix_chmod},
1156 {"getcwd", posix_getcwd},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001157#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001158 {"link", posix_link},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001159#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001161 {"lstat", posix_lstat},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001162 {"mkdir", posix_mkdir},
Guido van Rossum775f4da1993-01-09 17:18:52 +00001163#ifndef MSDOS
1164 {"nice", posix_nice},
1165#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001166 {"readlink", posix_readlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001167 {"rename", posix_rename},
1168 {"rmdir", posix_rmdir},
1169 {"stat", posix_stat},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001170 {"symlink", posix_symlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001171 {"system", posix_system},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001172#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001173 {"umask", posix_umask},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001174#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001175#ifndef NO_UNAME
1176 {"uname", posix_uname},
1177#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001178 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001179 {"utime", posix_utime},
Guido van Rossum22db57e1992-04-05 14:25:30 +00001180#ifdef DO_TIMES
1181 {"times", posix_times},
1182#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001183
Guido van Rossum85e3b011991-06-03 12:42:10 +00001184#ifndef MSDOS
1185 {"_exit", posix__exit},
Guido van Rossum89b33251993-10-22 14:26:06 +00001186 {"execv", posix_execv},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001187 {"fork", posix_fork},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001188 {"getegid", posix_getegid},
1189 {"geteuid", posix_geteuid},
1190 {"getgid", posix_getgid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001191 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +00001192 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001193 {"getppid", posix_getppid},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001194 {"getuid", posix_getuid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001195 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +00001196 {"popen", posix_popen},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001197 {"setpgrp", posix_setpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001198 {"wait", posix_wait},
Guido van Rossum21803b81992-08-09 12:55:27 +00001199 {"waitpid", posix_waitpid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001200#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001201
Guido van Rossumc2670a01992-09-13 20:07:29 +00001202#ifdef DO_PG
1203 {"setsid", posix_setsid},
1204 {"setpgid", posix_setpgid},
Guido van Rossum7066dd71992-09-17 17:54:56 +00001205 {"tcgetpgrp", posix_tcgetpgrp},
1206 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001207#endif
1208
Guido van Rossum687dd131993-05-17 08:34:16 +00001209 {"open", posix_open},
1210 {"close", posix_close},
1211 {"dup", posix_dup},
1212 {"dup2", posix_dup2},
1213 {"lseek", posix_lseek},
1214 {"read", posix_read},
1215 {"write", posix_write},
1216 {"fstat", posix_fstat},
1217 {"fdopen", posix_fdopen},
1218#ifndef MSDOS
1219 {"pipe", posix_pipe},
1220#endif
1221
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001222 {NULL, NULL} /* Sentinel */
1223};
1224
1225
1226void
1227initposix()
1228{
1229 object *m, *d, *v;
1230
1231 m = initmodule("posix", posix_methods);
1232 d = getmoduledict(m);
1233
1234 /* Initialize posix.environ dictionary */
1235 v = convertenviron();
1236 if (v == NULL || dictinsert(d, "environ", v) != 0)
1237 fatal("can't define posix.environ");
1238 DECREF(v);
1239
1240 /* Initialize posix.error exception */
1241 PosixError = newstringobject("posix.error");
1242 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1243 fatal("can't define posix.error");
1244}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001245
Guido van Rossum3b066191991-06-04 19:40:25 +00001246
1247/* Function used elsewhere to get a file's modification time */
1248
1249long
1250getmtime(path)
1251 char *path;
1252{
1253 struct stat st;
1254 if (stat(path, &st) != 0)
1255 return -1;
1256 else
1257 return st.st_mtime;
1258}
1259
1260
Guido van Rossume22e6441993-07-09 10:51:31 +00001261#ifdef TURBO_C
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001262
1263/* A small "compatibility library" for TurboC under MS-DOS */
1264
Guido van Rossume22e6441993-07-09 10:51:31 +00001265//#include <sir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001266#include <io.h>
1267#include <dos.h>
1268#include <fcntl.h>
1269
1270int
1271chmod(path, mode)
1272 char *path;
1273 int mode;
1274{
1275 return _chmod(path, 1, mode);
1276}
1277
1278int
1279utime(path, times)
1280 char *path;
1281 time_t times[2];
1282{
1283 struct date dt;
1284 struct time tm;
1285 struct ftime dft;
1286 int fh;
1287 unixtodos(tv[0].tv_sec,&dt,&tm);
1288 dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min;
1289 dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day;
1290 dft.ft_month = dt.da_mon;
1291 dft.ft_year = (dt.da_year - 1980); /* this is for TC library */
1292
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001293 if ((fh = open(path,O_RDWR)) < 0)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001294 return posix_error(); /* can't open file to set time */
1295 if (setftime(fh,&dft) < 0)
1296 {
1297 close(fh);
1298 return posix_error();
1299 }
1300 close(fh); /* close the temp handle */
1301}
1302
Guido van Rossume22e6441993-07-09 10:51:31 +00001303#endif /* TURBO_C */