blob: d84360bf9d4d308f1e9f468c186800a88e2bc954 [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 Rossumc6dcc9f1993-11-05 10:15:19 +0000159 if (!getargs(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 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 Rossumc6dcc9f1993-11-05 10:15:19 +0000216 if (!getargs(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217 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;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000293 if (!getargs(args, "s", &name))
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000294 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
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000323 if (!getargs(args, "s", &name))
Guido van Rossume22e6441993-07-09 10:51:31 +0000324 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;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000368 if (!getargs(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 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 Rossumc6dcc9f1993-11-05 10:15:19 +0000473 if (!getargs(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 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
Guido van Rossum85e3b011991-06-03 12:42:10 +0000590static object *
Guido van Rossum89b33251993-10-22 14:26:06 +0000591posix_execv(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000592 object *self;
593 object *args;
594{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000595 char *path;
596 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000597 char **argvlist;
598 int i, argc;
599 object *(*getitem) PROTO((object *, int));
600
Guido van Rossum89b33251993-10-22 14:26:06 +0000601 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000602 argv is a list or tuple of strings. */
603
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000604 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000605 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000606 if (is_listobject(argv)) {
607 argc = getlistsize(argv);
608 getitem = getlistitem;
609 }
610 else if (is_tupleobject(argv)) {
611 argc = gettuplesize(argv);
612 getitem = gettupleitem;
613 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000614 else {
615 badarg:
616 err_badarg();
617 return NULL;
618 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000619
620 argvlist = NEW(char *, argc+1);
621 if (argvlist == NULL)
622 return NULL;
623 for (i = 0; i < argc; i++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000624 if (!getargs((*getitem)(argv, i), "s", &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000625 DEL(argvlist);
626 goto badarg;
627 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000628 }
629 argvlist[argc] = NULL;
630
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000631 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000632
633 /* If we get here it's definitely an error */
634
635 DEL(argvlist);
636 return posix_error();
637}
638
639static object *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000640posix_execve(self, args)
641 object *self;
642 object *args;
643{
644 char *path;
645 object *argv, *env;
646 char **argvlist;
647 char **envlist;
648 object *key, *val;
649 int i, pos, argc, envc;
650 object *(*getitem) PROTO((object *, int));
651
652 /* execve has three arguments: (path, argv, env), where
653 argv is a list or tuple of strings and env is a dictionary
654 like posix.environ. */
655
656 if (!getargs(args, "(sOO)", &path, &argv, &env))
657 return NULL;
658 if (is_listobject(argv)) {
659 argc = getlistsize(argv);
660 getitem = getlistitem;
661 }
662 else if (is_tupleobject(argv)) {
663 argc = gettuplesize(argv);
664 getitem = gettupleitem;
665 }
666 else {
667 err_setstr(TypeError, "argv must be tuple or list");
668 return NULL;
669 }
670 if (!is_dictobject(env)) {
671 err_setstr(TypeError, "env must be dictionary");
672 return NULL;
673 }
674
675 argvlist = NEW(char *, argc+1);
676 if (argvlist == NULL) {
677 err_nomem();
678 return NULL;
679 }
680 for (i = 0; i < argc; i++) {
681 if (!getargs((*getitem)(argv, i),
682 "s;argv must be list of strings",
683 &argvlist[i])) {
684 goto fail_1;
685 }
686 }
687 argvlist[argc] = NULL;
688
689 i = getmappingsize(env);
690 envlist = NEW(char *, i + 1);
691 if (envlist == NULL) {
692 err_nomem();
693 goto fail_1;
694 }
695 pos = 0;
696 envc = 0;
697 while (mappinggetnext(env, &pos, &key, &val)) {
698 char *p, *k, *v;
699 if (!getargs(key, "s;non-string key in env", &k) ||
700 !getargs(val, "s;non-string value in env", &v)) {
701 goto fail_2;
702 }
703 p = NEW(char, getstringsize(key) + getstringsize(val) + 2);
704 if (p == NULL) {
705 err_nomem();
706 goto fail_2;
707 }
708 sprintf(p, "%s=%s", k, v);
709 envlist[envc++] = p;
710 }
711 envlist[envc] = 0;
712
713 execve(path, argvlist, envlist);
714
715 /* If we get here it's definitely an error */
716
717 (void) posix_error();
718
719 fail_2:
720 while (--envc >= 0)
721 DEL(envlist[envc]);
722 DEL(envlist);
723 fail_1:
724 DEL(argvlist);
725
726 return NULL;
727}
728
729static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000730posix_fork(self, args)
731 object *self;
732 object *args;
733{
734 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000735 if (!getnoarg(args))
736 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000737 pid = fork();
738 if (pid == -1)
739 return posix_error();
740 return newintobject((long)pid);
741}
742
743static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000744posix_getegid(self, args)
745 object *self;
746 object *args;
747{
748 if (!getnoarg(args))
749 return NULL;
750 return newintobject((long)getegid());
751}
752
753static object *
754posix_geteuid(self, args)
755 object *self;
756 object *args;
757{
758 if (!getnoarg(args))
759 return NULL;
760 return newintobject((long)geteuid());
761}
762
763static object *
764posix_getgid(self, args)
765 object *self;
766 object *args;
767{
768 if (!getnoarg(args))
769 return NULL;
770 return newintobject((long)getgid());
771}
772
773static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000774posix_getpid(self, args)
775 object *self;
776 object *args;
777{
Guido van Rossum04814471991-06-04 20:23:49 +0000778 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000779 return NULL;
780 return newintobject((long)getpid());
781}
782
783static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000784posix_getpgrp(self, args)
785 object *self;
786 object *args;
787{
788 if (!getnoarg(args))
789 return NULL;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000790#ifdef SYSV
791 return newintobject((long)getpgrp());
792#else
Guido van Rossum971443b1991-06-07 13:59:29 +0000793 return newintobject((long)getpgrp(0));
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000794#endif
Guido van Rossum04814471991-06-04 20:23:49 +0000795}
796
797static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +0000798posix_setpgrp(self, args)
799 object *self;
800 object *args;
801{
802 if (!getnoarg(args))
803 return NULL;
804#ifdef SYSV
805 if (setpgrp() < 0)
806#else
807 if (setpgrp(0, 0) < 0)
808#endif
Guido van Rossum687dd131993-05-17 08:34:16 +0000809 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000810 INCREF(None);
811 return None;
812}
813
814static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000815posix_getppid(self, args)
816 object *self;
817 object *args;
818{
Guido van Rossum04814471991-06-04 20:23:49 +0000819 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000820 return NULL;
821 return newintobject((long)getppid());
822}
823
824static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000825posix_getuid(self, args)
826 object *self;
827 object *args;
828{
829 if (!getnoarg(args))
830 return NULL;
831 return newintobject((long)getuid());
832}
833
834static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000835posix_kill(self, args)
836 object *self;
837 object *args;
838{
839 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +0000840 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000841 return NULL;
842 if (kill(pid, sig) == -1)
843 return posix_error();
844 INCREF(None);
845 return None;
846}
847
848static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000849posix_popen(self, args)
850 object *self;
851 object *args;
852{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000853 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000854 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000855 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000856 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000857 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000858 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000859 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +0000860 if (fp == NULL)
861 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000862 /* From now on, ignore SIGPIPE and let the error checking
863 do the work. */
864 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000865 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000866}
867
868static object *
Guido van Rossum21803b81992-08-09 12:55:27 +0000869posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000870 object *self;
871 object *args;
872{
Guido van Rossum85e3b011991-06-03 12:42:10 +0000873#ifdef NO_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +0000874 err_setstr(PosixError,
875 "posix.waitpid() not supported on this system");
876 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000877#else
Guido van Rossum21803b81992-08-09 12:55:27 +0000878 int pid, options, sts;
879 if (!getargs(args, "(ii)", &pid, &options))
880 return NULL;
881 BGN_SAVE
882 pid = waitpid(pid, &sts, options);
883 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +0000884 if (pid == -1)
885 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +0000886 else
887 return mkvalue("ii", pid, sts);
888#endif
889}
890
891static object *
892posix_wait(self, args)
893 object *self;
894 object *args;
895{
896 int pid, sts;
897 if (args != NULL)
898 return posix_waitpid(self, args); /* BW compat */
899 BGN_SAVE
900 pid = wait(&sts);
901 END_SAVE
902 if (pid == -1)
903 return posix_error();
904 else
905 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000906}
907
908#endif /* MSDOS */
909
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910static object *
911posix_lstat(self, args)
912 object *self;
913 object *args;
914{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000915 return posix_do_stat(self, args, lstat);
916}
917
918static object *
919posix_readlink(self, args)
920 object *self;
921 object *args;
922{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000923#ifdef NO_LSTAT
924 err_setstr(PosixError, "readlink not implemented on this system");
925 return NULL;
926#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000927 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000928 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929 int n;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000930 if (!getargs(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000931 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000932 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000933 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000934 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000935 if (n < 0)
936 return posix_error();
937 return newsizedstringobject(buf, n);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000938#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000939}
940
941static object *
942posix_symlink(self, args)
943 object *self;
944 object *args;
945{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000946#ifdef NO_LSTAT
947 err_setstr(PosixError, "symlink not implemented on this system");
948 return NULL;
949#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000950 return posix_2str(args, symlink);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000951#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952}
953
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954
Guido van Rossum22db57e1992-04-05 14:25:30 +0000955#ifdef DO_TIMES
956
957static object *
958posix_times(self, args)
959 object *self;
960 object *args;
961{
962 struct tms t;
963 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +0000964 if (!getnoarg(args))
965 return NULL;
966 errno = 0;
967 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +0000968 if (c == (clock_t) -1)
969 return posix_error();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000970 return mkvalue("dddd",
971 (double)t.tms_utime / HZ,
972 (double)t.tms_stime / HZ,
973 (double)t.tms_cutime / HZ,
974 (double)t.tms_cstime / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +0000975}
976
Guido van Rossumc2670a01992-09-13 20:07:29 +0000977#endif /* DO_TIMES */
978
979#ifdef DO_PG
980
981static object *
982posix_setsid(self, args)
983 object *self;
984 object *args;
985{
986 if (!getnoarg(args))
987 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000988 if (setsid() < 0)
989 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000990 INCREF(None);
991 return None;
992}
993
994static object *
995posix_setpgid(self, args)
996 object *self;
997 object *args;
998{
999 int pid, pgrp;
1000 if (!getargs(args, "(ii)", &pid, &pgrp))
1001 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001002 if (setpgid(pid, pgrp) < 0)
1003 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +00001004 INCREF(None);
1005 return None;
1006}
1007
Guido van Rossum7066dd71992-09-17 17:54:56 +00001008static object *
1009posix_tcgetpgrp(self, args)
1010 object *self;
1011 object *args;
1012{
1013 int fd, pgid;
1014 if (!getargs(args, "i", &fd))
1015 return NULL;
1016 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001017 if (pgid < 0)
1018 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +00001019 return newintobject((long)pgid);
1020}
1021
1022static object *
1023posix_tcsetpgrp(self, args)
1024 object *self;
1025 object *args;
1026{
1027 int fd, pgid;
1028 if (!getargs(args, "(ii)", &fd, &pgid))
1029 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001030 if (tcsetpgrp(fd, pgid) < 0)
1031 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +00001032 INCREF(None);
1033 return None;
1034}
1035
Guido van Rossumc2670a01992-09-13 20:07:29 +00001036#endif /* DO_PG */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001037
Guido van Rossum687dd131993-05-17 08:34:16 +00001038/* Functions acting on file descriptors */
1039
Guido van Rossum234f9421993-06-17 12:35:49 +00001040static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001041posix_open(self, args)
1042 object *self;
1043 object *args;
1044{
1045 char *file;
1046 int flag;
1047 int mode = 0777;
1048 int fd;
1049 if (!getargs(args, "(si)", &file, &flag)) {
1050 err_clear();
1051 if (!getargs(args, "(sii)", &file, &flag, &mode))
1052 return NULL;
1053 }
1054 BGN_SAVE
1055 fd = open(file, flag, mode);
1056 END_SAVE
1057 if (fd < 0)
1058 return posix_error();
1059 return newintobject((long)fd);
1060}
1061
Guido van Rossum234f9421993-06-17 12:35:49 +00001062static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001063posix_close(self, args)
1064 object *self;
1065 object *args;
1066{
1067 int fd, res;
1068 if (!getargs(args, "i", &fd))
1069 return NULL;
1070 BGN_SAVE
1071 res = close(fd);
1072 END_SAVE
1073 if (res < 0)
1074 return posix_error();
1075 INCREF(None);
1076 return None;
1077}
1078
Guido van Rossum234f9421993-06-17 12:35:49 +00001079static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001080posix_dup(self, args)
1081 object *self;
1082 object *args;
1083{
1084 int fd;
1085 if (!getargs(args, "i", &fd))
1086 return NULL;
1087 BGN_SAVE
1088 fd = dup(fd);
1089 END_SAVE
1090 if (fd < 0)
1091 return posix_error();
1092 return newintobject((long)fd);
1093}
1094
Guido van Rossum234f9421993-06-17 12:35:49 +00001095static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001096posix_dup2(self, args)
1097 object *self;
1098 object *args;
1099{
1100 int fd, fd2, res;
1101 if (!getargs(args, "(ii)", &fd, &fd2))
1102 return NULL;
1103 BGN_SAVE
1104 res = dup2(fd, fd2);
1105 END_SAVE
1106 if (res < 0)
1107 return posix_error();
1108 INCREF(None);
1109 return None;
1110}
1111
Guido van Rossum234f9421993-06-17 12:35:49 +00001112static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001113posix_lseek(self, args)
1114 object *self;
1115 object *args;
1116{
1117 int fd, how;
1118 long pos, res;
1119 if (!getargs(args, "(ili)", &fd, &pos, &how))
1120 return NULL;
1121#ifdef SEEK_SET
1122 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1123 switch (how) {
1124 case 0: how = SEEK_SET; break;
1125 case 1: how = SEEK_CUR; break;
1126 case 2: how = SEEK_END; break;
1127 }
1128#endif
1129 BGN_SAVE
1130 res = lseek(fd, pos, how);
1131 END_SAVE
1132 if (res < 0)
1133 return posix_error();
1134 return newintobject(res);
1135}
1136
Guido van Rossum234f9421993-06-17 12:35:49 +00001137static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001138posix_read(self, args)
1139 object *self;
1140 object *args;
1141{
1142 int fd, size;
1143 object *buffer;
1144 if (!getargs(args, "(ii)", &fd, &size))
1145 return NULL;
1146 buffer = newsizedstringobject((char *)NULL, size);
1147 if (buffer == NULL)
1148 return NULL;
1149 BGN_SAVE
1150 size = read(fd, getstringvalue(buffer), size);
1151 END_SAVE
1152 if (size < 0) {
1153 DECREF(buffer);
1154 return posix_error();
1155 }
1156 resizestring(&buffer, size);
1157 return buffer;
1158}
1159
Guido van Rossum234f9421993-06-17 12:35:49 +00001160static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001161posix_write(self, args)
1162 object *self;
1163 object *args;
1164{
1165 int fd, size;
1166 char *buffer;
1167 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1168 return NULL;
1169 BGN_SAVE
1170 size = write(fd, buffer, size);
1171 END_SAVE
1172 if (size < 0)
1173 return posix_error();
1174 return newintobject((long)size);
1175}
1176
Guido van Rossum234f9421993-06-17 12:35:49 +00001177static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001178posix_fstat(self, args)
1179 object *self;
1180 object *args;
1181{
1182 int fd;
1183 struct stat st;
1184 int res;
1185 if (!getargs(args, "i", &fd))
1186 return NULL;
1187 BGN_SAVE
1188 res = fstat(fd, &st);
1189 END_SAVE
1190 if (res != 0)
1191 return posix_error();
1192 return mkvalue("(llllllllll)",
1193 (long)st.st_mode,
1194 (long)st.st_ino,
1195 (long)st.st_dev,
1196 (long)st.st_nlink,
1197 (long)st.st_uid,
1198 (long)st.st_gid,
1199 (long)st.st_size,
1200 (long)st.st_atime,
1201 (long)st.st_mtime,
1202 (long)st.st_ctime);
1203}
1204
1205static object *
1206posix_fdopen(self, args)
1207 object *self;
1208 object *args;
1209{
1210 extern int fclose PROTO((FILE *));
1211 int fd;
1212 char *mode;
1213 FILE *fp;
1214 if (!getargs(args, "(is)", &fd, &mode))
1215 return NULL;
1216 BGN_SAVE
1217 fp = fdopen(fd, mode);
1218 END_SAVE
1219 if (fp == NULL)
1220 return posix_error();
1221 /* From now on, ignore SIGPIPE and let the error checking
1222 do the work. */
1223 (void) signal(SIGPIPE, SIG_IGN);
1224 return newopenfileobject(fp, "(fdopen)", mode, fclose);
1225}
1226
1227#ifndef MSDOS
Guido van Rossum234f9421993-06-17 12:35:49 +00001228static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001229posix_pipe(self, args)
1230 object *self;
1231 object *args;
1232{
1233 int fds[2];
1234 int res;
1235 if (!getargs(args, ""))
1236 return NULL;
1237 BGN_SAVE
1238 res = pipe(fds);
1239 END_SAVE
1240 if (res != 0)
1241 return posix_error();
1242 return mkvalue("(ii)", fds[0], fds[1]);
1243}
1244#endif /* MSDOS */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001245
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001246static struct methodlist posix_methods[] = {
1247 {"chdir", posix_chdir},
1248 {"chmod", posix_chmod},
1249 {"getcwd", posix_getcwd},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001250#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001251 {"link", posix_link},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001252#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001253 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001254 {"lstat", posix_lstat},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001255 {"mkdir", posix_mkdir},
Guido van Rossum775f4da1993-01-09 17:18:52 +00001256#ifndef MSDOS
1257 {"nice", posix_nice},
1258#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001259 {"readlink", posix_readlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001260 {"rename", posix_rename},
1261 {"rmdir", posix_rmdir},
1262 {"stat", posix_stat},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001263 {"symlink", posix_symlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001264 {"system", posix_system},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001265#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001266 {"umask", posix_umask},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001267#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001268#ifndef NO_UNAME
1269 {"uname", posix_uname},
1270#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001271 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001272 {"utime", posix_utime},
Guido van Rossum22db57e1992-04-05 14:25:30 +00001273#ifdef DO_TIMES
1274 {"times", posix_times},
1275#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001276
Guido van Rossum85e3b011991-06-03 12:42:10 +00001277#ifndef MSDOS
1278 {"_exit", posix__exit},
Guido van Rossum89b33251993-10-22 14:26:06 +00001279 {"execv", posix_execv},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001280 {"execve", posix_execve},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001281 {"fork", posix_fork},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001282 {"getegid", posix_getegid},
1283 {"geteuid", posix_geteuid},
1284 {"getgid", posix_getgid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001285 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +00001286 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001287 {"getppid", posix_getppid},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001288 {"getuid", posix_getuid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001289 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +00001290 {"popen", posix_popen},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001291 {"setpgrp", posix_setpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001292 {"wait", posix_wait},
Guido van Rossum21803b81992-08-09 12:55:27 +00001293 {"waitpid", posix_waitpid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001294#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001295
Guido van Rossumc2670a01992-09-13 20:07:29 +00001296#ifdef DO_PG
1297 {"setsid", posix_setsid},
1298 {"setpgid", posix_setpgid},
Guido van Rossum7066dd71992-09-17 17:54:56 +00001299 {"tcgetpgrp", posix_tcgetpgrp},
1300 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001301#endif
1302
Guido van Rossum687dd131993-05-17 08:34:16 +00001303 {"open", posix_open},
1304 {"close", posix_close},
1305 {"dup", posix_dup},
1306 {"dup2", posix_dup2},
1307 {"lseek", posix_lseek},
1308 {"read", posix_read},
1309 {"write", posix_write},
1310 {"fstat", posix_fstat},
1311 {"fdopen", posix_fdopen},
1312#ifndef MSDOS
1313 {"pipe", posix_pipe},
1314#endif
1315
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001316 {NULL, NULL} /* Sentinel */
1317};
1318
1319
1320void
1321initposix()
1322{
1323 object *m, *d, *v;
1324
1325 m = initmodule("posix", posix_methods);
1326 d = getmoduledict(m);
1327
1328 /* Initialize posix.environ dictionary */
1329 v = convertenviron();
1330 if (v == NULL || dictinsert(d, "environ", v) != 0)
1331 fatal("can't define posix.environ");
1332 DECREF(v);
1333
1334 /* Initialize posix.error exception */
1335 PosixError = newstringobject("posix.error");
1336 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1337 fatal("can't define posix.error");
1338}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001339
Guido van Rossum3b066191991-06-04 19:40:25 +00001340
1341/* Function used elsewhere to get a file's modification time */
1342
1343long
1344getmtime(path)
1345 char *path;
1346{
1347 struct stat st;
1348 if (stat(path, &st) != 0)
1349 return -1;
1350 else
1351 return st.st_mtime;
1352}
1353
1354
Guido van Rossume22e6441993-07-09 10:51:31 +00001355#ifdef TURBO_C
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001356
1357/* A small "compatibility library" for TurboC under MS-DOS */
1358
Guido van Rossume22e6441993-07-09 10:51:31 +00001359//#include <sir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001360#include <io.h>
1361#include <dos.h>
1362#include <fcntl.h>
1363
1364int
1365chmod(path, mode)
1366 char *path;
1367 int mode;
1368{
1369 return _chmod(path, 1, mode);
1370}
1371
1372int
1373utime(path, times)
1374 char *path;
1375 time_t times[2];
1376{
1377 struct date dt;
1378 struct time tm;
1379 struct ftime dft;
1380 int fh;
1381 unixtodos(tv[0].tv_sec,&dt,&tm);
1382 dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min;
1383 dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day;
1384 dft.ft_month = dt.da_mon;
1385 dft.ft_year = (dt.da_year - 1980); /* this is for TC library */
1386
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001387 if ((fh = open(path,O_RDWR)) < 0)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001388 return posix_error(); /* can't open file to set time */
1389 if (setftime(fh,&dft) < 0)
1390 {
1391 close(fh);
1392 return posix_error();
1393 }
1394 close(fh); /* close the temp handle */
1395}
1396
Guido van Rossume22e6441993-07-09 10:51:31 +00001397#endif /* TURBO_C */