blob: 7a5ef1cab3045cd4c467ca7bc8724c2000645b76 [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
Ronald Oussorend06b6f22006-04-23 11:59:25 +000014#ifdef __APPLE__
15 /*
Victor Stinnerd6f85422010-05-05 23:33:33 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Ronald Oussorend06b6f22006-04-23 11:59:25 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
29#include "structseq.h"
Serhiy Storchakada5c2a02013-02-12 09:27:53 +020030#ifndef MS_WINDOWS
31#include "posixmodule.h"
32#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000036#endif /* defined(__VMS) */
37
Anthony Baxterac6bd462006-04-13 02:06:09 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000042PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000043"This module provides access to operating system functionality that is\n\
44standardized by the C Standard and the POSIX standard (a thinly\n\
45disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000046corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000048#ifndef Py_USING_UNICODE
49/* This is used in signatures of functions. */
50#define Py_UNICODE void
51#endif
52
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000053#if defined(PYOS_OS2)
54#define INCL_DOS
55#define INCL_DOSERRORS
56#define INCL_DOSPROCESS
57#define INCL_NOPMAPI
58#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#if defined(PYCC_GCC)
60#include <ctype.h>
61#include <io.h>
62#include <stdio.h>
63#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000064#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000065#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000066#endif
67
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000068#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000070#endif /* HAVE_SYS_TYPES_H */
71
72#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000073#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000074#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000075
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#ifdef HAVE_SYS_WAIT_H
Victor Stinnerd6f85422010-05-05 23:33:33 +000077#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000078#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000079
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000080#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000081#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000082#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000083
Guido van Rossumb6775db1994-08-01 11:34:53 +000084#ifdef HAVE_FCNTL_H
85#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000086#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Guido van Rossuma6535fd2001-10-18 19:44:10 +000088#ifdef HAVE_GRP_H
89#include <grp.h>
90#endif
91
Barry Warsaw5676bd12003-01-07 20:57:09 +000092#ifdef HAVE_SYSEXITS_H
93#include <sysexits.h>
94#endif /* HAVE_SYSEXITS_H */
95
Anthony Baxter8a560de2004-10-13 15:30:56 +000096#ifdef HAVE_SYS_LOADAVG_H
97#include <sys/loadavg.h>
98#endif
99
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000100/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000101/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000103#include <process.h>
104#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000105#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000106#define HAVE_GETCWD 1
107#define HAVE_OPENDIR 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000108#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#if defined(__OS2__)
110#define HAVE_EXECV 1
111#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000112#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000113#include <process.h>
114#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000115#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_EXECV 1
117#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118#define HAVE_OPENDIR 1
119#define HAVE_PIPE 1
120#define HAVE_POPEN 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000121#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000122#define HAVE_WAIT 1
123#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000124#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000125#define HAVE_GETCWD 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000126#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_EXECV 1
128#define HAVE_PIPE 1
129#define HAVE_POPEN 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000130#define HAVE_SYSTEM 1
131#define HAVE_CWAIT 1
132#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000133#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000134#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000135#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
136/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinnerd6f85422010-05-05 23:33:33 +0000137#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000138/* Unix functions that the configure script doesn't check for */
139#define HAVE_EXECV 1
140#define HAVE_FORK 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000141#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000142#define HAVE_FORK1 1
143#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144#define HAVE_GETCWD 1
145#define HAVE_GETEGID 1
146#define HAVE_GETEUID 1
147#define HAVE_GETGID 1
148#define HAVE_GETPPID 1
149#define HAVE_GETUID 1
150#define HAVE_KILL 1
151#define HAVE_OPENDIR 1
152#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000153#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000155#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000156#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157#define HAVE_WAIT 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000158#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000159#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#endif /* _MSC_VER */
161#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000162#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000163#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000164
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000166
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000167#if defined(__sgi)&&_COMPILER_VERSION>=700
168/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
169 (default) */
170extern char *ctermid_r(char *);
171#endif
172
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000173#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000174#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000177#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000179#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000180extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000182#endif
183#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000184extern int chdir(char *);
185extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chdir(const char *);
188extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000189#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000190#ifdef __BORLANDC__
191extern int chmod(const char *, int);
192#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000194#endif
Christian Heimes36281872007-11-30 21:11:28 +0000195/*#ifdef HAVE_FCHMOD
196extern int fchmod(int, mode_t);
197#endif*/
198/*#ifdef HAVE_LCHMOD
199extern int lchmod(const char *, mode_t);
200#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000201extern int chown(const char *, uid_t, gid_t);
202extern char *getcwd(char *, int);
203extern char *strerror(int);
204extern int link(const char *, const char *);
205extern int rename(const char *, const char *);
206extern int stat(const char *, struct stat *);
207extern int unlink(const char *);
208extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000211#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000213extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000216
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#ifdef HAVE_UTIME_H
220#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000223#ifdef HAVE_SYS_UTIME_H
224#include <sys/utime.h>
225#define HAVE_UTIME_H /* pretend we do for the rest of this file */
226#endif /* HAVE_SYS_UTIME_H */
227
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#ifdef HAVE_SYS_TIMES_H
229#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000230#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231
232#ifdef HAVE_SYS_PARAM_H
233#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235
236#ifdef HAVE_SYS_UTSNAME_H
237#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) strlen((dirent)->d_name)
243#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000244#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000245#include <direct.h>
246#define NAMLEN(dirent) strlen((dirent)->d_name)
247#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000250#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000251#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000256#endif
257#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259#endif
260#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000263#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000265#endif
266#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000268#endif
269#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000271#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000272#include "osdefs.h"
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000273#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <windows.h>
Victor Stinnerd6f85422010-05-05 23:33:33 +0000275#include <shellapi.h> /* for ShellExecute() */
276#define popen _popen
277#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279
Guido van Rossumd48f2521997-12-05 22:19:34 +0000280#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000282#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283
Tim Petersbc2e10e2002-03-03 23:17:02 +0000284#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000285#if defined(PATH_MAX) && PATH_MAX > 1024
286#define MAXPATHLEN PATH_MAX
287#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000289#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#endif /* MAXPATHLEN */
291
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000292#ifdef UNION_WAIT
293/* Emulate some macros on systems that have a union instead of macros */
294
295#ifndef WIFEXITED
296#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
297#endif
298
299#ifndef WEXITSTATUS
300#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
301#endif
302
303#ifndef WTERMSIG
304#define WTERMSIG(u_wait) ((u_wait).w_termsig)
305#endif
306
Neal Norwitzd5a37542006-03-20 06:48:34 +0000307#define WAIT_TYPE union wait
308#define WAIT_STATUS_INT(s) (s.w_status)
309
310#else /* !UNION_WAIT */
311#define WAIT_TYPE int
312#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000313#endif /* UNION_WAIT */
314
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000315/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou4fe38582009-05-24 12:15:04 +0000316#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000317#define PARSE_PID "i"
318#define PyLong_FromPid PyInt_FromLong
319#define PyLong_AsPid PyInt_AsLong
320#elif SIZEOF_PID_T == SIZEOF_LONG
321#define PARSE_PID "l"
322#define PyLong_FromPid PyInt_FromLong
323#define PyLong_AsPid PyInt_AsLong
324#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
325#define PARSE_PID "L"
326#define PyLong_FromPid PyLong_FromLongLong
327#define PyLong_AsPid PyInt_AsLongLong
328#else
329#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000330#endif /* SIZEOF_PID_T */
331
Greg Wardb48bc172000-03-01 21:51:56 +0000332/* Don't use the "_r" form if we don't need it (also, won't have a
333 prototype for it, at least on Solaris -- maybe others as well?). */
334#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
335#define USE_CTERMID_R
336#endif
337
338#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
339#define USE_TMPNAM_R
340#endif
341
Tim Peters11b23062003-04-23 02:39:17 +0000342#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000343#include <sys/mkdev.h>
344#else
345#if defined(MAJOR_IN_SYSMACROS)
346#include <sys/sysmacros.h>
347#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000348#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
349#include <sys/mkdev.h>
350#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000351#endif
Fred Drake699f3522000-06-29 21:12:41 +0000352
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200353
354#ifndef MS_WINDOWS
355PyObject *
356_PyInt_FromUid(uid_t uid)
357{
358 if (uid <= LONG_MAX)
359 return PyInt_FromLong(uid);
Benjamin Peterson4d7fc3c2013-03-23 15:35:24 -0500360 return PyLong_FromUnsignedLong(uid);
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200361}
362
363PyObject *
364_PyInt_FromGid(gid_t gid)
365{
366 if (gid <= LONG_MAX)
367 return PyInt_FromLong(gid);
Benjamin Peterson4d7fc3c2013-03-23 15:35:24 -0500368 return PyLong_FromUnsignedLong(gid);
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200369}
370
371int
372_Py_Uid_Converter(PyObject *obj, void *p)
373{
374 int overflow;
375 long result;
376 if (PyFloat_Check(obj)) {
377 PyErr_SetString(PyExc_TypeError,
378 "integer argument expected, got float");
379 return 0;
380 }
381 result = PyLong_AsLongAndOverflow(obj, &overflow);
382 if (overflow < 0)
383 goto OverflowDown;
384 if (!overflow && result == -1) {
385 /* error or -1 */
386 if (PyErr_Occurred())
387 return 0;
388 *(uid_t *)p = (uid_t)-1;
389 }
390 else {
391 /* unsigned uid_t */
392 unsigned long uresult;
393 if (overflow > 0) {
394 uresult = PyLong_AsUnsignedLong(obj);
395 if (PyErr_Occurred()) {
396 if (PyErr_ExceptionMatches(PyExc_OverflowError))
397 goto OverflowUp;
398 return 0;
399 }
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200400 } else {
401 if (result < 0)
402 goto OverflowDown;
403 uresult = result;
404 }
405 if (sizeof(uid_t) < sizeof(long) &&
406 (unsigned long)(uid_t)uresult != uresult)
407 goto OverflowUp;
408 *(uid_t *)p = (uid_t)uresult;
409 }
410 return 1;
411
412OverflowDown:
413 PyErr_SetString(PyExc_OverflowError,
414 "user id is less than minimum");
415 return 0;
416
417OverflowUp:
418 PyErr_SetString(PyExc_OverflowError,
419 "user id is greater than maximum");
420 return 0;
421}
422
423int
424_Py_Gid_Converter(PyObject *obj, void *p)
425{
426 int overflow;
427 long result;
428 if (PyFloat_Check(obj)) {
429 PyErr_SetString(PyExc_TypeError,
430 "integer argument expected, got float");
431 return 0;
432 }
433 result = PyLong_AsLongAndOverflow(obj, &overflow);
434 if (overflow < 0)
435 goto OverflowDown;
436 if (!overflow && result == -1) {
437 /* error or -1 */
438 if (PyErr_Occurred())
439 return 0;
440 *(gid_t *)p = (gid_t)-1;
441 }
442 else {
443 /* unsigned gid_t */
444 unsigned long uresult;
445 if (overflow > 0) {
446 uresult = PyLong_AsUnsignedLong(obj);
447 if (PyErr_Occurred()) {
448 if (PyErr_ExceptionMatches(PyExc_OverflowError))
449 goto OverflowUp;
450 return 0;
451 }
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200452 } else {
453 if (result < 0)
454 goto OverflowDown;
455 uresult = result;
456 }
457 if (sizeof(gid_t) < sizeof(long) &&
458 (unsigned long)(gid_t)uresult != uresult)
459 goto OverflowUp;
460 *(gid_t *)p = (gid_t)uresult;
461 }
462 return 1;
463
464OverflowDown:
465 PyErr_SetString(PyExc_OverflowError,
466 "group id is less than minimum");
467 return 0;
468
469OverflowUp:
470 PyErr_SetString(PyExc_OverflowError,
471 "group id is greater than maximum");
472 return 0;
473}
474#endif /* MS_WINDOWS */
475
476
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000477#if defined _MSC_VER && _MSC_VER >= 1400
478/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200479 * valid and raise an assertion if it isn't.
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000480 * Normally, an invalid fd is likely to be a C program error and therefore
481 * an assertion can be useful, but it does contradict the POSIX standard
482 * which for write(2) states:
483 * "Otherwise, -1 shall be returned and errno set to indicate the error."
484 * "[EBADF] The fildes argument is not a valid file descriptor open for
485 * writing."
486 * Furthermore, python allows the user to enter any old integer
487 * as a fd and should merely raise a python exception on error.
488 * The Microsoft CRT doesn't provide an official way to check for the
489 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinnerd6f85422010-05-05 23:33:33 +0000490 * by using the exported __pinfo data member and knowledge of the
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000491 * internal structures involved.
492 * The structures below must be updated for each version of visual studio
493 * according to the file internal.h in the CRT source, until MS comes
494 * up with a less hacky way to do this.
495 * (all of this is to avoid globally modifying the CRT behaviour using
496 * _set_invalid_parameter_handler() and _CrtSetReportMode())
497 */
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000498/* The actual size of the structure is determined at runtime.
499 * Only the first items must be present.
500 */
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000501typedef struct {
Victor Stinnerd6f85422010-05-05 23:33:33 +0000502 intptr_t osfhnd;
503 char osfile;
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000504} my_ioinfo;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000505
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000506extern __declspec(dllimport) char * __pioinfo[];
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000507#define IOINFO_L2E 5
508#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
509#define IOINFO_ARRAYS 64
510#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
511#define FOPEN 0x01
512#define _NO_CONSOLE_FILENO (intptr_t)-2
513
514/* This function emulates what the windows CRT does to validate file handles */
515int
516_PyVerify_fd(int fd)
517{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000518 const int i1 = fd >> IOINFO_L2E;
519 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000520
Victor Stinnerd6f85422010-05-05 23:33:33 +0000521 static int sizeof_ioinfo = 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000522
Victor Stinnerd6f85422010-05-05 23:33:33 +0000523 /* Determine the actual size of the ioinfo structure,
524 * as used by the CRT loaded in memory
525 */
526 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
527 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
528 }
529 if (sizeof_ioinfo == 0) {
530 /* This should not happen... */
531 goto fail;
532 }
533
534 /* See that it isn't a special CLEAR fileno */
535 if (fd != _NO_CONSOLE_FILENO) {
536 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
537 * we check pointer validity and other info
538 */
539 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
540 /* finally, check that the file is open */
541 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
542 if (info->osfile & FOPEN) {
543 return 1;
544 }
545 }
546 }
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000547 fail:
Victor Stinnerd6f85422010-05-05 23:33:33 +0000548 errno = EBADF;
549 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000550}
551
552/* the special case of checking dup2. The target fd must be in a sensible range */
553static int
554_PyVerify_fd_dup2(int fd1, int fd2)
555{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000556 if (!_PyVerify_fd(fd1))
557 return 0;
558 if (fd2 == _NO_CONSOLE_FILENO)
559 return 0;
560 if ((unsigned)fd2 < _NHANDLE_)
561 return 1;
562 else
563 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000564}
565#else
566/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
567#define _PyVerify_fd_dup2(A, B) (1)
568#endif
569
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100571#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +0000572/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100573** environ directly, we must obtain it with _NSGetEnviron(). See also
574** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +0000575*/
576#include <crt_externs.h>
577static char **environ;
578#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000580#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581
Barry Warsaw53699e91996-12-10 23:23:01 +0000582static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000583convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000585 PyObject *d;
586 char **e;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000587#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +0000588 APIRET rc;
589 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
590#endif
591 d = PyDict_New();
592 if (d == NULL)
593 return NULL;
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100594#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinnerd6f85422010-05-05 23:33:33 +0000595 if (environ == NULL)
596 environ = *_NSGetEnviron();
597#endif
598 if (environ == NULL)
599 return d;
600 /* This part ignores errors */
601 for (e = environ; *e != NULL; e++) {
602 PyObject *k;
603 PyObject *v;
604 char *p = strchr(*e, '=');
605 if (p == NULL)
606 continue;
607 k = PyString_FromStringAndSize(*e, (int)(p-*e));
608 if (k == NULL) {
609 PyErr_Clear();
610 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000611 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000612 v = PyString_FromString(p+1);
613 if (v == NULL) {
614 PyErr_Clear();
615 Py_DECREF(k);
616 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000617 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000618 if (PyDict_GetItem(d, k) == NULL) {
619 if (PyDict_SetItem(d, k, v) != 0)
620 PyErr_Clear();
621 }
622 Py_DECREF(k);
623 Py_DECREF(v);
624 }
625#if defined(PYOS_OS2)
626 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
627 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
628 PyObject *v = PyString_FromString(buffer);
629 PyDict_SetItemString(d, "BEGINLIBPATH", v);
630 Py_DECREF(v);
631 }
632 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
633 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
634 PyObject *v = PyString_FromString(buffer);
635 PyDict_SetItemString(d, "ENDLIBPATH", v);
636 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000637 }
638#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000639 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640}
641
642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643/* Set a POSIX-specific error from errno, and return NULL */
644
Barry Warsawd58d7641998-07-23 16:14:40 +0000645static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000647{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000648 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Barry Warsawd58d7641998-07-23 16:14:40 +0000650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000651posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000652{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000654}
655
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000656#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000657static PyObject *
658posix_error_with_unicode_filename(Py_UNICODE* name)
659{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000660 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000662#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000663
664
Mark Hammondef8b6542001-05-13 08:04:26 +0000665static PyObject *
666posix_error_with_allocated_filename(char* name)
667{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000668 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
669 PyMem_Free(name);
670 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000671}
672
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000673#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000674static PyObject *
675win32_error(char* function, char* filename)
676{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000677 /* XXX We should pass the function name along in the future.
678 (_winreg.c also wants to pass the function name.)
679 This would however require an additional param to the
680 Windows error object, which is non-trivial.
681 */
682 errno = GetLastError();
683 if (filename)
684 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
685 else
686 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000687}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000688
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000689static PyObject *
690win32_error_unicode(char* function, Py_UNICODE* filename)
691{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000692 /* XXX - see win32_error for comments on 'function' */
693 errno = GetLastError();
694 if (filename)
695 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
696 else
697 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000698}
699
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000700static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000701convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000702{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000703 if (PyUnicode_CheckExact(*param))
704 Py_INCREF(*param);
705 else if (PyUnicode_Check(*param))
706 /* For a Unicode subtype that's not a Unicode object,
707 return a true Unicode object with the same data. */
708 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
709 PyUnicode_GET_SIZE(*param));
710 else
711 *param = PyUnicode_FromEncodedObject(*param,
712 Py_FileSystemDefaultEncoding,
713 "strict");
714 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000715}
716
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000717#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718
Guido van Rossumd48f2521997-12-05 22:19:34 +0000719#if defined(PYOS_OS2)
720/**********************************************************************
721 * Helper Function to Trim and Format OS/2 Messages
722 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000723static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000724os2_formatmsg(char *msgbuf, int msglen, char *reason)
725{
726 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
727
728 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
Victor Stinner862490a2010-05-06 00:03:44 +0000729 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
Guido van Rossumd48f2521997-12-05 22:19:34 +0000730
Victor Stinner862490a2010-05-06 00:03:44 +0000731 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
732 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000733 }
734
735 /* Add Optional Reason Text */
736 if (reason) {
737 strcat(msgbuf, " : ");
738 strcat(msgbuf, reason);
739 }
740}
741
742/**********************************************************************
743 * Decode an OS/2 Operating System Error Code
744 *
745 * A convenience function to lookup an OS/2 error code and return a
746 * text message we can use to raise a Python exception.
747 *
748 * Notes:
749 * The messages for errors returned from the OS/2 kernel reside in
750 * the file OSO001.MSG in the \OS2 directory hierarchy.
751 *
752 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000753static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000754os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
755{
756 APIRET rc;
757 ULONG msglen;
758
759 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
760 Py_BEGIN_ALLOW_THREADS
761 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
762 errorcode, "oso001.msg", &msglen);
763 Py_END_ALLOW_THREADS
764
765 if (rc == NO_ERROR)
766 os2_formatmsg(msgbuf, msglen, reason);
767 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000768 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000769 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000770
771 return msgbuf;
772}
773
774/* Set an OS/2-specific error and return NULL. OS/2 kernel
775 errors are not in a global variable e.g. 'errno' nor are
776 they congruent with posix error numbers. */
777
Victor Stinnerd6f85422010-05-05 23:33:33 +0000778static PyObject *
779os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000780{
781 char text[1024];
782 PyObject *v;
783
784 os2_strerror(text, sizeof(text), code, "");
785
786 v = Py_BuildValue("(is)", code, text);
787 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000788 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000789 Py_DECREF(v);
790 }
791 return NULL; /* Signal to Python that an Exception is Pending */
792}
793
794#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795
796/* POSIX generic methods */
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000799posix_fildes(PyObject *fdobj, int (*func)(int))
800{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000801 int fd;
802 int res;
803 fd = PyObject_AsFileDescriptor(fdobj);
804 if (fd < 0)
805 return NULL;
806 if (!_PyVerify_fd(fd))
807 return posix_error();
808 Py_BEGIN_ALLOW_THREADS
809 res = (*func)(fd);
810 Py_END_ALLOW_THREADS
811 if (res < 0)
812 return posix_error();
813 Py_INCREF(Py_None);
814 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000815}
Guido van Rossum21142a01999-01-08 21:05:37 +0000816
817static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000818posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000820 char *path1 = NULL;
821 int res;
822 if (!PyArg_ParseTuple(args, format,
823 Py_FileSystemDefaultEncoding, &path1))
824 return NULL;
825 Py_BEGIN_ALLOW_THREADS
826 res = (*func)(path1);
827 Py_END_ALLOW_THREADS
828 if (res < 0)
829 return posix_error_with_allocated_filename(path1);
830 PyMem_Free(path1);
831 Py_INCREF(Py_None);
832 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833}
834
Barry Warsaw53699e91996-12-10 23:23:01 +0000835static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000836posix_2str(PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000837 char *format,
838 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000840 char *path1 = NULL, *path2 = NULL;
841 int res;
842 if (!PyArg_ParseTuple(args, format,
843 Py_FileSystemDefaultEncoding, &path1,
844 Py_FileSystemDefaultEncoding, &path2))
845 return NULL;
846 Py_BEGIN_ALLOW_THREADS
847 res = (*func)(path1, path2);
848 Py_END_ALLOW_THREADS
849 PyMem_Free(path1);
850 PyMem_Free(path2);
851 if (res != 0)
852 /* XXX how to report both path1 and path2??? */
853 return posix_error();
854 Py_INCREF(Py_None);
855 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856}
857
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000858#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000859static PyObject*
Victor Stinnerd6f85422010-05-05 23:33:33 +0000860win32_1str(PyObject* args, char* func,
861 char* format, BOOL (__stdcall *funcA)(LPCSTR),
862 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000863{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000864 PyObject *uni;
865 char *ansi;
866 BOOL result;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +0000867
Victor Stinnerd6f85422010-05-05 23:33:33 +0000868 if (!PyArg_ParseTuple(args, wformat, &uni))
869 PyErr_Clear();
870 else {
871 Py_BEGIN_ALLOW_THREADS
872 result = funcW(PyUnicode_AsUnicode(uni));
873 Py_END_ALLOW_THREADS
874 if (!result)
875 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
876 Py_INCREF(Py_None);
877 return Py_None;
878 }
879 if (!PyArg_ParseTuple(args, format, &ansi))
880 return NULL;
881 Py_BEGIN_ALLOW_THREADS
882 result = funcA(ansi);
883 Py_END_ALLOW_THREADS
884 if (!result)
885 return win32_error(func, ansi);
886 Py_INCREF(Py_None);
887 return Py_None;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000888
889}
890
891/* This is a reimplementation of the C library's chdir function,
892 but one that produces Win32 errors instead of DOS error codes.
893 chdir is essentially a wrapper around SetCurrentDirectory; however,
894 it also needs to set "magic" environment variables indicating
895 the per-drive current directory, which are of the form =<drive>: */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000896static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000897win32_chdir(LPCSTR path)
898{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000899 char new_path[MAX_PATH+1];
900 int result;
901 char env[4] = "=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000902
Victor Stinnerd6f85422010-05-05 23:33:33 +0000903 if(!SetCurrentDirectoryA(path))
904 return FALSE;
905 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
906 if (!result)
907 return FALSE;
908 /* In the ANSI API, there should not be any paths longer
909 than MAX_PATH. */
910 assert(result <= MAX_PATH+1);
911 if (strncmp(new_path, "\\\\", 2) == 0 ||
912 strncmp(new_path, "//", 2) == 0)
913 /* UNC path, nothing to do. */
914 return TRUE;
915 env[1] = new_path[0];
916 return SetEnvironmentVariableA(env, new_path);
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000917}
918
919/* The Unicode version differs from the ANSI version
920 since the current directory might exceed MAX_PATH characters */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000921static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000922win32_wchdir(LPCWSTR path)
923{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000924 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
925 int result;
926 wchar_t env[4] = L"=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000927
Victor Stinnerd6f85422010-05-05 23:33:33 +0000928 if(!SetCurrentDirectoryW(path))
929 return FALSE;
930 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
931 if (!result)
932 return FALSE;
933 if (result > MAX_PATH+1) {
934 new_path = malloc(result * sizeof(wchar_t));
935 if (!new_path) {
936 SetLastError(ERROR_OUTOFMEMORY);
937 return FALSE;
938 }
939 result = GetCurrentDirectoryW(result, new_path);
940 if (!result) {
941 free(new_path);
942 return FALSE;
943 }
944 }
945 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
946 wcsncmp(new_path, L"//", 2) == 0)
947 /* UNC path, nothing to do. */
948 return TRUE;
949 env[1] = new_path[0];
950 result = SetEnvironmentVariableW(env, new_path);
951 if (new_path != _new_path)
952 free(new_path);
953 return result;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000954}
955#endif
956
Antoine Pitrouff48c0a2011-07-01 22:56:03 +0200957/* choose the appropriate stat and fstat functions and return structs */
958#undef STAT
959#undef FSTAT
960#undef STRUCT_STAT
961#if defined(MS_WIN64) || defined(MS_WINDOWS)
962# define STAT win32_stat
963# define FSTAT win32_fstat
964# define STRUCT_STAT struct win32_stat
965#else
966# define STAT stat
967# define FSTAT fstat
968# define STRUCT_STAT struct stat
969#endif
970
Martin v. Löwis14694662006-02-03 12:54:16 +0000971#ifdef MS_WINDOWS
972/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
973 - time stamps are restricted to second resolution
974 - file modification times suffer from forth-and-back conversions between
975 UTC and local time
976 Therefore, we implement our own stat, based on the Win32 API directly.
977*/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000978#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000979
980struct win32_stat{
981 int st_dev;
982 __int64 st_ino;
983 unsigned short st_mode;
984 int st_nlink;
985 int st_uid;
986 int st_gid;
987 int st_rdev;
988 __int64 st_size;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000989 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000990 int st_atime_nsec;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000991 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000992 int st_mtime_nsec;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000993 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 int st_ctime_nsec;
995};
996
997static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
998
999static void
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001000FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001001{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001002 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1003 /* Cannot simply cast and dereference in_ptr,
1004 since it might not be aligned properly */
1005 __int64 in;
1006 memcpy(&in, in_ptr, sizeof(in));
1007 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001008 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001009}
1010
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001011static void
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001012time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001013{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001014 /* XXX endianness */
1015 __int64 out;
1016 out = time_in + secs_between_epochs;
1017 out = out * 10000000 + nsec_in / 100;
1018 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001019}
1020
Martin v. Löwis14694662006-02-03 12:54:16 +00001021/* Below, we *know* that ugo+r is 0444 */
1022#if _S_IREAD != 0400
1023#error Unsupported C library
1024#endif
1025static int
1026attributes_to_mode(DWORD attr)
1027{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001028 int m = 0;
1029 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1030 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1031 else
1032 m |= _S_IFREG;
1033 if (attr & FILE_ATTRIBUTE_READONLY)
1034 m |= 0444;
1035 else
1036 m |= 0666;
1037 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001038}
1039
1040static int
1041attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
1042{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001043 memset(result, 0, sizeof(*result));
1044 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1045 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1046 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1047 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1048 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +00001049
Victor Stinnerd6f85422010-05-05 23:33:33 +00001050 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001051}
1052
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001053static BOOL
1054attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1055{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001056 HANDLE hFindFile;
1057 WIN32_FIND_DATAA FileData;
1058 hFindFile = FindFirstFileA(pszFile, &FileData);
1059 if (hFindFile == INVALID_HANDLE_VALUE)
1060 return FALSE;
1061 FindClose(hFindFile);
1062 pfad->dwFileAttributes = FileData.dwFileAttributes;
1063 pfad->ftCreationTime = FileData.ftCreationTime;
1064 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1065 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1066 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1067 pfad->nFileSizeLow = FileData.nFileSizeLow;
1068 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001069}
1070
1071static BOOL
1072attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1073{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001074 HANDLE hFindFile;
1075 WIN32_FIND_DATAW FileData;
1076 hFindFile = FindFirstFileW(pszFile, &FileData);
1077 if (hFindFile == INVALID_HANDLE_VALUE)
1078 return FALSE;
1079 FindClose(hFindFile);
1080 pfad->dwFileAttributes = FileData.dwFileAttributes;
1081 pfad->ftCreationTime = FileData.ftCreationTime;
1082 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1083 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1084 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1085 pfad->nFileSizeLow = FileData.nFileSizeLow;
1086 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001087}
1088
Victor Stinnerd6f85422010-05-05 23:33:33 +00001089static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001090win32_stat(const char* path, struct win32_stat *result)
1091{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001092 WIN32_FILE_ATTRIBUTE_DATA info;
1093 int code;
1094 char *dot;
1095 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1096 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1097 /* Protocol violation: we explicitly clear errno, instead of
1098 setting it to a POSIX error. Callers should use GetLastError. */
1099 errno = 0;
1100 return -1;
1101 } else {
1102 /* Could not get attributes on open file. Fall back to
1103 reading the directory. */
1104 if (!attributes_from_dir(path, &info)) {
1105 /* Very strange. This should not fail now */
1106 errno = 0;
1107 return -1;
1108 }
1109 }
1110 }
1111 code = attribute_data_to_stat(&info, result);
1112 if (code != 0)
1113 return code;
1114 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1115 dot = strrchr(path, '.');
1116 if (dot) {
1117 if (stricmp(dot, ".bat") == 0 ||
1118 stricmp(dot, ".cmd") == 0 ||
1119 stricmp(dot, ".exe") == 0 ||
1120 stricmp(dot, ".com") == 0)
1121 result->st_mode |= 0111;
1122 }
1123 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001124}
1125
Victor Stinnerd6f85422010-05-05 23:33:33 +00001126static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001127win32_wstat(const wchar_t* path, struct win32_stat *result)
1128{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001129 int code;
1130 const wchar_t *dot;
1131 WIN32_FILE_ATTRIBUTE_DATA info;
1132 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1133 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1134 /* Protocol violation: we explicitly clear errno, instead of
1135 setting it to a POSIX error. Callers should use GetLastError. */
1136 errno = 0;
1137 return -1;
1138 } else {
1139 /* Could not get attributes on open file. Fall back to
1140 reading the directory. */
1141 if (!attributes_from_dir_w(path, &info)) {
1142 /* Very strange. This should not fail now */
1143 errno = 0;
1144 return -1;
1145 }
1146 }
1147 }
1148 code = attribute_data_to_stat(&info, result);
1149 if (code < 0)
1150 return code;
1151 /* Set IFEXEC if it is an .exe, .bat, ... */
1152 dot = wcsrchr(path, '.');
1153 if (dot) {
1154 if (_wcsicmp(dot, L".bat") == 0 ||
1155 _wcsicmp(dot, L".cmd") == 0 ||
1156 _wcsicmp(dot, L".exe") == 0 ||
1157 _wcsicmp(dot, L".com") == 0)
1158 result->st_mode |= 0111;
1159 }
1160 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001161}
1162
1163static int
1164win32_fstat(int file_number, struct win32_stat *result)
1165{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001166 BY_HANDLE_FILE_INFORMATION info;
1167 HANDLE h;
1168 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001169
Victor Stinnerd6f85422010-05-05 23:33:33 +00001170 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001171
Victor Stinnerd6f85422010-05-05 23:33:33 +00001172 /* Protocol violation: we explicitly clear errno, instead of
1173 setting it to a POSIX error. Callers should use GetLastError. */
1174 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001175
Victor Stinnerd6f85422010-05-05 23:33:33 +00001176 if (h == INVALID_HANDLE_VALUE) {
1177 /* This is really a C library error (invalid file handle).
1178 We set the Win32 error to the closes one matching. */
1179 SetLastError(ERROR_INVALID_HANDLE);
1180 return -1;
1181 }
1182 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001183
Victor Stinnerd6f85422010-05-05 23:33:33 +00001184 type = GetFileType(h);
1185 if (type == FILE_TYPE_UNKNOWN) {
1186 DWORD error = GetLastError();
1187 if (error != 0) {
1188 return -1;
1189 }
1190 /* else: valid but unknown file */
1191 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001192
Victor Stinnerd6f85422010-05-05 23:33:33 +00001193 if (type != FILE_TYPE_DISK) {
1194 if (type == FILE_TYPE_CHAR)
1195 result->st_mode = _S_IFCHR;
1196 else if (type == FILE_TYPE_PIPE)
1197 result->st_mode = _S_IFIFO;
1198 return 0;
1199 }
1200
1201 if (!GetFileInformationByHandle(h, &info)) {
1202 return -1;
1203 }
1204
1205 /* similar to stat() */
1206 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1207 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1208 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1209 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1210 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1211 /* specific to fstat() */
1212 result->st_nlink = info.nNumberOfLinks;
1213 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1214 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001215}
1216
1217#endif /* MS_WINDOWS */
1218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001219PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001220"stat_result: Result from stat or lstat.\n\n\
1221This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001222 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001223or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1224\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001225Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1226or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001227\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001228See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001229
1230static PyStructSequence_Field stat_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001231 {"st_mode", "protection bits"},
1232 {"st_ino", "inode"},
1233 {"st_dev", "device"},
1234 {"st_nlink", "number of hard links"},
1235 {"st_uid", "user ID of owner"},
1236 {"st_gid", "group ID of owner"},
1237 {"st_size", "total size, in bytes"},
1238 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1239 {NULL, "integer time of last access"},
1240 {NULL, "integer time of last modification"},
1241 {NULL, "integer time of last change"},
1242 {"st_atime", "time of last access"},
1243 {"st_mtime", "time of last modification"},
1244 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001245#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001246 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001247#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001248#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001249 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001250#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001251#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001252 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001253#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001254#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001255 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001256#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001257#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001258 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001259#endif
1260#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001261 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001262#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001263 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001264};
1265
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001266#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001267#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001268#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001269#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001270#endif
1271
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001272#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001273#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1274#else
1275#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1276#endif
1277
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001278#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1280#else
1281#define ST_RDEV_IDX ST_BLOCKS_IDX
1282#endif
1283
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001284#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1285#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1286#else
1287#define ST_FLAGS_IDX ST_RDEV_IDX
1288#endif
1289
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001290#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001291#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001292#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001293#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001294#endif
1295
1296#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1297#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1298#else
1299#define ST_BIRTHTIME_IDX ST_GEN_IDX
1300#endif
1301
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001302static PyStructSequence_Desc stat_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001303 "stat_result", /* name */
1304 stat_result__doc__, /* doc */
1305 stat_result_fields,
1306 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001307};
1308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001309PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1311This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001312 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001313or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001315See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001316
1317static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001318 {"f_bsize", },
1319 {"f_frsize", },
1320 {"f_blocks", },
1321 {"f_bfree", },
1322 {"f_bavail", },
1323 {"f_files", },
1324 {"f_ffree", },
1325 {"f_favail", },
1326 {"f_flag", },
1327 {"f_namemax",},
1328 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001329};
1330
1331static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001332 "statvfs_result", /* name */
1333 statvfs_result__doc__, /* doc */
1334 statvfs_result_fields,
1335 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336};
1337
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001338static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001339static PyTypeObject StatResultType;
1340static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001341static newfunc structseq_new;
1342
1343static PyObject *
1344statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1345{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001346 PyStructSequence *result;
1347 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001348
Victor Stinnerd6f85422010-05-05 23:33:33 +00001349 result = (PyStructSequence*)structseq_new(type, args, kwds);
1350 if (!result)
1351 return NULL;
1352 /* If we have been initialized from a tuple,
1353 st_?time might be set to None. Initialize it
1354 from the int slots. */
1355 for (i = 7; i <= 9; i++) {
1356 if (result->ob_item[i+3] == Py_None) {
1357 Py_DECREF(Py_None);
1358 Py_INCREF(result->ob_item[i]);
1359 result->ob_item[i+3] = result->ob_item[i];
1360 }
1361 }
1362 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001363}
1364
1365
1366
1367/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001368static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001369
1370PyDoc_STRVAR(stat_float_times__doc__,
1371"stat_float_times([newval]) -> oldval\n\n\
1372Determine whether os.[lf]stat represents time stamps as float objects.\n\
1373If newval is True, future calls to stat() return floats, if it is False,\n\
1374future calls return ints. \n\
1375If newval is omitted, return the current setting.\n");
1376
1377static PyObject*
1378stat_float_times(PyObject* self, PyObject *args)
1379{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001380 int newval = -1;
1381 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1382 return NULL;
1383 if (newval == -1)
1384 /* Return old value */
1385 return PyBool_FromLong(_stat_float_times);
1386 _stat_float_times = newval;
1387 Py_INCREF(Py_None);
1388 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001389}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001390
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001391static void
1392fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1393{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001394 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001395#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinnerd6f85422010-05-05 23:33:33 +00001396 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001397#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001398 ival = PyInt_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001399#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001400 if (!ival)
1401 return;
1402 if (_stat_float_times) {
1403 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1404 } else {
1405 fval = ival;
1406 Py_INCREF(fval);
1407 }
1408 PyStructSequence_SET_ITEM(v, index, ival);
1409 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001410}
1411
Tim Peters5aa91602002-01-30 05:46:57 +00001412/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001413 (used by posix_stat() and posix_fstat()) */
1414static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001415_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001416{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001417 unsigned long ansec, mnsec, cnsec;
1418 PyObject *v = PyStructSequence_New(&StatResultType);
1419 if (v == NULL)
1420 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001421
Victor Stinnerd6f85422010-05-05 23:33:33 +00001422 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001423#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001424 PyStructSequence_SET_ITEM(v, 1,
1425 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001426#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001427 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001428#endif
1429#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001430 PyStructSequence_SET_ITEM(v, 2,
1431 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001432#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001433 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001434#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001435 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02001436#if defined(MS_WINDOWS)
1437 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong(0));
1438 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong(0));
1439#else
1440 PyStructSequence_SET_ITEM(v, 4, _PyInt_FromUid(st->st_uid));
1441 PyStructSequence_SET_ITEM(v, 5, _PyInt_FromGid(st->st_gid));
1442#endif
Fred Drake699f3522000-06-29 21:12:41 +00001443#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001444 PyStructSequence_SET_ITEM(v, 6,
1445 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001446#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001447 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001448#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001449
Martin v. Löwis14694662006-02-03 12:54:16 +00001450#if defined(HAVE_STAT_TV_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001451 ansec = st->st_atim.tv_nsec;
1452 mnsec = st->st_mtim.tv_nsec;
1453 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001454#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001455 ansec = st->st_atimespec.tv_nsec;
1456 mnsec = st->st_mtimespec.tv_nsec;
1457 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001458#elif defined(HAVE_STAT_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001459 ansec = st->st_atime_nsec;
1460 mnsec = st->st_mtime_nsec;
1461 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001462#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001463 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001464#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001465 fill_time(v, 7, st->st_atime, ansec);
1466 fill_time(v, 8, st->st_mtime, mnsec);
1467 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001468
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001469#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001470 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1471 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001472#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001473#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001474 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1475 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001476#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001477#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001478 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1479 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001480#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001481#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001482 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1483 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001484#endif
1485#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001486 {
1487 PyObject *val;
1488 unsigned long bsec,bnsec;
1489 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001490#ifdef HAVE_STAT_TV_NSEC2
Victor Stinnerd6f85422010-05-05 23:33:33 +00001491 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001492#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001493 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001494#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001495 if (_stat_float_times) {
1496 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1497 } else {
1498 val = PyInt_FromLong((long)bsec);
1499 }
1500 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1501 val);
1502 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001503#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001504#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001505 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1506 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001507#endif
Fred Drake699f3522000-06-29 21:12:41 +00001508
Victor Stinnerd6f85422010-05-05 23:33:33 +00001509 if (PyErr_Occurred()) {
1510 Py_DECREF(v);
1511 return NULL;
1512 }
Fred Drake699f3522000-06-29 21:12:41 +00001513
Victor Stinnerd6f85422010-05-05 23:33:33 +00001514 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001515}
1516
Martin v. Löwisd8948722004-06-02 09:57:56 +00001517#ifdef MS_WINDOWS
1518
1519/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1520 where / can be used in place of \ and the trailing slash is optional.
1521 Both SERVER and SHARE must have at least one character.
1522*/
1523
1524#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1525#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001526#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001527#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001528#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001529
Tim Peters4ad82172004-08-30 17:02:04 +00001530static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001531IsUNCRootA(char *path, int pathlen)
1532{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001533 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001534
Victor Stinnerd6f85422010-05-05 23:33:33 +00001535 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001536
Victor Stinnerd6f85422010-05-05 23:33:33 +00001537 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1538 /* minimum UNCRoot is \\x\y */
1539 return FALSE;
1540 for (i = 2; i < pathlen ; i++)
1541 if (ISSLASH(path[i])) break;
1542 if (i == 2 || i == pathlen)
1543 /* do not allow \\\SHARE or \\SERVER */
1544 return FALSE;
1545 share = i+1;
1546 for (i = share; i < pathlen; i++)
1547 if (ISSLASH(path[i])) break;
1548 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001549
Victor Stinnerd6f85422010-05-05 23:33:33 +00001550 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001551}
1552
Tim Peters4ad82172004-08-30 17:02:04 +00001553static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001554IsUNCRootW(Py_UNICODE *path, int pathlen)
1555{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001556 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001557
Victor Stinnerd6f85422010-05-05 23:33:33 +00001558 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001559
Victor Stinnerd6f85422010-05-05 23:33:33 +00001560 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1561 /* minimum UNCRoot is \\x\y */
1562 return FALSE;
1563 for (i = 2; i < pathlen ; i++)
1564 if (ISSLASH(path[i])) break;
1565 if (i == 2 || i == pathlen)
1566 /* do not allow \\\SHARE or \\SERVER */
1567 return FALSE;
1568 share = i+1;
1569 for (i = share; i < pathlen; i++)
1570 if (ISSLASH(path[i])) break;
1571 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001572
Victor Stinnerd6f85422010-05-05 23:33:33 +00001573 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001574}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001575#endif /* MS_WINDOWS */
1576
Barry Warsaw53699e91996-12-10 23:23:01 +00001577static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001578posix_do_stat(PyObject *self, PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +00001579 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001580#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001581 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001582#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001583 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001584#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001585 char *wformat,
1586 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001587{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001588 STRUCT_STAT st;
1589 char *path = NULL; /* pass this to stat; do not free() it */
1590 char *pathfree = NULL; /* this memory must be free'd */
1591 int res;
1592 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001593
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001594#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001595 PyUnicodeObject *po;
1596 if (PyArg_ParseTuple(args, wformat, &po)) {
1597 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001598
Victor Stinnerd6f85422010-05-05 23:33:33 +00001599 Py_BEGIN_ALLOW_THREADS
1600 /* PyUnicode_AS_UNICODE result OK without
1601 thread lock as it is a simple dereference. */
1602 res = wstatfunc(wpath, &st);
1603 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001604
Victor Stinnerd6f85422010-05-05 23:33:33 +00001605 if (res != 0)
1606 return win32_error_unicode("stat", wpath);
1607 return _pystat_fromstructstat(&st);
1608 }
1609 /* Drop the argument parsing error as narrow strings
1610 are also valid. */
1611 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001612#endif
1613
Victor Stinnerd6f85422010-05-05 23:33:33 +00001614 if (!PyArg_ParseTuple(args, format,
1615 Py_FileSystemDefaultEncoding, &path))
1616 return NULL;
1617 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001618
Victor Stinnerd6f85422010-05-05 23:33:33 +00001619 Py_BEGIN_ALLOW_THREADS
1620 res = (*statfunc)(path, &st);
1621 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001622
Victor Stinnerd6f85422010-05-05 23:33:33 +00001623 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001624#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001625 result = win32_error("stat", pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001626#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001627 result = posix_error_with_filename(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001628#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001629 }
1630 else
1631 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001632
Victor Stinnerd6f85422010-05-05 23:33:33 +00001633 PyMem_Free(pathfree);
1634 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635}
1636
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001637/* POSIX methods */
1638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001639PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001640"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001641Use the real uid/gid to test for access to a path. Note that most\n\
1642operations will use the effective uid/gid, therefore this routine can\n\
1643be used in a suid/sgid environment to test if the invoking user has the\n\
1644specified access to the path. The mode argument can be F_OK to test\n\
1645existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001646
1647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001648posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001649{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001650 char *path;
1651 int mode;
1652
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001653#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001654 DWORD attr;
1655 PyUnicodeObject *po;
1656 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1657 Py_BEGIN_ALLOW_THREADS
1658 /* PyUnicode_AS_UNICODE OK without thread lock as
1659 it is a simple dereference. */
1660 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1661 Py_END_ALLOW_THREADS
1662 goto finish;
1663 }
1664 /* Drop the argument parsing error as narrow strings
1665 are also valid. */
1666 PyErr_Clear();
1667 if (!PyArg_ParseTuple(args, "eti:access",
1668 Py_FileSystemDefaultEncoding, &path, &mode))
1669 return NULL;
1670 Py_BEGIN_ALLOW_THREADS
1671 attr = GetFileAttributesA(path);
1672 Py_END_ALLOW_THREADS
1673 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001674finish:
Victor Stinnerd6f85422010-05-05 23:33:33 +00001675 if (attr == 0xFFFFFFFF)
1676 /* File does not exist, or cannot read attributes */
1677 return PyBool_FromLong(0);
1678 /* Access is possible if either write access wasn't requested, or
1679 the file isn't read-only, or if it's a directory, as there are
1680 no read-only directories on Windows. */
1681 return PyBool_FromLong(!(mode & 2)
1682 || !(attr & FILE_ATTRIBUTE_READONLY)
1683 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001684#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001685 int res;
1686 if (!PyArg_ParseTuple(args, "eti:access",
1687 Py_FileSystemDefaultEncoding, &path, &mode))
1688 return NULL;
1689 Py_BEGIN_ALLOW_THREADS
1690 res = access(path, mode);
1691 Py_END_ALLOW_THREADS
1692 PyMem_Free(path);
1693 return PyBool_FromLong(res == 0);
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001694#endif /* MS_WINDOWS */
Guido van Rossum94f6f721999-01-06 18:42:14 +00001695}
1696
Guido van Rossumd371ff11999-01-25 16:12:23 +00001697#ifndef F_OK
1698#define F_OK 0
1699#endif
1700#ifndef R_OK
1701#define R_OK 4
1702#endif
1703#ifndef W_OK
1704#define W_OK 2
1705#endif
1706#ifndef X_OK
1707#define X_OK 1
1708#endif
1709
1710#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001712"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001713Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001714
1715static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001716posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001717{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001718 int id;
1719 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001720
Victor Stinnerd6f85422010-05-05 23:33:33 +00001721 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1722 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001723
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001724#if defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001725 /* file descriptor 0 only, the default input device (stdin) */
1726 if (id == 0) {
1727 ret = ttyname();
1728 }
1729 else {
1730 ret = NULL;
1731 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001732#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001733 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001734#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001735 if (ret == NULL)
1736 return posix_error();
1737 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001738}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001739#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001740
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001741#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001742PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001743"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001744Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001745
1746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001747posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001748{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001749 char *ret;
1750 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001751
Greg Wardb48bc172000-03-01 21:51:56 +00001752#ifdef USE_CTERMID_R
Victor Stinnerd6f85422010-05-05 23:33:33 +00001753 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001754#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001755 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001756#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001757 if (ret == NULL)
1758 return posix_error();
1759 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001760}
1761#endif
1762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001763PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001764"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001766
Barry Warsaw53699e91996-12-10 23:23:01 +00001767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001768posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001769{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001770#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001771 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001772#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001773 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001774#elif defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001775 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001776#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001777 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001778#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001779}
1780
Fred Drake4d1e64b2002-04-15 19:40:07 +00001781#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001784Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001786
1787static PyObject *
1788posix_fchdir(PyObject *self, PyObject *fdobj)
1789{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001790 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001791}
1792#endif /* HAVE_FCHDIR */
1793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001796"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001798
Barry Warsaw53699e91996-12-10 23:23:01 +00001799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001800posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001801{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001802 char *path = NULL;
1803 int i;
1804 int res;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001805#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001806 DWORD attr;
1807 PyUnicodeObject *po;
1808 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1809 Py_BEGIN_ALLOW_THREADS
1810 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1811 if (attr != 0xFFFFFFFF) {
1812 if (i & _S_IWRITE)
1813 attr &= ~FILE_ATTRIBUTE_READONLY;
1814 else
1815 attr |= FILE_ATTRIBUTE_READONLY;
1816 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1817 }
1818 else
1819 res = 0;
1820 Py_END_ALLOW_THREADS
1821 if (!res)
1822 return win32_error_unicode("chmod",
1823 PyUnicode_AS_UNICODE(po));
1824 Py_INCREF(Py_None);
1825 return Py_None;
1826 }
1827 /* Drop the argument parsing error as narrow strings
1828 are also valid. */
1829 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00001830
Victor Stinnerd6f85422010-05-05 23:33:33 +00001831 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1832 &path, &i))
1833 return NULL;
1834 Py_BEGIN_ALLOW_THREADS
1835 attr = GetFileAttributesA(path);
1836 if (attr != 0xFFFFFFFF) {
1837 if (i & _S_IWRITE)
1838 attr &= ~FILE_ATTRIBUTE_READONLY;
1839 else
1840 attr |= FILE_ATTRIBUTE_READONLY;
1841 res = SetFileAttributesA(path, attr);
1842 }
1843 else
1844 res = 0;
1845 Py_END_ALLOW_THREADS
1846 if (!res) {
1847 win32_error("chmod", path);
1848 PyMem_Free(path);
1849 return NULL;
1850 }
1851 PyMem_Free(path);
1852 Py_INCREF(Py_None);
1853 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001854#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001855 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1856 &path, &i))
1857 return NULL;
1858 Py_BEGIN_ALLOW_THREADS
1859 res = chmod(path, i);
1860 Py_END_ALLOW_THREADS
1861 if (res < 0)
1862 return posix_error_with_allocated_filename(path);
1863 PyMem_Free(path);
1864 Py_INCREF(Py_None);
1865 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001866#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001867}
1868
Christian Heimes36281872007-11-30 21:11:28 +00001869#ifdef HAVE_FCHMOD
1870PyDoc_STRVAR(posix_fchmod__doc__,
1871"fchmod(fd, mode)\n\n\
1872Change the access permissions of the file given by file\n\
1873descriptor fd.");
1874
1875static PyObject *
1876posix_fchmod(PyObject *self, PyObject *args)
1877{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001878 int fd, mode, res;
1879 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1880 return NULL;
1881 Py_BEGIN_ALLOW_THREADS
1882 res = fchmod(fd, mode);
1883 Py_END_ALLOW_THREADS
1884 if (res < 0)
1885 return posix_error();
1886 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001887}
1888#endif /* HAVE_FCHMOD */
1889
1890#ifdef HAVE_LCHMOD
1891PyDoc_STRVAR(posix_lchmod__doc__,
1892"lchmod(path, mode)\n\n\
1893Change the access permissions of a file. If path is a symlink, this\n\
1894affects the link itself rather than the target.");
1895
1896static PyObject *
1897posix_lchmod(PyObject *self, PyObject *args)
1898{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001899 char *path = NULL;
1900 int i;
1901 int res;
1902 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1903 &path, &i))
1904 return NULL;
1905 Py_BEGIN_ALLOW_THREADS
1906 res = lchmod(path, i);
1907 Py_END_ALLOW_THREADS
1908 if (res < 0)
1909 return posix_error_with_allocated_filename(path);
1910 PyMem_Free(path);
1911 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001912}
1913#endif /* HAVE_LCHMOD */
1914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001915
Martin v. Löwis382abef2007-02-19 10:55:19 +00001916#ifdef HAVE_CHFLAGS
1917PyDoc_STRVAR(posix_chflags__doc__,
1918"chflags(path, flags)\n\n\
1919Set file flags.");
1920
1921static PyObject *
1922posix_chflags(PyObject *self, PyObject *args)
1923{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001924 char *path;
1925 unsigned long flags;
1926 int res;
1927 if (!PyArg_ParseTuple(args, "etk:chflags",
1928 Py_FileSystemDefaultEncoding, &path, &flags))
1929 return NULL;
1930 Py_BEGIN_ALLOW_THREADS
1931 res = chflags(path, flags);
1932 Py_END_ALLOW_THREADS
1933 if (res < 0)
1934 return posix_error_with_allocated_filename(path);
1935 PyMem_Free(path);
1936 Py_INCREF(Py_None);
1937 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001938}
1939#endif /* HAVE_CHFLAGS */
1940
1941#ifdef HAVE_LCHFLAGS
1942PyDoc_STRVAR(posix_lchflags__doc__,
1943"lchflags(path, flags)\n\n\
1944Set file flags.\n\
1945This function will not follow symbolic links.");
1946
1947static PyObject *
1948posix_lchflags(PyObject *self, PyObject *args)
1949{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001950 char *path;
1951 unsigned long flags;
1952 int res;
1953 if (!PyArg_ParseTuple(args, "etk:lchflags",
1954 Py_FileSystemDefaultEncoding, &path, &flags))
1955 return NULL;
1956 Py_BEGIN_ALLOW_THREADS
1957 res = lchflags(path, flags);
1958 Py_END_ALLOW_THREADS
1959 if (res < 0)
1960 return posix_error_with_allocated_filename(path);
1961 PyMem_Free(path);
1962 Py_INCREF(Py_None);
1963 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001964}
1965#endif /* HAVE_LCHFLAGS */
1966
Martin v. Löwis244edc82001-10-04 22:44:26 +00001967#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001968PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001969"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001970Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001971
1972static PyObject *
1973posix_chroot(PyObject *self, PyObject *args)
1974{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001975 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001976}
1977#endif
1978
Guido van Rossum21142a01999-01-08 21:05:37 +00001979#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001980PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001981"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001983
1984static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001985posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001986{
Stefan Krah93f7a322010-11-26 17:35:50 +00001987 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001988}
1989#endif /* HAVE_FSYNC */
1990
1991#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001992
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001993#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001994extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1995#endif
1996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001997PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001998"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001999force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002000 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002001
2002static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002003posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002004{
Stefan Krah93f7a322010-11-26 17:35:50 +00002005 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002006}
2007#endif /* HAVE_FDATASYNC */
2008
2009
Fredrik Lundh10723342000-07-10 16:38:09 +00002010#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002011PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002012"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002014
Barry Warsaw53699e91996-12-10 23:23:01 +00002015static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002016posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002017{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002018 char *path = NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002019 uid_t uid;
2020 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002021 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002022 if (!PyArg_ParseTuple(args, "etO&O&:chown",
Victor Stinnerd6f85422010-05-05 23:33:33 +00002023 Py_FileSystemDefaultEncoding, &path,
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002024 _Py_Uid_Converter, &uid,
2025 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002026 return NULL;
2027 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002028 res = chown(path, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002029 Py_END_ALLOW_THREADS
2030 if (res < 0)
2031 return posix_error_with_allocated_filename(path);
2032 PyMem_Free(path);
2033 Py_INCREF(Py_None);
2034 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002035}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002036#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002037
Christian Heimes36281872007-11-30 21:11:28 +00002038#ifdef HAVE_FCHOWN
2039PyDoc_STRVAR(posix_fchown__doc__,
2040"fchown(fd, uid, gid)\n\n\
2041Change the owner and group id of the file given by file descriptor\n\
2042fd to the numeric uid and gid.");
2043
2044static PyObject *
2045posix_fchown(PyObject *self, PyObject *args)
2046{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002047 int fd;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002048 uid_t uid;
2049 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002050 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002051 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd,
2052 _Py_Uid_Converter, &uid,
2053 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002054 return NULL;
2055 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002056 res = fchown(fd, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002057 Py_END_ALLOW_THREADS
2058 if (res < 0)
2059 return posix_error();
2060 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00002061}
2062#endif /* HAVE_FCHOWN */
2063
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002064#ifdef HAVE_LCHOWN
2065PyDoc_STRVAR(posix_lchown__doc__,
2066"lchown(path, uid, gid)\n\n\
2067Change the owner and group id of path to the numeric uid and gid.\n\
2068This function will not follow symbolic links.");
2069
2070static PyObject *
2071posix_lchown(PyObject *self, PyObject *args)
2072{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002073 char *path = NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002074 uid_t uid;
2075 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002076 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002077 if (!PyArg_ParseTuple(args, "etO&O&:lchown",
Victor Stinnerd6f85422010-05-05 23:33:33 +00002078 Py_FileSystemDefaultEncoding, &path,
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002079 _Py_Uid_Converter, &uid,
2080 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002081 return NULL;
2082 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002083 res = lchown(path, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002084 Py_END_ALLOW_THREADS
2085 if (res < 0)
2086 return posix_error_with_allocated_filename(path);
2087 PyMem_Free(path);
2088 Py_INCREF(Py_None);
2089 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002090}
2091#endif /* HAVE_LCHOWN */
2092
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002093
Guido van Rossum36bc6801995-06-14 22:54:23 +00002094#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002096"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098
Trent Nelsonda4277a2012-08-29 09:20:41 -04002099#if (defined(__sun) && defined(__SVR4)) || \
2100 defined(__OpenBSD__) || \
2101 defined(__NetBSD__)
Stefan Krah182ae642010-07-13 19:17:08 +00002102/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
2103static PyObject *
2104posix_getcwd(PyObject *self, PyObject *noargs)
2105{
2106 char buf[PATH_MAX+2];
2107 char *res;
2108
2109 Py_BEGIN_ALLOW_THREADS
Stefan Krah8cb9f032010-07-13 19:40:00 +00002110 res = getcwd(buf, sizeof buf);
Stefan Krah182ae642010-07-13 19:17:08 +00002111 Py_END_ALLOW_THREADS
2112
2113 if (res == NULL)
2114 return posix_error();
2115
2116 return PyString_FromString(buf);
2117}
2118#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002119static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002120posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002121{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002122 int bufsize_incr = 1024;
2123 int bufsize = 0;
2124 char *tmpbuf = NULL;
2125 char *res = NULL;
2126 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00002127
Victor Stinnerd6f85422010-05-05 23:33:33 +00002128 Py_BEGIN_ALLOW_THREADS
2129 do {
2130 bufsize = bufsize + bufsize_incr;
2131 tmpbuf = malloc(bufsize);
2132 if (tmpbuf == NULL) {
2133 break;
2134 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002135#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002136 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002137#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002138 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002139#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00002140
Victor Stinnerd6f85422010-05-05 23:33:33 +00002141 if (res == NULL) {
2142 free(tmpbuf);
2143 }
2144 } while ((res == NULL) && (errno == ERANGE));
2145 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002146
Victor Stinnerd6f85422010-05-05 23:33:33 +00002147 if (res == NULL)
2148 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002149
Victor Stinnerd6f85422010-05-05 23:33:33 +00002150 dynamic_return = PyString_FromString(tmpbuf);
2151 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002152
Victor Stinnerd6f85422010-05-05 23:33:33 +00002153 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002154}
Stefan Krah182ae642010-07-13 19:17:08 +00002155#endif /* getcwd() NULL/ERANGE workaround. */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156
Walter Dörwald3b918c32002-11-21 20:18:46 +00002157#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158PyDoc_STRVAR(posix_getcwdu__doc__,
2159"getcwdu() -> path\n\n\
2160Return a unicode string representing the current working directory.");
2161
2162static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002163posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002164{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002165 char buf[1026];
2166 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002167
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002168#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002169 DWORD len;
2170 wchar_t wbuf[1026];
2171 wchar_t *wbuf2 = wbuf;
2172 PyObject *resobj;
2173 Py_BEGIN_ALLOW_THREADS
2174 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2175 /* If the buffer is large enough, len does not include the
2176 terminating \0. If the buffer is too small, len includes
2177 the space needed for the terminator. */
2178 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2179 wbuf2 = malloc(len * sizeof(wchar_t));
2180 if (wbuf2)
2181 len = GetCurrentDirectoryW(len, wbuf2);
2182 }
2183 Py_END_ALLOW_THREADS
2184 if (!wbuf2) {
2185 PyErr_NoMemory();
2186 return NULL;
2187 }
2188 if (!len) {
2189 if (wbuf2 != wbuf) free(wbuf2);
2190 return win32_error("getcwdu", NULL);
2191 }
2192 resobj = PyUnicode_FromWideChar(wbuf2, len);
2193 if (wbuf2 != wbuf) free(wbuf2);
2194 return resobj;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002195#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002196
Victor Stinnerd6f85422010-05-05 23:33:33 +00002197 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002198#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002199 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002200#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002201 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002202#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002203 Py_END_ALLOW_THREADS
2204 if (res == NULL)
2205 return posix_error();
2206 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002207}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002208#endif /* Py_USING_UNICODE */
2209#endif /* HAVE_GETCWD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002211
Guido van Rossumb6775db1994-08-01 11:34:53 +00002212#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002214"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002215Create a hard link to a file.");
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_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002220 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002222#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002225PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002226"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002227Return a list containing the names of the entries in the directory.\n\
2228\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00002229 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002230\n\
2231The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002233
Barry Warsaw53699e91996-12-10 23:23:01 +00002234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002235posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002236{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002237 /* XXX Should redo this putting the (now four) versions of opendir
2238 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002239#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002240
Victor Stinnerd6f85422010-05-05 23:33:33 +00002241 PyObject *d, *v;
2242 HANDLE hFindFile;
2243 BOOL result;
2244 WIN32_FIND_DATA FileData;
2245 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2246 char *bufptr = namebuf;
2247 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002248
Victor Stinnerd6f85422010-05-05 23:33:33 +00002249 PyObject *po;
2250 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2251 WIN32_FIND_DATAW wFileData;
2252 Py_UNICODE *wnamebuf;
2253 /* Overallocate for \\*.*\0 */
2254 len = PyUnicode_GET_SIZE(po);
2255 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2256 if (!wnamebuf) {
2257 PyErr_NoMemory();
2258 return NULL;
2259 }
2260 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2261 if (len > 0) {
2262 Py_UNICODE wch = wnamebuf[len-1];
2263 if (wch != L'/' && wch != L'\\' && wch != L':')
2264 wnamebuf[len++] = L'\\';
2265 wcscpy(wnamebuf + len, L"*.*");
2266 }
2267 if ((d = PyList_New(0)) == NULL) {
2268 free(wnamebuf);
2269 return NULL;
2270 }
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002271 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002272 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002273 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002274 if (hFindFile == INVALID_HANDLE_VALUE) {
2275 int error = GetLastError();
2276 if (error == ERROR_FILE_NOT_FOUND) {
2277 free(wnamebuf);
2278 return d;
2279 }
2280 Py_DECREF(d);
2281 win32_error_unicode("FindFirstFileW", wnamebuf);
2282 free(wnamebuf);
2283 return NULL;
2284 }
2285 do {
2286 /* Skip over . and .. */
2287 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2288 wcscmp(wFileData.cFileName, L"..") != 0) {
2289 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2290 if (v == NULL) {
2291 Py_DECREF(d);
2292 d = NULL;
2293 break;
2294 }
2295 if (PyList_Append(d, v) != 0) {
2296 Py_DECREF(v);
2297 Py_DECREF(d);
2298 d = NULL;
2299 break;
2300 }
2301 Py_DECREF(v);
2302 }
2303 Py_BEGIN_ALLOW_THREADS
2304 result = FindNextFileW(hFindFile, &wFileData);
2305 Py_END_ALLOW_THREADS
2306 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2307 it got to the end of the directory. */
2308 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2309 Py_DECREF(d);
2310 win32_error_unicode("FindNextFileW", wnamebuf);
2311 FindClose(hFindFile);
2312 free(wnamebuf);
2313 return NULL;
2314 }
2315 } while (result == TRUE);
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002316
Victor Stinnerd6f85422010-05-05 23:33:33 +00002317 if (FindClose(hFindFile) == FALSE) {
2318 Py_DECREF(d);
2319 win32_error_unicode("FindClose", wnamebuf);
2320 free(wnamebuf);
2321 return NULL;
2322 }
2323 free(wnamebuf);
2324 return d;
2325 }
2326 /* Drop the argument parsing error as narrow strings
2327 are also valid. */
2328 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002329
Victor Stinnerd6f85422010-05-05 23:33:33 +00002330 if (!PyArg_ParseTuple(args, "et#:listdir",
2331 Py_FileSystemDefaultEncoding, &bufptr, &len))
2332 return NULL;
2333 if (len > 0) {
2334 char ch = namebuf[len-1];
2335 if (ch != SEP && ch != ALTSEP && ch != ':')
2336 namebuf[len++] = '/';
2337 strcpy(namebuf + len, "*.*");
2338 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002339
Victor Stinnerd6f85422010-05-05 23:33:33 +00002340 if ((d = PyList_New(0)) == NULL)
2341 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002342
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002343 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002344 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002345 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002346 if (hFindFile == INVALID_HANDLE_VALUE) {
2347 int error = GetLastError();
2348 if (error == ERROR_FILE_NOT_FOUND)
2349 return d;
2350 Py_DECREF(d);
2351 return win32_error("FindFirstFile", namebuf);
2352 }
2353 do {
2354 /* Skip over . and .. */
2355 if (strcmp(FileData.cFileName, ".") != 0 &&
2356 strcmp(FileData.cFileName, "..") != 0) {
2357 v = PyString_FromString(FileData.cFileName);
2358 if (v == NULL) {
2359 Py_DECREF(d);
2360 d = NULL;
2361 break;
2362 }
2363 if (PyList_Append(d, v) != 0) {
2364 Py_DECREF(v);
2365 Py_DECREF(d);
2366 d = NULL;
2367 break;
2368 }
2369 Py_DECREF(v);
2370 }
2371 Py_BEGIN_ALLOW_THREADS
2372 result = FindNextFile(hFindFile, &FileData);
2373 Py_END_ALLOW_THREADS
2374 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2375 it got to the end of the directory. */
2376 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2377 Py_DECREF(d);
2378 win32_error("FindNextFile", namebuf);
2379 FindClose(hFindFile);
2380 return NULL;
2381 }
2382 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002383
Victor Stinnerd6f85422010-05-05 23:33:33 +00002384 if (FindClose(hFindFile) == FALSE) {
2385 Py_DECREF(d);
2386 return win32_error("FindClose", namebuf);
2387 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002388
Victor Stinnerd6f85422010-05-05 23:33:33 +00002389 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002390
Tim Peters0bb44a42000-09-15 07:44:49 +00002391#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002392
2393#ifndef MAX_PATH
2394#define MAX_PATH CCHMAXPATH
2395#endif
2396 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002397 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002398 PyObject *d, *v;
2399 char namebuf[MAX_PATH+5];
2400 HDIR hdir = 1;
2401 ULONG srchcnt = 1;
2402 FILEFINDBUF3 ep;
2403 APIRET rc;
2404
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002405 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002406 return NULL;
2407 if (len >= MAX_PATH) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002408 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002409 return NULL;
2410 }
2411 strcpy(namebuf, name);
2412 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002413 if (*pt == ALTSEP)
2414 *pt = SEP;
2415 if (namebuf[len-1] != SEP)
2416 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002417 strcpy(namebuf + len, "*.*");
2418
Victor Stinnerd6f85422010-05-05 23:33:33 +00002419 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002420 return NULL;
2421
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002422 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2423 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002424 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002425 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2426 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2427 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002428
2429 if (rc != NO_ERROR) {
2430 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002431 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002432 }
2433
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002434 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002435 do {
2436 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002437 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002438 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002439
2440 strcpy(namebuf, ep.achName);
2441
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002442 /* Leave Case of Name Alone -- In Native Form */
2443 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002444
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002445 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002446 if (v == NULL) {
2447 Py_DECREF(d);
2448 d = NULL;
2449 break;
2450 }
2451 if (PyList_Append(d, v) != 0) {
2452 Py_DECREF(v);
2453 Py_DECREF(d);
2454 d = NULL;
2455 break;
2456 }
2457 Py_DECREF(v);
2458 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2459 }
2460
2461 return d;
2462#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002463
Victor Stinnerd6f85422010-05-05 23:33:33 +00002464 char *name = NULL;
2465 PyObject *d, *v;
2466 DIR *dirp;
2467 struct dirent *ep;
2468 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002469
Victor Stinnerd6f85422010-05-05 23:33:33 +00002470 errno = 0;
2471 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2472 arg_is_unicode = 0;
2473 PyErr_Clear();
2474 }
2475 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2476 return NULL;
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002477 Py_BEGIN_ALLOW_THREADS
2478 dirp = opendir(name);
2479 Py_END_ALLOW_THREADS
2480 if (dirp == NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002481 return posix_error_with_allocated_filename(name);
2482 }
2483 if ((d = PyList_New(0)) == NULL) {
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002484 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002485 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002486 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002487 PyMem_Free(name);
2488 return NULL;
2489 }
2490 for (;;) {
2491 errno = 0;
2492 Py_BEGIN_ALLOW_THREADS
2493 ep = readdir(dirp);
2494 Py_END_ALLOW_THREADS
2495 if (ep == NULL) {
2496 if (errno == 0) {
2497 break;
2498 } else {
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002499 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002500 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002501 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002502 Py_DECREF(d);
2503 return posix_error_with_allocated_filename(name);
2504 }
2505 }
2506 if (ep->d_name[0] == '.' &&
2507 (NAMLEN(ep) == 1 ||
2508 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2509 continue;
2510 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2511 if (v == NULL) {
2512 Py_DECREF(d);
2513 d = NULL;
2514 break;
2515 }
Just van Rossum46c97842003-02-25 21:42:15 +00002516#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00002517 if (arg_is_unicode) {
2518 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002519
Victor Stinnerd6f85422010-05-05 23:33:33 +00002520 w = PyUnicode_FromEncodedObject(v,
2521 Py_FileSystemDefaultEncoding,
2522 "strict");
2523 if (w != NULL) {
2524 Py_DECREF(v);
2525 v = w;
2526 }
2527 else {
2528 /* fall back to the original byte string, as
2529 discussed in patch #683592 */
2530 PyErr_Clear();
2531 }
2532 }
Just van Rossum46c97842003-02-25 21:42:15 +00002533#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002534 if (PyList_Append(d, v) != 0) {
2535 Py_DECREF(v);
2536 Py_DECREF(d);
2537 d = NULL;
2538 break;
2539 }
2540 Py_DECREF(v);
2541 }
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002542 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002543 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002544 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002545 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002546
Victor Stinnerd6f85422010-05-05 23:33:33 +00002547 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002548
Tim Peters0bb44a42000-09-15 07:44:49 +00002549#endif /* which OS */
2550} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002551
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002552#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002553/* A helper function for abspath on win32 */
2554static PyObject *
2555posix__getfullpathname(PyObject *self, PyObject *args)
2556{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002557 /* assume encoded strings won't more than double no of chars */
2558 char inbuf[MAX_PATH*2];
2559 char *inbufp = inbuf;
2560 Py_ssize_t insize = sizeof(inbuf);
2561 char outbuf[MAX_PATH*2];
2562 char *temp;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002563
Victor Stinnerd6f85422010-05-05 23:33:33 +00002564 PyUnicodeObject *po;
2565 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2566 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2567 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2568 Py_UNICODE *wtemp;
2569 DWORD result;
2570 PyObject *v;
2571 result = GetFullPathNameW(wpath,
2572 sizeof(woutbuf)/sizeof(woutbuf[0]),
2573 woutbuf, &wtemp);
2574 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2575 woutbufp = malloc(result * sizeof(Py_UNICODE));
2576 if (!woutbufp)
2577 return PyErr_NoMemory();
2578 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2579 }
2580 if (result)
2581 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2582 else
2583 v = win32_error_unicode("GetFullPathNameW", wpath);
2584 if (woutbufp != woutbuf)
2585 free(woutbufp);
2586 return v;
2587 }
2588 /* Drop the argument parsing error as narrow strings
2589 are also valid. */
2590 PyErr_Clear();
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002591
Victor Stinnerd6f85422010-05-05 23:33:33 +00002592 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2593 Py_FileSystemDefaultEncoding, &inbufp,
2594 &insize))
2595 return NULL;
2596 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2597 outbuf, &temp))
2598 return win32_error("GetFullPathName", inbuf);
2599 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2600 return PyUnicode_Decode(outbuf, strlen(outbuf),
2601 Py_FileSystemDefaultEncoding, NULL);
2602 }
2603 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002604} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002605#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002607PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002608"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610
Barry Warsaw53699e91996-12-10 23:23:01 +00002611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002612posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002613{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002614 int res;
2615 char *path = NULL;
2616 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002617
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002618#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002619 PyUnicodeObject *po;
2620 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2621 Py_BEGIN_ALLOW_THREADS
2622 /* PyUnicode_AS_UNICODE OK without thread lock as
2623 it is a simple dereference. */
2624 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2625 Py_END_ALLOW_THREADS
2626 if (!res)
2627 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2628 Py_INCREF(Py_None);
2629 return Py_None;
2630 }
2631 /* Drop the argument parsing error as narrow strings
2632 are also valid. */
2633 PyErr_Clear();
2634 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2635 Py_FileSystemDefaultEncoding, &path, &mode))
2636 return NULL;
2637 Py_BEGIN_ALLOW_THREADS
2638 /* PyUnicode_AS_UNICODE OK without thread lock as
2639 it is a simple dereference. */
2640 res = CreateDirectoryA(path, NULL);
2641 Py_END_ALLOW_THREADS
2642 if (!res) {
2643 win32_error("mkdir", path);
2644 PyMem_Free(path);
2645 return NULL;
2646 }
2647 PyMem_Free(path);
2648 Py_INCREF(Py_None);
2649 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002650#else /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002651
Victor Stinnerd6f85422010-05-05 23:33:33 +00002652 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2653 Py_FileSystemDefaultEncoding, &path, &mode))
2654 return NULL;
2655 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002656#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002657 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002658#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002659 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002660#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002661 Py_END_ALLOW_THREADS
2662 if (res < 0)
2663 return posix_error_with_allocated_filename(path);
2664 PyMem_Free(path);
2665 Py_INCREF(Py_None);
2666 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002667#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002668}
2669
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002670
Neal Norwitz1818ed72006-03-26 00:29:48 +00002671/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2672#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002673#include <sys/resource.h>
2674#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002675
Neal Norwitz1818ed72006-03-26 00:29:48 +00002676
2677#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002679"nice(inc) -> new_priority\n\n\
2680Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002681
Barry Warsaw53699e91996-12-10 23:23:01 +00002682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002683posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002684{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002685 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002686
Victor Stinnerd6f85422010-05-05 23:33:33 +00002687 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2688 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002689
Victor Stinnerd6f85422010-05-05 23:33:33 +00002690 /* There are two flavours of 'nice': one that returns the new
2691 priority (as required by almost all standards out there) and the
2692 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2693 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002694
Victor Stinnerd6f85422010-05-05 23:33:33 +00002695 If we are of the nice family that returns the new priority, we
2696 need to clear errno before the call, and check if errno is filled
2697 before calling posix_error() on a returnvalue of -1, because the
2698 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002699
Victor Stinnerd6f85422010-05-05 23:33:33 +00002700 errno = 0;
2701 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002702#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002703 if (value == 0)
2704 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002705#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002706 if (value == -1 && errno != 0)
2707 /* either nice() or getpriority() returned an error */
2708 return posix_error();
2709 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002710}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002711#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002714"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002715Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Barry Warsaw53699e91996-12-10 23:23:01 +00002717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002718posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002719{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002720#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002721 PyObject *o1, *o2;
2722 char *p1, *p2;
2723 BOOL result;
2724 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2725 goto error;
2726 if (!convert_to_unicode(&o1))
2727 goto error;
2728 if (!convert_to_unicode(&o2)) {
2729 Py_DECREF(o1);
2730 goto error;
2731 }
2732 Py_BEGIN_ALLOW_THREADS
2733 result = MoveFileW(PyUnicode_AsUnicode(o1),
2734 PyUnicode_AsUnicode(o2));
2735 Py_END_ALLOW_THREADS
2736 Py_DECREF(o1);
2737 Py_DECREF(o2);
2738 if (!result)
2739 return win32_error("rename", NULL);
2740 Py_INCREF(Py_None);
2741 return Py_None;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002742error:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002743 PyErr_Clear();
2744 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2745 return NULL;
2746 Py_BEGIN_ALLOW_THREADS
2747 result = MoveFileA(p1, p2);
2748 Py_END_ALLOW_THREADS
2749 if (!result)
2750 return win32_error("rename", NULL);
2751 Py_INCREF(Py_None);
2752 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002753#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002754 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002755#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002756}
2757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002759PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002760"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002761Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Barry Warsaw53699e91996-12-10 23:23:01 +00002763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002764posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002765{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002766#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002767 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002768#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002769 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002770#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002771}
2772
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002775"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002780{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002781#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002782 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002783#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002784 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002785#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002786}
2787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002789#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002791"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002793
Barry Warsaw53699e91996-12-10 23:23:01 +00002794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002795posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002796{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002797 char *command;
2798 long sts;
2799 if (!PyArg_ParseTuple(args, "s:system", &command))
2800 return NULL;
2801 Py_BEGIN_ALLOW_THREADS
2802 sts = system(command);
2803 Py_END_ALLOW_THREADS
2804 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002805}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002806#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002807
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002809PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002810"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002811Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002812
Barry Warsaw53699e91996-12-10 23:23:01 +00002813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002814posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002815{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002816 int i;
2817 if (!PyArg_ParseTuple(args, "i:umask", &i))
2818 return NULL;
2819 i = (int)umask(i);
2820 if (i < 0)
2821 return posix_error();
2822 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002823}
2824
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002827"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002828Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002831"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002832Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002833
Barry Warsaw53699e91996-12-10 23:23:01 +00002834static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002835posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002836{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002837#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002838 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002839#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002840 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002841#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002842}
2843
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002844
Guido van Rossumb6775db1994-08-01 11:34:53 +00002845#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002846PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002847"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002848Return a tuple identifying the current operating system.");
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_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002852{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002853 struct utsname u;
2854 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002855
Victor Stinnerd6f85422010-05-05 23:33:33 +00002856 Py_BEGIN_ALLOW_THREADS
2857 res = uname(&u);
2858 Py_END_ALLOW_THREADS
2859 if (res < 0)
2860 return posix_error();
2861 return Py_BuildValue("(sssss)",
2862 u.sysname,
2863 u.nodename,
2864 u.release,
2865 u.version,
2866 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002867}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002868#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002869
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002870static int
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002871extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002872{
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002873 time_t intval;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002874 if (PyFloat_Check(t)) {
2875 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002876 PyObject *intobj = PyNumber_Long(t);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002877 if (!intobj)
2878 return -1;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002879#if SIZEOF_TIME_T > SIZEOF_LONG
2880 intval = PyInt_AsUnsignedLongLongMask(intobj);
2881#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002882 intval = PyInt_AsLong(intobj);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002883#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002884 Py_DECREF(intobj);
2885 if (intval == -1 && PyErr_Occurred())
2886 return -1;
2887 *sec = intval;
2888 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2889 if (*usec < 0)
2890 /* If rounding gave us a negative number,
2891 truncate. */
2892 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002893 return 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002894 }
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002895#if SIZEOF_TIME_T > SIZEOF_LONG
2896 intval = PyInt_AsUnsignedLongLongMask(t);
2897#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002898 intval = PyInt_AsLong(t);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002899#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002900 if (intval == -1 && PyErr_Occurred())
2901 return -1;
2902 *sec = intval;
2903 *usec = 0;
2904 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002907PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002908"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002909utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002910Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002912
Barry Warsaw53699e91996-12-10 23:23:01 +00002913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002914posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002915{
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002916#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002917 PyObject *arg;
2918 PyUnicodeObject *obwpath;
2919 wchar_t *wpath = NULL;
2920 char *apath = NULL;
2921 HANDLE hFile;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002922 time_t atimesec, mtimesec;
2923 long ausec, musec;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002924 FILETIME atime, mtime;
2925 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002926
Victor Stinnerd6f85422010-05-05 23:33:33 +00002927 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2928 wpath = PyUnicode_AS_UNICODE(obwpath);
2929 Py_BEGIN_ALLOW_THREADS
2930 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2931 NULL, OPEN_EXISTING,
2932 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2933 Py_END_ALLOW_THREADS
2934 if (hFile == INVALID_HANDLE_VALUE)
2935 return win32_error_unicode("utime", wpath);
2936 } else
2937 /* Drop the argument parsing error as narrow strings
2938 are also valid. */
2939 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002940
Victor Stinnerd6f85422010-05-05 23:33:33 +00002941 if (!wpath) {
2942 if (!PyArg_ParseTuple(args, "etO:utime",
2943 Py_FileSystemDefaultEncoding, &apath, &arg))
2944 return NULL;
2945 Py_BEGIN_ALLOW_THREADS
2946 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2947 NULL, OPEN_EXISTING,
2948 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2949 Py_END_ALLOW_THREADS
2950 if (hFile == INVALID_HANDLE_VALUE) {
2951 win32_error("utime", apath);
2952 PyMem_Free(apath);
2953 return NULL;
2954 }
2955 PyMem_Free(apath);
2956 }
2957
2958 if (arg == Py_None) {
2959 SYSTEMTIME now;
2960 GetSystemTime(&now);
2961 if (!SystemTimeToFileTime(&now, &mtime) ||
2962 !SystemTimeToFileTime(&now, &atime)) {
2963 win32_error("utime", NULL);
2964 goto done;
Stefan Krah93f7a322010-11-26 17:35:50 +00002965 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00002966 }
2967 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2968 PyErr_SetString(PyExc_TypeError,
2969 "utime() arg 2 must be a tuple (atime, mtime)");
2970 goto done;
2971 }
2972 else {
2973 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2974 &atimesec, &ausec) == -1)
2975 goto done;
2976 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2977 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2978 &mtimesec, &musec) == -1)
2979 goto done;
2980 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2981 }
2982 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2983 /* Avoid putting the file name into the error here,
2984 as that may confuse the user into believing that
2985 something is wrong with the file, when it also
2986 could be the time stamp that gives a problem. */
2987 win32_error("utime", NULL);
Antoine Pitrou1f1613f2011-01-06 18:30:26 +00002988 goto done;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002989 }
2990 Py_INCREF(Py_None);
2991 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002992done:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002993 CloseHandle(hFile);
2994 return result;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002995#else /* MS_WINDOWS */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002996
Victor Stinnerd6f85422010-05-05 23:33:33 +00002997 char *path = NULL;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002998 time_t atime, mtime;
2999 long ausec, musec;
Victor Stinnerd6f85422010-05-05 23:33:33 +00003000 int res;
3001 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003002
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003003#if defined(HAVE_UTIMES)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003004 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003005#define ATIME buf[0].tv_sec
3006#define MTIME buf[1].tv_sec
3007#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003008/* XXX should define struct utimbuf instead, above */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003009 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003010#define ATIME buf.actime
3011#define MTIME buf.modtime
3012#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003013#else /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003014 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003015#define ATIME buf[0]
3016#define MTIME buf[1]
3017#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003018#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003019
Mark Hammond817c9292003-12-03 01:22:38 +00003020
Victor Stinnerd6f85422010-05-05 23:33:33 +00003021 if (!PyArg_ParseTuple(args, "etO:utime",
3022 Py_FileSystemDefaultEncoding, &path, &arg))
3023 return NULL;
3024 if (arg == Py_None) {
3025 /* optional time values not given */
3026 Py_BEGIN_ALLOW_THREADS
3027 res = utime(path, NULL);
3028 Py_END_ALLOW_THREADS
3029 }
3030 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3031 PyErr_SetString(PyExc_TypeError,
3032 "utime() arg 2 must be a tuple (atime, mtime)");
3033 PyMem_Free(path);
3034 return NULL;
3035 }
3036 else {
3037 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3038 &atime, &ausec) == -1) {
3039 PyMem_Free(path);
3040 return NULL;
3041 }
3042 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3043 &mtime, &musec) == -1) {
3044 PyMem_Free(path);
3045 return NULL;
3046 }
3047 ATIME = atime;
3048 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003049#ifdef HAVE_UTIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00003050 buf[0].tv_usec = ausec;
3051 buf[1].tv_usec = musec;
3052 Py_BEGIN_ALLOW_THREADS
3053 res = utimes(path, buf);
3054 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003055#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003056 Py_BEGIN_ALLOW_THREADS
3057 res = utime(path, UTIME_ARG);
3058 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003059#endif /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003060 }
3061 if (res < 0) {
3062 return posix_error_with_allocated_filename(path);
3063 }
3064 PyMem_Free(path);
3065 Py_INCREF(Py_None);
3066 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003067#undef UTIME_ARG
3068#undef ATIME
3069#undef MTIME
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00003070#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003071}
3072
Guido van Rossum85e3b011991-06-03 12:42:10 +00003073
Guido van Rossum3b066191991-06-04 19:40:25 +00003074/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003076PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003077"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003078Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003079
Barry Warsaw53699e91996-12-10 23:23:01 +00003080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003081posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003082{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003083 int sts;
3084 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3085 return NULL;
3086 _exit(sts);
3087 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003088}
3089
Martin v. Löwis114619e2002-10-07 06:44:21 +00003090#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3091static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003092free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003093{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003094 Py_ssize_t i;
3095 for (i = 0; i < count; i++)
3096 PyMem_Free(array[i]);
3097 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003098}
3099#endif
3100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003101
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003102#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003103PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003104"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003105Execute an executable path with arguments, replacing current process.\n\
3106\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003107 path: path of executable file\n\
3108 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003109
Barry Warsaw53699e91996-12-10 23:23:01 +00003110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003111posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003112{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003113 char *path;
3114 PyObject *argv;
3115 char **argvlist;
3116 Py_ssize_t i, argc;
3117 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003118
Victor Stinnerd6f85422010-05-05 23:33:33 +00003119 /* execv has two arguments: (path, argv), where
3120 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003121
Victor Stinnerd6f85422010-05-05 23:33:33 +00003122 if (!PyArg_ParseTuple(args, "etO:execv",
3123 Py_FileSystemDefaultEncoding,
3124 &path, &argv))
3125 return NULL;
3126 if (PyList_Check(argv)) {
3127 argc = PyList_Size(argv);
3128 getitem = PyList_GetItem;
3129 }
3130 else if (PyTuple_Check(argv)) {
3131 argc = PyTuple_Size(argv);
3132 getitem = PyTuple_GetItem;
3133 }
3134 else {
3135 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3136 PyMem_Free(path);
3137 return NULL;
3138 }
3139 if (argc < 1) {
3140 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3141 PyMem_Free(path);
3142 return NULL;
3143 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003144
Victor Stinnerd6f85422010-05-05 23:33:33 +00003145 argvlist = PyMem_NEW(char *, argc+1);
3146 if (argvlist == NULL) {
3147 PyMem_Free(path);
3148 return PyErr_NoMemory();
3149 }
3150 for (i = 0; i < argc; i++) {
3151 if (!PyArg_Parse((*getitem)(argv, i), "et",
3152 Py_FileSystemDefaultEncoding,
3153 &argvlist[i])) {
3154 free_string_array(argvlist, i);
3155 PyErr_SetString(PyExc_TypeError,
3156 "execv() arg 2 must contain only strings");
3157 PyMem_Free(path);
3158 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003159
Victor Stinnerd6f85422010-05-05 23:33:33 +00003160 }
3161 }
3162 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003163
Victor Stinnerd6f85422010-05-05 23:33:33 +00003164 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003165
Victor Stinnerd6f85422010-05-05 23:33:33 +00003166 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003167
Victor Stinnerd6f85422010-05-05 23:33:33 +00003168 free_string_array(argvlist, argc);
3169 PyMem_Free(path);
3170 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003171}
3172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003174PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003175"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003176Execute a path with arguments and environment, replacing current process.\n\
3177\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003178 path: path of executable file\n\
3179 args: tuple or list of arguments\n\
3180 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181
Barry Warsaw53699e91996-12-10 23:23:01 +00003182static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003183posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003184{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003185 char *path;
3186 PyObject *argv, *env;
3187 char **argvlist;
3188 char **envlist;
3189 PyObject *key, *val, *keys=NULL, *vals=NULL;
3190 Py_ssize_t i, pos, argc, envc;
3191 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3192 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003193
Victor Stinnerd6f85422010-05-05 23:33:33 +00003194 /* execve has three arguments: (path, argv, env), where
3195 argv is a list or tuple of strings and env is a dictionary
3196 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003197
Victor Stinnerd6f85422010-05-05 23:33:33 +00003198 if (!PyArg_ParseTuple(args, "etOO:execve",
3199 Py_FileSystemDefaultEncoding,
3200 &path, &argv, &env))
3201 return NULL;
3202 if (PyList_Check(argv)) {
3203 argc = PyList_Size(argv);
3204 getitem = PyList_GetItem;
3205 }
3206 else if (PyTuple_Check(argv)) {
3207 argc = PyTuple_Size(argv);
3208 getitem = PyTuple_GetItem;
3209 }
3210 else {
3211 PyErr_SetString(PyExc_TypeError,
3212 "execve() arg 2 must be a tuple or list");
3213 goto fail_0;
3214 }
3215 if (!PyMapping_Check(env)) {
3216 PyErr_SetString(PyExc_TypeError,
3217 "execve() arg 3 must be a mapping object");
3218 goto fail_0;
3219 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003220
Victor Stinnerd6f85422010-05-05 23:33:33 +00003221 argvlist = PyMem_NEW(char *, argc+1);
3222 if (argvlist == NULL) {
3223 PyErr_NoMemory();
3224 goto fail_0;
3225 }
3226 for (i = 0; i < argc; i++) {
3227 if (!PyArg_Parse((*getitem)(argv, i),
3228 "et;execve() arg 2 must contain only strings",
3229 Py_FileSystemDefaultEncoding,
3230 &argvlist[i]))
3231 {
3232 lastarg = i;
3233 goto fail_1;
3234 }
3235 }
3236 lastarg = argc;
3237 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003238
Victor Stinnerd6f85422010-05-05 23:33:33 +00003239 i = PyMapping_Size(env);
3240 if (i < 0)
3241 goto fail_1;
3242 envlist = PyMem_NEW(char *, i + 1);
3243 if (envlist == NULL) {
3244 PyErr_NoMemory();
3245 goto fail_1;
3246 }
3247 envc = 0;
3248 keys = PyMapping_Keys(env);
3249 vals = PyMapping_Values(env);
3250 if (!keys || !vals)
3251 goto fail_2;
3252 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3253 PyErr_SetString(PyExc_TypeError,
3254 "execve(): env.keys() or env.values() is not a list");
3255 goto fail_2;
3256 }
Tim Peters5aa91602002-01-30 05:46:57 +00003257
Victor Stinnerd6f85422010-05-05 23:33:33 +00003258 for (pos = 0; pos < i; pos++) {
3259 char *p, *k, *v;
3260 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003261
Victor Stinnerd6f85422010-05-05 23:33:33 +00003262 key = PyList_GetItem(keys, pos);
3263 val = PyList_GetItem(vals, pos);
3264 if (!key || !val)
3265 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003266
Victor Stinnerd6f85422010-05-05 23:33:33 +00003267 if (!PyArg_Parse(
3268 key,
3269 "s;execve() arg 3 contains a non-string key",
3270 &k) ||
3271 !PyArg_Parse(
3272 val,
3273 "s;execve() arg 3 contains a non-string value",
3274 &v))
3275 {
3276 goto fail_2;
3277 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003278
3279#if defined(PYOS_OS2)
3280 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3281 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3282#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003283 len = PyString_Size(key) + PyString_Size(val) + 2;
3284 p = PyMem_NEW(char, len);
3285 if (p == NULL) {
3286 PyErr_NoMemory();
3287 goto fail_2;
3288 }
3289 PyOS_snprintf(p, len, "%s=%s", k, v);
3290 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003291#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003292 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003293#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003294 }
3295 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003296
Victor Stinnerd6f85422010-05-05 23:33:33 +00003297 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003298
Victor Stinnerd6f85422010-05-05 23:33:33 +00003299 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003300
Victor Stinnerd6f85422010-05-05 23:33:33 +00003301 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003302
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003303 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003304 while (--envc >= 0)
3305 PyMem_DEL(envlist[envc]);
3306 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003307 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003308 free_string_array(argvlist, lastarg);
3309 Py_XDECREF(vals);
3310 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003311 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003312 PyMem_Free(path);
3313 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003314}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003315#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003316
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003317
Guido van Rossuma1065681999-01-25 23:20:23 +00003318#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003319PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003320"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003321Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003322\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003323 mode: mode of process creation\n\
3324 path: path of executable file\n\
3325 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003326
3327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003328posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003329{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003330 char *path;
3331 PyObject *argv;
3332 char **argvlist;
3333 int mode, i;
3334 Py_ssize_t argc;
3335 Py_intptr_t spawnval;
3336 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003337
Victor Stinnerd6f85422010-05-05 23:33:33 +00003338 /* spawnv has three arguments: (mode, path, argv), where
3339 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003340
Victor Stinnerd6f85422010-05-05 23:33:33 +00003341 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3342 Py_FileSystemDefaultEncoding,
3343 &path, &argv))
3344 return NULL;
3345 if (PyList_Check(argv)) {
3346 argc = PyList_Size(argv);
3347 getitem = PyList_GetItem;
3348 }
3349 else if (PyTuple_Check(argv)) {
3350 argc = PyTuple_Size(argv);
3351 getitem = PyTuple_GetItem;
3352 }
3353 else {
3354 PyErr_SetString(PyExc_TypeError,
3355 "spawnv() arg 2 must be a tuple or list");
3356 PyMem_Free(path);
3357 return NULL;
3358 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003359
Victor Stinnerd6f85422010-05-05 23:33:33 +00003360 argvlist = PyMem_NEW(char *, argc+1);
3361 if (argvlist == NULL) {
3362 PyMem_Free(path);
3363 return PyErr_NoMemory();
3364 }
3365 for (i = 0; i < argc; i++) {
3366 if (!PyArg_Parse((*getitem)(argv, i), "et",
3367 Py_FileSystemDefaultEncoding,
3368 &argvlist[i])) {
3369 free_string_array(argvlist, i);
3370 PyErr_SetString(
3371 PyExc_TypeError,
3372 "spawnv() arg 2 must contain only strings");
3373 PyMem_Free(path);
3374 return NULL;
3375 }
3376 }
3377 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003378
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003379#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003380 Py_BEGIN_ALLOW_THREADS
3381 spawnval = spawnv(mode, path, argvlist);
3382 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003383#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003384 if (mode == _OLD_P_OVERLAY)
3385 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003386
Victor Stinnerd6f85422010-05-05 23:33:33 +00003387 Py_BEGIN_ALLOW_THREADS
3388 spawnval = _spawnv(mode, path, argvlist);
3389 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003390#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003391
Victor Stinnerd6f85422010-05-05 23:33:33 +00003392 free_string_array(argvlist, argc);
3393 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003394
Victor Stinnerd6f85422010-05-05 23:33:33 +00003395 if (spawnval == -1)
3396 return posix_error();
3397 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003398#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003399 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003400#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003401 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003402#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003403}
3404
3405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003406PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003407"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003408Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003409\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003410 mode: mode of process creation\n\
3411 path: path of executable file\n\
3412 args: tuple or list of arguments\n\
3413 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003414
3415static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003416posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003417{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003418 char *path;
3419 PyObject *argv, *env;
3420 char **argvlist;
3421 char **envlist;
3422 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3423 int mode, pos, envc;
3424 Py_ssize_t argc, i;
3425 Py_intptr_t spawnval;
3426 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3427 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003428
Victor Stinnerd6f85422010-05-05 23:33:33 +00003429 /* spawnve has four arguments: (mode, path, argv, env), where
3430 argv is a list or tuple of strings and env is a dictionary
3431 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003432
Victor Stinnerd6f85422010-05-05 23:33:33 +00003433 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3434 Py_FileSystemDefaultEncoding,
3435 &path, &argv, &env))
3436 return NULL;
3437 if (PyList_Check(argv)) {
3438 argc = PyList_Size(argv);
3439 getitem = PyList_GetItem;
3440 }
3441 else if (PyTuple_Check(argv)) {
3442 argc = PyTuple_Size(argv);
3443 getitem = PyTuple_GetItem;
3444 }
3445 else {
3446 PyErr_SetString(PyExc_TypeError,
3447 "spawnve() arg 2 must be a tuple or list");
3448 goto fail_0;
3449 }
3450 if (!PyMapping_Check(env)) {
3451 PyErr_SetString(PyExc_TypeError,
3452 "spawnve() arg 3 must be a mapping object");
3453 goto fail_0;
3454 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003455
Victor Stinnerd6f85422010-05-05 23:33:33 +00003456 argvlist = PyMem_NEW(char *, argc+1);
3457 if (argvlist == NULL) {
3458 PyErr_NoMemory();
3459 goto fail_0;
3460 }
3461 for (i = 0; i < argc; i++) {
3462 if (!PyArg_Parse((*getitem)(argv, i),
3463 "et;spawnve() arg 2 must contain only strings",
3464 Py_FileSystemDefaultEncoding,
3465 &argvlist[i]))
3466 {
3467 lastarg = i;
3468 goto fail_1;
3469 }
3470 }
3471 lastarg = argc;
3472 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003473
Victor Stinnerd6f85422010-05-05 23:33:33 +00003474 i = PyMapping_Size(env);
3475 if (i < 0)
3476 goto fail_1;
3477 envlist = PyMem_NEW(char *, i + 1);
3478 if (envlist == NULL) {
3479 PyErr_NoMemory();
3480 goto fail_1;
3481 }
3482 envc = 0;
3483 keys = PyMapping_Keys(env);
3484 vals = PyMapping_Values(env);
3485 if (!keys || !vals)
3486 goto fail_2;
3487 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3488 PyErr_SetString(PyExc_TypeError,
3489 "spawnve(): env.keys() or env.values() is not a list");
3490 goto fail_2;
3491 }
Tim Peters5aa91602002-01-30 05:46:57 +00003492
Victor Stinnerd6f85422010-05-05 23:33:33 +00003493 for (pos = 0; pos < i; pos++) {
3494 char *p, *k, *v;
3495 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003496
Victor Stinnerd6f85422010-05-05 23:33:33 +00003497 key = PyList_GetItem(keys, pos);
3498 val = PyList_GetItem(vals, pos);
3499 if (!key || !val)
3500 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003501
Victor Stinnerd6f85422010-05-05 23:33:33 +00003502 if (!PyArg_Parse(
3503 key,
3504 "s;spawnve() arg 3 contains a non-string key",
3505 &k) ||
3506 !PyArg_Parse(
3507 val,
3508 "s;spawnve() arg 3 contains a non-string value",
3509 &v))
3510 {
3511 goto fail_2;
3512 }
3513 len = PyString_Size(key) + PyString_Size(val) + 2;
3514 p = PyMem_NEW(char, len);
3515 if (p == NULL) {
3516 PyErr_NoMemory();
3517 goto fail_2;
3518 }
3519 PyOS_snprintf(p, len, "%s=%s", k, v);
3520 envlist[envc++] = p;
3521 }
3522 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003523
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003524#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003525 Py_BEGIN_ALLOW_THREADS
3526 spawnval = spawnve(mode, path, argvlist, envlist);
3527 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003528#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003529 if (mode == _OLD_P_OVERLAY)
3530 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003531
Victor Stinnerd6f85422010-05-05 23:33:33 +00003532 Py_BEGIN_ALLOW_THREADS
3533 spawnval = _spawnve(mode, path, argvlist, envlist);
3534 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003535#endif
Tim Peters25059d32001-12-07 20:35:43 +00003536
Victor Stinnerd6f85422010-05-05 23:33:33 +00003537 if (spawnval == -1)
3538 (void) posix_error();
3539 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003540#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003541 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003542#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003543 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003544#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003545
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003546 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003547 while (--envc >= 0)
3548 PyMem_DEL(envlist[envc]);
3549 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003550 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003551 free_string_array(argvlist, lastarg);
3552 Py_XDECREF(vals);
3553 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003554 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003555 PyMem_Free(path);
3556 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003557}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003558
3559/* OS/2 supports spawnvp & spawnvpe natively */
3560#if defined(PYOS_OS2)
3561PyDoc_STRVAR(posix_spawnvp__doc__,
3562"spawnvp(mode, file, args)\n\n\
3563Execute the program 'file' in a new process, using the environment\n\
3564search path to find the file.\n\
3565\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003566 mode: mode of process creation\n\
3567 file: executable file name\n\
3568 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003569
3570static PyObject *
3571posix_spawnvp(PyObject *self, PyObject *args)
3572{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003573 char *path;
3574 PyObject *argv;
3575 char **argvlist;
3576 int mode, i, argc;
3577 Py_intptr_t spawnval;
3578 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003579
Victor Stinnerd6f85422010-05-05 23:33:33 +00003580 /* spawnvp has three arguments: (mode, path, argv), where
3581 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003582
Victor Stinnerd6f85422010-05-05 23:33:33 +00003583 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3584 Py_FileSystemDefaultEncoding,
3585 &path, &argv))
3586 return NULL;
3587 if (PyList_Check(argv)) {
3588 argc = PyList_Size(argv);
3589 getitem = PyList_GetItem;
3590 }
3591 else if (PyTuple_Check(argv)) {
3592 argc = PyTuple_Size(argv);
3593 getitem = PyTuple_GetItem;
3594 }
3595 else {
3596 PyErr_SetString(PyExc_TypeError,
3597 "spawnvp() arg 2 must be a tuple or list");
3598 PyMem_Free(path);
3599 return NULL;
3600 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003601
Victor Stinnerd6f85422010-05-05 23:33:33 +00003602 argvlist = PyMem_NEW(char *, argc+1);
3603 if (argvlist == NULL) {
3604 PyMem_Free(path);
3605 return PyErr_NoMemory();
3606 }
3607 for (i = 0; i < argc; i++) {
3608 if (!PyArg_Parse((*getitem)(argv, i), "et",
3609 Py_FileSystemDefaultEncoding,
3610 &argvlist[i])) {
3611 free_string_array(argvlist, i);
3612 PyErr_SetString(
3613 PyExc_TypeError,
3614 "spawnvp() arg 2 must contain only strings");
3615 PyMem_Free(path);
3616 return NULL;
3617 }
3618 }
3619 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003620
Victor Stinnerd6f85422010-05-05 23:33:33 +00003621 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003622#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003623 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003624#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003625 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003626#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003627 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003628
Victor Stinnerd6f85422010-05-05 23:33:33 +00003629 free_string_array(argvlist, argc);
3630 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003631
Victor Stinnerd6f85422010-05-05 23:33:33 +00003632 if (spawnval == -1)
3633 return posix_error();
3634 else
3635 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003636}
3637
3638
3639PyDoc_STRVAR(posix_spawnvpe__doc__,
3640"spawnvpe(mode, file, args, env)\n\n\
3641Execute the program 'file' in a new process, using the environment\n\
3642search path to find the file.\n\
3643\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003644 mode: mode of process creation\n\
3645 file: executable file name\n\
3646 args: tuple or list of arguments\n\
3647 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003648
3649static PyObject *
3650posix_spawnvpe(PyObject *self, PyObject *args)
3651{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003652 char *path;
3653 PyObject *argv, *env;
3654 char **argvlist;
3655 char **envlist;
3656 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3657 int mode, i, pos, argc, envc;
3658 Py_intptr_t spawnval;
3659 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3660 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003661
Victor Stinnerd6f85422010-05-05 23:33:33 +00003662 /* spawnvpe has four arguments: (mode, path, argv, env), where
3663 argv is a list or tuple of strings and env is a dictionary
3664 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003665
Victor Stinnerd6f85422010-05-05 23:33:33 +00003666 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3667 Py_FileSystemDefaultEncoding,
3668 &path, &argv, &env))
3669 return NULL;
3670 if (PyList_Check(argv)) {
3671 argc = PyList_Size(argv);
3672 getitem = PyList_GetItem;
3673 }
3674 else if (PyTuple_Check(argv)) {
3675 argc = PyTuple_Size(argv);
3676 getitem = PyTuple_GetItem;
3677 }
3678 else {
3679 PyErr_SetString(PyExc_TypeError,
3680 "spawnvpe() arg 2 must be a tuple or list");
3681 goto fail_0;
3682 }
3683 if (!PyMapping_Check(env)) {
3684 PyErr_SetString(PyExc_TypeError,
3685 "spawnvpe() arg 3 must be a mapping object");
3686 goto fail_0;
3687 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003688
Victor Stinnerd6f85422010-05-05 23:33:33 +00003689 argvlist = PyMem_NEW(char *, argc+1);
3690 if (argvlist == NULL) {
3691 PyErr_NoMemory();
3692 goto fail_0;
3693 }
3694 for (i = 0; i < argc; i++) {
3695 if (!PyArg_Parse((*getitem)(argv, i),
3696 "et;spawnvpe() arg 2 must contain only strings",
3697 Py_FileSystemDefaultEncoding,
3698 &argvlist[i]))
3699 {
3700 lastarg = i;
3701 goto fail_1;
3702 }
3703 }
3704 lastarg = argc;
3705 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003706
Victor Stinnerd6f85422010-05-05 23:33:33 +00003707 i = PyMapping_Size(env);
3708 if (i < 0)
3709 goto fail_1;
3710 envlist = PyMem_NEW(char *, i + 1);
3711 if (envlist == NULL) {
3712 PyErr_NoMemory();
3713 goto fail_1;
3714 }
3715 envc = 0;
3716 keys = PyMapping_Keys(env);
3717 vals = PyMapping_Values(env);
3718 if (!keys || !vals)
3719 goto fail_2;
3720 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3721 PyErr_SetString(PyExc_TypeError,
3722 "spawnvpe(): env.keys() or env.values() is not a list");
3723 goto fail_2;
3724 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003725
Victor Stinnerd6f85422010-05-05 23:33:33 +00003726 for (pos = 0; pos < i; pos++) {
3727 char *p, *k, *v;
3728 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003729
Victor Stinnerd6f85422010-05-05 23:33:33 +00003730 key = PyList_GetItem(keys, pos);
3731 val = PyList_GetItem(vals, pos);
3732 if (!key || !val)
3733 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003734
Victor Stinnerd6f85422010-05-05 23:33:33 +00003735 if (!PyArg_Parse(
3736 key,
3737 "s;spawnvpe() arg 3 contains a non-string key",
3738 &k) ||
3739 !PyArg_Parse(
3740 val,
3741 "s;spawnvpe() arg 3 contains a non-string value",
3742 &v))
3743 {
3744 goto fail_2;
3745 }
3746 len = PyString_Size(key) + PyString_Size(val) + 2;
3747 p = PyMem_NEW(char, len);
3748 if (p == NULL) {
3749 PyErr_NoMemory();
3750 goto fail_2;
3751 }
3752 PyOS_snprintf(p, len, "%s=%s", k, v);
3753 envlist[envc++] = p;
3754 }
3755 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003756
Victor Stinnerd6f85422010-05-05 23:33:33 +00003757 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003758#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003759 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003760#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003761 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003762#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003763 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003764
Victor Stinnerd6f85422010-05-05 23:33:33 +00003765 if (spawnval == -1)
3766 (void) posix_error();
3767 else
3768 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003769
3770 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003771 while (--envc >= 0)
3772 PyMem_DEL(envlist[envc]);
3773 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003774 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003775 free_string_array(argvlist, lastarg);
3776 Py_XDECREF(vals);
3777 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003778 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003779 PyMem_Free(path);
3780 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003781}
3782#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003783#endif /* HAVE_SPAWNV */
3784
3785
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003786#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003789Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3790\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003792
3793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003794posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003795{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003796 pid_t pid;
3797 int result = 0;
3798 _PyImport_AcquireLock();
3799 pid = fork1();
3800 if (pid == 0) {
3801 /* child: this clobbers and resets the import lock. */
3802 PyOS_AfterFork();
3803 } else {
3804 /* parent: release the import lock. */
3805 result = _PyImport_ReleaseLock();
3806 }
3807 if (pid == -1)
3808 return posix_error();
3809 if (result < 0) {
3810 /* Don't clobber the OSError if the fork failed. */
3811 PyErr_SetString(PyExc_RuntimeError,
3812 "not holding the import lock");
3813 return NULL;
3814 }
3815 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003816}
3817#endif
3818
3819
Guido van Rossumad0ee831995-03-01 10:34:45 +00003820#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003821PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003822"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003823Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003825
Barry Warsaw53699e91996-12-10 23:23:01 +00003826static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003827posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003828{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003829 pid_t pid;
3830 int result = 0;
3831 _PyImport_AcquireLock();
3832 pid = fork();
3833 if (pid == 0) {
3834 /* child: this clobbers and resets the import lock. */
3835 PyOS_AfterFork();
3836 } else {
3837 /* parent: release the import lock. */
3838 result = _PyImport_ReleaseLock();
3839 }
3840 if (pid == -1)
3841 return posix_error();
3842 if (result < 0) {
3843 /* Don't clobber the OSError if the fork failed. */
3844 PyErr_SetString(PyExc_RuntimeError,
3845 "not holding the import lock");
3846 return NULL;
3847 }
3848 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003849}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003850#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003851
Neal Norwitzb59798b2003-03-21 01:43:31 +00003852/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003853/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3854#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003855#define DEV_PTY_FILE "/dev/ptc"
3856#define HAVE_DEV_PTMX
3857#else
3858#define DEV_PTY_FILE "/dev/ptmx"
3859#endif
3860
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003861#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003862#ifdef HAVE_PTY_H
3863#include <pty.h>
3864#else
3865#ifdef HAVE_LIBUTIL_H
3866#include <libutil.h>
Ronald Oussorena55af9a2010-01-17 16:25:57 +00003867#else
3868#ifdef HAVE_UTIL_H
3869#include <util.h>
3870#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003871#endif /* HAVE_LIBUTIL_H */
3872#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003873#ifdef HAVE_STROPTS_H
3874#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003875#endif
3876#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003877
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003878#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003879PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003880"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003881Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003882
3883static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003884posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003885{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003886 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003887#ifndef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003888 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003889#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003890#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003891 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003892#ifdef sun
Victor Stinnerd6f85422010-05-05 23:33:33 +00003893 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003894#endif
3895#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003896
Thomas Wouters70c21a12000-07-14 14:28:33 +00003897#ifdef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003898 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3899 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003900#elif defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003901 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3902 if (slave_name == NULL)
3903 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003904
Victor Stinnerd6f85422010-05-05 23:33:33 +00003905 slave_fd = open(slave_name, O_RDWR);
3906 if (slave_fd < 0)
3907 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003908#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003909 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3910 if (master_fd < 0)
3911 return posix_error();
3912 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3913 /* change permission of slave */
3914 if (grantpt(master_fd) < 0) {
3915 PyOS_setsig(SIGCHLD, sig_saved);
3916 return posix_error();
3917 }
3918 /* unlock slave */
3919 if (unlockpt(master_fd) < 0) {
3920 PyOS_setsig(SIGCHLD, sig_saved);
3921 return posix_error();
3922 }
3923 PyOS_setsig(SIGCHLD, sig_saved);
3924 slave_name = ptsname(master_fd); /* get name of slave */
3925 if (slave_name == NULL)
3926 return posix_error();
3927 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3928 if (slave_fd < 0)
3929 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003930#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003931 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3932 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003933#ifndef __hpux
Victor Stinnerd6f85422010-05-05 23:33:33 +00003934 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003935#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003936#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003937#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003938
Victor Stinnerd6f85422010-05-05 23:33:33 +00003939 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003940
Fred Drake8cef4cf2000-06-28 16:40:38 +00003941}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003942#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003943
3944#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003945PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003946"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003947Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3948Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003949To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003950
3951static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003952posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003953{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003954 int master_fd = -1, result = 0;
3955 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003956
Victor Stinnerd6f85422010-05-05 23:33:33 +00003957 _PyImport_AcquireLock();
3958 pid = forkpty(&master_fd, NULL, NULL, NULL);
3959 if (pid == 0) {
3960 /* child: this clobbers and resets the import lock. */
3961 PyOS_AfterFork();
3962 } else {
3963 /* parent: release the import lock. */
3964 result = _PyImport_ReleaseLock();
3965 }
3966 if (pid == -1)
3967 return posix_error();
3968 if (result < 0) {
3969 /* Don't clobber the OSError if the fork failed. */
3970 PyErr_SetString(PyExc_RuntimeError,
3971 "not holding the import lock");
3972 return NULL;
3973 }
3974 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003975}
3976#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003977
Guido van Rossumad0ee831995-03-01 10:34:45 +00003978#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003979PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003980"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003981Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003982
Barry Warsaw53699e91996-12-10 23:23:01 +00003983static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003984posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003985{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02003986 return _PyInt_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003987}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003988#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003989
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003990
Guido van Rossumad0ee831995-03-01 10:34:45 +00003991#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003992PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003993"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003994Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003995
Barry Warsaw53699e91996-12-10 23:23:01 +00003996static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003997posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003998{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02003999 return _PyInt_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004000}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004001#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004003
Guido van Rossumad0ee831995-03-01 10:34:45 +00004004#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004006"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004007Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004008
Barry Warsaw53699e91996-12-10 23:23:01 +00004009static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004010posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004011{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004012 return _PyInt_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004013}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004014#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004018"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004019Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004020
Barry Warsaw53699e91996-12-10 23:23:01 +00004021static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004022posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004023{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004024 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004025}
4026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004027
Fred Drakec9680921999-12-13 16:37:25 +00004028#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004029PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004030"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004031Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004032
4033static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004034posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004035{
4036 PyObject *result = NULL;
4037
Fred Drakec9680921999-12-13 16:37:25 +00004038#ifdef NGROUPS_MAX
4039#define MAX_GROUPS NGROUPS_MAX
4040#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004041 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004042#define MAX_GROUPS 64
4043#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004044 gid_t grouplist[MAX_GROUPS];
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004045
Victor Stinner59729ff2011-07-05 11:28:19 +02004046 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004047 * This is a helper variable to store the intermediate result when
4048 * that happens.
4049 *
4050 * To keep the code readable the OSX behaviour is unconditional,
4051 * according to the POSIX spec this should be safe on all unix-y
4052 * systems.
4053 */
4054 gid_t* alt_grouplist = grouplist;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004055 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004056
Ned Deily80743642013-08-01 21:19:09 -07004057#ifdef __APPLE__
4058 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
4059 * there are more groups than can fit in grouplist. Therefore, on OS X
4060 * always first call getgroups with length 0 to get the actual number
4061 * of groups.
4062 */
4063 n = getgroups(0, NULL);
4064 if (n < 0) {
4065 return posix_error();
4066 } else if (n <= MAX_GROUPS) {
4067 /* groups will fit in existing array */
4068 alt_grouplist = grouplist;
4069 } else {
4070 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4071 if (alt_grouplist == NULL) {
4072 errno = EINVAL;
4073 return posix_error();
4074 }
4075 }
4076
4077 n = getgroups(n, alt_grouplist);
4078 if (n == -1) {
4079 if (alt_grouplist != grouplist) {
4080 PyMem_Free(alt_grouplist);
4081 }
4082 return posix_error();
4083 }
4084#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004085 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004086 if (n < 0) {
4087 if (errno == EINVAL) {
4088 n = getgroups(0, NULL);
4089 if (n == -1) {
4090 return posix_error();
4091 }
4092 if (n == 0) {
4093 /* Avoid malloc(0) */
4094 alt_grouplist = grouplist;
4095 } else {
4096 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4097 if (alt_grouplist == NULL) {
4098 errno = EINVAL;
4099 return posix_error();
4100 }
4101 n = getgroups(n, alt_grouplist);
4102 if (n == -1) {
4103 PyMem_Free(alt_grouplist);
4104 return posix_error();
4105 }
4106 }
4107 } else {
4108 return posix_error();
4109 }
4110 }
Ned Deily80743642013-08-01 21:19:09 -07004111#endif
4112
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004113 result = PyList_New(n);
4114 if (result != NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00004115 int i;
4116 for (i = 0; i < n; ++i) {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004117 PyObject *o = _PyInt_FromGid(alt_grouplist[i]);
Victor Stinnerd6f85422010-05-05 23:33:33 +00004118 if (o == NULL) {
Stefan Krah93f7a322010-11-26 17:35:50 +00004119 Py_DECREF(result);
4120 result = NULL;
4121 break;
Fred Drakec9680921999-12-13 16:37:25 +00004122 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00004123 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004124 }
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004125 }
4126
4127 if (alt_grouplist != grouplist) {
4128 PyMem_Free(alt_grouplist);
Victor Stinnerd6f85422010-05-05 23:33:33 +00004129 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004130
Fred Drakec9680921999-12-13 16:37:25 +00004131 return result;
4132}
4133#endif
4134
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004135#ifdef HAVE_INITGROUPS
4136PyDoc_STRVAR(posix_initgroups__doc__,
4137"initgroups(username, gid) -> None\n\n\
4138Call the system initgroups() to initialize the group access list with all of\n\
4139the groups of which the specified username is a member, plus the specified\n\
4140group id.");
4141
4142static PyObject *
4143posix_initgroups(PyObject *self, PyObject *args)
4144{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004145 char *username;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004146#ifdef __APPLE__
4147 int gid;
4148#else
4149 gid_t gid;
4150#endif
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004151
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004152#ifdef __APPLE__
4153 if (!PyArg_ParseTuple(args, "si:initgroups", &username,
4154 &gid))
4155#else
4156 if (!PyArg_ParseTuple(args, "sO&:initgroups", &username,
4157 _Py_Gid_Converter, &gid))
4158#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004159 return NULL;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004160
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004161 if (initgroups(username, gid) == -1)
Victor Stinnerd6f85422010-05-05 23:33:33 +00004162 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004163
Victor Stinnerd6f85422010-05-05 23:33:33 +00004164 Py_INCREF(Py_None);
4165 return Py_None;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004166}
4167#endif
4168
Martin v. Löwis606edc12002-06-13 21:09:11 +00004169#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004170PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004171"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004172Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004173
4174static PyObject *
4175posix_getpgid(PyObject *self, PyObject *args)
4176{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004177 pid_t pid, pgid;
4178 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
4179 return NULL;
4180 pgid = getpgid(pid);
4181 if (pgid < 0)
4182 return posix_error();
4183 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004184}
4185#endif /* HAVE_GETPGID */
4186
4187
Guido van Rossumb6775db1994-08-01 11:34:53 +00004188#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004189PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004190"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004191Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004192
Barry Warsaw53699e91996-12-10 23:23:01 +00004193static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004194posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004195{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004196#ifdef GETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004197 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004198#else /* GETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004199 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004200#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004201}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004202#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004203
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004204
Guido van Rossumb6775db1994-08-01 11:34:53 +00004205#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004206PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004207"setpgrp()\n\n\
Senthil Kumaran0d6908b2010-06-17 16:38:34 +00004208Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004209
Barry Warsaw53699e91996-12-10 23:23:01 +00004210static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004211posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004212{
Guido van Rossum64933891994-10-20 21:56:42 +00004213#ifdef SETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004214 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004215#else /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004216 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004217#endif /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004218 return posix_error();
4219 Py_INCREF(Py_None);
4220 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004221}
4222
Guido van Rossumb6775db1994-08-01 11:34:53 +00004223#endif /* HAVE_SETPGRP */
4224
Guido van Rossumad0ee831995-03-01 10:34:45 +00004225#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004226PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004227"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004228Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004229
Barry Warsaw53699e91996-12-10 23:23:01 +00004230static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004231posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004232{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004233 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004234}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004235#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004236
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004237
Fred Drake12c6e2d1999-12-14 21:25:03 +00004238#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004240"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004241Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004242
4243static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004244posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004245{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004246 PyObject *result = NULL;
4247 char *name;
4248 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004249
Victor Stinnerd6f85422010-05-05 23:33:33 +00004250 errno = 0;
4251 name = getlogin();
4252 if (name == NULL) {
4253 if (errno)
4254 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004255 else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004256 PyErr_SetString(PyExc_OSError,
4257 "unable to determine login name");
4258 }
4259 else
4260 result = PyString_FromString(name);
4261 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004262
Fred Drake12c6e2d1999-12-14 21:25:03 +00004263 return result;
4264}
4265#endif
4266
Guido van Rossumad0ee831995-03-01 10:34:45 +00004267#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004268PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004269"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004270Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004271
Barry Warsaw53699e91996-12-10 23:23:01 +00004272static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004273posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004274{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004275 return _PyInt_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004276}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004277#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004279
Guido van Rossumad0ee831995-03-01 10:34:45 +00004280#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004281PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004282"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004283Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004284
Barry Warsaw53699e91996-12-10 23:23:01 +00004285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004286posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004287{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004288 pid_t pid;
4289 int sig;
4290 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4291 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004292#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004293 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4294 APIRET rc;
4295 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004296 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004297
4298 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4299 APIRET rc;
4300 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004301 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004302
4303 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004304 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004305#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004306 if (kill(pid, sig) == -1)
4307 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004308#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004309 Py_INCREF(Py_None);
4310 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004311}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004312#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004313
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004314#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004315PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004316"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004317Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004318
4319static PyObject *
4320posix_killpg(PyObject *self, PyObject *args)
4321{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004322 int sig;
4323 pid_t pgid;
4324 /* XXX some man pages make the `pgid` parameter an int, others
4325 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4326 take the same type. Moreover, pid_t is always at least as wide as
4327 int (else compilation of this module fails), which is safe. */
4328 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4329 return NULL;
4330 if (killpg(pgid, sig) == -1)
4331 return posix_error();
4332 Py_INCREF(Py_None);
4333 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004334}
4335#endif
4336
Brian Curtine5aa8862010-04-02 23:26:06 +00004337#ifdef MS_WINDOWS
4338PyDoc_STRVAR(win32_kill__doc__,
4339"kill(pid, sig)\n\n\
4340Kill a process with a signal.");
4341
4342static PyObject *
4343win32_kill(PyObject *self, PyObject *args)
4344{
Amaury Forgeot d'Arc03acec22010-05-15 21:45:30 +00004345 PyObject *result;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004346 DWORD pid, sig, err;
4347 HANDLE handle;
Brian Curtine5aa8862010-04-02 23:26:06 +00004348
Victor Stinnerd6f85422010-05-05 23:33:33 +00004349 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4350 return NULL;
Brian Curtine5aa8862010-04-02 23:26:06 +00004351
Victor Stinnerd6f85422010-05-05 23:33:33 +00004352 /* Console processes which share a common console can be sent CTRL+C or
4353 CTRL+BREAK events, provided they handle said events. */
4354 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4355 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4356 err = GetLastError();
4357 return PyErr_SetFromWindowsErr(err);
4358 }
4359 else
4360 Py_RETURN_NONE;
4361 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004362
Victor Stinnerd6f85422010-05-05 23:33:33 +00004363 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4364 attempt to open and terminate the process. */
4365 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4366 if (handle == NULL) {
4367 err = GetLastError();
4368 return PyErr_SetFromWindowsErr(err);
4369 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004370
Victor Stinnerd6f85422010-05-05 23:33:33 +00004371 if (TerminateProcess(handle, sig) == 0) {
4372 err = GetLastError();
4373 result = PyErr_SetFromWindowsErr(err);
4374 } else {
4375 Py_INCREF(Py_None);
4376 result = Py_None;
4377 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004378
Victor Stinnerd6f85422010-05-05 23:33:33 +00004379 CloseHandle(handle);
4380 return result;
Brian Curtine5aa8862010-04-02 23:26:06 +00004381}
Brian Curtincaea7e82011-06-08 19:29:53 -05004382
Brian Curtin5446f082011-06-09 10:00:42 -05004383PyDoc_STRVAR(posix__isdir__doc__,
4384"Return true if the pathname refers to an existing directory.");
4385
Brian Curtincaea7e82011-06-08 19:29:53 -05004386static PyObject *
4387posix__isdir(PyObject *self, PyObject *args)
4388{
4389 PyObject *opath;
4390 char *path;
4391 PyUnicodeObject *po;
4392 DWORD attributes;
4393
4394 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
4395 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
4396
4397 attributes = GetFileAttributesW(wpath);
4398 if (attributes == INVALID_FILE_ATTRIBUTES)
4399 Py_RETURN_FALSE;
4400 goto check;
4401 }
4402 /* Drop the argument parsing error as narrow strings
4403 are also valid. */
4404 PyErr_Clear();
4405
4406 if (!PyArg_ParseTuple(args, "et:_isdir",
4407 Py_FileSystemDefaultEncoding, &path))
4408 return NULL;
4409
4410 attributes = GetFileAttributesA(path);
Serhiy Storchaka46f5b352013-01-28 20:19:50 +02004411 PyMem_Free(path);
Brian Curtincaea7e82011-06-08 19:29:53 -05004412 if (attributes == INVALID_FILE_ATTRIBUTES)
4413 Py_RETURN_FALSE;
4414
4415check:
4416 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
4417 Py_RETURN_TRUE;
4418 else
4419 Py_RETURN_FALSE;
4420}
Brian Curtine5aa8862010-04-02 23:26:06 +00004421#endif /* MS_WINDOWS */
4422
Guido van Rossumc0125471996-06-28 18:55:32 +00004423#ifdef HAVE_PLOCK
4424
4425#ifdef HAVE_SYS_LOCK_H
4426#include <sys/lock.h>
4427#endif
4428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004429PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004430"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004432
Barry Warsaw53699e91996-12-10 23:23:01 +00004433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004434posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004435{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004436 int op;
4437 if (!PyArg_ParseTuple(args, "i:plock", &op))
4438 return NULL;
4439 if (plock(op) == -1)
4440 return posix_error();
4441 Py_INCREF(Py_None);
4442 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004443}
4444#endif
4445
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004446
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004447#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004448PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004449"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004450Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004451
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004452#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004453#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004454static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004455async_system(const char *command)
4456{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004457 char errormsg[256], args[1024];
4458 RESULTCODES rcodes;
4459 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004460
Victor Stinnerd6f85422010-05-05 23:33:33 +00004461 char *shell = getenv("COMSPEC");
4462 if (!shell)
4463 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004464
Victor Stinnerd6f85422010-05-05 23:33:33 +00004465 /* avoid overflowing the argument buffer */
4466 if (strlen(shell) + 3 + strlen(command) >= 1024)
4467 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004468
Victor Stinnerd6f85422010-05-05 23:33:33 +00004469 args[0] = '\0';
4470 strcat(args, shell);
4471 strcat(args, "/c ");
4472 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004473
Victor Stinnerd6f85422010-05-05 23:33:33 +00004474 /* execute asynchronously, inheriting the environment */
4475 rc = DosExecPgm(errormsg,
4476 sizeof(errormsg),
4477 EXEC_ASYNC,
4478 args,
4479 NULL,
4480 &rcodes,
4481 shell);
4482 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004483}
4484
Guido van Rossumd48f2521997-12-05 22:19:34 +00004485static FILE *
4486popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004487{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004488 int oldfd, tgtfd;
4489 HFILE pipeh[2];
4490 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004491
Victor Stinnerd6f85422010-05-05 23:33:33 +00004492 /* mode determines which of stdin or stdout is reconnected to
4493 * the pipe to the child
4494 */
4495 if (strchr(mode, 'r') != NULL) {
4496 tgt_fd = 1; /* stdout */
4497 } else if (strchr(mode, 'w')) {
4498 tgt_fd = 0; /* stdin */
4499 } else {
4500 *err = ERROR_INVALID_ACCESS;
4501 return NULL;
4502 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004503
Victor Stinnerd6f85422010-05-05 23:33:33 +00004504 /* setup the pipe */
4505 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4506 *err = rc;
4507 return NULL;
4508 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004509
Victor Stinnerd6f85422010-05-05 23:33:33 +00004510 /* prevent other threads accessing stdio */
4511 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004512
Victor Stinnerd6f85422010-05-05 23:33:33 +00004513 /* reconnect stdio and execute child */
4514 oldfd = dup(tgtfd);
4515 close(tgtfd);
4516 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4517 DosClose(pipeh[tgtfd]);
4518 rc = async_system(command);
4519 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004520
Victor Stinnerd6f85422010-05-05 23:33:33 +00004521 /* restore stdio */
4522 dup2(oldfd, tgtfd);
4523 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004524
Victor Stinnerd6f85422010-05-05 23:33:33 +00004525 /* allow other threads access to stdio */
4526 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004527
Victor Stinnerd6f85422010-05-05 23:33:33 +00004528 /* if execution of child was successful return file stream */
4529 if (rc == NO_ERROR)
4530 return fdopen(pipeh[1 - tgtfd], mode);
4531 else {
4532 DosClose(pipeh[1 - tgtfd]);
4533 *err = rc;
4534 return NULL;
4535 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004536}
4537
4538static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004539posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004540{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004541 char *name;
4542 char *mode = "r";
4543 int err, bufsize = -1;
4544 FILE *fp;
4545 PyObject *f;
4546 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4547 return NULL;
4548 Py_BEGIN_ALLOW_THREADS
4549 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4550 Py_END_ALLOW_THREADS
4551 if (fp == NULL)
4552 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004553
Victor Stinnerd6f85422010-05-05 23:33:33 +00004554 f = PyFile_FromFile(fp, name, mode, fclose);
4555 if (f != NULL)
4556 PyFile_SetBufSize(f, bufsize);
4557 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004558}
4559
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004560#elif defined(PYCC_GCC)
4561
4562/* standard posix version of popen() support */
4563static PyObject *
4564posix_popen(PyObject *self, PyObject *args)
4565{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004566 char *name;
4567 char *mode = "r";
4568 int bufsize = -1;
4569 FILE *fp;
4570 PyObject *f;
4571 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4572 return NULL;
4573 Py_BEGIN_ALLOW_THREADS
4574 fp = popen(name, mode);
4575 Py_END_ALLOW_THREADS
4576 if (fp == NULL)
4577 return posix_error();
4578 f = PyFile_FromFile(fp, name, mode, pclose);
4579 if (f != NULL)
4580 PyFile_SetBufSize(f, bufsize);
4581 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004582}
4583
4584/* fork() under OS/2 has lots'o'warts
4585 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4586 * most of this code is a ripoff of the win32 code, but using the
4587 * capabilities of EMX's C library routines
4588 */
4589
4590/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4591#define POPEN_1 1
4592#define POPEN_2 2
4593#define POPEN_3 3
4594#define POPEN_4 4
4595
4596static PyObject *_PyPopen(char *, int, int, int);
4597static int _PyPclose(FILE *file);
4598
4599/*
4600 * Internal dictionary mapping popen* file pointers to process handles,
4601 * for use when retrieving the process exit code. See _PyPclose() below
4602 * for more information on this dictionary's use.
4603 */
4604static PyObject *_PyPopenProcs = NULL;
4605
4606/* os2emx version of popen2()
4607 *
4608 * The result of this function is a pipe (file) connected to the
4609 * process's stdin, and a pipe connected to the process's stdout.
4610 */
4611
4612static PyObject *
4613os2emx_popen2(PyObject *self, PyObject *args)
4614{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004615 PyObject *f;
4616 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004617
Victor Stinnerd6f85422010-05-05 23:33:33 +00004618 char *cmdstring;
4619 char *mode = "t";
4620 int bufsize = -1;
4621 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4622 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004623
Victor Stinnerd6f85422010-05-05 23:33:33 +00004624 if (*mode == 't')
4625 tm = O_TEXT;
4626 else if (*mode != 'b') {
4627 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4628 return NULL;
4629 } else
4630 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004631
Victor Stinnerd6f85422010-05-05 23:33:33 +00004632 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004633
Victor Stinnerd6f85422010-05-05 23:33:33 +00004634 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004635}
4636
4637/*
4638 * Variation on os2emx.popen2
4639 *
4640 * The result of this function is 3 pipes - the process's stdin,
4641 * stdout and stderr
4642 */
4643
4644static PyObject *
4645os2emx_popen3(PyObject *self, PyObject *args)
4646{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004647 PyObject *f;
4648 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004649
Victor Stinnerd6f85422010-05-05 23:33:33 +00004650 char *cmdstring;
4651 char *mode = "t";
4652 int bufsize = -1;
4653 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4654 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004655
Victor Stinnerd6f85422010-05-05 23:33:33 +00004656 if (*mode == 't')
4657 tm = O_TEXT;
4658 else if (*mode != 'b') {
4659 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4660 return NULL;
4661 } else
4662 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004663
Victor Stinnerd6f85422010-05-05 23:33:33 +00004664 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004665
Victor Stinnerd6f85422010-05-05 23:33:33 +00004666 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004667}
4668
4669/*
4670 * Variation on os2emx.popen2
4671 *
Tim Peters11b23062003-04-23 02:39:17 +00004672 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004673 * and stdout+stderr combined as a single pipe.
4674 */
4675
4676static PyObject *
4677os2emx_popen4(PyObject *self, PyObject *args)
4678{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004679 PyObject *f;
4680 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004681
Victor Stinnerd6f85422010-05-05 23:33:33 +00004682 char *cmdstring;
4683 char *mode = "t";
4684 int bufsize = -1;
4685 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4686 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004687
Victor Stinnerd6f85422010-05-05 23:33:33 +00004688 if (*mode == 't')
4689 tm = O_TEXT;
4690 else if (*mode != 'b') {
4691 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4692 return NULL;
4693 } else
4694 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004695
Victor Stinnerd6f85422010-05-05 23:33:33 +00004696 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004697
Victor Stinnerd6f85422010-05-05 23:33:33 +00004698 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004699}
4700
4701/* a couple of structures for convenient handling of multiple
4702 * file handles and pipes
4703 */
4704struct file_ref
4705{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004706 int handle;
4707 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004708};
4709
4710struct pipe_ref
4711{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004712 int rd;
4713 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004714};
4715
4716/* The following code is derived from the win32 code */
4717
4718static PyObject *
4719_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4720{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004721 struct file_ref stdio[3];
4722 struct pipe_ref p_fd[3];
4723 FILE *p_s[3];
4724 int file_count, i, pipe_err;
4725 pid_t pipe_pid;
4726 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4727 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004728
Victor Stinnerd6f85422010-05-05 23:33:33 +00004729 /* file modes for subsequent fdopen's on pipe handles */
4730 if (mode == O_TEXT)
4731 {
4732 rd_mode = "rt";
4733 wr_mode = "wt";
4734 }
4735 else
4736 {
4737 rd_mode = "rb";
4738 wr_mode = "wb";
4739 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004740
Victor Stinnerd6f85422010-05-05 23:33:33 +00004741 /* prepare shell references */
4742 if ((shell = getenv("EMXSHELL")) == NULL)
4743 if ((shell = getenv("COMSPEC")) == NULL)
4744 {
4745 errno = ENOENT;
4746 return posix_error();
4747 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004748
Victor Stinnerd6f85422010-05-05 23:33:33 +00004749 sh_name = _getname(shell);
4750 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4751 opt = "/c";
4752 else
4753 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004754
Victor Stinnerd6f85422010-05-05 23:33:33 +00004755 /* save current stdio fds + their flags, and set not inheritable */
4756 i = pipe_err = 0;
4757 while (pipe_err >= 0 && i < 3)
4758 {
4759 pipe_err = stdio[i].handle = dup(i);
4760 stdio[i].flags = fcntl(i, F_GETFD, 0);
4761 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4762 i++;
4763 }
4764 if (pipe_err < 0)
4765 {
4766 /* didn't get them all saved - clean up and bail out */
4767 int saved_err = errno;
4768 while (i-- > 0)
4769 {
4770 close(stdio[i].handle);
4771 }
4772 errno = saved_err;
4773 return posix_error();
4774 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004775
Victor Stinnerd6f85422010-05-05 23:33:33 +00004776 /* create pipe ends */
4777 file_count = 2;
4778 if (n == POPEN_3)
4779 file_count = 3;
4780 i = pipe_err = 0;
4781 while ((pipe_err == 0) && (i < file_count))
4782 pipe_err = pipe((int *)&p_fd[i++]);
4783 if (pipe_err < 0)
4784 {
4785 /* didn't get them all made - clean up and bail out */
4786 while (i-- > 0)
4787 {
4788 close(p_fd[i].wr);
4789 close(p_fd[i].rd);
4790 }
4791 errno = EPIPE;
4792 return posix_error();
4793 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004794
Victor Stinnerd6f85422010-05-05 23:33:33 +00004795 /* change the actual standard IO streams over temporarily,
4796 * making the retained pipe ends non-inheritable
4797 */
4798 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004799
Victor Stinnerd6f85422010-05-05 23:33:33 +00004800 /* - stdin */
4801 if (dup2(p_fd[0].rd, 0) == 0)
4802 {
4803 close(p_fd[0].rd);
4804 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4805 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4806 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4807 {
4808 close(p_fd[0].wr);
4809 pipe_err = -1;
4810 }
4811 }
4812 else
4813 {
4814 pipe_err = -1;
4815 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004816
Victor Stinnerd6f85422010-05-05 23:33:33 +00004817 /* - stdout */
4818 if (pipe_err == 0)
4819 {
4820 if (dup2(p_fd[1].wr, 1) == 1)
4821 {
4822 close(p_fd[1].wr);
4823 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4824 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4825 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4826 {
4827 close(p_fd[1].rd);
4828 pipe_err = -1;
4829 }
4830 }
4831 else
4832 {
4833 pipe_err = -1;
4834 }
4835 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004836
Victor Stinnerd6f85422010-05-05 23:33:33 +00004837 /* - stderr, as required */
4838 if (pipe_err == 0)
4839 switch (n)
4840 {
4841 case POPEN_3:
4842 {
4843 if (dup2(p_fd[2].wr, 2) == 2)
4844 {
4845 close(p_fd[2].wr);
4846 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4847 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4848 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4849 {
4850 close(p_fd[2].rd);
4851 pipe_err = -1;
4852 }
4853 }
4854 else
4855 {
4856 pipe_err = -1;
4857 }
4858 break;
4859 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004860
Victor Stinnerd6f85422010-05-05 23:33:33 +00004861 case POPEN_4:
4862 {
4863 if (dup2(1, 2) != 2)
4864 {
4865 pipe_err = -1;
4866 }
4867 break;
4868 }
4869 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004870
Victor Stinnerd6f85422010-05-05 23:33:33 +00004871 /* spawn the child process */
4872 if (pipe_err == 0)
4873 {
4874 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4875 if (pipe_pid == -1)
4876 {
4877 pipe_err = -1;
4878 }
4879 else
4880 {
4881 /* save the PID into the FILE structure
4882 * NOTE: this implementation doesn't actually
4883 * take advantage of this, but do it for
4884 * completeness - AIM Apr01
4885 */
4886 for (i = 0; i < file_count; i++)
4887 p_s[i]->_pid = pipe_pid;
4888 }
4889 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004890
Victor Stinnerd6f85422010-05-05 23:33:33 +00004891 /* reset standard IO to normal */
4892 for (i = 0; i < 3; i++)
4893 {
4894 dup2(stdio[i].handle, i);
4895 fcntl(i, F_SETFD, stdio[i].flags);
4896 close(stdio[i].handle);
4897 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004898
Victor Stinnerd6f85422010-05-05 23:33:33 +00004899 /* if any remnant problems, clean up and bail out */
4900 if (pipe_err < 0)
4901 {
4902 for (i = 0; i < 3; i++)
4903 {
4904 close(p_fd[i].rd);
4905 close(p_fd[i].wr);
4906 }
4907 errno = EPIPE;
4908 return posix_error_with_filename(cmdstring);
4909 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004910
Victor Stinnerd6f85422010-05-05 23:33:33 +00004911 /* build tuple of file objects to return */
4912 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4913 PyFile_SetBufSize(p_f[0], bufsize);
4914 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4915 PyFile_SetBufSize(p_f[1], bufsize);
4916 if (n == POPEN_3)
4917 {
4918 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4919 PyFile_SetBufSize(p_f[0], bufsize);
4920 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4921 }
4922 else
4923 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004924
Victor Stinnerd6f85422010-05-05 23:33:33 +00004925 /*
4926 * Insert the files we've created into the process dictionary
4927 * all referencing the list with the process handle and the
4928 * initial number of files (see description below in _PyPclose).
4929 * Since if _PyPclose later tried to wait on a process when all
4930 * handles weren't closed, it could create a deadlock with the
4931 * child, we spend some energy here to try to ensure that we
4932 * either insert all file handles into the dictionary or none
4933 * at all. It's a little clumsy with the various popen modes
4934 * and variable number of files involved.
4935 */
4936 if (!_PyPopenProcs)
4937 {
4938 _PyPopenProcs = PyDict_New();
4939 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004940
Victor Stinnerd6f85422010-05-05 23:33:33 +00004941 if (_PyPopenProcs)
4942 {
4943 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4944 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004945
Victor Stinnerd6f85422010-05-05 23:33:33 +00004946 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4947 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004948
Victor Stinnerd6f85422010-05-05 23:33:33 +00004949 procObj = PyList_New(2);
4950 pidObj = PyLong_FromPid(pipe_pid);
4951 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004952
Victor Stinnerd6f85422010-05-05 23:33:33 +00004953 if (procObj && pidObj && intObj)
4954 {
4955 PyList_SetItem(procObj, 0, pidObj);
4956 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004957
Victor Stinnerd6f85422010-05-05 23:33:33 +00004958 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4959 if (fileObj[0])
4960 {
4961 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4962 fileObj[0],
4963 procObj);
4964 }
4965 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4966 if (fileObj[1])
4967 {
4968 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4969 fileObj[1],
4970 procObj);
4971 }
4972 if (file_count >= 3)
4973 {
4974 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4975 if (fileObj[2])
4976 {
4977 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4978 fileObj[2],
4979 procObj);
4980 }
4981 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004982
Victor Stinnerd6f85422010-05-05 23:33:33 +00004983 if (ins_rc[0] < 0 || !fileObj[0] ||
4984 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4985 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4986 {
4987 /* Something failed - remove any dictionary
4988 * entries that did make it.
4989 */
4990 if (!ins_rc[0] && fileObj[0])
4991 {
4992 PyDict_DelItem(_PyPopenProcs,
4993 fileObj[0]);
4994 }
4995 if (!ins_rc[1] && fileObj[1])
4996 {
4997 PyDict_DelItem(_PyPopenProcs,
4998 fileObj[1]);
4999 }
5000 if (!ins_rc[2] && fileObj[2])
5001 {
5002 PyDict_DelItem(_PyPopenProcs,
5003 fileObj[2]);
5004 }
5005 }
5006 }
Tim Peters11b23062003-04-23 02:39:17 +00005007
Victor Stinnerd6f85422010-05-05 23:33:33 +00005008 /*
5009 * Clean up our localized references for the dictionary keys
5010 * and value since PyDict_SetItem will Py_INCREF any copies
5011 * that got placed in the dictionary.
5012 */
5013 Py_XDECREF(procObj);
5014 Py_XDECREF(fileObj[0]);
5015 Py_XDECREF(fileObj[1]);
5016 Py_XDECREF(fileObj[2]);
5017 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005018
Victor Stinnerd6f85422010-05-05 23:33:33 +00005019 /* Child is launched. */
5020 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005021}
5022
5023/*
5024 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5025 * exit code for the child process and return as a result of the close.
5026 *
5027 * This function uses the _PyPopenProcs dictionary in order to map the
5028 * input file pointer to information about the process that was
5029 * originally created by the popen* call that created the file pointer.
5030 * The dictionary uses the file pointer as a key (with one entry
5031 * inserted for each file returned by the original popen* call) and a
5032 * single list object as the value for all files from a single call.
5033 * The list object contains the Win32 process handle at [0], and a file
5034 * count at [1], which is initialized to the total number of file
5035 * handles using that list.
5036 *
5037 * This function closes whichever handle it is passed, and decrements
5038 * the file count in the dictionary for the process handle pointed to
5039 * by this file. On the last close (when the file count reaches zero),
5040 * this function will wait for the child process and then return its
5041 * exit code as the result of the close() operation. This permits the
5042 * files to be closed in any order - it is always the close() of the
5043 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00005044 *
5045 * NOTE: This function is currently called with the GIL released.
5046 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005047 */
5048
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005049static int _PyPclose(FILE *file)
5050{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005051 int result;
5052 int exit_code;
5053 pid_t pipe_pid;
5054 PyObject *procObj, *pidObj, *intObj, *fileObj;
5055 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005056#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005057 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005058#endif
5059
Victor Stinnerd6f85422010-05-05 23:33:33 +00005060 /* Close the file handle first, to ensure it can't block the
5061 * child from exiting if it's the last handle.
5062 */
5063 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005064
5065#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005066 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005067#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005068 if (_PyPopenProcs)
5069 {
5070 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5071 (procObj = PyDict_GetItem(_PyPopenProcs,
5072 fileObj)) != NULL &&
5073 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
5074 (intObj = PyList_GetItem(procObj,1)) != NULL)
5075 {
5076 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
5077 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005078
Victor Stinnerd6f85422010-05-05 23:33:33 +00005079 if (file_count > 1)
5080 {
5081 /* Still other files referencing process */
5082 file_count--;
5083 PyList_SetItem(procObj,1,
5084 PyInt_FromLong((long) file_count));
5085 }
5086 else
5087 {
5088 /* Last file for this process */
5089 if (result != EOF &&
5090 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
5091 {
5092 /* extract exit status */
5093 if (WIFEXITED(exit_code))
5094 {
5095 result = WEXITSTATUS(exit_code);
5096 }
5097 else
5098 {
5099 errno = EPIPE;
5100 result = -1;
5101 }
5102 }
5103 else
5104 {
5105 /* Indicate failure - this will cause the file object
5106 * to raise an I/O error and translate the last
5107 * error code from errno. We do have a problem with
5108 * last errors that overlap the normal errno table,
5109 * but that's a consistent problem with the file object.
5110 */
5111 result = -1;
5112 }
5113 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005114
Victor Stinnerd6f85422010-05-05 23:33:33 +00005115 /* Remove this file pointer from dictionary */
5116 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005117
Victor Stinnerd6f85422010-05-05 23:33:33 +00005118 if (PyDict_Size(_PyPopenProcs) == 0)
5119 {
5120 Py_DECREF(_PyPopenProcs);
5121 _PyPopenProcs = NULL;
5122 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005123
Victor Stinnerd6f85422010-05-05 23:33:33 +00005124 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005125
Victor Stinnerd6f85422010-05-05 23:33:33 +00005126 Py_XDECREF(fileObj);
5127 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005128
5129#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005130 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005131#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005132 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005133}
5134
5135#endif /* PYCC_??? */
5136
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005137#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005138
5139/*
5140 * Portable 'popen' replacement for Win32.
5141 *
5142 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
5143 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00005144 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005145 */
5146
5147#include <malloc.h>
5148#include <io.h>
5149#include <fcntl.h>
5150
5151/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
5152#define POPEN_1 1
5153#define POPEN_2 2
5154#define POPEN_3 3
5155#define POPEN_4 4
5156
5157static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005158static int _PyPclose(FILE *file);
5159
5160/*
5161 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00005162 * for use when retrieving the process exit code. See _PyPclose() below
5163 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005164 */
5165static PyObject *_PyPopenProcs = NULL;
5166
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005167
5168/* popen that works from a GUI.
5169 *
5170 * The result of this function is a pipe (file) connected to the
5171 * processes stdin or stdout, depending on the requested mode.
5172 */
5173
5174static PyObject *
5175posix_popen(PyObject *self, PyObject *args)
5176{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005177 PyObject *f;
5178 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005179
Victor Stinnerd6f85422010-05-05 23:33:33 +00005180 char *cmdstring;
5181 char *mode = "r";
5182 int bufsize = -1;
5183 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
5184 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005185
Victor Stinnerd6f85422010-05-05 23:33:33 +00005186 if (*mode == 'r')
5187 tm = _O_RDONLY;
5188 else if (*mode != 'w') {
5189 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
5190 return NULL;
5191 } else
5192 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00005193
Victor Stinnerd6f85422010-05-05 23:33:33 +00005194 if (bufsize != -1) {
5195 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
5196 return NULL;
5197 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005198
Victor Stinnerd6f85422010-05-05 23:33:33 +00005199 if (*(mode+1) == 't')
5200 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
5201 else if (*(mode+1) == 'b')
5202 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
5203 else
5204 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005205
Victor Stinnerd6f85422010-05-05 23:33:33 +00005206 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005207}
5208
5209/* Variation on win32pipe.popen
5210 *
5211 * The result of this function is a pipe (file) connected to the
5212 * process's stdin, and a pipe connected to the process's stdout.
5213 */
5214
5215static PyObject *
5216win32_popen2(PyObject *self, PyObject *args)
5217{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005218 PyObject *f;
5219 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00005220
Victor Stinnerd6f85422010-05-05 23:33:33 +00005221 char *cmdstring;
5222 char *mode = "t";
5223 int bufsize = -1;
5224 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
5225 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005226
Victor Stinnerd6f85422010-05-05 23:33:33 +00005227 if (*mode == 't')
5228 tm = _O_TEXT;
5229 else if (*mode != 'b') {
5230 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
5231 return NULL;
5232 } else
5233 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005234
Victor Stinnerd6f85422010-05-05 23:33:33 +00005235 if (bufsize != -1) {
5236 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
5237 return NULL;
5238 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005239
Victor Stinnerd6f85422010-05-05 23:33:33 +00005240 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00005241
Victor Stinnerd6f85422010-05-05 23:33:33 +00005242 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005243}
5244
5245/*
5246 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00005247 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005248 * The result of this function is 3 pipes - the process's stdin,
5249 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005250 */
5251
5252static PyObject *
5253win32_popen3(PyObject *self, PyObject *args)
5254{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005255 PyObject *f;
5256 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005257
Victor Stinnerd6f85422010-05-05 23:33:33 +00005258 char *cmdstring;
5259 char *mode = "t";
5260 int bufsize = -1;
5261 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
5262 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005263
Victor Stinnerd6f85422010-05-05 23:33:33 +00005264 if (*mode == 't')
5265 tm = _O_TEXT;
5266 else if (*mode != 'b') {
5267 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
5268 return NULL;
5269 } else
5270 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005271
Victor Stinnerd6f85422010-05-05 23:33:33 +00005272 if (bufsize != -1) {
5273 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
5274 return NULL;
5275 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005276
Victor Stinnerd6f85422010-05-05 23:33:33 +00005277 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00005278
Victor Stinnerd6f85422010-05-05 23:33:33 +00005279 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005280}
5281
5282/*
5283 * Variation on win32pipe.popen
5284 *
Tim Peters5aa91602002-01-30 05:46:57 +00005285 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005286 * and stdout+stderr combined as a single pipe.
5287 */
5288
5289static PyObject *
5290win32_popen4(PyObject *self, PyObject *args)
5291{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005292 PyObject *f;
5293 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005294
Victor Stinnerd6f85422010-05-05 23:33:33 +00005295 char *cmdstring;
5296 char *mode = "t";
5297 int bufsize = -1;
5298 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
5299 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005300
Victor Stinnerd6f85422010-05-05 23:33:33 +00005301 if (*mode == 't')
5302 tm = _O_TEXT;
5303 else if (*mode != 'b') {
5304 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
5305 return NULL;
5306 } else
5307 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005308
Victor Stinnerd6f85422010-05-05 23:33:33 +00005309 if (bufsize != -1) {
5310 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
5311 return NULL;
5312 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005313
Victor Stinnerd6f85422010-05-05 23:33:33 +00005314 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005315
Victor Stinnerd6f85422010-05-05 23:33:33 +00005316 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005317}
5318
Mark Hammond08501372001-01-31 07:30:29 +00005319static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00005320_PyPopenCreateProcess(char *cmdstring,
Victor Stinnerd6f85422010-05-05 23:33:33 +00005321 HANDLE hStdin,
5322 HANDLE hStdout,
5323 HANDLE hStderr,
5324 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005325{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005326 PROCESS_INFORMATION piProcInfo;
5327 STARTUPINFO siStartInfo;
5328 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
5329 char *s1,*s2, *s3 = " /c ";
5330 const char *szConsoleSpawn = "w9xpopen.exe";
5331 int i;
5332 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005333
Victor Stinnerd6f85422010-05-05 23:33:33 +00005334 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
5335 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005336
Victor Stinnerd6f85422010-05-05 23:33:33 +00005337 s1 = (char *)alloca(i);
5338 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
5339 /* x < i, so x fits into an integer */
5340 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00005341
Victor Stinnerd6f85422010-05-05 23:33:33 +00005342 /* Explicitly check if we are using COMMAND.COM. If we are
5343 * then use the w9xpopen hack.
5344 */
5345 comshell = s1 + x;
5346 while (comshell >= s1 && *comshell != '\\')
5347 --comshell;
5348 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005349
Victor Stinnerd6f85422010-05-05 23:33:33 +00005350 if (GetVersion() < 0x80000000 &&
5351 _stricmp(comshell, "command.com") != 0) {
5352 /* NT/2000 and not using command.com. */
5353 x = i + strlen(s3) + strlen(cmdstring) + 1;
5354 s2 = (char *)alloca(x);
5355 ZeroMemory(s2, x);
5356 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5357 }
5358 else {
5359 /*
5360 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5361 * the workaround listed in KB: Q150956
5362 */
5363 char modulepath[_MAX_PATH];
5364 struct stat statinfo;
5365 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5366 for (x = i = 0; modulepath[i]; i++)
5367 if (modulepath[i] == SEP)
5368 x = i+1;
5369 modulepath[x] = '\0';
5370 /* Create the full-name to w9xpopen, so we can test it exists */
5371 strncat(modulepath,
5372 szConsoleSpawn,
5373 (sizeof(modulepath)/sizeof(modulepath[0]))
5374 -strlen(modulepath));
5375 if (stat(modulepath, &statinfo) != 0) {
5376 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5377 /* Eeek - file-not-found - possibly an embedding
5378 situation - see if we can locate it in sys.prefix
5379 */
5380 strncpy(modulepath,
5381 Py_GetExecPrefix(),
5382 mplen);
5383 modulepath[mplen-1] = '\0';
5384 if (modulepath[strlen(modulepath)-1] != '\\')
5385 strcat(modulepath, "\\");
5386 strncat(modulepath,
5387 szConsoleSpawn,
5388 mplen-strlen(modulepath));
5389 /* No where else to look - raise an easily identifiable
5390 error, rather than leaving Windows to report
5391 "file not found" - as the user is probably blissfully
5392 unaware this shim EXE is used, and it will confuse them.
5393 (well, it confused me for a while ;-)
5394 */
5395 if (stat(modulepath, &statinfo) != 0) {
5396 PyErr_Format(PyExc_RuntimeError,
5397 "Can not locate '%s' which is needed "
5398 "for popen to work with your shell "
5399 "or platform.",
5400 szConsoleSpawn);
5401 return FALSE;
5402 }
5403 }
5404 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5405 strlen(modulepath) +
5406 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005407
Victor Stinnerd6f85422010-05-05 23:33:33 +00005408 s2 = (char *)alloca(x);
5409 ZeroMemory(s2, x);
5410 /* To maintain correct argument passing semantics,
5411 we pass the command-line as it stands, and allow
5412 quoting to be applied. w9xpopen.exe will then
5413 use its argv vector, and re-quote the necessary
5414 args for the ultimate child process.
5415 */
5416 PyOS_snprintf(
5417 s2, x,
5418 "\"%s\" %s%s%s",
5419 modulepath,
5420 s1,
5421 s3,
5422 cmdstring);
5423 /* Not passing CREATE_NEW_CONSOLE has been known to
5424 cause random failures on win9x. Specifically a
5425 dialog:
5426 "Your program accessed mem currently in use at xxx"
5427 and a hopeful warning about the stability of your
5428 system.
5429 Cost is Ctrl+C won't kill children, but anyone
5430 who cares can have a go!
5431 */
5432 dwProcessFlags |= CREATE_NEW_CONSOLE;
5433 }
5434 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005435
Victor Stinnerd6f85422010-05-05 23:33:33 +00005436 /* Could be an else here to try cmd.exe / command.com in the path
5437 Now we'll just error out.. */
5438 else {
5439 PyErr_SetString(PyExc_RuntimeError,
5440 "Cannot locate a COMSPEC environment variable to "
5441 "use as the shell");
5442 return FALSE;
5443 }
Tim Peters5aa91602002-01-30 05:46:57 +00005444
Victor Stinnerd6f85422010-05-05 23:33:33 +00005445 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5446 siStartInfo.cb = sizeof(STARTUPINFO);
5447 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5448 siStartInfo.hStdInput = hStdin;
5449 siStartInfo.hStdOutput = hStdout;
5450 siStartInfo.hStdError = hStderr;
5451 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005452
Victor Stinnerd6f85422010-05-05 23:33:33 +00005453 if (CreateProcess(NULL,
5454 s2,
5455 NULL,
5456 NULL,
5457 TRUE,
5458 dwProcessFlags,
5459 NULL,
5460 NULL,
5461 &siStartInfo,
5462 &piProcInfo) ) {
5463 /* Close the handles now so anyone waiting is woken. */
5464 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005465
Victor Stinnerd6f85422010-05-05 23:33:33 +00005466 /* Return process handle */
5467 *hProcess = piProcInfo.hProcess;
5468 return TRUE;
5469 }
5470 win32_error("CreateProcess", s2);
5471 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005472}
5473
5474/* The following code is based off of KB: Q190351 */
5475
5476static PyObject *
5477_PyPopen(char *cmdstring, int mode, int n)
5478{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005479 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5480 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5481 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005482
Victor Stinnerd6f85422010-05-05 23:33:33 +00005483 SECURITY_ATTRIBUTES saAttr;
5484 BOOL fSuccess;
5485 int fd1, fd2, fd3;
5486 FILE *f1, *f2, *f3;
5487 long file_count;
5488 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005489
Victor Stinnerd6f85422010-05-05 23:33:33 +00005490 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5491 saAttr.bInheritHandle = TRUE;
5492 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005493
Victor Stinnerd6f85422010-05-05 23:33:33 +00005494 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5495 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005496
Victor Stinnerd6f85422010-05-05 23:33:33 +00005497 /* Create new output read handle and the input write handle. Set
5498 * the inheritance properties to FALSE. Otherwise, the child inherits
5499 * these handles; resulting in non-closeable handles to the pipes
5500 * being created. */
5501 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5502 GetCurrentProcess(), &hChildStdinWrDup, 0,
5503 FALSE,
5504 DUPLICATE_SAME_ACCESS);
5505 if (!fSuccess)
5506 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005507
Victor Stinnerd6f85422010-05-05 23:33:33 +00005508 /* Close the inheritable version of ChildStdin
5509 that we're using. */
5510 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005511
Victor Stinnerd6f85422010-05-05 23:33:33 +00005512 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5513 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005514
Victor Stinnerd6f85422010-05-05 23:33:33 +00005515 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5516 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5517 FALSE, DUPLICATE_SAME_ACCESS);
5518 if (!fSuccess)
5519 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005520
Victor Stinnerd6f85422010-05-05 23:33:33 +00005521 /* Close the inheritable version of ChildStdout
5522 that we're using. */
5523 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005524
Victor Stinnerd6f85422010-05-05 23:33:33 +00005525 if (n != POPEN_4) {
5526 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5527 return win32_error("CreatePipe", NULL);
5528 fSuccess = DuplicateHandle(GetCurrentProcess(),
5529 hChildStderrRd,
5530 GetCurrentProcess(),
5531 &hChildStderrRdDup, 0,
5532 FALSE, DUPLICATE_SAME_ACCESS);
5533 if (!fSuccess)
5534 return win32_error("DuplicateHandle", NULL);
5535 /* Close the inheritable version of ChildStdErr that we're using. */
5536 CloseHandle(hChildStderrRd);
5537 }
Tim Peters5aa91602002-01-30 05:46:57 +00005538
Victor Stinnerd6f85422010-05-05 23:33:33 +00005539 switch (n) {
5540 case POPEN_1:
5541 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5542 case _O_WRONLY | _O_TEXT:
5543 /* Case for writing to child Stdin in text mode. */
5544 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5545 f1 = _fdopen(fd1, "w");
5546 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5547 PyFile_SetBufSize(f, 0);
5548 /* We don't care about these pipes anymore, so close them. */
5549 CloseHandle(hChildStdoutRdDup);
5550 CloseHandle(hChildStderrRdDup);
5551 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005552
Victor Stinnerd6f85422010-05-05 23:33:33 +00005553 case _O_RDONLY | _O_TEXT:
5554 /* Case for reading from child Stdout in text mode. */
5555 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5556 f1 = _fdopen(fd1, "r");
5557 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5558 PyFile_SetBufSize(f, 0);
5559 /* We don't care about these pipes anymore, so close them. */
5560 CloseHandle(hChildStdinWrDup);
5561 CloseHandle(hChildStderrRdDup);
5562 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005563
Victor Stinnerd6f85422010-05-05 23:33:33 +00005564 case _O_RDONLY | _O_BINARY:
5565 /* Case for readinig from child Stdout in binary mode. */
5566 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5567 f1 = _fdopen(fd1, "rb");
5568 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5569 PyFile_SetBufSize(f, 0);
5570 /* We don't care about these pipes anymore, so close them. */
5571 CloseHandle(hChildStdinWrDup);
5572 CloseHandle(hChildStderrRdDup);
5573 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005574
Victor Stinnerd6f85422010-05-05 23:33:33 +00005575 case _O_WRONLY | _O_BINARY:
5576 /* Case for writing to child Stdin in binary mode. */
5577 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5578 f1 = _fdopen(fd1, "wb");
5579 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5580 PyFile_SetBufSize(f, 0);
5581 /* We don't care about these pipes anymore, so close them. */
5582 CloseHandle(hChildStdoutRdDup);
5583 CloseHandle(hChildStderrRdDup);
5584 break;
5585 }
5586 file_count = 1;
5587 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005588
Victor Stinnerd6f85422010-05-05 23:33:33 +00005589 case POPEN_2:
5590 case POPEN_4:
5591 {
5592 char *m1, *m2;
5593 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005594
Victor Stinnerd6f85422010-05-05 23:33:33 +00005595 if (mode & _O_TEXT) {
5596 m1 = "r";
5597 m2 = "w";
5598 } else {
5599 m1 = "rb";
5600 m2 = "wb";
5601 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005602
Victor Stinnerd6f85422010-05-05 23:33:33 +00005603 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5604 f1 = _fdopen(fd1, m2);
5605 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5606 f2 = _fdopen(fd2, m1);
5607 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5608 PyFile_SetBufSize(p1, 0);
5609 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5610 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005611
Victor Stinnerd6f85422010-05-05 23:33:33 +00005612 if (n != 4)
5613 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005614
Victor Stinnerd6f85422010-05-05 23:33:33 +00005615 f = PyTuple_Pack(2,p1,p2);
5616 Py_XDECREF(p1);
5617 Py_XDECREF(p2);
5618 file_count = 2;
5619 break;
5620 }
Tim Peters5aa91602002-01-30 05:46:57 +00005621
Victor Stinnerd6f85422010-05-05 23:33:33 +00005622 case POPEN_3:
5623 {
5624 char *m1, *m2;
5625 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005626
Victor Stinnerd6f85422010-05-05 23:33:33 +00005627 if (mode & _O_TEXT) {
5628 m1 = "r";
5629 m2 = "w";
5630 } else {
5631 m1 = "rb";
5632 m2 = "wb";
5633 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005634
Victor Stinnerd6f85422010-05-05 23:33:33 +00005635 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5636 f1 = _fdopen(fd1, m2);
5637 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5638 f2 = _fdopen(fd2, m1);
5639 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5640 f3 = _fdopen(fd3, m1);
5641 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5642 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5643 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5644 PyFile_SetBufSize(p1, 0);
5645 PyFile_SetBufSize(p2, 0);
5646 PyFile_SetBufSize(p3, 0);
5647 f = PyTuple_Pack(3,p1,p2,p3);
5648 Py_XDECREF(p1);
5649 Py_XDECREF(p2);
5650 Py_XDECREF(p3);
5651 file_count = 3;
5652 break;
5653 }
5654 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005655
Victor Stinnerd6f85422010-05-05 23:33:33 +00005656 if (n == POPEN_4) {
5657 if (!_PyPopenCreateProcess(cmdstring,
5658 hChildStdinRd,
5659 hChildStdoutWr,
5660 hChildStdoutWr,
5661 &hProcess))
5662 return NULL;
5663 }
5664 else {
5665 if (!_PyPopenCreateProcess(cmdstring,
5666 hChildStdinRd,
5667 hChildStdoutWr,
5668 hChildStderrWr,
5669 &hProcess))
5670 return NULL;
5671 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005672
Victor Stinnerd6f85422010-05-05 23:33:33 +00005673 /*
5674 * Insert the files we've created into the process dictionary
5675 * all referencing the list with the process handle and the
5676 * initial number of files (see description below in _PyPclose).
5677 * Since if _PyPclose later tried to wait on a process when all
5678 * handles weren't closed, it could create a deadlock with the
5679 * child, we spend some energy here to try to ensure that we
5680 * either insert all file handles into the dictionary or none
5681 * at all. It's a little clumsy with the various popen modes
5682 * and variable number of files involved.
5683 */
5684 if (!_PyPopenProcs) {
5685 _PyPopenProcs = PyDict_New();
5686 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005687
Victor Stinnerd6f85422010-05-05 23:33:33 +00005688 if (_PyPopenProcs) {
5689 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5690 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005691
Victor Stinnerd6f85422010-05-05 23:33:33 +00005692 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5693 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005694
Victor Stinnerd6f85422010-05-05 23:33:33 +00005695 procObj = PyList_New(2);
5696 hProcessObj = PyLong_FromVoidPtr(hProcess);
5697 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005698
Victor Stinnerd6f85422010-05-05 23:33:33 +00005699 if (procObj && hProcessObj && intObj) {
5700 PyList_SetItem(procObj,0,hProcessObj);
5701 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005702
Victor Stinnerd6f85422010-05-05 23:33:33 +00005703 fileObj[0] = PyLong_FromVoidPtr(f1);
5704 if (fileObj[0]) {
5705 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5706 fileObj[0],
5707 procObj);
5708 }
5709 if (file_count >= 2) {
5710 fileObj[1] = PyLong_FromVoidPtr(f2);
5711 if (fileObj[1]) {
5712 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5713 fileObj[1],
5714 procObj);
5715 }
5716 }
5717 if (file_count >= 3) {
5718 fileObj[2] = PyLong_FromVoidPtr(f3);
5719 if (fileObj[2]) {
5720 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5721 fileObj[2],
5722 procObj);
5723 }
5724 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005725
Victor Stinnerd6f85422010-05-05 23:33:33 +00005726 if (ins_rc[0] < 0 || !fileObj[0] ||
5727 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5728 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5729 /* Something failed - remove any dictionary
5730 * entries that did make it.
5731 */
5732 if (!ins_rc[0] && fileObj[0]) {
5733 PyDict_DelItem(_PyPopenProcs,
5734 fileObj[0]);
5735 }
5736 if (!ins_rc[1] && fileObj[1]) {
5737 PyDict_DelItem(_PyPopenProcs,
5738 fileObj[1]);
5739 }
5740 if (!ins_rc[2] && fileObj[2]) {
5741 PyDict_DelItem(_PyPopenProcs,
5742 fileObj[2]);
5743 }
5744 }
5745 }
Tim Peters5aa91602002-01-30 05:46:57 +00005746
Victor Stinnerd6f85422010-05-05 23:33:33 +00005747 /*
5748 * Clean up our localized references for the dictionary keys
5749 * and value since PyDict_SetItem will Py_INCREF any copies
5750 * that got placed in the dictionary.
5751 */
5752 Py_XDECREF(procObj);
5753 Py_XDECREF(fileObj[0]);
5754 Py_XDECREF(fileObj[1]);
5755 Py_XDECREF(fileObj[2]);
5756 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005757
Victor Stinnerd6f85422010-05-05 23:33:33 +00005758 /* Child is launched. Close the parents copy of those pipe
5759 * handles that only the child should have open. You need to
5760 * make sure that no handles to the write end of the output pipe
5761 * are maintained in this process or else the pipe will not close
5762 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005763
Victor Stinnerd6f85422010-05-05 23:33:33 +00005764 if (!CloseHandle(hChildStdinRd))
5765 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005766
Victor Stinnerd6f85422010-05-05 23:33:33 +00005767 if (!CloseHandle(hChildStdoutWr))
5768 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005769
Victor Stinnerd6f85422010-05-05 23:33:33 +00005770 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5771 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005772
Victor Stinnerd6f85422010-05-05 23:33:33 +00005773 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005774}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005775
5776/*
5777 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5778 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005779 *
5780 * This function uses the _PyPopenProcs dictionary in order to map the
5781 * input file pointer to information about the process that was
5782 * originally created by the popen* call that created the file pointer.
5783 * The dictionary uses the file pointer as a key (with one entry
5784 * inserted for each file returned by the original popen* call) and a
5785 * single list object as the value for all files from a single call.
5786 * The list object contains the Win32 process handle at [0], and a file
5787 * count at [1], which is initialized to the total number of file
5788 * handles using that list.
5789 *
5790 * This function closes whichever handle it is passed, and decrements
5791 * the file count in the dictionary for the process handle pointed to
5792 * by this file. On the last close (when the file count reaches zero),
5793 * this function will wait for the child process and then return its
5794 * exit code as the result of the close() operation. This permits the
5795 * files to be closed in any order - it is always the close() of the
5796 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005797 *
5798 * NOTE: This function is currently called with the GIL released.
5799 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005800 */
Tim Peters736aa322000-09-01 06:51:24 +00005801
Fredrik Lundh56055a42000-07-23 19:47:12 +00005802static int _PyPclose(FILE *file)
5803{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005804 int result;
5805 DWORD exit_code;
5806 HANDLE hProcess;
5807 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5808 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005809#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005810 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005811#endif
5812
Victor Stinnerd6f85422010-05-05 23:33:33 +00005813 /* Close the file handle first, to ensure it can't block the
5814 * child from exiting if it's the last handle.
5815 */
5816 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005817#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005818 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005819#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005820 if (_PyPopenProcs) {
5821 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5822 (procObj = PyDict_GetItem(_PyPopenProcs,
5823 fileObj)) != NULL &&
5824 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5825 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005826
Victor Stinnerd6f85422010-05-05 23:33:33 +00005827 hProcess = PyLong_AsVoidPtr(hProcessObj);
5828 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005829
Victor Stinnerd6f85422010-05-05 23:33:33 +00005830 if (file_count > 1) {
5831 /* Still other files referencing process */
5832 file_count--;
5833 PyList_SetItem(procObj,1,
5834 PyInt_FromLong(file_count));
5835 } else {
5836 /* Last file for this process */
5837 if (result != EOF &&
5838 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5839 GetExitCodeProcess(hProcess, &exit_code)) {
5840 /* Possible truncation here in 16-bit environments, but
5841 * real exit codes are just the lower byte in any event.
5842 */
5843 result = exit_code;
5844 } else {
5845 /* Indicate failure - this will cause the file object
5846 * to raise an I/O error and translate the last Win32
5847 * error code from errno. We do have a problem with
5848 * last errors that overlap the normal errno table,
5849 * but that's a consistent problem with the file object.
5850 */
5851 if (result != EOF) {
5852 /* If the error wasn't from the fclose(), then
5853 * set errno for the file object error handling.
5854 */
5855 errno = GetLastError();
5856 }
5857 result = -1;
5858 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005859
Victor Stinnerd6f85422010-05-05 23:33:33 +00005860 /* Free up the native handle at this point */
5861 CloseHandle(hProcess);
5862 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005863
Victor Stinnerd6f85422010-05-05 23:33:33 +00005864 /* Remove this file pointer from dictionary */
5865 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005866
Victor Stinnerd6f85422010-05-05 23:33:33 +00005867 if (PyDict_Size(_PyPopenProcs) == 0) {
5868 Py_DECREF(_PyPopenProcs);
5869 _PyPopenProcs = NULL;
5870 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005871
Victor Stinnerd6f85422010-05-05 23:33:33 +00005872 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005873
Victor Stinnerd6f85422010-05-05 23:33:33 +00005874 Py_XDECREF(fileObj);
5875 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005876
Tim Peters736aa322000-09-01 06:51:24 +00005877#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005878 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005879#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005880 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005881}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005882
5883#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005885posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005886{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005887 char *name;
5888 char *mode = "r";
5889 int bufsize = -1;
5890 FILE *fp;
5891 PyObject *f;
5892 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5893 return NULL;
5894 /* Strip mode of binary or text modifiers */
5895 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5896 mode = "r";
5897 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5898 mode = "w";
5899 Py_BEGIN_ALLOW_THREADS
5900 fp = popen(name, mode);
5901 Py_END_ALLOW_THREADS
5902 if (fp == NULL)
5903 return posix_error();
5904 f = PyFile_FromFile(fp, name, mode, pclose);
5905 if (f != NULL)
5906 PyFile_SetBufSize(f, bufsize);
5907 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005908}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005909
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005910#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005911#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005912
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005913
Guido van Rossumb6775db1994-08-01 11:34:53 +00005914#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005915PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005916"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005917Set the current process's user id.");
5918
Barry Warsaw53699e91996-12-10 23:23:01 +00005919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005920posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005921{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005922 uid_t uid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005923 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005924 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005925 if (setuid(uid) < 0)
5926 return posix_error();
5927 Py_INCREF(Py_None);
5928 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005929}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005930#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005931
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005932
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005933#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005934PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005935"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005936Set the current process's effective user id.");
5937
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005938static PyObject *
5939posix_seteuid (PyObject *self, PyObject *args)
5940{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005941 uid_t euid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005942 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005943 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005944 if (seteuid(euid) < 0) {
5945 return posix_error();
5946 } else {
5947 Py_INCREF(Py_None);
5948 return Py_None;
5949 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005950}
5951#endif /* HAVE_SETEUID */
5952
5953#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005954PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005955"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005956Set the current process's effective group id.");
5957
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005958static PyObject *
5959posix_setegid (PyObject *self, PyObject *args)
5960{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005961 gid_t egid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005962 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005963 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005964 if (setegid(egid) < 0) {
5965 return posix_error();
5966 } else {
5967 Py_INCREF(Py_None);
5968 return Py_None;
5969 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005970}
5971#endif /* HAVE_SETEGID */
5972
5973#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005974PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005975"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005976Set the current process's real and effective user ids.");
5977
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005978static PyObject *
5979posix_setreuid (PyObject *self, PyObject *args)
5980{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005981 uid_t ruid, euid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005982 if (!PyArg_ParseTuple(args, "O&O&:setreuid",
5983 _Py_Uid_Converter, &ruid,
5984 _Py_Uid_Converter, &euid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005985 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005986 if (setreuid(ruid, euid) < 0) {
5987 return posix_error();
5988 } else {
5989 Py_INCREF(Py_None);
5990 return Py_None;
5991 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005992}
5993#endif /* HAVE_SETREUID */
5994
5995#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005996PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005997"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998Set the current process's real and effective group ids.");
5999
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006000static PyObject *
6001posix_setregid (PyObject *self, PyObject *args)
6002{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006003 gid_t rgid, egid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006004 if (!PyArg_ParseTuple(args, "O&O&:setregid",
6005 _Py_Gid_Converter, &rgid,
6006 _Py_Gid_Converter, &egid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00006007 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006008 if (setregid(rgid, egid) < 0) {
6009 return posix_error();
6010 } else {
6011 Py_INCREF(Py_None);
6012 return Py_None;
6013 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006014}
6015#endif /* HAVE_SETREGID */
6016
Guido van Rossumb6775db1994-08-01 11:34:53 +00006017#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006018PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006019"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006020Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006021
Barry Warsaw53699e91996-12-10 23:23:01 +00006022static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006023posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006024{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006025 gid_t gid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006026 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00006027 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006028 if (setgid(gid) < 0)
6029 return posix_error();
6030 Py_INCREF(Py_None);
6031 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006032}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006033#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006034
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006035#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006036PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006037"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006038Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006039
6040static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00006041posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006042{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006043 int i, len;
6044 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006045
Victor Stinnerd6f85422010-05-05 23:33:33 +00006046 if (!PySequence_Check(groups)) {
6047 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6048 return NULL;
6049 }
6050 len = PySequence_Size(groups);
6051 if (len > MAX_GROUPS) {
6052 PyErr_SetString(PyExc_ValueError, "too many groups");
6053 return NULL;
6054 }
6055 for(i = 0; i < len; i++) {
6056 PyObject *elem;
6057 elem = PySequence_GetItem(groups, i);
6058 if (!elem)
6059 return NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006060 if (!PyInt_Check(elem) && !PyLong_Check(elem)) {
6061 PyErr_SetString(PyExc_TypeError,
6062 "groups must be integers");
6063 Py_DECREF(elem);
6064 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006065 } else {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006066 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00006067 Py_DECREF(elem);
6068 return NULL;
6069 }
6070 }
6071 Py_DECREF(elem);
6072 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006073
Victor Stinnerd6f85422010-05-05 23:33:33 +00006074 if (setgroups(len, grouplist) < 0)
6075 return posix_error();
6076 Py_INCREF(Py_None);
6077 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006078}
6079#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006080
Neal Norwitz6c2f9132006-03-20 07:25:26 +00006081#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00006082static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00006083wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00006084{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006085 PyObject *result;
6086 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00006087
Victor Stinnerd6f85422010-05-05 23:33:33 +00006088 if (pid == -1)
6089 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00006090
Victor Stinnerd6f85422010-05-05 23:33:33 +00006091 if (struct_rusage == NULL) {
6092 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6093 if (m == NULL)
6094 return NULL;
6095 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6096 Py_DECREF(m);
6097 if (struct_rusage == NULL)
6098 return NULL;
6099 }
Neal Norwitz05a45592006-03-20 06:30:08 +00006100
Victor Stinnerd6f85422010-05-05 23:33:33 +00006101 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6102 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6103 if (!result)
6104 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006105
6106#ifndef doubletime
6107#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6108#endif
6109
Victor Stinnerd6f85422010-05-05 23:33:33 +00006110 PyStructSequence_SET_ITEM(result, 0,
6111 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6112 PyStructSequence_SET_ITEM(result, 1,
6113 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00006114#define SET_INT(result, index, value)\
Victor Stinnerd6f85422010-05-05 23:33:33 +00006115 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
6116 SET_INT(result, 2, ru->ru_maxrss);
6117 SET_INT(result, 3, ru->ru_ixrss);
6118 SET_INT(result, 4, ru->ru_idrss);
6119 SET_INT(result, 5, ru->ru_isrss);
6120 SET_INT(result, 6, ru->ru_minflt);
6121 SET_INT(result, 7, ru->ru_majflt);
6122 SET_INT(result, 8, ru->ru_nswap);
6123 SET_INT(result, 9, ru->ru_inblock);
6124 SET_INT(result, 10, ru->ru_oublock);
6125 SET_INT(result, 11, ru->ru_msgsnd);
6126 SET_INT(result, 12, ru->ru_msgrcv);
6127 SET_INT(result, 13, ru->ru_nsignals);
6128 SET_INT(result, 14, ru->ru_nvcsw);
6129 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00006130#undef SET_INT
6131
Victor Stinnerd6f85422010-05-05 23:33:33 +00006132 if (PyErr_Occurred()) {
6133 Py_DECREF(result);
6134 return NULL;
6135 }
Neal Norwitz05a45592006-03-20 06:30:08 +00006136
Victor Stinnerd6f85422010-05-05 23:33:33 +00006137 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00006138}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00006139#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00006140
6141#ifdef HAVE_WAIT3
6142PyDoc_STRVAR(posix_wait3__doc__,
6143"wait3(options) -> (pid, status, rusage)\n\n\
6144Wait for completion of a child process.");
6145
6146static PyObject *
6147posix_wait3(PyObject *self, PyObject *args)
6148{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006149 pid_t pid;
6150 int options;
6151 struct rusage ru;
6152 WAIT_TYPE status;
6153 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006154
Victor Stinnerd6f85422010-05-05 23:33:33 +00006155 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6156 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006157
Victor Stinnerd6f85422010-05-05 23:33:33 +00006158 Py_BEGIN_ALLOW_THREADS
6159 pid = wait3(&status, options, &ru);
6160 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006161
Victor Stinnerd6f85422010-05-05 23:33:33 +00006162 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006163}
6164#endif /* HAVE_WAIT3 */
6165
6166#ifdef HAVE_WAIT4
6167PyDoc_STRVAR(posix_wait4__doc__,
6168"wait4(pid, options) -> (pid, status, rusage)\n\n\
6169Wait for completion of a given child process.");
6170
6171static PyObject *
6172posix_wait4(PyObject *self, PyObject *args)
6173{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006174 pid_t pid;
6175 int options;
6176 struct rusage ru;
6177 WAIT_TYPE status;
6178 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006179
Victor Stinnerd6f85422010-05-05 23:33:33 +00006180 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
6181 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006182
Victor Stinnerd6f85422010-05-05 23:33:33 +00006183 Py_BEGIN_ALLOW_THREADS
6184 pid = wait4(pid, &status, options, &ru);
6185 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006186
Victor Stinnerd6f85422010-05-05 23:33:33 +00006187 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006188}
6189#endif /* HAVE_WAIT4 */
6190
Guido van Rossumb6775db1994-08-01 11:34:53 +00006191#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006192PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006193"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006194Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006195
Barry Warsaw53699e91996-12-10 23:23:01 +00006196static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006197posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006198{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006199 pid_t pid;
6200 int options;
6201 WAIT_TYPE status;
6202 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006203
Victor Stinnerd6f85422010-05-05 23:33:33 +00006204 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6205 return NULL;
6206 Py_BEGIN_ALLOW_THREADS
6207 pid = waitpid(pid, &status, options);
6208 Py_END_ALLOW_THREADS
6209 if (pid == -1)
6210 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006211
Victor Stinnerd6f85422010-05-05 23:33:33 +00006212 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006213}
6214
Tim Petersab034fa2002-02-01 11:27:43 +00006215#elif defined(HAVE_CWAIT)
6216
6217/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006218PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006219"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006220"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006221
6222static PyObject *
6223posix_waitpid(PyObject *self, PyObject *args)
6224{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006225 Py_intptr_t pid;
6226 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006227
Victor Stinnerd6f85422010-05-05 23:33:33 +00006228 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6229 return NULL;
6230 Py_BEGIN_ALLOW_THREADS
6231 pid = _cwait(&status, pid, options);
6232 Py_END_ALLOW_THREADS
6233 if (pid == -1)
6234 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006235
Victor Stinnerd6f85422010-05-05 23:33:33 +00006236 /* shift the status left a byte so this is more like the POSIX waitpid */
6237 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006238}
6239#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006240
Guido van Rossumad0ee831995-03-01 10:34:45 +00006241#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006242PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006243"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006244Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006245
Barry Warsaw53699e91996-12-10 23:23:01 +00006246static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006247posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006248{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006249 pid_t pid;
6250 WAIT_TYPE status;
6251 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006252
Victor Stinnerd6f85422010-05-05 23:33:33 +00006253 Py_BEGIN_ALLOW_THREADS
6254 pid = wait(&status);
6255 Py_END_ALLOW_THREADS
6256 if (pid == -1)
6257 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006258
Victor Stinnerd6f85422010-05-05 23:33:33 +00006259 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006260}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006261#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006262
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006264PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006265"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006266Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006267
Barry Warsaw53699e91996-12-10 23:23:01 +00006268static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006269posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006270{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006271#ifdef HAVE_LSTAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00006272 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006273#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006274#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006275 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006276#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006277 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006278#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006279#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006280}
6281
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006282
Guido van Rossumb6775db1994-08-01 11:34:53 +00006283#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006284PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006285"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006286Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006287
Barry Warsaw53699e91996-12-10 23:23:01 +00006288static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006289posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006290{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006291 PyObject* v;
6292 char buf[MAXPATHLEN];
6293 char *path;
6294 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006295#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006296 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006297#endif
6298
Victor Stinnerd6f85422010-05-05 23:33:33 +00006299 if (!PyArg_ParseTuple(args, "et:readlink",
6300 Py_FileSystemDefaultEncoding, &path))
6301 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006302#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006303 v = PySequence_GetItem(args, 0);
6304 if (v == NULL) {
6305 PyMem_Free(path);
6306 return NULL;
6307 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006308
Victor Stinnerd6f85422010-05-05 23:33:33 +00006309 if (PyUnicode_Check(v)) {
6310 arg_is_unicode = 1;
6311 }
6312 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006313#endif
6314
Victor Stinnerd6f85422010-05-05 23:33:33 +00006315 Py_BEGIN_ALLOW_THREADS
6316 n = readlink(path, buf, (int) sizeof buf);
6317 Py_END_ALLOW_THREADS
6318 if (n < 0)
6319 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006320
Victor Stinnerd6f85422010-05-05 23:33:33 +00006321 PyMem_Free(path);
6322 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006323#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006324 if (arg_is_unicode) {
6325 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006326
Victor Stinnerd6f85422010-05-05 23:33:33 +00006327 w = PyUnicode_FromEncodedObject(v,
6328 Py_FileSystemDefaultEncoding,
6329 "strict");
6330 if (w != NULL) {
6331 Py_DECREF(v);
6332 v = w;
6333 }
6334 else {
6335 /* fall back to the original byte string, as
6336 discussed in patch #683592 */
6337 PyErr_Clear();
6338 }
6339 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006340#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006341 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006342}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006343#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006345
Guido van Rossumb6775db1994-08-01 11:34:53 +00006346#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006347PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006348"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006349Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006350
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006352posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006353{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006354 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006355}
6356#endif /* HAVE_SYMLINK */
6357
6358
6359#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006360#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6361static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006362system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006363{
6364 ULONG value = 0;
6365
6366 Py_BEGIN_ALLOW_THREADS
6367 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6368 Py_END_ALLOW_THREADS
6369
6370 return value;
6371}
6372
6373static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006374posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006375{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006376 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006377 return Py_BuildValue("ddddd",
6378 (double)0 /* t.tms_utime / HZ */,
6379 (double)0 /* t.tms_stime / HZ */,
6380 (double)0 /* t.tms_cutime / HZ */,
6381 (double)0 /* t.tms_cstime / HZ */,
6382 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006383}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006384#else /* not OS2 */
Martin v. Löwis03824e42008-12-29 18:17:34 +00006385#define NEED_TICKS_PER_SECOND
6386static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006387static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006388posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006389{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006390 struct tms t;
6391 clock_t c;
6392 errno = 0;
6393 c = times(&t);
6394 if (c == (clock_t) -1)
6395 return posix_error();
6396 return Py_BuildValue("ddddd",
6397 (double)t.tms_utime / ticks_per_second,
6398 (double)t.tms_stime / ticks_per_second,
6399 (double)t.tms_cutime / ticks_per_second,
6400 (double)t.tms_cstime / ticks_per_second,
6401 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006402}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006403#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006404#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006405
6406
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006407#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006408#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006409static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006410posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006411{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006412 FILETIME create, exit, kernel, user;
6413 HANDLE hProc;
6414 hProc = GetCurrentProcess();
6415 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6416 /* The fields of a FILETIME structure are the hi and lo part
6417 of a 64-bit value expressed in 100 nanosecond units.
6418 1e7 is one second in such units; 1e-7 the inverse.
6419 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6420 */
6421 return Py_BuildValue(
6422 "ddddd",
6423 (double)(user.dwHighDateTime*429.4967296 +
6424 user.dwLowDateTime*1e-7),
6425 (double)(kernel.dwHighDateTime*429.4967296 +
6426 kernel.dwLowDateTime*1e-7),
6427 (double)0,
6428 (double)0,
6429 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006430}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006431#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006432
6433#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006435"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006436Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006437#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006439
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006440#ifdef HAVE_GETSID
6441PyDoc_STRVAR(posix_getsid__doc__,
6442"getsid(pid) -> sid\n\n\
6443Call the system call getsid().");
6444
6445static PyObject *
6446posix_getsid(PyObject *self, PyObject *args)
6447{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006448 pid_t pid;
6449 int sid;
6450 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6451 return NULL;
6452 sid = getsid(pid);
6453 if (sid < 0)
6454 return posix_error();
6455 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006456}
6457#endif /* HAVE_GETSID */
6458
6459
Guido van Rossumb6775db1994-08-01 11:34:53 +00006460#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006461PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006462"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006463Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006464
Barry Warsaw53699e91996-12-10 23:23:01 +00006465static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006466posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006467{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006468 if (setsid() < 0)
6469 return posix_error();
6470 Py_INCREF(Py_None);
6471 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006472}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006473#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006474
Guido van Rossumb6775db1994-08-01 11:34:53 +00006475#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006476PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006477"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006478Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006479
Barry Warsaw53699e91996-12-10 23:23:01 +00006480static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006481posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006482{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006483 pid_t pid;
6484 int pgrp;
6485 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6486 return NULL;
6487 if (setpgid(pid, pgrp) < 0)
6488 return posix_error();
6489 Py_INCREF(Py_None);
6490 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006491}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006492#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006493
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006494
Guido van Rossumb6775db1994-08-01 11:34:53 +00006495#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006496PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006497"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006498Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006499
Barry Warsaw53699e91996-12-10 23:23:01 +00006500static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006501posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006502{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006503 int fd;
6504 pid_t pgid;
6505 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6506 return NULL;
6507 pgid = tcgetpgrp(fd);
6508 if (pgid < 0)
6509 return posix_error();
6510 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006511}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006512#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006514
Guido van Rossumb6775db1994-08-01 11:34:53 +00006515#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006516PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006517"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006518Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006519
Barry Warsaw53699e91996-12-10 23:23:01 +00006520static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006521posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006522{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006523 int fd;
6524 pid_t pgid;
6525 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6526 return NULL;
6527 if (tcsetpgrp(fd, pgid) < 0)
6528 return posix_error();
6529 Py_INCREF(Py_None);
6530 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006531}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006532#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006533
Guido van Rossum687dd131993-05-17 08:34:16 +00006534/* Functions acting on file descriptors */
6535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006536PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006537"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006538Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006539
Barry Warsaw53699e91996-12-10 23:23:01 +00006540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006541posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006542{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006543 char *file = NULL;
6544 int flag;
6545 int mode = 0777;
6546 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006547
6548#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006549 PyUnicodeObject *po;
6550 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6551 Py_BEGIN_ALLOW_THREADS
6552 /* PyUnicode_AS_UNICODE OK without thread
6553 lock as it is a simple dereference. */
6554 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6555 Py_END_ALLOW_THREADS
6556 if (fd < 0)
6557 return posix_error();
6558 return PyInt_FromLong((long)fd);
6559 }
6560 /* Drop the argument parsing error as narrow strings
6561 are also valid. */
6562 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006563#endif
6564
Victor Stinnerd6f85422010-05-05 23:33:33 +00006565 if (!PyArg_ParseTuple(args, "eti|i",
6566 Py_FileSystemDefaultEncoding, &file,
6567 &flag, &mode))
6568 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006569
Victor Stinnerd6f85422010-05-05 23:33:33 +00006570 Py_BEGIN_ALLOW_THREADS
6571 fd = open(file, flag, mode);
6572 Py_END_ALLOW_THREADS
6573 if (fd < 0)
6574 return posix_error_with_allocated_filename(file);
6575 PyMem_Free(file);
6576 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006577}
6578
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006580PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006581"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006582Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006583
Benjamin Peterson2748c5c2014-02-11 10:16:16 -05006584/*
6585The underscore at end of function name avoids a name clash with the libc
6586function posix_close.
6587*/
Barry Warsaw53699e91996-12-10 23:23:01 +00006588static PyObject *
Benjamin Peterson2748c5c2014-02-11 10:16:16 -05006589posix_close_(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006590{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006591 int fd, res;
6592 if (!PyArg_ParseTuple(args, "i:close", &fd))
6593 return NULL;
6594 if (!_PyVerify_fd(fd))
6595 return posix_error();
6596 Py_BEGIN_ALLOW_THREADS
6597 res = close(fd);
6598 Py_END_ALLOW_THREADS
6599 if (res < 0)
6600 return posix_error();
6601 Py_INCREF(Py_None);
6602 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006603}
6604
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006605
Victor Stinnerd6f85422010-05-05 23:33:33 +00006606PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006607"closerange(fd_low, fd_high)\n\n\
6608Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6609
6610static PyObject *
6611posix_closerange(PyObject *self, PyObject *args)
6612{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006613 int fd_from, fd_to, i;
6614 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6615 return NULL;
6616 Py_BEGIN_ALLOW_THREADS
6617 for (i = fd_from; i < fd_to; i++)
6618 if (_PyVerify_fd(i))
6619 close(i);
6620 Py_END_ALLOW_THREADS
6621 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006622}
6623
6624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006625PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006626"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006627Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006628
Barry Warsaw53699e91996-12-10 23:23:01 +00006629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006630posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006631{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006632 int fd;
6633 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6634 return NULL;
6635 if (!_PyVerify_fd(fd))
6636 return posix_error();
6637 Py_BEGIN_ALLOW_THREADS
6638 fd = dup(fd);
6639 Py_END_ALLOW_THREADS
6640 if (fd < 0)
6641 return posix_error();
6642 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006643}
6644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006646PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006647"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006648Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006649
Barry Warsaw53699e91996-12-10 23:23:01 +00006650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006651posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006652{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006653 int fd, fd2, res;
6654 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6655 return NULL;
6656 if (!_PyVerify_fd_dup2(fd, fd2))
6657 return posix_error();
6658 Py_BEGIN_ALLOW_THREADS
6659 res = dup2(fd, fd2);
6660 Py_END_ALLOW_THREADS
6661 if (res < 0)
6662 return posix_error();
6663 Py_INCREF(Py_None);
6664 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006665}
6666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006668PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006669"lseek(fd, pos, how) -> newpos\n\n\
Georg Brandl6d076412014-03-11 10:28:56 +01006670Set the current position of a file descriptor.\n\
6671Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006672
Barry Warsaw53699e91996-12-10 23:23:01 +00006673static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006674posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006675{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006676 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006677#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006678 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006679#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006680 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006681#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006682 PyObject *posobj;
6683 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6684 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006685#ifdef SEEK_SET
Victor Stinnerd6f85422010-05-05 23:33:33 +00006686 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6687 switch (how) {
6688 case 0: how = SEEK_SET; break;
6689 case 1: how = SEEK_CUR; break;
6690 case 2: how = SEEK_END; break;
6691 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006692#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006693
6694#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006695 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006696#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006697 pos = PyLong_Check(posobj) ?
6698 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006699#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006700 if (PyErr_Occurred())
6701 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006702
Victor Stinnerd6f85422010-05-05 23:33:33 +00006703 if (!_PyVerify_fd(fd))
6704 return posix_error();
6705 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006706#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006707 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006708#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006709 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006710#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006711 Py_END_ALLOW_THREADS
6712 if (res < 0)
6713 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006714
6715#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006716 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006717#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006718 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006719#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006720}
6721
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006723PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006724"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006725Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006726
Barry Warsaw53699e91996-12-10 23:23:01 +00006727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006728posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006729{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006730 int fd, size, n;
6731 PyObject *buffer;
6732 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6733 return NULL;
6734 if (size < 0) {
6735 errno = EINVAL;
6736 return posix_error();
6737 }
6738 buffer = PyString_FromStringAndSize((char *)NULL, size);
6739 if (buffer == NULL)
6740 return NULL;
Stefan Krahacaab2a2010-11-26 15:06:27 +00006741 if (!_PyVerify_fd(fd)) {
6742 Py_DECREF(buffer);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006743 return posix_error();
Stefan Krahacaab2a2010-11-26 15:06:27 +00006744 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00006745 Py_BEGIN_ALLOW_THREADS
6746 n = read(fd, PyString_AsString(buffer), size);
6747 Py_END_ALLOW_THREADS
6748 if (n < 0) {
6749 Py_DECREF(buffer);
6750 return posix_error();
6751 }
6752 if (n != size)
6753 _PyString_Resize(&buffer, n);
6754 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006755}
6756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006758PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006759"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006760Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006761
Barry Warsaw53699e91996-12-10 23:23:01 +00006762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006763posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006764{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006765 Py_buffer pbuf;
6766 int fd;
Victor Stinner59729ff2011-07-05 11:28:19 +02006767 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006768
Victor Stinnerd6f85422010-05-05 23:33:33 +00006769 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6770 return NULL;
Stefan Krahacaab2a2010-11-26 15:06:27 +00006771 if (!_PyVerify_fd(fd)) {
6772 PyBuffer_Release(&pbuf);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006773 return posix_error();
Stefan Krahacaab2a2010-11-26 15:06:27 +00006774 }
Victor Stinner59729ff2011-07-05 11:28:19 +02006775 len = pbuf.len;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006776 Py_BEGIN_ALLOW_THREADS
Victor Stinner59729ff2011-07-05 11:28:19 +02006777#if defined(MS_WIN64) || defined(MS_WINDOWS)
6778 if (len > INT_MAX)
6779 len = INT_MAX;
6780 size = write(fd, pbuf.buf, (int)len);
6781#else
6782 size = write(fd, pbuf.buf, len);
6783#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006784 Py_END_ALLOW_THREADS
Stefan Krahacaab2a2010-11-26 15:06:27 +00006785 PyBuffer_Release(&pbuf);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006786 if (size < 0)
6787 return posix_error();
6788 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006789}
6790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006792PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006793"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006794Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006795
Barry Warsaw53699e91996-12-10 23:23:01 +00006796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006797posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006798{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006799 int fd;
6800 STRUCT_STAT st;
6801 int res;
6802 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6803 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006804#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006805 /* on OpenVMS we must ensure that all bytes are written to the file */
6806 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006807#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006808 if (!_PyVerify_fd(fd))
6809 return posix_error();
6810 Py_BEGIN_ALLOW_THREADS
6811 res = FSTAT(fd, &st);
6812 Py_END_ALLOW_THREADS
6813 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006814#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006815 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006816#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006817 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006818#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006819 }
Tim Peters5aa91602002-01-30 05:46:57 +00006820
Victor Stinnerd6f85422010-05-05 23:33:33 +00006821 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006822}
6823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006825PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006826"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006827Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006828
Barry Warsaw53699e91996-12-10 23:23:01 +00006829static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006830posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006831{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006832 int fd;
6833 char *orgmode = "r";
6834 int bufsize = -1;
6835 FILE *fp;
6836 PyObject *f;
6837 char *mode;
6838 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6839 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006840
Victor Stinnerd6f85422010-05-05 23:33:33 +00006841 /* Sanitize mode. See fileobject.c */
6842 mode = PyMem_MALLOC(strlen(orgmode)+3);
6843 if (!mode) {
6844 PyErr_NoMemory();
6845 return NULL;
6846 }
6847 strcpy(mode, orgmode);
6848 if (_PyFile_SanitizeMode(mode)) {
6849 PyMem_FREE(mode);
6850 return NULL;
6851 }
Benjamin Peterson02ab7a82014-04-09 15:40:18 -04006852 if (!_PyVerify_fd(fd)) {
Benjamin Peterson5c863bf2014-04-14 19:45:46 -04006853 PyMem_FREE(mode);
6854 return posix_error();
6855 }
6856#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
6857 {
6858 struct stat buf;
6859 if (fstat(fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
6860 PyMem_FREE(mode);
6861 char *msg = strerror(EISDIR);
6862 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)",
6863 EISDIR, msg, "<fdopen>");
6864 PyErr_SetObject(PyExc_IOError, exc);
6865 Py_XDECREF(exc);
6866 return NULL;
6867 }
6868 }
6869#endif
6870 /* The dummy filename used here must be kept in sync with the value
6871 tested against in gzip.GzipFile.__init__() - see issue #13781. */
6872 f = PyFile_FromFile(NULL, "<fdopen>", orgmode, fclose);
6873 if (f == NULL) {
6874 PyMem_FREE(mode);
Benjamin Peterson02ab7a82014-04-09 15:40:18 -04006875 return NULL;
6876 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00006877 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006878#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006879 if (mode[0] == 'a') {
6880 /* try to make sure the O_APPEND flag is set */
6881 int flags;
6882 flags = fcntl(fd, F_GETFL);
6883 if (flags != -1)
6884 fcntl(fd, F_SETFL, flags | O_APPEND);
6885 fp = fdopen(fd, mode);
6886 if (fp == NULL && flags != -1)
6887 /* restore old mode if fdopen failed */
6888 fcntl(fd, F_SETFL, flags);
6889 } else {
6890 fp = fdopen(fd, mode);
6891 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006892#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006893 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006894#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006895 Py_END_ALLOW_THREADS
6896 PyMem_FREE(mode);
Benjamin Peterson5c863bf2014-04-14 19:45:46 -04006897 if (fp == NULL)
6898 return posix_error();
6899 /* We now know we will succeed, so initialize the file object. */
6900 ((PyFileObject *)f)->f_fp = fp;
6901 PyFile_SetBufSize(f, bufsize);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006902 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006903}
6904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006905PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006906"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006907Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006908connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006909
6910static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006911posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006912{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006913 int fd;
6914 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6915 return NULL;
6916 if (!_PyVerify_fd(fd))
6917 return PyBool_FromLong(0);
6918 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006919}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006920
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006921#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006922PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006923"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006924Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006925
Barry Warsaw53699e91996-12-10 23:23:01 +00006926static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006927posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006928{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006929#if defined(PYOS_OS2)
6930 HFILE read, write;
6931 APIRET rc;
6932
Victor Stinnerd6f85422010-05-05 23:33:33 +00006933 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006934 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006935 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006936 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006937 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006938
6939 return Py_BuildValue("(ii)", read, write);
6940#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006941#if !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006942 int fds[2];
6943 int res;
6944 Py_BEGIN_ALLOW_THREADS
6945 res = pipe(fds);
6946 Py_END_ALLOW_THREADS
6947 if (res != 0)
6948 return posix_error();
6949 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006950#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006951 HANDLE read, write;
6952 int read_fd, write_fd;
6953 BOOL ok;
6954 Py_BEGIN_ALLOW_THREADS
6955 ok = CreatePipe(&read, &write, NULL, 0);
6956 Py_END_ALLOW_THREADS
6957 if (!ok)
6958 return win32_error("CreatePipe", NULL);
6959 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6960 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6961 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006962#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006963#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006964}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006965#endif /* HAVE_PIPE */
6966
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006967
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006968#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006969PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006970"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006971Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006972
Barry Warsaw53699e91996-12-10 23:23:01 +00006973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006974posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006975{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006976 char *filename;
6977 int mode = 0666;
6978 int res;
6979 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6980 return NULL;
6981 Py_BEGIN_ALLOW_THREADS
6982 res = mkfifo(filename, mode);
6983 Py_END_ALLOW_THREADS
6984 if (res < 0)
6985 return posix_error();
6986 Py_INCREF(Py_None);
6987 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006988}
6989#endif
6990
6991
Neal Norwitz11690112002-07-30 01:08:28 +00006992#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006993PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006994"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006995Create a filesystem node (file, device special file or named pipe)\n\
6996named filename. mode specifies both the permissions to use and the\n\
6997type of node to be created, being combined (bitwise OR) with one of\n\
6998S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006999device defines the newly created device special file (probably using\n\
7000os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007001
7002
7003static PyObject *
7004posix_mknod(PyObject *self, PyObject *args)
7005{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007006 char *filename;
7007 int mode = 0600;
7008 int device = 0;
7009 int res;
7010 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
7011 return NULL;
7012 Py_BEGIN_ALLOW_THREADS
7013 res = mknod(filename, mode, device);
7014 Py_END_ALLOW_THREADS
7015 if (res < 0)
7016 return posix_error();
7017 Py_INCREF(Py_None);
7018 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007019}
7020#endif
7021
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007022#ifdef HAVE_DEVICE_MACROS
7023PyDoc_STRVAR(posix_major__doc__,
7024"major(device) -> major number\n\
7025Extracts a device major number from a raw device number.");
7026
7027static PyObject *
7028posix_major(PyObject *self, PyObject *args)
7029{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007030 int device;
7031 if (!PyArg_ParseTuple(args, "i:major", &device))
7032 return NULL;
7033 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007034}
7035
7036PyDoc_STRVAR(posix_minor__doc__,
7037"minor(device) -> minor number\n\
7038Extracts a device minor number from a raw device number.");
7039
7040static PyObject *
7041posix_minor(PyObject *self, PyObject *args)
7042{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007043 int device;
7044 if (!PyArg_ParseTuple(args, "i:minor", &device))
7045 return NULL;
7046 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007047}
7048
7049PyDoc_STRVAR(posix_makedev__doc__,
7050"makedev(major, minor) -> device number\n\
7051Composes a raw device number from the major and minor device numbers.");
7052
7053static PyObject *
7054posix_makedev(PyObject *self, PyObject *args)
7055{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007056 int major, minor;
7057 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7058 return NULL;
7059 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007060}
7061#endif /* device macros */
7062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007063
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007064#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007065PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007066"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007067Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007068
Barry Warsaw53699e91996-12-10 23:23:01 +00007069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007070posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007071{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007072 int fd;
7073 off_t length;
7074 int res;
7075 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007076
Victor Stinnerd6f85422010-05-05 23:33:33 +00007077 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
7078 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007079
7080#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007081 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007082#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007083 length = PyLong_Check(lenobj) ?
7084 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007085#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007086 if (PyErr_Occurred())
7087 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007088
Victor Stinnerd6f85422010-05-05 23:33:33 +00007089 Py_BEGIN_ALLOW_THREADS
7090 res = ftruncate(fd, length);
7091 Py_END_ALLOW_THREADS
7092 if (res < 0)
7093 return posix_error();
7094 Py_INCREF(Py_None);
7095 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007096}
7097#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007098
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007099#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007100PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007101"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007102Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007103
Fred Drake762e2061999-08-26 17:23:54 +00007104/* Save putenv() parameters as values here, so we can collect them when they
7105 * get re-set with another call for the same key. */
7106static PyObject *posix_putenv_garbage;
7107
Tim Peters5aa91602002-01-30 05:46:57 +00007108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007109posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007110{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007111 char *s1, *s2;
7112 char *newenv;
7113 PyObject *newstr;
7114 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007115
Victor Stinnerd6f85422010-05-05 23:33:33 +00007116 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
7117 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007118
7119#if defined(PYOS_OS2)
7120 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7121 APIRET rc;
7122
Guido van Rossumd48f2521997-12-05 22:19:34 +00007123 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
7124 if (rc != NO_ERROR)
7125 return os2_error(rc);
7126
7127 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7128 APIRET rc;
7129
Guido van Rossumd48f2521997-12-05 22:19:34 +00007130 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
7131 if (rc != NO_ERROR)
7132 return os2_error(rc);
7133 } else {
7134#endif
7135
Victor Stinnerd6f85422010-05-05 23:33:33 +00007136 /* XXX This can leak memory -- not easy to fix :-( */
7137 len = strlen(s1) + strlen(s2) + 2;
Victor Stinner53853c32011-11-22 22:20:13 +01007138#ifdef MS_WINDOWS
7139 if (_MAX_ENV < (len - 1)) {
7140 PyErr_Format(PyExc_ValueError,
7141 "the environment variable is longer than %u bytes",
7142 _MAX_ENV);
7143 return NULL;
7144 }
7145#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007146 /* len includes space for a trailing \0; the size arg to
7147 PyString_FromStringAndSize does not count that */
7148 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
7149 if (newstr == NULL)
7150 return PyErr_NoMemory();
7151 newenv = PyString_AS_STRING(newstr);
7152 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7153 if (putenv(newenv)) {
7154 Py_DECREF(newstr);
7155 posix_error();
7156 return NULL;
7157 }
7158 /* Install the first arg and newstr in posix_putenv_garbage;
7159 * this will cause previous value to be collected. This has to
7160 * happen after the real putenv() call because the old value
7161 * was still accessible until then. */
7162 if (PyDict_SetItem(posix_putenv_garbage,
7163 PyTuple_GET_ITEM(args, 0), newstr)) {
7164 /* really not much we can do; just leak */
7165 PyErr_Clear();
7166 }
7167 else {
7168 Py_DECREF(newstr);
7169 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007170
7171#if defined(PYOS_OS2)
7172 }
7173#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007174 Py_INCREF(Py_None);
7175 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007176}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007177#endif /* putenv */
7178
Guido van Rossumc524d952001-10-19 01:31:59 +00007179#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007180PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007181"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007182Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007183
7184static PyObject *
7185posix_unsetenv(PyObject *self, PyObject *args)
7186{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007187 char *s1;
Charles-François Natali93a11752011-11-27 13:01:35 +01007188#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner53853c32011-11-22 22:20:13 +01007189 int err;
Charles-François Natali93a11752011-11-27 13:01:35 +01007190#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007191
Victor Stinnerd6f85422010-05-05 23:33:33 +00007192 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7193 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007194
Charles-François Natali93a11752011-11-27 13:01:35 +01007195#ifdef HAVE_BROKEN_UNSETENV
7196 unsetenv(s1);
7197#else
Victor Stinner53853c32011-11-22 22:20:13 +01007198 err = unsetenv(s1);
Benjamin Peterson42d96dc2011-11-22 23:56:06 -06007199 if (err)
Victor Stinner53853c32011-11-22 22:20:13 +01007200 return posix_error();
Charles-François Natali93a11752011-11-27 13:01:35 +01007201#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007202
Victor Stinnerd6f85422010-05-05 23:33:33 +00007203 /* Remove the key from posix_putenv_garbage;
7204 * this will cause it to be collected. This has to
7205 * happen after the real unsetenv() call because the
7206 * old value was still accessible until then.
7207 */
7208 if (PyDict_DelItem(posix_putenv_garbage,
7209 PyTuple_GET_ITEM(args, 0))) {
7210 /* really not much we can do; just leak */
7211 PyErr_Clear();
7212 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007213
Victor Stinnerd6f85422010-05-05 23:33:33 +00007214 Py_INCREF(Py_None);
7215 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00007216}
7217#endif /* unsetenv */
7218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007219PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007220"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007221Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007222
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007224posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007225{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007226 int code;
7227 char *message;
7228 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7229 return NULL;
7230 message = strerror(code);
7231 if (message == NULL) {
7232 PyErr_SetString(PyExc_ValueError,
7233 "strerror() argument out of range");
7234 return NULL;
7235 }
7236 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007237}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007238
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007239
Guido van Rossumc9641791998-08-04 15:26:23 +00007240#ifdef HAVE_SYS_WAIT_H
7241
Fred Drake106c1a02002-04-23 15:58:02 +00007242#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007243PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007244"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007245Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007246
7247static PyObject *
7248posix_WCOREDUMP(PyObject *self, PyObject *args)
7249{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007250 WAIT_TYPE status;
7251 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007252
Victor Stinnerd6f85422010-05-05 23:33:33 +00007253 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7254 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007255
Victor Stinnerd6f85422010-05-05 23:33:33 +00007256 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007257}
7258#endif /* WCOREDUMP */
7259
7260#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007261PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007262"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007263Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007264job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007265
7266static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007267posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007268{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007269 WAIT_TYPE status;
7270 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007271
Victor Stinnerd6f85422010-05-05 23:33:33 +00007272 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7273 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007274
Victor Stinnerd6f85422010-05-05 23:33:33 +00007275 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007276}
7277#endif /* WIFCONTINUED */
7278
Guido van Rossumc9641791998-08-04 15:26:23 +00007279#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007280PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007281"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007282Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007283
7284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007285posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007286{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007287 WAIT_TYPE status;
7288 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007289
Victor Stinnerd6f85422010-05-05 23:33:33 +00007290 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7291 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007292
Victor Stinnerd6f85422010-05-05 23:33:33 +00007293 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007294}
7295#endif /* WIFSTOPPED */
7296
7297#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007298PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007299"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007300Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007301
7302static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007303posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007304{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007305 WAIT_TYPE status;
7306 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007307
Victor Stinnerd6f85422010-05-05 23:33:33 +00007308 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7309 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007310
Victor Stinnerd6f85422010-05-05 23:33:33 +00007311 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007312}
7313#endif /* WIFSIGNALED */
7314
7315#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007316PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007317"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007318Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007319system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007320
7321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007322posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007323{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007324 WAIT_TYPE status;
7325 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007326
Victor Stinnerd6f85422010-05-05 23:33:33 +00007327 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7328 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007329
Victor Stinnerd6f85422010-05-05 23:33:33 +00007330 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007331}
7332#endif /* WIFEXITED */
7333
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007334#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007335PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007336"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007337Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007338
7339static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007340posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007341{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007342 WAIT_TYPE status;
7343 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007344
Victor Stinnerd6f85422010-05-05 23:33:33 +00007345 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7346 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007347
Victor Stinnerd6f85422010-05-05 23:33:33 +00007348 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007349}
7350#endif /* WEXITSTATUS */
7351
7352#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007353PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007354"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007355Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007356value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007357
7358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007359posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007360{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007361 WAIT_TYPE status;
7362 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007363
Victor Stinnerd6f85422010-05-05 23:33:33 +00007364 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7365 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007366
Victor Stinnerd6f85422010-05-05 23:33:33 +00007367 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007368}
7369#endif /* WTERMSIG */
7370
7371#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007372PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007373"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007374Return the signal that stopped the process that provided\n\
7375the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007376
7377static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007378posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007379{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007380 WAIT_TYPE status;
7381 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007382
Victor Stinnerd6f85422010-05-05 23:33:33 +00007383 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7384 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007385
Victor Stinnerd6f85422010-05-05 23:33:33 +00007386 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007387}
7388#endif /* WSTOPSIG */
7389
7390#endif /* HAVE_SYS_WAIT_H */
7391
7392
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007393#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007394#ifdef _SCO_DS
7395/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7396 needed definitions in sys/statvfs.h */
7397#define _SVID3
7398#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007399#include <sys/statvfs.h>
7400
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007401static PyObject*
7402_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007403 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7404 if (v == NULL)
7405 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007406
7407#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007408 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7409 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7410 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7411 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7412 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7413 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7414 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7415 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7416 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7417 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007418#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007419 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7420 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7421 PyStructSequence_SET_ITEM(v, 2,
7422 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7423 PyStructSequence_SET_ITEM(v, 3,
7424 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7425 PyStructSequence_SET_ITEM(v, 4,
7426 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7427 PyStructSequence_SET_ITEM(v, 5,
7428 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7429 PyStructSequence_SET_ITEM(v, 6,
7430 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7431 PyStructSequence_SET_ITEM(v, 7,
7432 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7433 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7434 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007435#endif
7436
Victor Stinnerd6f85422010-05-05 23:33:33 +00007437 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007438}
7439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007440PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007441"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007442Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007443
7444static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007445posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007446{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007447 int fd, res;
7448 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007449
Victor Stinnerd6f85422010-05-05 23:33:33 +00007450 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7451 return NULL;
7452 Py_BEGIN_ALLOW_THREADS
7453 res = fstatvfs(fd, &st);
7454 Py_END_ALLOW_THREADS
7455 if (res != 0)
7456 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007457
Victor Stinnerd6f85422010-05-05 23:33:33 +00007458 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007459}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007460#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007461
7462
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007463#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007464#include <sys/statvfs.h>
7465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007466PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007467"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007468Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007469
7470static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007471posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007472{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007473 char *path;
7474 int res;
7475 struct statvfs st;
7476 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7477 return NULL;
7478 Py_BEGIN_ALLOW_THREADS
7479 res = statvfs(path, &st);
7480 Py_END_ALLOW_THREADS
7481 if (res != 0)
7482 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007483
Victor Stinnerd6f85422010-05-05 23:33:33 +00007484 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007485}
7486#endif /* HAVE_STATVFS */
7487
7488
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007489#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007490PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007491"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007492Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007493The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007494or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007495
7496static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007497posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007498{
7499 PyObject *result = NULL;
7500 char *dir = NULL;
7501 char *pfx = NULL;
7502 char *name;
7503
7504 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinnerd6f85422010-05-05 23:33:33 +00007505 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007506
7507 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007508 "tempnam is a potential security risk to your program") < 0)
7509 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007510
Antoine Pitroub0614612011-01-02 20:04:52 +00007511 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
7512 "use the tempfile module", 1) < 0)
7513 return NULL;
7514
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007515#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007516 name = _tempnam(dir, pfx);
7517#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007518 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007519#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007520 if (name == NULL)
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007521 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007522 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007523 free(name);
7524 return result;
7525}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007526#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007527
7528
7529#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007530PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007531"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007532Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007533
7534static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007535posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007536{
7537 FILE *fp;
7538
Antoine Pitroub0614612011-01-02 20:04:52 +00007539 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
7540 "use the tempfile module", 1) < 0)
7541 return NULL;
7542
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007543 fp = tmpfile();
7544 if (fp == NULL)
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007545 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007546 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007547}
7548#endif
7549
7550
7551#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007552PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007553"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007554Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007555
7556static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007557posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007558{
7559 char buffer[L_tmpnam];
7560 char *name;
7561
Skip Montanaro95618b52001-08-18 18:52:10 +00007562 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007563 "tmpnam is a potential security risk to your program") < 0)
7564 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007565
Antoine Pitroub0614612011-01-02 20:04:52 +00007566 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
7567 "use the tempfile module", 1) < 0)
7568 return NULL;
7569
Greg Wardb48bc172000-03-01 21:51:56 +00007570#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007571 name = tmpnam_r(buffer);
7572#else
7573 name = tmpnam(buffer);
7574#endif
7575 if (name == NULL) {
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007576 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007577#ifdef USE_TMPNAM_R
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007578 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007579#else
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007580 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007581#endif
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007582 );
7583 PyErr_SetObject(PyExc_OSError, err);
7584 Py_XDECREF(err);
7585 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007586 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007587 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007588}
7589#endif
7590
7591
Fred Drakec9680921999-12-13 16:37:25 +00007592/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7593 * It maps strings representing configuration variable names to
7594 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007595 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007596 * rarely-used constants. There are three separate tables that use
7597 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007598 *
7599 * This code is always included, even if none of the interfaces that
7600 * need it are included. The #if hackery needed to avoid it would be
7601 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007602 */
7603struct constdef {
7604 char *name;
7605 long value;
7606};
7607
Fred Drake12c6e2d1999-12-14 21:25:03 +00007608static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007609conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007610 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007611{
7612 if (PyInt_Check(arg)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007613 *valuep = PyInt_AS_LONG(arg);
7614 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007615 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007616 if (PyString_Check(arg)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007617 /* look up the value in the table using a binary search */
7618 size_t lo = 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00007619 size_t mid;
Stefan Krah93f7a322010-11-26 17:35:50 +00007620 size_t hi = tablesize;
7621 int cmp;
7622 char *confname = PyString_AS_STRING(arg);
7623 while (lo < hi) {
7624 mid = (lo + hi) / 2;
7625 cmp = strcmp(confname, table[mid].name);
7626 if (cmp < 0)
7627 hi = mid;
7628 else if (cmp > 0)
7629 lo = mid + 1;
7630 else {
7631 *valuep = table[mid].value;
7632 return 1;
7633 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007634 }
Stefan Krah93f7a322010-11-26 17:35:50 +00007635 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007636 }
7637 else
Stefan Krah93f7a322010-11-26 17:35:50 +00007638 PyErr_SetString(PyExc_TypeError,
7639 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007640 return 0;
7641}
7642
7643
7644#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7645static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007646#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007647 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007648#endif
7649#ifdef _PC_ABI_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007650 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007651#endif
Fred Drakec9680921999-12-13 16:37:25 +00007652#ifdef _PC_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007653 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007654#endif
7655#ifdef _PC_CHOWN_RESTRICTED
Victor Stinnerd6f85422010-05-05 23:33:33 +00007656 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007657#endif
7658#ifdef _PC_FILESIZEBITS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007659 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007660#endif
7661#ifdef _PC_LAST
Victor Stinnerd6f85422010-05-05 23:33:33 +00007662 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007663#endif
7664#ifdef _PC_LINK_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007665 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007666#endif
7667#ifdef _PC_MAX_CANON
Victor Stinnerd6f85422010-05-05 23:33:33 +00007668 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007669#endif
7670#ifdef _PC_MAX_INPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007671 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007672#endif
7673#ifdef _PC_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007674 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007675#endif
7676#ifdef _PC_NO_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007677 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007678#endif
7679#ifdef _PC_PATH_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007680 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007681#endif
7682#ifdef _PC_PIPE_BUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007683 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007684#endif
7685#ifdef _PC_PRIO_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007686 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007687#endif
7688#ifdef _PC_SOCK_MAXBUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007689 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007690#endif
7691#ifdef _PC_SYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007692 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007693#endif
7694#ifdef _PC_VDISABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007695 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007696#endif
7697};
7698
Fred Drakec9680921999-12-13 16:37:25 +00007699static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007700conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007701{
7702 return conv_confname(arg, valuep, posix_constants_pathconf,
7703 sizeof(posix_constants_pathconf)
7704 / sizeof(struct constdef));
7705}
7706#endif
7707
7708#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007709PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007710"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007711Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007712If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007713
7714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007715posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007716{
7717 PyObject *result = NULL;
7718 int name, fd;
7719
Fred Drake12c6e2d1999-12-14 21:25:03 +00007720 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7721 conv_path_confname, &name)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007722 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007723
Stefan Krah93f7a322010-11-26 17:35:50 +00007724 errno = 0;
7725 limit = fpathconf(fd, name);
7726 if (limit == -1 && errno != 0)
7727 posix_error();
7728 else
7729 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007730 }
7731 return result;
7732}
7733#endif
7734
7735
7736#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007737PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007738"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007739Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007740If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007741
7742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007743posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007744{
7745 PyObject *result = NULL;
7746 int name;
7747 char *path;
7748
7749 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7750 conv_path_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007751 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007752
Victor Stinnerd6f85422010-05-05 23:33:33 +00007753 errno = 0;
7754 limit = pathconf(path, name);
7755 if (limit == -1 && errno != 0) {
7756 if (errno == EINVAL)
Stefan Krahacaab2a2010-11-26 15:06:27 +00007757 /* could be a path or name problem */
7758 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007759 else
Stefan Krahacaab2a2010-11-26 15:06:27 +00007760 posix_error_with_filename(path);
Victor Stinnerd6f85422010-05-05 23:33:33 +00007761 }
7762 else
7763 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007764 }
7765 return result;
7766}
7767#endif
7768
7769#ifdef HAVE_CONFSTR
7770static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007771#ifdef _CS_ARCHITECTURE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007772 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007773#endif
7774#ifdef _CS_HOSTNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007775 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007776#endif
7777#ifdef _CS_HW_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007778 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007779#endif
7780#ifdef _CS_HW_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007781 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007782#endif
7783#ifdef _CS_INITTAB_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007784 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007785#endif
Fred Drakec9680921999-12-13 16:37:25 +00007786#ifdef _CS_LFS64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007787 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _CS_LFS64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007790 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _CS_LFS64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007793 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _CS_LFS64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007796 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _CS_LFS_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007799 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
7801#ifdef _CS_LFS_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007802 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007803#endif
7804#ifdef _CS_LFS_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007805 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
7807#ifdef _CS_LFS_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007808 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007809#endif
Fred Draked86ed291999-12-15 15:34:33 +00007810#ifdef _CS_MACHINE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007811 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007812#endif
Fred Drakec9680921999-12-13 16:37:25 +00007813#ifdef _CS_PATH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007814 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007815#endif
Fred Draked86ed291999-12-15 15:34:33 +00007816#ifdef _CS_RELEASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007817 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007818#endif
7819#ifdef _CS_SRPC_DOMAIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007820 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007821#endif
7822#ifdef _CS_SYSNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007823 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007824#endif
7825#ifdef _CS_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007826 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007827#endif
Fred Drakec9680921999-12-13 16:37:25 +00007828#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007829 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007832 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007835 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
7837#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007838 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007839#endif
7840#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007841 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007842#endif
7843#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007844 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
7846#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007847 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007848#endif
7849#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007850 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007851#endif
7852#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007853 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007854#endif
7855#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007856 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007857#endif
7858#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007859 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007860#endif
7861#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007862 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007863#endif
7864#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007865 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007866#endif
7867#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007868 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007869#endif
7870#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007871 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007872#endif
7873#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007874 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007875#endif
Fred Draked86ed291999-12-15 15:34:33 +00007876#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007877 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007878#endif
7879#ifdef _MIPS_CS_BASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007880 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007881#endif
7882#ifdef _MIPS_CS_HOSTID
Victor Stinnerd6f85422010-05-05 23:33:33 +00007883 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007884#endif
7885#ifdef _MIPS_CS_HW_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007886 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007887#endif
7888#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007889 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007890#endif
7891#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007892 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007893#endif
7894#ifdef _MIPS_CS_OSREL_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007895 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007896#endif
7897#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007898 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007899#endif
7900#ifdef _MIPS_CS_OS_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007901 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007902#endif
7903#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007904 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007905#endif
7906#ifdef _MIPS_CS_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007907 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007908#endif
7909#ifdef _MIPS_CS_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007910 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007911#endif
7912#ifdef _MIPS_CS_VENDOR
Victor Stinnerd6f85422010-05-05 23:33:33 +00007913 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007914#endif
Fred Drakec9680921999-12-13 16:37:25 +00007915};
7916
7917static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007918conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007919{
7920 return conv_confname(arg, valuep, posix_constants_confstr,
7921 sizeof(posix_constants_confstr)
7922 / sizeof(struct constdef));
7923}
7924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007925PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007926"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007927Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007928
7929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007930posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007931{
7932 PyObject *result = NULL;
7933 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007934 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007935
7936 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007937 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007938
Victor Stinnerd6f85422010-05-05 23:33:33 +00007939 errno = 0;
7940 len = confstr(name, buffer, sizeof(buffer));
7941 if (len == 0) {
7942 if (errno) {
7943 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007944 }
7945 else {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007946 result = Py_None;
7947 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007948 }
7949 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00007950 else {
7951 if ((unsigned int)len >= sizeof(buffer)) {
7952 result = PyString_FromStringAndSize(NULL, len-1);
7953 if (result != NULL)
7954 confstr(name, PyString_AS_STRING(result), len);
7955 }
7956 else
7957 result = PyString_FromStringAndSize(buffer, len-1);
7958 }
7959 }
Fred Drakec9680921999-12-13 16:37:25 +00007960 return result;
7961}
7962#endif
7963
7964
7965#ifdef HAVE_SYSCONF
7966static struct constdef posix_constants_sysconf[] = {
7967#ifdef _SC_2_CHAR_TERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00007968 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007969#endif
7970#ifdef _SC_2_C_BIND
Victor Stinnerd6f85422010-05-05 23:33:33 +00007971 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007972#endif
7973#ifdef _SC_2_C_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007974 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007975#endif
7976#ifdef _SC_2_C_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007977 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007978#endif
7979#ifdef _SC_2_FORT_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007980 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007981#endif
7982#ifdef _SC_2_FORT_RUN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007983 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007984#endif
7985#ifdef _SC_2_LOCALEDEF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007986 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007987#endif
7988#ifdef _SC_2_SW_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007989 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007990#endif
7991#ifdef _SC_2_UPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007992 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007993#endif
7994#ifdef _SC_2_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007995 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007996#endif
Fred Draked86ed291999-12-15 15:34:33 +00007997#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007998 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007999#endif
8000#ifdef _SC_ACL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008001 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008002#endif
Fred Drakec9680921999-12-13 16:37:25 +00008003#ifdef _SC_AIO_LISTIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008004 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008005#endif
Fred Drakec9680921999-12-13 16:37:25 +00008006#ifdef _SC_AIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008007 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008008#endif
8009#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008010 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008011#endif
8012#ifdef _SC_ARG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008013 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008014#endif
8015#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008016 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008017#endif
8018#ifdef _SC_ATEXIT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008019 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008020#endif
Fred Draked86ed291999-12-15 15:34:33 +00008021#ifdef _SC_AUDIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008022 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008023#endif
Fred Drakec9680921999-12-13 16:37:25 +00008024#ifdef _SC_AVPHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008025 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008026#endif
8027#ifdef _SC_BC_BASE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008028 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008029#endif
8030#ifdef _SC_BC_DIM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008031 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008032#endif
8033#ifdef _SC_BC_SCALE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008034 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008035#endif
8036#ifdef _SC_BC_STRING_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008037 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008038#endif
Fred Draked86ed291999-12-15 15:34:33 +00008039#ifdef _SC_CAP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008040 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008041#endif
Fred Drakec9680921999-12-13 16:37:25 +00008042#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008043 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008044#endif
8045#ifdef _SC_CHAR_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008046 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008047#endif
8048#ifdef _SC_CHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008049 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008050#endif
8051#ifdef _SC_CHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008052 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008053#endif
8054#ifdef _SC_CHILD_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008055 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008056#endif
8057#ifdef _SC_CLK_TCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008058 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008059#endif
8060#ifdef _SC_COHER_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008061 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008062#endif
8063#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008064 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008065#endif
8066#ifdef _SC_DCACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008067 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008068#endif
8069#ifdef _SC_DCACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008070 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008071#endif
8072#ifdef _SC_DCACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008073 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008074#endif
8075#ifdef _SC_DCACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008076 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008077#endif
8078#ifdef _SC_DCACHE_TBLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008079 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008080#endif
8081#ifdef _SC_DELAYTIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008082 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008083#endif
8084#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008085 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008086#endif
8087#ifdef _SC_EXPR_NEST_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008088 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008089#endif
8090#ifdef _SC_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008091 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008092#endif
8093#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008094 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008095#endif
8096#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008097 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008098#endif
8099#ifdef _SC_ICACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008100 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008101#endif
8102#ifdef _SC_ICACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008103 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008104#endif
8105#ifdef _SC_ICACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008106 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008107#endif
8108#ifdef _SC_ICACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008109 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008110#endif
Fred Draked86ed291999-12-15 15:34:33 +00008111#ifdef _SC_INF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008112 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008113#endif
Fred Drakec9680921999-12-13 16:37:25 +00008114#ifdef _SC_INT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008115 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008116#endif
8117#ifdef _SC_INT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008118 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008119#endif
8120#ifdef _SC_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008121 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008122#endif
Fred Draked86ed291999-12-15 15:34:33 +00008123#ifdef _SC_IP_SECOPTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008124 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008125#endif
Fred Drakec9680921999-12-13 16:37:25 +00008126#ifdef _SC_JOB_CONTROL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008127 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008128#endif
Fred Draked86ed291999-12-15 15:34:33 +00008129#ifdef _SC_KERN_POINTERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008130 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008131#endif
8132#ifdef _SC_KERN_SIM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008133 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008134#endif
Fred Drakec9680921999-12-13 16:37:25 +00008135#ifdef _SC_LINE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008136 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008137#endif
8138#ifdef _SC_LOGIN_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008139 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008140#endif
8141#ifdef _SC_LOGNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008142 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008143#endif
8144#ifdef _SC_LONG_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008145 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008146#endif
Fred Draked86ed291999-12-15 15:34:33 +00008147#ifdef _SC_MAC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008148 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008149#endif
Fred Drakec9680921999-12-13 16:37:25 +00008150#ifdef _SC_MAPPED_FILES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008151 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008152#endif
8153#ifdef _SC_MAXPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008154 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008155#endif
8156#ifdef _SC_MB_LEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008157 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008158#endif
8159#ifdef _SC_MEMLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008160 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008161#endif
8162#ifdef _SC_MEMLOCK_RANGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008163 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008164#endif
8165#ifdef _SC_MEMORY_PROTECTION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008166 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008167#endif
8168#ifdef _SC_MESSAGE_PASSING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008169 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008170#endif
Fred Draked86ed291999-12-15 15:34:33 +00008171#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008172 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008173#endif
Fred Drakec9680921999-12-13 16:37:25 +00008174#ifdef _SC_MQ_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008175 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008176#endif
8177#ifdef _SC_MQ_PRIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008178 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008179#endif
Fred Draked86ed291999-12-15 15:34:33 +00008180#ifdef _SC_NACLS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008181 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008182#endif
Fred Drakec9680921999-12-13 16:37:25 +00008183#ifdef _SC_NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008184 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008185#endif
8186#ifdef _SC_NL_ARGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008187 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008188#endif
8189#ifdef _SC_NL_LANGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008190 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008191#endif
8192#ifdef _SC_NL_MSGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008193 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008194#endif
8195#ifdef _SC_NL_NMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008196 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008197#endif
8198#ifdef _SC_NL_SETMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008199 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008200#endif
8201#ifdef _SC_NL_TEXTMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008202 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008203#endif
8204#ifdef _SC_NPROCESSORS_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008205 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008206#endif
8207#ifdef _SC_NPROCESSORS_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008208 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008209#endif
Fred Draked86ed291999-12-15 15:34:33 +00008210#ifdef _SC_NPROC_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008211 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008212#endif
8213#ifdef _SC_NPROC_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008214 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008215#endif
Fred Drakec9680921999-12-13 16:37:25 +00008216#ifdef _SC_NZERO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008217 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008218#endif
8219#ifdef _SC_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008220 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008221#endif
8222#ifdef _SC_PAGESIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008223 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008224#endif
8225#ifdef _SC_PAGE_SIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008226 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008227#endif
8228#ifdef _SC_PASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008229 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008230#endif
8231#ifdef _SC_PHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008232 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008233#endif
8234#ifdef _SC_PII
Victor Stinnerd6f85422010-05-05 23:33:33 +00008235 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008236#endif
8237#ifdef _SC_PII_INTERNET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008238 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008239#endif
8240#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008241 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008242#endif
8243#ifdef _SC_PII_INTERNET_STREAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008244 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008245#endif
8246#ifdef _SC_PII_OSI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008247 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008248#endif
8249#ifdef _SC_PII_OSI_CLTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008250 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008251#endif
8252#ifdef _SC_PII_OSI_COTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008253 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008254#endif
8255#ifdef _SC_PII_OSI_M
Victor Stinnerd6f85422010-05-05 23:33:33 +00008256 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008257#endif
8258#ifdef _SC_PII_SOCKET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008259 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008260#endif
8261#ifdef _SC_PII_XTI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008262 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008263#endif
8264#ifdef _SC_POLL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008265 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008266#endif
8267#ifdef _SC_PRIORITIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008268 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008269#endif
8270#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008271 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008272#endif
8273#ifdef _SC_REALTIME_SIGNALS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008274 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008275#endif
8276#ifdef _SC_RE_DUP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008277 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008278#endif
8279#ifdef _SC_RTSIG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008280 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008281#endif
8282#ifdef _SC_SAVED_IDS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008283 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008284#endif
8285#ifdef _SC_SCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008286 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008287#endif
8288#ifdef _SC_SCHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008289 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008290#endif
8291#ifdef _SC_SELECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008292 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008293#endif
8294#ifdef _SC_SEMAPHORES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008295 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008296#endif
8297#ifdef _SC_SEM_NSEMS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008298 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008299#endif
8300#ifdef _SC_SEM_VALUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008301 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008302#endif
8303#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008304 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008305#endif
8306#ifdef _SC_SHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008307 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008308#endif
8309#ifdef _SC_SHRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008310 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008311#endif
8312#ifdef _SC_SIGQUEUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008313 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008314#endif
8315#ifdef _SC_SIGRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008316 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008317#endif
8318#ifdef _SC_SIGRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008319 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008320#endif
Fred Draked86ed291999-12-15 15:34:33 +00008321#ifdef _SC_SOFTPOWER
Victor Stinnerd6f85422010-05-05 23:33:33 +00008322 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008323#endif
Fred Drakec9680921999-12-13 16:37:25 +00008324#ifdef _SC_SPLIT_CACHE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008325 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008326#endif
8327#ifdef _SC_SSIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008328 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008329#endif
8330#ifdef _SC_STACK_PROT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008331 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008332#endif
8333#ifdef _SC_STREAM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008334 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008335#endif
8336#ifdef _SC_SYNCHRONIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008337 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008338#endif
8339#ifdef _SC_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008340 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008341#endif
8342#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008343 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008344#endif
8345#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008346 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008347#endif
8348#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008349 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008350#endif
8351#ifdef _SC_THREAD_KEYS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008352 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008353#endif
8354#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008355 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008356#endif
8357#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008358 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008359#endif
8360#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008361 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008362#endif
8363#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008364 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008365#endif
8366#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008367 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008368#endif
8369#ifdef _SC_THREAD_STACK_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008370 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008371#endif
8372#ifdef _SC_THREAD_THREADS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008373 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008374#endif
8375#ifdef _SC_TIMERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008376 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008377#endif
8378#ifdef _SC_TIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008379 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008380#endif
8381#ifdef _SC_TTY_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008382 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008383#endif
8384#ifdef _SC_TZNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008385 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008386#endif
8387#ifdef _SC_T_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008388 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008389#endif
8390#ifdef _SC_UCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008391 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008392#endif
8393#ifdef _SC_UINT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008394 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008395#endif
8396#ifdef _SC_UIO_MAXIOV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008397 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008398#endif
8399#ifdef _SC_ULONG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008400 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008401#endif
8402#ifdef _SC_USHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008403 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008404#endif
8405#ifdef _SC_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008406 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008407#endif
8408#ifdef _SC_WORD_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008409 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008410#endif
8411#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinnerd6f85422010-05-05 23:33:33 +00008412 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008413#endif
8414#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008415 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008416#endif
8417#ifdef _SC_XBS5_LP64_OFF64
Victor Stinnerd6f85422010-05-05 23:33:33 +00008418 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008419#endif
8420#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008421 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008422#endif
8423#ifdef _SC_XOPEN_CRYPT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008424 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008425#endif
8426#ifdef _SC_XOPEN_ENH_I18N
Victor Stinnerd6f85422010-05-05 23:33:33 +00008427 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008428#endif
8429#ifdef _SC_XOPEN_LEGACY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008430 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008431#endif
8432#ifdef _SC_XOPEN_REALTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008433 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008434#endif
8435#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008436 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008437#endif
8438#ifdef _SC_XOPEN_SHM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008439 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008440#endif
8441#ifdef _SC_XOPEN_UNIX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008442 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008443#endif
8444#ifdef _SC_XOPEN_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008445 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008446#endif
8447#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008448 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008449#endif
8450#ifdef _SC_XOPEN_XPG2
Victor Stinnerd6f85422010-05-05 23:33:33 +00008451 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008452#endif
8453#ifdef _SC_XOPEN_XPG3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008454 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008455#endif
8456#ifdef _SC_XOPEN_XPG4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008457 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008458#endif
8459};
8460
8461static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008462conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008463{
8464 return conv_confname(arg, valuep, posix_constants_sysconf,
8465 sizeof(posix_constants_sysconf)
8466 / sizeof(struct constdef));
8467}
8468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008469PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008470"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008471Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008472
8473static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008474posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008475{
8476 PyObject *result = NULL;
8477 int name;
8478
8479 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner862490a2010-05-06 00:03:44 +00008480 int value;
Fred Drakec9680921999-12-13 16:37:25 +00008481
Victor Stinner862490a2010-05-06 00:03:44 +00008482 errno = 0;
8483 value = sysconf(name);
8484 if (value == -1 && errno != 0)
8485 posix_error();
8486 else
8487 result = PyInt_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008488 }
8489 return result;
8490}
8491#endif
8492
8493
Fred Drakebec628d1999-12-15 18:31:10 +00008494/* This code is used to ensure that the tables of configuration value names
8495 * are in sorted order as required by conv_confname(), and also to build the
8496 * the exported dictionaries that are used to publish information about the
8497 * names available on the host platform.
8498 *
8499 * Sorting the table at runtime ensures that the table is properly ordered
8500 * when used, even for platforms we're not able to test on. It also makes
8501 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008502 */
Fred Drakebec628d1999-12-15 18:31:10 +00008503
8504static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008505cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008506{
8507 const struct constdef *c1 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008508 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008509 const struct constdef *c2 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008510 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008511
8512 return strcmp(c1->name, c2->name);
8513}
8514
8515static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008516setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinnerd6f85422010-05-05 23:33:33 +00008517 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008518{
Fred Drakebec628d1999-12-15 18:31:10 +00008519 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008520 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008521
8522 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8523 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008524 if (d == NULL)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008525 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008526
Barry Warsaw3155db32000-04-13 15:20:40 +00008527 for (i=0; i < tablesize; ++i) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008528 PyObject *o = PyInt_FromLong(table[i].value);
8529 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8530 Py_XDECREF(o);
8531 Py_DECREF(d);
8532 return -1;
8533 }
8534 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008535 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008536 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008537}
8538
Fred Drakebec628d1999-12-15 18:31:10 +00008539/* Return -1 on failure, 0 on success. */
8540static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008541setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008542{
8543#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008544 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008545 sizeof(posix_constants_pathconf)
8546 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008547 "pathconf_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008548 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008549#endif
8550#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008551 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008552 sizeof(posix_constants_confstr)
8553 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008554 "confstr_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008555 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008556#endif
8557#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008558 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008559 sizeof(posix_constants_sysconf)
8560 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008561 "sysconf_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008562 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008563#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008564 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008565}
Fred Draked86ed291999-12-15 15:34:33 +00008566
8567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008568PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008569"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008570Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008571in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008572
8573static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008574posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008575{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008576 abort();
8577 /*NOTREACHED*/
8578 Py_FatalError("abort() called from Python code didn't abort!");
8579 return NULL;
8580}
Fred Drakebec628d1999-12-15 18:31:10 +00008581
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008582#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008583PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008584"startfile(filepath [, operation]) - Start a file with its associated\n\
8585application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008586\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008587When \"operation\" is not specified or \"open\", this acts like\n\
8588double-clicking the file in Explorer, or giving the file name as an\n\
8589argument to the DOS \"start\" command: the file is opened with whatever\n\
8590application (if any) its extension is associated.\n\
8591When another \"operation\" is given, it specifies what should be done with\n\
8592the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008593\n\
8594startfile returns as soon as the associated application is launched.\n\
8595There is no option to wait for the application to close, and no way\n\
8596to retrieve the application's exit status.\n\
8597\n\
8598The filepath is relative to the current directory. If you want to use\n\
8599an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008600the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008601
8602static PyObject *
8603win32_startfile(PyObject *self, PyObject *args)
8604{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008605 char *filepath;
8606 char *operation = NULL;
8607 HINSTANCE rc;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008608
Victor Stinnerd6f85422010-05-05 23:33:33 +00008609 PyObject *unipath, *woperation = NULL;
8610 if (!PyArg_ParseTuple(args, "U|s:startfile",
8611 &unipath, &operation)) {
8612 PyErr_Clear();
8613 goto normal;
8614 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008615
Victor Stinnerd6f85422010-05-05 23:33:33 +00008616 if (operation) {
8617 woperation = PyUnicode_DecodeASCII(operation,
8618 strlen(operation), NULL);
8619 if (!woperation) {
8620 PyErr_Clear();
8621 operation = NULL;
8622 goto normal;
8623 }
8624 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008625
Victor Stinnerd6f85422010-05-05 23:33:33 +00008626 Py_BEGIN_ALLOW_THREADS
8627 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8628 PyUnicode_AS_UNICODE(unipath),
8629 NULL, NULL, SW_SHOWNORMAL);
8630 Py_END_ALLOW_THREADS
8631
8632 Py_XDECREF(woperation);
8633 if (rc <= (HINSTANCE)32) {
8634 PyObject *errval = win32_error_unicode("startfile",
8635 PyUnicode_AS_UNICODE(unipath));
8636 return errval;
8637 }
8638 Py_INCREF(Py_None);
8639 return Py_None;
Georg Brandlad89dc82006-04-03 12:26:26 +00008640
8641normal:
Victor Stinnerd6f85422010-05-05 23:33:33 +00008642 if (!PyArg_ParseTuple(args, "et|s:startfile",
8643 Py_FileSystemDefaultEncoding, &filepath,
8644 &operation))
8645 return NULL;
8646 Py_BEGIN_ALLOW_THREADS
8647 rc = ShellExecute((HWND)0, operation, filepath,
8648 NULL, NULL, SW_SHOWNORMAL);
8649 Py_END_ALLOW_THREADS
8650 if (rc <= (HINSTANCE)32) {
8651 PyObject *errval = win32_error("startfile", filepath);
8652 PyMem_Free(filepath);
8653 return errval;
8654 }
8655 PyMem_Free(filepath);
8656 Py_INCREF(Py_None);
8657 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008658}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008659#endif /* MS_WINDOWS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008660
Martin v. Löwis438b5342002-12-27 10:16:42 +00008661#ifdef HAVE_GETLOADAVG
8662PyDoc_STRVAR(posix_getloadavg__doc__,
8663"getloadavg() -> (float, float, float)\n\n\
8664Return the number of processes in the system run queue averaged over\n\
8665the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8666was unobtainable");
8667
8668static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008669posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008670{
8671 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008672 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah93f7a322010-11-26 17:35:50 +00008673 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8674 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008675 } else
Stefan Krah93f7a322010-11-26 17:35:50 +00008676 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008677}
8678#endif
8679
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008680PyDoc_STRVAR(posix_urandom__doc__,
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008681"urandom(n) -> str\n\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008682Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008683
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008684static PyObject *
8685posix_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008686{
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008687 Py_ssize_t size;
8688 PyObject *result;
8689 int ret;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008690
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008691 /* Read arguments */
8692 if (!PyArg_ParseTuple(args, "n:urandom", &size))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008693 return NULL;
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008694 if (size < 0)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008695 return PyErr_Format(PyExc_ValueError,
8696 "negative argument not allowed");
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008697 result = PyBytes_FromStringAndSize(NULL, size);
8698 if (result == NULL)
8699 return NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008700
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008701 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
8702 PyBytes_GET_SIZE(result));
8703 if (ret == -1) {
8704 Py_DECREF(result);
8705 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008706 }
8707 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008708}
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008709
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008710#ifdef HAVE_SETRESUID
8711PyDoc_STRVAR(posix_setresuid__doc__,
8712"setresuid(ruid, euid, suid)\n\n\
8713Set the current process's real, effective, and saved user ids.");
8714
8715static PyObject*
8716posix_setresuid (PyObject *self, PyObject *args)
8717{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008718 uid_t ruid, euid, suid;
8719 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
8720 _Py_Uid_Converter, &ruid,
8721 _Py_Uid_Converter, &euid,
8722 _Py_Uid_Converter, &suid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008723 return NULL;
8724 if (setresuid(ruid, euid, suid) < 0)
8725 return posix_error();
8726 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008727}
8728#endif
8729
8730#ifdef HAVE_SETRESGID
8731PyDoc_STRVAR(posix_setresgid__doc__,
8732"setresgid(rgid, egid, sgid)\n\n\
8733Set the current process's real, effective, and saved group ids.");
8734
8735static PyObject*
8736posix_setresgid (PyObject *self, PyObject *args)
8737{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008738 gid_t rgid, egid, sgid;
8739 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
8740 _Py_Gid_Converter, &rgid,
8741 _Py_Gid_Converter, &egid,
8742 _Py_Gid_Converter, &sgid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008743 return NULL;
8744 if (setresgid(rgid, egid, sgid) < 0)
8745 return posix_error();
8746 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008747}
8748#endif
8749
8750#ifdef HAVE_GETRESUID
8751PyDoc_STRVAR(posix_getresuid__doc__,
8752"getresuid() -> (ruid, euid, suid)\n\n\
8753Get tuple of the current process's real, effective, and saved user ids.");
8754
8755static PyObject*
8756posix_getresuid (PyObject *self, PyObject *noargs)
8757{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008758 uid_t ruid, euid, suid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008759 if (getresuid(&ruid, &euid, &suid) < 0)
8760 return posix_error();
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008761 return Py_BuildValue("(NNN)", _PyInt_FromUid(ruid),
8762 _PyInt_FromUid(euid),
8763 _PyInt_FromUid(suid));
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008764}
8765#endif
8766
8767#ifdef HAVE_GETRESGID
8768PyDoc_STRVAR(posix_getresgid__doc__,
8769"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandl21946af2010-10-06 09:28:45 +00008770Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008771
8772static PyObject*
8773posix_getresgid (PyObject *self, PyObject *noargs)
8774{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008775 uid_t rgid, egid, sgid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008776 if (getresgid(&rgid, &egid, &sgid) < 0)
8777 return posix_error();
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008778 return Py_BuildValue("(NNN)", _PyInt_FromGid(rgid),
8779 _PyInt_FromGid(egid),
8780 _PyInt_FromGid(sgid));
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008781}
8782#endif
8783
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008784static PyMethodDef posix_methods[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008785 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008786#ifdef HAVE_TTYNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008787 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008788#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008789 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008790#ifdef HAVE_CHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008791 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008792#endif /* HAVE_CHFLAGS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008793 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008794#ifdef HAVE_FCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008795 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008796#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008797#ifdef HAVE_CHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008798 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008799#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008800#ifdef HAVE_LCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008801 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008802#endif /* HAVE_LCHMOD */
8803#ifdef HAVE_FCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008804 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008805#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008806#ifdef HAVE_LCHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008807 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008808#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008809#ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008810 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008811#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008812#ifdef HAVE_CHROOT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008813 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008814#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008815#ifdef HAVE_CTERMID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008816 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008817#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008818#ifdef HAVE_GETCWD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008819 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008820#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008821 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008822#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008823#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008824#ifdef HAVE_LINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008825 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008826#endif /* HAVE_LINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008827 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8828 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8829 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008830#ifdef HAVE_NICE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008831 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008832#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008833#ifdef HAVE_READLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008834 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008835#endif /* HAVE_READLINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008836 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8837 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8838 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8839 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008840#ifdef HAVE_SYMLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008841 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008842#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008843#ifdef HAVE_SYSTEM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008844 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008845#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008846 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008847#ifdef HAVE_UNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008848 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008849#endif /* HAVE_UNAME */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008850 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8851 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8852 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008853#ifdef HAVE_TIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008854 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008855#endif /* HAVE_TIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008856 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008857#ifdef HAVE_EXECV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008858 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8859 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008860#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008861#ifdef HAVE_SPAWNV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008862 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8863 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008864#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008865 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8866 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008867#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008868#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008869#ifdef HAVE_FORK1
Victor Stinnerd6f85422010-05-05 23:33:33 +00008870 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008871#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008872#ifdef HAVE_FORK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008873 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008874#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008875#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008876 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008877#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008878#ifdef HAVE_FORKPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008879 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008880#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008881#ifdef HAVE_GETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008882 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008883#endif /* HAVE_GETEGID */
8884#ifdef HAVE_GETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008885 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008886#endif /* HAVE_GETEUID */
8887#ifdef HAVE_GETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008888 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008889#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008890#ifdef HAVE_GETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008891 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008892#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008893 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008894#ifdef HAVE_GETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008895 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008896#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008897#ifdef HAVE_GETPPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008898 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008899#endif /* HAVE_GETPPID */
8900#ifdef HAVE_GETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008901 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008902#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008903#ifdef HAVE_GETLOGIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008904 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008905#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008906#ifdef HAVE_KILL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008907 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008908#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008909#ifdef HAVE_KILLPG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008910 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008911#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008912#ifdef HAVE_PLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008913 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008914#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008915#ifdef HAVE_POPEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008916 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008917#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008918 {"popen2", win32_popen2, METH_VARARGS},
8919 {"popen3", win32_popen3, METH_VARARGS},
8920 {"popen4", win32_popen4, METH_VARARGS},
8921 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8922 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008923#else
8924#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008925 {"popen2", os2emx_popen2, METH_VARARGS},
8926 {"popen3", os2emx_popen3, METH_VARARGS},
8927 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008928#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008929#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008930#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008931#ifdef HAVE_SETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008932 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008933#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008934#ifdef HAVE_SETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008935 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008936#endif /* HAVE_SETEUID */
8937#ifdef HAVE_SETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008938 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008939#endif /* HAVE_SETEGID */
8940#ifdef HAVE_SETREUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008941 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008942#endif /* HAVE_SETREUID */
8943#ifdef HAVE_SETREGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008944 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008945#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008946#ifdef HAVE_SETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008947 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008948#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008949#ifdef HAVE_SETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008950 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008951#endif /* HAVE_SETGROUPS */
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008952#ifdef HAVE_INITGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008953 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008954#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008955#ifdef HAVE_GETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008956 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008957#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008958#ifdef HAVE_SETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008959 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008960#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008961#ifdef HAVE_WAIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008962 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008963#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008964#ifdef HAVE_WAIT3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008965 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008966#endif /* HAVE_WAIT3 */
8967#ifdef HAVE_WAIT4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008968 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008969#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008970#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008971 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008972#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008973#ifdef HAVE_GETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008974 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008975#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008976#ifdef HAVE_SETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008977 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008978#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008979#ifdef HAVE_SETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008980 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008981#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008982#ifdef HAVE_TCGETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008983 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008984#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008985#ifdef HAVE_TCSETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008986 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008987#endif /* HAVE_TCSETPGRP */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008988 {"open", posix_open, METH_VARARGS, posix_open__doc__},
Benjamin Peterson2748c5c2014-02-11 10:16:16 -05008989 {"close", posix_close_, METH_VARARGS, posix_close__doc__},
Victor Stinnerd6f85422010-05-05 23:33:33 +00008990 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8991 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8992 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8993 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8994 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8995 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8996 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8997 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8998 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008999#ifdef HAVE_PIPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009000 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009001#endif
9002#ifdef HAVE_MKFIFO
Victor Stinnerd6f85422010-05-05 23:33:33 +00009003 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009004#endif
Neal Norwitz11690112002-07-30 01:08:28 +00009005#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009006 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00009007#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009008#ifdef HAVE_DEVICE_MACROS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009009 {"major", posix_major, METH_VARARGS, posix_major__doc__},
9010 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
9011 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009012#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009013#ifdef HAVE_FTRUNCATE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009014 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009015#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009016#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00009017 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009018#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009019#ifdef HAVE_UNSETENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00009020 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00009021#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009022 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009023#ifdef HAVE_FCHDIR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009024 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009025#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009026#ifdef HAVE_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009027 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009028#endif
9029#ifdef HAVE_FDATASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009030 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009031#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00009032#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009033#ifdef WCOREDUMP
Victor Stinnerd6f85422010-05-05 23:33:33 +00009034 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00009035#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009036#ifdef WIFCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009037 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009038#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00009039#ifdef WIFSTOPPED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009040 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009041#endif /* WIFSTOPPED */
9042#ifdef WIFSIGNALED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009043 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009044#endif /* WIFSIGNALED */
9045#ifdef WIFEXITED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009046 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009047#endif /* WIFEXITED */
9048#ifdef WEXITSTATUS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009049 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009050#endif /* WEXITSTATUS */
9051#ifdef WTERMSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009052 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009053#endif /* WTERMSIG */
9054#ifdef WSTOPSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009055 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009056#endif /* WSTOPSIG */
9057#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00009058#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009059 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009060#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00009061#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009062 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009063#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00009064#ifdef HAVE_TMPFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009065 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009066#endif
9067#ifdef HAVE_TEMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009068 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009069#endif
9070#ifdef HAVE_TMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009071 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009072#endif
Fred Drakec9680921999-12-13 16:37:25 +00009073#ifdef HAVE_CONFSTR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009074 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009075#endif
9076#ifdef HAVE_SYSCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009077 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009078#endif
9079#ifdef HAVE_FPATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009080 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009081#endif
9082#ifdef HAVE_PATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009083 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009084#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009085 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009086#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009087 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtin5446f082011-06-09 10:00:42 -05009088 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00009089#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009090#ifdef HAVE_GETLOADAVG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009091 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009092#endif
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009093#ifdef HAVE_SETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009094 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009095#endif
9096#ifdef HAVE_SETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009097 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009098#endif
9099#ifdef HAVE_GETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009100 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009101#endif
9102#ifdef HAVE_GETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009103 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009104#endif
Barry Warsaw1e13eb02012-02-20 20:42:21 -05009105 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Victor Stinnerd6f85422010-05-05 23:33:33 +00009106 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009107};
9108
9109
Barry Warsaw4a342091996-12-19 23:50:02 +00009110static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009111ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009112{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009113 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009114}
9115
Guido van Rossumd48f2521997-12-05 22:19:34 +00009116#if defined(PYOS_OS2)
9117/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009118static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009119{
9120 APIRET rc;
9121 ULONG values[QSV_MAX+1];
9122 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009123 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009124
9125 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009126 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009127 Py_END_ALLOW_THREADS
9128
9129 if (rc != NO_ERROR) {
9130 os2_error(rc);
9131 return -1;
9132 }
9133
Fred Drake4d1e64b2002-04-15 19:40:07 +00009134 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9135 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9136 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9137 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9138 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9139 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9140 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009141
9142 switch (values[QSV_VERSION_MINOR]) {
9143 case 0: ver = "2.00"; break;
9144 case 10: ver = "2.10"; break;
9145 case 11: ver = "2.11"; break;
9146 case 30: ver = "3.00"; break;
9147 case 40: ver = "4.00"; break;
9148 case 50: ver = "5.00"; break;
9149 default:
Tim Peters885d4572001-11-28 20:27:42 +00009150 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinnerd6f85422010-05-05 23:33:33 +00009151 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009152 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009153 ver = &tmp[0];
9154 }
9155
9156 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009157 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009158 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009159
9160 /* Add Indicator of Which Drive was Used to Boot the System */
9161 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9162 tmp[1] = ':';
9163 tmp[2] = '\0';
9164
Fred Drake4d1e64b2002-04-15 19:40:07 +00009165 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009166}
9167#endif
9168
Barry Warsaw4a342091996-12-19 23:50:02 +00009169static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009170all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009171{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009172#ifdef F_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009173 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009174#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009175#ifdef R_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009176 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009177#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009178#ifdef W_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009179 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009180#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009181#ifdef X_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009182 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009183#endif
Fred Drakec9680921999-12-13 16:37:25 +00009184#ifdef NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009185 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009186#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009187#ifdef TMP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009188 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009189#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009190#ifdef WCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009191 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009192#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009193#ifdef WNOHANG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009194 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009195#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009196#ifdef WUNTRACED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009197 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009198#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009199#ifdef O_RDONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009200 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009201#endif
9202#ifdef O_WRONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009203 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009204#endif
9205#ifdef O_RDWR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009206 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009207#endif
9208#ifdef O_NDELAY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009209 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009210#endif
9211#ifdef O_NONBLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009212 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009213#endif
9214#ifdef O_APPEND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009215 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009216#endif
9217#ifdef O_DSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009218 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009219#endif
9220#ifdef O_RSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009221 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009222#endif
9223#ifdef O_SYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009224 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009225#endif
9226#ifdef O_NOCTTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009227 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009228#endif
9229#ifdef O_CREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009230 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009231#endif
9232#ifdef O_EXCL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009233 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009234#endif
9235#ifdef O_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009236 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009237#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009238#ifdef O_BINARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009239 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009240#endif
9241#ifdef O_TEXT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009242 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009243#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009244#ifdef O_LARGEFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009245 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009246#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009247#ifdef O_SHLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009248 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009249#endif
9250#ifdef O_EXLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009251 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009252#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009253
Tim Peters5aa91602002-01-30 05:46:57 +00009254/* MS Windows */
9255#ifdef O_NOINHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009256 /* Don't inherit in child processes. */
9257 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009258#endif
9259#ifdef _O_SHORT_LIVED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009260 /* Optimize for short life (keep in memory). */
9261 /* MS forgot to define this one with a non-underscore form too. */
9262 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009263#endif
9264#ifdef O_TEMPORARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009265 /* Automatically delete when last handle is closed. */
9266 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009267#endif
9268#ifdef O_RANDOM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009269 /* Optimize for random access. */
9270 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009271#endif
9272#ifdef O_SEQUENTIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009273 /* Optimize for sequential access. */
9274 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009275#endif
9276
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009277/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00009278#ifdef O_ASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009279 /* Send a SIGIO signal whenever input or output
9280 becomes available on file descriptor */
9281 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00009282#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009283#ifdef O_DIRECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009284 /* Direct disk access. */
9285 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009286#endif
9287#ifdef O_DIRECTORY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009288 /* Must be a directory. */
9289 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009290#endif
9291#ifdef O_NOFOLLOW
Victor Stinnerd6f85422010-05-05 23:33:33 +00009292 /* Do not follow links. */
9293 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009294#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00009295#ifdef O_NOATIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00009296 /* Do not update the access time. */
9297 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00009298#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009299
Victor Stinnerd6f85422010-05-05 23:33:33 +00009300 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009301#ifdef EX_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009302 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009303#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009304#ifdef EX_USAGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009305 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009306#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009307#ifdef EX_DATAERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009308 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009309#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009310#ifdef EX_NOINPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009311 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009312#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009313#ifdef EX_NOUSER
Victor Stinnerd6f85422010-05-05 23:33:33 +00009314 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009315#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009316#ifdef EX_NOHOST
Victor Stinnerd6f85422010-05-05 23:33:33 +00009317 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009318#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009319#ifdef EX_UNAVAILABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009320 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009321#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009322#ifdef EX_SOFTWARE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009323 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009324#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009325#ifdef EX_OSERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009326 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009327#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009328#ifdef EX_OSFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009329 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009330#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009331#ifdef EX_CANTCREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009332 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009333#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009334#ifdef EX_IOERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009335 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009336#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009337#ifdef EX_TEMPFAIL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009338 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009339#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009340#ifdef EX_PROTOCOL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009341 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009342#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009343#ifdef EX_NOPERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009344 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009345#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009346#ifdef EX_CONFIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009347 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009348#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009349#ifdef EX_NOTFOUND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009350 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009351#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009352
Guido van Rossum246bc171999-02-01 23:54:31 +00009353#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009354#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009355 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9356 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9357 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9358 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9359 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9360 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9361 if (ins(d, "P_PM", (long)P_PM)) return -1;
9362 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9363 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9364 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9365 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9366 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9367 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9368 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9369 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9370 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9371 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9372 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9373 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9374 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009375#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009376 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9377 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9378 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9379 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9380 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009381#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009382#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009383
Guido van Rossumd48f2521997-12-05 22:19:34 +00009384#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009385 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009386#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009387 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009388}
9389
9390
Tim Peters5aa91602002-01-30 05:46:57 +00009391#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009392#define INITFUNC initnt
9393#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009394
9395#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009396#define INITFUNC initos2
9397#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009398
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009399#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009400#define INITFUNC initposix
9401#define MODNAME "posix"
9402#endif
9403
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009404PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009405INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009406{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009407 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009408
Victor Stinnerd6f85422010-05-05 23:33:33 +00009409 m = Py_InitModule3(MODNAME,
9410 posix_methods,
9411 posix__doc__);
9412 if (m == NULL)
9413 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009414
Victor Stinnerd6f85422010-05-05 23:33:33 +00009415 /* Initialize environ dictionary */
9416 v = convertenviron();
9417 Py_XINCREF(v);
9418 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9419 return;
9420 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009421
Victor Stinnerd6f85422010-05-05 23:33:33 +00009422 if (all_ins(m))
9423 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009424
Victor Stinnerd6f85422010-05-05 23:33:33 +00009425 if (setup_confname_tables(m))
9426 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009427
Victor Stinnerd6f85422010-05-05 23:33:33 +00009428 Py_INCREF(PyExc_OSError);
9429 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009430
Guido van Rossumb3d39562000-01-31 18:41:26 +00009431#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00009432 if (posix_putenv_garbage == NULL)
9433 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009434#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009435
Victor Stinnerd6f85422010-05-05 23:33:33 +00009436 if (!initialized) {
9437 stat_result_desc.name = MODNAME ".stat_result";
9438 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9439 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9440 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9441 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9442 structseq_new = StatResultType.tp_new;
9443 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009444
Victor Stinnerd6f85422010-05-05 23:33:33 +00009445 statvfs_result_desc.name = MODNAME ".statvfs_result";
9446 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009447#ifdef NEED_TICKS_PER_SECOND
9448# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009449 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009450# elif defined(HZ)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009451 ticks_per_second = HZ;
Martin v. Löwis03824e42008-12-29 18:17:34 +00009452# else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009453 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis03824e42008-12-29 18:17:34 +00009454# endif
9455#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009456 }
9457 Py_INCREF((PyObject*) &StatResultType);
9458 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9459 Py_INCREF((PyObject*) &StatVFSResultType);
9460 PyModule_AddObject(m, "statvfs_result",
9461 (PyObject*) &StatVFSResultType);
9462 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009463
9464#ifdef __APPLE__
Victor Stinnerd6f85422010-05-05 23:33:33 +00009465 /*
9466 * Step 2 of weak-linking support on Mac OS X.
9467 *
9468 * The code below removes functions that are not available on the
9469 * currently active platform.
9470 *
9471 * This block allow one to use a python binary that was build on
9472 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9473 * OSX 10.4.
9474 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009475#ifdef HAVE_FSTATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009476 if (fstatvfs == NULL) {
9477 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9478 return;
9479 }
9480 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009481#endif /* HAVE_FSTATVFS */
9482
9483#ifdef HAVE_STATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009484 if (statvfs == NULL) {
9485 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9486 return;
9487 }
9488 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009489#endif /* HAVE_STATVFS */
9490
9491# ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00009492 if (lchown == NULL) {
9493 if (PyObject_DelAttrString(m, "lchown") == -1) {
9494 return;
9495 }
9496 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009497#endif /* HAVE_LCHOWN */
9498
9499
9500#endif /* __APPLE__ */
9501
Guido van Rossumb6775db1994-08-01 11:34:53 +00009502}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009503
9504#ifdef __cplusplus
9505}
9506#endif
9507
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009508