blob: e843cfe3a2e0c1b4683c593d3368178a0172e085 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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 Rossuma4916fa1996-05-23 22:58:55 +000027/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +000028 actually calls itself 'nt', not 'posix', and a few functions are
29 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +000030 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +000031 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +000032 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000033
Guido van Rossuma4916fa1996-05-23 22:58:55 +000034/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000035
Guido van Rossum3f5da241990-12-20 15:06:42 +000036#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000038#include "ceval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Guido van Rossumb6775db1994-08-01 11:34:53 +000040#include <string.h>
41#include <errno.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000042#include <sys/types.h>
43#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000044#ifdef HAVE_SYS_WAIT_H
45#include <sys/wait.h> /* For WNOHANG */
46#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000047
48#include "mytime.h" /* For clock_t on some systems */
49
50#ifdef HAVE_FCNTL_H
51#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000052#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000053
Guido van Rossuma4916fa1996-05-23 22:58:55 +000054/* Various compilers have only certain posix functions */
55#ifdef __WATCOMC__ /* Watcom compiler */
56#define HAVE_GETCWD 1
57#define HAVE_OPENDIR 1
58#define HAVE_SYSTEM 1
59#if defined(__OS2__)
60#define HAVE_EXECV 1
61#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000062#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000063#include <process.h>
64#else
65#ifdef __BORLANDC__ /* Borland compiler */
66#define HAVE_EXECV 1
67#define HAVE_GETCWD 1
68#define HAVE_GETEGID 1
69#define HAVE_GETEUID 1
70#define HAVE_GETGID 1
71#define HAVE_GETPPID 1
72#define HAVE_GETUID 1
73#define HAVE_KILL 1
74#define HAVE_OPENDIR 1
75#define HAVE_PIPE 1
76#define HAVE_POPEN 1
77#define HAVE_SYSTEM 1
78#define HAVE_WAIT 1
79#else
80#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000081#define HAVE_GETCWD 1
82#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +000083#define HAVE_EXECV 1
84#define HAVE_PIPE 1
85#define HAVE_POPEN 1
86#define HAVE_SYSTEM 1
87#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +000088#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000089#else /* all other compilers */
90/* Unix functions that the configure script doesn't check for */
91#define HAVE_EXECV 1
92#define HAVE_FORK 1
93#define HAVE_GETCWD 1
94#define HAVE_GETEGID 1
95#define HAVE_GETEUID 1
96#define HAVE_GETGID 1
97#define HAVE_GETPPID 1
98#define HAVE_GETUID 1
99#define HAVE_KILL 1
100#define HAVE_OPENDIR 1
101#define HAVE_PIPE 1
102#define HAVE_POPEN 1
103#define HAVE_SYSTEM 1
104#define HAVE_WAIT 1
105#endif /* _MSC_VER */
106#endif /* __BORLANDC__ */
107#endif /* ! __WATCOMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000108
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000110
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000112#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000113#endif
114
115#ifdef NeXT
116/* NeXT's <unistd.h> and <utime.h> aren't worth much */
117#undef HAVE_UNISTD_H
118#undef HAVE_UTIME_H
119/* #undef HAVE_GETCWD */
120#endif
121
122#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000123/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
124extern int rename();
125extern int pclose();
126extern int lstat();
127extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000128#else /* !HAVE_UNISTD_H */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000129#if defined(__WATCOMC__) || defined(_MSC_VER)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130extern int mkdir PROTO((const char *));
131#else
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000132extern int mkdir PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#endif
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000134extern int chdir PROTO((const char *));
Guido van Rossume22e6441993-07-09 10:51:31 +0000135extern int rmdir PROTO((const char *));
136extern int chmod PROTO((const char *, mode_t));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137extern int chown PROTO((const char *, uid_t, gid_t));
138extern char *getcwd PROTO((char *, int));
Guido van Rossume22e6441993-07-09 10:51:31 +0000139extern char *strerror PROTO((int));
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000140extern int link PROTO((const char *, const char *));
141extern int rename PROTO((const char *, const char *));
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000142extern int stat PROTO((const char *, struct stat *));
143extern int unlink PROTO((const char *));
144extern int pclose PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#ifdef HAVE_SYMLINK
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000146extern int symlink PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000147#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148#ifdef HAVE_LSTAT
149extern int lstat PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000150#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000152
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155#ifdef HAVE_UTIME_H
156#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000157#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000159#ifdef HAVE_SYS_UTIME_H
160#include <sys/utime.h>
161#define HAVE_UTIME_H /* pretend we do for the rest of this file */
162#endif /* HAVE_SYS_UTIME_H */
163
Guido van Rossumb6775db1994-08-01 11:34:53 +0000164#ifdef HAVE_SYS_TIMES_H
165#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000166#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167
168#ifdef HAVE_SYS_PARAM_H
169#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000170#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171
172#ifdef HAVE_SYS_UTSNAME_H
173#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000174#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175
176#ifndef MAXPATHLEN
177#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000178#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000180#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000182#define NAMLEN(dirent) strlen((dirent)->d_name)
183#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000184#ifdef __WATCOMC__
185#include <direct.h>
186#define NAMLEN(dirent) strlen((dirent)->d_name)
187#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000189#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000191#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000193#endif
194#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000196#endif
197#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000199#endif
200#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000202#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#include <direct.h>
204#include <io.h>
205#include <process.h>
206#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000207#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000208#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000209#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#else /* 16-bit Windows */
211#include <dos.h>
212#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000213#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000214#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
216#ifdef OS2
217#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000218#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219
220/* Return a dictionary corresponding to the POSIX environment table */
221
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000222#if !defined(_MSC_VER) && !defined(__WATCOMC__)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000224#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225
226static object *
227convertenviron()
228{
229 object *d;
230 char **e;
231 d = newdictobject();
232 if (d == NULL)
233 return NULL;
234 if (environ == NULL)
235 return d;
236 /* XXX This part ignores errors */
237 for (e = environ; *e != NULL; e++) {
238 object *v;
239 char *p = strchr(*e, '=');
240 if (p == NULL)
241 continue;
242 v = newstringobject(p+1);
243 if (v == NULL)
244 continue;
245 *p = '\0';
246 (void) dictinsert(d, *e, v);
247 *p = '=';
248 DECREF(v);
249 }
250 return d;
251}
252
253
254static object *PosixError; /* Exception posix.error */
255
256/* Set a POSIX-specific error from errno, and return NULL */
257
Guido van Rossumad0ee831995-03-01 10:34:45 +0000258static object * posix_error()
259{
260 return err_errno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261}
262
263
264/* POSIX generic methods */
265
266static object *
267posix_1str(args, func)
268 object *args;
269 int (*func) FPROTO((const char *));
270{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000271 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000272 int res;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000273 if (!getargs(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000275 BGN_SAVE
276 res = (*func)(path1);
277 END_SAVE
278 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 return posix_error();
280 INCREF(None);
281 return None;
282}
283
284static object *
285posix_2str(args, func)
286 object *args;
287 int (*func) FPROTO((const char *, const char *));
288{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000289 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000290 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000291 if (!getargs(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000293 BGN_SAVE
294 res = (*func)(path1, path2);
295 END_SAVE
296 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 return posix_error();
298 INCREF(None);
299 return None;
300}
301
302static object *
303posix_strint(args, func)
304 object *args;
305 int (*func) FPROTO((const char *, int));
306{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000307 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000309 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000310 if (!getargs(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000312 BGN_SAVE
313 res = (*func)(path, i);
314 END_SAVE
315 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316 return posix_error();
317 INCREF(None);
318 return None;
319}
320
321static object *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000322posix_strintint(args, func)
323 object *args;
324 int (*func) FPROTO((const char *, int, int));
325{
326 char *path;
327 int i,i2;
328 int res;
329 if (!getargs(args, "(sii)", &path, &i, &i2))
330 return NULL;
331 BGN_SAVE
332 res = (*func)(path, i, i2);
333 END_SAVE
334 if (res < 0)
335 return posix_error();
336 INCREF(None);
337 return None;
338}
339
340static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341posix_do_stat(self, args, statfunc)
342 object *self;
343 object *args;
344 int (*statfunc) FPROTO((const char *, struct stat *));
345{
346 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000347 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000348 int res;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000349 if (!getargs(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000351 BGN_SAVE
352 res = (*statfunc)(path, &st);
353 END_SAVE
354 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 return posix_error();
Guido van Rossum687dd131993-05-17 08:34:16 +0000356 return mkvalue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000357 (long)st.st_mode,
358 (long)st.st_ino,
359 (long)st.st_dev,
360 (long)st.st_nlink,
361 (long)st.st_uid,
362 (long)st.st_gid,
363 (long)st.st_size,
364 (long)st.st_atime,
365 (long)st.st_mtime,
366 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367}
368
369
370/* POSIX methods */
371
372static object *
373posix_chdir(self, args)
374 object *self;
375 object *args;
376{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 return posix_1str(args, chdir);
378}
379
380static object *
381posix_chmod(self, args)
382 object *self;
383 object *args;
384{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385 return posix_strint(args, chmod);
386}
387
Guido van Rossumb6775db1994-08-01 11:34:53 +0000388#ifdef HAVE_CHOWN
389static object *
390posix_chown(self, args)
391 object *self;
392 object *args;
393{
394 return posix_strintint(args, chown);
395}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000396#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397
Guido van Rossum36bc6801995-06-14 22:54:23 +0000398#ifdef HAVE_GETCWD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399static object *
400posix_getcwd(self, args)
401 object *self;
402 object *args;
403{
404 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000405 char *res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406 if (!getnoarg(args))
407 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000408 BGN_SAVE
409 res = getcwd(buf, sizeof buf);
410 END_SAVE
411 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 return posix_error();
413 return newstringobject(buf);
414}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000415#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416
Guido van Rossumb6775db1994-08-01 11:34:53 +0000417#ifdef HAVE_LINK
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418static object *
419posix_link(self, args)
420 object *self;
421 object *args;
422{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423 return posix_2str(args, link);
424}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000425#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426
Guido van Rossumb6775db1994-08-01 11:34:53 +0000427static object *
428posix_listdir(self, args)
429 object *self;
430 object *args;
431{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000432#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000433
Guido van Rossumb6775db1994-08-01 11:34:53 +0000434 char *name;
435 int len;
436 object *d, *v;
437 HANDLE hFindFile;
438 WIN32_FIND_DATA FileData;
439 char namebuf[MAX_PATH+5];
440
441 if (!getargs(args, "s#", &name, &len))
442 return NULL;
443 if (len >= MAX_PATH) {
444 err_setstr(ValueError, "path too long");
445 return NULL;
446 }
447 strcpy(namebuf, name);
448 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
449 namebuf[len++] = '/';
450 strcpy(namebuf + len, "*.*");
451
452 if ((d = newlistobject(0)) == NULL)
453 return NULL;
454
455 hFindFile = FindFirstFile(namebuf, &FileData);
456 if (hFindFile == INVALID_HANDLE_VALUE) {
457 errno = GetLastError();
458 return posix_error();
459 }
460 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000461 if (FileData.cFileName[0] == '.' &&
462 (FileData.cFileName[1] == '\0' ||
463 FileData.cFileName[1] == '.' &&
464 FileData.cFileName[2] == '\0'))
465 continue;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000466 v = newstringobject(FileData.cFileName);
467 if (v == NULL) {
468 DECREF(d);
469 d = NULL;
470 break;
471 }
472 if (addlistitem(d, v) != 0) {
473 DECREF(v);
474 DECREF(d);
475 d = NULL;
476 break;
477 }
478 DECREF(v);
479 } while (FindNextFile(hFindFile, &FileData) == TRUE);
480
481 if (FindClose(hFindFile) == FALSE) {
482 errno = GetLastError();
483 return posix_error();
484 }
485
486 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000487
Guido van Rossum8d665e61996-06-26 18:22:49 +0000488#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000489#ifdef _MSC_VER /* 16-bit Windows */
490
491#ifndef MAX_PATH
492#define MAX_PATH 250
493#endif
494 char *name, *pt;
495 int len;
496 object *d, *v;
497 char namebuf[MAX_PATH+5];
498 struct _find_t ep;
499
500 if (!getargs(args, "s#", &name, &len))
501 return NULL;
502 if (len >= MAX_PATH) {
503 err_setstr(ValueError, "path too long");
504 return NULL;
505 }
506 strcpy(namebuf, name);
507 for (pt = namebuf; *pt; pt++)
508 if (*pt == '/')
509 *pt = '\\';
510 if (namebuf[len-1] != '\\')
511 namebuf[len++] = '\\';
512 strcpy(namebuf + len, "*.*");
513
514 if ((d = newlistobject(0)) == NULL)
515 return NULL;
516
517 if (_dos_findfirst(namebuf, _A_RDONLY |
518 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0){
519 errno = ENOENT;
520 return posix_error();
521 }
522 do {
523 if (ep.name[0] == '.' &&
524 (ep.name[1] == '\0' ||
525 ep.name[1] == '.' &&
526 ep.name[2] == '\0'))
527 continue;
528 strcpy(namebuf, ep.name);
529 for (pt = namebuf; *pt; pt++)
530 if (isupper(*pt))
531 *pt = tolower(*pt);
532 v = newstringobject(namebuf);
533 if (v == NULL) {
534 DECREF(d);
535 d = NULL;
536 break;
537 }
538 if (addlistitem(d, v) != 0) {
539 DECREF(v);
540 DECREF(d);
541 d = NULL;
542 break;
543 }
544 DECREF(v);
545 } while (_dos_findnext(&ep) == 0);
546
547 return d;
548
549#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000550
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000551 char *name;
552 object *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000554 struct dirent *ep;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000555 if (!getargs(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000557 BGN_SAVE
558 if ((dirp = opendir(name)) == NULL) {
559 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000561 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562 if ((d = newlistobject(0)) == NULL) {
563 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000564 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 return NULL;
566 }
567 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000568 if (ep->d_name[0] == '.' &&
569 (NAMLEN(ep) == 1 ||
570 ep->d_name[1] == '.' && NAMLEN(ep) == 2))
571 continue;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000572 v = newsizedstringobject(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573 if (v == NULL) {
574 DECREF(d);
575 d = NULL;
576 break;
577 }
578 if (addlistitem(d, v) != 0) {
579 DECREF(v);
580 DECREF(d);
581 d = NULL;
582 break;
583 }
584 DECREF(v);
585 }
586 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000587 END_SAVE
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000588
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000590
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000591#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000592#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593}
594
595static object *
596posix_mkdir(self, args)
597 object *self;
598 object *args;
599{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000600 int res;
601 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000602 int mode = 0777;
Guido van Rossumb0824db1996-02-25 04:50:32 +0000603 if (!newgetargs(args, "s|i", &path, &mode))
604 return NULL;
605 BGN_SAVE
Guido van Rossum8d665e61996-06-26 18:22:49 +0000606#if defined(__WATCOMC__) || defined(_MSC_VER)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000607 res = mkdir(path);
608#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000609 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000610#endif
Guido van Rossumb0824db1996-02-25 04:50:32 +0000611 END_SAVE
612 if (res < 0)
613 return posix_error();
614 INCREF(None);
615 return None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616}
617
Guido van Rossumb6775db1994-08-01 11:34:53 +0000618#ifdef HAVE_NICE
Guido van Rossum775f4da1993-01-09 17:18:52 +0000619static object *
620posix_nice(self, args)
621 object *self;
622 object *args;
623{
624 int increment, value;
625
626 if (!getargs(args, "i", &increment))
627 return NULL;
628 value = nice(increment);
629 if (value == -1)
630 return posix_error();
631 return newintobject((long) value);
632}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000633#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000634
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635static object *
636posix_rename(self, args)
637 object *self;
638 object *args;
639{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640 return posix_2str(args, rename);
641}
642
643static object *
644posix_rmdir(self, args)
645 object *self;
646 object *args;
647{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648 return posix_1str(args, rmdir);
649}
650
651static object *
652posix_stat(self, args)
653 object *self;
654 object *args;
655{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656 return posix_do_stat(self, args, stat);
657}
658
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000659#ifdef HAVE_SYSTEM
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660static object *
661posix_system(self, args)
662 object *self;
663 object *args;
664{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000665 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000666 long sts;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000667 if (!getargs(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000669 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000670 sts = system(command);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000671 END_SAVE
672 return newintobject(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000674#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675
676static object *
677posix_umask(self, args)
678 object *self;
679 object *args;
680{
681 int i;
682 if (!getintarg(args, &i))
683 return NULL;
684 i = umask(i);
685 if (i < 0)
686 return posix_error();
687 return newintobject((long)i);
688}
689
690static object *
691posix_unlink(self, args)
692 object *self;
693 object *args;
694{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695 return posix_1str(args, unlink);
696}
697
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698#ifdef HAVE_UNAME
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000699static object *
700posix_uname(self, args)
701 object *self;
702 object *args;
703{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000704 struct utsname u;
705 object *v;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000706 int res;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000707 if (!getnoarg(args))
708 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000709 BGN_SAVE
710 res = uname(&u);
711 END_SAVE
712 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000713 return posix_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000714 return mkvalue("(sssss)",
715 u.sysname,
716 u.nodename,
717 u.release,
718 u.version,
719 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000720}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000721#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000722
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000724posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725 object *self;
726 object *args;
727{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000728 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000729 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000730 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000731
Guido van Rossumb6775db1994-08-01 11:34:53 +0000732#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000733 struct utimbuf buf;
734#define ATIME buf.actime
735#define MTIME buf.modtime
736#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000737#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000738 time_t buf[2];
739#define ATIME buf[0]
740#define MTIME buf[1]
741#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000742#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000743
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000744 if (!getargs(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000746 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +0000747 MTIME = mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000748 BGN_SAVE
749 res = utime(path, UTIME_ARG);
750 END_SAVE
751 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000752 return posix_error();
753 INCREF(None);
754 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000755#undef UTIME_ARG
756#undef ATIME
757#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758}
759
Guido van Rossum85e3b011991-06-03 12:42:10 +0000760
Guido van Rossum3b066191991-06-04 19:40:25 +0000761/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000762
763static object *
764posix__exit(self, args)
765 object *self;
766 object *args;
767{
768 int sts;
769 if (!getintarg(args, &sts))
770 return NULL;
771 _exit(sts);
772 /* NOTREACHED */
773}
774
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000775#ifdef HAVE_EXECV
Guido van Rossum85e3b011991-06-03 12:42:10 +0000776static object *
Guido van Rossum89b33251993-10-22 14:26:06 +0000777posix_execv(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000778 object *self;
779 object *args;
780{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000781 char *path;
782 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000783 char **argvlist;
784 int i, argc;
785 object *(*getitem) PROTO((object *, int));
786
Guido van Rossum89b33251993-10-22 14:26:06 +0000787 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000788 argv is a list or tuple of strings. */
789
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000790 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000791 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000792 if (is_listobject(argv)) {
793 argc = getlistsize(argv);
794 getitem = getlistitem;
795 }
796 else if (is_tupleobject(argv)) {
797 argc = gettuplesize(argv);
798 getitem = gettupleitem;
799 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000800 else {
801 badarg:
802 err_badarg();
803 return NULL;
804 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000805
806 argvlist = NEW(char *, argc+1);
807 if (argvlist == NULL)
808 return NULL;
809 for (i = 0; i < argc; i++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000810 if (!getargs((*getitem)(argv, i), "s", &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000811 DEL(argvlist);
812 goto badarg;
813 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000814 }
815 argvlist[argc] = NULL;
816
Guido van Rossumb6775db1994-08-01 11:34:53 +0000817#ifdef BAD_EXEC_PROTOTYPES
818 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000819#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000820 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000821#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822
Guido van Rossum85e3b011991-06-03 12:42:10 +0000823 /* If we get here it's definitely an error */
824
825 DEL(argvlist);
826 return posix_error();
827}
828
829static object *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000830posix_execve(self, args)
831 object *self;
832 object *args;
833{
834 char *path;
835 object *argv, *env;
836 char **argvlist;
837 char **envlist;
838 object *key, *val;
839 int i, pos, argc, envc;
840 object *(*getitem) PROTO((object *, int));
841
842 /* execve has three arguments: (path, argv, env), where
843 argv is a list or tuple of strings and env is a dictionary
844 like posix.environ. */
845
846 if (!getargs(args, "(sOO)", &path, &argv, &env))
847 return NULL;
848 if (is_listobject(argv)) {
849 argc = getlistsize(argv);
850 getitem = getlistitem;
851 }
852 else if (is_tupleobject(argv)) {
853 argc = gettuplesize(argv);
854 getitem = gettupleitem;
855 }
856 else {
857 err_setstr(TypeError, "argv must be tuple or list");
858 return NULL;
859 }
860 if (!is_dictobject(env)) {
861 err_setstr(TypeError, "env must be dictionary");
862 return NULL;
863 }
864
865 argvlist = NEW(char *, argc+1);
866 if (argvlist == NULL) {
867 err_nomem();
868 return NULL;
869 }
870 for (i = 0; i < argc; i++) {
871 if (!getargs((*getitem)(argv, i),
872 "s;argv must be list of strings",
873 &argvlist[i])) {
874 goto fail_1;
875 }
876 }
877 argvlist[argc] = NULL;
878
879 i = getmappingsize(env);
880 envlist = NEW(char *, i + 1);
881 if (envlist == NULL) {
882 err_nomem();
883 goto fail_1;
884 }
885 pos = 0;
886 envc = 0;
887 while (mappinggetnext(env, &pos, &key, &val)) {
888 char *p, *k, *v;
889 if (!getargs(key, "s;non-string key in env", &k) ||
890 !getargs(val, "s;non-string value in env", &v)) {
891 goto fail_2;
892 }
893 p = NEW(char, getstringsize(key) + getstringsize(val) + 2);
894 if (p == NULL) {
895 err_nomem();
896 goto fail_2;
897 }
898 sprintf(p, "%s=%s", k, v);
899 envlist[envc++] = p;
900 }
901 envlist[envc] = 0;
902
Guido van Rossumb6775db1994-08-01 11:34:53 +0000903
904#ifdef BAD_EXEC_PROTOTYPES
905 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000906#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000907 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000908#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000909
910 /* If we get here it's definitely an error */
911
912 (void) posix_error();
913
914 fail_2:
915 while (--envc >= 0)
916 DEL(envlist[envc]);
917 DEL(envlist);
918 fail_1:
919 DEL(argvlist);
920
921 return NULL;
922}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000923#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000924
Guido van Rossumad0ee831995-03-01 10:34:45 +0000925#ifdef HAVE_FORK
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000926static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000927posix_fork(self, args)
928 object *self;
929 object *args;
930{
931 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000932 if (!getnoarg(args))
933 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000934 pid = fork();
935 if (pid == -1)
936 return posix_error();
937 return newintobject((long)pid);
938}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000939#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +0000940
Guido van Rossumad0ee831995-03-01 10:34:45 +0000941#ifdef HAVE_GETEGID
Guido van Rossum85e3b011991-06-03 12:42:10 +0000942static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000943posix_getegid(self, args)
944 object *self;
945 object *args;
946{
947 if (!getnoarg(args))
948 return NULL;
949 return newintobject((long)getegid());
950}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000951#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +0000952
Guido van Rossumad0ee831995-03-01 10:34:45 +0000953#ifdef HAVE_GETEUID
Guido van Rossum46003ff1992-05-15 11:05:24 +0000954static object *
955posix_geteuid(self, args)
956 object *self;
957 object *args;
958{
959 if (!getnoarg(args))
960 return NULL;
961 return newintobject((long)geteuid());
962}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000963#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +0000964
Guido van Rossumad0ee831995-03-01 10:34:45 +0000965#ifdef HAVE_GETGID
Guido van Rossum46003ff1992-05-15 11:05:24 +0000966static object *
967posix_getgid(self, args)
968 object *self;
969 object *args;
970{
971 if (!getnoarg(args))
972 return NULL;
973 return newintobject((long)getgid());
974}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000975#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +0000976
977static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000978posix_getpid(self, args)
979 object *self;
980 object *args;
981{
Guido van Rossum04814471991-06-04 20:23:49 +0000982 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000983 return NULL;
984 return newintobject((long)getpid());
985}
986
Guido van Rossumb6775db1994-08-01 11:34:53 +0000987#ifdef HAVE_GETPGRP
Guido van Rossum85e3b011991-06-03 12:42:10 +0000988static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000989posix_getpgrp(self, args)
990 object *self;
991 object *args;
992{
993 if (!getnoarg(args))
994 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000995#ifdef GETPGRP_HAVE_ARG
Guido van Rossum971443b1991-06-07 13:59:29 +0000996 return newintobject((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000997#else /* GETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000998 return newintobject((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000999#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001000}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001001#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001002
Guido van Rossumb6775db1994-08-01 11:34:53 +00001003#ifdef HAVE_SETPGRP
Guido van Rossum04814471991-06-04 20:23:49 +00001004static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001005posix_setpgrp(self, args)
1006 object *self;
1007 object *args;
1008{
1009 if (!getnoarg(args))
1010 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001011#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001012 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001013#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001014 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001015#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001016 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +00001017 INCREF(None);
1018 return None;
1019}
1020
Guido van Rossumb6775db1994-08-01 11:34:53 +00001021#endif /* HAVE_SETPGRP */
1022
Guido van Rossumad0ee831995-03-01 10:34:45 +00001023#ifdef HAVE_GETPPID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001024static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001025posix_getppid(self, args)
1026 object *self;
1027 object *args;
1028{
Guido van Rossum04814471991-06-04 20:23:49 +00001029 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001030 return NULL;
1031 return newintobject((long)getppid());
1032}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001033#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001034
Guido van Rossumad0ee831995-03-01 10:34:45 +00001035#ifdef HAVE_GETUID
Guido van Rossum85e3b011991-06-03 12:42:10 +00001036static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001037posix_getuid(self, args)
1038 object *self;
1039 object *args;
1040{
1041 if (!getnoarg(args))
1042 return NULL;
1043 return newintobject((long)getuid());
1044}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001045#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001046
Guido van Rossumad0ee831995-03-01 10:34:45 +00001047#ifdef HAVE_KILL
Guido van Rossum46003ff1992-05-15 11:05:24 +00001048static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001049posix_kill(self, args)
1050 object *self;
1051 object *args;
1052{
1053 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +00001054 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001055 return NULL;
1056 if (kill(pid, sig) == -1)
1057 return posix_error();
1058 INCREF(None);
1059 return None;
1060}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001061#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001062
Guido van Rossumc0125471996-06-28 18:55:32 +00001063#ifdef HAVE_PLOCK
1064
1065#ifdef HAVE_SYS_LOCK_H
1066#include <sys/lock.h>
1067#endif
1068
1069static object *
1070posix_plock(self, args)
1071 object *self;
1072 object *args;
1073{
1074 int op;
1075 if (!getargs(args, "i", &op))
1076 return NULL;
1077 if (plock(op) == -1)
1078 return posix_error();
1079 INCREF(None);
1080 return None;
1081}
1082#endif
1083
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001084#ifdef HAVE_POPEN
Guido van Rossum85e3b011991-06-03 12:42:10 +00001085static object *
Guido van Rossum3b066191991-06-04 19:40:25 +00001086posix_popen(self, args)
1087 object *self;
1088 object *args;
1089{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001090 char *name;
1091 char *mode = "r";
1092 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001093 FILE *fp;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001094 object *f;
1095 if (!newgetargs(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001096 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001097 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001098 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001099 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +00001100 if (fp == NULL)
1101 return posix_error();
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001102 f = newopenfileobject(fp, name, mode, pclose);
1103 if (f != NULL)
1104 setfilebufsize(f, bufsize);
1105 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001106}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001107#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001108
Guido van Rossumb6775db1994-08-01 11:34:53 +00001109#ifdef HAVE_SETUID
Guido van Rossum3b066191991-06-04 19:40:25 +00001110static object *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001111posix_setuid(self, args)
1112 object *self;
1113 object *args;
1114{
1115 int uid;
1116 if (!getargs(args, "i", &uid))
1117 return NULL;
1118 if (setuid(uid) < 0)
1119 return posix_error();
1120 INCREF(None);
1121 return None;
1122}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001123#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001124
Guido van Rossumb6775db1994-08-01 11:34:53 +00001125#ifdef HAVE_SETGID
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001126static object *
1127posix_setgid(self, args)
1128 object *self;
1129 object *args;
1130{
1131 int gid;
1132 if (!getargs(args, "i", &gid))
1133 return NULL;
1134 if (setgid(gid) < 0)
1135 return posix_error();
1136 INCREF(None);
1137 return None;
1138}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001139#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001140
Guido van Rossumb6775db1994-08-01 11:34:53 +00001141#ifdef HAVE_WAITPID
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001142static object *
Guido van Rossum21803b81992-08-09 12:55:27 +00001143posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001144 object *self;
1145 object *args;
1146{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001147 int pid, options, sts = 0;
Guido van Rossum21803b81992-08-09 12:55:27 +00001148 if (!getargs(args, "(ii)", &pid, &options))
1149 return NULL;
1150 BGN_SAVE
1151 pid = waitpid(pid, &sts, options);
1152 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +00001153 if (pid == -1)
1154 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001155 else
1156 return mkvalue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001157}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001158#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001159
Guido van Rossumad0ee831995-03-01 10:34:45 +00001160#ifdef HAVE_WAIT
Guido van Rossum21803b81992-08-09 12:55:27 +00001161static object *
1162posix_wait(self, args)
1163 object *self;
1164 object *args;
1165{
1166 int pid, sts;
Guido van Rossum21803b81992-08-09 12:55:27 +00001167 BGN_SAVE
1168 pid = wait(&sts);
1169 END_SAVE
1170 if (pid == -1)
1171 return posix_error();
1172 else
1173 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001174}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001175#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001176
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177static object *
1178posix_lstat(self, args)
1179 object *self;
1180 object *args;
1181{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001182#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001183 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001184#else /* !HAVE_LSTAT */
1185 return posix_do_stat(self, args, stat);
1186#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001187}
1188
Guido van Rossumb6775db1994-08-01 11:34:53 +00001189#ifdef HAVE_READLINK
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001190static object *
1191posix_readlink(self, args)
1192 object *self;
1193 object *args;
1194{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001195 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001196 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001197 int n;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001198 if (!getargs(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001200 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001201 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001202 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001203 if (n < 0)
1204 return posix_error();
1205 return newsizedstringobject(buf, n);
1206}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001207#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001208
Guido van Rossumb6775db1994-08-01 11:34:53 +00001209#ifdef HAVE_SYMLINK
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001210static object *
1211posix_symlink(self, args)
1212 object *self;
1213 object *args;
1214{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001215 return posix_2str(args, symlink);
1216}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001217#endif /* HAVE_SYMLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001218
Guido van Rossumb6775db1994-08-01 11:34:53 +00001219#ifdef HAVE_TIMES
1220#ifndef HZ
1221#define HZ 60 /* Universal constant :-) */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001222#endif /* HZ */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001223static object *
1224posix_times(self, args)
1225 object *self;
1226 object *args;
1227{
1228 struct tms t;
1229 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001230 if (!getnoarg(args))
1231 return NULL;
1232 errno = 0;
1233 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001234 if (c == (clock_t) -1)
1235 return posix_error();
Guido van Rossumbbc34101995-09-13 17:39:51 +00001236 return mkvalue("ddddd",
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001237 (double)t.tms_utime / HZ,
1238 (double)t.tms_stime / HZ,
1239 (double)t.tms_cutime / HZ,
Guido van Rossumbbc34101995-09-13 17:39:51 +00001240 (double)t.tms_cstime / HZ,
1241 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001242}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001243#endif /* HAVE_TIMES */
Guido van Rossumc0125471996-06-28 18:55:32 +00001244#if defined(_MSC_VER) && _MSC_VER > 850
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001245#define HAVE_TIMES /* so the method table will pick it up */
1246static object *
1247posix_times(self, args)
1248 object *self;
1249 object *args;
1250{
1251 FILETIME create, exit, kernel, user;
1252 HANDLE hProc;
1253 if (!getnoarg(args))
1254 return NULL;
1255 hProc = GetCurrentProcess();
1256 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Guido van Rossumbbc34101995-09-13 17:39:51 +00001257 return mkvalue("ddddd",
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001258 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime) / 2E6,
1259 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1260 (double)0,
Guido van Rossumbbc34101995-09-13 17:39:51 +00001261 (double)0,
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001262 (double)0);
1263}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001264#endif /* MS_WIN32 */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001265
Guido van Rossumb6775db1994-08-01 11:34:53 +00001266#ifdef HAVE_SETSID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001267static object *
1268posix_setsid(self, args)
1269 object *self;
1270 object *args;
1271{
1272 if (!getnoarg(args))
1273 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001274 if (setsid() < 0)
1275 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +00001276 INCREF(None);
1277 return None;
1278}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001279#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001280
Guido van Rossumb6775db1994-08-01 11:34:53 +00001281#ifdef HAVE_SETPGID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001282static object *
1283posix_setpgid(self, args)
1284 object *self;
1285 object *args;
1286{
1287 int pid, pgrp;
1288 if (!getargs(args, "(ii)", &pid, &pgrp))
1289 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001290 if (setpgid(pid, pgrp) < 0)
1291 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +00001292 INCREF(None);
1293 return None;
1294}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001295#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001296
Guido van Rossumb6775db1994-08-01 11:34:53 +00001297#ifdef HAVE_TCGETPGRP
Guido van Rossum7066dd71992-09-17 17:54:56 +00001298static object *
1299posix_tcgetpgrp(self, args)
1300 object *self;
1301 object *args;
1302{
1303 int fd, pgid;
1304 if (!getargs(args, "i", &fd))
1305 return NULL;
1306 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001307 if (pgid < 0)
1308 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +00001309 return newintobject((long)pgid);
1310}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001311#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001312
Guido van Rossumb6775db1994-08-01 11:34:53 +00001313#ifdef HAVE_TCSETPGRP
Guido van Rossum7066dd71992-09-17 17:54:56 +00001314static object *
1315posix_tcsetpgrp(self, args)
1316 object *self;
1317 object *args;
1318{
1319 int fd, pgid;
1320 if (!getargs(args, "(ii)", &fd, &pgid))
1321 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001322 if (tcsetpgrp(fd, pgid) < 0)
1323 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +00001324 INCREF(None);
1325 return None;
1326}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001327#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001328
Guido van Rossum687dd131993-05-17 08:34:16 +00001329/* Functions acting on file descriptors */
1330
Guido van Rossum234f9421993-06-17 12:35:49 +00001331static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001332posix_open(self, args)
1333 object *self;
1334 object *args;
1335{
1336 char *file;
1337 int flag;
1338 int mode = 0777;
1339 int fd;
1340 if (!getargs(args, "(si)", &file, &flag)) {
1341 err_clear();
1342 if (!getargs(args, "(sii)", &file, &flag, &mode))
1343 return NULL;
1344 }
1345 BGN_SAVE
1346 fd = open(file, flag, mode);
1347 END_SAVE
1348 if (fd < 0)
1349 return posix_error();
1350 return newintobject((long)fd);
1351}
1352
Guido van Rossum234f9421993-06-17 12:35:49 +00001353static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001354posix_close(self, args)
1355 object *self;
1356 object *args;
1357{
1358 int fd, res;
1359 if (!getargs(args, "i", &fd))
1360 return NULL;
1361 BGN_SAVE
1362 res = close(fd);
1363 END_SAVE
1364 if (res < 0)
1365 return posix_error();
1366 INCREF(None);
1367 return None;
1368}
1369
Guido van Rossum234f9421993-06-17 12:35:49 +00001370static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001371posix_dup(self, args)
1372 object *self;
1373 object *args;
1374{
1375 int fd;
1376 if (!getargs(args, "i", &fd))
1377 return NULL;
1378 BGN_SAVE
1379 fd = dup(fd);
1380 END_SAVE
1381 if (fd < 0)
1382 return posix_error();
1383 return newintobject((long)fd);
1384}
1385
Guido van Rossum234f9421993-06-17 12:35:49 +00001386static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001387posix_dup2(self, args)
1388 object *self;
1389 object *args;
1390{
1391 int fd, fd2, res;
1392 if (!getargs(args, "(ii)", &fd, &fd2))
1393 return NULL;
1394 BGN_SAVE
1395 res = dup2(fd, fd2);
1396 END_SAVE
1397 if (res < 0)
1398 return posix_error();
1399 INCREF(None);
1400 return None;
1401}
1402
Guido van Rossum234f9421993-06-17 12:35:49 +00001403static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001404posix_lseek(self, args)
1405 object *self;
1406 object *args;
1407{
1408 int fd, how;
1409 long pos, res;
1410 if (!getargs(args, "(ili)", &fd, &pos, &how))
1411 return NULL;
1412#ifdef SEEK_SET
1413 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1414 switch (how) {
1415 case 0: how = SEEK_SET; break;
1416 case 1: how = SEEK_CUR; break;
1417 case 2: how = SEEK_END; break;
1418 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001419#endif /* SEEK_END */
Guido van Rossum687dd131993-05-17 08:34:16 +00001420 BGN_SAVE
1421 res = lseek(fd, pos, how);
1422 END_SAVE
1423 if (res < 0)
1424 return posix_error();
1425 return newintobject(res);
1426}
1427
Guido van Rossum234f9421993-06-17 12:35:49 +00001428static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001429posix_read(self, args)
1430 object *self;
1431 object *args;
1432{
Guido van Rossum8bac5461996-06-11 18:38:48 +00001433 int fd, size, n;
Guido van Rossum687dd131993-05-17 08:34:16 +00001434 object *buffer;
1435 if (!getargs(args, "(ii)", &fd, &size))
1436 return NULL;
1437 buffer = newsizedstringobject((char *)NULL, size);
1438 if (buffer == NULL)
1439 return NULL;
1440 BGN_SAVE
Guido van Rossum8bac5461996-06-11 18:38:48 +00001441 n = read(fd, getstringvalue(buffer), size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001442 END_SAVE
Guido van Rossum8bac5461996-06-11 18:38:48 +00001443 if (n < 0) {
Guido van Rossum687dd131993-05-17 08:34:16 +00001444 DECREF(buffer);
1445 return posix_error();
1446 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00001447 if (n != size)
1448 resizestring(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00001449 return buffer;
1450}
1451
Guido van Rossum234f9421993-06-17 12:35:49 +00001452static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001453posix_write(self, args)
1454 object *self;
1455 object *args;
1456{
1457 int fd, size;
1458 char *buffer;
1459 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1460 return NULL;
1461 BGN_SAVE
1462 size = write(fd, buffer, size);
1463 END_SAVE
1464 if (size < 0)
1465 return posix_error();
1466 return newintobject((long)size);
1467}
1468
Guido van Rossum234f9421993-06-17 12:35:49 +00001469static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001470posix_fstat(self, args)
1471 object *self;
1472 object *args;
1473{
1474 int fd;
1475 struct stat st;
1476 int res;
1477 if (!getargs(args, "i", &fd))
1478 return NULL;
1479 BGN_SAVE
1480 res = fstat(fd, &st);
1481 END_SAVE
1482 if (res != 0)
1483 return posix_error();
1484 return mkvalue("(llllllllll)",
1485 (long)st.st_mode,
1486 (long)st.st_ino,
1487 (long)st.st_dev,
1488 (long)st.st_nlink,
1489 (long)st.st_uid,
1490 (long)st.st_gid,
1491 (long)st.st_size,
1492 (long)st.st_atime,
1493 (long)st.st_mtime,
1494 (long)st.st_ctime);
1495}
1496
1497static object *
1498posix_fdopen(self, args)
1499 object *self;
1500 object *args;
1501{
1502 extern int fclose PROTO((FILE *));
1503 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001504 char *mode = "r";
1505 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00001506 FILE *fp;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001507 object *f;
1508 if (!newgetargs(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00001509 return NULL;
1510 BGN_SAVE
1511 fp = fdopen(fd, mode);
1512 END_SAVE
1513 if (fp == NULL)
1514 return posix_error();
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001515 f = newopenfileobject(fp, "(fdopen)", mode, fclose);
1516 if (f != NULL)
1517 setfilebufsize(f, bufsize);
1518 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00001519}
1520
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001521#ifdef HAVE_PIPE
Guido van Rossum234f9421993-06-17 12:35:49 +00001522static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001523posix_pipe(self, args)
1524 object *self;
1525 object *args;
1526{
Guido van Rossum8d665e61996-06-26 18:22:49 +00001527#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00001528 int fds[2];
1529 int res;
1530 if (!getargs(args, ""))
1531 return NULL;
1532 BGN_SAVE
1533 res = pipe(fds);
1534 END_SAVE
1535 if (res != 0)
1536 return posix_error();
1537 return mkvalue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001538#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00001539 HANDLE read, write;
1540 BOOL ok;
1541 if (!getargs(args, ""))
1542 return NULL;
1543 BGN_SAVE
1544 ok = CreatePipe( &read, &write, NULL, 0);
1545 END_SAVE
1546 if (!ok)
1547 return posix_error();
1548 return mkvalue("(ii)", read, write);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001549#endif /* MS_WIN32 */
Guido van Rossum687dd131993-05-17 08:34:16 +00001550}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001551#endif /* HAVE_PIPE */
1552
1553#ifdef HAVE_MKFIFO
1554static object *
1555posix_mkfifo(self, args)
1556 object *self;
1557 object *args;
1558{
1559 char *file;
1560 int mode = 0666;
1561 int res;
1562 if (!newgetargs(args, "s|i", &file, &mode))
1563 return NULL;
1564 BGN_SAVE
1565 res = mkfifo(file, mode);
1566 END_SAVE
1567 if (res < 0)
1568 return posix_error();
1569 INCREF(None);
1570 return None;
1571}
1572#endif
1573
1574#ifdef HAVE_FTRUNCATE
1575static object *
1576posix_ftruncate(self, args)
1577 object *self; /* Not used */
1578 object *args;
1579{
1580 int fd;
1581 long length;
1582 int res;
1583
1584 if (!getargs(args, "(il)", &fd, &length))
1585 return NULL;
1586
1587 BGN_SAVE
1588 res = ftruncate(fd, length);
1589 END_SAVE
1590 if (res < 0) {
1591 err_errno(IOError);
1592 return NULL;
1593 }
1594 INCREF(None);
1595 return None;
1596}
1597#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001598
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001599static struct methodlist posix_methods[] = {
1600 {"chdir", posix_chdir},
1601 {"chmod", posix_chmod},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001602#ifdef HAVE_CHOWN
1603 {"chown", posix_chown},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001604#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00001605#ifdef HAVE_GETCWD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001606 {"getcwd", posix_getcwd},
Guido van Rossum36bc6801995-06-14 22:54:23 +00001607#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001608#ifdef HAVE_LINK
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609 {"link", posix_link},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001610#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001611 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001612 {"lstat", posix_lstat},
Guido van Rossumb0824db1996-02-25 04:50:32 +00001613 {"mkdir", posix_mkdir, 1},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001614#ifdef HAVE_NICE
Guido van Rossum775f4da1993-01-09 17:18:52 +00001615 {"nice", posix_nice},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001616#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001617#ifdef HAVE_READLINK
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001618 {"readlink", posix_readlink},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001619#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001620 {"rename", posix_rename},
1621 {"rmdir", posix_rmdir},
1622 {"stat", posix_stat},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001623#ifdef HAVE_SYMLINK
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001624 {"symlink", posix_symlink},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001625#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001626#ifdef HAVE_SYSTEM
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627 {"system", posix_system},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001628#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001629 {"umask", posix_umask},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001630#ifdef HAVE_UNAME
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001631 {"uname", posix_uname},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001632#endif /* HAVE_UNAME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001633 {"unlink", posix_unlink},
Guido van Rossumf4a59121996-02-13 00:12:56 +00001634 {"remove", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001635 {"utime", posix_utime},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001636#ifdef HAVE_TIMES
Guido van Rossum22db57e1992-04-05 14:25:30 +00001637 {"times", posix_times},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001638#endif /* HAVE_TIMES */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001639 {"_exit", posix__exit},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001640#ifdef HAVE_EXECV
Guido van Rossum89b33251993-10-22 14:26:06 +00001641 {"execv", posix_execv},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001642 {"execve", posix_execve},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001643#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00001644#ifdef HAVE_FORK
Guido van Rossum85e3b011991-06-03 12:42:10 +00001645 {"fork", posix_fork},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001646#endif /* HAVE_FORK */
1647#ifdef HAVE_GETEGID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001648 {"getegid", posix_getegid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001649#endif /* HAVE_GETEGID */
1650#ifdef HAVE_GETEUID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001651 {"geteuid", posix_geteuid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001652#endif /* HAVE_GETEUID */
1653#ifdef HAVE_GETGID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001654 {"getgid", posix_getgid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001655#endif /* HAVE_GETGID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001656 {"getpid", posix_getpid},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001657#ifdef HAVE_GETPGRP
Guido van Rossum04814471991-06-04 20:23:49 +00001658 {"getpgrp", posix_getpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001659#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00001660#ifdef HAVE_GETPPID
Guido van Rossum85e3b011991-06-03 12:42:10 +00001661 {"getppid", posix_getppid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001662#endif /* HAVE_GETPPID */
1663#ifdef HAVE_GETUID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001664 {"getuid", posix_getuid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001665#endif /* HAVE_GETUID */
1666#ifdef HAVE_KILL
Guido van Rossum85e3b011991-06-03 12:42:10 +00001667 {"kill", posix_kill},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001668#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00001669#ifdef HAVE_PLOCK
1670 {"plock", posix_plock},
1671#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001672#ifdef HAVE_POPEN
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001673 {"popen", posix_popen, 1},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001674#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001675#ifdef HAVE_SETUID
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001676 {"setuid", posix_setuid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001677#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001678#ifdef HAVE_SETGID
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001679 {"setgid", posix_setgid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001680#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001681#ifdef HAVE_SETPGRP
Guido van Rossumc2670a01992-09-13 20:07:29 +00001682 {"setpgrp", posix_setpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001683#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00001684#ifdef HAVE_WAIT
Guido van Rossum85e3b011991-06-03 12:42:10 +00001685 {"wait", posix_wait},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001686#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001687#ifdef HAVE_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +00001688 {"waitpid", posix_waitpid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001689#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001690#ifdef HAVE_SETSID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001691 {"setsid", posix_setsid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001692#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001693#ifdef HAVE_SETPGID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001694 {"setpgid", posix_setpgid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001695#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001696#ifdef HAVE_TCGETPGRP
Guido van Rossum7066dd71992-09-17 17:54:56 +00001697 {"tcgetpgrp", posix_tcgetpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001698#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001699#ifdef HAVE_TCSETPGRP
Guido van Rossum7066dd71992-09-17 17:54:56 +00001700 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001701#endif /* HAVE_TCSETPGRP */
Guido van Rossum687dd131993-05-17 08:34:16 +00001702 {"open", posix_open},
1703 {"close", posix_close},
1704 {"dup", posix_dup},
1705 {"dup2", posix_dup2},
1706 {"lseek", posix_lseek},
1707 {"read", posix_read},
1708 {"write", posix_write},
1709 {"fstat", posix_fstat},
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001710 {"fdopen", posix_fdopen, 1},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001711#ifdef HAVE_PIPE
Guido van Rossum687dd131993-05-17 08:34:16 +00001712 {"pipe", posix_pipe},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001713#endif
1714#ifdef HAVE_MKFIFO
1715 {"mkfifo", posix_mkfifo, 1},
1716#endif
1717#ifdef HAVE_FTRUNCATE
1718 {"ftruncate", posix_ftruncate, 1},
1719#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001720 {NULL, NULL} /* Sentinel */
1721};
1722
1723
Guido van Rossuma0e71301996-05-28 22:30:38 +00001724#if defined(_MSC_VER) || defined(__WATCOMC__)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001725void
1726initnt()
1727{
1728 object *m, *d, *v;
1729
1730 m = initmodule("nt", posix_methods);
1731 d = getmoduledict(m);
1732
1733 /* Initialize nt.environ dictionary */
1734 v = convertenviron();
1735 if (v == NULL || dictinsert(d, "environ", v) != 0)
1736 fatal("can't define nt.environ");
1737 DECREF(v);
1738
1739 /* Initialize nt.error exception */
1740 PosixError = newstringobject("nt.error");
1741 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1742 fatal("can't define nt.error");
1743}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001744#else /* not a PC port */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001745void
1746initposix()
1747{
1748 object *m, *d, *v;
1749
1750 m = initmodule("posix", posix_methods);
1751 d = getmoduledict(m);
1752
1753 /* Initialize posix.environ dictionary */
1754 v = convertenviron();
1755 if (v == NULL || dictinsert(d, "environ", v) != 0)
1756 fatal("can't define posix.environ");
1757 DECREF(v);
1758
Guido van Rossumde4e1241995-03-30 10:56:54 +00001759#ifdef WNOHANG
1760 /* Export WNOHANG symbol */
1761 v = newintobject((long)WNOHANG);
1762 if (v == NULL || dictinsert(d, "WNOHANG", v) != 0)
1763 fatal("can't define posix.WNOHANG");
1764 DECREF(v);
1765#endif
1766
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001767 /* Initialize posix.error exception */
1768 PosixError = newstringobject("posix.error");
1769 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1770 fatal("can't define posix.error");
1771}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001772#endif /* !_MSC_VER */