blob: 474e00e888b071308df94c2d27de0827bbbceb16 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000016#include "Python.h"
17#include "structseq.h"
18
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000019#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000020# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000021#endif /* defined(__VMS) */
22
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000023PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000024"This module provides access to operating system functionality that is\n\
25standardized by the C Standard and the POSIX standard (a thinly\n\
26disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000029#ifndef Py_USING_UNICODE
30/* This is used in signatures of functions. */
31#define Py_UNICODE void
32#endif
33
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000034#if defined(PYOS_OS2)
35#define INCL_DOS
36#define INCL_DOSERRORS
37#define INCL_DOSPROCESS
38#define INCL_NOPMAPI
39#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000040#if defined(PYCC_GCC)
41#include <ctype.h>
42#include <io.h>
43#include <stdio.h>
44#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000045#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000046#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#endif
48
Guido van Rossumb6775db1994-08-01 11:34:53 +000049#include <sys/types.h>
50#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000051
Guido van Rossum36bc6801995-06-14 22:54:23 +000052#ifdef HAVE_SYS_WAIT_H
53#include <sys/wait.h> /* For WNOHANG */
54#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000055
Guido van Rossuma376cc51996-12-05 23:43:35 +000056#include <signal.h>
Guido van Rossuma376cc51996-12-05 23:43:35 +000057
Guido van Rossumb6775db1994-08-01 11:34:53 +000058#ifdef HAVE_FCNTL_H
59#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000060#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000061
Guido van Rossuma6535fd2001-10-18 19:44:10 +000062#ifdef HAVE_GRP_H
63#include <grp.h>
64#endif
65
Barry Warsaw5676bd12003-01-07 20:57:09 +000066#ifdef HAVE_SYSEXITS_H
67#include <sysexits.h>
68#endif /* HAVE_SYSEXITS_H */
69
Guido van Rossuma4916fa1996-05-23 22:58:55 +000070/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000071/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000072#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000073#include <process.h>
74#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000075#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000076#define HAVE_GETCWD 1
77#define HAVE_OPENDIR 1
78#define HAVE_SYSTEM 1
79#if defined(__OS2__)
80#define HAVE_EXECV 1
81#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000082#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000083#include <process.h>
84#else
85#ifdef __BORLANDC__ /* Borland compiler */
86#define HAVE_EXECV 1
87#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000088#define HAVE_OPENDIR 1
89#define HAVE_PIPE 1
90#define HAVE_POPEN 1
91#define HAVE_SYSTEM 1
92#define HAVE_WAIT 1
93#else
94#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000095#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +000096#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000097#define HAVE_EXECV 1
98#define HAVE_PIPE 1
99#define HAVE_POPEN 1
100#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000101#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000102#define HAVE_FSYNC 1
103#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000104#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000105#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
106/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#else /* all other compilers */
108/* Unix functions that the configure script doesn't check for */
109#define HAVE_EXECV 1
110#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000111#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
112#define HAVE_FORK1 1
113#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_GETCWD 1
115#define HAVE_GETEGID 1
116#define HAVE_GETEUID 1
117#define HAVE_GETGID 1
118#define HAVE_GETPPID 1
119#define HAVE_GETUID 1
120#define HAVE_KILL 1
121#define HAVE_OPENDIR 1
122#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000123#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000125#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_SYSTEM 1
127#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000128#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000129#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#endif /* _MSC_VER */
131#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000132#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000133#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000134
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000136
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000137#if defined(__sgi)&&_COMPILER_VERSION>=700
138/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
139 (default) */
140extern char *ctermid_r(char *);
141#endif
142
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000143#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000144#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000145extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000146#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000147#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000148extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000150extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000152#endif
153#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000154extern int chdir(char *);
155extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int chdir(const char *);
158extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000159#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000160#ifdef __BORLANDC__
161extern int chmod(const char *, int);
162#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000163extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000164#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000165extern int chown(const char *, uid_t, gid_t);
166extern char *getcwd(char *, int);
167extern char *strerror(int);
168extern int link(const char *, const char *);
169extern int rename(const char *, const char *);
170extern int stat(const char *, struct stat *);
171extern int unlink(const char *);
172extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000175#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000178#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000180
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#ifdef HAVE_UTIME_H
184#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000185#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000187#ifdef HAVE_SYS_UTIME_H
188#include <sys/utime.h>
189#define HAVE_UTIME_H /* pretend we do for the rest of this file */
190#endif /* HAVE_SYS_UTIME_H */
191
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#ifdef HAVE_SYS_TIMES_H
193#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000194#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195
196#ifdef HAVE_SYS_PARAM_H
197#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000198#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199
200#ifdef HAVE_SYS_UTSNAME_H
201#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000202#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000204#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#define NAMLEN(dirent) strlen((dirent)->d_name)
207#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000208#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#include <direct.h>
210#define NAMLEN(dirent) strlen((dirent)->d_name)
211#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000213#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000214#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000215#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000217#endif
218#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#endif
221#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000223#endif
224#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#include <direct.h>
228#include <io.h>
229#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000230#include "osdefs.h"
Tim Petersee66d0c2002-07-15 16:10:55 +0000231#define WIN32_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000233#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000235#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossumd48f2521997-12-05 22:19:34 +0000238#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000240#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241
Tim Petersbc2e10e2002-03-03 23:17:02 +0000242#ifndef MAXPATHLEN
243#define MAXPATHLEN 1024
244#endif /* MAXPATHLEN */
245
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000246#ifdef UNION_WAIT
247/* Emulate some macros on systems that have a union instead of macros */
248
249#ifndef WIFEXITED
250#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
251#endif
252
253#ifndef WEXITSTATUS
254#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
255#endif
256
257#ifndef WTERMSIG
258#define WTERMSIG(u_wait) ((u_wait).w_termsig)
259#endif
260
261#endif /* UNION_WAIT */
262
Greg Wardb48bc172000-03-01 21:51:56 +0000263/* Don't use the "_r" form if we don't need it (also, won't have a
264 prototype for it, at least on Solaris -- maybe others as well?). */
265#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
266#define USE_CTERMID_R
267#endif
268
269#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
270#define USE_TMPNAM_R
271#endif
272
Fred Drake699f3522000-06-29 21:12:41 +0000273/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000274#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000275#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000276# define STAT _stati64
277# define FSTAT _fstati64
278# define STRUCT_STAT struct _stati64
279#else
280# define STAT stat
281# define FSTAT fstat
282# define STRUCT_STAT struct stat
283#endif
284
Tim Peters11b23062003-04-23 02:39:17 +0000285#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000286#include <sys/mkdev.h>
287#else
288#if defined(MAJOR_IN_SYSMACROS)
289#include <sys/sysmacros.h>
290#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000291#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
292#include <sys/mkdev.h>
293#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000294#endif
Fred Drake699f3522000-06-29 21:12:41 +0000295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000297#ifdef WITH_NEXT_FRAMEWORK
298/* On Darwin/MacOSX a shared library or framework has no access to
299** environ directly, we must obtain it with _NSGetEnviron().
300*/
301#include <crt_externs.h>
302static char **environ;
303#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000305#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306
Barry Warsaw53699e91996-12-10 23:23:01 +0000307static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000308convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309{
Barry Warsaw53699e91996-12-10 23:23:01 +0000310 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000312 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 if (d == NULL)
314 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000315#ifdef WITH_NEXT_FRAMEWORK
316 if (environ == NULL)
317 environ = *_NSGetEnviron();
318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319 if (environ == NULL)
320 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000321 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000323 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 char *p = strchr(*e, '=');
326 if (p == NULL)
327 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000328 k = PyString_FromStringAndSize(*e, (int)(p-*e));
329 if (k == NULL) {
330 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000332 }
333 v = PyString_FromString(p+1);
334 if (v == NULL) {
335 PyErr_Clear();
336 Py_DECREF(k);
337 continue;
338 }
339 if (PyDict_GetItem(d, k) == NULL) {
340 if (PyDict_SetItem(d, k, v) != 0)
341 PyErr_Clear();
342 }
343 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000344 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000346#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000347 {
348 APIRET rc;
349 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
350
351 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000352 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000353 PyObject *v = PyString_FromString(buffer);
354 PyDict_SetItemString(d, "BEGINLIBPATH", v);
355 Py_DECREF(v);
356 }
357 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
358 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
359 PyObject *v = PyString_FromString(buffer);
360 PyDict_SetItemString(d, "ENDLIBPATH", v);
361 Py_DECREF(v);
362 }
363 }
364#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 return d;
366}
367
368
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369/* Set a POSIX-specific error from errno, and return NULL */
370
Barry Warsawd58d7641998-07-23 16:14:40 +0000371static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000372posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000373{
Barry Warsawca74da41999-02-09 19:31:45 +0000374 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
Barry Warsawd58d7641998-07-23 16:14:40 +0000376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000377posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000378{
Barry Warsawca74da41999-02-09 19:31:45 +0000379 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000380}
381
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000382#ifdef Py_WIN_WIDE_FILENAMES
383static PyObject *
384posix_error_with_unicode_filename(Py_UNICODE* name)
385{
386 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
387}
388#endif /* Py_WIN_WIDE_FILENAMES */
389
390
Mark Hammondef8b6542001-05-13 08:04:26 +0000391static PyObject *
392posix_error_with_allocated_filename(char* name)
393{
394 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
395 PyMem_Free(name);
396 return rc;
397}
398
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000399#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000400static PyObject *
401win32_error(char* function, char* filename)
402{
Mark Hammond33a6da92000-08-15 00:46:38 +0000403 /* XXX We should pass the function name along in the future.
404 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000405 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000406 Windows error object, which is non-trivial.
407 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000408 errno = GetLastError();
409 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000410 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000411 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000412 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000413}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000414
415#ifdef Py_WIN_WIDE_FILENAMES
416static PyObject *
417win32_error_unicode(char* function, Py_UNICODE* filename)
418{
419 /* XXX - see win32_error for comments on 'function' */
420 errno = GetLastError();
421 if (filename)
422 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
423 else
424 return PyErr_SetFromWindowsErr(errno);
425}
426
427static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
428{
429 /* XXX Perhaps we should make this API an alias of
430 PyObject_Unicode() instead ?! */
431 if (PyUnicode_CheckExact(obj)) {
432 Py_INCREF(obj);
433 return obj;
434 }
435 if (PyUnicode_Check(obj)) {
436 /* For a Unicode subtype that's not a Unicode object,
437 return a true Unicode object with the same data. */
438 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
439 PyUnicode_GET_SIZE(obj));
440 }
Tim Peters11b23062003-04-23 02:39:17 +0000441 return PyUnicode_FromEncodedObject(obj,
442 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000443 "strict");
444}
445
446#endif /* Py_WIN_WIDE_FILENAMES */
447
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000448#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449
Guido van Rossumd48f2521997-12-05 22:19:34 +0000450#if defined(PYOS_OS2)
451/**********************************************************************
452 * Helper Function to Trim and Format OS/2 Messages
453 **********************************************************************/
454 static void
455os2_formatmsg(char *msgbuf, int msglen, char *reason)
456{
457 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
458
459 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
460 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
461
462 while (lastc > msgbuf && isspace(*lastc))
463 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
464 }
465
466 /* Add Optional Reason Text */
467 if (reason) {
468 strcat(msgbuf, " : ");
469 strcat(msgbuf, reason);
470 }
471}
472
473/**********************************************************************
474 * Decode an OS/2 Operating System Error Code
475 *
476 * A convenience function to lookup an OS/2 error code and return a
477 * text message we can use to raise a Python exception.
478 *
479 * Notes:
480 * The messages for errors returned from the OS/2 kernel reside in
481 * the file OSO001.MSG in the \OS2 directory hierarchy.
482 *
483 **********************************************************************/
484 static char *
485os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
486{
487 APIRET rc;
488 ULONG msglen;
489
490 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
491 Py_BEGIN_ALLOW_THREADS
492 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
493 errorcode, "oso001.msg", &msglen);
494 Py_END_ALLOW_THREADS
495
496 if (rc == NO_ERROR)
497 os2_formatmsg(msgbuf, msglen, reason);
498 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000499 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000500 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000501
502 return msgbuf;
503}
504
505/* Set an OS/2-specific error and return NULL. OS/2 kernel
506 errors are not in a global variable e.g. 'errno' nor are
507 they congruent with posix error numbers. */
508
509static PyObject * os2_error(int code)
510{
511 char text[1024];
512 PyObject *v;
513
514 os2_strerror(text, sizeof(text), code, "");
515
516 v = Py_BuildValue("(is)", code, text);
517 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000518 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000519 Py_DECREF(v);
520 }
521 return NULL; /* Signal to Python that an Exception is Pending */
522}
523
524#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525
526/* POSIX generic methods */
527
Barry Warsaw53699e91996-12-10 23:23:01 +0000528static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000529posix_fildes(PyObject *fdobj, int (*func)(int))
530{
531 int fd;
532 int res;
533 fd = PyObject_AsFileDescriptor(fdobj);
534 if (fd < 0)
535 return NULL;
536 Py_BEGIN_ALLOW_THREADS
537 res = (*func)(fd);
538 Py_END_ALLOW_THREADS
539 if (res < 0)
540 return posix_error();
541 Py_INCREF(Py_None);
542 return Py_None;
543}
Guido van Rossum21142a01999-01-08 21:05:37 +0000544
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000545#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000546static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000547unicode_file_names(void)
548{
549 static int canusewide = -1;
550 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000551 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000552 the Windows NT family. */
553 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
554 }
555 return canusewide;
556}
557#endif
Tim Peters11b23062003-04-23 02:39:17 +0000558
Guido van Rossum21142a01999-01-08 21:05:37 +0000559static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000560posix_1str(PyObject *args, char *format, int (*func)(const char*),
561 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
Mark Hammondef8b6542001-05-13 08:04:26 +0000563 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000564 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000565#ifdef Py_WIN_WIDE_FILENAMES
566 if (unicode_file_names()) {
567 PyUnicodeObject *po;
568 if (PyArg_ParseTuple(args, wformat, &po)) {
569 Py_BEGIN_ALLOW_THREADS
570 /* PyUnicode_AS_UNICODE OK without thread
571 lock as it is a simple dereference. */
572 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
573 Py_END_ALLOW_THREADS
574 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000575 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576 Py_INCREF(Py_None);
577 return Py_None;
578 }
579 /* Drop the argument parsing error as narrow
580 strings are also valid. */
581 PyErr_Clear();
582 }
583#else
584 /* Platforms that don't support Unicode filenames
585 shouldn't be passing these extra params */
586 assert(wformat==NULL && wfunc == NULL);
587#endif
588
Tim Peters5aa91602002-01-30 05:46:57 +0000589 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000590 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000592 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000593 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000594 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000595 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000596 return posix_error_with_allocated_filename(path1);
597 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000598 Py_INCREF(Py_None);
599 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600}
601
Barry Warsaw53699e91996-12-10 23:23:01 +0000602static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000603posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604 char *format,
605 int (*func)(const char *, const char *),
606 char *wformat,
607 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608{
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000610 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000611#ifdef Py_WIN_WIDE_FILENAMES
612 if (unicode_file_names()) {
613 PyObject *po1;
614 PyObject *po2;
615 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
616 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
617 PyObject *wpath1;
618 PyObject *wpath2;
619 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
620 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
621 if (!wpath1 || !wpath2) {
622 Py_XDECREF(wpath1);
623 Py_XDECREF(wpath2);
624 return NULL;
625 }
626 Py_BEGIN_ALLOW_THREADS
627 /* PyUnicode_AS_UNICODE OK without thread
628 lock as it is a simple dereference. */
629 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
630 PyUnicode_AS_UNICODE(wpath2));
631 Py_END_ALLOW_THREADS
632 Py_XDECREF(wpath1);
633 Py_XDECREF(wpath2);
634 if (res != 0)
635 return posix_error();
636 Py_INCREF(Py_None);
637 return Py_None;
638 }
639 /* Else flow through as neither is Unicode. */
640 }
641 /* Drop the argument parsing error as narrow
642 strings are also valid. */
643 PyErr_Clear();
644 }
645#else
646 /* Platforms that don't support Unicode filenames
647 shouldn't be passing these extra params */
648 assert(wformat==NULL && wfunc == NULL);
649#endif
650
Mark Hammondef8b6542001-05-13 08:04:26 +0000651 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000652 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000653 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000655 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000656 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000657 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000658 PyMem_Free(path1);
659 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000660 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000661 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 Py_INCREF(Py_None);
664 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665}
666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000667PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000668"stat_result: Result from stat or lstat.\n\n\
669This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000670 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000671or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
672\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000673Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000674they are available as attributes only.\n\
675\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000677
678static PyStructSequence_Field stat_result_fields[] = {
679 {"st_mode", "protection bits"},
680 {"st_ino", "inode"},
681 {"st_dev", "device"},
682 {"st_nlink", "number of hard links"},
683 {"st_uid", "user ID of owner"},
684 {"st_gid", "group ID of owner"},
685 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000686 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
687 {NULL, "integer time of last access"},
688 {NULL, "integer time of last modification"},
689 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000690 {"st_atime", "time of last access"},
691 {"st_mtime", "time of last modification"},
692 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000693#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000694 {"st_blksize", "blocksize for filesystem I/O"},
695#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000696#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000697 {"st_blocks", "number of blocks allocated"},
698#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000699#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000700 {"st_rdev", "device type (if inode device)"},
701#endif
702 {0}
703};
704
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000705#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000706#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000707#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000708#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000709#endif
710
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
713#else
714#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
715#endif
716
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000717#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000718#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
719#else
720#define ST_RDEV_IDX ST_BLOCKS_IDX
721#endif
722
723static PyStructSequence_Desc stat_result_desc = {
724 "stat_result", /* name */
725 stat_result__doc__, /* doc */
726 stat_result_fields,
727 10
728};
729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000731"statvfs_result: Result from statvfs or fstatvfs.\n\n\
732This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000733 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000734or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000735\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000736See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000737
738static PyStructSequence_Field statvfs_result_fields[] = {
739 {"f_bsize", },
740 {"f_frsize", },
741 {"f_blocks", },
742 {"f_bfree", },
743 {"f_bavail", },
744 {"f_files", },
745 {"f_ffree", },
746 {"f_favail", },
747 {"f_flag", },
748 {"f_namemax",},
749 {0}
750};
751
752static PyStructSequence_Desc statvfs_result_desc = {
753 "statvfs_result", /* name */
754 statvfs_result__doc__, /* doc */
755 statvfs_result_fields,
756 10
757};
758
759static PyTypeObject StatResultType;
760static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000761static newfunc structseq_new;
762
763static PyObject *
764statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
765{
766 PyStructSequence *result;
767 int i;
768
769 result = (PyStructSequence*)structseq_new(type, args, kwds);
770 if (!result)
771 return NULL;
772 /* If we have been initialized from a tuple,
773 st_?time might be set to None. Initialize it
774 from the int slots. */
775 for (i = 7; i <= 9; i++) {
776 if (result->ob_item[i+3] == Py_None) {
777 Py_DECREF(Py_None);
778 Py_INCREF(result->ob_item[i]);
779 result->ob_item[i+3] = result->ob_item[i];
780 }
781 }
782 return (PyObject*)result;
783}
784
785
786
787/* If true, st_?time is float. */
788static int _stat_float_times = 0;
789
790PyDoc_STRVAR(stat_float_times__doc__,
791"stat_float_times([newval]) -> oldval\n\n\
792Determine whether os.[lf]stat represents time stamps as float objects.\n\
793If newval is True, future calls to stat() return floats, if it is False,\n\
794future calls return ints. \n\
795If newval is omitted, return the current setting.\n");
796
797static PyObject*
798stat_float_times(PyObject* self, PyObject *args)
799{
800 int newval = -1;
801 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
802 return NULL;
803 if (newval == -1)
804 /* Return old value */
805 return PyBool_FromLong(_stat_float_times);
806 _stat_float_times = newval;
807 Py_INCREF(Py_None);
808 return Py_None;
809}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000810
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000811static void
812fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
813{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000814 PyObject *fval,*ival;
815#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000816 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000817#else
818 ival = PyInt_FromLong((long)sec);
819#endif
820 if (_stat_float_times) {
821 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
822 } else {
823 fval = ival;
824 Py_INCREF(fval);
825 }
826 PyStructSequence_SET_ITEM(v, index, ival);
827 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000828}
829
Tim Peters5aa91602002-01-30 05:46:57 +0000830/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000831 (used by posix_stat() and posix_fstat()) */
832static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000833_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000834{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000835 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000836 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000837 if (v == NULL)
838 return NULL;
839
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000840 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000841#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000842 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000843 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000844#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000845 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000846#endif
847#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000848 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000849 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000850#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000851 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000852#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000853 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
854 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
855 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000856#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000857 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000858 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000859#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000860 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000861#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000862
863#ifdef HAVE_STAT_TV_NSEC
864 ansec = st.st_atim.tv_nsec;
865 mnsec = st.st_mtim.tv_nsec;
866 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000867#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000868 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000869#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000870 fill_time(v, 7, st.st_atime, ansec);
871 fill_time(v, 8, st.st_mtime, mnsec);
872 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000873
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000874#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000875 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000876 PyInt_FromLong((long)st.st_blksize));
877#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000878#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000879 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000880 PyInt_FromLong((long)st.st_blocks));
881#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000882#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000883 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
884 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000885#endif
886
887 if (PyErr_Occurred()) {
888 Py_DECREF(v);
889 return NULL;
890 }
891
892 return v;
893}
894
Martin v. Löwisd8948722004-06-02 09:57:56 +0000895#ifdef MS_WINDOWS
896
897/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
898 where / can be used in place of \ and the trailing slash is optional.
899 Both SERVER and SHARE must have at least one character.
900*/
901
902#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
903#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
904#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
905
906static BOOL
907IsUNCRootA(char *path, int pathlen)
908{
909 #define ISSLASH ISSLASHA
910
911 int i, share;
912
913 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
914 /* minimum UNCRoot is \\x\y */
915 return FALSE;
916 for (i = 2; i < pathlen ; i++)
917 if (ISSLASH(path[i])) break;
918 if (i == 2 || i == pathlen)
919 /* do not allow \\\SHARE or \\SERVER */
920 return FALSE;
921 share = i+1;
922 for (i = share; i < pathlen; i++)
923 if (ISSLASH(path[i])) break;
924 return (i != share && (i == pathlen || i == pathlen-1));
925
926 #undef ISSLASH
927}
928
929#ifdef Py_WIN_WIDE_FILENAMES
930static BOOL
931IsUNCRootW(Py_UNICODE *path, int pathlen)
932{
933 #define ISSLASH ISSLASHW
934
935 int i, share;
936
937 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
938 /* minimum UNCRoot is \\x\y */
939 return FALSE;
940 for (i = 2; i < pathlen ; i++)
941 if (ISSLASH(path[i])) break;
942 if (i == 2 || i == pathlen)
943 /* do not allow \\\SHARE or \\SERVER */
944 return FALSE;
945 share = i+1;
946 for (i = share; i < pathlen; i++)
947 if (ISSLASH(path[i])) break;
948 return (i != share && (i == pathlen || i == pathlen-1));
949
950 #undef ISSLASH
951}
952#endif /* Py_WIN_WIDE_FILENAMES */
953#endif /* MS_WINDOWS */
954
Barry Warsaw53699e91996-12-10 23:23:01 +0000955static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000956posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000957 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000958#ifdef __VMS
959 int (*statfunc)(const char *, STRUCT_STAT *, ...),
960#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000961 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000962#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000963 char *wformat,
964 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965{
Fred Drake699f3522000-06-29 21:12:41 +0000966 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000967 char *path = NULL; /* pass this to stat; do not free() it */
968 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000969 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000970
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000971#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000972 int pathlen;
973 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000974#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000975
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000976
977#ifdef Py_WIN_WIDE_FILENAMES
978 /* If on wide-character-capable OS see if argument
979 is Unicode and if so use wide API. */
980 if (unicode_file_names()) {
981 PyUnicodeObject *po;
982 if (PyArg_ParseTuple(args, wformat, &po)) {
983 Py_UNICODE wpath[MAX_PATH+1];
984 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
985 /* the library call can blow up if the file name is too long! */
986 if (pathlen > MAX_PATH) {
987 errno = ENAMETOOLONG;
988 return posix_error();
989 }
990 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
991 /* Remove trailing slash or backslash, unless it's the current
992 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
993 */
Martin v. Löwisd8948722004-06-02 09:57:56 +0000994 if (pathlen > 0) {
995 if (ISSLASHW(wpath[pathlen-1])) {
996 /* It does end with a slash -- exempt the root drive cases. */
997 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') ||
998 IsUNCRootW(wpath, pathlen))
999 /* leave it alone */;
1000 else {
1001 /* nuke the trailing backslash */
1002 wpath[pathlen-1] = L'\0';
1003 }
1004 }
1005 else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 &&
1006 IsUNCRootW(wpath, pathlen)) {
1007 /* UNC root w/o trailing slash: add one when there's room */
1008 wpath[pathlen++] = L'\\';
1009 wpath[pathlen] = L'\0';
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001010 }
1011 }
1012 Py_BEGIN_ALLOW_THREADS
1013 /* PyUnicode_AS_UNICODE result OK without
1014 thread lock as it is a simple dereference. */
1015 res = wstatfunc(wpath, &st);
1016 Py_END_ALLOW_THREADS
1017 if (res != 0)
1018 return posix_error_with_unicode_filename(wpath);
1019 return _pystat_fromstructstat(st);
1020 }
1021 /* Drop the argument parsing error as narrow strings
1022 are also valid. */
1023 PyErr_Clear();
1024 }
1025#endif
1026
Tim Peters5aa91602002-01-30 05:46:57 +00001027 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001028 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001030 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001031
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001032#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +00001033 pathlen = strlen(path);
1034 /* the library call can blow up if the file name is too long! */
1035 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +00001036 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +00001037 errno = ENAMETOOLONG;
1038 return posix_error();
1039 }
1040
Tim Peters500bd032001-12-19 19:05:01 +00001041 /* Remove trailing slash or backslash, unless it's the current
1042 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
1043 */
Martin v. Löwisd8948722004-06-02 09:57:56 +00001044 if (pathlen > 0) {
1045 if (ISSLASHA(path[pathlen-1])) {
1046 /* It does end with a slash -- exempt the root drive cases. */
1047 if (pathlen == 1 || (pathlen == 3 && path[1] == ':') ||
1048 IsUNCRootA(path, pathlen))
1049 /* leave it alone */;
1050 else {
1051 /* nuke the trailing backslash */
1052 strncpy(pathcopy, path, pathlen);
1053 pathcopy[pathlen-1] = '\0';
1054 path = pathcopy;
1055 }
1056 }
1057 else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 &&
1058 IsUNCRootA(path, pathlen)) {
1059 /* UNC root w/o trailing slash: add one when there's room */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001060 strncpy(pathcopy, path, pathlen);
Martin v. Löwisd8948722004-06-02 09:57:56 +00001061 pathcopy[pathlen++] = '\\';
1062 pathcopy[pathlen] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +00001063 path = pathcopy;
1064 }
1065 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001066#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001067
Barry Warsaw53699e91996-12-10 23:23:01 +00001068 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001069 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001071 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +00001072 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001073
Tim Peters500bd032001-12-19 19:05:01 +00001074 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001075 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076}
1077
1078
1079/* POSIX methods */
1080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001081PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001082"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001083Use the real uid/gid to test for access to a path. Note that most\n\
1084operations will use the effective uid/gid, therefore this routine can\n\
1085be used in a suid/sgid environment to test if the invoking user has the\n\
1086specified access to the path. The mode argument can be F_OK to test\n\
1087existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001088
1089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001090posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001091{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001092 char *path;
1093 int mode;
1094 int res;
1095
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001096#ifdef Py_WIN_WIDE_FILENAMES
1097 if (unicode_file_names()) {
1098 PyUnicodeObject *po;
1099 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1100 Py_BEGIN_ALLOW_THREADS
1101 /* PyUnicode_AS_UNICODE OK without thread lock as
1102 it is a simple dereference. */
1103 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1104 Py_END_ALLOW_THREADS
1105 return(PyBool_FromLong(res == 0));
1106 }
1107 /* Drop the argument parsing error as narrow strings
1108 are also valid. */
1109 PyErr_Clear();
1110 }
1111#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001112 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001113 return NULL;
1114 Py_BEGIN_ALLOW_THREADS
1115 res = access(path, mode);
1116 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001117 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001118}
1119
Guido van Rossumd371ff11999-01-25 16:12:23 +00001120#ifndef F_OK
1121#define F_OK 0
1122#endif
1123#ifndef R_OK
1124#define R_OK 4
1125#endif
1126#ifndef W_OK
1127#define W_OK 2
1128#endif
1129#ifndef X_OK
1130#define X_OK 1
1131#endif
1132
1133#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001134PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001135"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001137
1138static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001139posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001140{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001141 int id;
1142 char *ret;
1143
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001144 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001145 return NULL;
1146
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001147#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001148 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001149 if (id == 0) {
1150 ret = ttyname();
1151 }
1152 else {
1153 ret = NULL;
1154 }
1155#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001156 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001157#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001158 if (ret == NULL)
1159 return(posix_error());
1160 return(PyString_FromString(ret));
1161}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001162#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001163
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001164#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001166"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001167Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001168
1169static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001170posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001171{
1172 char *ret;
1173 char buffer[L_ctermid];
1174
Greg Wardb48bc172000-03-01 21:51:56 +00001175#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001176 ret = ctermid_r(buffer);
1177#else
1178 ret = ctermid(buffer);
1179#endif
1180 if (ret == NULL)
1181 return(posix_error());
1182 return(PyString_FromString(buffer));
1183}
1184#endif
1185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001187"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001189
Barry Warsaw53699e91996-12-10 23:23:01 +00001190static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001191posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001192{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001193#ifdef MS_WINDOWS
1194 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1195#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1196 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001197#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001198 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001199 NULL, NULL);
1200#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001201 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001202#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001203}
1204
Fred Drake4d1e64b2002-04-15 19:40:07 +00001205#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001206PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001207"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001208Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001209opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001210
1211static PyObject *
1212posix_fchdir(PyObject *self, PyObject *fdobj)
1213{
1214 return posix_fildes(fdobj, fchdir);
1215}
1216#endif /* HAVE_FCHDIR */
1217
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001219PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001220"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001221Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001222
Barry Warsaw53699e91996-12-10 23:23:01 +00001223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001224posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001225{
Mark Hammondef8b6542001-05-13 08:04:26 +00001226 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001227 int i;
1228 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001229#ifdef Py_WIN_WIDE_FILENAMES
1230 if (unicode_file_names()) {
1231 PyUnicodeObject *po;
1232 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1233 Py_BEGIN_ALLOW_THREADS
1234 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1235 Py_END_ALLOW_THREADS
1236 if (res < 0)
1237 return posix_error_with_unicode_filename(
1238 PyUnicode_AS_UNICODE(po));
1239 Py_INCREF(Py_None);
1240 return Py_None;
1241 }
1242 /* Drop the argument parsing error as narrow strings
1243 are also valid. */
1244 PyErr_Clear();
1245 }
1246#endif /* Py_WIN_WIDE_FILENAMES */
1247 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001248 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001249 return NULL;
1250 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001251 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001252 Py_END_ALLOW_THREADS
1253 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001254 return posix_error_with_allocated_filename(path);
1255 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001256 Py_INCREF(Py_None);
1257 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001258}
1259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001260
Martin v. Löwis244edc82001-10-04 22:44:26 +00001261#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001262PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001263"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001264Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001265
1266static PyObject *
1267posix_chroot(PyObject *self, PyObject *args)
1268{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001269 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001270}
1271#endif
1272
Guido van Rossum21142a01999-01-08 21:05:37 +00001273#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001274PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001275"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001276force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001277
1278static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001279posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001280{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001281 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001282}
1283#endif /* HAVE_FSYNC */
1284
1285#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001286
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001287#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001288extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1289#endif
1290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001291PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001292"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001293force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001294 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001295
1296static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001297posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001298{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001299 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001300}
1301#endif /* HAVE_FDATASYNC */
1302
1303
Fredrik Lundh10723342000-07-10 16:38:09 +00001304#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001306"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001307Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001308
Barry Warsaw53699e91996-12-10 23:23:01 +00001309static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001310posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001311{
Mark Hammondef8b6542001-05-13 08:04:26 +00001312 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001313 int uid, gid;
1314 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001315 if (!PyArg_ParseTuple(args, "etii:chown",
1316 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001317 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001318 return NULL;
1319 Py_BEGIN_ALLOW_THREADS
1320 res = chown(path, (uid_t) uid, (gid_t) gid);
1321 Py_END_ALLOW_THREADS
1322 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001323 return posix_error_with_allocated_filename(path);
1324 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001325 Py_INCREF(Py_None);
1326 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001327}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001328#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001329
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001330#ifdef HAVE_LCHOWN
1331PyDoc_STRVAR(posix_lchown__doc__,
1332"lchown(path, uid, gid)\n\n\
1333Change the owner and group id of path to the numeric uid and gid.\n\
1334This function will not follow symbolic links.");
1335
1336static PyObject *
1337posix_lchown(PyObject *self, PyObject *args)
1338{
1339 char *path = NULL;
1340 int uid, gid;
1341 int res;
1342 if (!PyArg_ParseTuple(args, "etii:lchown",
1343 Py_FileSystemDefaultEncoding, &path,
1344 &uid, &gid))
1345 return NULL;
1346 Py_BEGIN_ALLOW_THREADS
1347 res = lchown(path, (uid_t) uid, (gid_t) gid);
1348 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001349 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001350 return posix_error_with_allocated_filename(path);
1351 PyMem_Free(path);
1352 Py_INCREF(Py_None);
1353 return Py_None;
1354}
1355#endif /* HAVE_LCHOWN */
1356
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001357
Guido van Rossum36bc6801995-06-14 22:54:23 +00001358#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001360"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001361Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001362
Barry Warsaw53699e91996-12-10 23:23:01 +00001363static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001364posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001365{
1366 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001367 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001368
Barry Warsaw53699e91996-12-10 23:23:01 +00001369 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001370#if defined(PYOS_OS2) && defined(PYCC_GCC)
1371 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001372#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001373 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001374#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001375 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001376 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001378 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001380
Walter Dörwald3b918c32002-11-21 20:18:46 +00001381#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001382PyDoc_STRVAR(posix_getcwdu__doc__,
1383"getcwdu() -> path\n\n\
1384Return a unicode string representing the current working directory.");
1385
1386static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001387posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001388{
1389 char buf[1026];
1390 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001391
1392#ifdef Py_WIN_WIDE_FILENAMES
1393 if (unicode_file_names()) {
1394 wchar_t *wres;
1395 wchar_t wbuf[1026];
1396 Py_BEGIN_ALLOW_THREADS
1397 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1398 Py_END_ALLOW_THREADS
1399 if (wres == NULL)
1400 return posix_error();
1401 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1402 }
1403#endif
1404
1405 Py_BEGIN_ALLOW_THREADS
1406#if defined(PYOS_OS2) && defined(PYCC_GCC)
1407 res = _getcwd2(buf, sizeof buf);
1408#else
1409 res = getcwd(buf, sizeof buf);
1410#endif
1411 Py_END_ALLOW_THREADS
1412 if (res == NULL)
1413 return posix_error();
1414 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1415}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001416#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001417#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001418
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001419
Guido van Rossumb6775db1994-08-01 11:34:53 +00001420#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001421PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001422"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001424
Barry Warsaw53699e91996-12-10 23:23:01 +00001425static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001426posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001428 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001430#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001434"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001435Return a list containing the names of the entries in the directory.\n\
1436\n\
1437 path: path of directory to list\n\
1438\n\
1439The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001440entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001441
Barry Warsaw53699e91996-12-10 23:23:01 +00001442static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001443posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001444{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001445 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001446 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001447#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001448
Barry Warsaw53699e91996-12-10 23:23:01 +00001449 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001450 HANDLE hFindFile;
1451 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001452 /* MAX_PATH characters could mean a bigger encoded string */
1453 char namebuf[MAX_PATH*2+5];
1454 char *bufptr = namebuf;
1455 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001456
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457#ifdef Py_WIN_WIDE_FILENAMES
1458 /* If on wide-character-capable OS see if argument
1459 is Unicode and if so use wide API. */
1460 if (unicode_file_names()) {
1461 PyUnicodeObject *po;
1462 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1463 WIN32_FIND_DATAW wFileData;
1464 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1465 Py_UNICODE wch;
1466 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1467 wnamebuf[MAX_PATH] = L'\0';
1468 len = wcslen(wnamebuf);
1469 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1470 if (wch != L'/' && wch != L'\\' && wch != L':')
1471 wnamebuf[len++] = L'/';
1472 wcscpy(wnamebuf + len, L"*.*");
1473 if ((d = PyList_New(0)) == NULL)
1474 return NULL;
1475 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1476 if (hFindFile == INVALID_HANDLE_VALUE) {
1477 errno = GetLastError();
1478 if (errno == ERROR_FILE_NOT_FOUND) {
1479 return d;
1480 }
1481 Py_DECREF(d);
1482 return win32_error_unicode("FindFirstFileW", wnamebuf);
1483 }
1484 do {
1485 if (wFileData.cFileName[0] == L'.' &&
1486 (wFileData.cFileName[1] == L'\0' ||
1487 wFileData.cFileName[1] == L'.' &&
1488 wFileData.cFileName[2] == L'\0'))
1489 continue;
1490 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1491 if (v == NULL) {
1492 Py_DECREF(d);
1493 d = NULL;
1494 break;
1495 }
1496 if (PyList_Append(d, v) != 0) {
1497 Py_DECREF(v);
1498 Py_DECREF(d);
1499 d = NULL;
1500 break;
1501 }
1502 Py_DECREF(v);
1503 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1504
1505 if (FindClose(hFindFile) == FALSE) {
1506 Py_DECREF(d);
1507 return win32_error_unicode("FindClose", wnamebuf);
1508 }
1509 return d;
1510 }
1511 /* Drop the argument parsing error as narrow strings
1512 are also valid. */
1513 PyErr_Clear();
1514 }
1515#endif
1516
Tim Peters5aa91602002-01-30 05:46:57 +00001517 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001518 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001519 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001520 if (len > 0) {
1521 char ch = namebuf[len-1];
1522 if (ch != SEP && ch != ALTSEP && ch != ':')
1523 namebuf[len++] = '/';
1524 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001525 strcpy(namebuf + len, "*.*");
1526
Barry Warsaw53699e91996-12-10 23:23:01 +00001527 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001528 return NULL;
1529
1530 hFindFile = FindFirstFile(namebuf, &FileData);
1531 if (hFindFile == INVALID_HANDLE_VALUE) {
1532 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001533 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001534 return d;
1535 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001536 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001537 }
1538 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001539 if (FileData.cFileName[0] == '.' &&
1540 (FileData.cFileName[1] == '\0' ||
1541 FileData.cFileName[1] == '.' &&
1542 FileData.cFileName[2] == '\0'))
1543 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001544 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001545 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001546 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001547 d = NULL;
1548 break;
1549 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001550 if (PyList_Append(d, v) != 0) {
1551 Py_DECREF(v);
1552 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001553 d = NULL;
1554 break;
1555 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001556 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001557 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1558
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001559 if (FindClose(hFindFile) == FALSE) {
1560 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001561 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001562 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001563
1564 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001565
Tim Peters0bb44a42000-09-15 07:44:49 +00001566#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001567
1568#ifndef MAX_PATH
1569#define MAX_PATH CCHMAXPATH
1570#endif
1571 char *name, *pt;
1572 int len;
1573 PyObject *d, *v;
1574 char namebuf[MAX_PATH+5];
1575 HDIR hdir = 1;
1576 ULONG srchcnt = 1;
1577 FILEFINDBUF3 ep;
1578 APIRET rc;
1579
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001580 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001581 return NULL;
1582 if (len >= MAX_PATH) {
1583 PyErr_SetString(PyExc_ValueError, "path too long");
1584 return NULL;
1585 }
1586 strcpy(namebuf, name);
1587 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001588 if (*pt == ALTSEP)
1589 *pt = SEP;
1590 if (namebuf[len-1] != SEP)
1591 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001592 strcpy(namebuf + len, "*.*");
1593
1594 if ((d = PyList_New(0)) == NULL)
1595 return NULL;
1596
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001597 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1598 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001599 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001600 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1601 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1602 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001603
1604 if (rc != NO_ERROR) {
1605 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001606 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001607 }
1608
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001609 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001610 do {
1611 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001612 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001613 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001614
1615 strcpy(namebuf, ep.achName);
1616
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001617 /* Leave Case of Name Alone -- In Native Form */
1618 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001619
1620 v = PyString_FromString(namebuf);
1621 if (v == NULL) {
1622 Py_DECREF(d);
1623 d = NULL;
1624 break;
1625 }
1626 if (PyList_Append(d, v) != 0) {
1627 Py_DECREF(v);
1628 Py_DECREF(d);
1629 d = NULL;
1630 break;
1631 }
1632 Py_DECREF(v);
1633 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1634 }
1635
1636 return d;
1637#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001638
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001639 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001641 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001642 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001643 int arg_is_unicode = 1;
1644
1645 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1646 arg_is_unicode = 0;
1647 PyErr_Clear();
1648 }
1649 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001650 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001651 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001652 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001653 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001656 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001657 return NULL;
1658 }
1659 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001660 if (ep->d_name[0] == '.' &&
1661 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001662 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001663 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001664 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001666 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667 d = NULL;
1668 break;
1669 }
Just van Rossum46c97842003-02-25 21:42:15 +00001670#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001671 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001672 PyObject *w;
1673
1674 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001675 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001676 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001677 if (w != NULL) {
1678 Py_DECREF(v);
1679 v = w;
1680 }
1681 else {
1682 /* fall back to the original byte string, as
1683 discussed in patch #683592 */
1684 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001685 }
Just van Rossum46c97842003-02-25 21:42:15 +00001686 }
1687#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001688 if (PyList_Append(d, v) != 0) {
1689 Py_DECREF(v);
1690 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691 d = NULL;
1692 break;
1693 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001694 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001695 }
1696 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001697 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001698
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001699 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001700
Tim Peters0bb44a42000-09-15 07:44:49 +00001701#endif /* which OS */
1702} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001703
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001704#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001705/* A helper function for abspath on win32 */
1706static PyObject *
1707posix__getfullpathname(PyObject *self, PyObject *args)
1708{
1709 /* assume encoded strings wont more than double no of chars */
1710 char inbuf[MAX_PATH*2];
1711 char *inbufp = inbuf;
1712 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1713 char outbuf[MAX_PATH*2];
1714 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001715#ifdef Py_WIN_WIDE_FILENAMES
1716 if (unicode_file_names()) {
1717 PyUnicodeObject *po;
1718 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1719 Py_UNICODE woutbuf[MAX_PATH*2];
1720 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001721 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001722 sizeof(woutbuf)/sizeof(woutbuf[0]),
1723 woutbuf, &wtemp))
1724 return win32_error("GetFullPathName", "");
1725 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1726 }
1727 /* Drop the argument parsing error as narrow strings
1728 are also valid. */
1729 PyErr_Clear();
1730 }
1731#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001732 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1733 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001734 &insize))
1735 return NULL;
1736 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1737 outbuf, &temp))
1738 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00001739 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
1740 return PyUnicode_Decode(outbuf, strlen(outbuf),
1741 Py_FileSystemDefaultEncoding, NULL);
1742 }
Mark Hammondef8b6542001-05-13 08:04:26 +00001743 return PyString_FromString(outbuf);
1744} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001745#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001748"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001750
Barry Warsaw53699e91996-12-10 23:23:01 +00001751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001752posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001753{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001754 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001755 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001756 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001757
1758#ifdef Py_WIN_WIDE_FILENAMES
1759 if (unicode_file_names()) {
1760 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001761 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001762 Py_BEGIN_ALLOW_THREADS
1763 /* PyUnicode_AS_UNICODE OK without thread lock as
1764 it is a simple dereference. */
1765 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1766 Py_END_ALLOW_THREADS
1767 if (res < 0)
1768 return posix_error();
1769 Py_INCREF(Py_None);
1770 return Py_None;
1771 }
1772 /* Drop the argument parsing error as narrow strings
1773 are also valid. */
1774 PyErr_Clear();
1775 }
1776#endif
1777
Tim Peters5aa91602002-01-30 05:46:57 +00001778 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001779 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001780 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001781 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001782#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001783 res = mkdir(path);
1784#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001785 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001786#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001787 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001788 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001789 return posix_error_with_allocated_filename(path);
1790 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001791 Py_INCREF(Py_None);
1792 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001793}
1794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001795
Guido van Rossumb6775db1994-08-01 11:34:53 +00001796#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001797#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1798#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1799#include <sys/resource.h>
1800#endif
1801#endif
1802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001804"nice(inc) -> new_priority\n\n\
1805Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001806
Barry Warsaw53699e91996-12-10 23:23:01 +00001807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001808posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001809{
1810 int increment, value;
1811
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001812 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001813 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001814
1815 /* There are two flavours of 'nice': one that returns the new
1816 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001817 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1818 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001819
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001820 If we are of the nice family that returns the new priority, we
1821 need to clear errno before the call, and check if errno is filled
1822 before calling posix_error() on a returnvalue of -1, because the
1823 -1 may be the actual new priority! */
1824
1825 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001826 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001827#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001828 if (value == 0)
1829 value = getpriority(PRIO_PROCESS, 0);
1830#endif
1831 if (value == -1 && errno != 0)
1832 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001833 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001834 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001835}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001836#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001837
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
1847 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1848#else
1849 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1850#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001851}
1852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001855"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001857
Barry Warsaw53699e91996-12-10 23:23:01 +00001858static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001859posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001860{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001861#ifdef MS_WINDOWS
1862 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1863#else
1864 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1865#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866}
1867
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001870"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872
Barry Warsaw53699e91996-12-10 23:23:01 +00001873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001874posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001875{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001876#ifdef MS_WINDOWS
1877 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1878#else
1879 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1880#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881}
1882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001883
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001884#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001886"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001888
Barry Warsaw53699e91996-12-10 23:23:01 +00001889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001890posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001891{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001892 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001893 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001894 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001895 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001896 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001897 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001898 Py_END_ALLOW_THREADS
1899 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001900}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001901#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001902
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001905"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001907
Barry Warsaw53699e91996-12-10 23:23:01 +00001908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001909posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001910{
1911 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001912 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001913 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001914 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001915 if (i < 0)
1916 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001917 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001918}
1919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001921PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001922"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001923Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001925PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001926"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001928
Barry Warsaw53699e91996-12-10 23:23:01 +00001929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001930posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001931{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001932#ifdef MS_WINDOWS
1933 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1934#else
1935 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1936#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001937}
1938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001939
Guido van Rossumb6775db1994-08-01 11:34:53 +00001940#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001942"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001944
Barry Warsaw53699e91996-12-10 23:23:01 +00001945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001946posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001947{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001948 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001949 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001950
Barry Warsaw53699e91996-12-10 23:23:01 +00001951 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001952 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001953 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001954 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001955 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001956 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001957 u.sysname,
1958 u.nodename,
1959 u.release,
1960 u.version,
1961 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001962}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001963#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001964
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001965static int
1966extract_time(PyObject *t, long* sec, long* usec)
1967{
1968 long intval;
1969 if (PyFloat_Check(t)) {
1970 double tval = PyFloat_AsDouble(t);
1971 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1972 if (!intobj)
1973 return -1;
1974 intval = PyInt_AsLong(intobj);
1975 Py_DECREF(intobj);
1976 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001977 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001978 if (*usec < 0)
1979 /* If rounding gave us a negative number,
1980 truncate. */
1981 *usec = 0;
1982 return 0;
1983 }
1984 intval = PyInt_AsLong(t);
1985 if (intval == -1 && PyErr_Occurred())
1986 return -1;
1987 *sec = intval;
1988 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001989 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001990}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001993"utime(path, (atime, utime))\n\
1994utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001995Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001997
Barry Warsaw53699e91996-12-10 23:23:01 +00001998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001999posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002000{
Neal Norwitz2adf2102004-06-09 01:46:02 +00002001 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002002 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002003 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002004 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002005
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002006#if defined(HAVE_UTIMES)
2007 struct timeval buf[2];
2008#define ATIME buf[0].tv_sec
2009#define MTIME buf[1].tv_sec
2010#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002011/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002012 struct utimbuf buf;
2013#define ATIME buf.actime
2014#define MTIME buf.modtime
2015#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002016#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002017 time_t buf[2];
2018#define ATIME buf[0]
2019#define MTIME buf[1]
2020#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002021#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002022
Mark Hammond817c9292003-12-03 01:22:38 +00002023 int have_unicode_filename = 0;
2024#ifdef Py_WIN_WIDE_FILENAMES
2025 PyUnicodeObject *obwpath;
2026 wchar_t *wpath;
2027 if (unicode_file_names()) {
2028 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2029 wpath = PyUnicode_AS_UNICODE(obwpath);
2030 have_unicode_filename = 1;
2031 } else
2032 /* Drop the argument parsing error as narrow strings
2033 are also valid. */
2034 PyErr_Clear();
2035 }
2036#endif /* Py_WIN_WIDE_FILENAMES */
2037
2038 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002039 !PyArg_ParseTuple(args, "etO:utime",
2040 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002041 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002042 if (arg == Py_None) {
2043 /* optional time values not given */
2044 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002045#ifdef Py_WIN_WIDE_FILENAMES
2046 if (have_unicode_filename)
2047 res = _wutime(wpath, NULL);
2048 else
2049#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002050 res = utime(path, NULL);
2051 Py_END_ALLOW_THREADS
2052 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002053 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002054 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002055 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002056 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002057 return NULL;
2058 }
2059 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002060 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002061 &atime, &ausec) == -1) {
2062 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002063 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002064 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002065 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002066 &mtime, &musec) == -1) {
2067 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002068 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002069 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002070 ATIME = atime;
2071 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002072#ifdef HAVE_UTIMES
2073 buf[0].tv_usec = ausec;
2074 buf[1].tv_usec = musec;
2075 Py_BEGIN_ALLOW_THREADS
2076 res = utimes(path, buf);
2077 Py_END_ALLOW_THREADS
2078#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002079 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002080#ifdef Py_WIN_WIDE_FILENAMES
2081 if (have_unicode_filename)
2082 /* utime is OK with utimbuf, but _wutime insists
2083 on _utimbuf (the msvc headers assert the
2084 underscore version is ansi) */
2085 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2086 else
2087#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002088 res = utime(path, UTIME_ARG);
2089 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002090#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002091 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002092 if (res < 0) {
2093#ifdef Py_WIN_WIDE_FILENAMES
Neal Norwitz2adf2102004-06-09 01:46:02 +00002094 if (have_unicode_filename)
Mark Hammond2d5914b2004-05-04 08:10:37 +00002095 return posix_error_with_unicode_filename(wpath);
2096#endif /* Py_WIN_WIDE_FILENAMES */
Neal Norwitz96652712004-06-06 20:40:27 +00002097 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002098 }
Neal Norwitz96652712004-06-06 20:40:27 +00002099 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 Py_INCREF(Py_None);
2101 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002102#undef UTIME_ARG
2103#undef ATIME
2104#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002105}
2106
Guido van Rossum85e3b011991-06-03 12:42:10 +00002107
Guido van Rossum3b066191991-06-04 19:40:25 +00002108/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002110PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002111"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113
Barry Warsaw53699e91996-12-10 23:23:01 +00002114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002115posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002116{
2117 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002118 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002119 return NULL;
2120 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002121 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002122}
2123
Martin v. Löwis114619e2002-10-07 06:44:21 +00002124#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2125static void
2126free_string_array(char **array, int count)
2127{
2128 int i;
2129 for (i = 0; i < count; i++)
2130 PyMem_Free(array[i]);
2131 PyMem_DEL(array);
2132}
2133#endif
2134
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002135
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002136#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002138"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139Execute an executable path with arguments, replacing current process.\n\
2140\n\
2141 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002143
Barry Warsaw53699e91996-12-10 23:23:01 +00002144static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002145posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002146{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002147 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002148 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002149 char **argvlist;
2150 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002151 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002152
Guido van Rossum89b33251993-10-22 14:26:06 +00002153 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002154 argv is a list or tuple of strings. */
2155
Martin v. Löwis114619e2002-10-07 06:44:21 +00002156 if (!PyArg_ParseTuple(args, "etO:execv",
2157 Py_FileSystemDefaultEncoding,
2158 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002159 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 if (PyList_Check(argv)) {
2161 argc = PyList_Size(argv);
2162 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002163 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 else if (PyTuple_Check(argv)) {
2165 argc = PyTuple_Size(argv);
2166 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002167 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002168 else {
Fred Drake661ea262000-10-24 19:57:45 +00002169 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002170 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002171 return NULL;
2172 }
2173
2174 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002175 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002176 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002177 return NULL;
2178 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002179
Barry Warsaw53699e91996-12-10 23:23:01 +00002180 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002181 if (argvlist == NULL) {
2182 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002183 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002184 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002185 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002186 if (!PyArg_Parse((*getitem)(argv, i), "et",
2187 Py_FileSystemDefaultEncoding,
2188 &argvlist[i])) {
2189 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002190 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002191 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002192 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002193 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002194
Guido van Rossum85e3b011991-06-03 12:42:10 +00002195 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002196 }
2197 argvlist[argc] = NULL;
2198
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002199 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002200
Guido van Rossum85e3b011991-06-03 12:42:10 +00002201 /* If we get here it's definitely an error */
2202
Martin v. Löwis114619e2002-10-07 06:44:21 +00002203 free_string_array(argvlist, argc);
2204 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002205 return posix_error();
2206}
2207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002209PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002210"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002211Execute a path with arguments and environment, replacing current process.\n\
2212\n\
2213 path: path of executable file\n\
2214 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002215 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002216
Barry Warsaw53699e91996-12-10 23:23:01 +00002217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002218posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002219{
2220 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002221 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002222 char **argvlist;
2223 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002224 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002225 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002226 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002227 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002228
2229 /* execve has three arguments: (path, argv, env), where
2230 argv is a list or tuple of strings and env is a dictionary
2231 like posix.environ. */
2232
Martin v. Löwis114619e2002-10-07 06:44:21 +00002233 if (!PyArg_ParseTuple(args, "etOO:execve",
2234 Py_FileSystemDefaultEncoding,
2235 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002236 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 if (PyList_Check(argv)) {
2238 argc = PyList_Size(argv);
2239 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002240 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002241 else if (PyTuple_Check(argv)) {
2242 argc = PyTuple_Size(argv);
2243 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002244 }
2245 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002246 PyErr_SetString(PyExc_TypeError,
2247 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002248 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002249 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002250 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002251 PyErr_SetString(PyExc_TypeError,
2252 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002253 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002254 }
2255
Guido van Rossum50422b42000-04-26 20:34:28 +00002256 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002257 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002258 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002259 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002260 }
2261
Barry Warsaw53699e91996-12-10 23:23:01 +00002262 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002263 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002265 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002266 }
2267 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002268 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002269 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002270 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002271 &argvlist[i]))
2272 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002273 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002274 goto fail_1;
2275 }
2276 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002277 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002278 argvlist[argc] = NULL;
2279
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002280 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002281 if (i < 0)
2282 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002283 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002284 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002285 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002286 goto fail_1;
2287 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002288 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002289 keys = PyMapping_Keys(env);
2290 vals = PyMapping_Values(env);
2291 if (!keys || !vals)
2292 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002293 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2294 PyErr_SetString(PyExc_TypeError,
2295 "execve(): env.keys() or env.values() is not a list");
2296 goto fail_2;
2297 }
Tim Peters5aa91602002-01-30 05:46:57 +00002298
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002299 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002300 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002301 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002302
2303 key = PyList_GetItem(keys, pos);
2304 val = PyList_GetItem(vals, pos);
2305 if (!key || !val)
2306 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002307
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002308 if (!PyArg_Parse(
2309 key,
2310 "s;execve() arg 3 contains a non-string key",
2311 &k) ||
2312 !PyArg_Parse(
2313 val,
2314 "s;execve() arg 3 contains a non-string value",
2315 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002316 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002317 goto fail_2;
2318 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002319
2320#if defined(PYOS_OS2)
2321 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2322 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2323#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002324 len = PyString_Size(key) + PyString_Size(val) + 2;
2325 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002326 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002327 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002328 goto fail_2;
2329 }
Tim Petersc8996f52001-12-03 20:41:00 +00002330 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002331 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002332#if defined(PYOS_OS2)
2333 }
2334#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002335 }
2336 envlist[envc] = 0;
2337
2338 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002339
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002340 /* If we get here it's definitely an error */
2341
2342 (void) posix_error();
2343
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002344 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002345 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002346 PyMem_DEL(envlist[envc]);
2347 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002348 fail_1:
2349 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002350 Py_XDECREF(vals);
2351 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002352 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002353 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002354 return NULL;
2355}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002356#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002357
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002358
Guido van Rossuma1065681999-01-25 23:20:23 +00002359#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002361"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002362Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002363\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002364 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002365 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002366 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002367
2368static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002369posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002370{
2371 char *path;
2372 PyObject *argv;
2373 char **argvlist;
2374 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002375 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002376 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002377
2378 /* spawnv has three arguments: (mode, path, argv), where
2379 argv is a list or tuple of strings. */
2380
Martin v. Löwis114619e2002-10-07 06:44:21 +00002381 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2382 Py_FileSystemDefaultEncoding,
2383 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002384 return NULL;
2385 if (PyList_Check(argv)) {
2386 argc = PyList_Size(argv);
2387 getitem = PyList_GetItem;
2388 }
2389 else if (PyTuple_Check(argv)) {
2390 argc = PyTuple_Size(argv);
2391 getitem = PyTuple_GetItem;
2392 }
2393 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002394 PyErr_SetString(PyExc_TypeError,
2395 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002396 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002397 return NULL;
2398 }
2399
2400 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002401 if (argvlist == NULL) {
2402 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002403 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002404 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002405 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002406 if (!PyArg_Parse((*getitem)(argv, i), "et",
2407 Py_FileSystemDefaultEncoding,
2408 &argvlist[i])) {
2409 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002410 PyErr_SetString(
2411 PyExc_TypeError,
2412 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002413 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002414 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002415 }
2416 }
2417 argvlist[argc] = NULL;
2418
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002419#if defined(PYOS_OS2) && defined(PYCC_GCC)
2420 Py_BEGIN_ALLOW_THREADS
2421 spawnval = spawnv(mode, path, argvlist);
2422 Py_END_ALLOW_THREADS
2423#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002424 if (mode == _OLD_P_OVERLAY)
2425 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002426
Tim Peters25059d32001-12-07 20:35:43 +00002427 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002428 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002429 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002430#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002431
Martin v. Löwis114619e2002-10-07 06:44:21 +00002432 free_string_array(argvlist, argc);
2433 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002434
Fred Drake699f3522000-06-29 21:12:41 +00002435 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002436 return posix_error();
2437 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002438#if SIZEOF_LONG == SIZEOF_VOID_P
2439 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002440#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002441 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002442#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002443}
2444
2445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002446PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002447"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002448Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002449\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002450 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002451 path: path of executable file\n\
2452 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002453 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002454
2455static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002456posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002457{
2458 char *path;
2459 PyObject *argv, *env;
2460 char **argvlist;
2461 char **envlist;
2462 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2463 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002464 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002465 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002466 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002467
2468 /* spawnve has four arguments: (mode, path, argv, env), where
2469 argv is a list or tuple of strings and env is a dictionary
2470 like posix.environ. */
2471
Martin v. Löwis114619e2002-10-07 06:44:21 +00002472 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2473 Py_FileSystemDefaultEncoding,
2474 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002475 return NULL;
2476 if (PyList_Check(argv)) {
2477 argc = PyList_Size(argv);
2478 getitem = PyList_GetItem;
2479 }
2480 else if (PyTuple_Check(argv)) {
2481 argc = PyTuple_Size(argv);
2482 getitem = PyTuple_GetItem;
2483 }
2484 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002485 PyErr_SetString(PyExc_TypeError,
2486 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002487 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002488 }
2489 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002490 PyErr_SetString(PyExc_TypeError,
2491 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002492 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002493 }
2494
2495 argvlist = PyMem_NEW(char *, argc+1);
2496 if (argvlist == NULL) {
2497 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002498 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002499 }
2500 for (i = 0; i < argc; i++) {
2501 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002502 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002503 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002504 &argvlist[i]))
2505 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002506 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002507 goto fail_1;
2508 }
2509 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002510 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002511 argvlist[argc] = NULL;
2512
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002513 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002514 if (i < 0)
2515 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002516 envlist = PyMem_NEW(char *, i + 1);
2517 if (envlist == NULL) {
2518 PyErr_NoMemory();
2519 goto fail_1;
2520 }
2521 envc = 0;
2522 keys = PyMapping_Keys(env);
2523 vals = PyMapping_Values(env);
2524 if (!keys || !vals)
2525 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002526 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2527 PyErr_SetString(PyExc_TypeError,
2528 "spawnve(): env.keys() or env.values() is not a list");
2529 goto fail_2;
2530 }
Tim Peters5aa91602002-01-30 05:46:57 +00002531
Guido van Rossuma1065681999-01-25 23:20:23 +00002532 for (pos = 0; pos < i; pos++) {
2533 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002534 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002535
2536 key = PyList_GetItem(keys, pos);
2537 val = PyList_GetItem(vals, pos);
2538 if (!key || !val)
2539 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002540
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002541 if (!PyArg_Parse(
2542 key,
2543 "s;spawnve() arg 3 contains a non-string key",
2544 &k) ||
2545 !PyArg_Parse(
2546 val,
2547 "s;spawnve() arg 3 contains a non-string value",
2548 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002549 {
2550 goto fail_2;
2551 }
Tim Petersc8996f52001-12-03 20:41:00 +00002552 len = PyString_Size(key) + PyString_Size(val) + 2;
2553 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002554 if (p == NULL) {
2555 PyErr_NoMemory();
2556 goto fail_2;
2557 }
Tim Petersc8996f52001-12-03 20:41:00 +00002558 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002559 envlist[envc++] = p;
2560 }
2561 envlist[envc] = 0;
2562
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002563#if defined(PYOS_OS2) && defined(PYCC_GCC)
2564 Py_BEGIN_ALLOW_THREADS
2565 spawnval = spawnve(mode, path, argvlist, envlist);
2566 Py_END_ALLOW_THREADS
2567#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002568 if (mode == _OLD_P_OVERLAY)
2569 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002570
2571 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002572 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002573 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002574#endif
Tim Peters25059d32001-12-07 20:35:43 +00002575
Fred Drake699f3522000-06-29 21:12:41 +00002576 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002577 (void) posix_error();
2578 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002579#if SIZEOF_LONG == SIZEOF_VOID_P
2580 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002581#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002582 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002583#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002584
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002585 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002586 while (--envc >= 0)
2587 PyMem_DEL(envlist[envc]);
2588 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002589 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002590 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002591 Py_XDECREF(vals);
2592 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002593 fail_0:
2594 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002595 return res;
2596}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002597
2598/* OS/2 supports spawnvp & spawnvpe natively */
2599#if defined(PYOS_OS2)
2600PyDoc_STRVAR(posix_spawnvp__doc__,
2601"spawnvp(mode, file, args)\n\n\
2602Execute the program 'file' in a new process, using the environment\n\
2603search path to find the file.\n\
2604\n\
2605 mode: mode of process creation\n\
2606 file: executable file name\n\
2607 args: tuple or list of strings");
2608
2609static PyObject *
2610posix_spawnvp(PyObject *self, PyObject *args)
2611{
2612 char *path;
2613 PyObject *argv;
2614 char **argvlist;
2615 int mode, i, argc;
2616 Py_intptr_t spawnval;
2617 PyObject *(*getitem)(PyObject *, int);
2618
2619 /* spawnvp has three arguments: (mode, path, argv), where
2620 argv is a list or tuple of strings. */
2621
2622 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2623 Py_FileSystemDefaultEncoding,
2624 &path, &argv))
2625 return NULL;
2626 if (PyList_Check(argv)) {
2627 argc = PyList_Size(argv);
2628 getitem = PyList_GetItem;
2629 }
2630 else if (PyTuple_Check(argv)) {
2631 argc = PyTuple_Size(argv);
2632 getitem = PyTuple_GetItem;
2633 }
2634 else {
2635 PyErr_SetString(PyExc_TypeError,
2636 "spawnvp() arg 2 must be a tuple or list");
2637 PyMem_Free(path);
2638 return NULL;
2639 }
2640
2641 argvlist = PyMem_NEW(char *, argc+1);
2642 if (argvlist == NULL) {
2643 PyMem_Free(path);
2644 return PyErr_NoMemory();
2645 }
2646 for (i = 0; i < argc; i++) {
2647 if (!PyArg_Parse((*getitem)(argv, i), "et",
2648 Py_FileSystemDefaultEncoding,
2649 &argvlist[i])) {
2650 free_string_array(argvlist, i);
2651 PyErr_SetString(
2652 PyExc_TypeError,
2653 "spawnvp() arg 2 must contain only strings");
2654 PyMem_Free(path);
2655 return NULL;
2656 }
2657 }
2658 argvlist[argc] = NULL;
2659
2660 Py_BEGIN_ALLOW_THREADS
2661#if defined(PYCC_GCC)
2662 spawnval = spawnvp(mode, path, argvlist);
2663#else
2664 spawnval = _spawnvp(mode, path, argvlist);
2665#endif
2666 Py_END_ALLOW_THREADS
2667
2668 free_string_array(argvlist, argc);
2669 PyMem_Free(path);
2670
2671 if (spawnval == -1)
2672 return posix_error();
2673 else
2674 return Py_BuildValue("l", (long) spawnval);
2675}
2676
2677
2678PyDoc_STRVAR(posix_spawnvpe__doc__,
2679"spawnvpe(mode, file, args, env)\n\n\
2680Execute the program 'file' in a new process, using the environment\n\
2681search path to find the file.\n\
2682\n\
2683 mode: mode of process creation\n\
2684 file: executable file name\n\
2685 args: tuple or list of arguments\n\
2686 env: dictionary of strings mapping to strings");
2687
2688static PyObject *
2689posix_spawnvpe(PyObject *self, PyObject *args)
2690{
2691 char *path;
2692 PyObject *argv, *env;
2693 char **argvlist;
2694 char **envlist;
2695 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2696 int mode, i, pos, argc, envc;
2697 Py_intptr_t spawnval;
2698 PyObject *(*getitem)(PyObject *, int);
2699 int lastarg = 0;
2700
2701 /* spawnvpe has four arguments: (mode, path, argv, env), where
2702 argv is a list or tuple of strings and env is a dictionary
2703 like posix.environ. */
2704
2705 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2706 Py_FileSystemDefaultEncoding,
2707 &path, &argv, &env))
2708 return NULL;
2709 if (PyList_Check(argv)) {
2710 argc = PyList_Size(argv);
2711 getitem = PyList_GetItem;
2712 }
2713 else if (PyTuple_Check(argv)) {
2714 argc = PyTuple_Size(argv);
2715 getitem = PyTuple_GetItem;
2716 }
2717 else {
2718 PyErr_SetString(PyExc_TypeError,
2719 "spawnvpe() arg 2 must be a tuple or list");
2720 goto fail_0;
2721 }
2722 if (!PyMapping_Check(env)) {
2723 PyErr_SetString(PyExc_TypeError,
2724 "spawnvpe() arg 3 must be a mapping object");
2725 goto fail_0;
2726 }
2727
2728 argvlist = PyMem_NEW(char *, argc+1);
2729 if (argvlist == NULL) {
2730 PyErr_NoMemory();
2731 goto fail_0;
2732 }
2733 for (i = 0; i < argc; i++) {
2734 if (!PyArg_Parse((*getitem)(argv, i),
2735 "et;spawnvpe() arg 2 must contain only strings",
2736 Py_FileSystemDefaultEncoding,
2737 &argvlist[i]))
2738 {
2739 lastarg = i;
2740 goto fail_1;
2741 }
2742 }
2743 lastarg = argc;
2744 argvlist[argc] = NULL;
2745
2746 i = PyMapping_Size(env);
2747 if (i < 0)
2748 goto fail_1;
2749 envlist = PyMem_NEW(char *, i + 1);
2750 if (envlist == NULL) {
2751 PyErr_NoMemory();
2752 goto fail_1;
2753 }
2754 envc = 0;
2755 keys = PyMapping_Keys(env);
2756 vals = PyMapping_Values(env);
2757 if (!keys || !vals)
2758 goto fail_2;
2759 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2760 PyErr_SetString(PyExc_TypeError,
2761 "spawnvpe(): env.keys() or env.values() is not a list");
2762 goto fail_2;
2763 }
2764
2765 for (pos = 0; pos < i; pos++) {
2766 char *p, *k, *v;
2767 size_t len;
2768
2769 key = PyList_GetItem(keys, pos);
2770 val = PyList_GetItem(vals, pos);
2771 if (!key || !val)
2772 goto fail_2;
2773
2774 if (!PyArg_Parse(
2775 key,
2776 "s;spawnvpe() arg 3 contains a non-string key",
2777 &k) ||
2778 !PyArg_Parse(
2779 val,
2780 "s;spawnvpe() arg 3 contains a non-string value",
2781 &v))
2782 {
2783 goto fail_2;
2784 }
2785 len = PyString_Size(key) + PyString_Size(val) + 2;
2786 p = PyMem_NEW(char, len);
2787 if (p == NULL) {
2788 PyErr_NoMemory();
2789 goto fail_2;
2790 }
2791 PyOS_snprintf(p, len, "%s=%s", k, v);
2792 envlist[envc++] = p;
2793 }
2794 envlist[envc] = 0;
2795
2796 Py_BEGIN_ALLOW_THREADS
2797#if defined(PYCC_GCC)
2798 spawnval = spawnve(mode, path, argvlist, envlist);
2799#else
2800 spawnval = _spawnve(mode, path, argvlist, envlist);
2801#endif
2802 Py_END_ALLOW_THREADS
2803
2804 if (spawnval == -1)
2805 (void) posix_error();
2806 else
2807 res = Py_BuildValue("l", (long) spawnval);
2808
2809 fail_2:
2810 while (--envc >= 0)
2811 PyMem_DEL(envlist[envc]);
2812 PyMem_DEL(envlist);
2813 fail_1:
2814 free_string_array(argvlist, lastarg);
2815 Py_XDECREF(vals);
2816 Py_XDECREF(keys);
2817 fail_0:
2818 PyMem_Free(path);
2819 return res;
2820}
2821#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00002822#endif /* HAVE_SPAWNV */
2823
2824
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002825#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002827"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002828Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2829\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002831
2832static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002833posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002834{
Neal Norwitze241ce82003-02-17 18:17:05 +00002835 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002836 if (pid == -1)
2837 return posix_error();
2838 PyOS_AfterFork();
2839 return PyInt_FromLong((long)pid);
2840}
2841#endif
2842
2843
Guido van Rossumad0ee831995-03-01 10:34:45 +00002844#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002845PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002846"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002847Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002848Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002849
Barry Warsaw53699e91996-12-10 23:23:01 +00002850static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002851posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002852{
Neal Norwitze241ce82003-02-17 18:17:05 +00002853 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002854 if (pid == -1)
2855 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002856 if (pid == 0)
2857 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002858 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002859}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002860#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002861
Neal Norwitzb59798b2003-03-21 01:43:31 +00002862/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002863/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2864#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002865#define DEV_PTY_FILE "/dev/ptc"
2866#define HAVE_DEV_PTMX
2867#else
2868#define DEV_PTY_FILE "/dev/ptmx"
2869#endif
2870
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002871#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002872#ifdef HAVE_PTY_H
2873#include <pty.h>
2874#else
2875#ifdef HAVE_LIBUTIL_H
2876#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002877#endif /* HAVE_LIBUTIL_H */
2878#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002879#ifdef HAVE_STROPTS_H
2880#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002881#endif
2882#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002883
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002884#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002885PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002886"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002887Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002888
2889static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002890posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002891{
2892 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002893#ifndef HAVE_OPENPTY
2894 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002895#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002896#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002897 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002898#ifdef sun
2899 extern char *ptsname();
2900#endif
2901#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002902
Thomas Wouters70c21a12000-07-14 14:28:33 +00002903#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002904 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2905 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002906#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002907 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2908 if (slave_name == NULL)
2909 return posix_error();
2910
2911 slave_fd = open(slave_name, O_RDWR);
2912 if (slave_fd < 0)
2913 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002914#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002915 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002916 if (master_fd < 0)
2917 return posix_error();
2918 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002919 /* change permission of slave */
2920 if (grantpt(master_fd) < 0) {
2921 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002922 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002923 }
2924 /* unlock slave */
2925 if (unlockpt(master_fd) < 0) {
2926 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002927 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002928 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002929 signal(SIGCHLD, sig_saved);
2930 slave_name = ptsname(master_fd); /* get name of slave */
2931 if (slave_name == NULL)
2932 return posix_error();
2933 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2934 if (slave_fd < 0)
2935 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002936#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002937 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2938 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002939#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002940 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002941#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002942#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002943#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002944
Fred Drake8cef4cf2000-06-28 16:40:38 +00002945 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002946
Fred Drake8cef4cf2000-06-28 16:40:38 +00002947}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002948#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002949
2950#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002951PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002952"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002953Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2954Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002955To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002956
2957static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002958posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002959{
2960 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002961
Fred Drake8cef4cf2000-06-28 16:40:38 +00002962 pid = forkpty(&master_fd, NULL, NULL, NULL);
2963 if (pid == -1)
2964 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002965 if (pid == 0)
2966 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002967 return Py_BuildValue("(ii)", pid, master_fd);
2968}
2969#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002970
Guido van Rossumad0ee831995-03-01 10:34:45 +00002971#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002973"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002975
Barry Warsaw53699e91996-12-10 23:23:01 +00002976static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002977posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002978{
Barry Warsaw53699e91996-12-10 23:23:01 +00002979 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002980}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002981#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002982
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002983
Guido van Rossumad0ee831995-03-01 10:34:45 +00002984#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002985PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002986"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002987Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002988
Barry Warsaw53699e91996-12-10 23:23:01 +00002989static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002990posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002991{
Barry Warsaw53699e91996-12-10 23:23:01 +00002992 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002993}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002994#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002996
Guido van Rossumad0ee831995-03-01 10:34:45 +00002997#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002998PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002999"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003000Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003001
Barry Warsaw53699e91996-12-10 23:23:01 +00003002static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003003posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003004{
Barry Warsaw53699e91996-12-10 23:23:01 +00003005 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003006}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003007#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003008
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003011"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013
Barry Warsaw53699e91996-12-10 23:23:01 +00003014static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003015posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003016{
Barry Warsaw53699e91996-12-10 23:23:01 +00003017 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003018}
3019
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003020
Fred Drakec9680921999-12-13 16:37:25 +00003021#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003022PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003023"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003024Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003025
3026static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003027posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003028{
3029 PyObject *result = NULL;
3030
Fred Drakec9680921999-12-13 16:37:25 +00003031#ifdef NGROUPS_MAX
3032#define MAX_GROUPS NGROUPS_MAX
3033#else
3034 /* defined to be 16 on Solaris7, so this should be a small number */
3035#define MAX_GROUPS 64
3036#endif
3037 gid_t grouplist[MAX_GROUPS];
3038 int n;
3039
3040 n = getgroups(MAX_GROUPS, grouplist);
3041 if (n < 0)
3042 posix_error();
3043 else {
3044 result = PyList_New(n);
3045 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003046 int i;
3047 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003048 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003049 if (o == NULL) {
3050 Py_DECREF(result);
3051 result = NULL;
3052 break;
3053 }
3054 PyList_SET_ITEM(result, i, o);
3055 }
3056 }
3057 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003058
Fred Drakec9680921999-12-13 16:37:25 +00003059 return result;
3060}
3061#endif
3062
Martin v. Löwis606edc12002-06-13 21:09:11 +00003063#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003064PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003065"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003066Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003067
3068static PyObject *
3069posix_getpgid(PyObject *self, PyObject *args)
3070{
3071 int pid, pgid;
3072 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3073 return NULL;
3074 pgid = getpgid(pid);
3075 if (pgid < 0)
3076 return posix_error();
3077 return PyInt_FromLong((long)pgid);
3078}
3079#endif /* HAVE_GETPGID */
3080
3081
Guido van Rossumb6775db1994-08-01 11:34:53 +00003082#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003083PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003084"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003085Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003086
Barry Warsaw53699e91996-12-10 23:23:01 +00003087static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003088posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003089{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003090#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003091 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003092#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003093 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003094#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003095}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003096#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003098
Guido van Rossumb6775db1994-08-01 11:34:53 +00003099#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003101"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003103
Barry Warsaw53699e91996-12-10 23:23:01 +00003104static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003105posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003106{
Guido van Rossum64933891994-10-20 21:56:42 +00003107#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003108 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003109#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003110 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003111#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003112 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003113 Py_INCREF(Py_None);
3114 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003115}
3116
Guido van Rossumb6775db1994-08-01 11:34:53 +00003117#endif /* HAVE_SETPGRP */
3118
Guido van Rossumad0ee831995-03-01 10:34:45 +00003119#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003120PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003121"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003122Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003123
Barry Warsaw53699e91996-12-10 23:23:01 +00003124static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003125posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003126{
Barry Warsaw53699e91996-12-10 23:23:01 +00003127 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003128}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003129#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003131
Fred Drake12c6e2d1999-12-14 21:25:03 +00003132#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003133PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003134"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003136
3137static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003138posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003139{
Neal Norwitze241ce82003-02-17 18:17:05 +00003140 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003141 char *name;
3142 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003143
Fred Drakea30680b2000-12-06 21:24:28 +00003144 errno = 0;
3145 name = getlogin();
3146 if (name == NULL) {
3147 if (errno)
3148 posix_error();
3149 else
3150 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003151 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003152 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003153 else
3154 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003155 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003156
Fred Drake12c6e2d1999-12-14 21:25:03 +00003157 return result;
3158}
3159#endif
3160
Guido van Rossumad0ee831995-03-01 10:34:45 +00003161#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003162PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003163"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165
Barry Warsaw53699e91996-12-10 23:23:01 +00003166static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003167posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003168{
Barry Warsaw53699e91996-12-10 23:23:01 +00003169 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003170}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003171#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173
Guido van Rossumad0ee831995-03-01 10:34:45 +00003174#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003175PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003176"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003177Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178
Barry Warsaw53699e91996-12-10 23:23:01 +00003179static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003180posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003181{
3182 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003183 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003184 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003185#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003186 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3187 APIRET rc;
3188 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003189 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003190
3191 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3192 APIRET rc;
3193 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003194 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003195
3196 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003197 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003198#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003199 if (kill(pid, sig) == -1)
3200 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003201#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003202 Py_INCREF(Py_None);
3203 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003204}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003205#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003206
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003207#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003209"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003211
3212static PyObject *
3213posix_killpg(PyObject *self, PyObject *args)
3214{
3215 int pgid, sig;
3216 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3217 return NULL;
3218 if (killpg(pgid, sig) == -1)
3219 return posix_error();
3220 Py_INCREF(Py_None);
3221 return Py_None;
3222}
3223#endif
3224
Guido van Rossumc0125471996-06-28 18:55:32 +00003225#ifdef HAVE_PLOCK
3226
3227#ifdef HAVE_SYS_LOCK_H
3228#include <sys/lock.h>
3229#endif
3230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003231PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003232"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003233Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003234
Barry Warsaw53699e91996-12-10 23:23:01 +00003235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003236posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003237{
3238 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003239 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003240 return NULL;
3241 if (plock(op) == -1)
3242 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003243 Py_INCREF(Py_None);
3244 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003245}
3246#endif
3247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003248
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003249#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003250PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003251"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003252Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003254#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003255#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003256static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003257async_system(const char *command)
3258{
3259 char *p, errormsg[256], args[1024];
3260 RESULTCODES rcodes;
3261 APIRET rc;
3262 char *shell = getenv("COMSPEC");
3263 if (!shell)
3264 shell = "cmd";
3265
3266 strcpy(args, shell);
3267 p = &args[ strlen(args)+1 ];
3268 strcpy(p, "/c ");
3269 strcat(p, command);
3270 p += strlen(p) + 1;
3271 *p = '\0';
3272
3273 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003274 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003275 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003276 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003277 &rcodes, shell);
3278 return rc;
3279}
3280
Guido van Rossumd48f2521997-12-05 22:19:34 +00003281static FILE *
3282popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003283{
3284 HFILE rhan, whan;
3285 FILE *retfd = NULL;
3286 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
3287
Guido van Rossumd48f2521997-12-05 22:19:34 +00003288 if (rc != NO_ERROR) {
3289 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003290 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003291 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003292
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003293 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
3294 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003295
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003296 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3297 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003298
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003299 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3300 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003301
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003302 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003303 }
3304
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003305 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3306 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003307
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003308 if (rc == NO_ERROR)
3309 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3310
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003311 close(oldfd); /* And Close Saved STDOUT Handle */
3312 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003313
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003314 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3315 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003316
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003317 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3318 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003319
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003320 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3321 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003322
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003323 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003324 }
3325
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003326 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3327 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003328
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003329 if (rc == NO_ERROR)
3330 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3331
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003332 close(oldfd); /* And Close Saved STDIN Handle */
3333 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003334
Guido van Rossumd48f2521997-12-05 22:19:34 +00003335 } else {
3336 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003337 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003338 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003339}
3340
3341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003342posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003343{
3344 char *name;
3345 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003346 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003347 FILE *fp;
3348 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003349 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003350 return NULL;
3351 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003352 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003353 Py_END_ALLOW_THREADS
3354 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003355 return os2_error(err);
3356
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003357 f = PyFile_FromFile(fp, name, mode, fclose);
3358 if (f != NULL)
3359 PyFile_SetBufSize(f, bufsize);
3360 return f;
3361}
3362
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003363#elif defined(PYCC_GCC)
3364
3365/* standard posix version of popen() support */
3366static PyObject *
3367posix_popen(PyObject *self, PyObject *args)
3368{
3369 char *name;
3370 char *mode = "r";
3371 int bufsize = -1;
3372 FILE *fp;
3373 PyObject *f;
3374 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3375 return NULL;
3376 Py_BEGIN_ALLOW_THREADS
3377 fp = popen(name, mode);
3378 Py_END_ALLOW_THREADS
3379 if (fp == NULL)
3380 return posix_error();
3381 f = PyFile_FromFile(fp, name, mode, pclose);
3382 if (f != NULL)
3383 PyFile_SetBufSize(f, bufsize);
3384 return f;
3385}
3386
3387/* fork() under OS/2 has lots'o'warts
3388 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3389 * most of this code is a ripoff of the win32 code, but using the
3390 * capabilities of EMX's C library routines
3391 */
3392
3393/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3394#define POPEN_1 1
3395#define POPEN_2 2
3396#define POPEN_3 3
3397#define POPEN_4 4
3398
3399static PyObject *_PyPopen(char *, int, int, int);
3400static int _PyPclose(FILE *file);
3401
3402/*
3403 * Internal dictionary mapping popen* file pointers to process handles,
3404 * for use when retrieving the process exit code. See _PyPclose() below
3405 * for more information on this dictionary's use.
3406 */
3407static PyObject *_PyPopenProcs = NULL;
3408
3409/* os2emx version of popen2()
3410 *
3411 * The result of this function is a pipe (file) connected to the
3412 * process's stdin, and a pipe connected to the process's stdout.
3413 */
3414
3415static PyObject *
3416os2emx_popen2(PyObject *self, PyObject *args)
3417{
3418 PyObject *f;
3419 int tm=0;
3420
3421 char *cmdstring;
3422 char *mode = "t";
3423 int bufsize = -1;
3424 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3425 return NULL;
3426
3427 if (*mode == 't')
3428 tm = O_TEXT;
3429 else if (*mode != 'b') {
3430 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3431 return NULL;
3432 } else
3433 tm = O_BINARY;
3434
3435 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3436
3437 return f;
3438}
3439
3440/*
3441 * Variation on os2emx.popen2
3442 *
3443 * The result of this function is 3 pipes - the process's stdin,
3444 * stdout and stderr
3445 */
3446
3447static PyObject *
3448os2emx_popen3(PyObject *self, PyObject *args)
3449{
3450 PyObject *f;
3451 int tm = 0;
3452
3453 char *cmdstring;
3454 char *mode = "t";
3455 int bufsize = -1;
3456 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3457 return NULL;
3458
3459 if (*mode == 't')
3460 tm = O_TEXT;
3461 else if (*mode != 'b') {
3462 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3463 return NULL;
3464 } else
3465 tm = O_BINARY;
3466
3467 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3468
3469 return f;
3470}
3471
3472/*
3473 * Variation on os2emx.popen2
3474 *
Tim Peters11b23062003-04-23 02:39:17 +00003475 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003476 * and stdout+stderr combined as a single pipe.
3477 */
3478
3479static PyObject *
3480os2emx_popen4(PyObject *self, PyObject *args)
3481{
3482 PyObject *f;
3483 int tm = 0;
3484
3485 char *cmdstring;
3486 char *mode = "t";
3487 int bufsize = -1;
3488 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3489 return NULL;
3490
3491 if (*mode == 't')
3492 tm = O_TEXT;
3493 else if (*mode != 'b') {
3494 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3495 return NULL;
3496 } else
3497 tm = O_BINARY;
3498
3499 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3500
3501 return f;
3502}
3503
3504/* a couple of structures for convenient handling of multiple
3505 * file handles and pipes
3506 */
3507struct file_ref
3508{
3509 int handle;
3510 int flags;
3511};
3512
3513struct pipe_ref
3514{
3515 int rd;
3516 int wr;
3517};
3518
3519/* The following code is derived from the win32 code */
3520
3521static PyObject *
3522_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3523{
3524 struct file_ref stdio[3];
3525 struct pipe_ref p_fd[3];
3526 FILE *p_s[3];
3527 int file_count, i, pipe_err, pipe_pid;
3528 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3529 PyObject *f, *p_f[3];
3530
3531 /* file modes for subsequent fdopen's on pipe handles */
3532 if (mode == O_TEXT)
3533 {
3534 rd_mode = "rt";
3535 wr_mode = "wt";
3536 }
3537 else
3538 {
3539 rd_mode = "rb";
3540 wr_mode = "wb";
3541 }
3542
3543 /* prepare shell references */
3544 if ((shell = getenv("EMXSHELL")) == NULL)
3545 if ((shell = getenv("COMSPEC")) == NULL)
3546 {
3547 errno = ENOENT;
3548 return posix_error();
3549 }
3550
3551 sh_name = _getname(shell);
3552 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3553 opt = "/c";
3554 else
3555 opt = "-c";
3556
3557 /* save current stdio fds + their flags, and set not inheritable */
3558 i = pipe_err = 0;
3559 while (pipe_err >= 0 && i < 3)
3560 {
3561 pipe_err = stdio[i].handle = dup(i);
3562 stdio[i].flags = fcntl(i, F_GETFD, 0);
3563 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3564 i++;
3565 }
3566 if (pipe_err < 0)
3567 {
3568 /* didn't get them all saved - clean up and bail out */
3569 int saved_err = errno;
3570 while (i-- > 0)
3571 {
3572 close(stdio[i].handle);
3573 }
3574 errno = saved_err;
3575 return posix_error();
3576 }
3577
3578 /* create pipe ends */
3579 file_count = 2;
3580 if (n == POPEN_3)
3581 file_count = 3;
3582 i = pipe_err = 0;
3583 while ((pipe_err == 0) && (i < file_count))
3584 pipe_err = pipe((int *)&p_fd[i++]);
3585 if (pipe_err < 0)
3586 {
3587 /* didn't get them all made - clean up and bail out */
3588 while (i-- > 0)
3589 {
3590 close(p_fd[i].wr);
3591 close(p_fd[i].rd);
3592 }
3593 errno = EPIPE;
3594 return posix_error();
3595 }
3596
3597 /* change the actual standard IO streams over temporarily,
3598 * making the retained pipe ends non-inheritable
3599 */
3600 pipe_err = 0;
3601
3602 /* - stdin */
3603 if (dup2(p_fd[0].rd, 0) == 0)
3604 {
3605 close(p_fd[0].rd);
3606 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3607 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3608 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3609 {
3610 close(p_fd[0].wr);
3611 pipe_err = -1;
3612 }
3613 }
3614 else
3615 {
3616 pipe_err = -1;
3617 }
3618
3619 /* - stdout */
3620 if (pipe_err == 0)
3621 {
3622 if (dup2(p_fd[1].wr, 1) == 1)
3623 {
3624 close(p_fd[1].wr);
3625 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3626 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3627 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3628 {
3629 close(p_fd[1].rd);
3630 pipe_err = -1;
3631 }
3632 }
3633 else
3634 {
3635 pipe_err = -1;
3636 }
3637 }
3638
3639 /* - stderr, as required */
3640 if (pipe_err == 0)
3641 switch (n)
3642 {
3643 case POPEN_3:
3644 {
3645 if (dup2(p_fd[2].wr, 2) == 2)
3646 {
3647 close(p_fd[2].wr);
3648 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3649 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3650 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3651 {
3652 close(p_fd[2].rd);
3653 pipe_err = -1;
3654 }
3655 }
3656 else
3657 {
3658 pipe_err = -1;
3659 }
3660 break;
3661 }
3662
3663 case POPEN_4:
3664 {
3665 if (dup2(1, 2) != 2)
3666 {
3667 pipe_err = -1;
3668 }
3669 break;
3670 }
3671 }
3672
3673 /* spawn the child process */
3674 if (pipe_err == 0)
3675 {
3676 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3677 if (pipe_pid == -1)
3678 {
3679 pipe_err = -1;
3680 }
3681 else
3682 {
3683 /* save the PID into the FILE structure
3684 * NOTE: this implementation doesn't actually
3685 * take advantage of this, but do it for
3686 * completeness - AIM Apr01
3687 */
3688 for (i = 0; i < file_count; i++)
3689 p_s[i]->_pid = pipe_pid;
3690 }
3691 }
3692
3693 /* reset standard IO to normal */
3694 for (i = 0; i < 3; i++)
3695 {
3696 dup2(stdio[i].handle, i);
3697 fcntl(i, F_SETFD, stdio[i].flags);
3698 close(stdio[i].handle);
3699 }
3700
3701 /* if any remnant problems, clean up and bail out */
3702 if (pipe_err < 0)
3703 {
3704 for (i = 0; i < 3; i++)
3705 {
3706 close(p_fd[i].rd);
3707 close(p_fd[i].wr);
3708 }
3709 errno = EPIPE;
3710 return posix_error_with_filename(cmdstring);
3711 }
3712
3713 /* build tuple of file objects to return */
3714 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3715 PyFile_SetBufSize(p_f[0], bufsize);
3716 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3717 PyFile_SetBufSize(p_f[1], bufsize);
3718 if (n == POPEN_3)
3719 {
3720 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3721 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003722 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003723 }
3724 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003725 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003726
3727 /*
3728 * Insert the files we've created into the process dictionary
3729 * all referencing the list with the process handle and the
3730 * initial number of files (see description below in _PyPclose).
3731 * Since if _PyPclose later tried to wait on a process when all
3732 * handles weren't closed, it could create a deadlock with the
3733 * child, we spend some energy here to try to ensure that we
3734 * either insert all file handles into the dictionary or none
3735 * at all. It's a little clumsy with the various popen modes
3736 * and variable number of files involved.
3737 */
3738 if (!_PyPopenProcs)
3739 {
3740 _PyPopenProcs = PyDict_New();
3741 }
3742
3743 if (_PyPopenProcs)
3744 {
3745 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3746 int ins_rc[3];
3747
3748 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3749 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3750
3751 procObj = PyList_New(2);
3752 pidObj = PyInt_FromLong((long) pipe_pid);
3753 intObj = PyInt_FromLong((long) file_count);
3754
3755 if (procObj && pidObj && intObj)
3756 {
3757 PyList_SetItem(procObj, 0, pidObj);
3758 PyList_SetItem(procObj, 1, intObj);
3759
3760 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3761 if (fileObj[0])
3762 {
3763 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3764 fileObj[0],
3765 procObj);
3766 }
3767 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3768 if (fileObj[1])
3769 {
3770 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3771 fileObj[1],
3772 procObj);
3773 }
3774 if (file_count >= 3)
3775 {
3776 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3777 if (fileObj[2])
3778 {
3779 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3780 fileObj[2],
3781 procObj);
3782 }
3783 }
3784
3785 if (ins_rc[0] < 0 || !fileObj[0] ||
3786 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3787 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3788 {
3789 /* Something failed - remove any dictionary
3790 * entries that did make it.
3791 */
3792 if (!ins_rc[0] && fileObj[0])
3793 {
3794 PyDict_DelItem(_PyPopenProcs,
3795 fileObj[0]);
3796 }
3797 if (!ins_rc[1] && fileObj[1])
3798 {
3799 PyDict_DelItem(_PyPopenProcs,
3800 fileObj[1]);
3801 }
3802 if (!ins_rc[2] && fileObj[2])
3803 {
3804 PyDict_DelItem(_PyPopenProcs,
3805 fileObj[2]);
3806 }
3807 }
3808 }
Tim Peters11b23062003-04-23 02:39:17 +00003809
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003810 /*
3811 * Clean up our localized references for the dictionary keys
3812 * and value since PyDict_SetItem will Py_INCREF any copies
3813 * that got placed in the dictionary.
3814 */
3815 Py_XDECREF(procObj);
3816 Py_XDECREF(fileObj[0]);
3817 Py_XDECREF(fileObj[1]);
3818 Py_XDECREF(fileObj[2]);
3819 }
3820
3821 /* Child is launched. */
3822 return f;
3823}
3824
3825/*
3826 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3827 * exit code for the child process and return as a result of the close.
3828 *
3829 * This function uses the _PyPopenProcs dictionary in order to map the
3830 * input file pointer to information about the process that was
3831 * originally created by the popen* call that created the file pointer.
3832 * The dictionary uses the file pointer as a key (with one entry
3833 * inserted for each file returned by the original popen* call) and a
3834 * single list object as the value for all files from a single call.
3835 * The list object contains the Win32 process handle at [0], and a file
3836 * count at [1], which is initialized to the total number of file
3837 * handles using that list.
3838 *
3839 * This function closes whichever handle it is passed, and decrements
3840 * the file count in the dictionary for the process handle pointed to
3841 * by this file. On the last close (when the file count reaches zero),
3842 * this function will wait for the child process and then return its
3843 * exit code as the result of the close() operation. This permits the
3844 * files to be closed in any order - it is always the close() of the
3845 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003846 *
3847 * NOTE: This function is currently called with the GIL released.
3848 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003849 */
3850
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003851static int _PyPclose(FILE *file)
3852{
3853 int result;
3854 int exit_code;
3855 int pipe_pid;
3856 PyObject *procObj, *pidObj, *intObj, *fileObj;
3857 int file_count;
3858#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003859 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003860#endif
3861
3862 /* Close the file handle first, to ensure it can't block the
3863 * child from exiting if it's the last handle.
3864 */
3865 result = fclose(file);
3866
3867#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003868 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003869#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003870 if (_PyPopenProcs)
3871 {
3872 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3873 (procObj = PyDict_GetItem(_PyPopenProcs,
3874 fileObj)) != NULL &&
3875 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3876 (intObj = PyList_GetItem(procObj,1)) != NULL)
3877 {
3878 pipe_pid = (int) PyInt_AsLong(pidObj);
3879 file_count = (int) PyInt_AsLong(intObj);
3880
3881 if (file_count > 1)
3882 {
3883 /* Still other files referencing process */
3884 file_count--;
3885 PyList_SetItem(procObj,1,
3886 PyInt_FromLong((long) file_count));
3887 }
3888 else
3889 {
3890 /* Last file for this process */
3891 if (result != EOF &&
3892 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3893 {
3894 /* extract exit status */
3895 if (WIFEXITED(exit_code))
3896 {
3897 result = WEXITSTATUS(exit_code);
3898 }
3899 else
3900 {
3901 errno = EPIPE;
3902 result = -1;
3903 }
3904 }
3905 else
3906 {
3907 /* Indicate failure - this will cause the file object
3908 * to raise an I/O error and translate the last
3909 * error code from errno. We do have a problem with
3910 * last errors that overlap the normal errno table,
3911 * but that's a consistent problem with the file object.
3912 */
3913 result = -1;
3914 }
3915 }
3916
3917 /* Remove this file pointer from dictionary */
3918 PyDict_DelItem(_PyPopenProcs, fileObj);
3919
3920 if (PyDict_Size(_PyPopenProcs) == 0)
3921 {
3922 Py_DECREF(_PyPopenProcs);
3923 _PyPopenProcs = NULL;
3924 }
3925
3926 } /* if object retrieval ok */
3927
3928 Py_XDECREF(fileObj);
3929 } /* if _PyPopenProcs */
3930
3931#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003932 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003933#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003934 return result;
3935}
3936
3937#endif /* PYCC_??? */
3938
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003939#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003940
3941/*
3942 * Portable 'popen' replacement for Win32.
3943 *
3944 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3945 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003946 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003947 */
3948
3949#include <malloc.h>
3950#include <io.h>
3951#include <fcntl.h>
3952
3953/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3954#define POPEN_1 1
3955#define POPEN_2 2
3956#define POPEN_3 3
3957#define POPEN_4 4
3958
3959static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003960static int _PyPclose(FILE *file);
3961
3962/*
3963 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003964 * for use when retrieving the process exit code. See _PyPclose() below
3965 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003966 */
3967static PyObject *_PyPopenProcs = NULL;
3968
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003969
3970/* popen that works from a GUI.
3971 *
3972 * The result of this function is a pipe (file) connected to the
3973 * processes stdin or stdout, depending on the requested mode.
3974 */
3975
3976static PyObject *
3977posix_popen(PyObject *self, PyObject *args)
3978{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003979 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003980 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003981
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003982 char *cmdstring;
3983 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003984 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003985 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003986 return NULL;
3987
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003988 if (*mode == 'r')
3989 tm = _O_RDONLY;
3990 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003991 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003992 return NULL;
3993 } else
3994 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003995
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003996 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003997 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003998 return NULL;
3999 }
4000
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004001 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004002 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004003 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004004 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004005 else
4006 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4007
4008 return f;
4009}
4010
4011/* Variation on win32pipe.popen
4012 *
4013 * The result of this function is a pipe (file) connected to the
4014 * process's stdin, and a pipe connected to the process's stdout.
4015 */
4016
4017static PyObject *
4018win32_popen2(PyObject *self, PyObject *args)
4019{
4020 PyObject *f;
4021 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004022
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004023 char *cmdstring;
4024 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004025 int bufsize = -1;
4026 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004027 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004028
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004029 if (*mode == 't')
4030 tm = _O_TEXT;
4031 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004032 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004033 return NULL;
4034 } else
4035 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004036
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004037 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004038 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004039 return NULL;
4040 }
4041
4042 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004043
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004044 return f;
4045}
4046
4047/*
4048 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004049 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004050 * The result of this function is 3 pipes - the process's stdin,
4051 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004052 */
4053
4054static PyObject *
4055win32_popen3(PyObject *self, PyObject *args)
4056{
4057 PyObject *f;
4058 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004059
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004060 char *cmdstring;
4061 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004062 int bufsize = -1;
4063 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004064 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004065
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004066 if (*mode == 't')
4067 tm = _O_TEXT;
4068 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004069 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004070 return NULL;
4071 } else
4072 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004073
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004074 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004075 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004076 return NULL;
4077 }
4078
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004079 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004080
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004081 return f;
4082}
4083
4084/*
4085 * Variation on win32pipe.popen
4086 *
Tim Peters5aa91602002-01-30 05:46:57 +00004087 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004088 * and stdout+stderr combined as a single pipe.
4089 */
4090
4091static PyObject *
4092win32_popen4(PyObject *self, PyObject *args)
4093{
4094 PyObject *f;
4095 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004096
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004097 char *cmdstring;
4098 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004099 int bufsize = -1;
4100 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004101 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004102
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004103 if (*mode == 't')
4104 tm = _O_TEXT;
4105 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004106 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004107 return NULL;
4108 } else
4109 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004110
4111 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004112 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004113 return NULL;
4114 }
4115
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004116 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004117
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004118 return f;
4119}
4120
Mark Hammond08501372001-01-31 07:30:29 +00004121static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004122_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004123 HANDLE hStdin,
4124 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004125 HANDLE hStderr,
4126 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004127{
4128 PROCESS_INFORMATION piProcInfo;
4129 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004130 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004131 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004132 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004133 int i;
4134 int x;
4135
4136 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004137 char *comshell;
4138
Tim Peters92e4dd82002-10-05 01:47:34 +00004139 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004140 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4141 return x;
Tim Peters402d5982001-08-27 06:37:48 +00004142
4143 /* Explicitly check if we are using COMMAND.COM. If we are
4144 * then use the w9xpopen hack.
4145 */
4146 comshell = s1 + x;
4147 while (comshell >= s1 && *comshell != '\\')
4148 --comshell;
4149 ++comshell;
4150
4151 if (GetVersion() < 0x80000000 &&
4152 _stricmp(comshell, "command.com") != 0) {
4153 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004154 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004155 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004156 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004157 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004158 }
4159 else {
4160 /*
Tim Peters402d5982001-08-27 06:37:48 +00004161 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4162 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004163 */
Mark Hammond08501372001-01-31 07:30:29 +00004164 char modulepath[_MAX_PATH];
4165 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004166 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4167 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004168 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004169 x = i+1;
4170 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004171 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004172 strncat(modulepath,
4173 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004174 (sizeof(modulepath)/sizeof(modulepath[0]))
4175 -strlen(modulepath));
4176 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004177 /* Eeek - file-not-found - possibly an embedding
4178 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004179 */
Tim Peters5aa91602002-01-30 05:46:57 +00004180 strncpy(modulepath,
4181 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004182 sizeof(modulepath)/sizeof(modulepath[0]));
4183 if (modulepath[strlen(modulepath)-1] != '\\')
4184 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004185 strncat(modulepath,
4186 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004187 (sizeof(modulepath)/sizeof(modulepath[0]))
4188 -strlen(modulepath));
4189 /* No where else to look - raise an easily identifiable
4190 error, rather than leaving Windows to report
4191 "file not found" - as the user is probably blissfully
4192 unaware this shim EXE is used, and it will confuse them.
4193 (well, it confused me for a while ;-)
4194 */
4195 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004196 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004197 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004198 "for popen to work with your shell "
4199 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004200 szConsoleSpawn);
4201 return FALSE;
4202 }
4203 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004204 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004205 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004206 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004207
Tim Peters92e4dd82002-10-05 01:47:34 +00004208 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004209 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004210 /* To maintain correct argument passing semantics,
4211 we pass the command-line as it stands, and allow
4212 quoting to be applied. w9xpopen.exe will then
4213 use its argv vector, and re-quote the necessary
4214 args for the ultimate child process.
4215 */
Tim Peters75cdad52001-11-28 22:07:30 +00004216 PyOS_snprintf(
4217 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004218 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004219 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004220 s1,
4221 s3,
4222 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004223 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004224 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004225 dialog:
4226 "Your program accessed mem currently in use at xxx"
4227 and a hopeful warning about the stability of your
4228 system.
4229 Cost is Ctrl+C wont kill children, but anyone
4230 who cares can have a go!
4231 */
4232 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004233 }
4234 }
4235
4236 /* Could be an else here to try cmd.exe / command.com in the path
4237 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004238 else {
Tim Peters402d5982001-08-27 06:37:48 +00004239 PyErr_SetString(PyExc_RuntimeError,
4240 "Cannot locate a COMSPEC environment variable to "
4241 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004242 return FALSE;
4243 }
Tim Peters5aa91602002-01-30 05:46:57 +00004244
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004245 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4246 siStartInfo.cb = sizeof(STARTUPINFO);
4247 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4248 siStartInfo.hStdInput = hStdin;
4249 siStartInfo.hStdOutput = hStdout;
4250 siStartInfo.hStdError = hStderr;
4251 siStartInfo.wShowWindow = SW_HIDE;
4252
4253 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004254 s2,
4255 NULL,
4256 NULL,
4257 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004258 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004259 NULL,
4260 NULL,
4261 &siStartInfo,
4262 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004263 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004264 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004265
Mark Hammondb37a3732000-08-14 04:47:33 +00004266 /* Return process handle */
4267 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004268 return TRUE;
4269 }
Tim Peters402d5982001-08-27 06:37:48 +00004270 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004271 return FALSE;
4272}
4273
4274/* The following code is based off of KB: Q190351 */
4275
4276static PyObject *
4277_PyPopen(char *cmdstring, int mode, int n)
4278{
4279 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4280 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004281 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004282
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004283 SECURITY_ATTRIBUTES saAttr;
4284 BOOL fSuccess;
4285 int fd1, fd2, fd3;
4286 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004287 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004288 PyObject *f;
4289
4290 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4291 saAttr.bInheritHandle = TRUE;
4292 saAttr.lpSecurityDescriptor = NULL;
4293
4294 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4295 return win32_error("CreatePipe", NULL);
4296
4297 /* Create new output read handle and the input write handle. Set
4298 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004299 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004300 * being created. */
4301 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004302 GetCurrentProcess(), &hChildStdinWrDup, 0,
4303 FALSE,
4304 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004305 if (!fSuccess)
4306 return win32_error("DuplicateHandle", NULL);
4307
4308 /* Close the inheritable version of ChildStdin
4309 that we're using. */
4310 CloseHandle(hChildStdinWr);
4311
4312 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4313 return win32_error("CreatePipe", NULL);
4314
4315 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004316 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4317 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004318 if (!fSuccess)
4319 return win32_error("DuplicateHandle", NULL);
4320
4321 /* Close the inheritable version of ChildStdout
4322 that we're using. */
4323 CloseHandle(hChildStdoutRd);
4324
4325 if (n != POPEN_4) {
4326 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4327 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004328 fSuccess = DuplicateHandle(GetCurrentProcess(),
4329 hChildStderrRd,
4330 GetCurrentProcess(),
4331 &hChildStderrRdDup, 0,
4332 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004333 if (!fSuccess)
4334 return win32_error("DuplicateHandle", NULL);
4335 /* Close the inheritable version of ChildStdErr that we're using. */
4336 CloseHandle(hChildStderrRd);
4337 }
Tim Peters5aa91602002-01-30 05:46:57 +00004338
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004339 switch (n) {
4340 case POPEN_1:
4341 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4342 case _O_WRONLY | _O_TEXT:
4343 /* Case for writing to child Stdin in text mode. */
4344 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4345 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004346 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004347 PyFile_SetBufSize(f, 0);
4348 /* We don't care about these pipes anymore, so close them. */
4349 CloseHandle(hChildStdoutRdDup);
4350 CloseHandle(hChildStderrRdDup);
4351 break;
4352
4353 case _O_RDONLY | _O_TEXT:
4354 /* Case for reading from child Stdout in text mode. */
4355 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4356 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004357 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004358 PyFile_SetBufSize(f, 0);
4359 /* We don't care about these pipes anymore, so close them. */
4360 CloseHandle(hChildStdinWrDup);
4361 CloseHandle(hChildStderrRdDup);
4362 break;
4363
4364 case _O_RDONLY | _O_BINARY:
4365 /* Case for readinig from child Stdout in binary mode. */
4366 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4367 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004368 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004369 PyFile_SetBufSize(f, 0);
4370 /* We don't care about these pipes anymore, so close them. */
4371 CloseHandle(hChildStdinWrDup);
4372 CloseHandle(hChildStderrRdDup);
4373 break;
4374
4375 case _O_WRONLY | _O_BINARY:
4376 /* Case for writing to child Stdin in binary mode. */
4377 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4378 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004379 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004380 PyFile_SetBufSize(f, 0);
4381 /* We don't care about these pipes anymore, so close them. */
4382 CloseHandle(hChildStdoutRdDup);
4383 CloseHandle(hChildStderrRdDup);
4384 break;
4385 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004386 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004387 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004388
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004389 case POPEN_2:
4390 case POPEN_4:
4391 {
4392 char *m1, *m2;
4393 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004394
Tim Peters7dca21e2002-08-19 00:42:29 +00004395 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004396 m1 = "r";
4397 m2 = "w";
4398 } else {
4399 m1 = "rb";
4400 m2 = "wb";
4401 }
4402
4403 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4404 f1 = _fdopen(fd1, m2);
4405 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4406 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004407 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004408 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004409 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004410 PyFile_SetBufSize(p2, 0);
4411
4412 if (n != 4)
4413 CloseHandle(hChildStderrRdDup);
4414
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004415 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004416 Py_XDECREF(p1);
4417 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004418 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004419 break;
4420 }
Tim Peters5aa91602002-01-30 05:46:57 +00004421
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004422 case POPEN_3:
4423 {
4424 char *m1, *m2;
4425 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004426
Tim Peters7dca21e2002-08-19 00:42:29 +00004427 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004428 m1 = "r";
4429 m2 = "w";
4430 } else {
4431 m1 = "rb";
4432 m2 = "wb";
4433 }
4434
4435 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4436 f1 = _fdopen(fd1, m2);
4437 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4438 f2 = _fdopen(fd2, m1);
4439 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4440 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004441 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004442 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4443 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004444 PyFile_SetBufSize(p1, 0);
4445 PyFile_SetBufSize(p2, 0);
4446 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004447 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004448 Py_XDECREF(p1);
4449 Py_XDECREF(p2);
4450 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004451 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004452 break;
4453 }
4454 }
4455
4456 if (n == POPEN_4) {
4457 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004458 hChildStdinRd,
4459 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004460 hChildStdoutWr,
4461 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004462 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004463 }
4464 else {
4465 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004466 hChildStdinRd,
4467 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004468 hChildStderrWr,
4469 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004470 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004471 }
4472
Mark Hammondb37a3732000-08-14 04:47:33 +00004473 /*
4474 * Insert the files we've created into the process dictionary
4475 * all referencing the list with the process handle and the
4476 * initial number of files (see description below in _PyPclose).
4477 * Since if _PyPclose later tried to wait on a process when all
4478 * handles weren't closed, it could create a deadlock with the
4479 * child, we spend some energy here to try to ensure that we
4480 * either insert all file handles into the dictionary or none
4481 * at all. It's a little clumsy with the various popen modes
4482 * and variable number of files involved.
4483 */
4484 if (!_PyPopenProcs) {
4485 _PyPopenProcs = PyDict_New();
4486 }
4487
4488 if (_PyPopenProcs) {
4489 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4490 int ins_rc[3];
4491
4492 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4493 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4494
4495 procObj = PyList_New(2);
4496 hProcessObj = PyLong_FromVoidPtr(hProcess);
4497 intObj = PyInt_FromLong(file_count);
4498
4499 if (procObj && hProcessObj && intObj) {
4500 PyList_SetItem(procObj,0,hProcessObj);
4501 PyList_SetItem(procObj,1,intObj);
4502
4503 fileObj[0] = PyLong_FromVoidPtr(f1);
4504 if (fileObj[0]) {
4505 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4506 fileObj[0],
4507 procObj);
4508 }
4509 if (file_count >= 2) {
4510 fileObj[1] = PyLong_FromVoidPtr(f2);
4511 if (fileObj[1]) {
4512 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4513 fileObj[1],
4514 procObj);
4515 }
4516 }
4517 if (file_count >= 3) {
4518 fileObj[2] = PyLong_FromVoidPtr(f3);
4519 if (fileObj[2]) {
4520 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4521 fileObj[2],
4522 procObj);
4523 }
4524 }
4525
4526 if (ins_rc[0] < 0 || !fileObj[0] ||
4527 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4528 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4529 /* Something failed - remove any dictionary
4530 * entries that did make it.
4531 */
4532 if (!ins_rc[0] && fileObj[0]) {
4533 PyDict_DelItem(_PyPopenProcs,
4534 fileObj[0]);
4535 }
4536 if (!ins_rc[1] && fileObj[1]) {
4537 PyDict_DelItem(_PyPopenProcs,
4538 fileObj[1]);
4539 }
4540 if (!ins_rc[2] && fileObj[2]) {
4541 PyDict_DelItem(_PyPopenProcs,
4542 fileObj[2]);
4543 }
4544 }
4545 }
Tim Peters5aa91602002-01-30 05:46:57 +00004546
Mark Hammondb37a3732000-08-14 04:47:33 +00004547 /*
4548 * Clean up our localized references for the dictionary keys
4549 * and value since PyDict_SetItem will Py_INCREF any copies
4550 * that got placed in the dictionary.
4551 */
4552 Py_XDECREF(procObj);
4553 Py_XDECREF(fileObj[0]);
4554 Py_XDECREF(fileObj[1]);
4555 Py_XDECREF(fileObj[2]);
4556 }
4557
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004558 /* Child is launched. Close the parents copy of those pipe
4559 * handles that only the child should have open. You need to
4560 * make sure that no handles to the write end of the output pipe
4561 * are maintained in this process or else the pipe will not close
4562 * when the child process exits and the ReadFile will hang. */
4563
4564 if (!CloseHandle(hChildStdinRd))
4565 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004566
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004567 if (!CloseHandle(hChildStdoutWr))
4568 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004569
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004570 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4571 return win32_error("CloseHandle", NULL);
4572
4573 return f;
4574}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004575
4576/*
4577 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4578 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004579 *
4580 * This function uses the _PyPopenProcs dictionary in order to map the
4581 * input file pointer to information about the process that was
4582 * originally created by the popen* call that created the file pointer.
4583 * The dictionary uses the file pointer as a key (with one entry
4584 * inserted for each file returned by the original popen* call) and a
4585 * single list object as the value for all files from a single call.
4586 * The list object contains the Win32 process handle at [0], and a file
4587 * count at [1], which is initialized to the total number of file
4588 * handles using that list.
4589 *
4590 * This function closes whichever handle it is passed, and decrements
4591 * the file count in the dictionary for the process handle pointed to
4592 * by this file. On the last close (when the file count reaches zero),
4593 * this function will wait for the child process and then return its
4594 * exit code as the result of the close() operation. This permits the
4595 * files to be closed in any order - it is always the close() of the
4596 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004597 *
4598 * NOTE: This function is currently called with the GIL released.
4599 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004600 */
Tim Peters736aa322000-09-01 06:51:24 +00004601
Fredrik Lundh56055a42000-07-23 19:47:12 +00004602static int _PyPclose(FILE *file)
4603{
Fredrik Lundh20318932000-07-26 17:29:12 +00004604 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004605 DWORD exit_code;
4606 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004607 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4608 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004609#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004610 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004611#endif
4612
Fredrik Lundh20318932000-07-26 17:29:12 +00004613 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004614 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004615 */
4616 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004617#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004618 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004619#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004620 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004621 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4622 (procObj = PyDict_GetItem(_PyPopenProcs,
4623 fileObj)) != NULL &&
4624 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4625 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4626
4627 hProcess = PyLong_AsVoidPtr(hProcessObj);
4628 file_count = PyInt_AsLong(intObj);
4629
4630 if (file_count > 1) {
4631 /* Still other files referencing process */
4632 file_count--;
4633 PyList_SetItem(procObj,1,
4634 PyInt_FromLong(file_count));
4635 } else {
4636 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004637 if (result != EOF &&
4638 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4639 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004640 /* Possible truncation here in 16-bit environments, but
4641 * real exit codes are just the lower byte in any event.
4642 */
4643 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004644 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004645 /* Indicate failure - this will cause the file object
4646 * to raise an I/O error and translate the last Win32
4647 * error code from errno. We do have a problem with
4648 * last errors that overlap the normal errno table,
4649 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004650 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004651 if (result != EOF) {
4652 /* If the error wasn't from the fclose(), then
4653 * set errno for the file object error handling.
4654 */
4655 errno = GetLastError();
4656 }
4657 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004658 }
4659
4660 /* Free up the native handle at this point */
4661 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004662 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004663
Mark Hammondb37a3732000-08-14 04:47:33 +00004664 /* Remove this file pointer from dictionary */
4665 PyDict_DelItem(_PyPopenProcs, fileObj);
4666
4667 if (PyDict_Size(_PyPopenProcs) == 0) {
4668 Py_DECREF(_PyPopenProcs);
4669 _PyPopenProcs = NULL;
4670 }
4671
4672 } /* if object retrieval ok */
4673
4674 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004675 } /* if _PyPopenProcs */
4676
Tim Peters736aa322000-09-01 06:51:24 +00004677#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004678 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004679#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004680 return result;
4681}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004682
4683#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004685posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004686{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004687 char *name;
4688 char *mode = "r";
4689 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004690 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004691 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004692 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004693 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004694 /* Strip mode of binary or text modifiers */
4695 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4696 mode = "r";
4697 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4698 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004700 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004701 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004702 if (fp == NULL)
4703 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004704 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004705 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004706 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004707 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004708}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004709
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004710#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004711#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004713
Guido van Rossumb6775db1994-08-01 11:34:53 +00004714#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004715PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004716"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004717Set the current process's user id.");
4718
Barry Warsaw53699e91996-12-10 23:23:01 +00004719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004720posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004721{
4722 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004723 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004724 return NULL;
4725 if (setuid(uid) < 0)
4726 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004727 Py_INCREF(Py_None);
4728 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004729}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004730#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004732
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004733#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004734PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004735"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004736Set the current process's effective user id.");
4737
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004738static PyObject *
4739posix_seteuid (PyObject *self, PyObject *args)
4740{
4741 int euid;
4742 if (!PyArg_ParseTuple(args, "i", &euid)) {
4743 return NULL;
4744 } else if (seteuid(euid) < 0) {
4745 return posix_error();
4746 } else {
4747 Py_INCREF(Py_None);
4748 return Py_None;
4749 }
4750}
4751#endif /* HAVE_SETEUID */
4752
4753#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004754PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004755"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004756Set the current process's effective group id.");
4757
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004758static PyObject *
4759posix_setegid (PyObject *self, PyObject *args)
4760{
4761 int egid;
4762 if (!PyArg_ParseTuple(args, "i", &egid)) {
4763 return NULL;
4764 } else if (setegid(egid) < 0) {
4765 return posix_error();
4766 } else {
4767 Py_INCREF(Py_None);
4768 return Py_None;
4769 }
4770}
4771#endif /* HAVE_SETEGID */
4772
4773#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004774PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004775"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776Set the current process's real and effective user ids.");
4777
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004778static PyObject *
4779posix_setreuid (PyObject *self, PyObject *args)
4780{
4781 int ruid, euid;
4782 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4783 return NULL;
4784 } else if (setreuid(ruid, euid) < 0) {
4785 return posix_error();
4786 } else {
4787 Py_INCREF(Py_None);
4788 return Py_None;
4789 }
4790}
4791#endif /* HAVE_SETREUID */
4792
4793#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004794PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004795"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004796Set the current process's real and effective group ids.");
4797
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004798static PyObject *
4799posix_setregid (PyObject *self, PyObject *args)
4800{
4801 int rgid, egid;
4802 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4803 return NULL;
4804 } else if (setregid(rgid, egid) < 0) {
4805 return posix_error();
4806 } else {
4807 Py_INCREF(Py_None);
4808 return Py_None;
4809 }
4810}
4811#endif /* HAVE_SETREGID */
4812
Guido van Rossumb6775db1994-08-01 11:34:53 +00004813#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004815"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004817
Barry Warsaw53699e91996-12-10 23:23:01 +00004818static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004819posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004820{
4821 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004822 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004823 return NULL;
4824 if (setgid(gid) < 0)
4825 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004826 Py_INCREF(Py_None);
4827 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004828}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004829#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004830
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004831#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004832PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004833"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004834Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004835
4836static PyObject *
4837posix_setgroups(PyObject *self, PyObject *args)
4838{
4839 PyObject *groups;
4840 int i, len;
4841 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004842
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004843 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4844 return NULL;
4845 if (!PySequence_Check(groups)) {
4846 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4847 return NULL;
4848 }
4849 len = PySequence_Size(groups);
4850 if (len > MAX_GROUPS) {
4851 PyErr_SetString(PyExc_ValueError, "too many groups");
4852 return NULL;
4853 }
4854 for(i = 0; i < len; i++) {
4855 PyObject *elem;
4856 elem = PySequence_GetItem(groups, i);
4857 if (!elem)
4858 return NULL;
4859 if (!PyInt_Check(elem)) {
4860 PyErr_SetString(PyExc_TypeError,
4861 "groups must be integers");
4862 Py_DECREF(elem);
4863 return NULL;
4864 }
4865 /* XXX: check that value fits into gid_t. */
4866 grouplist[i] = PyInt_AsLong(elem);
4867 Py_DECREF(elem);
4868 }
4869
4870 if (setgroups(len, grouplist) < 0)
4871 return posix_error();
4872 Py_INCREF(Py_None);
4873 return Py_None;
4874}
4875#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Guido van Rossumb6775db1994-08-01 11:34:53 +00004877#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004878PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004879"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004880Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004881
Barry Warsaw53699e91996-12-10 23:23:01 +00004882static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004883posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004884{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004885 int pid, options;
4886#ifdef UNION_WAIT
4887 union wait status;
4888#define status_i (status.w_status)
4889#else
4890 int status;
4891#define status_i status
4892#endif
4893 status_i = 0;
4894
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004895 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004896 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004897 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004898 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004899 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004900 if (pid == -1)
4901 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004902 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004903 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004904}
4905
Tim Petersab034fa2002-02-01 11:27:43 +00004906#elif defined(HAVE_CWAIT)
4907
4908/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004909PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004910"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004911"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004912
4913static PyObject *
4914posix_waitpid(PyObject *self, PyObject *args)
4915{
4916 int pid, options;
4917 int status;
4918
4919 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4920 return NULL;
4921 Py_BEGIN_ALLOW_THREADS
4922 pid = _cwait(&status, pid, options);
4923 Py_END_ALLOW_THREADS
4924 if (pid == -1)
4925 return posix_error();
4926 else
4927 /* shift the status left a byte so this is more like the
4928 POSIX waitpid */
4929 return Py_BuildValue("ii", pid, status << 8);
4930}
4931#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004932
Guido van Rossumad0ee831995-03-01 10:34:45 +00004933#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004934PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004935"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004936Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004937
Barry Warsaw53699e91996-12-10 23:23:01 +00004938static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004939posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004940{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004941 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004942#ifdef UNION_WAIT
4943 union wait status;
4944#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004945#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004946 int status;
4947#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004948#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004949
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004950 status_i = 0;
4951 Py_BEGIN_ALLOW_THREADS
4952 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004953 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004954 if (pid == -1)
4955 return posix_error();
4956 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004957 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004958#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004959}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004960#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004961
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004963PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004964"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004966
Barry Warsaw53699e91996-12-10 23:23:01 +00004967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004968posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004969{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004970#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004971 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004972#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004973#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004974 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004975#else
4976 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4977#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004978#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004979}
4980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004981
Guido van Rossumb6775db1994-08-01 11:34:53 +00004982#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004983PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004984"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004985Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004986
Barry Warsaw53699e91996-12-10 23:23:01 +00004987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004988posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004989{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004990 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004991 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004992 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004993 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004994 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004995 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004996 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004997 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004998 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004999 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005000 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005001}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005002#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005003
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005004
Guido van Rossumb6775db1994-08-01 11:34:53 +00005005#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005006PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005007"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005008Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005009
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005010static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005011posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005012{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005013 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005014}
5015#endif /* HAVE_SYMLINK */
5016
5017
5018#ifdef HAVE_TIMES
5019#ifndef HZ
5020#define HZ 60 /* Universal constant :-) */
5021#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005022
Guido van Rossumd48f2521997-12-05 22:19:34 +00005023#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5024static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005025system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005026{
5027 ULONG value = 0;
5028
5029 Py_BEGIN_ALLOW_THREADS
5030 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5031 Py_END_ALLOW_THREADS
5032
5033 return value;
5034}
5035
5036static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005037posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005038{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005039 /* Currently Only Uptime is Provided -- Others Later */
5040 return Py_BuildValue("ddddd",
5041 (double)0 /* t.tms_utime / HZ */,
5042 (double)0 /* t.tms_stime / HZ */,
5043 (double)0 /* t.tms_cutime / HZ */,
5044 (double)0 /* t.tms_cstime / HZ */,
5045 (double)system_uptime() / 1000);
5046}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005047#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005048static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005049posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005050{
5051 struct tms t;
5052 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005053 errno = 0;
5054 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005055 if (c == (clock_t) -1)
5056 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005057 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005058 (double)t.tms_utime / HZ,
5059 (double)t.tms_stime / HZ,
5060 (double)t.tms_cutime / HZ,
5061 (double)t.tms_cstime / HZ,
5062 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005063}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005064#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005065#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005066
5067
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005068#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005069#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005070static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005071posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005072{
5073 FILETIME create, exit, kernel, user;
5074 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005075 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005076 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5077 /* The fields of a FILETIME structure are the hi and lo part
5078 of a 64-bit value expressed in 100 nanosecond units.
5079 1e7 is one second in such units; 1e-7 the inverse.
5080 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5081 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005082 return Py_BuildValue(
5083 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005084 (double)(kernel.dwHighDateTime*429.4967296 +
5085 kernel.dwLowDateTime*1e-7),
5086 (double)(user.dwHighDateTime*429.4967296 +
5087 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005088 (double)0,
5089 (double)0,
5090 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005091}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005092#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005093
5094#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005095PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005096"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005097Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005098#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005100
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005101#ifdef HAVE_GETSID
5102PyDoc_STRVAR(posix_getsid__doc__,
5103"getsid(pid) -> sid\n\n\
5104Call the system call getsid().");
5105
5106static PyObject *
5107posix_getsid(PyObject *self, PyObject *args)
5108{
5109 int pid, sid;
5110 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5111 return NULL;
5112 sid = getsid(pid);
5113 if (sid < 0)
5114 return posix_error();
5115 return PyInt_FromLong((long)sid);
5116}
5117#endif /* HAVE_GETSID */
5118
5119
Guido van Rossumb6775db1994-08-01 11:34:53 +00005120#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005121PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005122"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005123Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005124
Barry Warsaw53699e91996-12-10 23:23:01 +00005125static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005126posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005127{
Guido van Rossum687dd131993-05-17 08:34:16 +00005128 if (setsid() < 0)
5129 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005130 Py_INCREF(Py_None);
5131 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005132}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005133#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005134
Guido van Rossumb6775db1994-08-01 11:34:53 +00005135#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005136PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005137"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005138Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005139
Barry Warsaw53699e91996-12-10 23:23:01 +00005140static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005141posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005142{
5143 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005144 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005145 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005146 if (setpgid(pid, pgrp) < 0)
5147 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005148 Py_INCREF(Py_None);
5149 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005150}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005151#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005152
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005153
Guido van Rossumb6775db1994-08-01 11:34:53 +00005154#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005155PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005156"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005157Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005158
Barry Warsaw53699e91996-12-10 23:23:01 +00005159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005160posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005161{
5162 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005163 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005164 return NULL;
5165 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005166 if (pgid < 0)
5167 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005168 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005169}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005170#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005172
Guido van Rossumb6775db1994-08-01 11:34:53 +00005173#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005174PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005175"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005176Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005177
Barry Warsaw53699e91996-12-10 23:23:01 +00005178static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005179posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005180{
5181 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005182 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005183 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005184 if (tcsetpgrp(fd, pgid) < 0)
5185 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005186 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005187 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005188}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005189#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005190
Guido van Rossum687dd131993-05-17 08:34:16 +00005191/* Functions acting on file descriptors */
5192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005193PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005194"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005195Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005196
Barry Warsaw53699e91996-12-10 23:23:01 +00005197static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005198posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005199{
Mark Hammondef8b6542001-05-13 08:04:26 +00005200 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005201 int flag;
5202 int mode = 0777;
5203 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005204
5205#ifdef MS_WINDOWS
5206 if (unicode_file_names()) {
5207 PyUnicodeObject *po;
5208 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5209 Py_BEGIN_ALLOW_THREADS
5210 /* PyUnicode_AS_UNICODE OK without thread
5211 lock as it is a simple dereference. */
5212 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5213 Py_END_ALLOW_THREADS
5214 if (fd < 0)
5215 return posix_error();
5216 return PyInt_FromLong((long)fd);
5217 }
5218 /* Drop the argument parsing error as narrow strings
5219 are also valid. */
5220 PyErr_Clear();
5221 }
5222#endif
5223
Tim Peters5aa91602002-01-30 05:46:57 +00005224 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005225 Py_FileSystemDefaultEncoding, &file,
5226 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005227 return NULL;
5228
Barry Warsaw53699e91996-12-10 23:23:01 +00005229 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005230 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005231 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005232 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005233 return posix_error_with_allocated_filename(file);
5234 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005235 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005236}
5237
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005239PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005240"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005241Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005242
Barry Warsaw53699e91996-12-10 23:23:01 +00005243static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005244posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005245{
5246 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005247 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005248 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005249 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005250 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005251 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005252 if (res < 0)
5253 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005254 Py_INCREF(Py_None);
5255 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005256}
5257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005259PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005260"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005261Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005262
Barry Warsaw53699e91996-12-10 23:23:01 +00005263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005264posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005265{
5266 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005267 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005268 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005269 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005270 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005271 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005272 if (fd < 0)
5273 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005274 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005275}
5276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005278PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005279"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005281
Barry Warsaw53699e91996-12-10 23:23:01 +00005282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005283posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005284{
5285 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005286 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005287 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005288 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005289 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005290 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005291 if (res < 0)
5292 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005293 Py_INCREF(Py_None);
5294 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005295}
5296
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005298PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005299"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005300Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005301
Barry Warsaw53699e91996-12-10 23:23:01 +00005302static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005303posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005304{
5305 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005306#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005307 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005308#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005309 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005310#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005311 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005312 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005313 return NULL;
5314#ifdef SEEK_SET
5315 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5316 switch (how) {
5317 case 0: how = SEEK_SET; break;
5318 case 1: how = SEEK_CUR; break;
5319 case 2: how = SEEK_END; break;
5320 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005321#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005322
5323#if !defined(HAVE_LARGEFILE_SUPPORT)
5324 pos = PyInt_AsLong(posobj);
5325#else
5326 pos = PyLong_Check(posobj) ?
5327 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5328#endif
5329 if (PyErr_Occurred())
5330 return NULL;
5331
Barry Warsaw53699e91996-12-10 23:23:01 +00005332 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005333#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005334 res = _lseeki64(fd, pos, how);
5335#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005336 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005337#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005338 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005339 if (res < 0)
5340 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005341
5342#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005343 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005344#else
5345 return PyLong_FromLongLong(res);
5346#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005347}
5348
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005350PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005351"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005352Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005353
Barry Warsaw53699e91996-12-10 23:23:01 +00005354static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005355posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005356{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005357 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005358 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005359 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005360 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005361 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005362 if (buffer == NULL)
5363 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005364 Py_BEGIN_ALLOW_THREADS
5365 n = read(fd, PyString_AsString(buffer), size);
5366 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005367 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005368 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005369 return posix_error();
5370 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005371 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005372 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005373 return buffer;
5374}
5375
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005377PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005378"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005379Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005380
Barry Warsaw53699e91996-12-10 23:23:01 +00005381static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005382posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005383{
5384 int fd, size;
5385 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005386 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005387 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005388 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005389 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005390 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005391 if (size < 0)
5392 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005393 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005394}
5395
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005398"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005400
Barry Warsaw53699e91996-12-10 23:23:01 +00005401static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005402posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005403{
5404 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005405 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005406 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005407 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005408 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005409#ifdef __VMS
5410 /* on OpenVMS we must ensure that all bytes are written to the file */
5411 fsync(fd);
5412#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005413 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005414 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005415 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005416 if (res != 0)
5417 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005418
Fred Drake699f3522000-06-29 21:12:41 +00005419 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005420}
5421
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005423PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005424"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005425Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005426
Barry Warsaw53699e91996-12-10 23:23:01 +00005427static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005428posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005429{
Guido van Rossum687dd131993-05-17 08:34:16 +00005430 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005431 char *mode = "r";
5432 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005433 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005434 PyObject *f;
5435 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005436 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005437
Thomas Heller1f043e22002-11-07 16:00:59 +00005438 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5439 PyErr_Format(PyExc_ValueError,
5440 "invalid file mode '%s'", mode);
5441 return NULL;
5442 }
5443
Barry Warsaw53699e91996-12-10 23:23:01 +00005444 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005445 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005446 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005447 if (fp == NULL)
5448 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005449 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005450 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005451 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005452 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005453}
5454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005455PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005456"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005457Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005459
5460static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005461posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005462{
5463 int fd;
5464 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5465 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005466 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005467}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005468
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005469#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005470PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005471"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005472Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005473
Barry Warsaw53699e91996-12-10 23:23:01 +00005474static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005475posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005476{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005477#if defined(PYOS_OS2)
5478 HFILE read, write;
5479 APIRET rc;
5480
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005481 Py_BEGIN_ALLOW_THREADS
5482 rc = DosCreatePipe( &read, &write, 4096);
5483 Py_END_ALLOW_THREADS
5484 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005485 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005486
5487 return Py_BuildValue("(ii)", read, write);
5488#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005489#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005490 int fds[2];
5491 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005492 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005493 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005494 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005495 if (res != 0)
5496 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005497 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005498#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005499 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005500 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005501 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005502 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005503 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005504 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005505 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005506 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005507 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5508 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005509 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005510#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005511#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005512}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005513#endif /* HAVE_PIPE */
5514
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005515
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005516#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005517PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005518"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005520
Barry Warsaw53699e91996-12-10 23:23:01 +00005521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005522posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005523{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005524 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005525 int mode = 0666;
5526 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005527 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005528 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005529 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005530 res = mkfifo(filename, mode);
5531 Py_END_ALLOW_THREADS
5532 if (res < 0)
5533 return posix_error();
5534 Py_INCREF(Py_None);
5535 return Py_None;
5536}
5537#endif
5538
5539
Neal Norwitz11690112002-07-30 01:08:28 +00005540#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005541PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005542"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005543Create a filesystem node (file, device special file or named pipe)\n\
5544named filename. mode specifies both the permissions to use and the\n\
5545type of node to be created, being combined (bitwise OR) with one of\n\
5546S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005547device defines the newly created device special file (probably using\n\
5548os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005549
5550
5551static PyObject *
5552posix_mknod(PyObject *self, PyObject *args)
5553{
5554 char *filename;
5555 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005556 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005557 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005558 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005559 return NULL;
5560 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005561 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005562 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005563 if (res < 0)
5564 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005565 Py_INCREF(Py_None);
5566 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005567}
5568#endif
5569
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005570#ifdef HAVE_DEVICE_MACROS
5571PyDoc_STRVAR(posix_major__doc__,
5572"major(device) -> major number\n\
5573Extracts a device major number from a raw device number.");
5574
5575static PyObject *
5576posix_major(PyObject *self, PyObject *args)
5577{
5578 int device;
5579 if (!PyArg_ParseTuple(args, "i:major", &device))
5580 return NULL;
5581 return PyInt_FromLong((long)major(device));
5582}
5583
5584PyDoc_STRVAR(posix_minor__doc__,
5585"minor(device) -> minor number\n\
5586Extracts a device minor number from a raw device number.");
5587
5588static PyObject *
5589posix_minor(PyObject *self, PyObject *args)
5590{
5591 int device;
5592 if (!PyArg_ParseTuple(args, "i:minor", &device))
5593 return NULL;
5594 return PyInt_FromLong((long)minor(device));
5595}
5596
5597PyDoc_STRVAR(posix_makedev__doc__,
5598"makedev(major, minor) -> device number\n\
5599Composes a raw device number from the major and minor device numbers.");
5600
5601static PyObject *
5602posix_makedev(PyObject *self, PyObject *args)
5603{
5604 int major, minor;
5605 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5606 return NULL;
5607 return PyInt_FromLong((long)makedev(major, minor));
5608}
5609#endif /* device macros */
5610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005611
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005612#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005613PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005614"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005615Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005616
Barry Warsaw53699e91996-12-10 23:23:01 +00005617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005618posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005619{
5620 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005621 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005622 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005623 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005624
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005625 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005626 return NULL;
5627
5628#if !defined(HAVE_LARGEFILE_SUPPORT)
5629 length = PyInt_AsLong(lenobj);
5630#else
5631 length = PyLong_Check(lenobj) ?
5632 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5633#endif
5634 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005635 return NULL;
5636
Barry Warsaw53699e91996-12-10 23:23:01 +00005637 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005638 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005639 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005640 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005641 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005642 return NULL;
5643 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005644 Py_INCREF(Py_None);
5645 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005646}
5647#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005648
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005649#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005650PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005651"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005652Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005653
Fred Drake762e2061999-08-26 17:23:54 +00005654/* Save putenv() parameters as values here, so we can collect them when they
5655 * get re-set with another call for the same key. */
5656static PyObject *posix_putenv_garbage;
5657
Tim Peters5aa91602002-01-30 05:46:57 +00005658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005659posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005660{
5661 char *s1, *s2;
5662 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005663 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005664 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005665
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005666 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005667 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005668
5669#if defined(PYOS_OS2)
5670 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5671 APIRET rc;
5672
Guido van Rossumd48f2521997-12-05 22:19:34 +00005673 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5674 if (rc != NO_ERROR)
5675 return os2_error(rc);
5676
5677 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5678 APIRET rc;
5679
Guido van Rossumd48f2521997-12-05 22:19:34 +00005680 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5681 if (rc != NO_ERROR)
5682 return os2_error(rc);
5683 } else {
5684#endif
5685
Fred Drake762e2061999-08-26 17:23:54 +00005686 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005687 len = strlen(s1) + strlen(s2) + 2;
5688 /* len includes space for a trailing \0; the size arg to
5689 PyString_FromStringAndSize does not count that */
5690 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005691 if (newstr == NULL)
5692 return PyErr_NoMemory();
5693 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005694 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005695 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005696 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005697 posix_error();
5698 return NULL;
5699 }
Fred Drake762e2061999-08-26 17:23:54 +00005700 /* Install the first arg and newstr in posix_putenv_garbage;
5701 * this will cause previous value to be collected. This has to
5702 * happen after the real putenv() call because the old value
5703 * was still accessible until then. */
5704 if (PyDict_SetItem(posix_putenv_garbage,
5705 PyTuple_GET_ITEM(args, 0), newstr)) {
5706 /* really not much we can do; just leak */
5707 PyErr_Clear();
5708 }
5709 else {
5710 Py_DECREF(newstr);
5711 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005712
5713#if defined(PYOS_OS2)
5714 }
5715#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005716 Py_INCREF(Py_None);
5717 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005718}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005719#endif /* putenv */
5720
Guido van Rossumc524d952001-10-19 01:31:59 +00005721#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005722PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005723"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005724Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005725
5726static PyObject *
5727posix_unsetenv(PyObject *self, PyObject *args)
5728{
5729 char *s1;
5730
5731 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5732 return NULL;
5733
5734 unsetenv(s1);
5735
5736 /* Remove the key from posix_putenv_garbage;
5737 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005738 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005739 * old value was still accessible until then.
5740 */
5741 if (PyDict_DelItem(posix_putenv_garbage,
5742 PyTuple_GET_ITEM(args, 0))) {
5743 /* really not much we can do; just leak */
5744 PyErr_Clear();
5745 }
5746
5747 Py_INCREF(Py_None);
5748 return Py_None;
5749}
5750#endif /* unsetenv */
5751
Guido van Rossumb6a47161997-09-15 22:54:34 +00005752#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005753PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005754"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005755Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005756
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005758posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005759{
5760 int code;
5761 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005762 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005763 return NULL;
5764 message = strerror(code);
5765 if (message == NULL) {
5766 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005767 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005768 return NULL;
5769 }
5770 return PyString_FromString(message);
5771}
5772#endif /* strerror */
5773
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005774
Guido van Rossumc9641791998-08-04 15:26:23 +00005775#ifdef HAVE_SYS_WAIT_H
5776
Fred Drake106c1a02002-04-23 15:58:02 +00005777#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005778PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005779"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005780Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005781
5782static PyObject *
5783posix_WCOREDUMP(PyObject *self, PyObject *args)
5784{
5785#ifdef UNION_WAIT
5786 union wait status;
5787#define status_i (status.w_status)
5788#else
5789 int status;
5790#define status_i status
5791#endif
5792 status_i = 0;
5793
5794 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5795 {
5796 return NULL;
5797 }
5798
5799 return PyBool_FromLong(WCOREDUMP(status));
5800#undef status_i
5801}
5802#endif /* WCOREDUMP */
5803
5804#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005806"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005807Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005808job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005809
5810static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005811posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005812{
5813#ifdef UNION_WAIT
5814 union wait status;
5815#define status_i (status.w_status)
5816#else
5817 int status;
5818#define status_i status
5819#endif
5820 status_i = 0;
5821
5822 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5823 {
5824 return NULL;
5825 }
5826
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005827 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005828#undef status_i
5829}
5830#endif /* WIFCONTINUED */
5831
Guido van Rossumc9641791998-08-04 15:26:23 +00005832#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005833PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005834"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005835Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005836
5837static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005838posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005839{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005840#ifdef UNION_WAIT
5841 union wait status;
5842#define status_i (status.w_status)
5843#else
5844 int status;
5845#define status_i status
5846#endif
5847 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005848
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005849 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005850 {
5851 return NULL;
5852 }
Tim Peters5aa91602002-01-30 05:46:57 +00005853
Fred Drake106c1a02002-04-23 15:58:02 +00005854 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005855#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005856}
5857#endif /* WIFSTOPPED */
5858
5859#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005860PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005861"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005862Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005863
5864static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005865posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005866{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005867#ifdef UNION_WAIT
5868 union wait status;
5869#define status_i (status.w_status)
5870#else
5871 int status;
5872#define status_i status
5873#endif
5874 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005875
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005876 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005877 {
5878 return NULL;
5879 }
Tim Peters5aa91602002-01-30 05:46:57 +00005880
Fred Drake106c1a02002-04-23 15:58:02 +00005881 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005882#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005883}
5884#endif /* WIFSIGNALED */
5885
5886#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005887PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005888"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005889Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005890system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005891
5892static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005893posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005894{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005895#ifdef UNION_WAIT
5896 union wait status;
5897#define status_i (status.w_status)
5898#else
5899 int status;
5900#define status_i status
5901#endif
5902 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005903
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005904 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005905 {
5906 return NULL;
5907 }
Tim Peters5aa91602002-01-30 05:46:57 +00005908
Fred Drake106c1a02002-04-23 15:58:02 +00005909 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005910#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005911}
5912#endif /* WIFEXITED */
5913
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005914#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005915PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005916"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005917Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005918
5919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005920posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005921{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005922#ifdef UNION_WAIT
5923 union wait status;
5924#define status_i (status.w_status)
5925#else
5926 int status;
5927#define status_i status
5928#endif
5929 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005930
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005931 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005932 {
5933 return NULL;
5934 }
Tim Peters5aa91602002-01-30 05:46:57 +00005935
Guido van Rossumc9641791998-08-04 15:26:23 +00005936 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005937#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005938}
5939#endif /* WEXITSTATUS */
5940
5941#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005942PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005943"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005944Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005945value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005946
5947static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005948posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005949{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005950#ifdef UNION_WAIT
5951 union wait status;
5952#define status_i (status.w_status)
5953#else
5954 int status;
5955#define status_i status
5956#endif
5957 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005958
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005959 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005960 {
5961 return NULL;
5962 }
Tim Peters5aa91602002-01-30 05:46:57 +00005963
Guido van Rossumc9641791998-08-04 15:26:23 +00005964 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005965#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005966}
5967#endif /* WTERMSIG */
5968
5969#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005970PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005971"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005972Return the signal that stopped the process that provided\n\
5973the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005974
5975static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005976posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005977{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005978#ifdef UNION_WAIT
5979 union wait status;
5980#define status_i (status.w_status)
5981#else
5982 int status;
5983#define status_i status
5984#endif
5985 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005986
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005987 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005988 {
5989 return NULL;
5990 }
Tim Peters5aa91602002-01-30 05:46:57 +00005991
Guido van Rossumc9641791998-08-04 15:26:23 +00005992 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005993#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005994}
5995#endif /* WSTOPSIG */
5996
5997#endif /* HAVE_SYS_WAIT_H */
5998
5999
Guido van Rossum94f6f721999-01-06 18:42:14 +00006000#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006001#ifdef _SCO_DS
6002/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6003 needed definitions in sys/statvfs.h */
6004#define _SVID3
6005#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006006#include <sys/statvfs.h>
6007
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006008static PyObject*
6009_pystatvfs_fromstructstatvfs(struct statvfs st) {
6010 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6011 if (v == NULL)
6012 return NULL;
6013
6014#if !defined(HAVE_LARGEFILE_SUPPORT)
6015 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6016 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6017 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6018 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6019 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6020 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6021 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6022 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6023 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6024 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6025#else
6026 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6027 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006028 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006029 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006030 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006031 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006032 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006033 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006034 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006035 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006036 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006037 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006038 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006039 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006040 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6041 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6042#endif
6043
6044 return v;
6045}
6046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006047PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006048"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006049Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006050
6051static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006052posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006053{
6054 int fd, res;
6055 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006056
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006057 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006058 return NULL;
6059 Py_BEGIN_ALLOW_THREADS
6060 res = fstatvfs(fd, &st);
6061 Py_END_ALLOW_THREADS
6062 if (res != 0)
6063 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006064
6065 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006066}
6067#endif /* HAVE_FSTATVFS */
6068
6069
6070#if defined(HAVE_STATVFS)
6071#include <sys/statvfs.h>
6072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006073PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006074"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006075Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006076
6077static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006078posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006079{
6080 char *path;
6081 int res;
6082 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006083 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006084 return NULL;
6085 Py_BEGIN_ALLOW_THREADS
6086 res = statvfs(path, &st);
6087 Py_END_ALLOW_THREADS
6088 if (res != 0)
6089 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006090
6091 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006092}
6093#endif /* HAVE_STATVFS */
6094
6095
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006096#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006097PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006098"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006099Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006100The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006101or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006102
6103static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006104posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006105{
6106 PyObject *result = NULL;
6107 char *dir = NULL;
6108 char *pfx = NULL;
6109 char *name;
6110
6111 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6112 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006113
6114 if (PyErr_Warn(PyExc_RuntimeWarning,
6115 "tempnam is a potential security risk to your program") < 0)
6116 return NULL;
6117
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006118#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006119 name = _tempnam(dir, pfx);
6120#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006121 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006122#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006123 if (name == NULL)
6124 return PyErr_NoMemory();
6125 result = PyString_FromString(name);
6126 free(name);
6127 return result;
6128}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006129#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006130
6131
6132#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006134"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006136
6137static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006138posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006139{
6140 FILE *fp;
6141
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006142 fp = tmpfile();
6143 if (fp == NULL)
6144 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006145 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006146}
6147#endif
6148
6149
6150#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006151PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006152"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006153Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006154
6155static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006156posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006157{
6158 char buffer[L_tmpnam];
6159 char *name;
6160
Skip Montanaro95618b52001-08-18 18:52:10 +00006161 if (PyErr_Warn(PyExc_RuntimeWarning,
6162 "tmpnam is a potential security risk to your program") < 0)
6163 return NULL;
6164
Greg Wardb48bc172000-03-01 21:51:56 +00006165#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006166 name = tmpnam_r(buffer);
6167#else
6168 name = tmpnam(buffer);
6169#endif
6170 if (name == NULL) {
6171 PyErr_SetObject(PyExc_OSError,
6172 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006173#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006174 "unexpected NULL from tmpnam_r"
6175#else
6176 "unexpected NULL from tmpnam"
6177#endif
6178 ));
6179 return NULL;
6180 }
6181 return PyString_FromString(buffer);
6182}
6183#endif
6184
6185
Fred Drakec9680921999-12-13 16:37:25 +00006186/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6187 * It maps strings representing configuration variable names to
6188 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006189 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006190 * rarely-used constants. There are three separate tables that use
6191 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006192 *
6193 * This code is always included, even if none of the interfaces that
6194 * need it are included. The #if hackery needed to avoid it would be
6195 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006196 */
6197struct constdef {
6198 char *name;
6199 long value;
6200};
6201
Fred Drake12c6e2d1999-12-14 21:25:03 +00006202static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006203conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6204 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006205{
6206 if (PyInt_Check(arg)) {
6207 *valuep = PyInt_AS_LONG(arg);
6208 return 1;
6209 }
6210 if (PyString_Check(arg)) {
6211 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006212 size_t lo = 0;
6213 size_t mid;
6214 size_t hi = tablesize;
6215 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006216 char *confname = PyString_AS_STRING(arg);
6217 while (lo < hi) {
6218 mid = (lo + hi) / 2;
6219 cmp = strcmp(confname, table[mid].name);
6220 if (cmp < 0)
6221 hi = mid;
6222 else if (cmp > 0)
6223 lo = mid + 1;
6224 else {
6225 *valuep = table[mid].value;
6226 return 1;
6227 }
6228 }
6229 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6230 }
6231 else
6232 PyErr_SetString(PyExc_TypeError,
6233 "configuration names must be strings or integers");
6234 return 0;
6235}
6236
6237
6238#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6239static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006240#ifdef _PC_ABI_AIO_XFER_MAX
6241 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6242#endif
6243#ifdef _PC_ABI_ASYNC_IO
6244 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6245#endif
Fred Drakec9680921999-12-13 16:37:25 +00006246#ifdef _PC_ASYNC_IO
6247 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6248#endif
6249#ifdef _PC_CHOWN_RESTRICTED
6250 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6251#endif
6252#ifdef _PC_FILESIZEBITS
6253 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6254#endif
6255#ifdef _PC_LAST
6256 {"PC_LAST", _PC_LAST},
6257#endif
6258#ifdef _PC_LINK_MAX
6259 {"PC_LINK_MAX", _PC_LINK_MAX},
6260#endif
6261#ifdef _PC_MAX_CANON
6262 {"PC_MAX_CANON", _PC_MAX_CANON},
6263#endif
6264#ifdef _PC_MAX_INPUT
6265 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6266#endif
6267#ifdef _PC_NAME_MAX
6268 {"PC_NAME_MAX", _PC_NAME_MAX},
6269#endif
6270#ifdef _PC_NO_TRUNC
6271 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6272#endif
6273#ifdef _PC_PATH_MAX
6274 {"PC_PATH_MAX", _PC_PATH_MAX},
6275#endif
6276#ifdef _PC_PIPE_BUF
6277 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6278#endif
6279#ifdef _PC_PRIO_IO
6280 {"PC_PRIO_IO", _PC_PRIO_IO},
6281#endif
6282#ifdef _PC_SOCK_MAXBUF
6283 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6284#endif
6285#ifdef _PC_SYNC_IO
6286 {"PC_SYNC_IO", _PC_SYNC_IO},
6287#endif
6288#ifdef _PC_VDISABLE
6289 {"PC_VDISABLE", _PC_VDISABLE},
6290#endif
6291};
6292
Fred Drakec9680921999-12-13 16:37:25 +00006293static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006294conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006295{
6296 return conv_confname(arg, valuep, posix_constants_pathconf,
6297 sizeof(posix_constants_pathconf)
6298 / sizeof(struct constdef));
6299}
6300#endif
6301
6302#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006303PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006304"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006305Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006306If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006307
6308static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006309posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006310{
6311 PyObject *result = NULL;
6312 int name, fd;
6313
Fred Drake12c6e2d1999-12-14 21:25:03 +00006314 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6315 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006316 long limit;
6317
6318 errno = 0;
6319 limit = fpathconf(fd, name);
6320 if (limit == -1 && errno != 0)
6321 posix_error();
6322 else
6323 result = PyInt_FromLong(limit);
6324 }
6325 return result;
6326}
6327#endif
6328
6329
6330#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006331PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006332"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006333Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006334If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006335
6336static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006337posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006338{
6339 PyObject *result = NULL;
6340 int name;
6341 char *path;
6342
6343 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6344 conv_path_confname, &name)) {
6345 long limit;
6346
6347 errno = 0;
6348 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006349 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006350 if (errno == EINVAL)
6351 /* could be a path or name problem */
6352 posix_error();
6353 else
6354 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006355 }
Fred Drakec9680921999-12-13 16:37:25 +00006356 else
6357 result = PyInt_FromLong(limit);
6358 }
6359 return result;
6360}
6361#endif
6362
6363#ifdef HAVE_CONFSTR
6364static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006365#ifdef _CS_ARCHITECTURE
6366 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6367#endif
6368#ifdef _CS_HOSTNAME
6369 {"CS_HOSTNAME", _CS_HOSTNAME},
6370#endif
6371#ifdef _CS_HW_PROVIDER
6372 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6373#endif
6374#ifdef _CS_HW_SERIAL
6375 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6376#endif
6377#ifdef _CS_INITTAB_NAME
6378 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6379#endif
Fred Drakec9680921999-12-13 16:37:25 +00006380#ifdef _CS_LFS64_CFLAGS
6381 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6382#endif
6383#ifdef _CS_LFS64_LDFLAGS
6384 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6385#endif
6386#ifdef _CS_LFS64_LIBS
6387 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6388#endif
6389#ifdef _CS_LFS64_LINTFLAGS
6390 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6391#endif
6392#ifdef _CS_LFS_CFLAGS
6393 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6394#endif
6395#ifdef _CS_LFS_LDFLAGS
6396 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6397#endif
6398#ifdef _CS_LFS_LIBS
6399 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6400#endif
6401#ifdef _CS_LFS_LINTFLAGS
6402 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6403#endif
Fred Draked86ed291999-12-15 15:34:33 +00006404#ifdef _CS_MACHINE
6405 {"CS_MACHINE", _CS_MACHINE},
6406#endif
Fred Drakec9680921999-12-13 16:37:25 +00006407#ifdef _CS_PATH
6408 {"CS_PATH", _CS_PATH},
6409#endif
Fred Draked86ed291999-12-15 15:34:33 +00006410#ifdef _CS_RELEASE
6411 {"CS_RELEASE", _CS_RELEASE},
6412#endif
6413#ifdef _CS_SRPC_DOMAIN
6414 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6415#endif
6416#ifdef _CS_SYSNAME
6417 {"CS_SYSNAME", _CS_SYSNAME},
6418#endif
6419#ifdef _CS_VERSION
6420 {"CS_VERSION", _CS_VERSION},
6421#endif
Fred Drakec9680921999-12-13 16:37:25 +00006422#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6423 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6424#endif
6425#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6426 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6427#endif
6428#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6429 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6430#endif
6431#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6432 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6433#endif
6434#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6435 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6436#endif
6437#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6438 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6439#endif
6440#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6441 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6442#endif
6443#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6444 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6445#endif
6446#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6447 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6448#endif
6449#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6450 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6451#endif
6452#ifdef _CS_XBS5_LP64_OFF64_LIBS
6453 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6454#endif
6455#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6456 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6457#endif
6458#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6459 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6460#endif
6461#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6462 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6463#endif
6464#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6465 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6466#endif
6467#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6468 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6469#endif
Fred Draked86ed291999-12-15 15:34:33 +00006470#ifdef _MIPS_CS_AVAIL_PROCESSORS
6471 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6472#endif
6473#ifdef _MIPS_CS_BASE
6474 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6475#endif
6476#ifdef _MIPS_CS_HOSTID
6477 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6478#endif
6479#ifdef _MIPS_CS_HW_NAME
6480 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6481#endif
6482#ifdef _MIPS_CS_NUM_PROCESSORS
6483 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6484#endif
6485#ifdef _MIPS_CS_OSREL_MAJ
6486 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6487#endif
6488#ifdef _MIPS_CS_OSREL_MIN
6489 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6490#endif
6491#ifdef _MIPS_CS_OSREL_PATCH
6492 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6493#endif
6494#ifdef _MIPS_CS_OS_NAME
6495 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6496#endif
6497#ifdef _MIPS_CS_OS_PROVIDER
6498 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6499#endif
6500#ifdef _MIPS_CS_PROCESSORS
6501 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6502#endif
6503#ifdef _MIPS_CS_SERIAL
6504 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6505#endif
6506#ifdef _MIPS_CS_VENDOR
6507 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6508#endif
Fred Drakec9680921999-12-13 16:37:25 +00006509};
6510
6511static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006512conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006513{
6514 return conv_confname(arg, valuep, posix_constants_confstr,
6515 sizeof(posix_constants_confstr)
6516 / sizeof(struct constdef));
6517}
6518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006519PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006520"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006521Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006522
6523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006524posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006525{
6526 PyObject *result = NULL;
6527 int name;
6528 char buffer[64];
6529
6530 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6531 int len = confstr(name, buffer, sizeof(buffer));
6532
Fred Drakec9680921999-12-13 16:37:25 +00006533 errno = 0;
6534 if (len == 0) {
6535 if (errno != 0)
6536 posix_error();
6537 else
6538 result = PyString_FromString("");
6539 }
6540 else {
6541 if (len >= sizeof(buffer)) {
6542 result = PyString_FromStringAndSize(NULL, len);
6543 if (result != NULL)
6544 confstr(name, PyString_AS_STRING(result), len+1);
6545 }
6546 else
6547 result = PyString_FromString(buffer);
6548 }
6549 }
6550 return result;
6551}
6552#endif
6553
6554
6555#ifdef HAVE_SYSCONF
6556static struct constdef posix_constants_sysconf[] = {
6557#ifdef _SC_2_CHAR_TERM
6558 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6559#endif
6560#ifdef _SC_2_C_BIND
6561 {"SC_2_C_BIND", _SC_2_C_BIND},
6562#endif
6563#ifdef _SC_2_C_DEV
6564 {"SC_2_C_DEV", _SC_2_C_DEV},
6565#endif
6566#ifdef _SC_2_C_VERSION
6567 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6568#endif
6569#ifdef _SC_2_FORT_DEV
6570 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6571#endif
6572#ifdef _SC_2_FORT_RUN
6573 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6574#endif
6575#ifdef _SC_2_LOCALEDEF
6576 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6577#endif
6578#ifdef _SC_2_SW_DEV
6579 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6580#endif
6581#ifdef _SC_2_UPE
6582 {"SC_2_UPE", _SC_2_UPE},
6583#endif
6584#ifdef _SC_2_VERSION
6585 {"SC_2_VERSION", _SC_2_VERSION},
6586#endif
Fred Draked86ed291999-12-15 15:34:33 +00006587#ifdef _SC_ABI_ASYNCHRONOUS_IO
6588 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6589#endif
6590#ifdef _SC_ACL
6591 {"SC_ACL", _SC_ACL},
6592#endif
Fred Drakec9680921999-12-13 16:37:25 +00006593#ifdef _SC_AIO_LISTIO_MAX
6594 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6595#endif
Fred Drakec9680921999-12-13 16:37:25 +00006596#ifdef _SC_AIO_MAX
6597 {"SC_AIO_MAX", _SC_AIO_MAX},
6598#endif
6599#ifdef _SC_AIO_PRIO_DELTA_MAX
6600 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6601#endif
6602#ifdef _SC_ARG_MAX
6603 {"SC_ARG_MAX", _SC_ARG_MAX},
6604#endif
6605#ifdef _SC_ASYNCHRONOUS_IO
6606 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6607#endif
6608#ifdef _SC_ATEXIT_MAX
6609 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6610#endif
Fred Draked86ed291999-12-15 15:34:33 +00006611#ifdef _SC_AUDIT
6612 {"SC_AUDIT", _SC_AUDIT},
6613#endif
Fred Drakec9680921999-12-13 16:37:25 +00006614#ifdef _SC_AVPHYS_PAGES
6615 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6616#endif
6617#ifdef _SC_BC_BASE_MAX
6618 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6619#endif
6620#ifdef _SC_BC_DIM_MAX
6621 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6622#endif
6623#ifdef _SC_BC_SCALE_MAX
6624 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6625#endif
6626#ifdef _SC_BC_STRING_MAX
6627 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6628#endif
Fred Draked86ed291999-12-15 15:34:33 +00006629#ifdef _SC_CAP
6630 {"SC_CAP", _SC_CAP},
6631#endif
Fred Drakec9680921999-12-13 16:37:25 +00006632#ifdef _SC_CHARCLASS_NAME_MAX
6633 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6634#endif
6635#ifdef _SC_CHAR_BIT
6636 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6637#endif
6638#ifdef _SC_CHAR_MAX
6639 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6640#endif
6641#ifdef _SC_CHAR_MIN
6642 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6643#endif
6644#ifdef _SC_CHILD_MAX
6645 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6646#endif
6647#ifdef _SC_CLK_TCK
6648 {"SC_CLK_TCK", _SC_CLK_TCK},
6649#endif
6650#ifdef _SC_COHER_BLKSZ
6651 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6652#endif
6653#ifdef _SC_COLL_WEIGHTS_MAX
6654 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6655#endif
6656#ifdef _SC_DCACHE_ASSOC
6657 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6658#endif
6659#ifdef _SC_DCACHE_BLKSZ
6660 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6661#endif
6662#ifdef _SC_DCACHE_LINESZ
6663 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6664#endif
6665#ifdef _SC_DCACHE_SZ
6666 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6667#endif
6668#ifdef _SC_DCACHE_TBLKSZ
6669 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6670#endif
6671#ifdef _SC_DELAYTIMER_MAX
6672 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6673#endif
6674#ifdef _SC_EQUIV_CLASS_MAX
6675 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6676#endif
6677#ifdef _SC_EXPR_NEST_MAX
6678 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6679#endif
6680#ifdef _SC_FSYNC
6681 {"SC_FSYNC", _SC_FSYNC},
6682#endif
6683#ifdef _SC_GETGR_R_SIZE_MAX
6684 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6685#endif
6686#ifdef _SC_GETPW_R_SIZE_MAX
6687 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6688#endif
6689#ifdef _SC_ICACHE_ASSOC
6690 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6691#endif
6692#ifdef _SC_ICACHE_BLKSZ
6693 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6694#endif
6695#ifdef _SC_ICACHE_LINESZ
6696 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6697#endif
6698#ifdef _SC_ICACHE_SZ
6699 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6700#endif
Fred Draked86ed291999-12-15 15:34:33 +00006701#ifdef _SC_INF
6702 {"SC_INF", _SC_INF},
6703#endif
Fred Drakec9680921999-12-13 16:37:25 +00006704#ifdef _SC_INT_MAX
6705 {"SC_INT_MAX", _SC_INT_MAX},
6706#endif
6707#ifdef _SC_INT_MIN
6708 {"SC_INT_MIN", _SC_INT_MIN},
6709#endif
6710#ifdef _SC_IOV_MAX
6711 {"SC_IOV_MAX", _SC_IOV_MAX},
6712#endif
Fred Draked86ed291999-12-15 15:34:33 +00006713#ifdef _SC_IP_SECOPTS
6714 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6715#endif
Fred Drakec9680921999-12-13 16:37:25 +00006716#ifdef _SC_JOB_CONTROL
6717 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6718#endif
Fred Draked86ed291999-12-15 15:34:33 +00006719#ifdef _SC_KERN_POINTERS
6720 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6721#endif
6722#ifdef _SC_KERN_SIM
6723 {"SC_KERN_SIM", _SC_KERN_SIM},
6724#endif
Fred Drakec9680921999-12-13 16:37:25 +00006725#ifdef _SC_LINE_MAX
6726 {"SC_LINE_MAX", _SC_LINE_MAX},
6727#endif
6728#ifdef _SC_LOGIN_NAME_MAX
6729 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6730#endif
6731#ifdef _SC_LOGNAME_MAX
6732 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6733#endif
6734#ifdef _SC_LONG_BIT
6735 {"SC_LONG_BIT", _SC_LONG_BIT},
6736#endif
Fred Draked86ed291999-12-15 15:34:33 +00006737#ifdef _SC_MAC
6738 {"SC_MAC", _SC_MAC},
6739#endif
Fred Drakec9680921999-12-13 16:37:25 +00006740#ifdef _SC_MAPPED_FILES
6741 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6742#endif
6743#ifdef _SC_MAXPID
6744 {"SC_MAXPID", _SC_MAXPID},
6745#endif
6746#ifdef _SC_MB_LEN_MAX
6747 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6748#endif
6749#ifdef _SC_MEMLOCK
6750 {"SC_MEMLOCK", _SC_MEMLOCK},
6751#endif
6752#ifdef _SC_MEMLOCK_RANGE
6753 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6754#endif
6755#ifdef _SC_MEMORY_PROTECTION
6756 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6757#endif
6758#ifdef _SC_MESSAGE_PASSING
6759 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6760#endif
Fred Draked86ed291999-12-15 15:34:33 +00006761#ifdef _SC_MMAP_FIXED_ALIGNMENT
6762 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6763#endif
Fred Drakec9680921999-12-13 16:37:25 +00006764#ifdef _SC_MQ_OPEN_MAX
6765 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6766#endif
6767#ifdef _SC_MQ_PRIO_MAX
6768 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6769#endif
Fred Draked86ed291999-12-15 15:34:33 +00006770#ifdef _SC_NACLS_MAX
6771 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6772#endif
Fred Drakec9680921999-12-13 16:37:25 +00006773#ifdef _SC_NGROUPS_MAX
6774 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6775#endif
6776#ifdef _SC_NL_ARGMAX
6777 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6778#endif
6779#ifdef _SC_NL_LANGMAX
6780 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6781#endif
6782#ifdef _SC_NL_MSGMAX
6783 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6784#endif
6785#ifdef _SC_NL_NMAX
6786 {"SC_NL_NMAX", _SC_NL_NMAX},
6787#endif
6788#ifdef _SC_NL_SETMAX
6789 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6790#endif
6791#ifdef _SC_NL_TEXTMAX
6792 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6793#endif
6794#ifdef _SC_NPROCESSORS_CONF
6795 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6796#endif
6797#ifdef _SC_NPROCESSORS_ONLN
6798 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6799#endif
Fred Draked86ed291999-12-15 15:34:33 +00006800#ifdef _SC_NPROC_CONF
6801 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6802#endif
6803#ifdef _SC_NPROC_ONLN
6804 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6805#endif
Fred Drakec9680921999-12-13 16:37:25 +00006806#ifdef _SC_NZERO
6807 {"SC_NZERO", _SC_NZERO},
6808#endif
6809#ifdef _SC_OPEN_MAX
6810 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6811#endif
6812#ifdef _SC_PAGESIZE
6813 {"SC_PAGESIZE", _SC_PAGESIZE},
6814#endif
6815#ifdef _SC_PAGE_SIZE
6816 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6817#endif
6818#ifdef _SC_PASS_MAX
6819 {"SC_PASS_MAX", _SC_PASS_MAX},
6820#endif
6821#ifdef _SC_PHYS_PAGES
6822 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6823#endif
6824#ifdef _SC_PII
6825 {"SC_PII", _SC_PII},
6826#endif
6827#ifdef _SC_PII_INTERNET
6828 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6829#endif
6830#ifdef _SC_PII_INTERNET_DGRAM
6831 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6832#endif
6833#ifdef _SC_PII_INTERNET_STREAM
6834 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6835#endif
6836#ifdef _SC_PII_OSI
6837 {"SC_PII_OSI", _SC_PII_OSI},
6838#endif
6839#ifdef _SC_PII_OSI_CLTS
6840 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6841#endif
6842#ifdef _SC_PII_OSI_COTS
6843 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6844#endif
6845#ifdef _SC_PII_OSI_M
6846 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6847#endif
6848#ifdef _SC_PII_SOCKET
6849 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6850#endif
6851#ifdef _SC_PII_XTI
6852 {"SC_PII_XTI", _SC_PII_XTI},
6853#endif
6854#ifdef _SC_POLL
6855 {"SC_POLL", _SC_POLL},
6856#endif
6857#ifdef _SC_PRIORITIZED_IO
6858 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6859#endif
6860#ifdef _SC_PRIORITY_SCHEDULING
6861 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6862#endif
6863#ifdef _SC_REALTIME_SIGNALS
6864 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6865#endif
6866#ifdef _SC_RE_DUP_MAX
6867 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6868#endif
6869#ifdef _SC_RTSIG_MAX
6870 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6871#endif
6872#ifdef _SC_SAVED_IDS
6873 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6874#endif
6875#ifdef _SC_SCHAR_MAX
6876 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6877#endif
6878#ifdef _SC_SCHAR_MIN
6879 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6880#endif
6881#ifdef _SC_SELECT
6882 {"SC_SELECT", _SC_SELECT},
6883#endif
6884#ifdef _SC_SEMAPHORES
6885 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6886#endif
6887#ifdef _SC_SEM_NSEMS_MAX
6888 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6889#endif
6890#ifdef _SC_SEM_VALUE_MAX
6891 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6892#endif
6893#ifdef _SC_SHARED_MEMORY_OBJECTS
6894 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6895#endif
6896#ifdef _SC_SHRT_MAX
6897 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6898#endif
6899#ifdef _SC_SHRT_MIN
6900 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6901#endif
6902#ifdef _SC_SIGQUEUE_MAX
6903 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6904#endif
6905#ifdef _SC_SIGRT_MAX
6906 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6907#endif
6908#ifdef _SC_SIGRT_MIN
6909 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6910#endif
Fred Draked86ed291999-12-15 15:34:33 +00006911#ifdef _SC_SOFTPOWER
6912 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6913#endif
Fred Drakec9680921999-12-13 16:37:25 +00006914#ifdef _SC_SPLIT_CACHE
6915 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6916#endif
6917#ifdef _SC_SSIZE_MAX
6918 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6919#endif
6920#ifdef _SC_STACK_PROT
6921 {"SC_STACK_PROT", _SC_STACK_PROT},
6922#endif
6923#ifdef _SC_STREAM_MAX
6924 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6925#endif
6926#ifdef _SC_SYNCHRONIZED_IO
6927 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6928#endif
6929#ifdef _SC_THREADS
6930 {"SC_THREADS", _SC_THREADS},
6931#endif
6932#ifdef _SC_THREAD_ATTR_STACKADDR
6933 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6934#endif
6935#ifdef _SC_THREAD_ATTR_STACKSIZE
6936 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6937#endif
6938#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6939 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6940#endif
6941#ifdef _SC_THREAD_KEYS_MAX
6942 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6943#endif
6944#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6945 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6946#endif
6947#ifdef _SC_THREAD_PRIO_INHERIT
6948 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6949#endif
6950#ifdef _SC_THREAD_PRIO_PROTECT
6951 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6952#endif
6953#ifdef _SC_THREAD_PROCESS_SHARED
6954 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6955#endif
6956#ifdef _SC_THREAD_SAFE_FUNCTIONS
6957 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6958#endif
6959#ifdef _SC_THREAD_STACK_MIN
6960 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6961#endif
6962#ifdef _SC_THREAD_THREADS_MAX
6963 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6964#endif
6965#ifdef _SC_TIMERS
6966 {"SC_TIMERS", _SC_TIMERS},
6967#endif
6968#ifdef _SC_TIMER_MAX
6969 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6970#endif
6971#ifdef _SC_TTY_NAME_MAX
6972 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6973#endif
6974#ifdef _SC_TZNAME_MAX
6975 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6976#endif
6977#ifdef _SC_T_IOV_MAX
6978 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6979#endif
6980#ifdef _SC_UCHAR_MAX
6981 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6982#endif
6983#ifdef _SC_UINT_MAX
6984 {"SC_UINT_MAX", _SC_UINT_MAX},
6985#endif
6986#ifdef _SC_UIO_MAXIOV
6987 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6988#endif
6989#ifdef _SC_ULONG_MAX
6990 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6991#endif
6992#ifdef _SC_USHRT_MAX
6993 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6994#endif
6995#ifdef _SC_VERSION
6996 {"SC_VERSION", _SC_VERSION},
6997#endif
6998#ifdef _SC_WORD_BIT
6999 {"SC_WORD_BIT", _SC_WORD_BIT},
7000#endif
7001#ifdef _SC_XBS5_ILP32_OFF32
7002 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7003#endif
7004#ifdef _SC_XBS5_ILP32_OFFBIG
7005 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7006#endif
7007#ifdef _SC_XBS5_LP64_OFF64
7008 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7009#endif
7010#ifdef _SC_XBS5_LPBIG_OFFBIG
7011 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7012#endif
7013#ifdef _SC_XOPEN_CRYPT
7014 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7015#endif
7016#ifdef _SC_XOPEN_ENH_I18N
7017 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7018#endif
7019#ifdef _SC_XOPEN_LEGACY
7020 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7021#endif
7022#ifdef _SC_XOPEN_REALTIME
7023 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7024#endif
7025#ifdef _SC_XOPEN_REALTIME_THREADS
7026 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7027#endif
7028#ifdef _SC_XOPEN_SHM
7029 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7030#endif
7031#ifdef _SC_XOPEN_UNIX
7032 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7033#endif
7034#ifdef _SC_XOPEN_VERSION
7035 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7036#endif
7037#ifdef _SC_XOPEN_XCU_VERSION
7038 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7039#endif
7040#ifdef _SC_XOPEN_XPG2
7041 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7042#endif
7043#ifdef _SC_XOPEN_XPG3
7044 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7045#endif
7046#ifdef _SC_XOPEN_XPG4
7047 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7048#endif
7049};
7050
7051static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007052conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007053{
7054 return conv_confname(arg, valuep, posix_constants_sysconf,
7055 sizeof(posix_constants_sysconf)
7056 / sizeof(struct constdef));
7057}
7058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007059PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007060"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007061Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007062
7063static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007064posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007065{
7066 PyObject *result = NULL;
7067 int name;
7068
7069 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7070 int value;
7071
7072 errno = 0;
7073 value = sysconf(name);
7074 if (value == -1 && errno != 0)
7075 posix_error();
7076 else
7077 result = PyInt_FromLong(value);
7078 }
7079 return result;
7080}
7081#endif
7082
7083
Fred Drakebec628d1999-12-15 18:31:10 +00007084/* This code is used to ensure that the tables of configuration value names
7085 * are in sorted order as required by conv_confname(), and also to build the
7086 * the exported dictionaries that are used to publish information about the
7087 * names available on the host platform.
7088 *
7089 * Sorting the table at runtime ensures that the table is properly ordered
7090 * when used, even for platforms we're not able to test on. It also makes
7091 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007092 */
Fred Drakebec628d1999-12-15 18:31:10 +00007093
7094static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007095cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007096{
7097 const struct constdef *c1 =
7098 (const struct constdef *) v1;
7099 const struct constdef *c2 =
7100 (const struct constdef *) v2;
7101
7102 return strcmp(c1->name, c2->name);
7103}
7104
7105static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007106setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007107 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007108{
Fred Drakebec628d1999-12-15 18:31:10 +00007109 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007110 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007111
7112 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7113 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007114 if (d == NULL)
7115 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007116
Barry Warsaw3155db32000-04-13 15:20:40 +00007117 for (i=0; i < tablesize; ++i) {
7118 PyObject *o = PyInt_FromLong(table[i].value);
7119 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7120 Py_XDECREF(o);
7121 Py_DECREF(d);
7122 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007123 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007124 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007125 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007126 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007127}
7128
Fred Drakebec628d1999-12-15 18:31:10 +00007129/* Return -1 on failure, 0 on success. */
7130static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007131setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007132{
7133#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007134 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007135 sizeof(posix_constants_pathconf)
7136 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007137 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007138 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007139#endif
7140#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007141 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007142 sizeof(posix_constants_confstr)
7143 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007144 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007145 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007146#endif
7147#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007148 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007149 sizeof(posix_constants_sysconf)
7150 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007151 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007152 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007153#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007154 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007155}
Fred Draked86ed291999-12-15 15:34:33 +00007156
7157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007158PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007159"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007160Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007161in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007162
7163static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007164posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007165{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007166 abort();
7167 /*NOTREACHED*/
7168 Py_FatalError("abort() called from Python code didn't abort!");
7169 return NULL;
7170}
Fred Drakebec628d1999-12-15 18:31:10 +00007171
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007172#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007173PyDoc_STRVAR(win32_startfile__doc__,
7174"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007175\n\
7176This acts like double-clicking the file in Explorer, or giving the file\n\
7177name as an argument to the DOS \"start\" command: the file is opened\n\
7178with whatever application (if any) its extension is associated.\n\
7179\n\
7180startfile returns as soon as the associated application is launched.\n\
7181There is no option to wait for the application to close, and no way\n\
7182to retrieve the application's exit status.\n\
7183\n\
7184The filepath is relative to the current directory. If you want to use\n\
7185an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007186the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007187
7188static PyObject *
7189win32_startfile(PyObject *self, PyObject *args)
7190{
7191 char *filepath;
7192 HINSTANCE rc;
7193 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
7194 return NULL;
7195 Py_BEGIN_ALLOW_THREADS
7196 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
7197 Py_END_ALLOW_THREADS
7198 if (rc <= (HINSTANCE)32)
7199 return win32_error("startfile", filepath);
7200 Py_INCREF(Py_None);
7201 return Py_None;
7202}
7203#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007204
Martin v. Löwis438b5342002-12-27 10:16:42 +00007205#ifdef HAVE_GETLOADAVG
7206PyDoc_STRVAR(posix_getloadavg__doc__,
7207"getloadavg() -> (float, float, float)\n\n\
7208Return the number of processes in the system run queue averaged over\n\
7209the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7210was unobtainable");
7211
7212static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007213posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007214{
7215 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007216 if (getloadavg(loadavg, 3)!=3) {
7217 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7218 return NULL;
7219 } else
7220 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7221}
7222#endif
7223
7224
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007225static PyMethodDef posix_methods[] = {
7226 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7227#ifdef HAVE_TTYNAME
7228 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7229#endif
7230 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7231 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007232#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007233 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007234#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007235#ifdef HAVE_LCHOWN
7236 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7237#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007238#ifdef HAVE_CHROOT
7239 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7240#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007241#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007242 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007243#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007244#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007245 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007246#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007247 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007248#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007249#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007250#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007251 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007252#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007253 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7254 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7255 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007256#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007257 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007258#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007259#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007260 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007261#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007262 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7263 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7264 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007265 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007266#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007267 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007268#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007269#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007270 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007271#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007272 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007273#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007274 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007275#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007276 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7277 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7278 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007279#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007280 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007281#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007282 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007283#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007284 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7285 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007286#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007287#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007288 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7289 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007290#if defined(PYOS_OS2)
7291 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7292 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7293#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007294#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007295#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007296 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007297#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007298#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007299 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007300#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007301#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007302 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007303#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007304#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007305 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007306#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007307#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007308 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007309#endif /* HAVE_GETEGID */
7310#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007311 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007312#endif /* HAVE_GETEUID */
7313#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007314 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007315#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007316#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007317 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007318#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007319 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007320#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007321 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007322#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007323#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007324 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007325#endif /* HAVE_GETPPID */
7326#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007327 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007328#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007329#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007330 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007331#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007332#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007333 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007334#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007335#ifdef HAVE_KILLPG
7336 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7337#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007338#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007339 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007340#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007341#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007342 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007343#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007344 {"popen2", win32_popen2, METH_VARARGS},
7345 {"popen3", win32_popen3, METH_VARARGS},
7346 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007347 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007348#else
7349#if defined(PYOS_OS2) && defined(PYCC_GCC)
7350 {"popen2", os2emx_popen2, METH_VARARGS},
7351 {"popen3", os2emx_popen3, METH_VARARGS},
7352 {"popen4", os2emx_popen4, METH_VARARGS},
7353#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007354#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007355#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007356#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007357 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007358#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007359#ifdef HAVE_SETEUID
7360 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7361#endif /* HAVE_SETEUID */
7362#ifdef HAVE_SETEGID
7363 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7364#endif /* HAVE_SETEGID */
7365#ifdef HAVE_SETREUID
7366 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7367#endif /* HAVE_SETREUID */
7368#ifdef HAVE_SETREGID
7369 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7370#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007371#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007372 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007373#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007374#ifdef HAVE_SETGROUPS
7375 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7376#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007377#ifdef HAVE_GETPGID
7378 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7379#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007380#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007381 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007382#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007383#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007384 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007385#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007386#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007387 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007388#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007389#ifdef HAVE_GETSID
7390 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7391#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007392#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007393 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007394#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007395#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007396 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007397#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007398#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007399 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007400#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007401#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007402 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007403#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007404 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7405 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7406 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7407 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7408 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7409 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7410 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7411 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7412 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007413 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007414#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007415 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007416#endif
7417#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007418 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007419#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007420#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007421 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7422#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007423#ifdef HAVE_DEVICE_MACROS
7424 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7425 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7426 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7427#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007428#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007429 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007430#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007431#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007432 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007433#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007434#ifdef HAVE_UNSETENV
7435 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7436#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007437#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007438 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007439#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007440#ifdef HAVE_FCHDIR
7441 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7442#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007443#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007444 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007445#endif
7446#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007447 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007448#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007449#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007450#ifdef WCOREDUMP
7451 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7452#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007453#ifdef WIFCONTINUED
7454 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7455#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007456#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007457 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007458#endif /* WIFSTOPPED */
7459#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007460 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007461#endif /* WIFSIGNALED */
7462#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007463 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007464#endif /* WIFEXITED */
7465#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007466 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007467#endif /* WEXITSTATUS */
7468#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007469 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007470#endif /* WTERMSIG */
7471#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007472 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007473#endif /* WSTOPSIG */
7474#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007475#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007476 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007477#endif
7478#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007479 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007480#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007481#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007482 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007483#endif
7484#ifdef HAVE_TEMPNAM
7485 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7486#endif
7487#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007488 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007489#endif
Fred Drakec9680921999-12-13 16:37:25 +00007490#ifdef HAVE_CONFSTR
7491 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7492#endif
7493#ifdef HAVE_SYSCONF
7494 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7495#endif
7496#ifdef HAVE_FPATHCONF
7497 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7498#endif
7499#ifdef HAVE_PATHCONF
7500 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7501#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007502 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007503#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007504 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7505#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007506#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007507 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007508#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007509 {NULL, NULL} /* Sentinel */
7510};
7511
7512
Barry Warsaw4a342091996-12-19 23:50:02 +00007513static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007514ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007515{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007516 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007517}
7518
Guido van Rossumd48f2521997-12-05 22:19:34 +00007519#if defined(PYOS_OS2)
7520/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007521static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007522{
7523 APIRET rc;
7524 ULONG values[QSV_MAX+1];
7525 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007526 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007527
7528 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007529 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007530 Py_END_ALLOW_THREADS
7531
7532 if (rc != NO_ERROR) {
7533 os2_error(rc);
7534 return -1;
7535 }
7536
Fred Drake4d1e64b2002-04-15 19:40:07 +00007537 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7538 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7539 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7540 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7541 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7542 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7543 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007544
7545 switch (values[QSV_VERSION_MINOR]) {
7546 case 0: ver = "2.00"; break;
7547 case 10: ver = "2.10"; break;
7548 case 11: ver = "2.11"; break;
7549 case 30: ver = "3.00"; break;
7550 case 40: ver = "4.00"; break;
7551 case 50: ver = "5.00"; break;
7552 default:
Tim Peters885d4572001-11-28 20:27:42 +00007553 PyOS_snprintf(tmp, sizeof(tmp),
7554 "%d-%d", values[QSV_VERSION_MAJOR],
7555 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007556 ver = &tmp[0];
7557 }
7558
7559 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007560 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007561 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007562
7563 /* Add Indicator of Which Drive was Used to Boot the System */
7564 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7565 tmp[1] = ':';
7566 tmp[2] = '\0';
7567
Fred Drake4d1e64b2002-04-15 19:40:07 +00007568 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007569}
7570#endif
7571
Barry Warsaw4a342091996-12-19 23:50:02 +00007572static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007573all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007574{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007575#ifdef F_OK
7576 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007577#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007578#ifdef R_OK
7579 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007580#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007581#ifdef W_OK
7582 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007584#ifdef X_OK
7585 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007586#endif
Fred Drakec9680921999-12-13 16:37:25 +00007587#ifdef NGROUPS_MAX
7588 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7589#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007590#ifdef TMP_MAX
7591 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7592#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007593#ifdef WCONTINUED
7594 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7595#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007596#ifdef WNOHANG
7597 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007598#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007599#ifdef WUNTRACED
7600 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7601#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007602#ifdef O_RDONLY
7603 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7604#endif
7605#ifdef O_WRONLY
7606 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7607#endif
7608#ifdef O_RDWR
7609 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7610#endif
7611#ifdef O_NDELAY
7612 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7613#endif
7614#ifdef O_NONBLOCK
7615 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7616#endif
7617#ifdef O_APPEND
7618 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7619#endif
7620#ifdef O_DSYNC
7621 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7622#endif
7623#ifdef O_RSYNC
7624 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7625#endif
7626#ifdef O_SYNC
7627 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7628#endif
7629#ifdef O_NOCTTY
7630 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7631#endif
7632#ifdef O_CREAT
7633 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7634#endif
7635#ifdef O_EXCL
7636 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7637#endif
7638#ifdef O_TRUNC
7639 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7640#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007641#ifdef O_BINARY
7642 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7643#endif
7644#ifdef O_TEXT
7645 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7646#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007647#ifdef O_LARGEFILE
7648 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7649#endif
7650
Tim Peters5aa91602002-01-30 05:46:57 +00007651/* MS Windows */
7652#ifdef O_NOINHERIT
7653 /* Don't inherit in child processes. */
7654 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7655#endif
7656#ifdef _O_SHORT_LIVED
7657 /* Optimize for short life (keep in memory). */
7658 /* MS forgot to define this one with a non-underscore form too. */
7659 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7660#endif
7661#ifdef O_TEMPORARY
7662 /* Automatically delete when last handle is closed. */
7663 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7664#endif
7665#ifdef O_RANDOM
7666 /* Optimize for random access. */
7667 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7668#endif
7669#ifdef O_SEQUENTIAL
7670 /* Optimize for sequential access. */
7671 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7672#endif
7673
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007674/* GNU extensions. */
7675#ifdef O_DIRECT
7676 /* Direct disk access. */
7677 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7678#endif
7679#ifdef O_DIRECTORY
7680 /* Must be a directory. */
7681 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7682#endif
7683#ifdef O_NOFOLLOW
7684 /* Do not follow links. */
7685 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7686#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007687
Barry Warsaw5676bd12003-01-07 20:57:09 +00007688 /* These come from sysexits.h */
7689#ifdef EX_OK
7690 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007691#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007692#ifdef EX_USAGE
7693 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007694#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007695#ifdef EX_DATAERR
7696 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007697#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007698#ifdef EX_NOINPUT
7699 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007700#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007701#ifdef EX_NOUSER
7702 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007703#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007704#ifdef EX_NOHOST
7705 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007706#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007707#ifdef EX_UNAVAILABLE
7708 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007709#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007710#ifdef EX_SOFTWARE
7711 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007712#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007713#ifdef EX_OSERR
7714 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007715#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007716#ifdef EX_OSFILE
7717 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007718#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007719#ifdef EX_CANTCREAT
7720 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007721#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007722#ifdef EX_IOERR
7723 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007724#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007725#ifdef EX_TEMPFAIL
7726 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007727#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007728#ifdef EX_PROTOCOL
7729 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007730#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007731#ifdef EX_NOPERM
7732 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007733#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007734#ifdef EX_CONFIG
7735 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007736#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007737#ifdef EX_NOTFOUND
7738 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007739#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007740
Guido van Rossum246bc171999-02-01 23:54:31 +00007741#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007742#if defined(PYOS_OS2) && defined(PYCC_GCC)
7743 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7744 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7745 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7746 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7747 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7748 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7749 if (ins(d, "P_PM", (long)P_PM)) return -1;
7750 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7751 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7752 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7753 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7754 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7755 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7756 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7757 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7758 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7759 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7760 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7761 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7762 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7763#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007764 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7765 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7766 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7767 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7768 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007769#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007770#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007771
Guido van Rossumd48f2521997-12-05 22:19:34 +00007772#if defined(PYOS_OS2)
7773 if (insertvalues(d)) return -1;
7774#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007775 return 0;
7776}
7777
7778
Tim Peters5aa91602002-01-30 05:46:57 +00007779#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007780#define INITFUNC initnt
7781#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007782
7783#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007784#define INITFUNC initos2
7785#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007786
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007787#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007788#define INITFUNC initposix
7789#define MODNAME "posix"
7790#endif
7791
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007792PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007793INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007794{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007795 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007796
Fred Drake4d1e64b2002-04-15 19:40:07 +00007797 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007798 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007799 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007800
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007801 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007802 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007803 Py_XINCREF(v);
7804 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007805 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007806 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007807
Fred Drake4d1e64b2002-04-15 19:40:07 +00007808 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007809 return;
7810
Fred Drake4d1e64b2002-04-15 19:40:07 +00007811 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007812 return;
7813
Fred Drake4d1e64b2002-04-15 19:40:07 +00007814 Py_INCREF(PyExc_OSError);
7815 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007816
Guido van Rossumb3d39562000-01-31 18:41:26 +00007817#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007818 if (posix_putenv_garbage == NULL)
7819 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007820#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007821
Guido van Rossum14648392001-12-08 18:02:58 +00007822 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007823 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7824 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7825 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007826 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007827 structseq_new = StatResultType.tp_new;
7828 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007829 Py_INCREF((PyObject*) &StatResultType);
7830 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007831
Guido van Rossum14648392001-12-08 18:02:58 +00007832 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007833 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007834 Py_INCREF((PyObject*) &StatVFSResultType);
7835 PyModule_AddObject(m, "statvfs_result",
7836 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007837}