blob: df61c25e3b00bcf3b0022413a8ff0ac1b53eb777 [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, "/*.*" );
353 goto again;
354 }
355
356#endif /* MSDOS */
357#ifdef unix
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 DIR *dirp;
359 struct direct *ep;
360 if (!getstrarg(args, &name))
361 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362 BGN_SAVE
363 if ((dirp = opendir(name)) == NULL) {
364 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000366 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 if ((d = newlistobject(0)) == NULL) {
368 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000369 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370 return NULL;
371 }
372 while ((ep = readdir(dirp)) != NULL) {
373 v = newstringobject(ep->d_name);
374 if (v == NULL) {
375 DECREF(d);
376 d = NULL;
377 break;
378 }
379 if (addlistitem(d, v) != 0) {
380 DECREF(v);
381 DECREF(d);
382 d = NULL;
383 break;
384 }
385 DECREF(v);
386 }
387 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000388 END_SAVE
Guido van Rossume22e6441993-07-09 10:51:31 +0000389#endif /* unix */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000390
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391 return d;
392}
393
394static object *
395posix_mkdir(self, args)
396 object *self;
397 object *args;
398{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 return posix_strint(args, mkdir);
400}
401
Guido van Rossum775f4da1993-01-09 17:18:52 +0000402#ifndef MSDOS
403static object *
404posix_nice(self, args)
405 object *self;
406 object *args;
407{
408 int increment, value;
409
410 if (!getargs(args, "i", &increment))
411 return NULL;
412 value = nice(increment);
413 if (value == -1)
414 return posix_error();
415 return newintobject((long) value);
416}
417#endif
418
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000419#if i386 && ! _SEQUENT_
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000420int
421rename(from, to)
422 char *from;
423 char *to;
424{
425 int status;
426 /* XXX Shouldn't this unlink the destination first? */
427 status = link(from, to);
428 if (status != 0)
429 return status;
430 return unlink(from);
431}
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000432#endif /* i386 && ! _SEQUENT_ */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000433
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434static object *
435posix_rename(self, args)
436 object *self;
437 object *args;
438{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439 return posix_2str(args, rename);
440}
441
442static object *
443posix_rmdir(self, args)
444 object *self;
445 object *args;
446{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447 return posix_1str(args, rmdir);
448}
449
450static object *
451posix_stat(self, args)
452 object *self;
453 object *args;
454{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455 return posix_do_stat(self, args, stat);
456}
457
458static object *
459posix_system(self, args)
460 object *self;
461 object *args;
462{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000463 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000464 long sts;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 if (!getstrarg(args, &command))
466 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000468 sts = system(command);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000469 END_SAVE
470 return newintobject(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471}
472
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000473#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474static object *
475posix_umask(self, args)
476 object *self;
477 object *args;
478{
479 int i;
480 if (!getintarg(args, &i))
481 return NULL;
482 i = umask(i);
483 if (i < 0)
484 return posix_error();
485 return newintobject((long)i);
486}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000487#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488
489static object *
490posix_unlink(self, args)
491 object *self;
492 object *args;
493{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494 return posix_1str(args, unlink);
495}
496
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000497#ifndef NO_UNAME
498#include <sys/utsname.h>
499
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000500extern int uname PROTO((struct utsname *));
501
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000502static object *
503posix_uname(self, args)
504 object *self;
505 object *args;
506{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000507 struct utsname u;
508 object *v;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000509 int res;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000510 if (!getnoarg(args))
511 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000512 BGN_SAVE
513 res = uname(&u);
514 END_SAVE
515 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000516 return posix_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000517 return mkvalue("(sssss)",
518 u.sysname,
519 u.nodename,
520 u.release,
521 u.version,
522 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000523}
524#endif /* NO_UNAME */
525
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000526#ifdef UTIME_STRUCT
527#include <utime.h>
528#endif
529
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000531posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 object *self;
533 object *args;
534{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000535 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000536 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000537
538#ifdef UTIME_STRUCT
539 struct utimbuf buf;
540#define ATIME buf.actime
541#define MTIME buf.modtime
542#define UTIME_ARG &buf
543
544#else
545 time_t buf[2];
546#define ATIME buf[0]
547#define MTIME buf[1]
548#define UTIME_ARG buf
549#endif
550
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000551 if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000553 BGN_SAVE
554 res = utime(path, UTIME_ARG);
555 END_SAVE
556 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557 return posix_error();
558 INCREF(None);
559 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000560#undef UTIME_ARG
561#undef ATIME
562#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563}
564
Guido van Rossum85e3b011991-06-03 12:42:10 +0000565
566#ifndef MSDOS
567
Guido van Rossum3b066191991-06-04 19:40:25 +0000568/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000569
570static object *
571posix__exit(self, args)
572 object *self;
573 object *args;
574{
575 int sts;
576 if (!getintarg(args, &sts))
577 return NULL;
578 _exit(sts);
579 /* NOTREACHED */
580}
581
582/* XXX To do: exece, execp */
583
584static object *
Guido van Rossum89b33251993-10-22 14:26:06 +0000585posix_execv(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000586 object *self;
587 object *args;
588{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000589 char *path;
590 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000591 char **argvlist;
592 int i, argc;
593 object *(*getitem) PROTO((object *, int));
594
Guido van Rossum89b33251993-10-22 14:26:06 +0000595 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000596 argv is a list or tuple of strings. */
597
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000598 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000599 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000600 if (is_listobject(argv)) {
601 argc = getlistsize(argv);
602 getitem = getlistitem;
603 }
604 else if (is_tupleobject(argv)) {
605 argc = gettuplesize(argv);
606 getitem = gettupleitem;
607 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000608 else {
609 badarg:
610 err_badarg();
611 return NULL;
612 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000613
614 argvlist = NEW(char *, argc+1);
615 if (argvlist == NULL)
616 return NULL;
617 for (i = 0; i < argc; i++) {
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000618 if (!getstrarg((*getitem)(argv, i), &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000619 DEL(argvlist);
620 goto badarg;
621 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000622 }
623 argvlist[argc] = NULL;
624
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000625 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000626
627 /* If we get here it's definitely an error */
628
629 DEL(argvlist);
630 return posix_error();
631}
632
633static object *
634posix_fork(self, args)
635 object *self;
636 object *args;
637{
638 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000639 if (!getnoarg(args))
640 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000641 pid = fork();
642 if (pid == -1)
643 return posix_error();
644 return newintobject((long)pid);
645}
646
647static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000648posix_getegid(self, args)
649 object *self;
650 object *args;
651{
652 if (!getnoarg(args))
653 return NULL;
654 return newintobject((long)getegid());
655}
656
657static object *
658posix_geteuid(self, args)
659 object *self;
660 object *args;
661{
662 if (!getnoarg(args))
663 return NULL;
664 return newintobject((long)geteuid());
665}
666
667static object *
668posix_getgid(self, args)
669 object *self;
670 object *args;
671{
672 if (!getnoarg(args))
673 return NULL;
674 return newintobject((long)getgid());
675}
676
677static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000678posix_getpid(self, args)
679 object *self;
680 object *args;
681{
Guido van Rossum04814471991-06-04 20:23:49 +0000682 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000683 return NULL;
684 return newintobject((long)getpid());
685}
686
687static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000688posix_getpgrp(self, args)
689 object *self;
690 object *args;
691{
692 if (!getnoarg(args))
693 return NULL;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000694#ifdef SYSV
695 return newintobject((long)getpgrp());
696#else
Guido van Rossum971443b1991-06-07 13:59:29 +0000697 return newintobject((long)getpgrp(0));
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000698#endif
Guido van Rossum04814471991-06-04 20:23:49 +0000699}
700
701static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +0000702posix_setpgrp(self, args)
703 object *self;
704 object *args;
705{
706 if (!getnoarg(args))
707 return NULL;
708#ifdef SYSV
709 if (setpgrp() < 0)
710#else
711 if (setpgrp(0, 0) < 0)
712#endif
Guido van Rossum687dd131993-05-17 08:34:16 +0000713 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000714 INCREF(None);
715 return None;
716}
717
718static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000719posix_getppid(self, args)
720 object *self;
721 object *args;
722{
Guido van Rossum04814471991-06-04 20:23:49 +0000723 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000724 return NULL;
725 return newintobject((long)getppid());
726}
727
728static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000729posix_getuid(self, args)
730 object *self;
731 object *args;
732{
733 if (!getnoarg(args))
734 return NULL;
735 return newintobject((long)getuid());
736}
737
738static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000739posix_kill(self, args)
740 object *self;
741 object *args;
742{
743 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +0000744 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000745 return NULL;
746 if (kill(pid, sig) == -1)
747 return posix_error();
748 INCREF(None);
749 return None;
750}
751
752static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000753posix_popen(self, args)
754 object *self;
755 object *args;
756{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000757 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000758 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000759 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000760 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000761 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000762 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000763 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +0000764 if (fp == NULL)
765 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000766 /* From now on, ignore SIGPIPE and let the error checking
767 do the work. */
768 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000769 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000770}
771
772static object *
Guido van Rossum21803b81992-08-09 12:55:27 +0000773posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000774 object *self;
775 object *args;
776{
Guido van Rossum85e3b011991-06-03 12:42:10 +0000777#ifdef NO_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +0000778 err_setstr(PosixError,
779 "posix.waitpid() not supported on this system");
780 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000781#else
Guido van Rossum21803b81992-08-09 12:55:27 +0000782 int pid, options, sts;
783 if (!getargs(args, "(ii)", &pid, &options))
784 return NULL;
785 BGN_SAVE
786 pid = waitpid(pid, &sts, options);
787 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +0000788 if (pid == -1)
789 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +0000790 else
791 return mkvalue("ii", pid, sts);
792#endif
793}
794
795static object *
796posix_wait(self, args)
797 object *self;
798 object *args;
799{
800 int pid, sts;
801 if (args != NULL)
802 return posix_waitpid(self, args); /* BW compat */
803 BGN_SAVE
804 pid = wait(&sts);
805 END_SAVE
806 if (pid == -1)
807 return posix_error();
808 else
809 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000810}
811
812#endif /* MSDOS */
813
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000814static object *
815posix_lstat(self, args)
816 object *self;
817 object *args;
818{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819 return posix_do_stat(self, args, lstat);
820}
821
822static object *
823posix_readlink(self, args)
824 object *self;
825 object *args;
826{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000827#ifdef NO_LSTAT
828 err_setstr(PosixError, "readlink not implemented on this system");
829 return NULL;
830#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000831 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000832 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833 int n;
834 if (!getstrarg(args, &path))
835 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000836 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000837 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000838 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839 if (n < 0)
840 return posix_error();
841 return newsizedstringobject(buf, n);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000842#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843}
844
845static object *
846posix_symlink(self, args)
847 object *self;
848 object *args;
849{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000850#ifdef NO_LSTAT
851 err_setstr(PosixError, "symlink not implemented on this system");
852 return NULL;
853#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000854 return posix_2str(args, symlink);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000855#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856}
857
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858
Guido van Rossum22db57e1992-04-05 14:25:30 +0000859#ifdef DO_TIMES
860
861static object *
862posix_times(self, args)
863 object *self;
864 object *args;
865{
866 struct tms t;
867 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +0000868 if (!getnoarg(args))
869 return NULL;
870 errno = 0;
871 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +0000872 if (c == (clock_t) -1)
873 return posix_error();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000874 return mkvalue("dddd",
875 (double)t.tms_utime / HZ,
876 (double)t.tms_stime / HZ,
877 (double)t.tms_cutime / HZ,
878 (double)t.tms_cstime / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +0000879}
880
Guido van Rossumc2670a01992-09-13 20:07:29 +0000881#endif /* DO_TIMES */
882
883#ifdef DO_PG
884
885static object *
886posix_setsid(self, args)
887 object *self;
888 object *args;
889{
890 if (!getnoarg(args))
891 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000892 if (setsid() < 0)
893 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000894 INCREF(None);
895 return None;
896}
897
898static object *
899posix_setpgid(self, args)
900 object *self;
901 object *args;
902{
903 int pid, pgrp;
904 if (!getargs(args, "(ii)", &pid, &pgrp))
905 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000906 if (setpgid(pid, pgrp) < 0)
907 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000908 INCREF(None);
909 return None;
910}
911
Guido van Rossum7066dd71992-09-17 17:54:56 +0000912static object *
913posix_tcgetpgrp(self, args)
914 object *self;
915 object *args;
916{
917 int fd, pgid;
918 if (!getargs(args, "i", &fd))
919 return NULL;
920 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +0000921 if (pgid < 0)
922 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000923 return newintobject((long)pgid);
924}
925
926static object *
927posix_tcsetpgrp(self, args)
928 object *self;
929 object *args;
930{
931 int fd, pgid;
932 if (!getargs(args, "(ii)", &fd, &pgid))
933 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000934 if (tcsetpgrp(fd, pgid) < 0)
935 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000936 INCREF(None);
937 return None;
938}
939
Guido van Rossumc2670a01992-09-13 20:07:29 +0000940#endif /* DO_PG */
Guido van Rossum22db57e1992-04-05 14:25:30 +0000941
Guido van Rossum687dd131993-05-17 08:34:16 +0000942/* Functions acting on file descriptors */
943
Guido van Rossum234f9421993-06-17 12:35:49 +0000944static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000945posix_open(self, args)
946 object *self;
947 object *args;
948{
949 char *file;
950 int flag;
951 int mode = 0777;
952 int fd;
953 if (!getargs(args, "(si)", &file, &flag)) {
954 err_clear();
955 if (!getargs(args, "(sii)", &file, &flag, &mode))
956 return NULL;
957 }
958 BGN_SAVE
959 fd = open(file, flag, mode);
960 END_SAVE
961 if (fd < 0)
962 return posix_error();
963 return newintobject((long)fd);
964}
965
Guido van Rossum234f9421993-06-17 12:35:49 +0000966static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000967posix_close(self, args)
968 object *self;
969 object *args;
970{
971 int fd, res;
972 if (!getargs(args, "i", &fd))
973 return NULL;
974 BGN_SAVE
975 res = close(fd);
976 END_SAVE
977 if (res < 0)
978 return posix_error();
979 INCREF(None);
980 return None;
981}
982
Guido van Rossum234f9421993-06-17 12:35:49 +0000983static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000984posix_dup(self, args)
985 object *self;
986 object *args;
987{
988 int fd;
989 if (!getargs(args, "i", &fd))
990 return NULL;
991 BGN_SAVE
992 fd = dup(fd);
993 END_SAVE
994 if (fd < 0)
995 return posix_error();
996 return newintobject((long)fd);
997}
998
Guido van Rossum234f9421993-06-17 12:35:49 +0000999static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001000posix_dup2(self, args)
1001 object *self;
1002 object *args;
1003{
1004 int fd, fd2, res;
1005 if (!getargs(args, "(ii)", &fd, &fd2))
1006 return NULL;
1007 BGN_SAVE
1008 res = dup2(fd, fd2);
1009 END_SAVE
1010 if (res < 0)
1011 return posix_error();
1012 INCREF(None);
1013 return None;
1014}
1015
Guido van Rossum234f9421993-06-17 12:35:49 +00001016static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001017posix_lseek(self, args)
1018 object *self;
1019 object *args;
1020{
1021 int fd, how;
1022 long pos, res;
1023 if (!getargs(args, "(ili)", &fd, &pos, &how))
1024 return NULL;
1025#ifdef SEEK_SET
1026 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1027 switch (how) {
1028 case 0: how = SEEK_SET; break;
1029 case 1: how = SEEK_CUR; break;
1030 case 2: how = SEEK_END; break;
1031 }
1032#endif
1033 BGN_SAVE
1034 res = lseek(fd, pos, how);
1035 END_SAVE
1036 if (res < 0)
1037 return posix_error();
1038 return newintobject(res);
1039}
1040
Guido van Rossum234f9421993-06-17 12:35:49 +00001041static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001042posix_read(self, args)
1043 object *self;
1044 object *args;
1045{
1046 int fd, size;
1047 object *buffer;
1048 if (!getargs(args, "(ii)", &fd, &size))
1049 return NULL;
1050 buffer = newsizedstringobject((char *)NULL, size);
1051 if (buffer == NULL)
1052 return NULL;
1053 BGN_SAVE
1054 size = read(fd, getstringvalue(buffer), size);
1055 END_SAVE
1056 if (size < 0) {
1057 DECREF(buffer);
1058 return posix_error();
1059 }
1060 resizestring(&buffer, size);
1061 return buffer;
1062}
1063
Guido van Rossum234f9421993-06-17 12:35:49 +00001064static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001065posix_write(self, args)
1066 object *self;
1067 object *args;
1068{
1069 int fd, size;
1070 char *buffer;
1071 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1072 return NULL;
1073 BGN_SAVE
1074 size = write(fd, buffer, size);
1075 END_SAVE
1076 if (size < 0)
1077 return posix_error();
1078 return newintobject((long)size);
1079}
1080
Guido van Rossum234f9421993-06-17 12:35:49 +00001081static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001082posix_fstat(self, args)
1083 object *self;
1084 object *args;
1085{
1086 int fd;
1087 struct stat st;
1088 int res;
1089 if (!getargs(args, "i", &fd))
1090 return NULL;
1091 BGN_SAVE
1092 res = fstat(fd, &st);
1093 END_SAVE
1094 if (res != 0)
1095 return posix_error();
1096 return mkvalue("(llllllllll)",
1097 (long)st.st_mode,
1098 (long)st.st_ino,
1099 (long)st.st_dev,
1100 (long)st.st_nlink,
1101 (long)st.st_uid,
1102 (long)st.st_gid,
1103 (long)st.st_size,
1104 (long)st.st_atime,
1105 (long)st.st_mtime,
1106 (long)st.st_ctime);
1107}
1108
1109static object *
1110posix_fdopen(self, args)
1111 object *self;
1112 object *args;
1113{
1114 extern int fclose PROTO((FILE *));
1115 int fd;
1116 char *mode;
1117 FILE *fp;
1118 if (!getargs(args, "(is)", &fd, &mode))
1119 return NULL;
1120 BGN_SAVE
1121 fp = fdopen(fd, mode);
1122 END_SAVE
1123 if (fp == NULL)
1124 return posix_error();
1125 /* From now on, ignore SIGPIPE and let the error checking
1126 do the work. */
1127 (void) signal(SIGPIPE, SIG_IGN);
1128 return newopenfileobject(fp, "(fdopen)", mode, fclose);
1129}
1130
1131#ifndef MSDOS
Guido van Rossum234f9421993-06-17 12:35:49 +00001132static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001133posix_pipe(self, args)
1134 object *self;
1135 object *args;
1136{
1137 int fds[2];
1138 int res;
1139 if (!getargs(args, ""))
1140 return NULL;
1141 BGN_SAVE
1142 res = pipe(fds);
1143 END_SAVE
1144 if (res != 0)
1145 return posix_error();
1146 return mkvalue("(ii)", fds[0], fds[1]);
1147}
1148#endif /* MSDOS */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001149
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001150static struct methodlist posix_methods[] = {
1151 {"chdir", posix_chdir},
1152 {"chmod", posix_chmod},
1153 {"getcwd", posix_getcwd},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001154#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001155 {"link", posix_link},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001156#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001157 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001158 {"lstat", posix_lstat},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001159 {"mkdir", posix_mkdir},
Guido van Rossum775f4da1993-01-09 17:18:52 +00001160#ifndef MSDOS
1161 {"nice", posix_nice},
1162#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001163 {"readlink", posix_readlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001164 {"rename", posix_rename},
1165 {"rmdir", posix_rmdir},
1166 {"stat", posix_stat},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001167 {"symlink", posix_symlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001168 {"system", posix_system},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001169#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170 {"umask", posix_umask},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001171#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001172#ifndef NO_UNAME
1173 {"uname", posix_uname},
1174#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001175 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001176 {"utime", posix_utime},
Guido van Rossum22db57e1992-04-05 14:25:30 +00001177#ifdef DO_TIMES
1178 {"times", posix_times},
1179#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001180
Guido van Rossum85e3b011991-06-03 12:42:10 +00001181#ifndef MSDOS
1182 {"_exit", posix__exit},
Guido van Rossum89b33251993-10-22 14:26:06 +00001183 {"execv", posix_execv},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001184 {"fork", posix_fork},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001185 {"getegid", posix_getegid},
1186 {"geteuid", posix_geteuid},
1187 {"getgid", posix_getgid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001188 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +00001189 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001190 {"getppid", posix_getppid},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001191 {"getuid", posix_getuid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001192 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +00001193 {"popen", posix_popen},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001194 {"setpgrp", posix_setpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001195 {"wait", posix_wait},
Guido van Rossum21803b81992-08-09 12:55:27 +00001196 {"waitpid", posix_waitpid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001197#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001198
Guido van Rossumc2670a01992-09-13 20:07:29 +00001199#ifdef DO_PG
1200 {"setsid", posix_setsid},
1201 {"setpgid", posix_setpgid},
Guido van Rossum7066dd71992-09-17 17:54:56 +00001202 {"tcgetpgrp", posix_tcgetpgrp},
1203 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001204#endif
1205
Guido van Rossum687dd131993-05-17 08:34:16 +00001206 {"open", posix_open},
1207 {"close", posix_close},
1208 {"dup", posix_dup},
1209 {"dup2", posix_dup2},
1210 {"lseek", posix_lseek},
1211 {"read", posix_read},
1212 {"write", posix_write},
1213 {"fstat", posix_fstat},
1214 {"fdopen", posix_fdopen},
1215#ifndef MSDOS
1216 {"pipe", posix_pipe},
1217#endif
1218
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219 {NULL, NULL} /* Sentinel */
1220};
1221
1222
1223void
1224initposix()
1225{
1226 object *m, *d, *v;
1227
1228 m = initmodule("posix", posix_methods);
1229 d = getmoduledict(m);
1230
1231 /* Initialize posix.environ dictionary */
1232 v = convertenviron();
1233 if (v == NULL || dictinsert(d, "environ", v) != 0)
1234 fatal("can't define posix.environ");
1235 DECREF(v);
1236
1237 /* Initialize posix.error exception */
1238 PosixError = newstringobject("posix.error");
1239 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1240 fatal("can't define posix.error");
1241}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001242
Guido van Rossum3b066191991-06-04 19:40:25 +00001243
1244/* Function used elsewhere to get a file's modification time */
1245
1246long
1247getmtime(path)
1248 char *path;
1249{
1250 struct stat st;
1251 if (stat(path, &st) != 0)
1252 return -1;
1253 else
1254 return st.st_mtime;
1255}
1256
1257
Guido van Rossume22e6441993-07-09 10:51:31 +00001258#ifdef TURBO_C
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001259
1260/* A small "compatibility library" for TurboC under MS-DOS */
1261
Guido van Rossume22e6441993-07-09 10:51:31 +00001262//#include <sir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001263#include <io.h>
1264#include <dos.h>
1265#include <fcntl.h>
1266
1267int
1268chmod(path, mode)
1269 char *path;
1270 int mode;
1271{
1272 return _chmod(path, 1, mode);
1273}
1274
1275int
1276utime(path, times)
1277 char *path;
1278 time_t times[2];
1279{
1280 struct date dt;
1281 struct time tm;
1282 struct ftime dft;
1283 int fh;
1284 unixtodos(tv[0].tv_sec,&dt,&tm);
1285 dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min;
1286 dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day;
1287 dft.ft_month = dt.da_mon;
1288 dft.ft_year = (dt.da_year - 1980); /* this is for TC library */
1289
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001290 if ((fh = open(path,O_RDWR)) < 0)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001291 return posix_error(); /* can't open file to set time */
1292 if (setftime(fh,&dft) < 0)
1293 {
1294 close(fh);
1295 return posix_error();
1296 }
1297 close(fh); /* close the temp handle */
1298}
1299
Guido van Rossume22e6441993-07-09 10:51:31 +00001300#endif /* TURBO_C */