blob: a06ebd10a3fed7f766b0c700e8eeb69873456228 [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 }
400 if ((uid_t)uresult == (uid_t)-1)
401 goto OverflowUp;
402 } else {
403 if (result < 0)
404 goto OverflowDown;
405 uresult = result;
406 }
407 if (sizeof(uid_t) < sizeof(long) &&
408 (unsigned long)(uid_t)uresult != uresult)
409 goto OverflowUp;
410 *(uid_t *)p = (uid_t)uresult;
411 }
412 return 1;
413
414OverflowDown:
415 PyErr_SetString(PyExc_OverflowError,
416 "user id is less than minimum");
417 return 0;
418
419OverflowUp:
420 PyErr_SetString(PyExc_OverflowError,
421 "user id is greater than maximum");
422 return 0;
423}
424
425int
426_Py_Gid_Converter(PyObject *obj, void *p)
427{
428 int overflow;
429 long result;
430 if (PyFloat_Check(obj)) {
431 PyErr_SetString(PyExc_TypeError,
432 "integer argument expected, got float");
433 return 0;
434 }
435 result = PyLong_AsLongAndOverflow(obj, &overflow);
436 if (overflow < 0)
437 goto OverflowDown;
438 if (!overflow && result == -1) {
439 /* error or -1 */
440 if (PyErr_Occurred())
441 return 0;
442 *(gid_t *)p = (gid_t)-1;
443 }
444 else {
445 /* unsigned gid_t */
446 unsigned long uresult;
447 if (overflow > 0) {
448 uresult = PyLong_AsUnsignedLong(obj);
449 if (PyErr_Occurred()) {
450 if (PyErr_ExceptionMatches(PyExc_OverflowError))
451 goto OverflowUp;
452 return 0;
453 }
454 if ((gid_t)uresult == (gid_t)-1)
455 goto OverflowUp;
456 } else {
457 if (result < 0)
458 goto OverflowDown;
459 uresult = result;
460 }
461 if (sizeof(gid_t) < sizeof(long) &&
462 (unsigned long)(gid_t)uresult != uresult)
463 goto OverflowUp;
464 *(gid_t *)p = (gid_t)uresult;
465 }
466 return 1;
467
468OverflowDown:
469 PyErr_SetString(PyExc_OverflowError,
470 "group id is less than minimum");
471 return 0;
472
473OverflowUp:
474 PyErr_SetString(PyExc_OverflowError,
475 "group id is greater than maximum");
476 return 0;
477}
478#endif /* MS_WINDOWS */
479
480
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000481#if defined _MSC_VER && _MSC_VER >= 1400
482/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200483 * valid and raise an assertion if it isn't.
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000484 * Normally, an invalid fd is likely to be a C program error and therefore
485 * an assertion can be useful, but it does contradict the POSIX standard
486 * which for write(2) states:
487 * "Otherwise, -1 shall be returned and errno set to indicate the error."
488 * "[EBADF] The fildes argument is not a valid file descriptor open for
489 * writing."
490 * Furthermore, python allows the user to enter any old integer
491 * as a fd and should merely raise a python exception on error.
492 * The Microsoft CRT doesn't provide an official way to check for the
493 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinnerd6f85422010-05-05 23:33:33 +0000494 * by using the exported __pinfo data member and knowledge of the
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000495 * internal structures involved.
496 * The structures below must be updated for each version of visual studio
497 * according to the file internal.h in the CRT source, until MS comes
498 * up with a less hacky way to do this.
499 * (all of this is to avoid globally modifying the CRT behaviour using
500 * _set_invalid_parameter_handler() and _CrtSetReportMode())
501 */
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000502/* The actual size of the structure is determined at runtime.
503 * Only the first items must be present.
504 */
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000505typedef struct {
Victor Stinnerd6f85422010-05-05 23:33:33 +0000506 intptr_t osfhnd;
507 char osfile;
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000508} my_ioinfo;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000509
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000510extern __declspec(dllimport) char * __pioinfo[];
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000511#define IOINFO_L2E 5
512#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
513#define IOINFO_ARRAYS 64
514#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
515#define FOPEN 0x01
516#define _NO_CONSOLE_FILENO (intptr_t)-2
517
518/* This function emulates what the windows CRT does to validate file handles */
519int
520_PyVerify_fd(int fd)
521{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000522 const int i1 = fd >> IOINFO_L2E;
523 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000524
Victor Stinnerd6f85422010-05-05 23:33:33 +0000525 static int sizeof_ioinfo = 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000526
Victor Stinnerd6f85422010-05-05 23:33:33 +0000527 /* Determine the actual size of the ioinfo structure,
528 * as used by the CRT loaded in memory
529 */
530 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
531 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
532 }
533 if (sizeof_ioinfo == 0) {
534 /* This should not happen... */
535 goto fail;
536 }
537
538 /* See that it isn't a special CLEAR fileno */
539 if (fd != _NO_CONSOLE_FILENO) {
540 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
541 * we check pointer validity and other info
542 */
543 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
544 /* finally, check that the file is open */
545 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
546 if (info->osfile & FOPEN) {
547 return 1;
548 }
549 }
550 }
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000551 fail:
Victor Stinnerd6f85422010-05-05 23:33:33 +0000552 errno = EBADF;
553 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000554}
555
556/* the special case of checking dup2. The target fd must be in a sensible range */
557static int
558_PyVerify_fd_dup2(int fd1, int fd2)
559{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000560 if (!_PyVerify_fd(fd1))
561 return 0;
562 if (fd2 == _NO_CONSOLE_FILENO)
563 return 0;
564 if ((unsigned)fd2 < _NHANDLE_)
565 return 1;
566 else
567 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000568}
569#else
570/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
571#define _PyVerify_fd_dup2(A, B) (1)
572#endif
573
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100575#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +0000576/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100577** environ directly, we must obtain it with _NSGetEnviron(). See also
578** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +0000579*/
580#include <crt_externs.h>
581static char **environ;
582#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000584#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585
Barry Warsaw53699e91996-12-10 23:23:01 +0000586static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000587convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000589 PyObject *d;
590 char **e;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000591#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +0000592 APIRET rc;
593 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
594#endif
595 d = PyDict_New();
596 if (d == NULL)
597 return NULL;
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100598#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinnerd6f85422010-05-05 23:33:33 +0000599 if (environ == NULL)
600 environ = *_NSGetEnviron();
601#endif
602 if (environ == NULL)
603 return d;
604 /* This part ignores errors */
605 for (e = environ; *e != NULL; e++) {
606 PyObject *k;
607 PyObject *v;
608 char *p = strchr(*e, '=');
609 if (p == NULL)
610 continue;
611 k = PyString_FromStringAndSize(*e, (int)(p-*e));
612 if (k == NULL) {
613 PyErr_Clear();
614 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000615 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000616 v = PyString_FromString(p+1);
617 if (v == NULL) {
618 PyErr_Clear();
619 Py_DECREF(k);
620 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000621 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000622 if (PyDict_GetItem(d, k) == NULL) {
623 if (PyDict_SetItem(d, k, v) != 0)
624 PyErr_Clear();
625 }
626 Py_DECREF(k);
627 Py_DECREF(v);
628 }
629#if defined(PYOS_OS2)
630 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
631 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
632 PyObject *v = PyString_FromString(buffer);
633 PyDict_SetItemString(d, "BEGINLIBPATH", v);
634 Py_DECREF(v);
635 }
636 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
637 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
638 PyObject *v = PyString_FromString(buffer);
639 PyDict_SetItemString(d, "ENDLIBPATH", v);
640 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000641 }
642#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000643 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
646
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647/* Set a POSIX-specific error from errno, and return NULL */
648
Barry Warsawd58d7641998-07-23 16:14:40 +0000649static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000650posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000651{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000652 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000653}
Barry Warsawd58d7641998-07-23 16:14:40 +0000654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000655posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000656{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000657 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000658}
659
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000660#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661static PyObject *
662posix_error_with_unicode_filename(Py_UNICODE* name)
663{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000664 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000665}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000666#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000667
668
Mark Hammondef8b6542001-05-13 08:04:26 +0000669static PyObject *
670posix_error_with_allocated_filename(char* name)
671{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000672 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
673 PyMem_Free(name);
674 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000675}
676
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000677#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000678static PyObject *
679win32_error(char* function, char* filename)
680{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000681 /* XXX We should pass the function name along in the future.
682 (_winreg.c also wants to pass the function name.)
683 This would however require an additional param to the
684 Windows error object, which is non-trivial.
685 */
686 errno = GetLastError();
687 if (filename)
688 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
689 else
690 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000691}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000692
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000693static PyObject *
694win32_error_unicode(char* function, Py_UNICODE* filename)
695{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000696 /* XXX - see win32_error for comments on 'function' */
697 errno = GetLastError();
698 if (filename)
699 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
700 else
701 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000702}
703
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000704static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000705convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000706{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000707 if (PyUnicode_CheckExact(*param))
708 Py_INCREF(*param);
709 else if (PyUnicode_Check(*param))
710 /* For a Unicode subtype that's not a Unicode object,
711 return a true Unicode object with the same data. */
712 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
713 PyUnicode_GET_SIZE(*param));
714 else
715 *param = PyUnicode_FromEncodedObject(*param,
716 Py_FileSystemDefaultEncoding,
717 "strict");
718 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000719}
720
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000721#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722
Guido van Rossumd48f2521997-12-05 22:19:34 +0000723#if defined(PYOS_OS2)
724/**********************************************************************
725 * Helper Function to Trim and Format OS/2 Messages
726 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000727static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000728os2_formatmsg(char *msgbuf, int msglen, char *reason)
729{
730 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
731
732 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
Victor Stinner862490a2010-05-06 00:03:44 +0000733 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
Guido van Rossumd48f2521997-12-05 22:19:34 +0000734
Victor Stinner862490a2010-05-06 00:03:44 +0000735 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
736 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000737 }
738
739 /* Add Optional Reason Text */
740 if (reason) {
741 strcat(msgbuf, " : ");
742 strcat(msgbuf, reason);
743 }
744}
745
746/**********************************************************************
747 * Decode an OS/2 Operating System Error Code
748 *
749 * A convenience function to lookup an OS/2 error code and return a
750 * text message we can use to raise a Python exception.
751 *
752 * Notes:
753 * The messages for errors returned from the OS/2 kernel reside in
754 * the file OSO001.MSG in the \OS2 directory hierarchy.
755 *
756 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000757static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000758os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
759{
760 APIRET rc;
761 ULONG msglen;
762
763 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
764 Py_BEGIN_ALLOW_THREADS
765 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
766 errorcode, "oso001.msg", &msglen);
767 Py_END_ALLOW_THREADS
768
769 if (rc == NO_ERROR)
770 os2_formatmsg(msgbuf, msglen, reason);
771 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000772 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000773 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000774
775 return msgbuf;
776}
777
778/* Set an OS/2-specific error and return NULL. OS/2 kernel
779 errors are not in a global variable e.g. 'errno' nor are
780 they congruent with posix error numbers. */
781
Victor Stinnerd6f85422010-05-05 23:33:33 +0000782static PyObject *
783os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000784{
785 char text[1024];
786 PyObject *v;
787
788 os2_strerror(text, sizeof(text), code, "");
789
790 v = Py_BuildValue("(is)", code, text);
791 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000792 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000793 Py_DECREF(v);
794 }
795 return NULL; /* Signal to Python that an Exception is Pending */
796}
797
798#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000799
800/* POSIX generic methods */
801
Barry Warsaw53699e91996-12-10 23:23:01 +0000802static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000803posix_fildes(PyObject *fdobj, int (*func)(int))
804{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000805 int fd;
806 int res;
807 fd = PyObject_AsFileDescriptor(fdobj);
808 if (fd < 0)
809 return NULL;
810 if (!_PyVerify_fd(fd))
811 return posix_error();
812 Py_BEGIN_ALLOW_THREADS
813 res = (*func)(fd);
814 Py_END_ALLOW_THREADS
815 if (res < 0)
816 return posix_error();
817 Py_INCREF(Py_None);
818 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000819}
Guido van Rossum21142a01999-01-08 21:05:37 +0000820
821static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000822posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000823{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000824 char *path1 = NULL;
825 int res;
826 if (!PyArg_ParseTuple(args, format,
827 Py_FileSystemDefaultEncoding, &path1))
828 return NULL;
829 Py_BEGIN_ALLOW_THREADS
830 res = (*func)(path1);
831 Py_END_ALLOW_THREADS
832 if (res < 0)
833 return posix_error_with_allocated_filename(path1);
834 PyMem_Free(path1);
835 Py_INCREF(Py_None);
836 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837}
838
Barry Warsaw53699e91996-12-10 23:23:01 +0000839static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000840posix_2str(PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000841 char *format,
842 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000844 char *path1 = NULL, *path2 = NULL;
845 int res;
846 if (!PyArg_ParseTuple(args, format,
847 Py_FileSystemDefaultEncoding, &path1,
848 Py_FileSystemDefaultEncoding, &path2))
849 return NULL;
850 Py_BEGIN_ALLOW_THREADS
851 res = (*func)(path1, path2);
852 Py_END_ALLOW_THREADS
853 PyMem_Free(path1);
854 PyMem_Free(path2);
855 if (res != 0)
856 /* XXX how to report both path1 and path2??? */
857 return posix_error();
858 Py_INCREF(Py_None);
859 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000862#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000863static PyObject*
Victor Stinnerd6f85422010-05-05 23:33:33 +0000864win32_1str(PyObject* args, char* func,
865 char* format, BOOL (__stdcall *funcA)(LPCSTR),
866 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000867{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000868 PyObject *uni;
869 char *ansi;
870 BOOL result;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +0000871
Victor Stinnerd6f85422010-05-05 23:33:33 +0000872 if (!PyArg_ParseTuple(args, wformat, &uni))
873 PyErr_Clear();
874 else {
875 Py_BEGIN_ALLOW_THREADS
876 result = funcW(PyUnicode_AsUnicode(uni));
877 Py_END_ALLOW_THREADS
878 if (!result)
879 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
880 Py_INCREF(Py_None);
881 return Py_None;
882 }
883 if (!PyArg_ParseTuple(args, format, &ansi))
884 return NULL;
885 Py_BEGIN_ALLOW_THREADS
886 result = funcA(ansi);
887 Py_END_ALLOW_THREADS
888 if (!result)
889 return win32_error(func, ansi);
890 Py_INCREF(Py_None);
891 return Py_None;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000892
893}
894
895/* This is a reimplementation of the C library's chdir function,
896 but one that produces Win32 errors instead of DOS error codes.
897 chdir is essentially a wrapper around SetCurrentDirectory; however,
898 it also needs to set "magic" environment variables indicating
899 the per-drive current directory, which are of the form =<drive>: */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000900static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000901win32_chdir(LPCSTR path)
902{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000903 char new_path[MAX_PATH+1];
904 int result;
905 char env[4] = "=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000906
Victor Stinnerd6f85422010-05-05 23:33:33 +0000907 if(!SetCurrentDirectoryA(path))
908 return FALSE;
909 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
910 if (!result)
911 return FALSE;
912 /* In the ANSI API, there should not be any paths longer
913 than MAX_PATH. */
914 assert(result <= MAX_PATH+1);
915 if (strncmp(new_path, "\\\\", 2) == 0 ||
916 strncmp(new_path, "//", 2) == 0)
917 /* UNC path, nothing to do. */
918 return TRUE;
919 env[1] = new_path[0];
920 return SetEnvironmentVariableA(env, new_path);
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000921}
922
923/* The Unicode version differs from the ANSI version
924 since the current directory might exceed MAX_PATH characters */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000925static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000926win32_wchdir(LPCWSTR path)
927{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000928 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
929 int result;
930 wchar_t env[4] = L"=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000931
Victor Stinnerd6f85422010-05-05 23:33:33 +0000932 if(!SetCurrentDirectoryW(path))
933 return FALSE;
934 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
935 if (!result)
936 return FALSE;
937 if (result > MAX_PATH+1) {
938 new_path = malloc(result * sizeof(wchar_t));
939 if (!new_path) {
940 SetLastError(ERROR_OUTOFMEMORY);
941 return FALSE;
942 }
943 result = GetCurrentDirectoryW(result, new_path);
944 if (!result) {
945 free(new_path);
946 return FALSE;
947 }
948 }
949 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
950 wcsncmp(new_path, L"//", 2) == 0)
951 /* UNC path, nothing to do. */
952 return TRUE;
953 env[1] = new_path[0];
954 result = SetEnvironmentVariableW(env, new_path);
955 if (new_path != _new_path)
956 free(new_path);
957 return result;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000958}
959#endif
960
Antoine Pitrouff48c0a2011-07-01 22:56:03 +0200961/* choose the appropriate stat and fstat functions and return structs */
962#undef STAT
963#undef FSTAT
964#undef STRUCT_STAT
965#if defined(MS_WIN64) || defined(MS_WINDOWS)
966# define STAT win32_stat
967# define FSTAT win32_fstat
968# define STRUCT_STAT struct win32_stat
969#else
970# define STAT stat
971# define FSTAT fstat
972# define STRUCT_STAT struct stat
973#endif
974
Martin v. Löwis14694662006-02-03 12:54:16 +0000975#ifdef MS_WINDOWS
976/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
977 - time stamps are restricted to second resolution
978 - file modification times suffer from forth-and-back conversions between
979 UTC and local time
980 Therefore, we implement our own stat, based on the Win32 API directly.
981*/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000982#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000983
984struct win32_stat{
985 int st_dev;
986 __int64 st_ino;
987 unsigned short st_mode;
988 int st_nlink;
989 int st_uid;
990 int st_gid;
991 int st_rdev;
992 __int64 st_size;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000993 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 int st_atime_nsec;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000995 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000996 int st_mtime_nsec;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000997 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000998 int st_ctime_nsec;
999};
1000
1001static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1002
1003static void
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001004FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001005{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001006 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1007 /* Cannot simply cast and dereference in_ptr,
1008 since it might not be aligned properly */
1009 __int64 in;
1010 memcpy(&in, in_ptr, sizeof(in));
1011 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001012 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001013}
1014
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001015static void
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001016time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001017{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001018 /* XXX endianness */
1019 __int64 out;
1020 out = time_in + secs_between_epochs;
1021 out = out * 10000000 + nsec_in / 100;
1022 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001023}
1024
Martin v. Löwis14694662006-02-03 12:54:16 +00001025/* Below, we *know* that ugo+r is 0444 */
1026#if _S_IREAD != 0400
1027#error Unsupported C library
1028#endif
1029static int
1030attributes_to_mode(DWORD attr)
1031{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001032 int m = 0;
1033 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1034 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1035 else
1036 m |= _S_IFREG;
1037 if (attr & FILE_ATTRIBUTE_READONLY)
1038 m |= 0444;
1039 else
1040 m |= 0666;
1041 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001042}
1043
1044static int
1045attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
1046{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001047 memset(result, 0, sizeof(*result));
1048 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1049 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1050 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1051 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1052 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +00001053
Victor Stinnerd6f85422010-05-05 23:33:33 +00001054 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001055}
1056
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001057static BOOL
1058attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1059{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001060 HANDLE hFindFile;
1061 WIN32_FIND_DATAA FileData;
1062 hFindFile = FindFirstFileA(pszFile, &FileData);
1063 if (hFindFile == INVALID_HANDLE_VALUE)
1064 return FALSE;
1065 FindClose(hFindFile);
1066 pfad->dwFileAttributes = FileData.dwFileAttributes;
1067 pfad->ftCreationTime = FileData.ftCreationTime;
1068 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1069 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1070 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1071 pfad->nFileSizeLow = FileData.nFileSizeLow;
1072 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001073}
1074
1075static BOOL
1076attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1077{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001078 HANDLE hFindFile;
1079 WIN32_FIND_DATAW FileData;
1080 hFindFile = FindFirstFileW(pszFile, &FileData);
1081 if (hFindFile == INVALID_HANDLE_VALUE)
1082 return FALSE;
1083 FindClose(hFindFile);
1084 pfad->dwFileAttributes = FileData.dwFileAttributes;
1085 pfad->ftCreationTime = FileData.ftCreationTime;
1086 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1087 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1088 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1089 pfad->nFileSizeLow = FileData.nFileSizeLow;
1090 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001091}
1092
Victor Stinnerd6f85422010-05-05 23:33:33 +00001093static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001094win32_stat(const char* path, struct win32_stat *result)
1095{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001096 WIN32_FILE_ATTRIBUTE_DATA info;
1097 int code;
1098 char *dot;
1099 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1100 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1101 /* Protocol violation: we explicitly clear errno, instead of
1102 setting it to a POSIX error. Callers should use GetLastError. */
1103 errno = 0;
1104 return -1;
1105 } else {
1106 /* Could not get attributes on open file. Fall back to
1107 reading the directory. */
1108 if (!attributes_from_dir(path, &info)) {
1109 /* Very strange. This should not fail now */
1110 errno = 0;
1111 return -1;
1112 }
1113 }
1114 }
1115 code = attribute_data_to_stat(&info, result);
1116 if (code != 0)
1117 return code;
1118 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1119 dot = strrchr(path, '.');
1120 if (dot) {
1121 if (stricmp(dot, ".bat") == 0 ||
1122 stricmp(dot, ".cmd") == 0 ||
1123 stricmp(dot, ".exe") == 0 ||
1124 stricmp(dot, ".com") == 0)
1125 result->st_mode |= 0111;
1126 }
1127 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001128}
1129
Victor Stinnerd6f85422010-05-05 23:33:33 +00001130static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001131win32_wstat(const wchar_t* path, struct win32_stat *result)
1132{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001133 int code;
1134 const wchar_t *dot;
1135 WIN32_FILE_ATTRIBUTE_DATA info;
1136 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1137 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1138 /* Protocol violation: we explicitly clear errno, instead of
1139 setting it to a POSIX error. Callers should use GetLastError. */
1140 errno = 0;
1141 return -1;
1142 } else {
1143 /* Could not get attributes on open file. Fall back to
1144 reading the directory. */
1145 if (!attributes_from_dir_w(path, &info)) {
1146 /* Very strange. This should not fail now */
1147 errno = 0;
1148 return -1;
1149 }
1150 }
1151 }
1152 code = attribute_data_to_stat(&info, result);
1153 if (code < 0)
1154 return code;
1155 /* Set IFEXEC if it is an .exe, .bat, ... */
1156 dot = wcsrchr(path, '.');
1157 if (dot) {
1158 if (_wcsicmp(dot, L".bat") == 0 ||
1159 _wcsicmp(dot, L".cmd") == 0 ||
1160 _wcsicmp(dot, L".exe") == 0 ||
1161 _wcsicmp(dot, L".com") == 0)
1162 result->st_mode |= 0111;
1163 }
1164 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001165}
1166
1167static int
1168win32_fstat(int file_number, struct win32_stat *result)
1169{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001170 BY_HANDLE_FILE_INFORMATION info;
1171 HANDLE h;
1172 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001173
Victor Stinnerd6f85422010-05-05 23:33:33 +00001174 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001175
Victor Stinnerd6f85422010-05-05 23:33:33 +00001176 /* Protocol violation: we explicitly clear errno, instead of
1177 setting it to a POSIX error. Callers should use GetLastError. */
1178 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001179
Victor Stinnerd6f85422010-05-05 23:33:33 +00001180 if (h == INVALID_HANDLE_VALUE) {
1181 /* This is really a C library error (invalid file handle).
1182 We set the Win32 error to the closes one matching. */
1183 SetLastError(ERROR_INVALID_HANDLE);
1184 return -1;
1185 }
1186 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001187
Victor Stinnerd6f85422010-05-05 23:33:33 +00001188 type = GetFileType(h);
1189 if (type == FILE_TYPE_UNKNOWN) {
1190 DWORD error = GetLastError();
1191 if (error != 0) {
1192 return -1;
1193 }
1194 /* else: valid but unknown file */
1195 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001196
Victor Stinnerd6f85422010-05-05 23:33:33 +00001197 if (type != FILE_TYPE_DISK) {
1198 if (type == FILE_TYPE_CHAR)
1199 result->st_mode = _S_IFCHR;
1200 else if (type == FILE_TYPE_PIPE)
1201 result->st_mode = _S_IFIFO;
1202 return 0;
1203 }
1204
1205 if (!GetFileInformationByHandle(h, &info)) {
1206 return -1;
1207 }
1208
1209 /* similar to stat() */
1210 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1211 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1212 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1213 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1214 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1215 /* specific to fstat() */
1216 result->st_nlink = info.nNumberOfLinks;
1217 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1218 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001219}
1220
1221#endif /* MS_WINDOWS */
1222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001223PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001224"stat_result: Result from stat or lstat.\n\n\
1225This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001226 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001227or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1228\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001229Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1230or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001231\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001233
1234static PyStructSequence_Field stat_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001235 {"st_mode", "protection bits"},
1236 {"st_ino", "inode"},
1237 {"st_dev", "device"},
1238 {"st_nlink", "number of hard links"},
1239 {"st_uid", "user ID of owner"},
1240 {"st_gid", "group ID of owner"},
1241 {"st_size", "total size, in bytes"},
1242 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1243 {NULL, "integer time of last access"},
1244 {NULL, "integer time of last modification"},
1245 {NULL, "integer time of last change"},
1246 {"st_atime", "time of last access"},
1247 {"st_mtime", "time of last modification"},
1248 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001249#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001250 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001251#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001252#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001253 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001254#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001255#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001256 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001257#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001258#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001259 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001260#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001261#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001262 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001263#endif
1264#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001265 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001266#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001267 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001268};
1269
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001270#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001271#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001272#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001273#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001274#endif
1275
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001276#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001277#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1278#else
1279#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1280#endif
1281
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001282#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001283#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1284#else
1285#define ST_RDEV_IDX ST_BLOCKS_IDX
1286#endif
1287
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001288#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1289#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1290#else
1291#define ST_FLAGS_IDX ST_RDEV_IDX
1292#endif
1293
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001294#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001295#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001296#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001297#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001298#endif
1299
1300#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1301#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1302#else
1303#define ST_BIRTHTIME_IDX ST_GEN_IDX
1304#endif
1305
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306static PyStructSequence_Desc stat_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001307 "stat_result", /* name */
1308 stat_result__doc__, /* doc */
1309 stat_result_fields,
1310 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001311};
1312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1315This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001316 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001317or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001318\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001319See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001320
1321static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001322 {"f_bsize", },
1323 {"f_frsize", },
1324 {"f_blocks", },
1325 {"f_bfree", },
1326 {"f_bavail", },
1327 {"f_files", },
1328 {"f_ffree", },
1329 {"f_favail", },
1330 {"f_flag", },
1331 {"f_namemax",},
1332 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001333};
1334
1335static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001336 "statvfs_result", /* name */
1337 statvfs_result__doc__, /* doc */
1338 statvfs_result_fields,
1339 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340};
1341
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001342static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001343static PyTypeObject StatResultType;
1344static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001345static newfunc structseq_new;
1346
1347static PyObject *
1348statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1349{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001350 PyStructSequence *result;
1351 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001352
Victor Stinnerd6f85422010-05-05 23:33:33 +00001353 result = (PyStructSequence*)structseq_new(type, args, kwds);
1354 if (!result)
1355 return NULL;
1356 /* If we have been initialized from a tuple,
1357 st_?time might be set to None. Initialize it
1358 from the int slots. */
1359 for (i = 7; i <= 9; i++) {
1360 if (result->ob_item[i+3] == Py_None) {
1361 Py_DECREF(Py_None);
1362 Py_INCREF(result->ob_item[i]);
1363 result->ob_item[i+3] = result->ob_item[i];
1364 }
1365 }
1366 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001367}
1368
1369
1370
1371/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001372static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001373
1374PyDoc_STRVAR(stat_float_times__doc__,
1375"stat_float_times([newval]) -> oldval\n\n\
1376Determine whether os.[lf]stat represents time stamps as float objects.\n\
1377If newval is True, future calls to stat() return floats, if it is False,\n\
1378future calls return ints. \n\
1379If newval is omitted, return the current setting.\n");
1380
1381static PyObject*
1382stat_float_times(PyObject* self, PyObject *args)
1383{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001384 int newval = -1;
1385 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1386 return NULL;
1387 if (newval == -1)
1388 /* Return old value */
1389 return PyBool_FromLong(_stat_float_times);
1390 _stat_float_times = newval;
1391 Py_INCREF(Py_None);
1392 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001393}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001394
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001395static void
1396fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1397{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001398 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001399#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinnerd6f85422010-05-05 23:33:33 +00001400 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001401#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001402 ival = PyInt_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001403#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001404 if (!ival)
1405 return;
1406 if (_stat_float_times) {
1407 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1408 } else {
1409 fval = ival;
1410 Py_INCREF(fval);
1411 }
1412 PyStructSequence_SET_ITEM(v, index, ival);
1413 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001414}
1415
Tim Peters5aa91602002-01-30 05:46:57 +00001416/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001417 (used by posix_stat() and posix_fstat()) */
1418static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001419_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001420{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001421 unsigned long ansec, mnsec, cnsec;
1422 PyObject *v = PyStructSequence_New(&StatResultType);
1423 if (v == NULL)
1424 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001425
Victor Stinnerd6f85422010-05-05 23:33:33 +00001426 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001427#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001428 PyStructSequence_SET_ITEM(v, 1,
1429 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001430#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001431 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001432#endif
1433#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001434 PyStructSequence_SET_ITEM(v, 2,
1435 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001436#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001437 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001438#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001439 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02001440#if defined(MS_WINDOWS)
1441 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong(0));
1442 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong(0));
1443#else
1444 PyStructSequence_SET_ITEM(v, 4, _PyInt_FromUid(st->st_uid));
1445 PyStructSequence_SET_ITEM(v, 5, _PyInt_FromGid(st->st_gid));
1446#endif
Fred Drake699f3522000-06-29 21:12:41 +00001447#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001448 PyStructSequence_SET_ITEM(v, 6,
1449 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001450#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001451 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001452#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001453
Martin v. Löwis14694662006-02-03 12:54:16 +00001454#if defined(HAVE_STAT_TV_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001455 ansec = st->st_atim.tv_nsec;
1456 mnsec = st->st_mtim.tv_nsec;
1457 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001458#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001459 ansec = st->st_atimespec.tv_nsec;
1460 mnsec = st->st_mtimespec.tv_nsec;
1461 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001462#elif defined(HAVE_STAT_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001463 ansec = st->st_atime_nsec;
1464 mnsec = st->st_mtime_nsec;
1465 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001466#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001467 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001468#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001469 fill_time(v, 7, st->st_atime, ansec);
1470 fill_time(v, 8, st->st_mtime, mnsec);
1471 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001472
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001473#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001474 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1475 PyInt_FromLong((long)st->st_blksize));
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_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001478 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1479 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001480#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001481#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001482 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1483 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001484#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001485#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001486 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1487 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001488#endif
1489#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001490 {
1491 PyObject *val;
1492 unsigned long bsec,bnsec;
1493 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001494#ifdef HAVE_STAT_TV_NSEC2
Victor Stinnerd6f85422010-05-05 23:33:33 +00001495 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001496#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001497 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001498#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001499 if (_stat_float_times) {
1500 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1501 } else {
1502 val = PyInt_FromLong((long)bsec);
1503 }
1504 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1505 val);
1506 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001507#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001508#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001509 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1510 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001511#endif
Fred Drake699f3522000-06-29 21:12:41 +00001512
Victor Stinnerd6f85422010-05-05 23:33:33 +00001513 if (PyErr_Occurred()) {
1514 Py_DECREF(v);
1515 return NULL;
1516 }
Fred Drake699f3522000-06-29 21:12:41 +00001517
Victor Stinnerd6f85422010-05-05 23:33:33 +00001518 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001519}
1520
Martin v. Löwisd8948722004-06-02 09:57:56 +00001521#ifdef MS_WINDOWS
1522
1523/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1524 where / can be used in place of \ and the trailing slash is optional.
1525 Both SERVER and SHARE must have at least one character.
1526*/
1527
1528#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1529#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001530#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001531#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001532#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001533
Tim Peters4ad82172004-08-30 17:02:04 +00001534static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001535IsUNCRootA(char *path, int pathlen)
1536{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001537 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001538
Victor Stinnerd6f85422010-05-05 23:33:33 +00001539 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001540
Victor Stinnerd6f85422010-05-05 23:33:33 +00001541 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1542 /* minimum UNCRoot is \\x\y */
1543 return FALSE;
1544 for (i = 2; i < pathlen ; i++)
1545 if (ISSLASH(path[i])) break;
1546 if (i == 2 || i == pathlen)
1547 /* do not allow \\\SHARE or \\SERVER */
1548 return FALSE;
1549 share = i+1;
1550 for (i = share; i < pathlen; i++)
1551 if (ISSLASH(path[i])) break;
1552 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001553
Victor Stinnerd6f85422010-05-05 23:33:33 +00001554 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001555}
1556
Tim Peters4ad82172004-08-30 17:02:04 +00001557static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001558IsUNCRootW(Py_UNICODE *path, int pathlen)
1559{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001560 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001561
Victor Stinnerd6f85422010-05-05 23:33:33 +00001562 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001563
Victor Stinnerd6f85422010-05-05 23:33:33 +00001564 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1565 /* minimum UNCRoot is \\x\y */
1566 return FALSE;
1567 for (i = 2; i < pathlen ; i++)
1568 if (ISSLASH(path[i])) break;
1569 if (i == 2 || i == pathlen)
1570 /* do not allow \\\SHARE or \\SERVER */
1571 return FALSE;
1572 share = i+1;
1573 for (i = share; i < pathlen; i++)
1574 if (ISSLASH(path[i])) break;
1575 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001576
Victor Stinnerd6f85422010-05-05 23:33:33 +00001577 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001578}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001579#endif /* MS_WINDOWS */
1580
Barry Warsaw53699e91996-12-10 23:23:01 +00001581static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001582posix_do_stat(PyObject *self, PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +00001583 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001584#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001585 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001586#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001587 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001588#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001589 char *wformat,
1590 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001591{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001592 STRUCT_STAT st;
1593 char *path = NULL; /* pass this to stat; do not free() it */
1594 char *pathfree = NULL; /* this memory must be free'd */
1595 int res;
1596 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001597
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001598#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001599 PyUnicodeObject *po;
1600 if (PyArg_ParseTuple(args, wformat, &po)) {
1601 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001602
Victor Stinnerd6f85422010-05-05 23:33:33 +00001603 Py_BEGIN_ALLOW_THREADS
1604 /* PyUnicode_AS_UNICODE result OK without
1605 thread lock as it is a simple dereference. */
1606 res = wstatfunc(wpath, &st);
1607 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001608
Victor Stinnerd6f85422010-05-05 23:33:33 +00001609 if (res != 0)
1610 return win32_error_unicode("stat", wpath);
1611 return _pystat_fromstructstat(&st);
1612 }
1613 /* Drop the argument parsing error as narrow strings
1614 are also valid. */
1615 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001616#endif
1617
Victor Stinnerd6f85422010-05-05 23:33:33 +00001618 if (!PyArg_ParseTuple(args, format,
1619 Py_FileSystemDefaultEncoding, &path))
1620 return NULL;
1621 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001622
Victor Stinnerd6f85422010-05-05 23:33:33 +00001623 Py_BEGIN_ALLOW_THREADS
1624 res = (*statfunc)(path, &st);
1625 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001626
Victor Stinnerd6f85422010-05-05 23:33:33 +00001627 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001628#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001629 result = win32_error("stat", pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001630#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001631 result = posix_error_with_filename(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001632#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001633 }
1634 else
1635 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001636
Victor Stinnerd6f85422010-05-05 23:33:33 +00001637 PyMem_Free(pathfree);
1638 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001639}
1640
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001641/* POSIX methods */
1642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001644"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001645Use the real uid/gid to test for access to a path. Note that most\n\
1646operations will use the effective uid/gid, therefore this routine can\n\
1647be used in a suid/sgid environment to test if the invoking user has the\n\
1648specified access to the path. The mode argument can be F_OK to test\n\
1649existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001650
1651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001652posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001653{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001654 char *path;
1655 int mode;
1656
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001657#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001658 DWORD attr;
1659 PyUnicodeObject *po;
1660 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1661 Py_BEGIN_ALLOW_THREADS
1662 /* PyUnicode_AS_UNICODE OK without thread lock as
1663 it is a simple dereference. */
1664 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1665 Py_END_ALLOW_THREADS
1666 goto finish;
1667 }
1668 /* Drop the argument parsing error as narrow strings
1669 are also valid. */
1670 PyErr_Clear();
1671 if (!PyArg_ParseTuple(args, "eti:access",
1672 Py_FileSystemDefaultEncoding, &path, &mode))
1673 return NULL;
1674 Py_BEGIN_ALLOW_THREADS
1675 attr = GetFileAttributesA(path);
1676 Py_END_ALLOW_THREADS
1677 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001678finish:
Victor Stinnerd6f85422010-05-05 23:33:33 +00001679 if (attr == 0xFFFFFFFF)
1680 /* File does not exist, or cannot read attributes */
1681 return PyBool_FromLong(0);
1682 /* Access is possible if either write access wasn't requested, or
1683 the file isn't read-only, or if it's a directory, as there are
1684 no read-only directories on Windows. */
1685 return PyBool_FromLong(!(mode & 2)
1686 || !(attr & FILE_ATTRIBUTE_READONLY)
1687 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001688#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001689 int res;
1690 if (!PyArg_ParseTuple(args, "eti:access",
1691 Py_FileSystemDefaultEncoding, &path, &mode))
1692 return NULL;
1693 Py_BEGIN_ALLOW_THREADS
1694 res = access(path, mode);
1695 Py_END_ALLOW_THREADS
1696 PyMem_Free(path);
1697 return PyBool_FromLong(res == 0);
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001698#endif /* MS_WINDOWS */
Guido van Rossum94f6f721999-01-06 18:42:14 +00001699}
1700
Guido van Rossumd371ff11999-01-25 16:12:23 +00001701#ifndef F_OK
1702#define F_OK 0
1703#endif
1704#ifndef R_OK
1705#define R_OK 4
1706#endif
1707#ifndef W_OK
1708#define W_OK 2
1709#endif
1710#ifndef X_OK
1711#define X_OK 1
1712#endif
1713
1714#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001715PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001716"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001718
1719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001720posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001721{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001722 int id;
1723 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001724
Victor Stinnerd6f85422010-05-05 23:33:33 +00001725 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1726 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001727
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001728#if defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001729 /* file descriptor 0 only, the default input device (stdin) */
1730 if (id == 0) {
1731 ret = ttyname();
1732 }
1733 else {
1734 ret = NULL;
1735 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001736#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001737 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001738#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001739 if (ret == NULL)
1740 return posix_error();
1741 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001742}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001743#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001744
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001745#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001746PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001747"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001748Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001749
1750static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001751posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001752{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001753 char *ret;
1754 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001755
Greg Wardb48bc172000-03-01 21:51:56 +00001756#ifdef USE_CTERMID_R
Victor Stinnerd6f85422010-05-05 23:33:33 +00001757 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001758#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001759 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001760#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001761 if (ret == NULL)
1762 return posix_error();
1763 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001764}
1765#endif
1766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001767PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001768"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001769Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001770
Barry Warsaw53699e91996-12-10 23:23:01 +00001771static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001772posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001773{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001774#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001775 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001776#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001777 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001778#elif defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001779 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001780#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001781 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001782#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001783}
1784
Fred Drake4d1e64b2002-04-15 19:40:07 +00001785#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001786PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001787"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001788Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001790
1791static PyObject *
1792posix_fchdir(PyObject *self, PyObject *fdobj)
1793{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001794 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001795}
1796#endif /* HAVE_FCHDIR */
1797
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001802
Barry Warsaw53699e91996-12-10 23:23:01 +00001803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001804posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001806 char *path = NULL;
1807 int i;
1808 int res;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001809#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001810 DWORD attr;
1811 PyUnicodeObject *po;
1812 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1813 Py_BEGIN_ALLOW_THREADS
1814 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1815 if (attr != 0xFFFFFFFF) {
1816 if (i & _S_IWRITE)
1817 attr &= ~FILE_ATTRIBUTE_READONLY;
1818 else
1819 attr |= FILE_ATTRIBUTE_READONLY;
1820 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1821 }
1822 else
1823 res = 0;
1824 Py_END_ALLOW_THREADS
1825 if (!res)
1826 return win32_error_unicode("chmod",
1827 PyUnicode_AS_UNICODE(po));
1828 Py_INCREF(Py_None);
1829 return Py_None;
1830 }
1831 /* Drop the argument parsing error as narrow strings
1832 are also valid. */
1833 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00001834
Victor Stinnerd6f85422010-05-05 23:33:33 +00001835 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1836 &path, &i))
1837 return NULL;
1838 Py_BEGIN_ALLOW_THREADS
1839 attr = GetFileAttributesA(path);
1840 if (attr != 0xFFFFFFFF) {
1841 if (i & _S_IWRITE)
1842 attr &= ~FILE_ATTRIBUTE_READONLY;
1843 else
1844 attr |= FILE_ATTRIBUTE_READONLY;
1845 res = SetFileAttributesA(path, attr);
1846 }
1847 else
1848 res = 0;
1849 Py_END_ALLOW_THREADS
1850 if (!res) {
1851 win32_error("chmod", path);
1852 PyMem_Free(path);
1853 return NULL;
1854 }
1855 PyMem_Free(path);
1856 Py_INCREF(Py_None);
1857 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001858#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001859 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1860 &path, &i))
1861 return NULL;
1862 Py_BEGIN_ALLOW_THREADS
1863 res = chmod(path, i);
1864 Py_END_ALLOW_THREADS
1865 if (res < 0)
1866 return posix_error_with_allocated_filename(path);
1867 PyMem_Free(path);
1868 Py_INCREF(Py_None);
1869 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001870#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001871}
1872
Christian Heimes36281872007-11-30 21:11:28 +00001873#ifdef HAVE_FCHMOD
1874PyDoc_STRVAR(posix_fchmod__doc__,
1875"fchmod(fd, mode)\n\n\
1876Change the access permissions of the file given by file\n\
1877descriptor fd.");
1878
1879static PyObject *
1880posix_fchmod(PyObject *self, PyObject *args)
1881{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001882 int fd, mode, res;
1883 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1884 return NULL;
1885 Py_BEGIN_ALLOW_THREADS
1886 res = fchmod(fd, mode);
1887 Py_END_ALLOW_THREADS
1888 if (res < 0)
1889 return posix_error();
1890 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001891}
1892#endif /* HAVE_FCHMOD */
1893
1894#ifdef HAVE_LCHMOD
1895PyDoc_STRVAR(posix_lchmod__doc__,
1896"lchmod(path, mode)\n\n\
1897Change the access permissions of a file. If path is a symlink, this\n\
1898affects the link itself rather than the target.");
1899
1900static PyObject *
1901posix_lchmod(PyObject *self, PyObject *args)
1902{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001903 char *path = NULL;
1904 int i;
1905 int res;
1906 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1907 &path, &i))
1908 return NULL;
1909 Py_BEGIN_ALLOW_THREADS
1910 res = lchmod(path, i);
1911 Py_END_ALLOW_THREADS
1912 if (res < 0)
1913 return posix_error_with_allocated_filename(path);
1914 PyMem_Free(path);
1915 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001916}
1917#endif /* HAVE_LCHMOD */
1918
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001919
Martin v. Löwis382abef2007-02-19 10:55:19 +00001920#ifdef HAVE_CHFLAGS
1921PyDoc_STRVAR(posix_chflags__doc__,
1922"chflags(path, flags)\n\n\
1923Set file flags.");
1924
1925static PyObject *
1926posix_chflags(PyObject *self, PyObject *args)
1927{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001928 char *path;
1929 unsigned long flags;
1930 int res;
1931 if (!PyArg_ParseTuple(args, "etk:chflags",
1932 Py_FileSystemDefaultEncoding, &path, &flags))
1933 return NULL;
1934 Py_BEGIN_ALLOW_THREADS
1935 res = chflags(path, flags);
1936 Py_END_ALLOW_THREADS
1937 if (res < 0)
1938 return posix_error_with_allocated_filename(path);
1939 PyMem_Free(path);
1940 Py_INCREF(Py_None);
1941 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001942}
1943#endif /* HAVE_CHFLAGS */
1944
1945#ifdef HAVE_LCHFLAGS
1946PyDoc_STRVAR(posix_lchflags__doc__,
1947"lchflags(path, flags)\n\n\
1948Set file flags.\n\
1949This function will not follow symbolic links.");
1950
1951static PyObject *
1952posix_lchflags(PyObject *self, PyObject *args)
1953{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001954 char *path;
1955 unsigned long flags;
1956 int res;
1957 if (!PyArg_ParseTuple(args, "etk:lchflags",
1958 Py_FileSystemDefaultEncoding, &path, &flags))
1959 return NULL;
1960 Py_BEGIN_ALLOW_THREADS
1961 res = lchflags(path, flags);
1962 Py_END_ALLOW_THREADS
1963 if (res < 0)
1964 return posix_error_with_allocated_filename(path);
1965 PyMem_Free(path);
1966 Py_INCREF(Py_None);
1967 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001968}
1969#endif /* HAVE_LCHFLAGS */
1970
Martin v. Löwis244edc82001-10-04 22:44:26 +00001971#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001973"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001975
1976static PyObject *
1977posix_chroot(PyObject *self, PyObject *args)
1978{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001979 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001980}
1981#endif
1982
Guido van Rossum21142a01999-01-08 21:05:37 +00001983#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001984PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001985"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001987
1988static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001989posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001990{
Stefan Krah93f7a322010-11-26 17:35:50 +00001991 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001992}
1993#endif /* HAVE_FSYNC */
1994
1995#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001996
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001997#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001998extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1999#endif
2000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002001PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002002"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002003force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002005
2006static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002007posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002008{
Stefan Krah93f7a322010-11-26 17:35:50 +00002009 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002010}
2011#endif /* HAVE_FDATASYNC */
2012
2013
Fredrik Lundh10723342000-07-10 16:38:09 +00002014#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002016"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002017Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002018
Barry Warsaw53699e91996-12-10 23:23:01 +00002019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002020posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002021{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002022 char *path = NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002023 uid_t uid;
2024 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002025 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002026 if (!PyArg_ParseTuple(args, "etO&O&:chown",
Victor Stinnerd6f85422010-05-05 23:33:33 +00002027 Py_FileSystemDefaultEncoding, &path,
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002028 _Py_Uid_Converter, &uid,
2029 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002030 return NULL;
2031 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002032 res = chown(path, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002033 Py_END_ALLOW_THREADS
2034 if (res < 0)
2035 return posix_error_with_allocated_filename(path);
2036 PyMem_Free(path);
2037 Py_INCREF(Py_None);
2038 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002039}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002040#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002041
Christian Heimes36281872007-11-30 21:11:28 +00002042#ifdef HAVE_FCHOWN
2043PyDoc_STRVAR(posix_fchown__doc__,
2044"fchown(fd, uid, gid)\n\n\
2045Change the owner and group id of the file given by file descriptor\n\
2046fd to the numeric uid and gid.");
2047
2048static PyObject *
2049posix_fchown(PyObject *self, PyObject *args)
2050{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002051 int fd;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002052 uid_t uid;
2053 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002054 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002055 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd,
2056 _Py_Uid_Converter, &uid,
2057 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002058 return NULL;
2059 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002060 res = fchown(fd, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002061 Py_END_ALLOW_THREADS
2062 if (res < 0)
2063 return posix_error();
2064 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00002065}
2066#endif /* HAVE_FCHOWN */
2067
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002068#ifdef HAVE_LCHOWN
2069PyDoc_STRVAR(posix_lchown__doc__,
2070"lchown(path, uid, gid)\n\n\
2071Change the owner and group id of path to the numeric uid and gid.\n\
2072This function will not follow symbolic links.");
2073
2074static PyObject *
2075posix_lchown(PyObject *self, PyObject *args)
2076{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002077 char *path = NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002078 uid_t uid;
2079 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002080 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002081 if (!PyArg_ParseTuple(args, "etO&O&:lchown",
Victor Stinnerd6f85422010-05-05 23:33:33 +00002082 Py_FileSystemDefaultEncoding, &path,
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002083 _Py_Uid_Converter, &uid,
2084 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002085 return NULL;
2086 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002087 res = lchown(path, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002088 Py_END_ALLOW_THREADS
2089 if (res < 0)
2090 return posix_error_with_allocated_filename(path);
2091 PyMem_Free(path);
2092 Py_INCREF(Py_None);
2093 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002094}
2095#endif /* HAVE_LCHOWN */
2096
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002097
Guido van Rossum36bc6801995-06-14 22:54:23 +00002098#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002099PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002100"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002101Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002102
Trent Nelsonda4277a2012-08-29 09:20:41 -04002103#if (defined(__sun) && defined(__SVR4)) || \
2104 defined(__OpenBSD__) || \
2105 defined(__NetBSD__)
Stefan Krah182ae642010-07-13 19:17:08 +00002106/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
2107static PyObject *
2108posix_getcwd(PyObject *self, PyObject *noargs)
2109{
2110 char buf[PATH_MAX+2];
2111 char *res;
2112
2113 Py_BEGIN_ALLOW_THREADS
Stefan Krah8cb9f032010-07-13 19:40:00 +00002114 res = getcwd(buf, sizeof buf);
Stefan Krah182ae642010-07-13 19:17:08 +00002115 Py_END_ALLOW_THREADS
2116
2117 if (res == NULL)
2118 return posix_error();
2119
2120 return PyString_FromString(buf);
2121}
2122#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002123static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002124posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002125{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002126 int bufsize_incr = 1024;
2127 int bufsize = 0;
2128 char *tmpbuf = NULL;
2129 char *res = NULL;
2130 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00002131
Victor Stinnerd6f85422010-05-05 23:33:33 +00002132 Py_BEGIN_ALLOW_THREADS
2133 do {
2134 bufsize = bufsize + bufsize_incr;
2135 tmpbuf = malloc(bufsize);
2136 if (tmpbuf == NULL) {
2137 break;
2138 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002139#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002140 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002141#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002142 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002143#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00002144
Victor Stinnerd6f85422010-05-05 23:33:33 +00002145 if (res == NULL) {
2146 free(tmpbuf);
2147 }
2148 } while ((res == NULL) && (errno == ERANGE));
2149 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002150
Victor Stinnerd6f85422010-05-05 23:33:33 +00002151 if (res == NULL)
2152 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002153
Victor Stinnerd6f85422010-05-05 23:33:33 +00002154 dynamic_return = PyString_FromString(tmpbuf);
2155 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002156
Victor Stinnerd6f85422010-05-05 23:33:33 +00002157 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002158}
Stefan Krah182ae642010-07-13 19:17:08 +00002159#endif /* getcwd() NULL/ERANGE workaround. */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002160
Walter Dörwald3b918c32002-11-21 20:18:46 +00002161#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002162PyDoc_STRVAR(posix_getcwdu__doc__,
2163"getcwdu() -> path\n\n\
2164Return a unicode string representing the current working directory.");
2165
2166static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002167posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002168{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002169 char buf[1026];
2170 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002171
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002172#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002173 DWORD len;
2174 wchar_t wbuf[1026];
2175 wchar_t *wbuf2 = wbuf;
2176 PyObject *resobj;
2177 Py_BEGIN_ALLOW_THREADS
2178 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2179 /* If the buffer is large enough, len does not include the
2180 terminating \0. If the buffer is too small, len includes
2181 the space needed for the terminator. */
2182 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2183 wbuf2 = malloc(len * sizeof(wchar_t));
2184 if (wbuf2)
2185 len = GetCurrentDirectoryW(len, wbuf2);
2186 }
2187 Py_END_ALLOW_THREADS
2188 if (!wbuf2) {
2189 PyErr_NoMemory();
2190 return NULL;
2191 }
2192 if (!len) {
2193 if (wbuf2 != wbuf) free(wbuf2);
2194 return win32_error("getcwdu", NULL);
2195 }
2196 resobj = PyUnicode_FromWideChar(wbuf2, len);
2197 if (wbuf2 != wbuf) free(wbuf2);
2198 return resobj;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002199#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002200
Victor Stinnerd6f85422010-05-05 23:33:33 +00002201 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002202#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002203 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002204#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002205 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002206#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002207 Py_END_ALLOW_THREADS
2208 if (res == NULL)
2209 return posix_error();
2210 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002211}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002212#endif /* Py_USING_UNICODE */
2213#endif /* HAVE_GETCWD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002214
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002215
Guido van Rossumb6775db1994-08-01 11:34:53 +00002216#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002217PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002218"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002219Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002220
Barry Warsaw53699e91996-12-10 23:23:01 +00002221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002222posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002224 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002225}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002226#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002227
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002229PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002230"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002231Return a list containing the names of the entries in the directory.\n\
2232\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00002233 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002234\n\
2235The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002236entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002237
Barry Warsaw53699e91996-12-10 23:23:01 +00002238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002239posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002240{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002241 /* XXX Should redo this putting the (now four) versions of opendir
2242 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002243#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002244
Victor Stinnerd6f85422010-05-05 23:33:33 +00002245 PyObject *d, *v;
2246 HANDLE hFindFile;
2247 BOOL result;
2248 WIN32_FIND_DATA FileData;
2249 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2250 char *bufptr = namebuf;
2251 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002252
Victor Stinnerd6f85422010-05-05 23:33:33 +00002253 PyObject *po;
2254 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2255 WIN32_FIND_DATAW wFileData;
2256 Py_UNICODE *wnamebuf;
2257 /* Overallocate for \\*.*\0 */
2258 len = PyUnicode_GET_SIZE(po);
2259 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2260 if (!wnamebuf) {
2261 PyErr_NoMemory();
2262 return NULL;
2263 }
2264 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2265 if (len > 0) {
2266 Py_UNICODE wch = wnamebuf[len-1];
2267 if (wch != L'/' && wch != L'\\' && wch != L':')
2268 wnamebuf[len++] = L'\\';
2269 wcscpy(wnamebuf + len, L"*.*");
2270 }
2271 if ((d = PyList_New(0)) == NULL) {
2272 free(wnamebuf);
2273 return NULL;
2274 }
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002275 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002276 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002277 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002278 if (hFindFile == INVALID_HANDLE_VALUE) {
2279 int error = GetLastError();
2280 if (error == ERROR_FILE_NOT_FOUND) {
2281 free(wnamebuf);
2282 return d;
2283 }
2284 Py_DECREF(d);
2285 win32_error_unicode("FindFirstFileW", wnamebuf);
2286 free(wnamebuf);
2287 return NULL;
2288 }
2289 do {
2290 /* Skip over . and .. */
2291 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2292 wcscmp(wFileData.cFileName, L"..") != 0) {
2293 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2294 if (v == NULL) {
2295 Py_DECREF(d);
2296 d = NULL;
2297 break;
2298 }
2299 if (PyList_Append(d, v) != 0) {
2300 Py_DECREF(v);
2301 Py_DECREF(d);
2302 d = NULL;
2303 break;
2304 }
2305 Py_DECREF(v);
2306 }
2307 Py_BEGIN_ALLOW_THREADS
2308 result = FindNextFileW(hFindFile, &wFileData);
2309 Py_END_ALLOW_THREADS
2310 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2311 it got to the end of the directory. */
2312 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2313 Py_DECREF(d);
2314 win32_error_unicode("FindNextFileW", wnamebuf);
2315 FindClose(hFindFile);
2316 free(wnamebuf);
2317 return NULL;
2318 }
2319 } while (result == TRUE);
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002320
Victor Stinnerd6f85422010-05-05 23:33:33 +00002321 if (FindClose(hFindFile) == FALSE) {
2322 Py_DECREF(d);
2323 win32_error_unicode("FindClose", wnamebuf);
2324 free(wnamebuf);
2325 return NULL;
2326 }
2327 free(wnamebuf);
2328 return d;
2329 }
2330 /* Drop the argument parsing error as narrow strings
2331 are also valid. */
2332 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002333
Victor Stinnerd6f85422010-05-05 23:33:33 +00002334 if (!PyArg_ParseTuple(args, "et#:listdir",
2335 Py_FileSystemDefaultEncoding, &bufptr, &len))
2336 return NULL;
2337 if (len > 0) {
2338 char ch = namebuf[len-1];
2339 if (ch != SEP && ch != ALTSEP && ch != ':')
2340 namebuf[len++] = '/';
2341 strcpy(namebuf + len, "*.*");
2342 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002343
Victor Stinnerd6f85422010-05-05 23:33:33 +00002344 if ((d = PyList_New(0)) == NULL)
2345 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002346
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002347 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002348 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002349 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002350 if (hFindFile == INVALID_HANDLE_VALUE) {
2351 int error = GetLastError();
2352 if (error == ERROR_FILE_NOT_FOUND)
2353 return d;
2354 Py_DECREF(d);
2355 return win32_error("FindFirstFile", namebuf);
2356 }
2357 do {
2358 /* Skip over . and .. */
2359 if (strcmp(FileData.cFileName, ".") != 0 &&
2360 strcmp(FileData.cFileName, "..") != 0) {
2361 v = PyString_FromString(FileData.cFileName);
2362 if (v == NULL) {
2363 Py_DECREF(d);
2364 d = NULL;
2365 break;
2366 }
2367 if (PyList_Append(d, v) != 0) {
2368 Py_DECREF(v);
2369 Py_DECREF(d);
2370 d = NULL;
2371 break;
2372 }
2373 Py_DECREF(v);
2374 }
2375 Py_BEGIN_ALLOW_THREADS
2376 result = FindNextFile(hFindFile, &FileData);
2377 Py_END_ALLOW_THREADS
2378 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2379 it got to the end of the directory. */
2380 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2381 Py_DECREF(d);
2382 win32_error("FindNextFile", namebuf);
2383 FindClose(hFindFile);
2384 return NULL;
2385 }
2386 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002387
Victor Stinnerd6f85422010-05-05 23:33:33 +00002388 if (FindClose(hFindFile) == FALSE) {
2389 Py_DECREF(d);
2390 return win32_error("FindClose", namebuf);
2391 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002392
Victor Stinnerd6f85422010-05-05 23:33:33 +00002393 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002394
Tim Peters0bb44a42000-09-15 07:44:49 +00002395#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002396
2397#ifndef MAX_PATH
2398#define MAX_PATH CCHMAXPATH
2399#endif
2400 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002401 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002402 PyObject *d, *v;
2403 char namebuf[MAX_PATH+5];
2404 HDIR hdir = 1;
2405 ULONG srchcnt = 1;
2406 FILEFINDBUF3 ep;
2407 APIRET rc;
2408
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002409 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002410 return NULL;
2411 if (len >= MAX_PATH) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002412 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002413 return NULL;
2414 }
2415 strcpy(namebuf, name);
2416 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002417 if (*pt == ALTSEP)
2418 *pt = SEP;
2419 if (namebuf[len-1] != SEP)
2420 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002421 strcpy(namebuf + len, "*.*");
2422
Victor Stinnerd6f85422010-05-05 23:33:33 +00002423 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002424 return NULL;
2425
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002426 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2427 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002428 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002429 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2430 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2431 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002432
2433 if (rc != NO_ERROR) {
2434 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002435 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002436 }
2437
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002438 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002439 do {
2440 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002441 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002442 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002443
2444 strcpy(namebuf, ep.achName);
2445
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002446 /* Leave Case of Name Alone -- In Native Form */
2447 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002448
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002449 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002450 if (v == NULL) {
2451 Py_DECREF(d);
2452 d = NULL;
2453 break;
2454 }
2455 if (PyList_Append(d, v) != 0) {
2456 Py_DECREF(v);
2457 Py_DECREF(d);
2458 d = NULL;
2459 break;
2460 }
2461 Py_DECREF(v);
2462 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2463 }
2464
2465 return d;
2466#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002467
Victor Stinnerd6f85422010-05-05 23:33:33 +00002468 char *name = NULL;
2469 PyObject *d, *v;
2470 DIR *dirp;
2471 struct dirent *ep;
2472 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002473
Victor Stinnerd6f85422010-05-05 23:33:33 +00002474 errno = 0;
2475 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2476 arg_is_unicode = 0;
2477 PyErr_Clear();
2478 }
2479 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2480 return NULL;
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002481 Py_BEGIN_ALLOW_THREADS
2482 dirp = opendir(name);
2483 Py_END_ALLOW_THREADS
2484 if (dirp == NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002485 return posix_error_with_allocated_filename(name);
2486 }
2487 if ((d = PyList_New(0)) == NULL) {
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002488 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002489 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002490 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002491 PyMem_Free(name);
2492 return NULL;
2493 }
2494 for (;;) {
2495 errno = 0;
2496 Py_BEGIN_ALLOW_THREADS
2497 ep = readdir(dirp);
2498 Py_END_ALLOW_THREADS
2499 if (ep == NULL) {
2500 if (errno == 0) {
2501 break;
2502 } else {
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002503 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002504 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002505 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002506 Py_DECREF(d);
2507 return posix_error_with_allocated_filename(name);
2508 }
2509 }
2510 if (ep->d_name[0] == '.' &&
2511 (NAMLEN(ep) == 1 ||
2512 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2513 continue;
2514 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2515 if (v == NULL) {
2516 Py_DECREF(d);
2517 d = NULL;
2518 break;
2519 }
Just van Rossum46c97842003-02-25 21:42:15 +00002520#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00002521 if (arg_is_unicode) {
2522 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002523
Victor Stinnerd6f85422010-05-05 23:33:33 +00002524 w = PyUnicode_FromEncodedObject(v,
2525 Py_FileSystemDefaultEncoding,
2526 "strict");
2527 if (w != NULL) {
2528 Py_DECREF(v);
2529 v = w;
2530 }
2531 else {
2532 /* fall back to the original byte string, as
2533 discussed in patch #683592 */
2534 PyErr_Clear();
2535 }
2536 }
Just van Rossum46c97842003-02-25 21:42:15 +00002537#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002538 if (PyList_Append(d, v) != 0) {
2539 Py_DECREF(v);
2540 Py_DECREF(d);
2541 d = NULL;
2542 break;
2543 }
2544 Py_DECREF(v);
2545 }
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002546 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002547 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002548 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002549 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002550
Victor Stinnerd6f85422010-05-05 23:33:33 +00002551 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002552
Tim Peters0bb44a42000-09-15 07:44:49 +00002553#endif /* which OS */
2554} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002555
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002556#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002557/* A helper function for abspath on win32 */
2558static PyObject *
2559posix__getfullpathname(PyObject *self, PyObject *args)
2560{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002561 /* assume encoded strings won't more than double no of chars */
2562 char inbuf[MAX_PATH*2];
2563 char *inbufp = inbuf;
2564 Py_ssize_t insize = sizeof(inbuf);
2565 char outbuf[MAX_PATH*2];
2566 char *temp;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002567
Victor Stinnerd6f85422010-05-05 23:33:33 +00002568 PyUnicodeObject *po;
2569 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2570 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2571 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2572 Py_UNICODE *wtemp;
2573 DWORD result;
2574 PyObject *v;
2575 result = GetFullPathNameW(wpath,
2576 sizeof(woutbuf)/sizeof(woutbuf[0]),
2577 woutbuf, &wtemp);
2578 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2579 woutbufp = malloc(result * sizeof(Py_UNICODE));
2580 if (!woutbufp)
2581 return PyErr_NoMemory();
2582 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2583 }
2584 if (result)
2585 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2586 else
2587 v = win32_error_unicode("GetFullPathNameW", wpath);
2588 if (woutbufp != woutbuf)
2589 free(woutbufp);
2590 return v;
2591 }
2592 /* Drop the argument parsing error as narrow strings
2593 are also valid. */
2594 PyErr_Clear();
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002595
Victor Stinnerd6f85422010-05-05 23:33:33 +00002596 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2597 Py_FileSystemDefaultEncoding, &inbufp,
2598 &insize))
2599 return NULL;
2600 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2601 outbuf, &temp))
2602 return win32_error("GetFullPathName", inbuf);
2603 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2604 return PyUnicode_Decode(outbuf, strlen(outbuf),
2605 Py_FileSystemDefaultEncoding, NULL);
2606 }
2607 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002608} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002609#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002612"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002613Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002614
Barry Warsaw53699e91996-12-10 23:23:01 +00002615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002616posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002617{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002618 int res;
2619 char *path = NULL;
2620 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002621
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002622#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002623 PyUnicodeObject *po;
2624 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2625 Py_BEGIN_ALLOW_THREADS
2626 /* PyUnicode_AS_UNICODE OK without thread lock as
2627 it is a simple dereference. */
2628 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2629 Py_END_ALLOW_THREADS
2630 if (!res)
2631 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2632 Py_INCREF(Py_None);
2633 return Py_None;
2634 }
2635 /* Drop the argument parsing error as narrow strings
2636 are also valid. */
2637 PyErr_Clear();
2638 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2639 Py_FileSystemDefaultEncoding, &path, &mode))
2640 return NULL;
2641 Py_BEGIN_ALLOW_THREADS
2642 /* PyUnicode_AS_UNICODE OK without thread lock as
2643 it is a simple dereference. */
2644 res = CreateDirectoryA(path, NULL);
2645 Py_END_ALLOW_THREADS
2646 if (!res) {
2647 win32_error("mkdir", path);
2648 PyMem_Free(path);
2649 return NULL;
2650 }
2651 PyMem_Free(path);
2652 Py_INCREF(Py_None);
2653 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002654#else /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002655
Victor Stinnerd6f85422010-05-05 23:33:33 +00002656 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2657 Py_FileSystemDefaultEncoding, &path, &mode))
2658 return NULL;
2659 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002660#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002661 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002662#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002663 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002664#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002665 Py_END_ALLOW_THREADS
2666 if (res < 0)
2667 return posix_error_with_allocated_filename(path);
2668 PyMem_Free(path);
2669 Py_INCREF(Py_None);
2670 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002671#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002672}
2673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002674
Neal Norwitz1818ed72006-03-26 00:29:48 +00002675/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2676#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002677#include <sys/resource.h>
2678#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002679
Neal Norwitz1818ed72006-03-26 00:29:48 +00002680
2681#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002683"nice(inc) -> new_priority\n\n\
2684Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002685
Barry Warsaw53699e91996-12-10 23:23:01 +00002686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002687posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002688{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002689 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002690
Victor Stinnerd6f85422010-05-05 23:33:33 +00002691 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2692 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002693
Victor Stinnerd6f85422010-05-05 23:33:33 +00002694 /* There are two flavours of 'nice': one that returns the new
2695 priority (as required by almost all standards out there) and the
2696 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2697 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002698
Victor Stinnerd6f85422010-05-05 23:33:33 +00002699 If we are of the nice family that returns the new priority, we
2700 need to clear errno before the call, and check if errno is filled
2701 before calling posix_error() on a returnvalue of -1, because the
2702 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002703
Victor Stinnerd6f85422010-05-05 23:33:33 +00002704 errno = 0;
2705 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002706#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002707 if (value == 0)
2708 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002709#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002710 if (value == -1 && errno != 0)
2711 /* either nice() or getpriority() returned an error */
2712 return posix_error();
2713 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002714}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002715#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002718"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002720
Barry Warsaw53699e91996-12-10 23:23:01 +00002721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002722posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002723{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002724#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002725 PyObject *o1, *o2;
2726 char *p1, *p2;
2727 BOOL result;
2728 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2729 goto error;
2730 if (!convert_to_unicode(&o1))
2731 goto error;
2732 if (!convert_to_unicode(&o2)) {
2733 Py_DECREF(o1);
2734 goto error;
2735 }
2736 Py_BEGIN_ALLOW_THREADS
2737 result = MoveFileW(PyUnicode_AsUnicode(o1),
2738 PyUnicode_AsUnicode(o2));
2739 Py_END_ALLOW_THREADS
2740 Py_DECREF(o1);
2741 Py_DECREF(o2);
2742 if (!result)
2743 return win32_error("rename", NULL);
2744 Py_INCREF(Py_None);
2745 return Py_None;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002746error:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002747 PyErr_Clear();
2748 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2749 return NULL;
2750 Py_BEGIN_ALLOW_THREADS
2751 result = MoveFileA(p1, p2);
2752 Py_END_ALLOW_THREADS
2753 if (!result)
2754 return win32_error("rename", NULL);
2755 Py_INCREF(Py_None);
2756 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002757#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002758 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002759#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002760}
2761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002763PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002764"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002765Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002766
Barry Warsaw53699e91996-12-10 23:23:01 +00002767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002768posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002769{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002770#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002771 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002772#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002773 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002774#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002775}
2776
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002778PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002779"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002780Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002781
Barry Warsaw53699e91996-12-10 23:23:01 +00002782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002783posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002784{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002785#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002786 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002787#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002788 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002789#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002790}
2791
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002792
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002793#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002794PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002795"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002796Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002797
Barry Warsaw53699e91996-12-10 23:23:01 +00002798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002799posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002800{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002801 char *command;
2802 long sts;
2803 if (!PyArg_ParseTuple(args, "s:system", &command))
2804 return NULL;
2805 Py_BEGIN_ALLOW_THREADS
2806 sts = system(command);
2807 Py_END_ALLOW_THREADS
2808 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002809}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002810#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002811
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002813PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002814"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002815Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002816
Barry Warsaw53699e91996-12-10 23:23:01 +00002817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002818posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002819{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002820 int i;
2821 if (!PyArg_ParseTuple(args, "i:umask", &i))
2822 return NULL;
2823 i = (int)umask(i);
2824 if (i < 0)
2825 return posix_error();
2826 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002827}
2828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002831"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002832Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002834PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002835"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002836Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002837
Barry Warsaw53699e91996-12-10 23:23:01 +00002838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002839posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002840{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002841#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002842 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002843#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002844 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002845#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002846}
2847
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002848
Guido van Rossumb6775db1994-08-01 11:34:53 +00002849#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002850PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002851"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002852Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002853
Barry Warsaw53699e91996-12-10 23:23:01 +00002854static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002855posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002856{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002857 struct utsname u;
2858 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002859
Victor Stinnerd6f85422010-05-05 23:33:33 +00002860 Py_BEGIN_ALLOW_THREADS
2861 res = uname(&u);
2862 Py_END_ALLOW_THREADS
2863 if (res < 0)
2864 return posix_error();
2865 return Py_BuildValue("(sssss)",
2866 u.sysname,
2867 u.nodename,
2868 u.release,
2869 u.version,
2870 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002871}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002872#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002873
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002874static int
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002875extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002876{
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002877 time_t intval;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002878 if (PyFloat_Check(t)) {
2879 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002880 PyObject *intobj = PyNumber_Long(t);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002881 if (!intobj)
2882 return -1;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002883#if SIZEOF_TIME_T > SIZEOF_LONG
2884 intval = PyInt_AsUnsignedLongLongMask(intobj);
2885#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002886 intval = PyInt_AsLong(intobj);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002887#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002888 Py_DECREF(intobj);
2889 if (intval == -1 && PyErr_Occurred())
2890 return -1;
2891 *sec = intval;
2892 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2893 if (*usec < 0)
2894 /* If rounding gave us a negative number,
2895 truncate. */
2896 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002897 return 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002898 }
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002899#if SIZEOF_TIME_T > SIZEOF_LONG
2900 intval = PyInt_AsUnsignedLongLongMask(t);
2901#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002902 intval = PyInt_AsLong(t);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002903#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002904 if (intval == -1 && PyErr_Occurred())
2905 return -1;
2906 *sec = intval;
2907 *usec = 0;
2908 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002909}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002912"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002913utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002914Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002916
Barry Warsaw53699e91996-12-10 23:23:01 +00002917static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002918posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002919{
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002920#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002921 PyObject *arg;
2922 PyUnicodeObject *obwpath;
2923 wchar_t *wpath = NULL;
2924 char *apath = NULL;
2925 HANDLE hFile;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002926 time_t atimesec, mtimesec;
2927 long ausec, musec;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002928 FILETIME atime, mtime;
2929 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002930
Victor Stinnerd6f85422010-05-05 23:33:33 +00002931 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2932 wpath = PyUnicode_AS_UNICODE(obwpath);
2933 Py_BEGIN_ALLOW_THREADS
2934 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2935 NULL, OPEN_EXISTING,
2936 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2937 Py_END_ALLOW_THREADS
2938 if (hFile == INVALID_HANDLE_VALUE)
2939 return win32_error_unicode("utime", wpath);
2940 } else
2941 /* Drop the argument parsing error as narrow strings
2942 are also valid. */
2943 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002944
Victor Stinnerd6f85422010-05-05 23:33:33 +00002945 if (!wpath) {
2946 if (!PyArg_ParseTuple(args, "etO:utime",
2947 Py_FileSystemDefaultEncoding, &apath, &arg))
2948 return NULL;
2949 Py_BEGIN_ALLOW_THREADS
2950 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2951 NULL, OPEN_EXISTING,
2952 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2953 Py_END_ALLOW_THREADS
2954 if (hFile == INVALID_HANDLE_VALUE) {
2955 win32_error("utime", apath);
2956 PyMem_Free(apath);
2957 return NULL;
2958 }
2959 PyMem_Free(apath);
2960 }
2961
2962 if (arg == Py_None) {
2963 SYSTEMTIME now;
2964 GetSystemTime(&now);
2965 if (!SystemTimeToFileTime(&now, &mtime) ||
2966 !SystemTimeToFileTime(&now, &atime)) {
2967 win32_error("utime", NULL);
2968 goto done;
Stefan Krah93f7a322010-11-26 17:35:50 +00002969 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00002970 }
2971 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2972 PyErr_SetString(PyExc_TypeError,
2973 "utime() arg 2 must be a tuple (atime, mtime)");
2974 goto done;
2975 }
2976 else {
2977 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2978 &atimesec, &ausec) == -1)
2979 goto done;
2980 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2981 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2982 &mtimesec, &musec) == -1)
2983 goto done;
2984 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2985 }
2986 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2987 /* Avoid putting the file name into the error here,
2988 as that may confuse the user into believing that
2989 something is wrong with the file, when it also
2990 could be the time stamp that gives a problem. */
2991 win32_error("utime", NULL);
Antoine Pitrou1f1613f2011-01-06 18:30:26 +00002992 goto done;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002993 }
2994 Py_INCREF(Py_None);
2995 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002996done:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002997 CloseHandle(hFile);
2998 return result;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002999#else /* MS_WINDOWS */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00003000
Victor Stinnerd6f85422010-05-05 23:33:33 +00003001 char *path = NULL;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00003002 time_t atime, mtime;
3003 long ausec, musec;
Victor Stinnerd6f85422010-05-05 23:33:33 +00003004 int res;
3005 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003006
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003007#if defined(HAVE_UTIMES)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003008 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003009#define ATIME buf[0].tv_sec
3010#define MTIME buf[1].tv_sec
3011#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003012/* XXX should define struct utimbuf instead, above */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003013 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003014#define ATIME buf.actime
3015#define MTIME buf.modtime
3016#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003017#else /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003018 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003019#define ATIME buf[0]
3020#define MTIME buf[1]
3021#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003022#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003023
Mark Hammond817c9292003-12-03 01:22:38 +00003024
Victor Stinnerd6f85422010-05-05 23:33:33 +00003025 if (!PyArg_ParseTuple(args, "etO:utime",
3026 Py_FileSystemDefaultEncoding, &path, &arg))
3027 return NULL;
3028 if (arg == Py_None) {
3029 /* optional time values not given */
3030 Py_BEGIN_ALLOW_THREADS
3031 res = utime(path, NULL);
3032 Py_END_ALLOW_THREADS
3033 }
3034 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3035 PyErr_SetString(PyExc_TypeError,
3036 "utime() arg 2 must be a tuple (atime, mtime)");
3037 PyMem_Free(path);
3038 return NULL;
3039 }
3040 else {
3041 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3042 &atime, &ausec) == -1) {
3043 PyMem_Free(path);
3044 return NULL;
3045 }
3046 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3047 &mtime, &musec) == -1) {
3048 PyMem_Free(path);
3049 return NULL;
3050 }
3051 ATIME = atime;
3052 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003053#ifdef HAVE_UTIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00003054 buf[0].tv_usec = ausec;
3055 buf[1].tv_usec = musec;
3056 Py_BEGIN_ALLOW_THREADS
3057 res = utimes(path, buf);
3058 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003059#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003060 Py_BEGIN_ALLOW_THREADS
3061 res = utime(path, UTIME_ARG);
3062 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003063#endif /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003064 }
3065 if (res < 0) {
3066 return posix_error_with_allocated_filename(path);
3067 }
3068 PyMem_Free(path);
3069 Py_INCREF(Py_None);
3070 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003071#undef UTIME_ARG
3072#undef ATIME
3073#undef MTIME
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00003074#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003075}
3076
Guido van Rossum85e3b011991-06-03 12:42:10 +00003077
Guido van Rossum3b066191991-06-04 19:40:25 +00003078/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003080PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003081"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003082Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003083
Barry Warsaw53699e91996-12-10 23:23:01 +00003084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003085posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003086{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003087 int sts;
3088 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3089 return NULL;
3090 _exit(sts);
3091 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003092}
3093
Martin v. Löwis114619e2002-10-07 06:44:21 +00003094#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3095static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003096free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003097{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003098 Py_ssize_t i;
3099 for (i = 0; i < count; i++)
3100 PyMem_Free(array[i]);
3101 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003102}
3103#endif
3104
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003105
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003106#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003107PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003108"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003109Execute an executable path with arguments, replacing current process.\n\
3110\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003111 path: path of executable file\n\
3112 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003113
Barry Warsaw53699e91996-12-10 23:23:01 +00003114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003115posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003116{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003117 char *path;
3118 PyObject *argv;
3119 char **argvlist;
3120 Py_ssize_t i, argc;
3121 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003122
Victor Stinnerd6f85422010-05-05 23:33:33 +00003123 /* execv has two arguments: (path, argv), where
3124 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003125
Victor Stinnerd6f85422010-05-05 23:33:33 +00003126 if (!PyArg_ParseTuple(args, "etO:execv",
3127 Py_FileSystemDefaultEncoding,
3128 &path, &argv))
3129 return NULL;
3130 if (PyList_Check(argv)) {
3131 argc = PyList_Size(argv);
3132 getitem = PyList_GetItem;
3133 }
3134 else if (PyTuple_Check(argv)) {
3135 argc = PyTuple_Size(argv);
3136 getitem = PyTuple_GetItem;
3137 }
3138 else {
3139 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3140 PyMem_Free(path);
3141 return NULL;
3142 }
3143 if (argc < 1) {
3144 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3145 PyMem_Free(path);
3146 return NULL;
3147 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003148
Victor Stinnerd6f85422010-05-05 23:33:33 +00003149 argvlist = PyMem_NEW(char *, argc+1);
3150 if (argvlist == NULL) {
3151 PyMem_Free(path);
3152 return PyErr_NoMemory();
3153 }
3154 for (i = 0; i < argc; i++) {
3155 if (!PyArg_Parse((*getitem)(argv, i), "et",
3156 Py_FileSystemDefaultEncoding,
3157 &argvlist[i])) {
3158 free_string_array(argvlist, i);
3159 PyErr_SetString(PyExc_TypeError,
3160 "execv() arg 2 must contain only strings");
3161 PyMem_Free(path);
3162 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003163
Victor Stinnerd6f85422010-05-05 23:33:33 +00003164 }
3165 }
3166 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003167
Victor Stinnerd6f85422010-05-05 23:33:33 +00003168 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003169
Victor Stinnerd6f85422010-05-05 23:33:33 +00003170 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003171
Victor Stinnerd6f85422010-05-05 23:33:33 +00003172 free_string_array(argvlist, argc);
3173 PyMem_Free(path);
3174 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003175}
3176
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003178PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003179"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003180Execute a path with arguments and environment, replacing current process.\n\
3181\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003182 path: path of executable file\n\
3183 args: tuple or list of arguments\n\
3184 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003185
Barry Warsaw53699e91996-12-10 23:23:01 +00003186static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003187posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003188{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003189 char *path;
3190 PyObject *argv, *env;
3191 char **argvlist;
3192 char **envlist;
3193 PyObject *key, *val, *keys=NULL, *vals=NULL;
3194 Py_ssize_t i, pos, argc, envc;
3195 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3196 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003197
Victor Stinnerd6f85422010-05-05 23:33:33 +00003198 /* execve has three arguments: (path, argv, env), where
3199 argv is a list or tuple of strings and env is a dictionary
3200 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003201
Victor Stinnerd6f85422010-05-05 23:33:33 +00003202 if (!PyArg_ParseTuple(args, "etOO:execve",
3203 Py_FileSystemDefaultEncoding,
3204 &path, &argv, &env))
3205 return NULL;
3206 if (PyList_Check(argv)) {
3207 argc = PyList_Size(argv);
3208 getitem = PyList_GetItem;
3209 }
3210 else if (PyTuple_Check(argv)) {
3211 argc = PyTuple_Size(argv);
3212 getitem = PyTuple_GetItem;
3213 }
3214 else {
3215 PyErr_SetString(PyExc_TypeError,
3216 "execve() arg 2 must be a tuple or list");
3217 goto fail_0;
3218 }
3219 if (!PyMapping_Check(env)) {
3220 PyErr_SetString(PyExc_TypeError,
3221 "execve() arg 3 must be a mapping object");
3222 goto fail_0;
3223 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003224
Victor Stinnerd6f85422010-05-05 23:33:33 +00003225 argvlist = PyMem_NEW(char *, argc+1);
3226 if (argvlist == NULL) {
3227 PyErr_NoMemory();
3228 goto fail_0;
3229 }
3230 for (i = 0; i < argc; i++) {
3231 if (!PyArg_Parse((*getitem)(argv, i),
3232 "et;execve() arg 2 must contain only strings",
3233 Py_FileSystemDefaultEncoding,
3234 &argvlist[i]))
3235 {
3236 lastarg = i;
3237 goto fail_1;
3238 }
3239 }
3240 lastarg = argc;
3241 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003242
Victor Stinnerd6f85422010-05-05 23:33:33 +00003243 i = PyMapping_Size(env);
3244 if (i < 0)
3245 goto fail_1;
3246 envlist = PyMem_NEW(char *, i + 1);
3247 if (envlist == NULL) {
3248 PyErr_NoMemory();
3249 goto fail_1;
3250 }
3251 envc = 0;
3252 keys = PyMapping_Keys(env);
3253 vals = PyMapping_Values(env);
3254 if (!keys || !vals)
3255 goto fail_2;
3256 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3257 PyErr_SetString(PyExc_TypeError,
3258 "execve(): env.keys() or env.values() is not a list");
3259 goto fail_2;
3260 }
Tim Peters5aa91602002-01-30 05:46:57 +00003261
Victor Stinnerd6f85422010-05-05 23:33:33 +00003262 for (pos = 0; pos < i; pos++) {
3263 char *p, *k, *v;
3264 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003265
Victor Stinnerd6f85422010-05-05 23:33:33 +00003266 key = PyList_GetItem(keys, pos);
3267 val = PyList_GetItem(vals, pos);
3268 if (!key || !val)
3269 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003270
Victor Stinnerd6f85422010-05-05 23:33:33 +00003271 if (!PyArg_Parse(
3272 key,
3273 "s;execve() arg 3 contains a non-string key",
3274 &k) ||
3275 !PyArg_Parse(
3276 val,
3277 "s;execve() arg 3 contains a non-string value",
3278 &v))
3279 {
3280 goto fail_2;
3281 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003282
3283#if defined(PYOS_OS2)
3284 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3285 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3286#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003287 len = PyString_Size(key) + PyString_Size(val) + 2;
3288 p = PyMem_NEW(char, len);
3289 if (p == NULL) {
3290 PyErr_NoMemory();
3291 goto fail_2;
3292 }
3293 PyOS_snprintf(p, len, "%s=%s", k, v);
3294 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003295#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003296 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003297#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003298 }
3299 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003300
Victor Stinnerd6f85422010-05-05 23:33:33 +00003301 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003302
Victor Stinnerd6f85422010-05-05 23:33:33 +00003303 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003304
Victor Stinnerd6f85422010-05-05 23:33:33 +00003305 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003306
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003307 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003308 while (--envc >= 0)
3309 PyMem_DEL(envlist[envc]);
3310 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003311 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003312 free_string_array(argvlist, lastarg);
3313 Py_XDECREF(vals);
3314 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003315 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003316 PyMem_Free(path);
3317 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003318}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003319#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003321
Guido van Rossuma1065681999-01-25 23:20:23 +00003322#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003323PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003324"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003325Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003326\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003327 mode: mode of process creation\n\
3328 path: path of executable file\n\
3329 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003330
3331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003332posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003333{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003334 char *path;
3335 PyObject *argv;
3336 char **argvlist;
3337 int mode, i;
3338 Py_ssize_t argc;
3339 Py_intptr_t spawnval;
3340 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003341
Victor Stinnerd6f85422010-05-05 23:33:33 +00003342 /* spawnv has three arguments: (mode, path, argv), where
3343 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003344
Victor Stinnerd6f85422010-05-05 23:33:33 +00003345 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3346 Py_FileSystemDefaultEncoding,
3347 &path, &argv))
3348 return NULL;
3349 if (PyList_Check(argv)) {
3350 argc = PyList_Size(argv);
3351 getitem = PyList_GetItem;
3352 }
3353 else if (PyTuple_Check(argv)) {
3354 argc = PyTuple_Size(argv);
3355 getitem = PyTuple_GetItem;
3356 }
3357 else {
3358 PyErr_SetString(PyExc_TypeError,
3359 "spawnv() arg 2 must be a tuple or list");
3360 PyMem_Free(path);
3361 return NULL;
3362 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003363
Victor Stinnerd6f85422010-05-05 23:33:33 +00003364 argvlist = PyMem_NEW(char *, argc+1);
3365 if (argvlist == NULL) {
3366 PyMem_Free(path);
3367 return PyErr_NoMemory();
3368 }
3369 for (i = 0; i < argc; i++) {
3370 if (!PyArg_Parse((*getitem)(argv, i), "et",
3371 Py_FileSystemDefaultEncoding,
3372 &argvlist[i])) {
3373 free_string_array(argvlist, i);
3374 PyErr_SetString(
3375 PyExc_TypeError,
3376 "spawnv() arg 2 must contain only strings");
3377 PyMem_Free(path);
3378 return NULL;
3379 }
3380 }
3381 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003382
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003383#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003384 Py_BEGIN_ALLOW_THREADS
3385 spawnval = spawnv(mode, path, argvlist);
3386 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003387#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003388 if (mode == _OLD_P_OVERLAY)
3389 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003390
Victor Stinnerd6f85422010-05-05 23:33:33 +00003391 Py_BEGIN_ALLOW_THREADS
3392 spawnval = _spawnv(mode, path, argvlist);
3393 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003394#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003395
Victor Stinnerd6f85422010-05-05 23:33:33 +00003396 free_string_array(argvlist, argc);
3397 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003398
Victor Stinnerd6f85422010-05-05 23:33:33 +00003399 if (spawnval == -1)
3400 return posix_error();
3401 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003402#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003403 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003404#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003405 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003406#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003407}
3408
3409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003410PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003411"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003412Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003413\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003414 mode: mode of process creation\n\
3415 path: path of executable file\n\
3416 args: tuple or list of arguments\n\
3417 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003418
3419static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003420posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003421{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003422 char *path;
3423 PyObject *argv, *env;
3424 char **argvlist;
3425 char **envlist;
3426 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3427 int mode, pos, envc;
3428 Py_ssize_t argc, i;
3429 Py_intptr_t spawnval;
3430 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3431 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003432
Victor Stinnerd6f85422010-05-05 23:33:33 +00003433 /* spawnve has four arguments: (mode, path, argv, env), where
3434 argv is a list or tuple of strings and env is a dictionary
3435 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003436
Victor Stinnerd6f85422010-05-05 23:33:33 +00003437 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3438 Py_FileSystemDefaultEncoding,
3439 &path, &argv, &env))
3440 return NULL;
3441 if (PyList_Check(argv)) {
3442 argc = PyList_Size(argv);
3443 getitem = PyList_GetItem;
3444 }
3445 else if (PyTuple_Check(argv)) {
3446 argc = PyTuple_Size(argv);
3447 getitem = PyTuple_GetItem;
3448 }
3449 else {
3450 PyErr_SetString(PyExc_TypeError,
3451 "spawnve() arg 2 must be a tuple or list");
3452 goto fail_0;
3453 }
3454 if (!PyMapping_Check(env)) {
3455 PyErr_SetString(PyExc_TypeError,
3456 "spawnve() arg 3 must be a mapping object");
3457 goto fail_0;
3458 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003459
Victor Stinnerd6f85422010-05-05 23:33:33 +00003460 argvlist = PyMem_NEW(char *, argc+1);
3461 if (argvlist == NULL) {
3462 PyErr_NoMemory();
3463 goto fail_0;
3464 }
3465 for (i = 0; i < argc; i++) {
3466 if (!PyArg_Parse((*getitem)(argv, i),
3467 "et;spawnve() arg 2 must contain only strings",
3468 Py_FileSystemDefaultEncoding,
3469 &argvlist[i]))
3470 {
3471 lastarg = i;
3472 goto fail_1;
3473 }
3474 }
3475 lastarg = argc;
3476 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003477
Victor Stinnerd6f85422010-05-05 23:33:33 +00003478 i = PyMapping_Size(env);
3479 if (i < 0)
3480 goto fail_1;
3481 envlist = PyMem_NEW(char *, i + 1);
3482 if (envlist == NULL) {
3483 PyErr_NoMemory();
3484 goto fail_1;
3485 }
3486 envc = 0;
3487 keys = PyMapping_Keys(env);
3488 vals = PyMapping_Values(env);
3489 if (!keys || !vals)
3490 goto fail_2;
3491 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3492 PyErr_SetString(PyExc_TypeError,
3493 "spawnve(): env.keys() or env.values() is not a list");
3494 goto fail_2;
3495 }
Tim Peters5aa91602002-01-30 05:46:57 +00003496
Victor Stinnerd6f85422010-05-05 23:33:33 +00003497 for (pos = 0; pos < i; pos++) {
3498 char *p, *k, *v;
3499 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003500
Victor Stinnerd6f85422010-05-05 23:33:33 +00003501 key = PyList_GetItem(keys, pos);
3502 val = PyList_GetItem(vals, pos);
3503 if (!key || !val)
3504 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003505
Victor Stinnerd6f85422010-05-05 23:33:33 +00003506 if (!PyArg_Parse(
3507 key,
3508 "s;spawnve() arg 3 contains a non-string key",
3509 &k) ||
3510 !PyArg_Parse(
3511 val,
3512 "s;spawnve() arg 3 contains a non-string value",
3513 &v))
3514 {
3515 goto fail_2;
3516 }
3517 len = PyString_Size(key) + PyString_Size(val) + 2;
3518 p = PyMem_NEW(char, len);
3519 if (p == NULL) {
3520 PyErr_NoMemory();
3521 goto fail_2;
3522 }
3523 PyOS_snprintf(p, len, "%s=%s", k, v);
3524 envlist[envc++] = p;
3525 }
3526 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003527
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003528#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003529 Py_BEGIN_ALLOW_THREADS
3530 spawnval = spawnve(mode, path, argvlist, envlist);
3531 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003532#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003533 if (mode == _OLD_P_OVERLAY)
3534 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003535
Victor Stinnerd6f85422010-05-05 23:33:33 +00003536 Py_BEGIN_ALLOW_THREADS
3537 spawnval = _spawnve(mode, path, argvlist, envlist);
3538 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003539#endif
Tim Peters25059d32001-12-07 20:35:43 +00003540
Victor Stinnerd6f85422010-05-05 23:33:33 +00003541 if (spawnval == -1)
3542 (void) posix_error();
3543 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003544#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003545 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003546#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003547 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003548#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003549
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003550 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003551 while (--envc >= 0)
3552 PyMem_DEL(envlist[envc]);
3553 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003554 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003555 free_string_array(argvlist, lastarg);
3556 Py_XDECREF(vals);
3557 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003558 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003559 PyMem_Free(path);
3560 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003561}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003562
3563/* OS/2 supports spawnvp & spawnvpe natively */
3564#if defined(PYOS_OS2)
3565PyDoc_STRVAR(posix_spawnvp__doc__,
3566"spawnvp(mode, file, args)\n\n\
3567Execute the program 'file' in a new process, using the environment\n\
3568search path to find the file.\n\
3569\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003570 mode: mode of process creation\n\
3571 file: executable file name\n\
3572 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003573
3574static PyObject *
3575posix_spawnvp(PyObject *self, PyObject *args)
3576{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003577 char *path;
3578 PyObject *argv;
3579 char **argvlist;
3580 int mode, i, argc;
3581 Py_intptr_t spawnval;
3582 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003583
Victor Stinnerd6f85422010-05-05 23:33:33 +00003584 /* spawnvp has three arguments: (mode, path, argv), where
3585 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003586
Victor Stinnerd6f85422010-05-05 23:33:33 +00003587 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3588 Py_FileSystemDefaultEncoding,
3589 &path, &argv))
3590 return NULL;
3591 if (PyList_Check(argv)) {
3592 argc = PyList_Size(argv);
3593 getitem = PyList_GetItem;
3594 }
3595 else if (PyTuple_Check(argv)) {
3596 argc = PyTuple_Size(argv);
3597 getitem = PyTuple_GetItem;
3598 }
3599 else {
3600 PyErr_SetString(PyExc_TypeError,
3601 "spawnvp() arg 2 must be a tuple or list");
3602 PyMem_Free(path);
3603 return NULL;
3604 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003605
Victor Stinnerd6f85422010-05-05 23:33:33 +00003606 argvlist = PyMem_NEW(char *, argc+1);
3607 if (argvlist == NULL) {
3608 PyMem_Free(path);
3609 return PyErr_NoMemory();
3610 }
3611 for (i = 0; i < argc; i++) {
3612 if (!PyArg_Parse((*getitem)(argv, i), "et",
3613 Py_FileSystemDefaultEncoding,
3614 &argvlist[i])) {
3615 free_string_array(argvlist, i);
3616 PyErr_SetString(
3617 PyExc_TypeError,
3618 "spawnvp() arg 2 must contain only strings");
3619 PyMem_Free(path);
3620 return NULL;
3621 }
3622 }
3623 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003624
Victor Stinnerd6f85422010-05-05 23:33:33 +00003625 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003626#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003627 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003628#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003629 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003630#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003631 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003632
Victor Stinnerd6f85422010-05-05 23:33:33 +00003633 free_string_array(argvlist, argc);
3634 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003635
Victor Stinnerd6f85422010-05-05 23:33:33 +00003636 if (spawnval == -1)
3637 return posix_error();
3638 else
3639 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003640}
3641
3642
3643PyDoc_STRVAR(posix_spawnvpe__doc__,
3644"spawnvpe(mode, file, args, env)\n\n\
3645Execute the program 'file' in a new process, using the environment\n\
3646search path to find the file.\n\
3647\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003648 mode: mode of process creation\n\
3649 file: executable file name\n\
3650 args: tuple or list of arguments\n\
3651 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003652
3653static PyObject *
3654posix_spawnvpe(PyObject *self, PyObject *args)
3655{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003656 char *path;
3657 PyObject *argv, *env;
3658 char **argvlist;
3659 char **envlist;
3660 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3661 int mode, i, pos, argc, envc;
3662 Py_intptr_t spawnval;
3663 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3664 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003665
Victor Stinnerd6f85422010-05-05 23:33:33 +00003666 /* spawnvpe has four arguments: (mode, path, argv, env), where
3667 argv is a list or tuple of strings and env is a dictionary
3668 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003669
Victor Stinnerd6f85422010-05-05 23:33:33 +00003670 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3671 Py_FileSystemDefaultEncoding,
3672 &path, &argv, &env))
3673 return NULL;
3674 if (PyList_Check(argv)) {
3675 argc = PyList_Size(argv);
3676 getitem = PyList_GetItem;
3677 }
3678 else if (PyTuple_Check(argv)) {
3679 argc = PyTuple_Size(argv);
3680 getitem = PyTuple_GetItem;
3681 }
3682 else {
3683 PyErr_SetString(PyExc_TypeError,
3684 "spawnvpe() arg 2 must be a tuple or list");
3685 goto fail_0;
3686 }
3687 if (!PyMapping_Check(env)) {
3688 PyErr_SetString(PyExc_TypeError,
3689 "spawnvpe() arg 3 must be a mapping object");
3690 goto fail_0;
3691 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003692
Victor Stinnerd6f85422010-05-05 23:33:33 +00003693 argvlist = PyMem_NEW(char *, argc+1);
3694 if (argvlist == NULL) {
3695 PyErr_NoMemory();
3696 goto fail_0;
3697 }
3698 for (i = 0; i < argc; i++) {
3699 if (!PyArg_Parse((*getitem)(argv, i),
3700 "et;spawnvpe() arg 2 must contain only strings",
3701 Py_FileSystemDefaultEncoding,
3702 &argvlist[i]))
3703 {
3704 lastarg = i;
3705 goto fail_1;
3706 }
3707 }
3708 lastarg = argc;
3709 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003710
Victor Stinnerd6f85422010-05-05 23:33:33 +00003711 i = PyMapping_Size(env);
3712 if (i < 0)
3713 goto fail_1;
3714 envlist = PyMem_NEW(char *, i + 1);
3715 if (envlist == NULL) {
3716 PyErr_NoMemory();
3717 goto fail_1;
3718 }
3719 envc = 0;
3720 keys = PyMapping_Keys(env);
3721 vals = PyMapping_Values(env);
3722 if (!keys || !vals)
3723 goto fail_2;
3724 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3725 PyErr_SetString(PyExc_TypeError,
3726 "spawnvpe(): env.keys() or env.values() is not a list");
3727 goto fail_2;
3728 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003729
Victor Stinnerd6f85422010-05-05 23:33:33 +00003730 for (pos = 0; pos < i; pos++) {
3731 char *p, *k, *v;
3732 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003733
Victor Stinnerd6f85422010-05-05 23:33:33 +00003734 key = PyList_GetItem(keys, pos);
3735 val = PyList_GetItem(vals, pos);
3736 if (!key || !val)
3737 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003738
Victor Stinnerd6f85422010-05-05 23:33:33 +00003739 if (!PyArg_Parse(
3740 key,
3741 "s;spawnvpe() arg 3 contains a non-string key",
3742 &k) ||
3743 !PyArg_Parse(
3744 val,
3745 "s;spawnvpe() arg 3 contains a non-string value",
3746 &v))
3747 {
3748 goto fail_2;
3749 }
3750 len = PyString_Size(key) + PyString_Size(val) + 2;
3751 p = PyMem_NEW(char, len);
3752 if (p == NULL) {
3753 PyErr_NoMemory();
3754 goto fail_2;
3755 }
3756 PyOS_snprintf(p, len, "%s=%s", k, v);
3757 envlist[envc++] = p;
3758 }
3759 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003760
Victor Stinnerd6f85422010-05-05 23:33:33 +00003761 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003762#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003763 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003764#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003765 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003766#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003767 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003768
Victor Stinnerd6f85422010-05-05 23:33:33 +00003769 if (spawnval == -1)
3770 (void) posix_error();
3771 else
3772 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003773
3774 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003775 while (--envc >= 0)
3776 PyMem_DEL(envlist[envc]);
3777 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003778 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003779 free_string_array(argvlist, lastarg);
3780 Py_XDECREF(vals);
3781 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003782 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003783 PyMem_Free(path);
3784 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003785}
3786#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003787#endif /* HAVE_SPAWNV */
3788
3789
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003790#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003792"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003793Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3794\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003795Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003796
3797static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003798posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003799{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003800 pid_t pid;
3801 int result = 0;
3802 _PyImport_AcquireLock();
3803 pid = fork1();
3804 if (pid == 0) {
3805 /* child: this clobbers and resets the import lock. */
3806 PyOS_AfterFork();
3807 } else {
3808 /* parent: release the import lock. */
3809 result = _PyImport_ReleaseLock();
3810 }
3811 if (pid == -1)
3812 return posix_error();
3813 if (result < 0) {
3814 /* Don't clobber the OSError if the fork failed. */
3815 PyErr_SetString(PyExc_RuntimeError,
3816 "not holding the import lock");
3817 return NULL;
3818 }
3819 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003820}
3821#endif
3822
3823
Guido van Rossumad0ee831995-03-01 10:34:45 +00003824#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003825PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003826"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003827Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003829
Barry Warsaw53699e91996-12-10 23:23:01 +00003830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003831posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003832{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003833 pid_t pid;
3834 int result = 0;
3835 _PyImport_AcquireLock();
3836 pid = fork();
3837 if (pid == 0) {
3838 /* child: this clobbers and resets the import lock. */
3839 PyOS_AfterFork();
3840 } else {
3841 /* parent: release the import lock. */
3842 result = _PyImport_ReleaseLock();
3843 }
3844 if (pid == -1)
3845 return posix_error();
3846 if (result < 0) {
3847 /* Don't clobber the OSError if the fork failed. */
3848 PyErr_SetString(PyExc_RuntimeError,
3849 "not holding the import lock");
3850 return NULL;
3851 }
3852 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003853}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003854#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003855
Neal Norwitzb59798b2003-03-21 01:43:31 +00003856/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003857/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3858#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003859#define DEV_PTY_FILE "/dev/ptc"
3860#define HAVE_DEV_PTMX
3861#else
3862#define DEV_PTY_FILE "/dev/ptmx"
3863#endif
3864
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003865#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003866#ifdef HAVE_PTY_H
3867#include <pty.h>
3868#else
3869#ifdef HAVE_LIBUTIL_H
3870#include <libutil.h>
Ronald Oussorena55af9a2010-01-17 16:25:57 +00003871#else
3872#ifdef HAVE_UTIL_H
3873#include <util.h>
3874#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003875#endif /* HAVE_LIBUTIL_H */
3876#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003877#ifdef HAVE_STROPTS_H
3878#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003879#endif
3880#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003881
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003882#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003883PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003884"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003886
3887static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003888posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003889{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003890 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003891#ifndef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003892 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003893#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003894#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003895 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003896#ifdef sun
Victor Stinnerd6f85422010-05-05 23:33:33 +00003897 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003898#endif
3899#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003900
Thomas Wouters70c21a12000-07-14 14:28:33 +00003901#ifdef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003902 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3903 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003904#elif defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003905 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3906 if (slave_name == NULL)
3907 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003908
Victor Stinnerd6f85422010-05-05 23:33:33 +00003909 slave_fd = open(slave_name, O_RDWR);
3910 if (slave_fd < 0)
3911 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003912#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003913 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3914 if (master_fd < 0)
3915 return posix_error();
3916 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3917 /* change permission of slave */
3918 if (grantpt(master_fd) < 0) {
3919 PyOS_setsig(SIGCHLD, sig_saved);
3920 return posix_error();
3921 }
3922 /* unlock slave */
3923 if (unlockpt(master_fd) < 0) {
3924 PyOS_setsig(SIGCHLD, sig_saved);
3925 return posix_error();
3926 }
3927 PyOS_setsig(SIGCHLD, sig_saved);
3928 slave_name = ptsname(master_fd); /* get name of slave */
3929 if (slave_name == NULL)
3930 return posix_error();
3931 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3932 if (slave_fd < 0)
3933 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003934#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003935 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3936 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003937#ifndef __hpux
Victor Stinnerd6f85422010-05-05 23:33:33 +00003938 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003939#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003940#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003941#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003942
Victor Stinnerd6f85422010-05-05 23:33:33 +00003943 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003944
Fred Drake8cef4cf2000-06-28 16:40:38 +00003945}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003946#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003947
3948#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003949PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003950"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003951Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3952Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003953To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003954
3955static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003956posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003957{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003958 int master_fd = -1, result = 0;
3959 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003960
Victor Stinnerd6f85422010-05-05 23:33:33 +00003961 _PyImport_AcquireLock();
3962 pid = forkpty(&master_fd, NULL, NULL, NULL);
3963 if (pid == 0) {
3964 /* child: this clobbers and resets the import lock. */
3965 PyOS_AfterFork();
3966 } else {
3967 /* parent: release the import lock. */
3968 result = _PyImport_ReleaseLock();
3969 }
3970 if (pid == -1)
3971 return posix_error();
3972 if (result < 0) {
3973 /* Don't clobber the OSError if the fork failed. */
3974 PyErr_SetString(PyExc_RuntimeError,
3975 "not holding the import lock");
3976 return NULL;
3977 }
3978 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003979}
3980#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003981
Guido van Rossumad0ee831995-03-01 10:34:45 +00003982#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003983PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003984"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003985Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003986
Barry Warsaw53699e91996-12-10 23:23:01 +00003987static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003988posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003989{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02003990 return _PyInt_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003991}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003992#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003993
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003994
Guido van Rossumad0ee831995-03-01 10:34:45 +00003995#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003996PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003997"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003998Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003999
Barry Warsaw53699e91996-12-10 23:23:01 +00004000static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004001posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004002{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004003 return _PyInt_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004004}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004005#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004006
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004007
Guido van Rossumad0ee831995-03-01 10:34:45 +00004008#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004009PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004010"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004011Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004012
Barry Warsaw53699e91996-12-10 23:23:01 +00004013static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004014posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004015{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004016 return _PyInt_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004017}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004018#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004019
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004020
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004021PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004022"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004023Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004024
Barry Warsaw53699e91996-12-10 23:23:01 +00004025static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004026posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004027{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004028 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004029}
4030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004031
Fred Drakec9680921999-12-13 16:37:25 +00004032#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004033PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004034"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004035Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004036
4037static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004038posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004039{
4040 PyObject *result = NULL;
4041
Fred Drakec9680921999-12-13 16:37:25 +00004042#ifdef NGROUPS_MAX
4043#define MAX_GROUPS NGROUPS_MAX
4044#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004045 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004046#define MAX_GROUPS 64
4047#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004048 gid_t grouplist[MAX_GROUPS];
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004049
Victor Stinner59729ff2011-07-05 11:28:19 +02004050 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004051 * This is a helper variable to store the intermediate result when
4052 * that happens.
4053 *
4054 * To keep the code readable the OSX behaviour is unconditional,
4055 * according to the POSIX spec this should be safe on all unix-y
4056 * systems.
4057 */
4058 gid_t* alt_grouplist = grouplist;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004059 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004060
Victor Stinnerd6f85422010-05-05 23:33:33 +00004061 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004062 if (n < 0) {
4063 if (errno == EINVAL) {
4064 n = getgroups(0, NULL);
4065 if (n == -1) {
4066 return posix_error();
4067 }
4068 if (n == 0) {
4069 /* Avoid malloc(0) */
4070 alt_grouplist = grouplist;
4071 } else {
4072 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4073 if (alt_grouplist == NULL) {
4074 errno = EINVAL;
4075 return posix_error();
4076 }
4077 n = getgroups(n, alt_grouplist);
4078 if (n == -1) {
4079 PyMem_Free(alt_grouplist);
4080 return posix_error();
4081 }
4082 }
4083 } else {
4084 return posix_error();
4085 }
4086 }
4087 result = PyList_New(n);
4088 if (result != NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00004089 int i;
4090 for (i = 0; i < n; ++i) {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004091 PyObject *o = _PyInt_FromGid(alt_grouplist[i]);
Victor Stinnerd6f85422010-05-05 23:33:33 +00004092 if (o == NULL) {
Stefan Krah93f7a322010-11-26 17:35:50 +00004093 Py_DECREF(result);
4094 result = NULL;
4095 break;
Fred Drakec9680921999-12-13 16:37:25 +00004096 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00004097 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004098 }
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004099 }
4100
4101 if (alt_grouplist != grouplist) {
4102 PyMem_Free(alt_grouplist);
Victor Stinnerd6f85422010-05-05 23:33:33 +00004103 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004104
Fred Drakec9680921999-12-13 16:37:25 +00004105 return result;
4106}
4107#endif
4108
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004109#ifdef HAVE_INITGROUPS
4110PyDoc_STRVAR(posix_initgroups__doc__,
4111"initgroups(username, gid) -> None\n\n\
4112Call the system initgroups() to initialize the group access list with all of\n\
4113the groups of which the specified username is a member, plus the specified\n\
4114group id.");
4115
4116static PyObject *
4117posix_initgroups(PyObject *self, PyObject *args)
4118{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004119 char *username;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004120#ifdef __APPLE__
4121 int gid;
4122#else
4123 gid_t gid;
4124#endif
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004125
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004126#ifdef __APPLE__
4127 if (!PyArg_ParseTuple(args, "si:initgroups", &username,
4128 &gid))
4129#else
4130 if (!PyArg_ParseTuple(args, "sO&:initgroups", &username,
4131 _Py_Gid_Converter, &gid))
4132#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004133 return NULL;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004134
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004135 if (initgroups(username, gid) == -1)
Victor Stinnerd6f85422010-05-05 23:33:33 +00004136 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004137
Victor Stinnerd6f85422010-05-05 23:33:33 +00004138 Py_INCREF(Py_None);
4139 return Py_None;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004140}
4141#endif
4142
Martin v. Löwis606edc12002-06-13 21:09:11 +00004143#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004144PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004145"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004146Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004147
4148static PyObject *
4149posix_getpgid(PyObject *self, PyObject *args)
4150{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004151 pid_t pid, pgid;
4152 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
4153 return NULL;
4154 pgid = getpgid(pid);
4155 if (pgid < 0)
4156 return posix_error();
4157 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004158}
4159#endif /* HAVE_GETPGID */
4160
4161
Guido van Rossumb6775db1994-08-01 11:34:53 +00004162#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004163PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004164"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004165Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004166
Barry Warsaw53699e91996-12-10 23:23:01 +00004167static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004168posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004169{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004170#ifdef GETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004171 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004172#else /* GETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004173 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004174#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004175}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004176#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004177
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004178
Guido van Rossumb6775db1994-08-01 11:34:53 +00004179#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004180PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004181"setpgrp()\n\n\
Senthil Kumaran0d6908b2010-06-17 16:38:34 +00004182Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004183
Barry Warsaw53699e91996-12-10 23:23:01 +00004184static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004185posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004186{
Guido van Rossum64933891994-10-20 21:56:42 +00004187#ifdef SETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004188 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004189#else /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004190 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004191#endif /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004192 return posix_error();
4193 Py_INCREF(Py_None);
4194 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004195}
4196
Guido van Rossumb6775db1994-08-01 11:34:53 +00004197#endif /* HAVE_SETPGRP */
4198
Guido van Rossumad0ee831995-03-01 10:34:45 +00004199#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004201"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004203
Barry Warsaw53699e91996-12-10 23:23:01 +00004204static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004205posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004206{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004207 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004208}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004209#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Fred Drake12c6e2d1999-12-14 21:25:03 +00004212#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004214"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004216
4217static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004218posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004219{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004220 PyObject *result = NULL;
4221 char *name;
4222 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004223
Victor Stinnerd6f85422010-05-05 23:33:33 +00004224 errno = 0;
4225 name = getlogin();
4226 if (name == NULL) {
4227 if (errno)
4228 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004229 else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004230 PyErr_SetString(PyExc_OSError,
4231 "unable to determine login name");
4232 }
4233 else
4234 result = PyString_FromString(name);
4235 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004236
Fred Drake12c6e2d1999-12-14 21:25:03 +00004237 return result;
4238}
4239#endif
4240
Guido van Rossumad0ee831995-03-01 10:34:45 +00004241#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004242PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004243"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004244Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004245
Barry Warsaw53699e91996-12-10 23:23:01 +00004246static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004247posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004248{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004249 return _PyInt_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004250}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004251#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004253
Guido van Rossumad0ee831995-03-01 10:34:45 +00004254#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004255PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004256"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004257Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004258
Barry Warsaw53699e91996-12-10 23:23:01 +00004259static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004260posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004261{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004262 pid_t pid;
4263 int sig;
4264 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4265 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004266#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004267 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4268 APIRET rc;
4269 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004270 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004271
4272 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4273 APIRET rc;
4274 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004275 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004276
4277 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004278 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004279#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004280 if (kill(pid, sig) == -1)
4281 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004282#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004283 Py_INCREF(Py_None);
4284 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004285}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004286#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004287
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004288#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004289PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004290"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004291Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004292
4293static PyObject *
4294posix_killpg(PyObject *self, PyObject *args)
4295{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004296 int sig;
4297 pid_t pgid;
4298 /* XXX some man pages make the `pgid` parameter an int, others
4299 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4300 take the same type. Moreover, pid_t is always at least as wide as
4301 int (else compilation of this module fails), which is safe. */
4302 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4303 return NULL;
4304 if (killpg(pgid, sig) == -1)
4305 return posix_error();
4306 Py_INCREF(Py_None);
4307 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004308}
4309#endif
4310
Brian Curtine5aa8862010-04-02 23:26:06 +00004311#ifdef MS_WINDOWS
4312PyDoc_STRVAR(win32_kill__doc__,
4313"kill(pid, sig)\n\n\
4314Kill a process with a signal.");
4315
4316static PyObject *
4317win32_kill(PyObject *self, PyObject *args)
4318{
Amaury Forgeot d'Arc03acec22010-05-15 21:45:30 +00004319 PyObject *result;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004320 DWORD pid, sig, err;
4321 HANDLE handle;
Brian Curtine5aa8862010-04-02 23:26:06 +00004322
Victor Stinnerd6f85422010-05-05 23:33:33 +00004323 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4324 return NULL;
Brian Curtine5aa8862010-04-02 23:26:06 +00004325
Victor Stinnerd6f85422010-05-05 23:33:33 +00004326 /* Console processes which share a common console can be sent CTRL+C or
4327 CTRL+BREAK events, provided they handle said events. */
4328 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4329 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4330 err = GetLastError();
4331 return PyErr_SetFromWindowsErr(err);
4332 }
4333 else
4334 Py_RETURN_NONE;
4335 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004336
Victor Stinnerd6f85422010-05-05 23:33:33 +00004337 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4338 attempt to open and terminate the process. */
4339 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4340 if (handle == NULL) {
4341 err = GetLastError();
4342 return PyErr_SetFromWindowsErr(err);
4343 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004344
Victor Stinnerd6f85422010-05-05 23:33:33 +00004345 if (TerminateProcess(handle, sig) == 0) {
4346 err = GetLastError();
4347 result = PyErr_SetFromWindowsErr(err);
4348 } else {
4349 Py_INCREF(Py_None);
4350 result = Py_None;
4351 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004352
Victor Stinnerd6f85422010-05-05 23:33:33 +00004353 CloseHandle(handle);
4354 return result;
Brian Curtine5aa8862010-04-02 23:26:06 +00004355}
Brian Curtincaea7e82011-06-08 19:29:53 -05004356
Brian Curtin5446f082011-06-09 10:00:42 -05004357PyDoc_STRVAR(posix__isdir__doc__,
4358"Return true if the pathname refers to an existing directory.");
4359
Brian Curtincaea7e82011-06-08 19:29:53 -05004360static PyObject *
4361posix__isdir(PyObject *self, PyObject *args)
4362{
4363 PyObject *opath;
4364 char *path;
4365 PyUnicodeObject *po;
4366 DWORD attributes;
4367
4368 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
4369 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
4370
4371 attributes = GetFileAttributesW(wpath);
4372 if (attributes == INVALID_FILE_ATTRIBUTES)
4373 Py_RETURN_FALSE;
4374 goto check;
4375 }
4376 /* Drop the argument parsing error as narrow strings
4377 are also valid. */
4378 PyErr_Clear();
4379
4380 if (!PyArg_ParseTuple(args, "et:_isdir",
4381 Py_FileSystemDefaultEncoding, &path))
4382 return NULL;
4383
4384 attributes = GetFileAttributesA(path);
Serhiy Storchaka46f5b352013-01-28 20:19:50 +02004385 PyMem_Free(path);
Brian Curtincaea7e82011-06-08 19:29:53 -05004386 if (attributes == INVALID_FILE_ATTRIBUTES)
4387 Py_RETURN_FALSE;
4388
4389check:
4390 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
4391 Py_RETURN_TRUE;
4392 else
4393 Py_RETURN_FALSE;
4394}
Brian Curtine5aa8862010-04-02 23:26:06 +00004395#endif /* MS_WINDOWS */
4396
Guido van Rossumc0125471996-06-28 18:55:32 +00004397#ifdef HAVE_PLOCK
4398
4399#ifdef HAVE_SYS_LOCK_H
4400#include <sys/lock.h>
4401#endif
4402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004403PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004404"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004405Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004406
Barry Warsaw53699e91996-12-10 23:23:01 +00004407static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004408posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004409{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004410 int op;
4411 if (!PyArg_ParseTuple(args, "i:plock", &op))
4412 return NULL;
4413 if (plock(op) == -1)
4414 return posix_error();
4415 Py_INCREF(Py_None);
4416 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004417}
4418#endif
4419
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004420
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004421#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004422PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004423"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004424Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004425
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004426#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004427#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004428static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004429async_system(const char *command)
4430{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004431 char errormsg[256], args[1024];
4432 RESULTCODES rcodes;
4433 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004434
Victor Stinnerd6f85422010-05-05 23:33:33 +00004435 char *shell = getenv("COMSPEC");
4436 if (!shell)
4437 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004438
Victor Stinnerd6f85422010-05-05 23:33:33 +00004439 /* avoid overflowing the argument buffer */
4440 if (strlen(shell) + 3 + strlen(command) >= 1024)
4441 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004442
Victor Stinnerd6f85422010-05-05 23:33:33 +00004443 args[0] = '\0';
4444 strcat(args, shell);
4445 strcat(args, "/c ");
4446 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004447
Victor Stinnerd6f85422010-05-05 23:33:33 +00004448 /* execute asynchronously, inheriting the environment */
4449 rc = DosExecPgm(errormsg,
4450 sizeof(errormsg),
4451 EXEC_ASYNC,
4452 args,
4453 NULL,
4454 &rcodes,
4455 shell);
4456 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004457}
4458
Guido van Rossumd48f2521997-12-05 22:19:34 +00004459static FILE *
4460popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004461{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004462 int oldfd, tgtfd;
4463 HFILE pipeh[2];
4464 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004465
Victor Stinnerd6f85422010-05-05 23:33:33 +00004466 /* mode determines which of stdin or stdout is reconnected to
4467 * the pipe to the child
4468 */
4469 if (strchr(mode, 'r') != NULL) {
4470 tgt_fd = 1; /* stdout */
4471 } else if (strchr(mode, 'w')) {
4472 tgt_fd = 0; /* stdin */
4473 } else {
4474 *err = ERROR_INVALID_ACCESS;
4475 return NULL;
4476 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004477
Victor Stinnerd6f85422010-05-05 23:33:33 +00004478 /* setup the pipe */
4479 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4480 *err = rc;
4481 return NULL;
4482 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004483
Victor Stinnerd6f85422010-05-05 23:33:33 +00004484 /* prevent other threads accessing stdio */
4485 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004486
Victor Stinnerd6f85422010-05-05 23:33:33 +00004487 /* reconnect stdio and execute child */
4488 oldfd = dup(tgtfd);
4489 close(tgtfd);
4490 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4491 DosClose(pipeh[tgtfd]);
4492 rc = async_system(command);
4493 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004494
Victor Stinnerd6f85422010-05-05 23:33:33 +00004495 /* restore stdio */
4496 dup2(oldfd, tgtfd);
4497 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004498
Victor Stinnerd6f85422010-05-05 23:33:33 +00004499 /* allow other threads access to stdio */
4500 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004501
Victor Stinnerd6f85422010-05-05 23:33:33 +00004502 /* if execution of child was successful return file stream */
4503 if (rc == NO_ERROR)
4504 return fdopen(pipeh[1 - tgtfd], mode);
4505 else {
4506 DosClose(pipeh[1 - tgtfd]);
4507 *err = rc;
4508 return NULL;
4509 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004510}
4511
4512static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004513posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004514{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004515 char *name;
4516 char *mode = "r";
4517 int err, bufsize = -1;
4518 FILE *fp;
4519 PyObject *f;
4520 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4521 return NULL;
4522 Py_BEGIN_ALLOW_THREADS
4523 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4524 Py_END_ALLOW_THREADS
4525 if (fp == NULL)
4526 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004527
Victor Stinnerd6f85422010-05-05 23:33:33 +00004528 f = PyFile_FromFile(fp, name, mode, fclose);
4529 if (f != NULL)
4530 PyFile_SetBufSize(f, bufsize);
4531 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004532}
4533
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004534#elif defined(PYCC_GCC)
4535
4536/* standard posix version of popen() support */
4537static PyObject *
4538posix_popen(PyObject *self, PyObject *args)
4539{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004540 char *name;
4541 char *mode = "r";
4542 int bufsize = -1;
4543 FILE *fp;
4544 PyObject *f;
4545 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4546 return NULL;
4547 Py_BEGIN_ALLOW_THREADS
4548 fp = popen(name, mode);
4549 Py_END_ALLOW_THREADS
4550 if (fp == NULL)
4551 return posix_error();
4552 f = PyFile_FromFile(fp, name, mode, pclose);
4553 if (f != NULL)
4554 PyFile_SetBufSize(f, bufsize);
4555 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004556}
4557
4558/* fork() under OS/2 has lots'o'warts
4559 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4560 * most of this code is a ripoff of the win32 code, but using the
4561 * capabilities of EMX's C library routines
4562 */
4563
4564/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4565#define POPEN_1 1
4566#define POPEN_2 2
4567#define POPEN_3 3
4568#define POPEN_4 4
4569
4570static PyObject *_PyPopen(char *, int, int, int);
4571static int _PyPclose(FILE *file);
4572
4573/*
4574 * Internal dictionary mapping popen* file pointers to process handles,
4575 * for use when retrieving the process exit code. See _PyPclose() below
4576 * for more information on this dictionary's use.
4577 */
4578static PyObject *_PyPopenProcs = NULL;
4579
4580/* os2emx version of popen2()
4581 *
4582 * The result of this function is a pipe (file) connected to the
4583 * process's stdin, and a pipe connected to the process's stdout.
4584 */
4585
4586static PyObject *
4587os2emx_popen2(PyObject *self, PyObject *args)
4588{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004589 PyObject *f;
4590 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004591
Victor Stinnerd6f85422010-05-05 23:33:33 +00004592 char *cmdstring;
4593 char *mode = "t";
4594 int bufsize = -1;
4595 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4596 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004597
Victor Stinnerd6f85422010-05-05 23:33:33 +00004598 if (*mode == 't')
4599 tm = O_TEXT;
4600 else if (*mode != 'b') {
4601 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4602 return NULL;
4603 } else
4604 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004605
Victor Stinnerd6f85422010-05-05 23:33:33 +00004606 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004607
Victor Stinnerd6f85422010-05-05 23:33:33 +00004608 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004609}
4610
4611/*
4612 * Variation on os2emx.popen2
4613 *
4614 * The result of this function is 3 pipes - the process's stdin,
4615 * stdout and stderr
4616 */
4617
4618static PyObject *
4619os2emx_popen3(PyObject *self, PyObject *args)
4620{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004621 PyObject *f;
4622 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004623
Victor Stinnerd6f85422010-05-05 23:33:33 +00004624 char *cmdstring;
4625 char *mode = "t";
4626 int bufsize = -1;
4627 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4628 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004629
Victor Stinnerd6f85422010-05-05 23:33:33 +00004630 if (*mode == 't')
4631 tm = O_TEXT;
4632 else if (*mode != 'b') {
4633 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4634 return NULL;
4635 } else
4636 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004637
Victor Stinnerd6f85422010-05-05 23:33:33 +00004638 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004639
Victor Stinnerd6f85422010-05-05 23:33:33 +00004640 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004641}
4642
4643/*
4644 * Variation on os2emx.popen2
4645 *
Tim Peters11b23062003-04-23 02:39:17 +00004646 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004647 * and stdout+stderr combined as a single pipe.
4648 */
4649
4650static PyObject *
4651os2emx_popen4(PyObject *self, PyObject *args)
4652{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004653 PyObject *f;
4654 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004655
Victor Stinnerd6f85422010-05-05 23:33:33 +00004656 char *cmdstring;
4657 char *mode = "t";
4658 int bufsize = -1;
4659 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4660 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004661
Victor Stinnerd6f85422010-05-05 23:33:33 +00004662 if (*mode == 't')
4663 tm = O_TEXT;
4664 else if (*mode != 'b') {
4665 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4666 return NULL;
4667 } else
4668 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004669
Victor Stinnerd6f85422010-05-05 23:33:33 +00004670 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004671
Victor Stinnerd6f85422010-05-05 23:33:33 +00004672 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004673}
4674
4675/* a couple of structures for convenient handling of multiple
4676 * file handles and pipes
4677 */
4678struct file_ref
4679{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004680 int handle;
4681 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004682};
4683
4684struct pipe_ref
4685{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004686 int rd;
4687 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004688};
4689
4690/* The following code is derived from the win32 code */
4691
4692static PyObject *
4693_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4694{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004695 struct file_ref stdio[3];
4696 struct pipe_ref p_fd[3];
4697 FILE *p_s[3];
4698 int file_count, i, pipe_err;
4699 pid_t pipe_pid;
4700 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4701 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004702
Victor Stinnerd6f85422010-05-05 23:33:33 +00004703 /* file modes for subsequent fdopen's on pipe handles */
4704 if (mode == O_TEXT)
4705 {
4706 rd_mode = "rt";
4707 wr_mode = "wt";
4708 }
4709 else
4710 {
4711 rd_mode = "rb";
4712 wr_mode = "wb";
4713 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004714
Victor Stinnerd6f85422010-05-05 23:33:33 +00004715 /* prepare shell references */
4716 if ((shell = getenv("EMXSHELL")) == NULL)
4717 if ((shell = getenv("COMSPEC")) == NULL)
4718 {
4719 errno = ENOENT;
4720 return posix_error();
4721 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004722
Victor Stinnerd6f85422010-05-05 23:33:33 +00004723 sh_name = _getname(shell);
4724 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4725 opt = "/c";
4726 else
4727 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004728
Victor Stinnerd6f85422010-05-05 23:33:33 +00004729 /* save current stdio fds + their flags, and set not inheritable */
4730 i = pipe_err = 0;
4731 while (pipe_err >= 0 && i < 3)
4732 {
4733 pipe_err = stdio[i].handle = dup(i);
4734 stdio[i].flags = fcntl(i, F_GETFD, 0);
4735 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4736 i++;
4737 }
4738 if (pipe_err < 0)
4739 {
4740 /* didn't get them all saved - clean up and bail out */
4741 int saved_err = errno;
4742 while (i-- > 0)
4743 {
4744 close(stdio[i].handle);
4745 }
4746 errno = saved_err;
4747 return posix_error();
4748 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004749
Victor Stinnerd6f85422010-05-05 23:33:33 +00004750 /* create pipe ends */
4751 file_count = 2;
4752 if (n == POPEN_3)
4753 file_count = 3;
4754 i = pipe_err = 0;
4755 while ((pipe_err == 0) && (i < file_count))
4756 pipe_err = pipe((int *)&p_fd[i++]);
4757 if (pipe_err < 0)
4758 {
4759 /* didn't get them all made - clean up and bail out */
4760 while (i-- > 0)
4761 {
4762 close(p_fd[i].wr);
4763 close(p_fd[i].rd);
4764 }
4765 errno = EPIPE;
4766 return posix_error();
4767 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004768
Victor Stinnerd6f85422010-05-05 23:33:33 +00004769 /* change the actual standard IO streams over temporarily,
4770 * making the retained pipe ends non-inheritable
4771 */
4772 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004773
Victor Stinnerd6f85422010-05-05 23:33:33 +00004774 /* - stdin */
4775 if (dup2(p_fd[0].rd, 0) == 0)
4776 {
4777 close(p_fd[0].rd);
4778 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4779 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4780 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4781 {
4782 close(p_fd[0].wr);
4783 pipe_err = -1;
4784 }
4785 }
4786 else
4787 {
4788 pipe_err = -1;
4789 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004790
Victor Stinnerd6f85422010-05-05 23:33:33 +00004791 /* - stdout */
4792 if (pipe_err == 0)
4793 {
4794 if (dup2(p_fd[1].wr, 1) == 1)
4795 {
4796 close(p_fd[1].wr);
4797 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4798 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4799 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4800 {
4801 close(p_fd[1].rd);
4802 pipe_err = -1;
4803 }
4804 }
4805 else
4806 {
4807 pipe_err = -1;
4808 }
4809 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004810
Victor Stinnerd6f85422010-05-05 23:33:33 +00004811 /* - stderr, as required */
4812 if (pipe_err == 0)
4813 switch (n)
4814 {
4815 case POPEN_3:
4816 {
4817 if (dup2(p_fd[2].wr, 2) == 2)
4818 {
4819 close(p_fd[2].wr);
4820 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4821 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4822 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4823 {
4824 close(p_fd[2].rd);
4825 pipe_err = -1;
4826 }
4827 }
4828 else
4829 {
4830 pipe_err = -1;
4831 }
4832 break;
4833 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004834
Victor Stinnerd6f85422010-05-05 23:33:33 +00004835 case POPEN_4:
4836 {
4837 if (dup2(1, 2) != 2)
4838 {
4839 pipe_err = -1;
4840 }
4841 break;
4842 }
4843 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004844
Victor Stinnerd6f85422010-05-05 23:33:33 +00004845 /* spawn the child process */
4846 if (pipe_err == 0)
4847 {
4848 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4849 if (pipe_pid == -1)
4850 {
4851 pipe_err = -1;
4852 }
4853 else
4854 {
4855 /* save the PID into the FILE structure
4856 * NOTE: this implementation doesn't actually
4857 * take advantage of this, but do it for
4858 * completeness - AIM Apr01
4859 */
4860 for (i = 0; i < file_count; i++)
4861 p_s[i]->_pid = pipe_pid;
4862 }
4863 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004864
Victor Stinnerd6f85422010-05-05 23:33:33 +00004865 /* reset standard IO to normal */
4866 for (i = 0; i < 3; i++)
4867 {
4868 dup2(stdio[i].handle, i);
4869 fcntl(i, F_SETFD, stdio[i].flags);
4870 close(stdio[i].handle);
4871 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004872
Victor Stinnerd6f85422010-05-05 23:33:33 +00004873 /* if any remnant problems, clean up and bail out */
4874 if (pipe_err < 0)
4875 {
4876 for (i = 0; i < 3; i++)
4877 {
4878 close(p_fd[i].rd);
4879 close(p_fd[i].wr);
4880 }
4881 errno = EPIPE;
4882 return posix_error_with_filename(cmdstring);
4883 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004884
Victor Stinnerd6f85422010-05-05 23:33:33 +00004885 /* build tuple of file objects to return */
4886 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4887 PyFile_SetBufSize(p_f[0], bufsize);
4888 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4889 PyFile_SetBufSize(p_f[1], bufsize);
4890 if (n == POPEN_3)
4891 {
4892 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4893 PyFile_SetBufSize(p_f[0], bufsize);
4894 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4895 }
4896 else
4897 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004898
Victor Stinnerd6f85422010-05-05 23:33:33 +00004899 /*
4900 * Insert the files we've created into the process dictionary
4901 * all referencing the list with the process handle and the
4902 * initial number of files (see description below in _PyPclose).
4903 * Since if _PyPclose later tried to wait on a process when all
4904 * handles weren't closed, it could create a deadlock with the
4905 * child, we spend some energy here to try to ensure that we
4906 * either insert all file handles into the dictionary or none
4907 * at all. It's a little clumsy with the various popen modes
4908 * and variable number of files involved.
4909 */
4910 if (!_PyPopenProcs)
4911 {
4912 _PyPopenProcs = PyDict_New();
4913 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004914
Victor Stinnerd6f85422010-05-05 23:33:33 +00004915 if (_PyPopenProcs)
4916 {
4917 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4918 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004919
Victor Stinnerd6f85422010-05-05 23:33:33 +00004920 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4921 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004922
Victor Stinnerd6f85422010-05-05 23:33:33 +00004923 procObj = PyList_New(2);
4924 pidObj = PyLong_FromPid(pipe_pid);
4925 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004926
Victor Stinnerd6f85422010-05-05 23:33:33 +00004927 if (procObj && pidObj && intObj)
4928 {
4929 PyList_SetItem(procObj, 0, pidObj);
4930 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004931
Victor Stinnerd6f85422010-05-05 23:33:33 +00004932 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4933 if (fileObj[0])
4934 {
4935 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4936 fileObj[0],
4937 procObj);
4938 }
4939 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4940 if (fileObj[1])
4941 {
4942 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4943 fileObj[1],
4944 procObj);
4945 }
4946 if (file_count >= 3)
4947 {
4948 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4949 if (fileObj[2])
4950 {
4951 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4952 fileObj[2],
4953 procObj);
4954 }
4955 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004956
Victor Stinnerd6f85422010-05-05 23:33:33 +00004957 if (ins_rc[0] < 0 || !fileObj[0] ||
4958 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4959 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4960 {
4961 /* Something failed - remove any dictionary
4962 * entries that did make it.
4963 */
4964 if (!ins_rc[0] && fileObj[0])
4965 {
4966 PyDict_DelItem(_PyPopenProcs,
4967 fileObj[0]);
4968 }
4969 if (!ins_rc[1] && fileObj[1])
4970 {
4971 PyDict_DelItem(_PyPopenProcs,
4972 fileObj[1]);
4973 }
4974 if (!ins_rc[2] && fileObj[2])
4975 {
4976 PyDict_DelItem(_PyPopenProcs,
4977 fileObj[2]);
4978 }
4979 }
4980 }
Tim Peters11b23062003-04-23 02:39:17 +00004981
Victor Stinnerd6f85422010-05-05 23:33:33 +00004982 /*
4983 * Clean up our localized references for the dictionary keys
4984 * and value since PyDict_SetItem will Py_INCREF any copies
4985 * that got placed in the dictionary.
4986 */
4987 Py_XDECREF(procObj);
4988 Py_XDECREF(fileObj[0]);
4989 Py_XDECREF(fileObj[1]);
4990 Py_XDECREF(fileObj[2]);
4991 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004992
Victor Stinnerd6f85422010-05-05 23:33:33 +00004993 /* Child is launched. */
4994 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004995}
4996
4997/*
4998 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4999 * exit code for the child process and return as a result of the close.
5000 *
5001 * This function uses the _PyPopenProcs dictionary in order to map the
5002 * input file pointer to information about the process that was
5003 * originally created by the popen* call that created the file pointer.
5004 * The dictionary uses the file pointer as a key (with one entry
5005 * inserted for each file returned by the original popen* call) and a
5006 * single list object as the value for all files from a single call.
5007 * The list object contains the Win32 process handle at [0], and a file
5008 * count at [1], which is initialized to the total number of file
5009 * handles using that list.
5010 *
5011 * This function closes whichever handle it is passed, and decrements
5012 * the file count in the dictionary for the process handle pointed to
5013 * by this file. On the last close (when the file count reaches zero),
5014 * this function will wait for the child process and then return its
5015 * exit code as the result of the close() operation. This permits the
5016 * files to be closed in any order - it is always the close() of the
5017 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00005018 *
5019 * NOTE: This function is currently called with the GIL released.
5020 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005021 */
5022
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005023static int _PyPclose(FILE *file)
5024{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005025 int result;
5026 int exit_code;
5027 pid_t pipe_pid;
5028 PyObject *procObj, *pidObj, *intObj, *fileObj;
5029 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005030#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005031 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005032#endif
5033
Victor Stinnerd6f85422010-05-05 23:33:33 +00005034 /* Close the file handle first, to ensure it can't block the
5035 * child from exiting if it's the last handle.
5036 */
5037 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005038
5039#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005040 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005041#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005042 if (_PyPopenProcs)
5043 {
5044 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5045 (procObj = PyDict_GetItem(_PyPopenProcs,
5046 fileObj)) != NULL &&
5047 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
5048 (intObj = PyList_GetItem(procObj,1)) != NULL)
5049 {
5050 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
5051 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005052
Victor Stinnerd6f85422010-05-05 23:33:33 +00005053 if (file_count > 1)
5054 {
5055 /* Still other files referencing process */
5056 file_count--;
5057 PyList_SetItem(procObj,1,
5058 PyInt_FromLong((long) file_count));
5059 }
5060 else
5061 {
5062 /* Last file for this process */
5063 if (result != EOF &&
5064 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
5065 {
5066 /* extract exit status */
5067 if (WIFEXITED(exit_code))
5068 {
5069 result = WEXITSTATUS(exit_code);
5070 }
5071 else
5072 {
5073 errno = EPIPE;
5074 result = -1;
5075 }
5076 }
5077 else
5078 {
5079 /* Indicate failure - this will cause the file object
5080 * to raise an I/O error and translate the last
5081 * error code from errno. We do have a problem with
5082 * last errors that overlap the normal errno table,
5083 * but that's a consistent problem with the file object.
5084 */
5085 result = -1;
5086 }
5087 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005088
Victor Stinnerd6f85422010-05-05 23:33:33 +00005089 /* Remove this file pointer from dictionary */
5090 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005091
Victor Stinnerd6f85422010-05-05 23:33:33 +00005092 if (PyDict_Size(_PyPopenProcs) == 0)
5093 {
5094 Py_DECREF(_PyPopenProcs);
5095 _PyPopenProcs = NULL;
5096 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005097
Victor Stinnerd6f85422010-05-05 23:33:33 +00005098 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005099
Victor Stinnerd6f85422010-05-05 23:33:33 +00005100 Py_XDECREF(fileObj);
5101 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005102
5103#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005104 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005105#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005106 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005107}
5108
5109#endif /* PYCC_??? */
5110
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005111#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005112
5113/*
5114 * Portable 'popen' replacement for Win32.
5115 *
5116 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
5117 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00005118 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005119 */
5120
5121#include <malloc.h>
5122#include <io.h>
5123#include <fcntl.h>
5124
5125/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
5126#define POPEN_1 1
5127#define POPEN_2 2
5128#define POPEN_3 3
5129#define POPEN_4 4
5130
5131static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005132static int _PyPclose(FILE *file);
5133
5134/*
5135 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00005136 * for use when retrieving the process exit code. See _PyPclose() below
5137 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005138 */
5139static PyObject *_PyPopenProcs = NULL;
5140
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005141
5142/* popen that works from a GUI.
5143 *
5144 * The result of this function is a pipe (file) connected to the
5145 * processes stdin or stdout, depending on the requested mode.
5146 */
5147
5148static PyObject *
5149posix_popen(PyObject *self, PyObject *args)
5150{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005151 PyObject *f;
5152 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005153
Victor Stinnerd6f85422010-05-05 23:33:33 +00005154 char *cmdstring;
5155 char *mode = "r";
5156 int bufsize = -1;
5157 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
5158 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005159
Victor Stinnerd6f85422010-05-05 23:33:33 +00005160 if (*mode == 'r')
5161 tm = _O_RDONLY;
5162 else if (*mode != 'w') {
5163 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
5164 return NULL;
5165 } else
5166 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00005167
Victor Stinnerd6f85422010-05-05 23:33:33 +00005168 if (bufsize != -1) {
5169 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
5170 return NULL;
5171 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005172
Victor Stinnerd6f85422010-05-05 23:33:33 +00005173 if (*(mode+1) == 't')
5174 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
5175 else if (*(mode+1) == 'b')
5176 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
5177 else
5178 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005179
Victor Stinnerd6f85422010-05-05 23:33:33 +00005180 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005181}
5182
5183/* Variation on win32pipe.popen
5184 *
5185 * The result of this function is a pipe (file) connected to the
5186 * process's stdin, and a pipe connected to the process's stdout.
5187 */
5188
5189static PyObject *
5190win32_popen2(PyObject *self, PyObject *args)
5191{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005192 PyObject *f;
5193 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00005194
Victor Stinnerd6f85422010-05-05 23:33:33 +00005195 char *cmdstring;
5196 char *mode = "t";
5197 int bufsize = -1;
5198 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
5199 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005200
Victor Stinnerd6f85422010-05-05 23:33:33 +00005201 if (*mode == 't')
5202 tm = _O_TEXT;
5203 else if (*mode != 'b') {
5204 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
5205 return NULL;
5206 } else
5207 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005208
Victor Stinnerd6f85422010-05-05 23:33:33 +00005209 if (bufsize != -1) {
5210 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
5211 return NULL;
5212 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005213
Victor Stinnerd6f85422010-05-05 23:33:33 +00005214 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00005215
Victor Stinnerd6f85422010-05-05 23:33:33 +00005216 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005217}
5218
5219/*
5220 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00005221 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005222 * The result of this function is 3 pipes - the process's stdin,
5223 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005224 */
5225
5226static PyObject *
5227win32_popen3(PyObject *self, PyObject *args)
5228{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005229 PyObject *f;
5230 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005231
Victor Stinnerd6f85422010-05-05 23:33:33 +00005232 char *cmdstring;
5233 char *mode = "t";
5234 int bufsize = -1;
5235 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
5236 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005237
Victor Stinnerd6f85422010-05-05 23:33:33 +00005238 if (*mode == 't')
5239 tm = _O_TEXT;
5240 else if (*mode != 'b') {
5241 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
5242 return NULL;
5243 } else
5244 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005245
Victor Stinnerd6f85422010-05-05 23:33:33 +00005246 if (bufsize != -1) {
5247 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
5248 return NULL;
5249 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005250
Victor Stinnerd6f85422010-05-05 23:33:33 +00005251 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00005252
Victor Stinnerd6f85422010-05-05 23:33:33 +00005253 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005254}
5255
5256/*
5257 * Variation on win32pipe.popen
5258 *
Tim Peters5aa91602002-01-30 05:46:57 +00005259 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005260 * and stdout+stderr combined as a single pipe.
5261 */
5262
5263static PyObject *
5264win32_popen4(PyObject *self, PyObject *args)
5265{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005266 PyObject *f;
5267 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005268
Victor Stinnerd6f85422010-05-05 23:33:33 +00005269 char *cmdstring;
5270 char *mode = "t";
5271 int bufsize = -1;
5272 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
5273 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005274
Victor Stinnerd6f85422010-05-05 23:33:33 +00005275 if (*mode == 't')
5276 tm = _O_TEXT;
5277 else if (*mode != 'b') {
5278 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
5279 return NULL;
5280 } else
5281 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005282
Victor Stinnerd6f85422010-05-05 23:33:33 +00005283 if (bufsize != -1) {
5284 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
5285 return NULL;
5286 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005287
Victor Stinnerd6f85422010-05-05 23:33:33 +00005288 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005289
Victor Stinnerd6f85422010-05-05 23:33:33 +00005290 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005291}
5292
Mark Hammond08501372001-01-31 07:30:29 +00005293static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00005294_PyPopenCreateProcess(char *cmdstring,
Victor Stinnerd6f85422010-05-05 23:33:33 +00005295 HANDLE hStdin,
5296 HANDLE hStdout,
5297 HANDLE hStderr,
5298 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005299{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005300 PROCESS_INFORMATION piProcInfo;
5301 STARTUPINFO siStartInfo;
5302 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
5303 char *s1,*s2, *s3 = " /c ";
5304 const char *szConsoleSpawn = "w9xpopen.exe";
5305 int i;
5306 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005307
Victor Stinnerd6f85422010-05-05 23:33:33 +00005308 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
5309 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005310
Victor Stinnerd6f85422010-05-05 23:33:33 +00005311 s1 = (char *)alloca(i);
5312 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
5313 /* x < i, so x fits into an integer */
5314 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00005315
Victor Stinnerd6f85422010-05-05 23:33:33 +00005316 /* Explicitly check if we are using COMMAND.COM. If we are
5317 * then use the w9xpopen hack.
5318 */
5319 comshell = s1 + x;
5320 while (comshell >= s1 && *comshell != '\\')
5321 --comshell;
5322 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005323
Victor Stinnerd6f85422010-05-05 23:33:33 +00005324 if (GetVersion() < 0x80000000 &&
5325 _stricmp(comshell, "command.com") != 0) {
5326 /* NT/2000 and not using command.com. */
5327 x = i + strlen(s3) + strlen(cmdstring) + 1;
5328 s2 = (char *)alloca(x);
5329 ZeroMemory(s2, x);
5330 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5331 }
5332 else {
5333 /*
5334 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5335 * the workaround listed in KB: Q150956
5336 */
5337 char modulepath[_MAX_PATH];
5338 struct stat statinfo;
5339 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5340 for (x = i = 0; modulepath[i]; i++)
5341 if (modulepath[i] == SEP)
5342 x = i+1;
5343 modulepath[x] = '\0';
5344 /* Create the full-name to w9xpopen, so we can test it exists */
5345 strncat(modulepath,
5346 szConsoleSpawn,
5347 (sizeof(modulepath)/sizeof(modulepath[0]))
5348 -strlen(modulepath));
5349 if (stat(modulepath, &statinfo) != 0) {
5350 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5351 /* Eeek - file-not-found - possibly an embedding
5352 situation - see if we can locate it in sys.prefix
5353 */
5354 strncpy(modulepath,
5355 Py_GetExecPrefix(),
5356 mplen);
5357 modulepath[mplen-1] = '\0';
5358 if (modulepath[strlen(modulepath)-1] != '\\')
5359 strcat(modulepath, "\\");
5360 strncat(modulepath,
5361 szConsoleSpawn,
5362 mplen-strlen(modulepath));
5363 /* No where else to look - raise an easily identifiable
5364 error, rather than leaving Windows to report
5365 "file not found" - as the user is probably blissfully
5366 unaware this shim EXE is used, and it will confuse them.
5367 (well, it confused me for a while ;-)
5368 */
5369 if (stat(modulepath, &statinfo) != 0) {
5370 PyErr_Format(PyExc_RuntimeError,
5371 "Can not locate '%s' which is needed "
5372 "for popen to work with your shell "
5373 "or platform.",
5374 szConsoleSpawn);
5375 return FALSE;
5376 }
5377 }
5378 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5379 strlen(modulepath) +
5380 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005381
Victor Stinnerd6f85422010-05-05 23:33:33 +00005382 s2 = (char *)alloca(x);
5383 ZeroMemory(s2, x);
5384 /* To maintain correct argument passing semantics,
5385 we pass the command-line as it stands, and allow
5386 quoting to be applied. w9xpopen.exe will then
5387 use its argv vector, and re-quote the necessary
5388 args for the ultimate child process.
5389 */
5390 PyOS_snprintf(
5391 s2, x,
5392 "\"%s\" %s%s%s",
5393 modulepath,
5394 s1,
5395 s3,
5396 cmdstring);
5397 /* Not passing CREATE_NEW_CONSOLE has been known to
5398 cause random failures on win9x. Specifically a
5399 dialog:
5400 "Your program accessed mem currently in use at xxx"
5401 and a hopeful warning about the stability of your
5402 system.
5403 Cost is Ctrl+C won't kill children, but anyone
5404 who cares can have a go!
5405 */
5406 dwProcessFlags |= CREATE_NEW_CONSOLE;
5407 }
5408 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005409
Victor Stinnerd6f85422010-05-05 23:33:33 +00005410 /* Could be an else here to try cmd.exe / command.com in the path
5411 Now we'll just error out.. */
5412 else {
5413 PyErr_SetString(PyExc_RuntimeError,
5414 "Cannot locate a COMSPEC environment variable to "
5415 "use as the shell");
5416 return FALSE;
5417 }
Tim Peters5aa91602002-01-30 05:46:57 +00005418
Victor Stinnerd6f85422010-05-05 23:33:33 +00005419 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5420 siStartInfo.cb = sizeof(STARTUPINFO);
5421 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5422 siStartInfo.hStdInput = hStdin;
5423 siStartInfo.hStdOutput = hStdout;
5424 siStartInfo.hStdError = hStderr;
5425 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005426
Victor Stinnerd6f85422010-05-05 23:33:33 +00005427 if (CreateProcess(NULL,
5428 s2,
5429 NULL,
5430 NULL,
5431 TRUE,
5432 dwProcessFlags,
5433 NULL,
5434 NULL,
5435 &siStartInfo,
5436 &piProcInfo) ) {
5437 /* Close the handles now so anyone waiting is woken. */
5438 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005439
Victor Stinnerd6f85422010-05-05 23:33:33 +00005440 /* Return process handle */
5441 *hProcess = piProcInfo.hProcess;
5442 return TRUE;
5443 }
5444 win32_error("CreateProcess", s2);
5445 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005446}
5447
5448/* The following code is based off of KB: Q190351 */
5449
5450static PyObject *
5451_PyPopen(char *cmdstring, int mode, int n)
5452{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005453 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5454 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5455 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005456
Victor Stinnerd6f85422010-05-05 23:33:33 +00005457 SECURITY_ATTRIBUTES saAttr;
5458 BOOL fSuccess;
5459 int fd1, fd2, fd3;
5460 FILE *f1, *f2, *f3;
5461 long file_count;
5462 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005463
Victor Stinnerd6f85422010-05-05 23:33:33 +00005464 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5465 saAttr.bInheritHandle = TRUE;
5466 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005467
Victor Stinnerd6f85422010-05-05 23:33:33 +00005468 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5469 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005470
Victor Stinnerd6f85422010-05-05 23:33:33 +00005471 /* Create new output read handle and the input write handle. Set
5472 * the inheritance properties to FALSE. Otherwise, the child inherits
5473 * these handles; resulting in non-closeable handles to the pipes
5474 * being created. */
5475 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5476 GetCurrentProcess(), &hChildStdinWrDup, 0,
5477 FALSE,
5478 DUPLICATE_SAME_ACCESS);
5479 if (!fSuccess)
5480 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005481
Victor Stinnerd6f85422010-05-05 23:33:33 +00005482 /* Close the inheritable version of ChildStdin
5483 that we're using. */
5484 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005485
Victor Stinnerd6f85422010-05-05 23:33:33 +00005486 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5487 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005488
Victor Stinnerd6f85422010-05-05 23:33:33 +00005489 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5490 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5491 FALSE, DUPLICATE_SAME_ACCESS);
5492 if (!fSuccess)
5493 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005494
Victor Stinnerd6f85422010-05-05 23:33:33 +00005495 /* Close the inheritable version of ChildStdout
5496 that we're using. */
5497 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005498
Victor Stinnerd6f85422010-05-05 23:33:33 +00005499 if (n != POPEN_4) {
5500 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5501 return win32_error("CreatePipe", NULL);
5502 fSuccess = DuplicateHandle(GetCurrentProcess(),
5503 hChildStderrRd,
5504 GetCurrentProcess(),
5505 &hChildStderrRdDup, 0,
5506 FALSE, DUPLICATE_SAME_ACCESS);
5507 if (!fSuccess)
5508 return win32_error("DuplicateHandle", NULL);
5509 /* Close the inheritable version of ChildStdErr that we're using. */
5510 CloseHandle(hChildStderrRd);
5511 }
Tim Peters5aa91602002-01-30 05:46:57 +00005512
Victor Stinnerd6f85422010-05-05 23:33:33 +00005513 switch (n) {
5514 case POPEN_1:
5515 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5516 case _O_WRONLY | _O_TEXT:
5517 /* Case for writing to child Stdin in text mode. */
5518 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5519 f1 = _fdopen(fd1, "w");
5520 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5521 PyFile_SetBufSize(f, 0);
5522 /* We don't care about these pipes anymore, so close them. */
5523 CloseHandle(hChildStdoutRdDup);
5524 CloseHandle(hChildStderrRdDup);
5525 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005526
Victor Stinnerd6f85422010-05-05 23:33:33 +00005527 case _O_RDONLY | _O_TEXT:
5528 /* Case for reading from child Stdout in text mode. */
5529 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5530 f1 = _fdopen(fd1, "r");
5531 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5532 PyFile_SetBufSize(f, 0);
5533 /* We don't care about these pipes anymore, so close them. */
5534 CloseHandle(hChildStdinWrDup);
5535 CloseHandle(hChildStderrRdDup);
5536 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005537
Victor Stinnerd6f85422010-05-05 23:33:33 +00005538 case _O_RDONLY | _O_BINARY:
5539 /* Case for readinig from child Stdout in binary mode. */
5540 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5541 f1 = _fdopen(fd1, "rb");
5542 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5543 PyFile_SetBufSize(f, 0);
5544 /* We don't care about these pipes anymore, so close them. */
5545 CloseHandle(hChildStdinWrDup);
5546 CloseHandle(hChildStderrRdDup);
5547 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005548
Victor Stinnerd6f85422010-05-05 23:33:33 +00005549 case _O_WRONLY | _O_BINARY:
5550 /* Case for writing to child Stdin in binary mode. */
5551 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5552 f1 = _fdopen(fd1, "wb");
5553 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5554 PyFile_SetBufSize(f, 0);
5555 /* We don't care about these pipes anymore, so close them. */
5556 CloseHandle(hChildStdoutRdDup);
5557 CloseHandle(hChildStderrRdDup);
5558 break;
5559 }
5560 file_count = 1;
5561 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005562
Victor Stinnerd6f85422010-05-05 23:33:33 +00005563 case POPEN_2:
5564 case POPEN_4:
5565 {
5566 char *m1, *m2;
5567 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005568
Victor Stinnerd6f85422010-05-05 23:33:33 +00005569 if (mode & _O_TEXT) {
5570 m1 = "r";
5571 m2 = "w";
5572 } else {
5573 m1 = "rb";
5574 m2 = "wb";
5575 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005576
Victor Stinnerd6f85422010-05-05 23:33:33 +00005577 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5578 f1 = _fdopen(fd1, m2);
5579 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5580 f2 = _fdopen(fd2, m1);
5581 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5582 PyFile_SetBufSize(p1, 0);
5583 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5584 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005585
Victor Stinnerd6f85422010-05-05 23:33:33 +00005586 if (n != 4)
5587 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005588
Victor Stinnerd6f85422010-05-05 23:33:33 +00005589 f = PyTuple_Pack(2,p1,p2);
5590 Py_XDECREF(p1);
5591 Py_XDECREF(p2);
5592 file_count = 2;
5593 break;
5594 }
Tim Peters5aa91602002-01-30 05:46:57 +00005595
Victor Stinnerd6f85422010-05-05 23:33:33 +00005596 case POPEN_3:
5597 {
5598 char *m1, *m2;
5599 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005600
Victor Stinnerd6f85422010-05-05 23:33:33 +00005601 if (mode & _O_TEXT) {
5602 m1 = "r";
5603 m2 = "w";
5604 } else {
5605 m1 = "rb";
5606 m2 = "wb";
5607 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005608
Victor Stinnerd6f85422010-05-05 23:33:33 +00005609 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5610 f1 = _fdopen(fd1, m2);
5611 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5612 f2 = _fdopen(fd2, m1);
5613 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5614 f3 = _fdopen(fd3, m1);
5615 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5616 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5617 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5618 PyFile_SetBufSize(p1, 0);
5619 PyFile_SetBufSize(p2, 0);
5620 PyFile_SetBufSize(p3, 0);
5621 f = PyTuple_Pack(3,p1,p2,p3);
5622 Py_XDECREF(p1);
5623 Py_XDECREF(p2);
5624 Py_XDECREF(p3);
5625 file_count = 3;
5626 break;
5627 }
5628 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005629
Victor Stinnerd6f85422010-05-05 23:33:33 +00005630 if (n == POPEN_4) {
5631 if (!_PyPopenCreateProcess(cmdstring,
5632 hChildStdinRd,
5633 hChildStdoutWr,
5634 hChildStdoutWr,
5635 &hProcess))
5636 return NULL;
5637 }
5638 else {
5639 if (!_PyPopenCreateProcess(cmdstring,
5640 hChildStdinRd,
5641 hChildStdoutWr,
5642 hChildStderrWr,
5643 &hProcess))
5644 return NULL;
5645 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005646
Victor Stinnerd6f85422010-05-05 23:33:33 +00005647 /*
5648 * Insert the files we've created into the process dictionary
5649 * all referencing the list with the process handle and the
5650 * initial number of files (see description below in _PyPclose).
5651 * Since if _PyPclose later tried to wait on a process when all
5652 * handles weren't closed, it could create a deadlock with the
5653 * child, we spend some energy here to try to ensure that we
5654 * either insert all file handles into the dictionary or none
5655 * at all. It's a little clumsy with the various popen modes
5656 * and variable number of files involved.
5657 */
5658 if (!_PyPopenProcs) {
5659 _PyPopenProcs = PyDict_New();
5660 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005661
Victor Stinnerd6f85422010-05-05 23:33:33 +00005662 if (_PyPopenProcs) {
5663 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5664 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005665
Victor Stinnerd6f85422010-05-05 23:33:33 +00005666 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5667 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005668
Victor Stinnerd6f85422010-05-05 23:33:33 +00005669 procObj = PyList_New(2);
5670 hProcessObj = PyLong_FromVoidPtr(hProcess);
5671 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005672
Victor Stinnerd6f85422010-05-05 23:33:33 +00005673 if (procObj && hProcessObj && intObj) {
5674 PyList_SetItem(procObj,0,hProcessObj);
5675 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005676
Victor Stinnerd6f85422010-05-05 23:33:33 +00005677 fileObj[0] = PyLong_FromVoidPtr(f1);
5678 if (fileObj[0]) {
5679 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5680 fileObj[0],
5681 procObj);
5682 }
5683 if (file_count >= 2) {
5684 fileObj[1] = PyLong_FromVoidPtr(f2);
5685 if (fileObj[1]) {
5686 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5687 fileObj[1],
5688 procObj);
5689 }
5690 }
5691 if (file_count >= 3) {
5692 fileObj[2] = PyLong_FromVoidPtr(f3);
5693 if (fileObj[2]) {
5694 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5695 fileObj[2],
5696 procObj);
5697 }
5698 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005699
Victor Stinnerd6f85422010-05-05 23:33:33 +00005700 if (ins_rc[0] < 0 || !fileObj[0] ||
5701 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5702 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5703 /* Something failed - remove any dictionary
5704 * entries that did make it.
5705 */
5706 if (!ins_rc[0] && fileObj[0]) {
5707 PyDict_DelItem(_PyPopenProcs,
5708 fileObj[0]);
5709 }
5710 if (!ins_rc[1] && fileObj[1]) {
5711 PyDict_DelItem(_PyPopenProcs,
5712 fileObj[1]);
5713 }
5714 if (!ins_rc[2] && fileObj[2]) {
5715 PyDict_DelItem(_PyPopenProcs,
5716 fileObj[2]);
5717 }
5718 }
5719 }
Tim Peters5aa91602002-01-30 05:46:57 +00005720
Victor Stinnerd6f85422010-05-05 23:33:33 +00005721 /*
5722 * Clean up our localized references for the dictionary keys
5723 * and value since PyDict_SetItem will Py_INCREF any copies
5724 * that got placed in the dictionary.
5725 */
5726 Py_XDECREF(procObj);
5727 Py_XDECREF(fileObj[0]);
5728 Py_XDECREF(fileObj[1]);
5729 Py_XDECREF(fileObj[2]);
5730 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005731
Victor Stinnerd6f85422010-05-05 23:33:33 +00005732 /* Child is launched. Close the parents copy of those pipe
5733 * handles that only the child should have open. You need to
5734 * make sure that no handles to the write end of the output pipe
5735 * are maintained in this process or else the pipe will not close
5736 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005737
Victor Stinnerd6f85422010-05-05 23:33:33 +00005738 if (!CloseHandle(hChildStdinRd))
5739 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005740
Victor Stinnerd6f85422010-05-05 23:33:33 +00005741 if (!CloseHandle(hChildStdoutWr))
5742 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005743
Victor Stinnerd6f85422010-05-05 23:33:33 +00005744 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5745 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005746
Victor Stinnerd6f85422010-05-05 23:33:33 +00005747 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005748}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005749
5750/*
5751 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5752 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005753 *
5754 * This function uses the _PyPopenProcs dictionary in order to map the
5755 * input file pointer to information about the process that was
5756 * originally created by the popen* call that created the file pointer.
5757 * The dictionary uses the file pointer as a key (with one entry
5758 * inserted for each file returned by the original popen* call) and a
5759 * single list object as the value for all files from a single call.
5760 * The list object contains the Win32 process handle at [0], and a file
5761 * count at [1], which is initialized to the total number of file
5762 * handles using that list.
5763 *
5764 * This function closes whichever handle it is passed, and decrements
5765 * the file count in the dictionary for the process handle pointed to
5766 * by this file. On the last close (when the file count reaches zero),
5767 * this function will wait for the child process and then return its
5768 * exit code as the result of the close() operation. This permits the
5769 * files to be closed in any order - it is always the close() of the
5770 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005771 *
5772 * NOTE: This function is currently called with the GIL released.
5773 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005774 */
Tim Peters736aa322000-09-01 06:51:24 +00005775
Fredrik Lundh56055a42000-07-23 19:47:12 +00005776static int _PyPclose(FILE *file)
5777{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005778 int result;
5779 DWORD exit_code;
5780 HANDLE hProcess;
5781 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5782 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005783#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005784 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005785#endif
5786
Victor Stinnerd6f85422010-05-05 23:33:33 +00005787 /* Close the file handle first, to ensure it can't block the
5788 * child from exiting if it's the last handle.
5789 */
5790 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005791#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005792 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005793#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005794 if (_PyPopenProcs) {
5795 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5796 (procObj = PyDict_GetItem(_PyPopenProcs,
5797 fileObj)) != NULL &&
5798 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5799 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005800
Victor Stinnerd6f85422010-05-05 23:33:33 +00005801 hProcess = PyLong_AsVoidPtr(hProcessObj);
5802 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005803
Victor Stinnerd6f85422010-05-05 23:33:33 +00005804 if (file_count > 1) {
5805 /* Still other files referencing process */
5806 file_count--;
5807 PyList_SetItem(procObj,1,
5808 PyInt_FromLong(file_count));
5809 } else {
5810 /* Last file for this process */
5811 if (result != EOF &&
5812 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5813 GetExitCodeProcess(hProcess, &exit_code)) {
5814 /* Possible truncation here in 16-bit environments, but
5815 * real exit codes are just the lower byte in any event.
5816 */
5817 result = exit_code;
5818 } else {
5819 /* Indicate failure - this will cause the file object
5820 * to raise an I/O error and translate the last Win32
5821 * error code from errno. We do have a problem with
5822 * last errors that overlap the normal errno table,
5823 * but that's a consistent problem with the file object.
5824 */
5825 if (result != EOF) {
5826 /* If the error wasn't from the fclose(), then
5827 * set errno for the file object error handling.
5828 */
5829 errno = GetLastError();
5830 }
5831 result = -1;
5832 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005833
Victor Stinnerd6f85422010-05-05 23:33:33 +00005834 /* Free up the native handle at this point */
5835 CloseHandle(hProcess);
5836 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005837
Victor Stinnerd6f85422010-05-05 23:33:33 +00005838 /* Remove this file pointer from dictionary */
5839 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005840
Victor Stinnerd6f85422010-05-05 23:33:33 +00005841 if (PyDict_Size(_PyPopenProcs) == 0) {
5842 Py_DECREF(_PyPopenProcs);
5843 _PyPopenProcs = NULL;
5844 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005845
Victor Stinnerd6f85422010-05-05 23:33:33 +00005846 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005847
Victor Stinnerd6f85422010-05-05 23:33:33 +00005848 Py_XDECREF(fileObj);
5849 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005850
Tim Peters736aa322000-09-01 06:51:24 +00005851#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005852 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005853#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005854 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005855}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005856
5857#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005858static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005859posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005860{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005861 char *name;
5862 char *mode = "r";
5863 int bufsize = -1;
5864 FILE *fp;
5865 PyObject *f;
5866 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5867 return NULL;
5868 /* Strip mode of binary or text modifiers */
5869 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5870 mode = "r";
5871 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5872 mode = "w";
5873 Py_BEGIN_ALLOW_THREADS
5874 fp = popen(name, mode);
5875 Py_END_ALLOW_THREADS
5876 if (fp == NULL)
5877 return posix_error();
5878 f = PyFile_FromFile(fp, name, mode, pclose);
5879 if (f != NULL)
5880 PyFile_SetBufSize(f, bufsize);
5881 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005882}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005883
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005884#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005885#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005886
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005887
Guido van Rossumb6775db1994-08-01 11:34:53 +00005888#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005889PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005890"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005891Set the current process's user id.");
5892
Barry Warsaw53699e91996-12-10 23:23:01 +00005893static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005894posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005895{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005896 uid_t uid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005897 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005898 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005899 if (setuid(uid) < 0)
5900 return posix_error();
5901 Py_INCREF(Py_None);
5902 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005903}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005904#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005905
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005906
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005907#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005908PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005909"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005910Set the current process's effective user id.");
5911
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005912static PyObject *
5913posix_seteuid (PyObject *self, PyObject *args)
5914{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005915 uid_t euid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005916 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005917 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005918 if (seteuid(euid) < 0) {
5919 return posix_error();
5920 } else {
5921 Py_INCREF(Py_None);
5922 return Py_None;
5923 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005924}
5925#endif /* HAVE_SETEUID */
5926
5927#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005928PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005929"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005930Set the current process's effective group id.");
5931
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005932static PyObject *
5933posix_setegid (PyObject *self, PyObject *args)
5934{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005935 gid_t egid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005936 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005937 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005938 if (setegid(egid) < 0) {
5939 return posix_error();
5940 } else {
5941 Py_INCREF(Py_None);
5942 return Py_None;
5943 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005944}
5945#endif /* HAVE_SETEGID */
5946
5947#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005948PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005949"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005950Set the current process's real and effective user ids.");
5951
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005952static PyObject *
5953posix_setreuid (PyObject *self, PyObject *args)
5954{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005955 uid_t ruid, euid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005956 if (!PyArg_ParseTuple(args, "O&O&:setreuid",
5957 _Py_Uid_Converter, &ruid,
5958 _Py_Uid_Converter, &euid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005959 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005960 if (setreuid(ruid, euid) < 0) {
5961 return posix_error();
5962 } else {
5963 Py_INCREF(Py_None);
5964 return Py_None;
5965 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005966}
5967#endif /* HAVE_SETREUID */
5968
5969#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005970PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005971"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005972Set the current process's real and effective group ids.");
5973
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005974static PyObject *
5975posix_setregid (PyObject *self, PyObject *args)
5976{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005977 gid_t rgid, egid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005978 if (!PyArg_ParseTuple(args, "O&O&:setregid",
5979 _Py_Gid_Converter, &rgid,
5980 _Py_Gid_Converter, &egid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005981 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005982 if (setregid(rgid, egid) < 0) {
5983 return posix_error();
5984 } else {
5985 Py_INCREF(Py_None);
5986 return Py_None;
5987 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005988}
5989#endif /* HAVE_SETREGID */
5990
Guido van Rossumb6775db1994-08-01 11:34:53 +00005991#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005992PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005993"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005994Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005995
Barry Warsaw53699e91996-12-10 23:23:01 +00005996static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005997posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005998{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005999 gid_t gid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006000 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00006001 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006002 if (setgid(gid) < 0)
6003 return posix_error();
6004 Py_INCREF(Py_None);
6005 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006006}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006007#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006008
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006009#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006010PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006011"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006012Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006013
6014static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00006015posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006016{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006017 int i, len;
6018 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006019
Victor Stinnerd6f85422010-05-05 23:33:33 +00006020 if (!PySequence_Check(groups)) {
6021 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6022 return NULL;
6023 }
6024 len = PySequence_Size(groups);
6025 if (len > MAX_GROUPS) {
6026 PyErr_SetString(PyExc_ValueError, "too many groups");
6027 return NULL;
6028 }
6029 for(i = 0; i < len; i++) {
6030 PyObject *elem;
6031 elem = PySequence_GetItem(groups, i);
6032 if (!elem)
6033 return NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006034 if (!PyInt_Check(elem) && !PyLong_Check(elem)) {
6035 PyErr_SetString(PyExc_TypeError,
6036 "groups must be integers");
6037 Py_DECREF(elem);
6038 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006039 } else {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006040 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00006041 Py_DECREF(elem);
6042 return NULL;
6043 }
6044 }
6045 Py_DECREF(elem);
6046 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006047
Victor Stinnerd6f85422010-05-05 23:33:33 +00006048 if (setgroups(len, grouplist) < 0)
6049 return posix_error();
6050 Py_INCREF(Py_None);
6051 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006052}
6053#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006054
Neal Norwitz6c2f9132006-03-20 07:25:26 +00006055#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00006056static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00006057wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00006058{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006059 PyObject *result;
6060 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00006061
Victor Stinnerd6f85422010-05-05 23:33:33 +00006062 if (pid == -1)
6063 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00006064
Victor Stinnerd6f85422010-05-05 23:33:33 +00006065 if (struct_rusage == NULL) {
6066 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6067 if (m == NULL)
6068 return NULL;
6069 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6070 Py_DECREF(m);
6071 if (struct_rusage == NULL)
6072 return NULL;
6073 }
Neal Norwitz05a45592006-03-20 06:30:08 +00006074
Victor Stinnerd6f85422010-05-05 23:33:33 +00006075 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6076 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6077 if (!result)
6078 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006079
6080#ifndef doubletime
6081#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6082#endif
6083
Victor Stinnerd6f85422010-05-05 23:33:33 +00006084 PyStructSequence_SET_ITEM(result, 0,
6085 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6086 PyStructSequence_SET_ITEM(result, 1,
6087 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00006088#define SET_INT(result, index, value)\
Victor Stinnerd6f85422010-05-05 23:33:33 +00006089 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
6090 SET_INT(result, 2, ru->ru_maxrss);
6091 SET_INT(result, 3, ru->ru_ixrss);
6092 SET_INT(result, 4, ru->ru_idrss);
6093 SET_INT(result, 5, ru->ru_isrss);
6094 SET_INT(result, 6, ru->ru_minflt);
6095 SET_INT(result, 7, ru->ru_majflt);
6096 SET_INT(result, 8, ru->ru_nswap);
6097 SET_INT(result, 9, ru->ru_inblock);
6098 SET_INT(result, 10, ru->ru_oublock);
6099 SET_INT(result, 11, ru->ru_msgsnd);
6100 SET_INT(result, 12, ru->ru_msgrcv);
6101 SET_INT(result, 13, ru->ru_nsignals);
6102 SET_INT(result, 14, ru->ru_nvcsw);
6103 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00006104#undef SET_INT
6105
Victor Stinnerd6f85422010-05-05 23:33:33 +00006106 if (PyErr_Occurred()) {
6107 Py_DECREF(result);
6108 return NULL;
6109 }
Neal Norwitz05a45592006-03-20 06:30:08 +00006110
Victor Stinnerd6f85422010-05-05 23:33:33 +00006111 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00006112}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00006113#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00006114
6115#ifdef HAVE_WAIT3
6116PyDoc_STRVAR(posix_wait3__doc__,
6117"wait3(options) -> (pid, status, rusage)\n\n\
6118Wait for completion of a child process.");
6119
6120static PyObject *
6121posix_wait3(PyObject *self, PyObject *args)
6122{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006123 pid_t pid;
6124 int options;
6125 struct rusage ru;
6126 WAIT_TYPE status;
6127 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006128
Victor Stinnerd6f85422010-05-05 23:33:33 +00006129 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6130 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006131
Victor Stinnerd6f85422010-05-05 23:33:33 +00006132 Py_BEGIN_ALLOW_THREADS
6133 pid = wait3(&status, options, &ru);
6134 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006135
Victor Stinnerd6f85422010-05-05 23:33:33 +00006136 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006137}
6138#endif /* HAVE_WAIT3 */
6139
6140#ifdef HAVE_WAIT4
6141PyDoc_STRVAR(posix_wait4__doc__,
6142"wait4(pid, options) -> (pid, status, rusage)\n\n\
6143Wait for completion of a given child process.");
6144
6145static PyObject *
6146posix_wait4(PyObject *self, PyObject *args)
6147{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006148 pid_t pid;
6149 int options;
6150 struct rusage ru;
6151 WAIT_TYPE status;
6152 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006153
Victor Stinnerd6f85422010-05-05 23:33:33 +00006154 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
6155 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006156
Victor Stinnerd6f85422010-05-05 23:33:33 +00006157 Py_BEGIN_ALLOW_THREADS
6158 pid = wait4(pid, &status, options, &ru);
6159 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006160
Victor Stinnerd6f85422010-05-05 23:33:33 +00006161 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006162}
6163#endif /* HAVE_WAIT4 */
6164
Guido van Rossumb6775db1994-08-01 11:34:53 +00006165#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006166PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006167"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006168Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006169
Barry Warsaw53699e91996-12-10 23:23:01 +00006170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006171posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006172{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006173 pid_t pid;
6174 int options;
6175 WAIT_TYPE status;
6176 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006177
Victor Stinnerd6f85422010-05-05 23:33:33 +00006178 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6179 return NULL;
6180 Py_BEGIN_ALLOW_THREADS
6181 pid = waitpid(pid, &status, options);
6182 Py_END_ALLOW_THREADS
6183 if (pid == -1)
6184 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006185
Victor Stinnerd6f85422010-05-05 23:33:33 +00006186 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006187}
6188
Tim Petersab034fa2002-02-01 11:27:43 +00006189#elif defined(HAVE_CWAIT)
6190
6191/* MS C has a variant of waitpid() that's usable for most purposes. */
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 << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006194"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006195
6196static PyObject *
6197posix_waitpid(PyObject *self, PyObject *args)
6198{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006199 Py_intptr_t pid;
6200 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006201
Victor Stinnerd6f85422010-05-05 23:33:33 +00006202 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6203 return NULL;
6204 Py_BEGIN_ALLOW_THREADS
6205 pid = _cwait(&status, pid, options);
6206 Py_END_ALLOW_THREADS
6207 if (pid == -1)
6208 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006209
Victor Stinnerd6f85422010-05-05 23:33:33 +00006210 /* shift the status left a byte so this is more like the POSIX waitpid */
6211 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006212}
6213#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006214
Guido van Rossumad0ee831995-03-01 10:34:45 +00006215#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006217"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006218Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006219
Barry Warsaw53699e91996-12-10 23:23:01 +00006220static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006221posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006222{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006223 pid_t pid;
6224 WAIT_TYPE status;
6225 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006226
Victor Stinnerd6f85422010-05-05 23:33:33 +00006227 Py_BEGIN_ALLOW_THREADS
6228 pid = wait(&status);
6229 Py_END_ALLOW_THREADS
6230 if (pid == -1)
6231 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006232
Victor Stinnerd6f85422010-05-05 23:33:33 +00006233 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006234}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006235#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006236
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006239"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006240Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006241
Barry Warsaw53699e91996-12-10 23:23:01 +00006242static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006243posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006244{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006245#ifdef HAVE_LSTAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00006246 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006247#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006248#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006249 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006250#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006251 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006252#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006253#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006254}
6255
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006256
Guido van Rossumb6775db1994-08-01 11:34:53 +00006257#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006258PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006259"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006260Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006261
Barry Warsaw53699e91996-12-10 23:23:01 +00006262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006263posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006264{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006265 PyObject* v;
6266 char buf[MAXPATHLEN];
6267 char *path;
6268 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006269#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006270 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006271#endif
6272
Victor Stinnerd6f85422010-05-05 23:33:33 +00006273 if (!PyArg_ParseTuple(args, "et:readlink",
6274 Py_FileSystemDefaultEncoding, &path))
6275 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006276#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006277 v = PySequence_GetItem(args, 0);
6278 if (v == NULL) {
6279 PyMem_Free(path);
6280 return NULL;
6281 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006282
Victor Stinnerd6f85422010-05-05 23:33:33 +00006283 if (PyUnicode_Check(v)) {
6284 arg_is_unicode = 1;
6285 }
6286 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006287#endif
6288
Victor Stinnerd6f85422010-05-05 23:33:33 +00006289 Py_BEGIN_ALLOW_THREADS
6290 n = readlink(path, buf, (int) sizeof buf);
6291 Py_END_ALLOW_THREADS
6292 if (n < 0)
6293 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006294
Victor Stinnerd6f85422010-05-05 23:33:33 +00006295 PyMem_Free(path);
6296 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006297#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006298 if (arg_is_unicode) {
6299 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006300
Victor Stinnerd6f85422010-05-05 23:33:33 +00006301 w = PyUnicode_FromEncodedObject(v,
6302 Py_FileSystemDefaultEncoding,
6303 "strict");
6304 if (w != NULL) {
6305 Py_DECREF(v);
6306 v = w;
6307 }
6308 else {
6309 /* fall back to the original byte string, as
6310 discussed in patch #683592 */
6311 PyErr_Clear();
6312 }
6313 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006314#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006315 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006316}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006317#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006318
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006319
Guido van Rossumb6775db1994-08-01 11:34:53 +00006320#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006321PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006322"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006323Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006324
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006325static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006326posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006327{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006328 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006329}
6330#endif /* HAVE_SYMLINK */
6331
6332
6333#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006334#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6335static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006336system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006337{
6338 ULONG value = 0;
6339
6340 Py_BEGIN_ALLOW_THREADS
6341 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6342 Py_END_ALLOW_THREADS
6343
6344 return value;
6345}
6346
6347static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006348posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006349{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006350 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006351 return Py_BuildValue("ddddd",
6352 (double)0 /* t.tms_utime / HZ */,
6353 (double)0 /* t.tms_stime / HZ */,
6354 (double)0 /* t.tms_cutime / HZ */,
6355 (double)0 /* t.tms_cstime / HZ */,
6356 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006357}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006358#else /* not OS2 */
Martin v. Löwis03824e42008-12-29 18:17:34 +00006359#define NEED_TICKS_PER_SECOND
6360static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006361static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006362posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006363{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006364 struct tms t;
6365 clock_t c;
6366 errno = 0;
6367 c = times(&t);
6368 if (c == (clock_t) -1)
6369 return posix_error();
6370 return Py_BuildValue("ddddd",
6371 (double)t.tms_utime / ticks_per_second,
6372 (double)t.tms_stime / ticks_per_second,
6373 (double)t.tms_cutime / ticks_per_second,
6374 (double)t.tms_cstime / ticks_per_second,
6375 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006376}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006377#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006378#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006379
6380
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006381#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006382#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006383static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006384posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006385{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006386 FILETIME create, exit, kernel, user;
6387 HANDLE hProc;
6388 hProc = GetCurrentProcess();
6389 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6390 /* The fields of a FILETIME structure are the hi and lo part
6391 of a 64-bit value expressed in 100 nanosecond units.
6392 1e7 is one second in such units; 1e-7 the inverse.
6393 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6394 */
6395 return Py_BuildValue(
6396 "ddddd",
6397 (double)(user.dwHighDateTime*429.4967296 +
6398 user.dwLowDateTime*1e-7),
6399 (double)(kernel.dwHighDateTime*429.4967296 +
6400 kernel.dwLowDateTime*1e-7),
6401 (double)0,
6402 (double)0,
6403 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006404}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006405#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006406
6407#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006408PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006409"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006410Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006411#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006413
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006414#ifdef HAVE_GETSID
6415PyDoc_STRVAR(posix_getsid__doc__,
6416"getsid(pid) -> sid\n\n\
6417Call the system call getsid().");
6418
6419static PyObject *
6420posix_getsid(PyObject *self, PyObject *args)
6421{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006422 pid_t pid;
6423 int sid;
6424 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6425 return NULL;
6426 sid = getsid(pid);
6427 if (sid < 0)
6428 return posix_error();
6429 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006430}
6431#endif /* HAVE_GETSID */
6432
6433
Guido van Rossumb6775db1994-08-01 11:34:53 +00006434#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006435PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006436"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006437Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006438
Barry Warsaw53699e91996-12-10 23:23:01 +00006439static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006440posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006441{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006442 if (setsid() < 0)
6443 return posix_error();
6444 Py_INCREF(Py_None);
6445 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006446}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006447#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006448
Guido van Rossumb6775db1994-08-01 11:34:53 +00006449#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006450PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006451"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006452Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006453
Barry Warsaw53699e91996-12-10 23:23:01 +00006454static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006455posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006456{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006457 pid_t pid;
6458 int pgrp;
6459 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6460 return NULL;
6461 if (setpgid(pid, pgrp) < 0)
6462 return posix_error();
6463 Py_INCREF(Py_None);
6464 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006465}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006466#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006468
Guido van Rossumb6775db1994-08-01 11:34:53 +00006469#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006470PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006471"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006472Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006473
Barry Warsaw53699e91996-12-10 23:23:01 +00006474static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006475posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006476{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006477 int fd;
6478 pid_t pgid;
6479 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6480 return NULL;
6481 pgid = tcgetpgrp(fd);
6482 if (pgid < 0)
6483 return posix_error();
6484 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006485}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006486#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006487
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006488
Guido van Rossumb6775db1994-08-01 11:34:53 +00006489#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006490PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006491"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006492Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006493
Barry Warsaw53699e91996-12-10 23:23:01 +00006494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006495posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006496{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006497 int fd;
6498 pid_t pgid;
6499 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6500 return NULL;
6501 if (tcsetpgrp(fd, pgid) < 0)
6502 return posix_error();
6503 Py_INCREF(Py_None);
6504 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006505}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006506#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006507
Guido van Rossum687dd131993-05-17 08:34:16 +00006508/* Functions acting on file descriptors */
6509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006510PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006511"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006512Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006513
Barry Warsaw53699e91996-12-10 23:23:01 +00006514static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006515posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006516{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006517 char *file = NULL;
6518 int flag;
6519 int mode = 0777;
6520 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006521
6522#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006523 PyUnicodeObject *po;
6524 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6525 Py_BEGIN_ALLOW_THREADS
6526 /* PyUnicode_AS_UNICODE OK without thread
6527 lock as it is a simple dereference. */
6528 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6529 Py_END_ALLOW_THREADS
6530 if (fd < 0)
6531 return posix_error();
6532 return PyInt_FromLong((long)fd);
6533 }
6534 /* Drop the argument parsing error as narrow strings
6535 are also valid. */
6536 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006537#endif
6538
Victor Stinnerd6f85422010-05-05 23:33:33 +00006539 if (!PyArg_ParseTuple(args, "eti|i",
6540 Py_FileSystemDefaultEncoding, &file,
6541 &flag, &mode))
6542 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006543
Victor Stinnerd6f85422010-05-05 23:33:33 +00006544 Py_BEGIN_ALLOW_THREADS
6545 fd = open(file, flag, mode);
6546 Py_END_ALLOW_THREADS
6547 if (fd < 0)
6548 return posix_error_with_allocated_filename(file);
6549 PyMem_Free(file);
6550 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006551}
6552
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006554PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006555"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006556Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006557
Barry Warsaw53699e91996-12-10 23:23:01 +00006558static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006559posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006560{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006561 int fd, res;
6562 if (!PyArg_ParseTuple(args, "i:close", &fd))
6563 return NULL;
6564 if (!_PyVerify_fd(fd))
6565 return posix_error();
6566 Py_BEGIN_ALLOW_THREADS
6567 res = close(fd);
6568 Py_END_ALLOW_THREADS
6569 if (res < 0)
6570 return posix_error();
6571 Py_INCREF(Py_None);
6572 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006573}
6574
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006575
Victor Stinnerd6f85422010-05-05 23:33:33 +00006576PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006577"closerange(fd_low, fd_high)\n\n\
6578Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6579
6580static PyObject *
6581posix_closerange(PyObject *self, PyObject *args)
6582{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006583 int fd_from, fd_to, i;
6584 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6585 return NULL;
6586 Py_BEGIN_ALLOW_THREADS
6587 for (i = fd_from; i < fd_to; i++)
6588 if (_PyVerify_fd(i))
6589 close(i);
6590 Py_END_ALLOW_THREADS
6591 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006592}
6593
6594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006595PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006596"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006597Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006598
Barry Warsaw53699e91996-12-10 23:23:01 +00006599static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006600posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006601{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006602 int fd;
6603 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6604 return NULL;
6605 if (!_PyVerify_fd(fd))
6606 return posix_error();
6607 Py_BEGIN_ALLOW_THREADS
6608 fd = dup(fd);
6609 Py_END_ALLOW_THREADS
6610 if (fd < 0)
6611 return posix_error();
6612 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006613}
6614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006616PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006617"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006618Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006619
Barry Warsaw53699e91996-12-10 23:23:01 +00006620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006621posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006622{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006623 int fd, fd2, res;
6624 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6625 return NULL;
6626 if (!_PyVerify_fd_dup2(fd, fd2))
6627 return posix_error();
6628 Py_BEGIN_ALLOW_THREADS
6629 res = dup2(fd, fd2);
6630 Py_END_ALLOW_THREADS
6631 if (res < 0)
6632 return posix_error();
6633 Py_INCREF(Py_None);
6634 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006635}
6636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006638PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006639"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006640Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006641
Barry Warsaw53699e91996-12-10 23:23:01 +00006642static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006643posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006644{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006645 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006646#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006647 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006648#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006649 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006650#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006651 PyObject *posobj;
6652 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6653 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006654#ifdef SEEK_SET
Victor Stinnerd6f85422010-05-05 23:33:33 +00006655 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6656 switch (how) {
6657 case 0: how = SEEK_SET; break;
6658 case 1: how = SEEK_CUR; break;
6659 case 2: how = SEEK_END; break;
6660 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006661#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006662
6663#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006664 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006665#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006666 pos = PyLong_Check(posobj) ?
6667 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006668#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006669 if (PyErr_Occurred())
6670 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006671
Victor Stinnerd6f85422010-05-05 23:33:33 +00006672 if (!_PyVerify_fd(fd))
6673 return posix_error();
6674 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006675#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006676 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006677#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006678 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006679#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006680 Py_END_ALLOW_THREADS
6681 if (res < 0)
6682 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006683
6684#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006685 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006686#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006687 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006688#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006689}
6690
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006692PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006693"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006694Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006695
Barry Warsaw53699e91996-12-10 23:23:01 +00006696static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006697posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006698{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006699 int fd, size, n;
6700 PyObject *buffer;
6701 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6702 return NULL;
6703 if (size < 0) {
6704 errno = EINVAL;
6705 return posix_error();
6706 }
6707 buffer = PyString_FromStringAndSize((char *)NULL, size);
6708 if (buffer == NULL)
6709 return NULL;
Stefan Krahacaab2a2010-11-26 15:06:27 +00006710 if (!_PyVerify_fd(fd)) {
6711 Py_DECREF(buffer);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006712 return posix_error();
Stefan Krahacaab2a2010-11-26 15:06:27 +00006713 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00006714 Py_BEGIN_ALLOW_THREADS
6715 n = read(fd, PyString_AsString(buffer), size);
6716 Py_END_ALLOW_THREADS
6717 if (n < 0) {
6718 Py_DECREF(buffer);
6719 return posix_error();
6720 }
6721 if (n != size)
6722 _PyString_Resize(&buffer, n);
6723 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006724}
6725
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006727PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006728"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006729Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006730
Barry Warsaw53699e91996-12-10 23:23:01 +00006731static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006732posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006733{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006734 Py_buffer pbuf;
6735 int fd;
Victor Stinner59729ff2011-07-05 11:28:19 +02006736 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006737
Victor Stinnerd6f85422010-05-05 23:33:33 +00006738 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6739 return NULL;
Stefan Krahacaab2a2010-11-26 15:06:27 +00006740 if (!_PyVerify_fd(fd)) {
6741 PyBuffer_Release(&pbuf);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006742 return posix_error();
Stefan Krahacaab2a2010-11-26 15:06:27 +00006743 }
Victor Stinner59729ff2011-07-05 11:28:19 +02006744 len = pbuf.len;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006745 Py_BEGIN_ALLOW_THREADS
Victor Stinner59729ff2011-07-05 11:28:19 +02006746#if defined(MS_WIN64) || defined(MS_WINDOWS)
6747 if (len > INT_MAX)
6748 len = INT_MAX;
6749 size = write(fd, pbuf.buf, (int)len);
6750#else
6751 size = write(fd, pbuf.buf, len);
6752#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006753 Py_END_ALLOW_THREADS
Stefan Krahacaab2a2010-11-26 15:06:27 +00006754 PyBuffer_Release(&pbuf);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006755 if (size < 0)
6756 return posix_error();
6757 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006758}
6759
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006761PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006762"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006763Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006764
Barry Warsaw53699e91996-12-10 23:23:01 +00006765static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006766posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006767{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006768 int fd;
6769 STRUCT_STAT st;
6770 int res;
6771 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6772 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006773#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006774 /* on OpenVMS we must ensure that all bytes are written to the file */
6775 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006776#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006777 if (!_PyVerify_fd(fd))
6778 return posix_error();
6779 Py_BEGIN_ALLOW_THREADS
6780 res = FSTAT(fd, &st);
6781 Py_END_ALLOW_THREADS
6782 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006783#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006784 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006785#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006786 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006787#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006788 }
Tim Peters5aa91602002-01-30 05:46:57 +00006789
Victor Stinnerd6f85422010-05-05 23:33:33 +00006790 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006791}
6792
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006794PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006795"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006796Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006797
Barry Warsaw53699e91996-12-10 23:23:01 +00006798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006799posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006800{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006801 int fd;
6802 char *orgmode = "r";
6803 int bufsize = -1;
6804 FILE *fp;
6805 PyObject *f;
6806 char *mode;
6807 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6808 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006809
Victor Stinnerd6f85422010-05-05 23:33:33 +00006810 /* Sanitize mode. See fileobject.c */
6811 mode = PyMem_MALLOC(strlen(orgmode)+3);
6812 if (!mode) {
6813 PyErr_NoMemory();
6814 return NULL;
6815 }
6816 strcpy(mode, orgmode);
6817 if (_PyFile_SanitizeMode(mode)) {
6818 PyMem_FREE(mode);
6819 return NULL;
6820 }
6821 if (!_PyVerify_fd(fd))
6822 return posix_error();
6823 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006824#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006825 if (mode[0] == 'a') {
6826 /* try to make sure the O_APPEND flag is set */
6827 int flags;
6828 flags = fcntl(fd, F_GETFL);
6829 if (flags != -1)
6830 fcntl(fd, F_SETFL, flags | O_APPEND);
6831 fp = fdopen(fd, mode);
6832 if (fp == NULL && flags != -1)
6833 /* restore old mode if fdopen failed */
6834 fcntl(fd, F_SETFL, flags);
6835 } else {
6836 fp = fdopen(fd, mode);
6837 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006838#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006839 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006840#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006841 Py_END_ALLOW_THREADS
6842 PyMem_FREE(mode);
6843 if (fp == NULL)
6844 return posix_error();
Nadeem Vawdad7664de2012-01-19 00:40:46 +02006845 /* The dummy filename used here must be kept in sync with the value
6846 tested against in gzip.GzipFile.__init__() - see issue #13781. */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006847 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6848 if (f != NULL)
6849 PyFile_SetBufSize(f, bufsize);
6850 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006851}
6852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006853PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006854"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006855Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006856connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006857
6858static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006859posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006860{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006861 int fd;
6862 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6863 return NULL;
6864 if (!_PyVerify_fd(fd))
6865 return PyBool_FromLong(0);
6866 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006867}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006868
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006869#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006870PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006871"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006872Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006873
Barry Warsaw53699e91996-12-10 23:23:01 +00006874static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006875posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006876{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006877#if defined(PYOS_OS2)
6878 HFILE read, write;
6879 APIRET rc;
6880
Victor Stinnerd6f85422010-05-05 23:33:33 +00006881 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006882 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006883 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006884 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006885 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006886
6887 return Py_BuildValue("(ii)", read, write);
6888#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006889#if !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006890 int fds[2];
6891 int res;
6892 Py_BEGIN_ALLOW_THREADS
6893 res = pipe(fds);
6894 Py_END_ALLOW_THREADS
6895 if (res != 0)
6896 return posix_error();
6897 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006898#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006899 HANDLE read, write;
6900 int read_fd, write_fd;
6901 BOOL ok;
6902 Py_BEGIN_ALLOW_THREADS
6903 ok = CreatePipe(&read, &write, NULL, 0);
6904 Py_END_ALLOW_THREADS
6905 if (!ok)
6906 return win32_error("CreatePipe", NULL);
6907 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6908 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6909 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006910#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006911#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006912}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006913#endif /* HAVE_PIPE */
6914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006915
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006916#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006917PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006918"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006919Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006920
Barry Warsaw53699e91996-12-10 23:23:01 +00006921static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006922posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006923{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006924 char *filename;
6925 int mode = 0666;
6926 int res;
6927 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6928 return NULL;
6929 Py_BEGIN_ALLOW_THREADS
6930 res = mkfifo(filename, mode);
6931 Py_END_ALLOW_THREADS
6932 if (res < 0)
6933 return posix_error();
6934 Py_INCREF(Py_None);
6935 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006936}
6937#endif
6938
6939
Neal Norwitz11690112002-07-30 01:08:28 +00006940#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006941PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006942"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006943Create a filesystem node (file, device special file or named pipe)\n\
6944named filename. mode specifies both the permissions to use and the\n\
6945type of node to be created, being combined (bitwise OR) with one of\n\
6946S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006947device defines the newly created device special file (probably using\n\
6948os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006949
6950
6951static PyObject *
6952posix_mknod(PyObject *self, PyObject *args)
6953{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006954 char *filename;
6955 int mode = 0600;
6956 int device = 0;
6957 int res;
6958 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6959 return NULL;
6960 Py_BEGIN_ALLOW_THREADS
6961 res = mknod(filename, mode, device);
6962 Py_END_ALLOW_THREADS
6963 if (res < 0)
6964 return posix_error();
6965 Py_INCREF(Py_None);
6966 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006967}
6968#endif
6969
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006970#ifdef HAVE_DEVICE_MACROS
6971PyDoc_STRVAR(posix_major__doc__,
6972"major(device) -> major number\n\
6973Extracts a device major number from a raw device number.");
6974
6975static PyObject *
6976posix_major(PyObject *self, PyObject *args)
6977{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006978 int device;
6979 if (!PyArg_ParseTuple(args, "i:major", &device))
6980 return NULL;
6981 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006982}
6983
6984PyDoc_STRVAR(posix_minor__doc__,
6985"minor(device) -> minor number\n\
6986Extracts a device minor number from a raw device number.");
6987
6988static PyObject *
6989posix_minor(PyObject *self, PyObject *args)
6990{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006991 int device;
6992 if (!PyArg_ParseTuple(args, "i:minor", &device))
6993 return NULL;
6994 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006995}
6996
6997PyDoc_STRVAR(posix_makedev__doc__,
6998"makedev(major, minor) -> device number\n\
6999Composes a raw device number from the major and minor device numbers.");
7000
7001static PyObject *
7002posix_makedev(PyObject *self, PyObject *args)
7003{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007004 int major, minor;
7005 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7006 return NULL;
7007 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007008}
7009#endif /* device macros */
7010
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007011
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007012#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007013PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007014"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007015Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007016
Barry Warsaw53699e91996-12-10 23:23:01 +00007017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007018posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007019{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007020 int fd;
7021 off_t length;
7022 int res;
7023 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007024
Victor Stinnerd6f85422010-05-05 23:33:33 +00007025 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
7026 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007027
7028#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007029 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007030#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007031 length = PyLong_Check(lenobj) ?
7032 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007033#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007034 if (PyErr_Occurred())
7035 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007036
Victor Stinnerd6f85422010-05-05 23:33:33 +00007037 Py_BEGIN_ALLOW_THREADS
7038 res = ftruncate(fd, length);
7039 Py_END_ALLOW_THREADS
7040 if (res < 0)
7041 return posix_error();
7042 Py_INCREF(Py_None);
7043 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007044}
7045#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007046
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007047#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007048PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007049"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007050Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007051
Fred Drake762e2061999-08-26 17:23:54 +00007052/* Save putenv() parameters as values here, so we can collect them when they
7053 * get re-set with another call for the same key. */
7054static PyObject *posix_putenv_garbage;
7055
Tim Peters5aa91602002-01-30 05:46:57 +00007056static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007057posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007058{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007059 char *s1, *s2;
7060 char *newenv;
7061 PyObject *newstr;
7062 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007063
Victor Stinnerd6f85422010-05-05 23:33:33 +00007064 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
7065 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007066
7067#if defined(PYOS_OS2)
7068 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7069 APIRET rc;
7070
Guido van Rossumd48f2521997-12-05 22:19:34 +00007071 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
7072 if (rc != NO_ERROR)
7073 return os2_error(rc);
7074
7075 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7076 APIRET rc;
7077
Guido van Rossumd48f2521997-12-05 22:19:34 +00007078 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
7079 if (rc != NO_ERROR)
7080 return os2_error(rc);
7081 } else {
7082#endif
7083
Victor Stinnerd6f85422010-05-05 23:33:33 +00007084 /* XXX This can leak memory -- not easy to fix :-( */
7085 len = strlen(s1) + strlen(s2) + 2;
Victor Stinner53853c32011-11-22 22:20:13 +01007086#ifdef MS_WINDOWS
7087 if (_MAX_ENV < (len - 1)) {
7088 PyErr_Format(PyExc_ValueError,
7089 "the environment variable is longer than %u bytes",
7090 _MAX_ENV);
7091 return NULL;
7092 }
7093#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007094 /* len includes space for a trailing \0; the size arg to
7095 PyString_FromStringAndSize does not count that */
7096 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
7097 if (newstr == NULL)
7098 return PyErr_NoMemory();
7099 newenv = PyString_AS_STRING(newstr);
7100 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7101 if (putenv(newenv)) {
7102 Py_DECREF(newstr);
7103 posix_error();
7104 return NULL;
7105 }
7106 /* Install the first arg and newstr in posix_putenv_garbage;
7107 * this will cause previous value to be collected. This has to
7108 * happen after the real putenv() call because the old value
7109 * was still accessible until then. */
7110 if (PyDict_SetItem(posix_putenv_garbage,
7111 PyTuple_GET_ITEM(args, 0), newstr)) {
7112 /* really not much we can do; just leak */
7113 PyErr_Clear();
7114 }
7115 else {
7116 Py_DECREF(newstr);
7117 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007118
7119#if defined(PYOS_OS2)
7120 }
7121#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007122 Py_INCREF(Py_None);
7123 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007124}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007125#endif /* putenv */
7126
Guido van Rossumc524d952001-10-19 01:31:59 +00007127#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007128PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007129"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007130Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007131
7132static PyObject *
7133posix_unsetenv(PyObject *self, PyObject *args)
7134{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007135 char *s1;
Charles-François Natali93a11752011-11-27 13:01:35 +01007136#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner53853c32011-11-22 22:20:13 +01007137 int err;
Charles-François Natali93a11752011-11-27 13:01:35 +01007138#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007139
Victor Stinnerd6f85422010-05-05 23:33:33 +00007140 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7141 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007142
Charles-François Natali93a11752011-11-27 13:01:35 +01007143#ifdef HAVE_BROKEN_UNSETENV
7144 unsetenv(s1);
7145#else
Victor Stinner53853c32011-11-22 22:20:13 +01007146 err = unsetenv(s1);
Benjamin Peterson42d96dc2011-11-22 23:56:06 -06007147 if (err)
Victor Stinner53853c32011-11-22 22:20:13 +01007148 return posix_error();
Charles-François Natali93a11752011-11-27 13:01:35 +01007149#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007150
Victor Stinnerd6f85422010-05-05 23:33:33 +00007151 /* Remove the key from posix_putenv_garbage;
7152 * this will cause it to be collected. This has to
7153 * happen after the real unsetenv() call because the
7154 * old value was still accessible until then.
7155 */
7156 if (PyDict_DelItem(posix_putenv_garbage,
7157 PyTuple_GET_ITEM(args, 0))) {
7158 /* really not much we can do; just leak */
7159 PyErr_Clear();
7160 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007161
Victor Stinnerd6f85422010-05-05 23:33:33 +00007162 Py_INCREF(Py_None);
7163 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00007164}
7165#endif /* unsetenv */
7166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007167PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007168"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007169Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007170
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007172posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007173{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007174 int code;
7175 char *message;
7176 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7177 return NULL;
7178 message = strerror(code);
7179 if (message == NULL) {
7180 PyErr_SetString(PyExc_ValueError,
7181 "strerror() argument out of range");
7182 return NULL;
7183 }
7184 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007185}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007186
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007187
Guido van Rossumc9641791998-08-04 15:26:23 +00007188#ifdef HAVE_SYS_WAIT_H
7189
Fred Drake106c1a02002-04-23 15:58:02 +00007190#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007191PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007192"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007193Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007194
7195static PyObject *
7196posix_WCOREDUMP(PyObject *self, PyObject *args)
7197{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007198 WAIT_TYPE status;
7199 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007200
Victor Stinnerd6f85422010-05-05 23:33:33 +00007201 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7202 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007203
Victor Stinnerd6f85422010-05-05 23:33:33 +00007204 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007205}
7206#endif /* WCOREDUMP */
7207
7208#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007209PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007210"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007211Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007212job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007213
7214static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007215posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007216{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007217 WAIT_TYPE status;
7218 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007219
Victor Stinnerd6f85422010-05-05 23:33:33 +00007220 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7221 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007222
Victor Stinnerd6f85422010-05-05 23:33:33 +00007223 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007224}
7225#endif /* WIFCONTINUED */
7226
Guido van Rossumc9641791998-08-04 15:26:23 +00007227#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007228PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007229"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007230Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007231
7232static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007233posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007234{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007235 WAIT_TYPE status;
7236 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007237
Victor Stinnerd6f85422010-05-05 23:33:33 +00007238 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7239 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007240
Victor Stinnerd6f85422010-05-05 23:33:33 +00007241 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007242}
7243#endif /* WIFSTOPPED */
7244
7245#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007246PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007247"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007248Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007249
7250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007251posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007252{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007253 WAIT_TYPE status;
7254 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007255
Victor Stinnerd6f85422010-05-05 23:33:33 +00007256 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7257 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007258
Victor Stinnerd6f85422010-05-05 23:33:33 +00007259 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007260}
7261#endif /* WIFSIGNALED */
7262
7263#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007264PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007265"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007266Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007267system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007268
7269static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007270posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007271{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007272 WAIT_TYPE status;
7273 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007274
Victor Stinnerd6f85422010-05-05 23:33:33 +00007275 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7276 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007277
Victor Stinnerd6f85422010-05-05 23:33:33 +00007278 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007279}
7280#endif /* WIFEXITED */
7281
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007282#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007283PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007284"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007285Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007286
7287static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007288posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007289{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007290 WAIT_TYPE status;
7291 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007292
Victor Stinnerd6f85422010-05-05 23:33:33 +00007293 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7294 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007295
Victor Stinnerd6f85422010-05-05 23:33:33 +00007296 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007297}
7298#endif /* WEXITSTATUS */
7299
7300#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007301PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007302"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007303Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007304value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007305
7306static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007307posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007308{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007309 WAIT_TYPE status;
7310 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007311
Victor Stinnerd6f85422010-05-05 23:33:33 +00007312 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7313 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007314
Victor Stinnerd6f85422010-05-05 23:33:33 +00007315 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007316}
7317#endif /* WTERMSIG */
7318
7319#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007320PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007321"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007322Return the signal that stopped the process that provided\n\
7323the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007324
7325static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007326posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007327{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007328 WAIT_TYPE status;
7329 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007330
Victor Stinnerd6f85422010-05-05 23:33:33 +00007331 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7332 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007333
Victor Stinnerd6f85422010-05-05 23:33:33 +00007334 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007335}
7336#endif /* WSTOPSIG */
7337
7338#endif /* HAVE_SYS_WAIT_H */
7339
7340
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007341#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007342#ifdef _SCO_DS
7343/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7344 needed definitions in sys/statvfs.h */
7345#define _SVID3
7346#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007347#include <sys/statvfs.h>
7348
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007349static PyObject*
7350_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007351 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7352 if (v == NULL)
7353 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007354
7355#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007356 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7357 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7358 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7359 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7360 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7361 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7362 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7363 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7364 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7365 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007366#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007367 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7368 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7369 PyStructSequence_SET_ITEM(v, 2,
7370 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7371 PyStructSequence_SET_ITEM(v, 3,
7372 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7373 PyStructSequence_SET_ITEM(v, 4,
7374 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7375 PyStructSequence_SET_ITEM(v, 5,
7376 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7377 PyStructSequence_SET_ITEM(v, 6,
7378 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7379 PyStructSequence_SET_ITEM(v, 7,
7380 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7381 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7382 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007383#endif
7384
Victor Stinnerd6f85422010-05-05 23:33:33 +00007385 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007386}
7387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007388PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007389"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007390Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007391
7392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007393posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007394{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007395 int fd, res;
7396 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007397
Victor Stinnerd6f85422010-05-05 23:33:33 +00007398 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7399 return NULL;
7400 Py_BEGIN_ALLOW_THREADS
7401 res = fstatvfs(fd, &st);
7402 Py_END_ALLOW_THREADS
7403 if (res != 0)
7404 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007405
Victor Stinnerd6f85422010-05-05 23:33:33 +00007406 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007407}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007408#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007409
7410
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007411#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007412#include <sys/statvfs.h>
7413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007414PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007415"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007416Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007417
7418static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007419posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007420{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007421 char *path;
7422 int res;
7423 struct statvfs st;
7424 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7425 return NULL;
7426 Py_BEGIN_ALLOW_THREADS
7427 res = statvfs(path, &st);
7428 Py_END_ALLOW_THREADS
7429 if (res != 0)
7430 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007431
Victor Stinnerd6f85422010-05-05 23:33:33 +00007432 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007433}
7434#endif /* HAVE_STATVFS */
7435
7436
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007437#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007438PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007439"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007440Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007441The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007442or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007443
7444static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007445posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007446{
7447 PyObject *result = NULL;
7448 char *dir = NULL;
7449 char *pfx = NULL;
7450 char *name;
7451
7452 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinnerd6f85422010-05-05 23:33:33 +00007453 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007454
7455 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007456 "tempnam is a potential security risk to your program") < 0)
7457 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007458
Antoine Pitroub0614612011-01-02 20:04:52 +00007459 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
7460 "use the tempfile module", 1) < 0)
7461 return NULL;
7462
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007463#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007464 name = _tempnam(dir, pfx);
7465#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007466 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007467#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007468 if (name == NULL)
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007469 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007470 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007471 free(name);
7472 return result;
7473}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007474#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007475
7476
7477#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007478PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007479"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007480Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007481
7482static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007483posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007484{
7485 FILE *fp;
7486
Antoine Pitroub0614612011-01-02 20:04:52 +00007487 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
7488 "use the tempfile module", 1) < 0)
7489 return NULL;
7490
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007491 fp = tmpfile();
7492 if (fp == NULL)
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007493 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007494 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007495}
7496#endif
7497
7498
7499#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007500PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007501"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007502Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007503
7504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007505posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007506{
7507 char buffer[L_tmpnam];
7508 char *name;
7509
Skip Montanaro95618b52001-08-18 18:52:10 +00007510 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007511 "tmpnam is a potential security risk to your program") < 0)
7512 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007513
Antoine Pitroub0614612011-01-02 20:04:52 +00007514 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
7515 "use the tempfile module", 1) < 0)
7516 return NULL;
7517
Greg Wardb48bc172000-03-01 21:51:56 +00007518#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007519 name = tmpnam_r(buffer);
7520#else
7521 name = tmpnam(buffer);
7522#endif
7523 if (name == NULL) {
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007524 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007525#ifdef USE_TMPNAM_R
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007526 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007527#else
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007528 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007529#endif
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007530 );
7531 PyErr_SetObject(PyExc_OSError, err);
7532 Py_XDECREF(err);
7533 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007534 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007535 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007536}
7537#endif
7538
7539
Fred Drakec9680921999-12-13 16:37:25 +00007540/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7541 * It maps strings representing configuration variable names to
7542 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007543 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007544 * rarely-used constants. There are three separate tables that use
7545 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007546 *
7547 * This code is always included, even if none of the interfaces that
7548 * need it are included. The #if hackery needed to avoid it would be
7549 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007550 */
7551struct constdef {
7552 char *name;
7553 long value;
7554};
7555
Fred Drake12c6e2d1999-12-14 21:25:03 +00007556static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007557conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007558 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007559{
7560 if (PyInt_Check(arg)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007561 *valuep = PyInt_AS_LONG(arg);
7562 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007563 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007564 if (PyString_Check(arg)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007565 /* look up the value in the table using a binary search */
7566 size_t lo = 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00007567 size_t mid;
Stefan Krah93f7a322010-11-26 17:35:50 +00007568 size_t hi = tablesize;
7569 int cmp;
7570 char *confname = PyString_AS_STRING(arg);
7571 while (lo < hi) {
7572 mid = (lo + hi) / 2;
7573 cmp = strcmp(confname, table[mid].name);
7574 if (cmp < 0)
7575 hi = mid;
7576 else if (cmp > 0)
7577 lo = mid + 1;
7578 else {
7579 *valuep = table[mid].value;
7580 return 1;
7581 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007582 }
Stefan Krah93f7a322010-11-26 17:35:50 +00007583 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007584 }
7585 else
Stefan Krah93f7a322010-11-26 17:35:50 +00007586 PyErr_SetString(PyExc_TypeError,
7587 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007588 return 0;
7589}
7590
7591
7592#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7593static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007594#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007595 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007596#endif
7597#ifdef _PC_ABI_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007598 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007599#endif
Fred Drakec9680921999-12-13 16:37:25 +00007600#ifdef _PC_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007601 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007602#endif
7603#ifdef _PC_CHOWN_RESTRICTED
Victor Stinnerd6f85422010-05-05 23:33:33 +00007604 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007605#endif
7606#ifdef _PC_FILESIZEBITS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007607 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007608#endif
7609#ifdef _PC_LAST
Victor Stinnerd6f85422010-05-05 23:33:33 +00007610 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007611#endif
7612#ifdef _PC_LINK_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007613 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007614#endif
7615#ifdef _PC_MAX_CANON
Victor Stinnerd6f85422010-05-05 23:33:33 +00007616 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007617#endif
7618#ifdef _PC_MAX_INPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007619 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007620#endif
7621#ifdef _PC_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007622 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007623#endif
7624#ifdef _PC_NO_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007625 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007626#endif
7627#ifdef _PC_PATH_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007628 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007629#endif
7630#ifdef _PC_PIPE_BUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007631 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007632#endif
7633#ifdef _PC_PRIO_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007634 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007635#endif
7636#ifdef _PC_SOCK_MAXBUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007637 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007638#endif
7639#ifdef _PC_SYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007640 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007641#endif
7642#ifdef _PC_VDISABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007643 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007644#endif
7645};
7646
Fred Drakec9680921999-12-13 16:37:25 +00007647static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007648conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007649{
7650 return conv_confname(arg, valuep, posix_constants_pathconf,
7651 sizeof(posix_constants_pathconf)
7652 / sizeof(struct constdef));
7653}
7654#endif
7655
7656#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007657PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007658"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007659Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007660If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007661
7662static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007663posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007664{
7665 PyObject *result = NULL;
7666 int name, fd;
7667
Fred Drake12c6e2d1999-12-14 21:25:03 +00007668 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7669 conv_path_confname, &name)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007670 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007671
Stefan Krah93f7a322010-11-26 17:35:50 +00007672 errno = 0;
7673 limit = fpathconf(fd, name);
7674 if (limit == -1 && errno != 0)
7675 posix_error();
7676 else
7677 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007678 }
7679 return result;
7680}
7681#endif
7682
7683
7684#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007685PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007686"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007687Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007688If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007689
7690static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007691posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007692{
7693 PyObject *result = NULL;
7694 int name;
7695 char *path;
7696
7697 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7698 conv_path_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007699 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007700
Victor Stinnerd6f85422010-05-05 23:33:33 +00007701 errno = 0;
7702 limit = pathconf(path, name);
7703 if (limit == -1 && errno != 0) {
7704 if (errno == EINVAL)
Stefan Krahacaab2a2010-11-26 15:06:27 +00007705 /* could be a path or name problem */
7706 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007707 else
Stefan Krahacaab2a2010-11-26 15:06:27 +00007708 posix_error_with_filename(path);
Victor Stinnerd6f85422010-05-05 23:33:33 +00007709 }
7710 else
7711 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007712 }
7713 return result;
7714}
7715#endif
7716
7717#ifdef HAVE_CONFSTR
7718static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007719#ifdef _CS_ARCHITECTURE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007720 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007721#endif
7722#ifdef _CS_HOSTNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007723 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007724#endif
7725#ifdef _CS_HW_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007726 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007727#endif
7728#ifdef _CS_HW_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007729 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007730#endif
7731#ifdef _CS_INITTAB_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007732 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007733#endif
Fred Drakec9680921999-12-13 16:37:25 +00007734#ifdef _CS_LFS64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007735 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007736#endif
7737#ifdef _CS_LFS64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007738 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007739#endif
7740#ifdef _CS_LFS64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007741 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007742#endif
7743#ifdef _CS_LFS64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007744 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007745#endif
7746#ifdef _CS_LFS_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007747 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007748#endif
7749#ifdef _CS_LFS_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007750 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007751#endif
7752#ifdef _CS_LFS_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007753 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007754#endif
7755#ifdef _CS_LFS_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007756 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007757#endif
Fred Draked86ed291999-12-15 15:34:33 +00007758#ifdef _CS_MACHINE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007759 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007760#endif
Fred Drakec9680921999-12-13 16:37:25 +00007761#ifdef _CS_PATH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007762 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007763#endif
Fred Draked86ed291999-12-15 15:34:33 +00007764#ifdef _CS_RELEASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007765 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007766#endif
7767#ifdef _CS_SRPC_DOMAIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007768 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007769#endif
7770#ifdef _CS_SYSNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007771 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007772#endif
7773#ifdef _CS_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007774 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007775#endif
Fred Drakec9680921999-12-13 16:37:25 +00007776#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007777 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007778#endif
7779#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007780 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007781#endif
7782#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007783 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007784#endif
7785#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007786 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007787#endif
7788#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007789 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007790#endif
7791#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007792 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007793#endif
7794#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007795 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007796#endif
7797#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007798 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007799#endif
7800#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007801 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007802#endif
7803#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007804 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007805#endif
7806#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007807 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007808#endif
7809#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007810 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007811#endif
7812#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007813 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007814#endif
7815#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007816 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007817#endif
7818#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007819 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007820#endif
7821#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007822 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007823#endif
Fred Draked86ed291999-12-15 15:34:33 +00007824#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007825 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007826#endif
7827#ifdef _MIPS_CS_BASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007828 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007829#endif
7830#ifdef _MIPS_CS_HOSTID
Victor Stinnerd6f85422010-05-05 23:33:33 +00007831 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007832#endif
7833#ifdef _MIPS_CS_HW_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007834 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007835#endif
7836#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007837 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007838#endif
7839#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007840 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007841#endif
7842#ifdef _MIPS_CS_OSREL_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007843 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007844#endif
7845#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007846 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007847#endif
7848#ifdef _MIPS_CS_OS_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007849 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007850#endif
7851#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007852 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007853#endif
7854#ifdef _MIPS_CS_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007855 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007856#endif
7857#ifdef _MIPS_CS_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007858 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007859#endif
7860#ifdef _MIPS_CS_VENDOR
Victor Stinnerd6f85422010-05-05 23:33:33 +00007861 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007862#endif
Fred Drakec9680921999-12-13 16:37:25 +00007863};
7864
7865static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007866conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007867{
7868 return conv_confname(arg, valuep, posix_constants_confstr,
7869 sizeof(posix_constants_confstr)
7870 / sizeof(struct constdef));
7871}
7872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007873PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007874"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007875Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007876
7877static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007878posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007879{
7880 PyObject *result = NULL;
7881 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007882 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007883
7884 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007885 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007886
Victor Stinnerd6f85422010-05-05 23:33:33 +00007887 errno = 0;
7888 len = confstr(name, buffer, sizeof(buffer));
7889 if (len == 0) {
7890 if (errno) {
7891 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007892 }
7893 else {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007894 result = Py_None;
7895 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007896 }
7897 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00007898 else {
7899 if ((unsigned int)len >= sizeof(buffer)) {
7900 result = PyString_FromStringAndSize(NULL, len-1);
7901 if (result != NULL)
7902 confstr(name, PyString_AS_STRING(result), len);
7903 }
7904 else
7905 result = PyString_FromStringAndSize(buffer, len-1);
7906 }
7907 }
Fred Drakec9680921999-12-13 16:37:25 +00007908 return result;
7909}
7910#endif
7911
7912
7913#ifdef HAVE_SYSCONF
7914static struct constdef posix_constants_sysconf[] = {
7915#ifdef _SC_2_CHAR_TERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00007916 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007917#endif
7918#ifdef _SC_2_C_BIND
Victor Stinnerd6f85422010-05-05 23:33:33 +00007919 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_2_C_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007922 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
7924#ifdef _SC_2_C_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007925 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007926#endif
7927#ifdef _SC_2_FORT_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007928 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_2_FORT_RUN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007931 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
7933#ifdef _SC_2_LOCALEDEF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007934 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007935#endif
7936#ifdef _SC_2_SW_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007937 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
7939#ifdef _SC_2_UPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007940 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007941#endif
7942#ifdef _SC_2_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007943 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007944#endif
Fred Draked86ed291999-12-15 15:34:33 +00007945#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007946 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007947#endif
7948#ifdef _SC_ACL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007949 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007950#endif
Fred Drakec9680921999-12-13 16:37:25 +00007951#ifdef _SC_AIO_LISTIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007952 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
Fred Drakec9680921999-12-13 16:37:25 +00007954#ifdef _SC_AIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007955 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007956#endif
7957#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007958 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007959#endif
7960#ifdef _SC_ARG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007961 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007964 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_ATEXIT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007967 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
Fred Draked86ed291999-12-15 15:34:33 +00007969#ifdef _SC_AUDIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007970 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007971#endif
Fred Drakec9680921999-12-13 16:37:25 +00007972#ifdef _SC_AVPHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00007973 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007974#endif
7975#ifdef _SC_BC_BASE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007976 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007977#endif
7978#ifdef _SC_BC_DIM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007979 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_BC_SCALE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007982 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_BC_STRING_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007985 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
Fred Draked86ed291999-12-15 15:34:33 +00007987#ifdef _SC_CAP
Victor Stinnerd6f85422010-05-05 23:33:33 +00007988 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007989#endif
Fred Drakec9680921999-12-13 16:37:25 +00007990#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007991 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_CHAR_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007994 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_CHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007997 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_CHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008000 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_CHILD_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008003 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_CLK_TCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008006 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_COHER_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008009 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008012 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
8014#ifdef _SC_DCACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008015 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_DCACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008018 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_DCACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008021 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_DCACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008024 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_DCACHE_TBLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008027 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_DELAYTIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008030 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008033 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_EXPR_NEST_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008036 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008039 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008042 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008045 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_ICACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008048 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef _SC_ICACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008051 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
8053#ifdef _SC_ICACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008054 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
8056#ifdef _SC_ICACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008057 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008058#endif
Fred Draked86ed291999-12-15 15:34:33 +00008059#ifdef _SC_INF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008060 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008061#endif
Fred Drakec9680921999-12-13 16:37:25 +00008062#ifdef _SC_INT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008063 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
8065#ifdef _SC_INT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008066 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008067#endif
8068#ifdef _SC_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008069 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
Fred Draked86ed291999-12-15 15:34:33 +00008071#ifdef _SC_IP_SECOPTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008072 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008073#endif
Fred Drakec9680921999-12-13 16:37:25 +00008074#ifdef _SC_JOB_CONTROL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008075 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
Fred Draked86ed291999-12-15 15:34:33 +00008077#ifdef _SC_KERN_POINTERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008078 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008079#endif
8080#ifdef _SC_KERN_SIM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008081 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008082#endif
Fred Drakec9680921999-12-13 16:37:25 +00008083#ifdef _SC_LINE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008084 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef _SC_LOGIN_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008087 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_LOGNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008090 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_LONG_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008093 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
Fred Draked86ed291999-12-15 15:34:33 +00008095#ifdef _SC_MAC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008096 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008097#endif
Fred Drakec9680921999-12-13 16:37:25 +00008098#ifdef _SC_MAPPED_FILES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008099 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _SC_MAXPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008102 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _SC_MB_LEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008105 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _SC_MEMLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008108 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _SC_MEMLOCK_RANGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008111 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _SC_MEMORY_PROTECTION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008114 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _SC_MESSAGE_PASSING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008117 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
Fred Draked86ed291999-12-15 15:34:33 +00008119#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008120 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008121#endif
Fred Drakec9680921999-12-13 16:37:25 +00008122#ifdef _SC_MQ_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008123 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _SC_MQ_PRIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008126 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
Fred Draked86ed291999-12-15 15:34:33 +00008128#ifdef _SC_NACLS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008129 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008130#endif
Fred Drakec9680921999-12-13 16:37:25 +00008131#ifdef _SC_NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008132 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _SC_NL_ARGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008135 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _SC_NL_LANGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008138 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _SC_NL_MSGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008141 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _SC_NL_NMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008144 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _SC_NL_SETMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008147 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _SC_NL_TEXTMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008150 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _SC_NPROCESSORS_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008153 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
8155#ifdef _SC_NPROCESSORS_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008156 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008157#endif
Fred Draked86ed291999-12-15 15:34:33 +00008158#ifdef _SC_NPROC_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008159 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008160#endif
8161#ifdef _SC_NPROC_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008162 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008163#endif
Fred Drakec9680921999-12-13 16:37:25 +00008164#ifdef _SC_NZERO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008165 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008166#endif
8167#ifdef _SC_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008168 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008169#endif
8170#ifdef _SC_PAGESIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008171 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008172#endif
8173#ifdef _SC_PAGE_SIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008174 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
8176#ifdef _SC_PASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008177 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008178#endif
8179#ifdef _SC_PHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008180 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008181#endif
8182#ifdef _SC_PII
Victor Stinnerd6f85422010-05-05 23:33:33 +00008183 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008184#endif
8185#ifdef _SC_PII_INTERNET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008186 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
8188#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008189 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008190#endif
8191#ifdef _SC_PII_INTERNET_STREAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008192 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008193#endif
8194#ifdef _SC_PII_OSI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008195 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008196#endif
8197#ifdef _SC_PII_OSI_CLTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008198 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008199#endif
8200#ifdef _SC_PII_OSI_COTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008201 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008202#endif
8203#ifdef _SC_PII_OSI_M
Victor Stinnerd6f85422010-05-05 23:33:33 +00008204 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008205#endif
8206#ifdef _SC_PII_SOCKET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008207 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008208#endif
8209#ifdef _SC_PII_XTI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008210 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008211#endif
8212#ifdef _SC_POLL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008213 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008214#endif
8215#ifdef _SC_PRIORITIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008216 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008217#endif
8218#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008219 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008220#endif
8221#ifdef _SC_REALTIME_SIGNALS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008222 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008223#endif
8224#ifdef _SC_RE_DUP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008225 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008226#endif
8227#ifdef _SC_RTSIG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008228 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008229#endif
8230#ifdef _SC_SAVED_IDS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008231 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008232#endif
8233#ifdef _SC_SCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008234 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008235#endif
8236#ifdef _SC_SCHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008237 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008238#endif
8239#ifdef _SC_SELECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008240 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008241#endif
8242#ifdef _SC_SEMAPHORES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008243 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008244#endif
8245#ifdef _SC_SEM_NSEMS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008246 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008247#endif
8248#ifdef _SC_SEM_VALUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008249 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008250#endif
8251#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008252 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008253#endif
8254#ifdef _SC_SHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008255 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008256#endif
8257#ifdef _SC_SHRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008258 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008259#endif
8260#ifdef _SC_SIGQUEUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008261 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008262#endif
8263#ifdef _SC_SIGRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008264 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008265#endif
8266#ifdef _SC_SIGRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008267 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008268#endif
Fred Draked86ed291999-12-15 15:34:33 +00008269#ifdef _SC_SOFTPOWER
Victor Stinnerd6f85422010-05-05 23:33:33 +00008270 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008271#endif
Fred Drakec9680921999-12-13 16:37:25 +00008272#ifdef _SC_SPLIT_CACHE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008273 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008274#endif
8275#ifdef _SC_SSIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008276 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008277#endif
8278#ifdef _SC_STACK_PROT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008279 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008280#endif
8281#ifdef _SC_STREAM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008282 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008283#endif
8284#ifdef _SC_SYNCHRONIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008285 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008286#endif
8287#ifdef _SC_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008288 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008289#endif
8290#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008291 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008292#endif
8293#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008294 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008295#endif
8296#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008297 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008298#endif
8299#ifdef _SC_THREAD_KEYS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008300 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008301#endif
8302#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008303 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008304#endif
8305#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008306 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008307#endif
8308#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008309 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008310#endif
8311#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008312 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008313#endif
8314#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008315 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008316#endif
8317#ifdef _SC_THREAD_STACK_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008318 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008319#endif
8320#ifdef _SC_THREAD_THREADS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008321 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008322#endif
8323#ifdef _SC_TIMERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008324 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008325#endif
8326#ifdef _SC_TIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008327 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008328#endif
8329#ifdef _SC_TTY_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008330 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008331#endif
8332#ifdef _SC_TZNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008333 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008334#endif
8335#ifdef _SC_T_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008336 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008337#endif
8338#ifdef _SC_UCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008339 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008340#endif
8341#ifdef _SC_UINT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008342 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008343#endif
8344#ifdef _SC_UIO_MAXIOV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008345 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008346#endif
8347#ifdef _SC_ULONG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008348 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008349#endif
8350#ifdef _SC_USHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008351 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008352#endif
8353#ifdef _SC_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008354 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008355#endif
8356#ifdef _SC_WORD_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008357 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008358#endif
8359#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinnerd6f85422010-05-05 23:33:33 +00008360 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008361#endif
8362#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008363 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008364#endif
8365#ifdef _SC_XBS5_LP64_OFF64
Victor Stinnerd6f85422010-05-05 23:33:33 +00008366 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008367#endif
8368#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008369 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008370#endif
8371#ifdef _SC_XOPEN_CRYPT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008372 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008373#endif
8374#ifdef _SC_XOPEN_ENH_I18N
Victor Stinnerd6f85422010-05-05 23:33:33 +00008375 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008376#endif
8377#ifdef _SC_XOPEN_LEGACY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008378 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008379#endif
8380#ifdef _SC_XOPEN_REALTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008381 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008382#endif
8383#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008384 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008385#endif
8386#ifdef _SC_XOPEN_SHM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008387 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008388#endif
8389#ifdef _SC_XOPEN_UNIX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008390 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008391#endif
8392#ifdef _SC_XOPEN_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008393 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008394#endif
8395#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008396 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008397#endif
8398#ifdef _SC_XOPEN_XPG2
Victor Stinnerd6f85422010-05-05 23:33:33 +00008399 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008400#endif
8401#ifdef _SC_XOPEN_XPG3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008402 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008403#endif
8404#ifdef _SC_XOPEN_XPG4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008405 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008406#endif
8407};
8408
8409static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008410conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008411{
8412 return conv_confname(arg, valuep, posix_constants_sysconf,
8413 sizeof(posix_constants_sysconf)
8414 / sizeof(struct constdef));
8415}
8416
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008417PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008418"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008419Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008420
8421static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008422posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008423{
8424 PyObject *result = NULL;
8425 int name;
8426
8427 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner862490a2010-05-06 00:03:44 +00008428 int value;
Fred Drakec9680921999-12-13 16:37:25 +00008429
Victor Stinner862490a2010-05-06 00:03:44 +00008430 errno = 0;
8431 value = sysconf(name);
8432 if (value == -1 && errno != 0)
8433 posix_error();
8434 else
8435 result = PyInt_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008436 }
8437 return result;
8438}
8439#endif
8440
8441
Fred Drakebec628d1999-12-15 18:31:10 +00008442/* This code is used to ensure that the tables of configuration value names
8443 * are in sorted order as required by conv_confname(), and also to build the
8444 * the exported dictionaries that are used to publish information about the
8445 * names available on the host platform.
8446 *
8447 * Sorting the table at runtime ensures that the table is properly ordered
8448 * when used, even for platforms we're not able to test on. It also makes
8449 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008450 */
Fred Drakebec628d1999-12-15 18:31:10 +00008451
8452static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008453cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008454{
8455 const struct constdef *c1 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008456 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008457 const struct constdef *c2 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008458 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008459
8460 return strcmp(c1->name, c2->name);
8461}
8462
8463static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008464setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinnerd6f85422010-05-05 23:33:33 +00008465 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008466{
Fred Drakebec628d1999-12-15 18:31:10 +00008467 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008468 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008469
8470 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8471 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008472 if (d == NULL)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008473 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008474
Barry Warsaw3155db32000-04-13 15:20:40 +00008475 for (i=0; i < tablesize; ++i) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008476 PyObject *o = PyInt_FromLong(table[i].value);
8477 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8478 Py_XDECREF(o);
8479 Py_DECREF(d);
8480 return -1;
8481 }
8482 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008483 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008484 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008485}
8486
Fred Drakebec628d1999-12-15 18:31:10 +00008487/* Return -1 on failure, 0 on success. */
8488static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008489setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008490{
8491#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008492 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008493 sizeof(posix_constants_pathconf)
8494 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008495 "pathconf_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008496 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008497#endif
8498#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008499 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008500 sizeof(posix_constants_confstr)
8501 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008502 "confstr_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008503 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008504#endif
8505#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008506 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008507 sizeof(posix_constants_sysconf)
8508 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008509 "sysconf_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008510 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008511#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008512 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008513}
Fred Draked86ed291999-12-15 15:34:33 +00008514
8515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008516PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008517"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008518Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008519in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008520
8521static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008522posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008523{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008524 abort();
8525 /*NOTREACHED*/
8526 Py_FatalError("abort() called from Python code didn't abort!");
8527 return NULL;
8528}
Fred Drakebec628d1999-12-15 18:31:10 +00008529
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008530#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008531PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008532"startfile(filepath [, operation]) - Start a file with its associated\n\
8533application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008534\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008535When \"operation\" is not specified or \"open\", this acts like\n\
8536double-clicking the file in Explorer, or giving the file name as an\n\
8537argument to the DOS \"start\" command: the file is opened with whatever\n\
8538application (if any) its extension is associated.\n\
8539When another \"operation\" is given, it specifies what should be done with\n\
8540the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008541\n\
8542startfile returns as soon as the associated application is launched.\n\
8543There is no option to wait for the application to close, and no way\n\
8544to retrieve the application's exit status.\n\
8545\n\
8546The filepath is relative to the current directory. If you want to use\n\
8547an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008548the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008549
8550static PyObject *
8551win32_startfile(PyObject *self, PyObject *args)
8552{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008553 char *filepath;
8554 char *operation = NULL;
8555 HINSTANCE rc;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008556
Victor Stinnerd6f85422010-05-05 23:33:33 +00008557 PyObject *unipath, *woperation = NULL;
8558 if (!PyArg_ParseTuple(args, "U|s:startfile",
8559 &unipath, &operation)) {
8560 PyErr_Clear();
8561 goto normal;
8562 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008563
Victor Stinnerd6f85422010-05-05 23:33:33 +00008564 if (operation) {
8565 woperation = PyUnicode_DecodeASCII(operation,
8566 strlen(operation), NULL);
8567 if (!woperation) {
8568 PyErr_Clear();
8569 operation = NULL;
8570 goto normal;
8571 }
8572 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008573
Victor Stinnerd6f85422010-05-05 23:33:33 +00008574 Py_BEGIN_ALLOW_THREADS
8575 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8576 PyUnicode_AS_UNICODE(unipath),
8577 NULL, NULL, SW_SHOWNORMAL);
8578 Py_END_ALLOW_THREADS
8579
8580 Py_XDECREF(woperation);
8581 if (rc <= (HINSTANCE)32) {
8582 PyObject *errval = win32_error_unicode("startfile",
8583 PyUnicode_AS_UNICODE(unipath));
8584 return errval;
8585 }
8586 Py_INCREF(Py_None);
8587 return Py_None;
Georg Brandlad89dc82006-04-03 12:26:26 +00008588
8589normal:
Victor Stinnerd6f85422010-05-05 23:33:33 +00008590 if (!PyArg_ParseTuple(args, "et|s:startfile",
8591 Py_FileSystemDefaultEncoding, &filepath,
8592 &operation))
8593 return NULL;
8594 Py_BEGIN_ALLOW_THREADS
8595 rc = ShellExecute((HWND)0, operation, filepath,
8596 NULL, NULL, SW_SHOWNORMAL);
8597 Py_END_ALLOW_THREADS
8598 if (rc <= (HINSTANCE)32) {
8599 PyObject *errval = win32_error("startfile", filepath);
8600 PyMem_Free(filepath);
8601 return errval;
8602 }
8603 PyMem_Free(filepath);
8604 Py_INCREF(Py_None);
8605 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008606}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008607#endif /* MS_WINDOWS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008608
Martin v. Löwis438b5342002-12-27 10:16:42 +00008609#ifdef HAVE_GETLOADAVG
8610PyDoc_STRVAR(posix_getloadavg__doc__,
8611"getloadavg() -> (float, float, float)\n\n\
8612Return the number of processes in the system run queue averaged over\n\
8613the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8614was unobtainable");
8615
8616static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008617posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008618{
8619 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008620 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah93f7a322010-11-26 17:35:50 +00008621 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8622 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008623 } else
Stefan Krah93f7a322010-11-26 17:35:50 +00008624 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008625}
8626#endif
8627
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008628PyDoc_STRVAR(posix_urandom__doc__,
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008629"urandom(n) -> str\n\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008630Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008631
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008632static PyObject *
8633posix_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008634{
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008635 Py_ssize_t size;
8636 PyObject *result;
8637 int ret;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008638
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008639 /* Read arguments */
8640 if (!PyArg_ParseTuple(args, "n:urandom", &size))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008641 return NULL;
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008642 if (size < 0)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008643 return PyErr_Format(PyExc_ValueError,
8644 "negative argument not allowed");
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008645 result = PyBytes_FromStringAndSize(NULL, size);
8646 if (result == NULL)
8647 return NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008648
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008649 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
8650 PyBytes_GET_SIZE(result));
8651 if (ret == -1) {
8652 Py_DECREF(result);
8653 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008654 }
8655 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008656}
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008657
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008658#ifdef HAVE_SETRESUID
8659PyDoc_STRVAR(posix_setresuid__doc__,
8660"setresuid(ruid, euid, suid)\n\n\
8661Set the current process's real, effective, and saved user ids.");
8662
8663static PyObject*
8664posix_setresuid (PyObject *self, PyObject *args)
8665{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008666 uid_t ruid, euid, suid;
8667 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
8668 _Py_Uid_Converter, &ruid,
8669 _Py_Uid_Converter, &euid,
8670 _Py_Uid_Converter, &suid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008671 return NULL;
8672 if (setresuid(ruid, euid, suid) < 0)
8673 return posix_error();
8674 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008675}
8676#endif
8677
8678#ifdef HAVE_SETRESGID
8679PyDoc_STRVAR(posix_setresgid__doc__,
8680"setresgid(rgid, egid, sgid)\n\n\
8681Set the current process's real, effective, and saved group ids.");
8682
8683static PyObject*
8684posix_setresgid (PyObject *self, PyObject *args)
8685{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008686 gid_t rgid, egid, sgid;
8687 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
8688 _Py_Gid_Converter, &rgid,
8689 _Py_Gid_Converter, &egid,
8690 _Py_Gid_Converter, &sgid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008691 return NULL;
8692 if (setresgid(rgid, egid, sgid) < 0)
8693 return posix_error();
8694 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008695}
8696#endif
8697
8698#ifdef HAVE_GETRESUID
8699PyDoc_STRVAR(posix_getresuid__doc__,
8700"getresuid() -> (ruid, euid, suid)\n\n\
8701Get tuple of the current process's real, effective, and saved user ids.");
8702
8703static PyObject*
8704posix_getresuid (PyObject *self, PyObject *noargs)
8705{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008706 uid_t ruid, euid, suid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008707 if (getresuid(&ruid, &euid, &suid) < 0)
8708 return posix_error();
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008709 return Py_BuildValue("(NNN)", _PyInt_FromUid(ruid),
8710 _PyInt_FromUid(euid),
8711 _PyInt_FromUid(suid));
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008712}
8713#endif
8714
8715#ifdef HAVE_GETRESGID
8716PyDoc_STRVAR(posix_getresgid__doc__,
8717"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandl21946af2010-10-06 09:28:45 +00008718Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008719
8720static PyObject*
8721posix_getresgid (PyObject *self, PyObject *noargs)
8722{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008723 uid_t rgid, egid, sgid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008724 if (getresgid(&rgid, &egid, &sgid) < 0)
8725 return posix_error();
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008726 return Py_BuildValue("(NNN)", _PyInt_FromGid(rgid),
8727 _PyInt_FromGid(egid),
8728 _PyInt_FromGid(sgid));
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008729}
8730#endif
8731
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008732static PyMethodDef posix_methods[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008733 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008734#ifdef HAVE_TTYNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008735 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008736#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008737 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008738#ifdef HAVE_CHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008739 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008740#endif /* HAVE_CHFLAGS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008741 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008742#ifdef HAVE_FCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008743 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008744#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008745#ifdef HAVE_CHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008746 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008747#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008748#ifdef HAVE_LCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008749 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008750#endif /* HAVE_LCHMOD */
8751#ifdef HAVE_FCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008752 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008753#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008754#ifdef HAVE_LCHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008755 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008756#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008757#ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008758 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008759#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008760#ifdef HAVE_CHROOT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008761 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008762#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008763#ifdef HAVE_CTERMID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008764 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008765#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008766#ifdef HAVE_GETCWD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008767 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008768#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008769 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008770#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008771#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008772#ifdef HAVE_LINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008773 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008774#endif /* HAVE_LINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008775 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8776 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8777 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008778#ifdef HAVE_NICE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008779 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008780#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008781#ifdef HAVE_READLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008782 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008783#endif /* HAVE_READLINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008784 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8785 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8786 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8787 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008788#ifdef HAVE_SYMLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008789 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008790#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008791#ifdef HAVE_SYSTEM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008792 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008793#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008794 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008795#ifdef HAVE_UNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008796 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008797#endif /* HAVE_UNAME */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008798 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8799 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8800 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008801#ifdef HAVE_TIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008802 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008803#endif /* HAVE_TIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008804 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008805#ifdef HAVE_EXECV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008806 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8807 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008808#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008809#ifdef HAVE_SPAWNV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008810 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8811 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008812#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008813 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8814 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008815#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008816#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008817#ifdef HAVE_FORK1
Victor Stinnerd6f85422010-05-05 23:33:33 +00008818 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008819#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008820#ifdef HAVE_FORK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008821 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008822#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008823#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008824 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008825#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008826#ifdef HAVE_FORKPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008827 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008828#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008829#ifdef HAVE_GETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008830 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008831#endif /* HAVE_GETEGID */
8832#ifdef HAVE_GETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008833 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008834#endif /* HAVE_GETEUID */
8835#ifdef HAVE_GETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008836 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008837#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008838#ifdef HAVE_GETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008839 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008840#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008841 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008842#ifdef HAVE_GETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008843 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008844#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008845#ifdef HAVE_GETPPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008846 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008847#endif /* HAVE_GETPPID */
8848#ifdef HAVE_GETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008849 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008850#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008851#ifdef HAVE_GETLOGIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008852 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008853#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008854#ifdef HAVE_KILL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008855 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008856#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008857#ifdef HAVE_KILLPG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008858 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008859#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008860#ifdef HAVE_PLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008861 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008862#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008863#ifdef HAVE_POPEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008864 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008865#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008866 {"popen2", win32_popen2, METH_VARARGS},
8867 {"popen3", win32_popen3, METH_VARARGS},
8868 {"popen4", win32_popen4, METH_VARARGS},
8869 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8870 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008871#else
8872#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008873 {"popen2", os2emx_popen2, METH_VARARGS},
8874 {"popen3", os2emx_popen3, METH_VARARGS},
8875 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008876#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008877#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008878#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008879#ifdef HAVE_SETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008880 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008881#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008882#ifdef HAVE_SETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008883 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008884#endif /* HAVE_SETEUID */
8885#ifdef HAVE_SETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008886 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008887#endif /* HAVE_SETEGID */
8888#ifdef HAVE_SETREUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008889 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008890#endif /* HAVE_SETREUID */
8891#ifdef HAVE_SETREGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008892 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008893#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008894#ifdef HAVE_SETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008895 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008896#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008897#ifdef HAVE_SETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008898 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008899#endif /* HAVE_SETGROUPS */
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008900#ifdef HAVE_INITGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008901 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008902#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008903#ifdef HAVE_GETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008904 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008905#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008906#ifdef HAVE_SETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008907 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008908#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008909#ifdef HAVE_WAIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008910 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008911#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008912#ifdef HAVE_WAIT3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008913 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008914#endif /* HAVE_WAIT3 */
8915#ifdef HAVE_WAIT4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008916 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008917#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008918#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008919 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008920#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008921#ifdef HAVE_GETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008922 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008923#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008924#ifdef HAVE_SETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008925 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008926#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008927#ifdef HAVE_SETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008928 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008929#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008930#ifdef HAVE_TCGETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008931 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008932#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008933#ifdef HAVE_TCSETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008934 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008935#endif /* HAVE_TCSETPGRP */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008936 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8937 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8938 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8939 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8940 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8941 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8942 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8943 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8944 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8945 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8946 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008947#ifdef HAVE_PIPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008948 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008949#endif
8950#ifdef HAVE_MKFIFO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008951 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008952#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008953#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008954 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008955#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008956#ifdef HAVE_DEVICE_MACROS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008957 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8958 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8959 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008960#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008961#ifdef HAVE_FTRUNCATE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008962 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008963#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008964#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008965 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008966#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008967#ifdef HAVE_UNSETENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008968 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008969#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008970 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008971#ifdef HAVE_FCHDIR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008972 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008973#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008974#ifdef HAVE_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008975 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008976#endif
8977#ifdef HAVE_FDATASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008978 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008979#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008980#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008981#ifdef WCOREDUMP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008982 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008983#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008984#ifdef WIFCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008985 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008986#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008987#ifdef WIFSTOPPED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008988 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008989#endif /* WIFSTOPPED */
8990#ifdef WIFSIGNALED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008991 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008992#endif /* WIFSIGNALED */
8993#ifdef WIFEXITED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008994 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008995#endif /* WIFEXITED */
8996#ifdef WEXITSTATUS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008997 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008998#endif /* WEXITSTATUS */
8999#ifdef WTERMSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009000 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009001#endif /* WTERMSIG */
9002#ifdef WSTOPSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009003 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009004#endif /* WSTOPSIG */
9005#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00009006#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009007 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009008#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00009009#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009010 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009011#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00009012#ifdef HAVE_TMPFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009013 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009014#endif
9015#ifdef HAVE_TEMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009016 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009017#endif
9018#ifdef HAVE_TMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009019 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009020#endif
Fred Drakec9680921999-12-13 16:37:25 +00009021#ifdef HAVE_CONFSTR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009022 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009023#endif
9024#ifdef HAVE_SYSCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009025 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009026#endif
9027#ifdef HAVE_FPATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009028 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009029#endif
9030#ifdef HAVE_PATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009031 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009032#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009033 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009034#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009035 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtin5446f082011-06-09 10:00:42 -05009036 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00009037#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009038#ifdef HAVE_GETLOADAVG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009039 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009040#endif
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009041#ifdef HAVE_SETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009042 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009043#endif
9044#ifdef HAVE_SETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009045 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009046#endif
9047#ifdef HAVE_GETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009048 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009049#endif
9050#ifdef HAVE_GETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009051 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009052#endif
Barry Warsaw1e13eb02012-02-20 20:42:21 -05009053 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Victor Stinnerd6f85422010-05-05 23:33:33 +00009054 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009055};
9056
9057
Barry Warsaw4a342091996-12-19 23:50:02 +00009058static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009059ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009060{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009061 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009062}
9063
Guido van Rossumd48f2521997-12-05 22:19:34 +00009064#if defined(PYOS_OS2)
9065/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009066static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009067{
9068 APIRET rc;
9069 ULONG values[QSV_MAX+1];
9070 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009071 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009072
9073 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009074 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009075 Py_END_ALLOW_THREADS
9076
9077 if (rc != NO_ERROR) {
9078 os2_error(rc);
9079 return -1;
9080 }
9081
Fred Drake4d1e64b2002-04-15 19:40:07 +00009082 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9083 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9084 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9085 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9086 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9087 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9088 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009089
9090 switch (values[QSV_VERSION_MINOR]) {
9091 case 0: ver = "2.00"; break;
9092 case 10: ver = "2.10"; break;
9093 case 11: ver = "2.11"; break;
9094 case 30: ver = "3.00"; break;
9095 case 40: ver = "4.00"; break;
9096 case 50: ver = "5.00"; break;
9097 default:
Tim Peters885d4572001-11-28 20:27:42 +00009098 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinnerd6f85422010-05-05 23:33:33 +00009099 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009100 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009101 ver = &tmp[0];
9102 }
9103
9104 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009105 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009106 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009107
9108 /* Add Indicator of Which Drive was Used to Boot the System */
9109 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9110 tmp[1] = ':';
9111 tmp[2] = '\0';
9112
Fred Drake4d1e64b2002-04-15 19:40:07 +00009113 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009114}
9115#endif
9116
Barry Warsaw4a342091996-12-19 23:50:02 +00009117static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009118all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009119{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009120#ifdef F_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009121 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009122#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009123#ifdef R_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009124 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009125#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009126#ifdef W_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009127 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009128#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009129#ifdef X_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009130 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009131#endif
Fred Drakec9680921999-12-13 16:37:25 +00009132#ifdef NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009133 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009134#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009135#ifdef TMP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009136 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009137#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009138#ifdef WCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009139 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009140#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009141#ifdef WNOHANG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009142 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009143#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009144#ifdef WUNTRACED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009145 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009146#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009147#ifdef O_RDONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009148 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009149#endif
9150#ifdef O_WRONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009151 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009152#endif
9153#ifdef O_RDWR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009154 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009155#endif
9156#ifdef O_NDELAY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009157 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009158#endif
9159#ifdef O_NONBLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009160 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009161#endif
9162#ifdef O_APPEND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009163 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009164#endif
9165#ifdef O_DSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009166 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009167#endif
9168#ifdef O_RSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009169 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009170#endif
9171#ifdef O_SYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009172 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009173#endif
9174#ifdef O_NOCTTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009175 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009176#endif
9177#ifdef O_CREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009178 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009179#endif
9180#ifdef O_EXCL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009181 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009182#endif
9183#ifdef O_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009184 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009185#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009186#ifdef O_BINARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009187 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009188#endif
9189#ifdef O_TEXT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009190 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009191#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009192#ifdef O_LARGEFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009193 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009194#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009195#ifdef O_SHLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009196 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009197#endif
9198#ifdef O_EXLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009199 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009200#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009201
Tim Peters5aa91602002-01-30 05:46:57 +00009202/* MS Windows */
9203#ifdef O_NOINHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009204 /* Don't inherit in child processes. */
9205 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009206#endif
9207#ifdef _O_SHORT_LIVED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009208 /* Optimize for short life (keep in memory). */
9209 /* MS forgot to define this one with a non-underscore form too. */
9210 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009211#endif
9212#ifdef O_TEMPORARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009213 /* Automatically delete when last handle is closed. */
9214 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009215#endif
9216#ifdef O_RANDOM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009217 /* Optimize for random access. */
9218 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009219#endif
9220#ifdef O_SEQUENTIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009221 /* Optimize for sequential access. */
9222 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009223#endif
9224
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009225/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00009226#ifdef O_ASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009227 /* Send a SIGIO signal whenever input or output
9228 becomes available on file descriptor */
9229 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00009230#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009231#ifdef O_DIRECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009232 /* Direct disk access. */
9233 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009234#endif
9235#ifdef O_DIRECTORY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009236 /* Must be a directory. */
9237 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009238#endif
9239#ifdef O_NOFOLLOW
Victor Stinnerd6f85422010-05-05 23:33:33 +00009240 /* Do not follow links. */
9241 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009242#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00009243#ifdef O_NOATIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00009244 /* Do not update the access time. */
9245 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00009246#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009247
Victor Stinnerd6f85422010-05-05 23:33:33 +00009248 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009249#ifdef EX_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009250 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009251#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009252#ifdef EX_USAGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009253 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009254#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009255#ifdef EX_DATAERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009256 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009257#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009258#ifdef EX_NOINPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009259 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009260#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009261#ifdef EX_NOUSER
Victor Stinnerd6f85422010-05-05 23:33:33 +00009262 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009263#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009264#ifdef EX_NOHOST
Victor Stinnerd6f85422010-05-05 23:33:33 +00009265 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009266#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009267#ifdef EX_UNAVAILABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009268 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009269#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009270#ifdef EX_SOFTWARE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009271 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009272#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009273#ifdef EX_OSERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009274 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009275#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009276#ifdef EX_OSFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009277 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009278#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009279#ifdef EX_CANTCREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009280 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009281#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009282#ifdef EX_IOERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009283 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009284#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009285#ifdef EX_TEMPFAIL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009286 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009287#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009288#ifdef EX_PROTOCOL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009289 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009290#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009291#ifdef EX_NOPERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009292 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009293#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009294#ifdef EX_CONFIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009295 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009296#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009297#ifdef EX_NOTFOUND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009298 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009299#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009300
Guido van Rossum246bc171999-02-01 23:54:31 +00009301#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009302#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009303 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9304 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9305 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9306 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9307 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9308 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9309 if (ins(d, "P_PM", (long)P_PM)) return -1;
9310 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9311 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9312 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9313 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9314 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9315 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9316 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9317 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9318 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9319 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9320 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9321 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9322 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009323#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009324 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9325 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9326 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9327 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9328 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009329#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009330#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009331
Guido van Rossumd48f2521997-12-05 22:19:34 +00009332#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009333 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009334#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009335 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009336}
9337
9338
Tim Peters5aa91602002-01-30 05:46:57 +00009339#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009340#define INITFUNC initnt
9341#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009342
9343#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009344#define INITFUNC initos2
9345#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009346
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009347#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009348#define INITFUNC initposix
9349#define MODNAME "posix"
9350#endif
9351
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009352PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009353INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009354{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009355 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009356
Victor Stinnerd6f85422010-05-05 23:33:33 +00009357 m = Py_InitModule3(MODNAME,
9358 posix_methods,
9359 posix__doc__);
9360 if (m == NULL)
9361 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009362
Victor Stinnerd6f85422010-05-05 23:33:33 +00009363 /* Initialize environ dictionary */
9364 v = convertenviron();
9365 Py_XINCREF(v);
9366 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9367 return;
9368 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009369
Victor Stinnerd6f85422010-05-05 23:33:33 +00009370 if (all_ins(m))
9371 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009372
Victor Stinnerd6f85422010-05-05 23:33:33 +00009373 if (setup_confname_tables(m))
9374 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009375
Victor Stinnerd6f85422010-05-05 23:33:33 +00009376 Py_INCREF(PyExc_OSError);
9377 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009378
Guido van Rossumb3d39562000-01-31 18:41:26 +00009379#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00009380 if (posix_putenv_garbage == NULL)
9381 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009382#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009383
Victor Stinnerd6f85422010-05-05 23:33:33 +00009384 if (!initialized) {
9385 stat_result_desc.name = MODNAME ".stat_result";
9386 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9387 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9388 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9389 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9390 structseq_new = StatResultType.tp_new;
9391 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009392
Victor Stinnerd6f85422010-05-05 23:33:33 +00009393 statvfs_result_desc.name = MODNAME ".statvfs_result";
9394 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009395#ifdef NEED_TICKS_PER_SECOND
9396# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009397 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009398# elif defined(HZ)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009399 ticks_per_second = HZ;
Martin v. Löwis03824e42008-12-29 18:17:34 +00009400# else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009401 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis03824e42008-12-29 18:17:34 +00009402# endif
9403#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009404 }
9405 Py_INCREF((PyObject*) &StatResultType);
9406 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9407 Py_INCREF((PyObject*) &StatVFSResultType);
9408 PyModule_AddObject(m, "statvfs_result",
9409 (PyObject*) &StatVFSResultType);
9410 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009411
9412#ifdef __APPLE__
Victor Stinnerd6f85422010-05-05 23:33:33 +00009413 /*
9414 * Step 2 of weak-linking support on Mac OS X.
9415 *
9416 * The code below removes functions that are not available on the
9417 * currently active platform.
9418 *
9419 * This block allow one to use a python binary that was build on
9420 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9421 * OSX 10.4.
9422 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009423#ifdef HAVE_FSTATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009424 if (fstatvfs == NULL) {
9425 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9426 return;
9427 }
9428 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009429#endif /* HAVE_FSTATVFS */
9430
9431#ifdef HAVE_STATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009432 if (statvfs == NULL) {
9433 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9434 return;
9435 }
9436 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009437#endif /* HAVE_STATVFS */
9438
9439# ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00009440 if (lchown == NULL) {
9441 if (PyObject_DelAttrString(m, "lchown") == -1) {
9442 return;
9443 }
9444 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009445#endif /* HAVE_LCHOWN */
9446
9447
9448#endif /* __APPLE__ */
9449
Guido van Rossumb6775db1994-08-01 11:34:53 +00009450}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009451
9452#ifdef __cplusplus
9453}
9454#endif
9455
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009456