blob: 73077431c0a0ae2a2c2099870d0f5bbb94879b15 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* POSIX module implementation */
33
Guido van Rossuma4916fa1996-05-23 22:58:55 +000034/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +000035 actually calls itself 'nt', not 'posix', and a few functions are
36 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +000037 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +000038 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +000039 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000040
Guido van Rossuma4916fa1996-05-23 22:58:55 +000041/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000042
Barry Warsaw53699e91996-12-10 23:23:01 +000043#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Guido van Rossumb6775db1994-08-01 11:34:53 +000045#include <sys/types.h>
46#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000047#ifdef HAVE_SYS_WAIT_H
48#include <sys/wait.h> /* For WNOHANG */
49#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000050
Guido van Rossuma376cc51996-12-05 23:43:35 +000051#ifdef HAVE_SIGNAL_H
52#include <signal.h>
53#endif
54
Guido van Rossumb6775db1994-08-01 11:34:53 +000055#include "mytime.h" /* For clock_t on some systems */
56
57#ifdef HAVE_FCNTL_H
58#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000059#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000060
Guido van Rossuma4916fa1996-05-23 22:58:55 +000061/* Various compilers have only certain posix functions */
62#ifdef __WATCOMC__ /* Watcom compiler */
63#define HAVE_GETCWD 1
64#define HAVE_OPENDIR 1
65#define HAVE_SYSTEM 1
66#if defined(__OS2__)
67#define HAVE_EXECV 1
68#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000069#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000070#include <process.h>
71#else
72#ifdef __BORLANDC__ /* Borland compiler */
73#define HAVE_EXECV 1
74#define HAVE_GETCWD 1
75#define HAVE_GETEGID 1
76#define HAVE_GETEUID 1
77#define HAVE_GETGID 1
78#define HAVE_GETPPID 1
79#define HAVE_GETUID 1
80#define HAVE_KILL 1
81#define HAVE_OPENDIR 1
82#define HAVE_PIPE 1
83#define HAVE_POPEN 1
84#define HAVE_SYSTEM 1
85#define HAVE_WAIT 1
86#else
87#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000088#define HAVE_GETCWD 1
89#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +000090#define HAVE_EXECV 1
91#define HAVE_PIPE 1
92#define HAVE_POPEN 1
93#define HAVE_SYSTEM 1
94#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +000095#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000096#else /* all other compilers */
97/* Unix functions that the configure script doesn't check for */
98#define HAVE_EXECV 1
99#define HAVE_FORK 1
100#define HAVE_GETCWD 1
101#define HAVE_GETEGID 1
102#define HAVE_GETEUID 1
103#define HAVE_GETGID 1
104#define HAVE_GETPPID 1
105#define HAVE_GETUID 1
106#define HAVE_KILL 1
107#define HAVE_OPENDIR 1
108#define HAVE_PIPE 1
109#define HAVE_POPEN 1
110#define HAVE_SYSTEM 1
111#define HAVE_WAIT 1
112#endif /* _MSC_VER */
113#endif /* __BORLANDC__ */
114#endif /* ! __WATCOMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000115
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000117
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000119#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000120#endif
121
122#ifdef NeXT
123/* NeXT's <unistd.h> and <utime.h> aren't worth much */
124#undef HAVE_UNISTD_H
125#undef HAVE_UTIME_H
126/* #undef HAVE_GETCWD */
127#endif
128
129#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000130/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
131extern int rename();
132extern int pclose();
133extern int lstat();
134extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135#else /* !HAVE_UNISTD_H */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000136#if defined(__WATCOMC__) || defined(_MSC_VER)
Barry Warsaw53699e91996-12-10 23:23:01 +0000137extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000138#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000139extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000141extern int chdir Py_PROTO((const char *));
142extern int rmdir Py_PROTO((const char *));
143extern int chmod Py_PROTO((const char *, mode_t));
144extern int chown Py_PROTO((const char *, uid_t, gid_t));
145extern char *getcwd Py_PROTO((char *, int));
146extern char *strerror Py_PROTO((int));
147extern int link Py_PROTO((const char *, const char *));
148extern int rename Py_PROTO((const char *, const char *));
149extern int stat Py_PROTO((const char *, struct stat *));
150extern int unlink Py_PROTO((const char *));
151extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000153extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000154#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000156extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000157#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162#ifdef HAVE_UTIME_H
163#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000164#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000166#ifdef HAVE_SYS_UTIME_H
167#include <sys/utime.h>
168#define HAVE_UTIME_H /* pretend we do for the rest of this file */
169#endif /* HAVE_SYS_UTIME_H */
170
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171#ifdef HAVE_SYS_TIMES_H
172#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000173#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174
175#ifdef HAVE_SYS_PARAM_H
176#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000177#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178
179#ifdef HAVE_SYS_UTSNAME_H
180#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000181#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182
183#ifndef MAXPATHLEN
184#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000185#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000187#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000189#define NAMLEN(dirent) strlen((dirent)->d_name)
190#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000191#ifdef __WATCOMC__
192#include <direct.h>
193#define NAMLEN(dirent) strlen((dirent)->d_name)
194#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000196#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000198#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000200#endif
201#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000203#endif
204#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#endif
207#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210#include <direct.h>
211#include <io.h>
212#include <process.h>
213#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000214#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000215#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000216#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#else /* 16-bit Windows */
218#include <dos.h>
219#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000220#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
223#ifdef OS2
224#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226
227/* Return a dictionary corresponding to the POSIX environment table */
228
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000229#if !defined(_MSC_VER) && !defined(__WATCOMC__)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232
Barry Warsaw53699e91996-12-10 23:23:01 +0000233static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234convertenviron()
235{
Barry Warsaw53699e91996-12-10 23:23:01 +0000236 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000238 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 if (d == NULL)
240 return NULL;
241 if (environ == NULL)
242 return d;
243 /* XXX This part ignores errors */
244 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000245 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246 char *p = strchr(*e, '=');
247 if (p == NULL)
248 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000249 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 if (v == NULL)
251 continue;
252 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000253 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000255 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 }
257 return d;
258}
259
260
Barry Warsaw53699e91996-12-10 23:23:01 +0000261static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262
263/* Set a POSIX-specific error from errno, and return NULL */
264
Barry Warsaw53699e91996-12-10 23:23:01 +0000265static PyObject * posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000266{
Barry Warsaw53699e91996-12-10 23:23:01 +0000267 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268}
269
270
271/* POSIX generic methods */
272
Barry Warsaw53699e91996-12-10 23:23:01 +0000273static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000275 PyObject *args;
276 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000278 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000279 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000280 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000282 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000283 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000284 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000287 Py_INCREF(Py_None);
288 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289}
290
Barry Warsaw53699e91996-12-10 23:23:01 +0000291static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000293 PyObject *args;
294 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000296 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000297 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000298 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000300 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000301 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000302 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000305 Py_INCREF(Py_None);
306 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307}
308
Barry Warsaw53699e91996-12-10 23:23:01 +0000309static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000311 PyObject *args;
312 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000314 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000316 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000317 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000319 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000320 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000321 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000322 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 Py_INCREF(Py_None);
325 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
Barry Warsaw53699e91996-12-10 23:23:01 +0000328static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000329posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000330 PyObject *args;
331 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000332{
333 char *path;
334 int i,i2;
335 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000336 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000337 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000338 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000340 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000341 if (res < 0)
342 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000343 Py_INCREF(Py_None);
344 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000345}
346
Barry Warsaw53699e91996-12-10 23:23:01 +0000347static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000349 PyObject *self;
350 PyObject *args;
351 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352{
353 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000354 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000355 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000358 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000359 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000361 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000363 return Py_BuildValue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000364 (long)st.st_mode,
365 (long)st.st_ino,
366 (long)st.st_dev,
367 (long)st.st_nlink,
368 (long)st.st_uid,
369 (long)st.st_gid,
370 (long)st.st_size,
371 (long)st.st_atime,
372 (long)st.st_mtime,
373 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374}
375
376
377/* POSIX methods */
378
Barry Warsaw53699e91996-12-10 23:23:01 +0000379static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000381 PyObject *self;
382 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384 return posix_1str(args, chdir);
385}
386
Barry Warsaw53699e91996-12-10 23:23:01 +0000387static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000389 PyObject *self;
390 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392 return posix_strint(args, chmod);
393}
394
Guido van Rossumb6775db1994-08-01 11:34:53 +0000395#ifdef HAVE_CHOWN
Barry Warsaw53699e91996-12-10 23:23:01 +0000396static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000398 PyObject *self;
399 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000400{
401 return posix_strintint(args, chown);
402}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000403#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000404
Guido van Rossum36bc6801995-06-14 22:54:23 +0000405#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +0000406static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000408 PyObject *self;
409 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
411 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000412 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000413 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000415 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000416 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000417 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000418 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000420 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000422#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423
Guido van Rossumb6775db1994-08-01 11:34:53 +0000424#ifdef HAVE_LINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000425static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000427 PyObject *self;
428 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 return posix_2str(args, link);
431}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000432#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433
Barry Warsaw53699e91996-12-10 23:23:01 +0000434static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000435posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000436 PyObject *self;
437 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000438{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000439#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000440
Guido van Rossumb6775db1994-08-01 11:34:53 +0000441 char *name;
442 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000443 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000444 HANDLE hFindFile;
445 WIN32_FIND_DATA FileData;
446 char namebuf[MAX_PATH+5];
447
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000449 return NULL;
450 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000452 return NULL;
453 }
454 strcpy(namebuf, name);
455 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
456 namebuf[len++] = '/';
457 strcpy(namebuf + len, "*.*");
458
Barry Warsaw53699e91996-12-10 23:23:01 +0000459 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000460 return NULL;
461
462 hFindFile = FindFirstFile(namebuf, &FileData);
463 if (hFindFile == INVALID_HANDLE_VALUE) {
464 errno = GetLastError();
465 return posix_error();
466 }
467 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000468 if (FileData.cFileName[0] == '.' &&
469 (FileData.cFileName[1] == '\0' ||
470 FileData.cFileName[1] == '.' &&
471 FileData.cFileName[2] == '\0'))
472 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000473 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000474 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000476 d = NULL;
477 break;
478 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000479 if (PyList_Append(d, v) != 0) {
480 Py_DECREF(v);
481 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000482 d = NULL;
483 break;
484 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000485 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000486 } while (FindNextFile(hFindFile, &FileData) == TRUE);
487
488 if (FindClose(hFindFile) == FALSE) {
489 errno = GetLastError();
490 return posix_error();
491 }
492
493 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000494
Guido van Rossum8d665e61996-06-26 18:22:49 +0000495#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000496#ifdef _MSC_VER /* 16-bit Windows */
497
498#ifndef MAX_PATH
499#define MAX_PATH 250
500#endif
501 char *name, *pt;
502 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000503 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000504 char namebuf[MAX_PATH+5];
505 struct _find_t ep;
506
Barry Warsaw53699e91996-12-10 23:23:01 +0000507 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000508 return NULL;
509 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000510 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000511 return NULL;
512 }
513 strcpy(namebuf, name);
514 for (pt = namebuf; *pt; pt++)
515 if (*pt == '/')
516 *pt = '\\';
517 if (namebuf[len-1] != '\\')
518 namebuf[len++] = '\\';
519 strcpy(namebuf + len, "*.*");
520
Barry Warsaw53699e91996-12-10 23:23:01 +0000521 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000522 return NULL;
523
524 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000525 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
526 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000527 errno = ENOENT;
528 return posix_error();
529 }
530 do {
531 if (ep.name[0] == '.' &&
532 (ep.name[1] == '\0' ||
533 ep.name[1] == '.' &&
534 ep.name[2] == '\0'))
535 continue;
536 strcpy(namebuf, ep.name);
537 for (pt = namebuf; *pt; pt++)
538 if (isupper(*pt))
539 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000540 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000541 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000542 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000543 d = NULL;
544 break;
545 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000546 if (PyList_Append(d, v) != 0) {
547 Py_DECREF(v);
548 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000549 d = NULL;
550 break;
551 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000552 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000553 } while (_dos_findnext(&ep) == 0);
554
555 return d;
556
557#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000558
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000559 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000560 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000562 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000563 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000565 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000566 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000567 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000569 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000570 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000572 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573 return NULL;
574 }
575 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000576 if (ep->d_name[0] == '.' &&
577 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000578 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000579 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000580 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000582 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583 d = NULL;
584 break;
585 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000586 if (PyList_Append(d, v) != 0) {
587 Py_DECREF(v);
588 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589 d = NULL;
590 break;
591 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000592 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593 }
594 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000595 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000596
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000598
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000599#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000600#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601}
602
Barry Warsaw53699e91996-12-10 23:23:01 +0000603static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000605 PyObject *self;
606 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000608 int res;
609 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000610 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000611 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000612 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8d665e61996-06-26 18:22:49 +0000614#if defined(__WATCOMC__) || defined(_MSC_VER)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000615 res = mkdir(path);
616#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000617 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000618#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000619 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000620 if (res < 0)
621 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000622 Py_INCREF(Py_None);
623 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624}
625
Guido van Rossumb6775db1994-08-01 11:34:53 +0000626#ifdef HAVE_NICE
Barry Warsaw53699e91996-12-10 23:23:01 +0000627static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000628posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000629 PyObject *self;
630 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000631{
632 int increment, value;
633
Barry Warsaw53699e91996-12-10 23:23:01 +0000634 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000635 return NULL;
636 value = nice(increment);
637 if (value == -1)
638 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000639 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000640}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000641#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000642
Barry Warsaw53699e91996-12-10 23:23:01 +0000643static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000645 PyObject *self;
646 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648 return posix_2str(args, rename);
649}
650
Barry Warsaw53699e91996-12-10 23:23:01 +0000651static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000653 PyObject *self;
654 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000655{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656 return posix_1str(args, rmdir);
657}
658
Barry Warsaw53699e91996-12-10 23:23:01 +0000659static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000661 PyObject *self;
662 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000663{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664 return posix_do_stat(self, args, stat);
665}
666
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000667#ifdef HAVE_SYSTEM
Barry Warsaw53699e91996-12-10 23:23:01 +0000668static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000669posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000670 PyObject *self;
671 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000673 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000674 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000675 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000677 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000678 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000679 Py_END_ALLOW_THREADS
680 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000682#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000683
Barry Warsaw53699e91996-12-10 23:23:01 +0000684static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000686 PyObject *self;
687 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688{
689 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000690 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691 return NULL;
692 i = umask(i);
693 if (i < 0)
694 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000695 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696}
697
Barry Warsaw53699e91996-12-10 23:23:01 +0000698static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000700 PyObject *self;
701 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703 return posix_1str(args, unlink);
704}
705
Guido van Rossumb6775db1994-08-01 11:34:53 +0000706#ifdef HAVE_UNAME
Barry Warsaw53699e91996-12-10 23:23:01 +0000707static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000708posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000709 PyObject *self;
710 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000711{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000712 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000713 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000714 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000715 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000716 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000717 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +0000718 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000719 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000720 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000721 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +0000722 u.sysname,
723 u.nodename,
724 u.release,
725 u.version,
726 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000727}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000728#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000729
Barry Warsaw53699e91996-12-10 23:23:01 +0000730static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000731posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000732 PyObject *self;
733 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000735 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000736 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000737 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000738
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000740 struct utimbuf buf;
741#define ATIME buf.actime
742#define MTIME buf.modtime
743#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000744#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000745 time_t buf[2];
746#define ATIME buf[0]
747#define MTIME buf[1]
748#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000749#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000750
Barry Warsaw53699e91996-12-10 23:23:01 +0000751 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000752 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000753 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +0000754 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +0000755 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000756 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +0000757 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000758 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000760 Py_INCREF(Py_None);
761 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000762#undef UTIME_ARG
763#undef ATIME
764#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765}
766
Guido van Rossum85e3b011991-06-03 12:42:10 +0000767
Guido van Rossum3b066191991-06-04 19:40:25 +0000768/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000769
Barry Warsaw53699e91996-12-10 23:23:01 +0000770static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000771posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000772 PyObject *self;
773 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000774{
775 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000776 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000777 return NULL;
778 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000779 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000780}
781
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000782#ifdef HAVE_EXECV
Barry Warsaw53699e91996-12-10 23:23:01 +0000783static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +0000784posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000785 PyObject *self;
786 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000787{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000788 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +0000789 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000790 char **argvlist;
791 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +0000792 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +0000793
Guido van Rossum89b33251993-10-22 14:26:06 +0000794 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000795 argv is a list or tuple of strings. */
796
Barry Warsaw53699e91996-12-10 23:23:01 +0000797 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000798 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000799 if (PyList_Check(argv)) {
800 argc = PyList_Size(argv);
801 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000802 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000803 else if (PyTuple_Check(argv)) {
804 argc = PyTuple_Size(argv);
805 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000806 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000807 else {
808 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +0000809 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000810 return NULL;
811 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000812
Barry Warsaw53699e91996-12-10 23:23:01 +0000813 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000814 if (argvlist == NULL)
815 return NULL;
816 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000817 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
818 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000819 goto badarg;
820 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000821 }
822 argvlist[argc] = NULL;
823
Guido van Rossumb6775db1994-08-01 11:34:53 +0000824#ifdef BAD_EXEC_PROTOTYPES
825 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000826#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000827 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000828#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829
Guido van Rossum85e3b011991-06-03 12:42:10 +0000830 /* If we get here it's definitely an error */
831
Barry Warsaw53699e91996-12-10 23:23:01 +0000832 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000833 return posix_error();
834}
835
Barry Warsaw53699e91996-12-10 23:23:01 +0000836static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000837posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000838 PyObject *self;
839 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000840{
841 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +0000842 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000843 char **argvlist;
844 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000845 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000846 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +0000847 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000848
849 /* execve has three arguments: (path, argv, env), where
850 argv is a list or tuple of strings and env is a dictionary
851 like posix.environ. */
852
Barry Warsaw53699e91996-12-10 23:23:01 +0000853 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000854 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000855 if (PyList_Check(argv)) {
856 argc = PyList_Size(argv);
857 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000858 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000859 else if (PyTuple_Check(argv)) {
860 argc = PyTuple_Size(argv);
861 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000862 }
863 else {
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000865 return NULL;
866 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000867 if (!PyMapping_Check(env)) {
868 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000869 return NULL;
870 }
871
Barry Warsaw53699e91996-12-10 23:23:01 +0000872 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000873 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000874 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000875 return NULL;
876 }
877 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000878 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +0000879 "s;argv must be list of strings",
880 &argvlist[i]))
881 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000882 goto fail_1;
883 }
884 }
885 argvlist[argc] = NULL;
886
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000887 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +0000888 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000889 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000890 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000891 goto fail_1;
892 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000893 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000894 keys = PyMapping_Keys(env);
895 vals = PyMapping_Values(env);
896 if (!keys || !vals)
897 goto fail_2;
898
899 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000900 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000901
902 key = PyList_GetItem(keys, pos);
903 val = PyList_GetItem(vals, pos);
904 if (!key || !val)
905 goto fail_2;
906
Barry Warsaw53699e91996-12-10 23:23:01 +0000907 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +0000908 !PyArg_Parse(val, "s;non-string value in env", &v))
909 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000910 goto fail_2;
911 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000912 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000913 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000914 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000915 goto fail_2;
916 }
917 sprintf(p, "%s=%s", k, v);
918 envlist[envc++] = p;
919 }
920 envlist[envc] = 0;
921
Guido van Rossumb6775db1994-08-01 11:34:53 +0000922
923#ifdef BAD_EXEC_PROTOTYPES
924 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000925#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000926 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000927#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000928
929 /* If we get here it's definitely an error */
930
931 (void) posix_error();
932
933 fail_2:
934 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +0000935 PyMem_DEL(envlist[envc]);
936 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000937 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +0000938 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000939 Py_XDECREF(vals);
940 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000941 return NULL;
942}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000943#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000944
Guido van Rossumad0ee831995-03-01 10:34:45 +0000945#ifdef HAVE_FORK
Barry Warsaw53699e91996-12-10 23:23:01 +0000946static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000947posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000948 PyObject *self;
949 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000950{
951 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +0000952 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000953 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000954 pid = fork();
955 if (pid == -1)
956 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000957 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000958}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000959#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +0000960
Guido van Rossumad0ee831995-03-01 10:34:45 +0000961#ifdef HAVE_GETEGID
Barry Warsaw53699e91996-12-10 23:23:01 +0000962static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000963posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 PyObject *self;
965 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +0000966{
Barry Warsaw53699e91996-12-10 23:23:01 +0000967 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +0000968 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000969 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +0000970}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000971#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +0000972
Guido van Rossumad0ee831995-03-01 10:34:45 +0000973#ifdef HAVE_GETEUID
Barry Warsaw53699e91996-12-10 23:23:01 +0000974static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000975posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 PyObject *self;
977 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +0000978{
Barry Warsaw53699e91996-12-10 23:23:01 +0000979 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +0000980 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000981 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +0000982}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000983#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +0000984
Guido van Rossumad0ee831995-03-01 10:34:45 +0000985#ifdef HAVE_GETGID
Barry Warsaw53699e91996-12-10 23:23:01 +0000986static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000987posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000988 PyObject *self;
989 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +0000990{
Barry Warsaw53699e91996-12-10 23:23:01 +0000991 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +0000992 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000993 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +0000994}
Guido van Rossumad0ee831995-03-01 10:34:45 +0000995#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +0000996
Barry Warsaw53699e91996-12-10 23:23:01 +0000997static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000998posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000999 PyObject *self;
1000 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001001{
Barry Warsaw53699e91996-12-10 23:23:01 +00001002 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001003 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001005}
1006
Guido van Rossumb6775db1994-08-01 11:34:53 +00001007#ifdef HAVE_GETPGRP
Barry Warsaw53699e91996-12-10 23:23:01 +00001008static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001009posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001010 PyObject *self;
1011 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001012{
Barry Warsaw53699e91996-12-10 23:23:01 +00001013 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001014 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001015#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001016 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001017#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001018 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001019#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001020}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001021#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001022
Guido van Rossumb6775db1994-08-01 11:34:53 +00001023#ifdef HAVE_SETPGRP
Barry Warsaw53699e91996-12-10 23:23:01 +00001024static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001025posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001026 PyObject *self;
1027 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001028{
Barry Warsaw53699e91996-12-10 23:23:01 +00001029 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001030 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001031#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001032 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001033#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001034 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001035#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001036 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001037 Py_INCREF(Py_None);
1038 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001039}
1040
Guido van Rossumb6775db1994-08-01 11:34:53 +00001041#endif /* HAVE_SETPGRP */
1042
Guido van Rossumad0ee831995-03-01 10:34:45 +00001043#ifdef HAVE_GETPPID
Barry Warsaw53699e91996-12-10 23:23:01 +00001044static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001045posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001046 PyObject *self;
1047 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001048{
Barry Warsaw53699e91996-12-10 23:23:01 +00001049 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001050 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001051 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001052}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001053#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001054
Guido van Rossumad0ee831995-03-01 10:34:45 +00001055#ifdef HAVE_GETUID
Barry Warsaw53699e91996-12-10 23:23:01 +00001056static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001057posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001058 PyObject *self;
1059 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001060{
Barry Warsaw53699e91996-12-10 23:23:01 +00001061 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001062 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001063 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001064}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001065#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001066
Guido van Rossumad0ee831995-03-01 10:34:45 +00001067#ifdef HAVE_KILL
Barry Warsaw53699e91996-12-10 23:23:01 +00001068static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001069posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 PyObject *self;
1071 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001072{
1073 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001074 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001075 return NULL;
1076 if (kill(pid, sig) == -1)
1077 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001078 Py_INCREF(Py_None);
1079 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001080}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001081#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001082
Guido van Rossumc0125471996-06-28 18:55:32 +00001083#ifdef HAVE_PLOCK
1084
1085#ifdef HAVE_SYS_LOCK_H
1086#include <sys/lock.h>
1087#endif
1088
Barry Warsaw53699e91996-12-10 23:23:01 +00001089static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001090posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001091 PyObject *self;
1092 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001093{
1094 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001095 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001096 return NULL;
1097 if (plock(op) == -1)
1098 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001099 Py_INCREF(Py_None);
1100 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001101}
1102#endif
1103
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001104#ifdef HAVE_POPEN
Barry Warsaw53699e91996-12-10 23:23:01 +00001105static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001106posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001107 PyObject *self;
1108 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001109{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001110 char *name;
1111 char *mode = "r";
1112 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001113 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001114 PyObject *f;
1115 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001116 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001117 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001118 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001119 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001120 if (fp == NULL)
1121 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001122 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001123 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001124 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001125 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001126}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001127#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001128
Guido van Rossumb6775db1994-08-01 11:34:53 +00001129#ifdef HAVE_SETUID
Barry Warsaw53699e91996-12-10 23:23:01 +00001130static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001131posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001132 PyObject *self;
1133 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001134{
1135 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001136 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001137 return NULL;
1138 if (setuid(uid) < 0)
1139 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001140 Py_INCREF(Py_None);
1141 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001142}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001143#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001144
Guido van Rossumb6775db1994-08-01 11:34:53 +00001145#ifdef HAVE_SETGID
Barry Warsaw53699e91996-12-10 23:23:01 +00001146static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001147posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001148 PyObject *self;
1149 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001150{
1151 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001152 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001153 return NULL;
1154 if (setgid(gid) < 0)
1155 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001156 Py_INCREF(Py_None);
1157 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001158}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001159#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001160
Guido van Rossumb6775db1994-08-01 11:34:53 +00001161#ifdef HAVE_WAITPID
Barry Warsaw53699e91996-12-10 23:23:01 +00001162static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001163posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001164 PyObject *self;
1165 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001166{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001167 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001168 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001169 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001170 Py_BEGIN_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001171 pid = waitpid(pid, &sts, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00001172 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001173 if (pid == -1)
1174 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001175 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001176 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001177}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001178#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001179
Guido van Rossumad0ee831995-03-01 10:34:45 +00001180#ifdef HAVE_WAIT
Barry Warsaw53699e91996-12-10 23:23:01 +00001181static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001182posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001183 PyObject *self;
1184 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001185{
1186 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001187 Py_BEGIN_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001188 pid = wait(&sts);
Barry Warsaw53699e91996-12-10 23:23:01 +00001189 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001190 if (pid == -1)
1191 return posix_error();
1192 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001193 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001194}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001195#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001196
Barry Warsaw53699e91996-12-10 23:23:01 +00001197static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001198posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001199 PyObject *self;
1200 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001202#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001203 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001204#else /* !HAVE_LSTAT */
1205 return posix_do_stat(self, args, stat);
1206#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001207}
1208
Guido van Rossumb6775db1994-08-01 11:34:53 +00001209#ifdef HAVE_READLINK
Barry Warsaw53699e91996-12-10 23:23:01 +00001210static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001212 PyObject *self;
1213 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001215 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001216 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001217 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001218 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001220 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001221 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001222 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001223 if (n < 0)
1224 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001225 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001226}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001227#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001228
Guido van Rossumb6775db1994-08-01 11:34:53 +00001229#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +00001230static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001231posix_symlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001232 PyObject *self;
1233 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001234{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001235 return posix_2str(args, symlink);
1236}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001237#endif /* HAVE_SYMLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001238
Guido van Rossumb6775db1994-08-01 11:34:53 +00001239#ifdef HAVE_TIMES
1240#ifndef HZ
1241#define HZ 60 /* Universal constant :-) */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001242#endif /* HZ */
Barry Warsaw53699e91996-12-10 23:23:01 +00001243static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001244posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001245 PyObject *self;
1246 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001247{
1248 struct tms t;
1249 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001250 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001251 return NULL;
1252 errno = 0;
1253 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001254 if (c == (clock_t) -1)
1255 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001256 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001257 (double)t.tms_utime / HZ,
1258 (double)t.tms_stime / HZ,
1259 (double)t.tms_cutime / HZ,
1260 (double)t.tms_cstime / HZ,
1261 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001262}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001263#endif /* HAVE_TIMES */
Guido van Rossum87755a21996-09-07 00:59:43 +00001264#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001265#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001266static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001267posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001268 PyObject *self;
1269 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001270{
1271 FILETIME create, exit, kernel, user;
1272 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001273 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001274 return NULL;
1275 hProc = GetCurrentProcess();
1276 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001277 return Py_BuildValue(
1278 "ddddd",
1279 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1280 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1281 (double)0,
1282 (double)0,
1283 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001284}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001285#endif /* MS_WIN32 */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001286
Guido van Rossumb6775db1994-08-01 11:34:53 +00001287#ifdef HAVE_SETSID
Barry Warsaw53699e91996-12-10 23:23:01 +00001288static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001289posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001290 PyObject *self;
1291 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001292{
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001294 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001295 if (setsid() < 0)
1296 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001297 Py_INCREF(Py_None);
1298 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001299}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001300#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001301
Guido van Rossumb6775db1994-08-01 11:34:53 +00001302#ifdef HAVE_SETPGID
Barry Warsaw53699e91996-12-10 23:23:01 +00001303static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001304posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 PyObject *self;
1306 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001307{
1308 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001309 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001310 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001311 if (setpgid(pid, pgrp) < 0)
1312 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 Py_INCREF(Py_None);
1314 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001315}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001316#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001317
Guido van Rossumb6775db1994-08-01 11:34:53 +00001318#ifdef HAVE_TCGETPGRP
Barry Warsaw53699e91996-12-10 23:23:01 +00001319static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001320posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001321 PyObject *self;
1322 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001323{
1324 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001325 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001326 return NULL;
1327 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001328 if (pgid < 0)
1329 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001330 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001331}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001332#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001333
Guido van Rossumb6775db1994-08-01 11:34:53 +00001334#ifdef HAVE_TCSETPGRP
Barry Warsaw53699e91996-12-10 23:23:01 +00001335static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001336posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001337 PyObject *self;
1338 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001339{
1340 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001341 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001342 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001343 if (tcsetpgrp(fd, pgid) < 0)
1344 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001345 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001346 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001347}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001348#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001349
Guido van Rossum687dd131993-05-17 08:34:16 +00001350/* Functions acting on file descriptors */
1351
Barry Warsaw53699e91996-12-10 23:23:01 +00001352static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001353posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001354 PyObject *self;
1355 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001356{
1357 char *file;
1358 int flag;
1359 int mode = 0777;
1360 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001361 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1362 return NULL;
1363
Barry Warsaw53699e91996-12-10 23:23:01 +00001364 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001365 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001366 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001367 if (fd < 0)
1368 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001369 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001370}
1371
Barry Warsaw53699e91996-12-10 23:23:01 +00001372static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001373posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001374 PyObject *self;
1375 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001376{
1377 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001378 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001379 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001380 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001381 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00001382 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001383 if (res < 0)
1384 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001385 Py_INCREF(Py_None);
1386 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00001387}
1388
Barry Warsaw53699e91996-12-10 23:23:01 +00001389static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001390posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001391 PyObject *self;
1392 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001393{
1394 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00001395 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001396 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001397 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001398 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00001399 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001400 if (fd < 0)
1401 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001402 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001403}
1404
Barry Warsaw53699e91996-12-10 23:23:01 +00001405static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001406posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001407 PyObject *self;
1408 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001409{
1410 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001411 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00001412 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001413 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001414 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00001415 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001416 if (res < 0)
1417 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001418 Py_INCREF(Py_None);
1419 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00001420}
1421
Barry Warsaw53699e91996-12-10 23:23:01 +00001422static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001423posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001424 PyObject *self;
1425 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001426{
1427 int fd, how;
1428 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001429 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00001430 return NULL;
1431#ifdef SEEK_SET
1432 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1433 switch (how) {
1434 case 0: how = SEEK_SET; break;
1435 case 1: how = SEEK_CUR; break;
1436 case 2: how = SEEK_END; break;
1437 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001438#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00001439 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001440 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00001441 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001442 if (res < 0)
1443 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001444 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00001445}
1446
Barry Warsaw53699e91996-12-10 23:23:01 +00001447static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001448posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001449 PyObject *self;
1450 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001451{
Guido van Rossum8bac5461996-06-11 18:38:48 +00001452 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001453 PyObject *buffer;
1454 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00001455 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001456 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001457 if (buffer == NULL)
1458 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001459 Py_BEGIN_ALLOW_THREADS
1460 n = read(fd, PyString_AsString(buffer), size);
1461 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00001462 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001463 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00001464 return posix_error();
1465 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00001466 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00001467 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00001468 return buffer;
1469}
1470
Barry Warsaw53699e91996-12-10 23:23:01 +00001471static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001472posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001473 PyObject *self;
1474 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001475{
1476 int fd, size;
1477 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00001478 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00001479 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001480 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001481 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00001482 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001483 if (size < 0)
1484 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001485 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001486}
1487
Barry Warsaw53699e91996-12-10 23:23:01 +00001488static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001489posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001490 PyObject *self;
1491 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001492{
1493 int fd;
1494 struct stat st;
1495 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001496 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001497 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001498 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001499 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001500 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001501 if (res != 0)
1502 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001503 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001504 (long)st.st_mode,
1505 (long)st.st_ino,
1506 (long)st.st_dev,
1507 (long)st.st_nlink,
1508 (long)st.st_uid,
1509 (long)st.st_gid,
1510 (long)st.st_size,
1511 (long)st.st_atime,
1512 (long)st.st_mtime,
1513 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00001514}
1515
Barry Warsaw53699e91996-12-10 23:23:01 +00001516static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001517posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001518 PyObject *self;
1519 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001520{
Barry Warsaw53699e91996-12-10 23:23:01 +00001521 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00001522 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001523 char *mode = "r";
1524 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00001525 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001526 PyObject *f;
1527 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00001528 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001529
Barry Warsaw53699e91996-12-10 23:23:01 +00001530 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001531 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001532 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001533 if (fp == NULL)
1534 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001535 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001536 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001537 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001538 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00001539}
1540
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001541#ifdef HAVE_PIPE
Barry Warsaw53699e91996-12-10 23:23:01 +00001542static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001543posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001544 PyObject *self;
1545 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001546{
Guido van Rossum8d665e61996-06-26 18:22:49 +00001547#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00001548 int fds[2];
1549 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001550 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00001551 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001552 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001553 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00001554 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001555 if (res != 0)
1556 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001557 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001558#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00001559 HANDLE read, write;
1560 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00001561 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00001562 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001563 Py_BEGIN_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00001564 ok = CreatePipe( &read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00001565 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00001566 if (!ok)
1567 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001568 return Py_BuildValue("(ii)", read, write);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001569#endif /* MS_WIN32 */
Guido van Rossum687dd131993-05-17 08:34:16 +00001570}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001571#endif /* HAVE_PIPE */
1572
1573#ifdef HAVE_MKFIFO
Barry Warsaw53699e91996-12-10 23:23:01 +00001574static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001575posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001576 PyObject *self;
1577 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001578{
1579 char *file;
1580 int mode = 0666;
1581 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001582 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001583 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001584 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001585 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001586 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001587 if (res < 0)
1588 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001589 Py_INCREF(Py_None);
1590 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001591}
1592#endif
1593
1594#ifdef HAVE_FTRUNCATE
Barry Warsaw53699e91996-12-10 23:23:01 +00001595static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001596posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001597 PyObject *self; /* Not used */
1598 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001599{
1600 int fd;
1601 long length;
1602 int res;
1603
Barry Warsaw53699e91996-12-10 23:23:01 +00001604 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001605 return NULL;
1606
Barry Warsaw53699e91996-12-10 23:23:01 +00001607 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001608 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00001609 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001610 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001611 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001612 return NULL;
1613 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001614 Py_INCREF(Py_None);
1615 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001616}
1617#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001618
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001619#ifdef HAVE_PUTENV
Barry Warsaw53699e91996-12-10 23:23:01 +00001620static PyObject *
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001621posix_putenv(self,args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001622 PyObject *self;
1623 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001624{
1625 char *s1, *s2;
1626 char *new;
1627
Barry Warsaw53699e91996-12-10 23:23:01 +00001628 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001629 return NULL;
1630 /* XXX This leaks memory -- not easy to fix :-( */
1631 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001632 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001633 (void) sprintf(new, "%s=%s", s1, s2);
1634 if (putenv(new)) {
1635 posix_error();
1636 return NULL;
1637 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001638 Py_INCREF(Py_None);
1639 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001640}
1641#endif
1642
Barry Warsaw53699e91996-12-10 23:23:01 +00001643static PyMethodDef posix_methods[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001644 {"chdir", posix_chdir},
1645 {"chmod", posix_chmod},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001646#ifdef HAVE_CHOWN
1647 {"chown", posix_chown},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001648#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00001649#ifdef HAVE_GETCWD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001650 {"getcwd", posix_getcwd},
Guido van Rossum36bc6801995-06-14 22:54:23 +00001651#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001652#ifdef HAVE_LINK
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001653 {"link", posix_link},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001654#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001656 {"lstat", posix_lstat},
Guido van Rossumb0824db1996-02-25 04:50:32 +00001657 {"mkdir", posix_mkdir, 1},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001658#ifdef HAVE_NICE
Guido van Rossum775f4da1993-01-09 17:18:52 +00001659 {"nice", posix_nice},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001660#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001661#ifdef HAVE_READLINK
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001662 {"readlink", posix_readlink},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001663#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001664 {"rename", posix_rename},
1665 {"rmdir", posix_rmdir},
1666 {"stat", posix_stat},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001667#ifdef HAVE_SYMLINK
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001668 {"symlink", posix_symlink},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001669#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001670#ifdef HAVE_SYSTEM
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001671 {"system", posix_system},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001672#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673 {"umask", posix_umask},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001674#ifdef HAVE_UNAME
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001675 {"uname", posix_uname},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001676#endif /* HAVE_UNAME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001677 {"unlink", posix_unlink},
Guido van Rossumf4a59121996-02-13 00:12:56 +00001678 {"remove", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001679 {"utime", posix_utime},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001680#ifdef HAVE_TIMES
Guido van Rossum22db57e1992-04-05 14:25:30 +00001681 {"times", posix_times},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001682#endif /* HAVE_TIMES */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001683 {"_exit", posix__exit},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001684#ifdef HAVE_EXECV
Guido van Rossum89b33251993-10-22 14:26:06 +00001685 {"execv", posix_execv},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001686 {"execve", posix_execve},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001687#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00001688#ifdef HAVE_FORK
Guido van Rossum85e3b011991-06-03 12:42:10 +00001689 {"fork", posix_fork},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001690#endif /* HAVE_FORK */
1691#ifdef HAVE_GETEGID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001692 {"getegid", posix_getegid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001693#endif /* HAVE_GETEGID */
1694#ifdef HAVE_GETEUID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001695 {"geteuid", posix_geteuid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001696#endif /* HAVE_GETEUID */
1697#ifdef HAVE_GETGID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001698 {"getgid", posix_getgid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001699#endif /* HAVE_GETGID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001700 {"getpid", posix_getpid},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001701#ifdef HAVE_GETPGRP
Guido van Rossum04814471991-06-04 20:23:49 +00001702 {"getpgrp", posix_getpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001703#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00001704#ifdef HAVE_GETPPID
Guido van Rossum85e3b011991-06-03 12:42:10 +00001705 {"getppid", posix_getppid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001706#endif /* HAVE_GETPPID */
1707#ifdef HAVE_GETUID
Guido van Rossum46003ff1992-05-15 11:05:24 +00001708 {"getuid", posix_getuid},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001709#endif /* HAVE_GETUID */
1710#ifdef HAVE_KILL
Guido van Rossum85e3b011991-06-03 12:42:10 +00001711 {"kill", posix_kill},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001712#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00001713#ifdef HAVE_PLOCK
1714 {"plock", posix_plock},
1715#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001716#ifdef HAVE_POPEN
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001717 {"popen", posix_popen, 1},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001718#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001719#ifdef HAVE_SETUID
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001720 {"setuid", posix_setuid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001721#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001722#ifdef HAVE_SETGID
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001723 {"setgid", posix_setgid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001724#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001725#ifdef HAVE_SETPGRP
Guido van Rossumc2670a01992-09-13 20:07:29 +00001726 {"setpgrp", posix_setpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001727#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00001728#ifdef HAVE_WAIT
Guido van Rossum85e3b011991-06-03 12:42:10 +00001729 {"wait", posix_wait},
Guido van Rossumad0ee831995-03-01 10:34:45 +00001730#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001731#ifdef HAVE_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +00001732 {"waitpid", posix_waitpid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001733#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001734#ifdef HAVE_SETSID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001735 {"setsid", posix_setsid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001736#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001737#ifdef HAVE_SETPGID
Guido van Rossumc2670a01992-09-13 20:07:29 +00001738 {"setpgid", posix_setpgid},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001739#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001740#ifdef HAVE_TCGETPGRP
Guido van Rossum7066dd71992-09-17 17:54:56 +00001741 {"tcgetpgrp", posix_tcgetpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001742#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001743#ifdef HAVE_TCSETPGRP
Guido van Rossum7066dd71992-09-17 17:54:56 +00001744 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001745#endif /* HAVE_TCSETPGRP */
Barry Warsaw43d68b81996-12-19 22:10:44 +00001746 {"open", posix_open, 1},
Guido van Rossum687dd131993-05-17 08:34:16 +00001747 {"close", posix_close},
1748 {"dup", posix_dup},
1749 {"dup2", posix_dup2},
1750 {"lseek", posix_lseek},
1751 {"read", posix_read},
1752 {"write", posix_write},
1753 {"fstat", posix_fstat},
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001754 {"fdopen", posix_fdopen, 1},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001755#ifdef HAVE_PIPE
Guido van Rossum687dd131993-05-17 08:34:16 +00001756 {"pipe", posix_pipe},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001757#endif
1758#ifdef HAVE_MKFIFO
1759 {"mkfifo", posix_mkfifo, 1},
1760#endif
1761#ifdef HAVE_FTRUNCATE
1762 {"ftruncate", posix_ftruncate, 1},
1763#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001764#ifdef HAVE_PUTENV
1765 {"putenv", posix_putenv, 1},
1766#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001767 {NULL, NULL} /* Sentinel */
1768};
1769
1770
Barry Warsaw4a342091996-12-19 23:50:02 +00001771static int
1772ins(d, symbol, value)
1773 PyObject* d;
1774 char* symbol;
1775 long value;
1776{
1777 PyObject* v = PyInt_FromLong(value);
1778 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
1779 return -1; /* triggers fatal error */
1780
1781 Py_DECREF(v);
1782 return 0;
1783}
1784
1785static int
1786all_ins(d)
1787 PyObject* d;
1788{
1789#ifdef WNOHANG
1790 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
1791#endif
1792#ifdef O_RDONLY
1793 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
1794#endif
1795#ifdef O_WRONLY
1796 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
1797#endif
1798#ifdef O_RDWR
1799 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
1800#endif
1801#ifdef O_NDELAY
1802 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
1803#endif
1804#ifdef O_NONBLOCK
1805 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
1806#endif
1807#ifdef O_APPEND
1808 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
1809#endif
1810#ifdef O_DSYNC
1811 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
1812#endif
1813#ifdef O_RSYNC
1814 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
1815#endif
1816#ifdef O_SYNC
1817 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
1818#endif
1819#ifdef O_NOCTTY
1820 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
1821#endif
1822#ifdef O_CREAT
1823 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
1824#endif
1825#ifdef O_EXCL
1826 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
1827#endif
1828#ifdef O_TRUNC
1829 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
1830#endif
1831 return 0;
1832}
1833
1834
1835
1836
Guido van Rossuma0e71301996-05-28 22:30:38 +00001837#if defined(_MSC_VER) || defined(__WATCOMC__)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001838void
1839initnt()
1840{
Barry Warsaw53699e91996-12-10 23:23:01 +00001841 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843 m = Py_InitModule("nt", posix_methods);
1844 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001845
1846 /* Initialize nt.environ dictionary */
1847 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00001848 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Barry Warsaw4a342091996-12-19 23:50:02 +00001849 goto finally;
Barry Warsaw53699e91996-12-10 23:23:01 +00001850 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001851
Barry Warsaw4a342091996-12-19 23:50:02 +00001852 if (all_ins(d))
1853 goto finally;
1854
Guido van Rossumb6775db1994-08-01 11:34:53 +00001855 /* Initialize nt.error exception */
Guido van Rossumba9d7c51997-04-29 15:49:54 +00001856 PosixError = PyString_FromString("os.error");
Barry Warsaw4a342091996-12-19 23:50:02 +00001857 PyDict_SetItemString(d, "error", PosixError);
1858
1859 if (!PyErr_Occurred())
1860 return;
1861
1862 finally:
1863 Py_FatalError("can't initialize NT posixmodule");
Guido van Rossumb6775db1994-08-01 11:34:53 +00001864}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001865#else /* not a PC port */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866void
1867initposix()
1868{
Barry Warsaw53699e91996-12-10 23:23:01 +00001869 PyObject *m, *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001870
Barry Warsaw53699e91996-12-10 23:23:01 +00001871 m = Py_InitModule("posix", posix_methods);
1872 d = PyModule_GetDict(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873
1874 /* Initialize posix.environ dictionary */
1875 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00001876 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Barry Warsaw4a342091996-12-19 23:50:02 +00001877 goto finally;
Barry Warsaw53699e91996-12-10 23:23:01 +00001878 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001879
Barry Warsaw4a342091996-12-19 23:50:02 +00001880 if (all_ins(d))
1881 goto finally;
1882
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001883 /* Initialize posix.error exception */
Guido van Rossumba9d7c51997-04-29 15:49:54 +00001884 PosixError = PyString_FromString("os.error");
Barry Warsaw4a342091996-12-19 23:50:02 +00001885 PyDict_SetItemString(d, "error", PosixError);
1886
1887 if (!PyErr_Occurred())
1888 return;
1889
1890 finally:
1891 Py_FatalError("can't initialize posix module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001892}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001893#endif /* !_MSC_VER */