blob: 66cb41a0e8ec64b5b520519d24a68c6135707a0d [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 Rossumc70b61f1993-11-01 16:23:18 +000042#ifdef _NEXT_SOURCE
43#define mode_t int
44#define NO_UNAME
45#endif
46
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047#include <signal.h>
48#include <string.h>
49#include <setjmp.h>
50#include <sys/types.h>
51#include <sys/stat.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000052
Guido van Rossum22db57e1992-04-05 14:25:30 +000053#ifdef DO_TIMES
54#include <sys/times.h>
55#include <sys/param.h>
56#include <errno.h>
57#endif
58
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059#ifdef SYSV
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000060
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000061#define UTIME_STRUCT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062#include <dirent.h>
63#define direct dirent
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000064#ifdef i386
65#define mode_t int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000067
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000068#else /* !SYSV */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000069
70#ifndef MSDOS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000071#include <sys/dir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000072#endif
73
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000074#endif /* !SYSV */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Guido van Rossum50e61dc1992-03-27 17:22:31 +000076#ifndef NO_UNISTD
Guido van Rossum22db57e1992-04-05 14:25:30 +000077#include <unistd.h> /* Take this out and hope the best if it doesn't exist */
Guido van Rossum50e61dc1992-03-27 17:22:31 +000078#endif
79
Guido van Rossum3f5da241990-12-20 15:06:42 +000080#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000082#include "ceval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000084#ifdef _SEQUENT_
85#include <unistd.h>
86#else /* _SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000087/* XXX Aren't these always declared in unistd.h? */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000088extern int mkdir PROTO((const char *, mode_t));
89extern int chdir PROTO((const char *));
Guido van Rossume22e6441993-07-09 10:51:31 +000090extern int rmdir PROTO((const char *));
91extern int chmod PROTO((const char *, mode_t));
92extern char *getcwd PROTO((char *, int)); /* XXX or size_t? */
Guido van Rossuma3309961993-07-28 09:05:47 +000093#ifndef MSDOS
Guido van Rossume22e6441993-07-09 10:51:31 +000094extern char *strerror PROTO((int));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000095extern int link PROTO((const char *, const char *));
96extern int rename PROTO((const char *, const char *));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000097extern int stat PROTO((const char *, struct stat *));
98extern int unlink PROTO((const char *));
99extern int pclose PROTO((FILE *));
Guido van Rossuma3309961993-07-28 09:05:47 +0000100#endif /* !MSDOS */
101#endif /* !_SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000102#ifdef NO_LSTAT
103#define lstat stat
104#else
105extern int lstat PROTO((const char *, struct stat *));
106extern int symlink PROTO((const char *, const char *));
107#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
110/* Return a dictionary corresponding to the POSIX environment table */
111
112extern char **environ;
113
114static object *
115convertenviron()
116{
117 object *d;
118 char **e;
119 d = newdictobject();
120 if (d == NULL)
121 return NULL;
122 if (environ == NULL)
123 return d;
124 /* XXX This part ignores errors */
125 for (e = environ; *e != NULL; e++) {
126 object *v;
127 char *p = strchr(*e, '=');
128 if (p == NULL)
129 continue;
130 v = newstringobject(p+1);
131 if (v == NULL)
132 continue;
133 *p = '\0';
134 (void) dictinsert(d, *e, v);
135 *p = '=';
136 DECREF(v);
137 }
138 return d;
139}
140
141
142static object *PosixError; /* Exception posix.error */
143
144/* Set a POSIX-specific error from errno, and return NULL */
145
Guido van Rossum687dd131993-05-17 08:34:16 +0000146static object * posix_error() { return err_errno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147}
148
149
150/* POSIX generic methods */
151
152static object *
153posix_1str(args, func)
154 object *args;
155 int (*func) FPROTO((const char *));
156{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000157 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000158 int res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 if (!getstrarg(args, &path1))
160 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000161 BGN_SAVE
162 res = (*func)(path1);
163 END_SAVE
164 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 return posix_error();
166 INCREF(None);
167 return None;
168}
169
170static object *
171posix_2str(args, func)
172 object *args;
173 int (*func) FPROTO((const char *, const char *));
174{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000175 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000176 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000177 if (!getargs(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000179 BGN_SAVE
180 res = (*func)(path1, path2);
181 END_SAVE
182 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183 return posix_error();
184 INCREF(None);
185 return None;
186}
187
188static object *
189posix_strint(args, func)
190 object *args;
191 int (*func) FPROTO((const char *, int));
192{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000193 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000195 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000196 if (!getargs(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000198 BGN_SAVE
199 res = (*func)(path, i);
200 END_SAVE
201 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 return posix_error();
203 INCREF(None);
204 return None;
205}
206
207static object *
208posix_do_stat(self, args, statfunc)
209 object *self;
210 object *args;
211 int (*statfunc) FPROTO((const char *, struct stat *));
212{
213 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000214 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000215 int res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216 if (!getstrarg(args, &path))
217 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000218 BGN_SAVE
219 res = (*statfunc)(path, &st);
220 END_SAVE
221 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 return posix_error();
Guido van Rossum687dd131993-05-17 08:34:16 +0000223 return mkvalue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000224 (long)st.st_mode,
225 (long)st.st_ino,
226 (long)st.st_dev,
227 (long)st.st_nlink,
228 (long)st.st_uid,
229 (long)st.st_gid,
230 (long)st.st_size,
231 (long)st.st_atime,
232 (long)st.st_mtime,
233 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234}
235
236
237/* POSIX methods */
238
239static object *
240posix_chdir(self, args)
241 object *self;
242 object *args;
243{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 return posix_1str(args, chdir);
245}
246
247static object *
248posix_chmod(self, args)
249 object *self;
250 object *args;
251{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 return posix_strint(args, chmod);
253}
254
255static object *
256posix_getcwd(self, args)
257 object *self;
258 object *args;
259{
260 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000261 char *res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 if (!getnoarg(args))
263 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000264 BGN_SAVE
265 res = getcwd(buf, sizeof buf);
266 END_SAVE
267 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 return posix_error();
269 return newstringobject(buf);
270}
271
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000272#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273static object *
274posix_link(self, args)
275 object *self;
276 object *args;
277{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278 return posix_2str(args, link);
279}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000280#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
282static object *
283posix_listdir(self, args)
284 object *self;
285 object *args;
286{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000287 char *name;
288 object *d, *v;
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000289
Guido van Rossume22e6441993-07-09 10:51:31 +0000290#ifdef TURBO_C
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000291 struct ffblk ep;
292 int rv;
293 if (!getstrarg(args, &name))
294 return NULL;
295
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000296 if (findfirst(name, &ep, 0) == -1)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000297 return posix_error();
298 if ((d = newlistobject(0)) == NULL)
299 return NULL;
300 do {
301 v = newstringobject(ep.ff_name);
302 if (v == NULL) {
303 DECREF(d);
304 d = NULL;
305 break;
306 }
307 if (addlistitem(d, v) != 0) {
308 DECREF(v);
309 DECREF(d);
310 d = NULL;
311 break;
312 }
313 DECREF(v);
314 } while ((rv = findnext(&ep)) == 0);
Guido van Rossume22e6441993-07-09 10:51:31 +0000315#endif /* TURBO_C */
316#ifdef MSDOS
317 struct find_t ep;
318 int rv;
319 char _name[100];
320 int attrib;
321 int num= 0;
322
323 if (!getstrarg(args, &name))
324 return NULL;
325 strcpy( _name, name );
326
327again:
328 if ((d = newlistobject(0)) == NULL)
329 return NULL;
330
331 if( _name[strlen( _name )-1]=='/' )
332 strcat( _name, "*.*" );
333
334 if (_dos_findfirst(_name, _A_NORMAL|_A_SUBDIR, &ep) == -1)
335 return posix_error();
336 attrib= ep.attrib;
337 do {
338 v = newstringobject(ep.name);
339 if (v == NULL) {
340 DECREF(d);
341 d = NULL;
342 break;
343 }
344 if (addlistitem(d, v) != 0) {
345 DECREF(v);
346 DECREF(d);
347 d = NULL;
348 break;
349 }
350 num++;
351 DECREF(v);
352 } while ((rv = _dos_findnext(&ep)) == 0);
353
354 if( attrib&_A_SUBDIR && num==1 )
355 {
356 DECREF( d );
357 strcat( _name, "/*.*" );
Guido van Rossum06191531993-10-26 15:22:37 +0000358 /* This comment is here to help the DEC alpha OSF/1 cpp
359 (which scans for comments but not for strings in
360 code that is #ifdef'ed out...) */
Guido van Rossume22e6441993-07-09 10:51:31 +0000361 goto again;
362 }
363
364#endif /* MSDOS */
365#ifdef unix
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 DIR *dirp;
367 struct direct *ep;
368 if (!getstrarg(args, &name))
369 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000370 BGN_SAVE
371 if ((dirp = opendir(name)) == NULL) {
372 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000374 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 if ((d = newlistobject(0)) == NULL) {
376 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000377 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378 return NULL;
379 }
380 while ((ep = readdir(dirp)) != NULL) {
381 v = newstringobject(ep->d_name);
382 if (v == NULL) {
383 DECREF(d);
384 d = NULL;
385 break;
386 }
387 if (addlistitem(d, v) != 0) {
388 DECREF(v);
389 DECREF(d);
390 d = NULL;
391 break;
392 }
393 DECREF(v);
394 }
395 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000396 END_SAVE
Guido van Rossume22e6441993-07-09 10:51:31 +0000397#endif /* unix */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000398
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 return d;
400}
401
402static object *
403posix_mkdir(self, args)
404 object *self;
405 object *args;
406{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 return posix_strint(args, mkdir);
408}
409
Guido van Rossum775f4da1993-01-09 17:18:52 +0000410#ifndef MSDOS
411static object *
412posix_nice(self, args)
413 object *self;
414 object *args;
415{
416 int increment, value;
417
418 if (!getargs(args, "i", &increment))
419 return NULL;
420 value = nice(increment);
421 if (value == -1)
422 return posix_error();
423 return newintobject((long) value);
424}
425#endif
426
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000427#if i386 && ! _SEQUENT_
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000428int
429rename(from, to)
430 char *from;
431 char *to;
432{
433 int status;
434 /* XXX Shouldn't this unlink the destination first? */
435 status = link(from, to);
436 if (status != 0)
437 return status;
438 return unlink(from);
439}
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000440#endif /* i386 && ! _SEQUENT_ */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000441
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442static object *
443posix_rename(self, args)
444 object *self;
445 object *args;
446{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447 return posix_2str(args, rename);
448}
449
450static object *
451posix_rmdir(self, args)
452 object *self;
453 object *args;
454{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455 return posix_1str(args, rmdir);
456}
457
458static object *
459posix_stat(self, args)
460 object *self;
461 object *args;
462{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 return posix_do_stat(self, args, stat);
464}
465
466static object *
467posix_system(self, args)
468 object *self;
469 object *args;
470{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000471 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000472 long sts;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473 if (!getstrarg(args, &command))
474 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000475 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000476 sts = system(command);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000477 END_SAVE
478 return newintobject(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479}
480
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000481#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482static object *
483posix_umask(self, args)
484 object *self;
485 object *args;
486{
487 int i;
488 if (!getintarg(args, &i))
489 return NULL;
490 i = umask(i);
491 if (i < 0)
492 return posix_error();
493 return newintobject((long)i);
494}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000495#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496
497static object *
498posix_unlink(self, args)
499 object *self;
500 object *args;
501{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502 return posix_1str(args, unlink);
503}
504
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000505#ifndef NO_UNAME
506#include <sys/utsname.h>
507
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000508extern int uname PROTO((struct utsname *));
509
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000510static object *
511posix_uname(self, args)
512 object *self;
513 object *args;
514{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000515 struct utsname u;
516 object *v;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000517 int res;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000518 if (!getnoarg(args))
519 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000520 BGN_SAVE
521 res = uname(&u);
522 END_SAVE
523 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000524 return posix_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000525 return mkvalue("(sssss)",
526 u.sysname,
527 u.nodename,
528 u.release,
529 u.version,
530 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000531}
532#endif /* NO_UNAME */
533
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000534#ifdef UTIME_STRUCT
535#include <utime.h>
536#endif
537
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000539posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540 object *self;
541 object *args;
542{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000543 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000544 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000545
546#ifdef UTIME_STRUCT
547 struct utimbuf buf;
548#define ATIME buf.actime
549#define MTIME buf.modtime
550#define UTIME_ARG &buf
551
552#else
553 time_t buf[2];
554#define ATIME buf[0]
555#define MTIME buf[1]
556#define UTIME_ARG buf
557#endif
558
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000559 if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000561 BGN_SAVE
562 res = utime(path, UTIME_ARG);
563 END_SAVE
564 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 return posix_error();
566 INCREF(None);
567 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000568#undef UTIME_ARG
569#undef ATIME
570#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571}
572
Guido van Rossum85e3b011991-06-03 12:42:10 +0000573
574#ifndef MSDOS
575
Guido van Rossum3b066191991-06-04 19:40:25 +0000576/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000577
578static object *
579posix__exit(self, args)
580 object *self;
581 object *args;
582{
583 int sts;
584 if (!getintarg(args, &sts))
585 return NULL;
586 _exit(sts);
587 /* NOTREACHED */
588}
589
590/* XXX To do: exece, execp */
591
592static object *
Guido van Rossum89b33251993-10-22 14:26:06 +0000593posix_execv(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000594 object *self;
595 object *args;
596{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000597 char *path;
598 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000599 char **argvlist;
600 int i, argc;
601 object *(*getitem) PROTO((object *, int));
602
Guido van Rossum89b33251993-10-22 14:26:06 +0000603 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000604 argv is a list or tuple of strings. */
605
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000606 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000607 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000608 if (is_listobject(argv)) {
609 argc = getlistsize(argv);
610 getitem = getlistitem;
611 }
612 else if (is_tupleobject(argv)) {
613 argc = gettuplesize(argv);
614 getitem = gettupleitem;
615 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000616 else {
617 badarg:
618 err_badarg();
619 return NULL;
620 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000621
622 argvlist = NEW(char *, argc+1);
623 if (argvlist == NULL)
624 return NULL;
625 for (i = 0; i < argc; i++) {
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000626 if (!getstrarg((*getitem)(argv, i), &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000627 DEL(argvlist);
628 goto badarg;
629 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000630 }
631 argvlist[argc] = NULL;
632
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000633 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000634
635 /* If we get here it's definitely an error */
636
637 DEL(argvlist);
638 return posix_error();
639}
640
641static object *
642posix_fork(self, args)
643 object *self;
644 object *args;
645{
646 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000647 if (!getnoarg(args))
648 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000649 pid = fork();
650 if (pid == -1)
651 return posix_error();
652 return newintobject((long)pid);
653}
654
655static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000656posix_getegid(self, args)
657 object *self;
658 object *args;
659{
660 if (!getnoarg(args))
661 return NULL;
662 return newintobject((long)getegid());
663}
664
665static object *
666posix_geteuid(self, args)
667 object *self;
668 object *args;
669{
670 if (!getnoarg(args))
671 return NULL;
672 return newintobject((long)geteuid());
673}
674
675static object *
676posix_getgid(self, args)
677 object *self;
678 object *args;
679{
680 if (!getnoarg(args))
681 return NULL;
682 return newintobject((long)getgid());
683}
684
685static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000686posix_getpid(self, args)
687 object *self;
688 object *args;
689{
Guido van Rossum04814471991-06-04 20:23:49 +0000690 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000691 return NULL;
692 return newintobject((long)getpid());
693}
694
695static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000696posix_getpgrp(self, args)
697 object *self;
698 object *args;
699{
700 if (!getnoarg(args))
701 return NULL;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000702#ifdef SYSV
703 return newintobject((long)getpgrp());
704#else
Guido van Rossum971443b1991-06-07 13:59:29 +0000705 return newintobject((long)getpgrp(0));
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000706#endif
Guido van Rossum04814471991-06-04 20:23:49 +0000707}
708
709static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +0000710posix_setpgrp(self, args)
711 object *self;
712 object *args;
713{
714 if (!getnoarg(args))
715 return NULL;
716#ifdef SYSV
717 if (setpgrp() < 0)
718#else
719 if (setpgrp(0, 0) < 0)
720#endif
Guido van Rossum687dd131993-05-17 08:34:16 +0000721 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000722 INCREF(None);
723 return None;
724}
725
726static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000727posix_getppid(self, args)
728 object *self;
729 object *args;
730{
Guido van Rossum04814471991-06-04 20:23:49 +0000731 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000732 return NULL;
733 return newintobject((long)getppid());
734}
735
736static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000737posix_getuid(self, args)
738 object *self;
739 object *args;
740{
741 if (!getnoarg(args))
742 return NULL;
743 return newintobject((long)getuid());
744}
745
746static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000747posix_kill(self, args)
748 object *self;
749 object *args;
750{
751 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +0000752 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000753 return NULL;
754 if (kill(pid, sig) == -1)
755 return posix_error();
756 INCREF(None);
757 return None;
758}
759
760static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000761posix_popen(self, args)
762 object *self;
763 object *args;
764{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000765 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000766 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000767 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000768 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000769 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000770 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000771 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +0000772 if (fp == NULL)
773 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000774 /* From now on, ignore SIGPIPE and let the error checking
775 do the work. */
776 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000777 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000778}
779
780static object *
Guido van Rossum21803b81992-08-09 12:55:27 +0000781posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000782 object *self;
783 object *args;
784{
Guido van Rossum85e3b011991-06-03 12:42:10 +0000785#ifdef NO_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +0000786 err_setstr(PosixError,
787 "posix.waitpid() not supported on this system");
788 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000789#else
Guido van Rossum21803b81992-08-09 12:55:27 +0000790 int pid, options, sts;
791 if (!getargs(args, "(ii)", &pid, &options))
792 return NULL;
793 BGN_SAVE
794 pid = waitpid(pid, &sts, options);
795 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +0000796 if (pid == -1)
797 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +0000798 else
799 return mkvalue("ii", pid, sts);
800#endif
801}
802
803static object *
804posix_wait(self, args)
805 object *self;
806 object *args;
807{
808 int pid, sts;
809 if (args != NULL)
810 return posix_waitpid(self, args); /* BW compat */
811 BGN_SAVE
812 pid = wait(&sts);
813 END_SAVE
814 if (pid == -1)
815 return posix_error();
816 else
817 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000818}
819
820#endif /* MSDOS */
821
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822static object *
823posix_lstat(self, args)
824 object *self;
825 object *args;
826{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827 return posix_do_stat(self, args, lstat);
828}
829
830static object *
831posix_readlink(self, args)
832 object *self;
833 object *args;
834{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000835#ifdef NO_LSTAT
836 err_setstr(PosixError, "readlink not implemented on this system");
837 return NULL;
838#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000840 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000841 int n;
842 if (!getstrarg(args, &path))
843 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000844 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000845 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000846 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847 if (n < 0)
848 return posix_error();
849 return newsizedstringobject(buf, n);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000850#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000851}
852
853static object *
854posix_symlink(self, args)
855 object *self;
856 object *args;
857{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000858#ifdef NO_LSTAT
859 err_setstr(PosixError, "symlink not implemented on this system");
860 return NULL;
861#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862 return posix_2str(args, symlink);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000863#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000864}
865
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866
Guido van Rossum22db57e1992-04-05 14:25:30 +0000867#ifdef DO_TIMES
868
869static object *
870posix_times(self, args)
871 object *self;
872 object *args;
873{
874 struct tms t;
875 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +0000876 if (!getnoarg(args))
877 return NULL;
878 errno = 0;
879 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +0000880 if (c == (clock_t) -1)
881 return posix_error();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000882 return mkvalue("dddd",
883 (double)t.tms_utime / HZ,
884 (double)t.tms_stime / HZ,
885 (double)t.tms_cutime / HZ,
886 (double)t.tms_cstime / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +0000887}
888
Guido van Rossumc2670a01992-09-13 20:07:29 +0000889#endif /* DO_TIMES */
890
891#ifdef DO_PG
892
893static object *
894posix_setsid(self, args)
895 object *self;
896 object *args;
897{
898 if (!getnoarg(args))
899 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000900 if (setsid() < 0)
901 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000902 INCREF(None);
903 return None;
904}
905
906static object *
907posix_setpgid(self, args)
908 object *self;
909 object *args;
910{
911 int pid, pgrp;
912 if (!getargs(args, "(ii)", &pid, &pgrp))
913 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000914 if (setpgid(pid, pgrp) < 0)
915 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000916 INCREF(None);
917 return None;
918}
919
Guido van Rossum7066dd71992-09-17 17:54:56 +0000920static object *
921posix_tcgetpgrp(self, args)
922 object *self;
923 object *args;
924{
925 int fd, pgid;
926 if (!getargs(args, "i", &fd))
927 return NULL;
928 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +0000929 if (pgid < 0)
930 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000931 return newintobject((long)pgid);
932}
933
934static object *
935posix_tcsetpgrp(self, args)
936 object *self;
937 object *args;
938{
939 int fd, pgid;
940 if (!getargs(args, "(ii)", &fd, &pgid))
941 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000942 if (tcsetpgrp(fd, pgid) < 0)
943 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000944 INCREF(None);
945 return None;
946}
947
Guido van Rossumc2670a01992-09-13 20:07:29 +0000948#endif /* DO_PG */
Guido van Rossum22db57e1992-04-05 14:25:30 +0000949
Guido van Rossum687dd131993-05-17 08:34:16 +0000950/* Functions acting on file descriptors */
951
Guido van Rossum234f9421993-06-17 12:35:49 +0000952static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000953posix_open(self, args)
954 object *self;
955 object *args;
956{
957 char *file;
958 int flag;
959 int mode = 0777;
960 int fd;
961 if (!getargs(args, "(si)", &file, &flag)) {
962 err_clear();
963 if (!getargs(args, "(sii)", &file, &flag, &mode))
964 return NULL;
965 }
966 BGN_SAVE
967 fd = open(file, flag, mode);
968 END_SAVE
969 if (fd < 0)
970 return posix_error();
971 return newintobject((long)fd);
972}
973
Guido van Rossum234f9421993-06-17 12:35:49 +0000974static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000975posix_close(self, args)
976 object *self;
977 object *args;
978{
979 int fd, res;
980 if (!getargs(args, "i", &fd))
981 return NULL;
982 BGN_SAVE
983 res = close(fd);
984 END_SAVE
985 if (res < 0)
986 return posix_error();
987 INCREF(None);
988 return None;
989}
990
Guido van Rossum234f9421993-06-17 12:35:49 +0000991static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000992posix_dup(self, args)
993 object *self;
994 object *args;
995{
996 int fd;
997 if (!getargs(args, "i", &fd))
998 return NULL;
999 BGN_SAVE
1000 fd = dup(fd);
1001 END_SAVE
1002 if (fd < 0)
1003 return posix_error();
1004 return newintobject((long)fd);
1005}
1006
Guido van Rossum234f9421993-06-17 12:35:49 +00001007static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001008posix_dup2(self, args)
1009 object *self;
1010 object *args;
1011{
1012 int fd, fd2, res;
1013 if (!getargs(args, "(ii)", &fd, &fd2))
1014 return NULL;
1015 BGN_SAVE
1016 res = dup2(fd, fd2);
1017 END_SAVE
1018 if (res < 0)
1019 return posix_error();
1020 INCREF(None);
1021 return None;
1022}
1023
Guido van Rossum234f9421993-06-17 12:35:49 +00001024static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001025posix_lseek(self, args)
1026 object *self;
1027 object *args;
1028{
1029 int fd, how;
1030 long pos, res;
1031 if (!getargs(args, "(ili)", &fd, &pos, &how))
1032 return NULL;
1033#ifdef SEEK_SET
1034 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1035 switch (how) {
1036 case 0: how = SEEK_SET; break;
1037 case 1: how = SEEK_CUR; break;
1038 case 2: how = SEEK_END; break;
1039 }
1040#endif
1041 BGN_SAVE
1042 res = lseek(fd, pos, how);
1043 END_SAVE
1044 if (res < 0)
1045 return posix_error();
1046 return newintobject(res);
1047}
1048
Guido van Rossum234f9421993-06-17 12:35:49 +00001049static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001050posix_read(self, args)
1051 object *self;
1052 object *args;
1053{
1054 int fd, size;
1055 object *buffer;
1056 if (!getargs(args, "(ii)", &fd, &size))
1057 return NULL;
1058 buffer = newsizedstringobject((char *)NULL, size);
1059 if (buffer == NULL)
1060 return NULL;
1061 BGN_SAVE
1062 size = read(fd, getstringvalue(buffer), size);
1063 END_SAVE
1064 if (size < 0) {
1065 DECREF(buffer);
1066 return posix_error();
1067 }
1068 resizestring(&buffer, size);
1069 return buffer;
1070}
1071
Guido van Rossum234f9421993-06-17 12:35:49 +00001072static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001073posix_write(self, args)
1074 object *self;
1075 object *args;
1076{
1077 int fd, size;
1078 char *buffer;
1079 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1080 return NULL;
1081 BGN_SAVE
1082 size = write(fd, buffer, size);
1083 END_SAVE
1084 if (size < 0)
1085 return posix_error();
1086 return newintobject((long)size);
1087}
1088
Guido van Rossum234f9421993-06-17 12:35:49 +00001089static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001090posix_fstat(self, args)
1091 object *self;
1092 object *args;
1093{
1094 int fd;
1095 struct stat st;
1096 int res;
1097 if (!getargs(args, "i", &fd))
1098 return NULL;
1099 BGN_SAVE
1100 res = fstat(fd, &st);
1101 END_SAVE
1102 if (res != 0)
1103 return posix_error();
1104 return mkvalue("(llllllllll)",
1105 (long)st.st_mode,
1106 (long)st.st_ino,
1107 (long)st.st_dev,
1108 (long)st.st_nlink,
1109 (long)st.st_uid,
1110 (long)st.st_gid,
1111 (long)st.st_size,
1112 (long)st.st_atime,
1113 (long)st.st_mtime,
1114 (long)st.st_ctime);
1115}
1116
1117static object *
1118posix_fdopen(self, args)
1119 object *self;
1120 object *args;
1121{
1122 extern int fclose PROTO((FILE *));
1123 int fd;
1124 char *mode;
1125 FILE *fp;
1126 if (!getargs(args, "(is)", &fd, &mode))
1127 return NULL;
1128 BGN_SAVE
1129 fp = fdopen(fd, mode);
1130 END_SAVE
1131 if (fp == NULL)
1132 return posix_error();
1133 /* From now on, ignore SIGPIPE and let the error checking
1134 do the work. */
1135 (void) signal(SIGPIPE, SIG_IGN);
1136 return newopenfileobject(fp, "(fdopen)", mode, fclose);
1137}
1138
1139#ifndef MSDOS
Guido van Rossum234f9421993-06-17 12:35:49 +00001140static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001141posix_pipe(self, args)
1142 object *self;
1143 object *args;
1144{
1145 int fds[2];
1146 int res;
1147 if (!getargs(args, ""))
1148 return NULL;
1149 BGN_SAVE
1150 res = pipe(fds);
1151 END_SAVE
1152 if (res != 0)
1153 return posix_error();
1154 return mkvalue("(ii)", fds[0], fds[1]);
1155}
1156#endif /* MSDOS */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001157
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001158static struct methodlist posix_methods[] = {
1159 {"chdir", posix_chdir},
1160 {"chmod", posix_chmod},
1161 {"getcwd", posix_getcwd},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001162#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163 {"link", posix_link},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001164#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001166 {"lstat", posix_lstat},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001167 {"mkdir", posix_mkdir},
Guido van Rossum775f4da1993-01-09 17:18:52 +00001168#ifndef MSDOS
1169 {"nice", posix_nice},
1170#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001171 {"readlink", posix_readlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 {"rename", posix_rename},
1173 {"rmdir", posix_rmdir},
1174 {"stat", posix_stat},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001175 {"symlink", posix_symlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001176 {"system", posix_system},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001177#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001178 {"umask", posix_umask},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001179#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001180#ifndef NO_UNAME
1181 {"uname", posix_uname},
1182#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001183 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001184 {"utime", posix_utime},
Guido van Rossum22db57e1992-04-05 14:25:30 +00001185#ifdef DO_TIMES
1186 {"times", posix_times},
1187#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001188
Guido van Rossum85e3b011991-06-03 12:42:10 +00001189#ifndef MSDOS
1190 {"_exit", posix__exit},
Guido van Rossum89b33251993-10-22 14:26:06 +00001191 {"execv", posix_execv},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001192 {"fork", posix_fork},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001193 {"getegid", posix_getegid},
1194 {"geteuid", posix_geteuid},
1195 {"getgid", posix_getgid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001196 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +00001197 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001198 {"getppid", posix_getppid},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001199 {"getuid", posix_getuid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001200 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +00001201 {"popen", posix_popen},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001202 {"setpgrp", posix_setpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001203 {"wait", posix_wait},
Guido van Rossum21803b81992-08-09 12:55:27 +00001204 {"waitpid", posix_waitpid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001205#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001206
Guido van Rossumc2670a01992-09-13 20:07:29 +00001207#ifdef DO_PG
1208 {"setsid", posix_setsid},
1209 {"setpgid", posix_setpgid},
Guido van Rossum7066dd71992-09-17 17:54:56 +00001210 {"tcgetpgrp", posix_tcgetpgrp},
1211 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001212#endif
1213
Guido van Rossum687dd131993-05-17 08:34:16 +00001214 {"open", posix_open},
1215 {"close", posix_close},
1216 {"dup", posix_dup},
1217 {"dup2", posix_dup2},
1218 {"lseek", posix_lseek},
1219 {"read", posix_read},
1220 {"write", posix_write},
1221 {"fstat", posix_fstat},
1222 {"fdopen", posix_fdopen},
1223#ifndef MSDOS
1224 {"pipe", posix_pipe},
1225#endif
1226
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001227 {NULL, NULL} /* Sentinel */
1228};
1229
1230
1231void
1232initposix()
1233{
1234 object *m, *d, *v;
1235
1236 m = initmodule("posix", posix_methods);
1237 d = getmoduledict(m);
1238
1239 /* Initialize posix.environ dictionary */
1240 v = convertenviron();
1241 if (v == NULL || dictinsert(d, "environ", v) != 0)
1242 fatal("can't define posix.environ");
1243 DECREF(v);
1244
1245 /* Initialize posix.error exception */
1246 PosixError = newstringobject("posix.error");
1247 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1248 fatal("can't define posix.error");
1249}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001250
Guido van Rossum3b066191991-06-04 19:40:25 +00001251
1252/* Function used elsewhere to get a file's modification time */
1253
1254long
1255getmtime(path)
1256 char *path;
1257{
1258 struct stat st;
1259 if (stat(path, &st) != 0)
1260 return -1;
1261 else
1262 return st.st_mtime;
1263}
1264
1265
Guido van Rossume22e6441993-07-09 10:51:31 +00001266#ifdef TURBO_C
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001267
1268/* A small "compatibility library" for TurboC under MS-DOS */
1269
Guido van Rossume22e6441993-07-09 10:51:31 +00001270//#include <sir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001271#include <io.h>
1272#include <dos.h>
1273#include <fcntl.h>
1274
1275int
1276chmod(path, mode)
1277 char *path;
1278 int mode;
1279{
1280 return _chmod(path, 1, mode);
1281}
1282
1283int
1284utime(path, times)
1285 char *path;
1286 time_t times[2];
1287{
1288 struct date dt;
1289 struct time tm;
1290 struct ftime dft;
1291 int fh;
1292 unixtodos(tv[0].tv_sec,&dt,&tm);
1293 dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min;
1294 dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day;
1295 dft.ft_month = dt.da_mon;
1296 dft.ft_year = (dt.da_year - 1980); /* this is for TC library */
1297
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001298 if ((fh = open(path,O_RDWR)) < 0)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001299 return posix_error(); /* can't open file to set time */
1300 if (setftime(fh,&dft) < 0)
1301 {
1302 close(fh);
1303 return posix_error();
1304 }
1305 close(fh); /* close the temp handle */
1306}
1307
Guido van Rossume22e6441993-07-09 10:51:31 +00001308#endif /* TURBO_C */