blob: 65ade9fb9fd8a5cc13c89bf976d0b31525bbf9cb [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Ronald Oussorend06b6f22006-04-23 11:59:25 +000014#ifdef __APPLE__
15 /*
Victor Stinnerd6f85422010-05-05 23:33:33 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Ronald Oussorend06b6f22006-04-23 11:59:25 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
29#include "structseq.h"
Serhiy Storchakada5c2a02013-02-12 09:27:53 +020030#ifndef MS_WINDOWS
31#include "posixmodule.h"
32#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000036#endif /* defined(__VMS) */
37
Anthony Baxterac6bd462006-04-13 02:06:09 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000042PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000043"This module provides access to operating system functionality that is\n\
44standardized by the C Standard and the POSIX standard (a thinly\n\
45disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000046corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000048#ifndef Py_USING_UNICODE
49/* This is used in signatures of functions. */
50#define Py_UNICODE void
51#endif
52
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000053#if defined(PYOS_OS2)
54#define INCL_DOS
55#define INCL_DOSERRORS
56#define INCL_DOSPROCESS
57#define INCL_NOPMAPI
58#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#if defined(PYCC_GCC)
60#include <ctype.h>
61#include <io.h>
62#include <stdio.h>
63#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000064#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000065#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000066#endif
67
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000068#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000070#endif /* HAVE_SYS_TYPES_H */
71
72#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000073#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000074#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000075
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#ifdef HAVE_SYS_WAIT_H
Victor Stinnerd6f85422010-05-05 23:33:33 +000077#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000078#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000079
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000080#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000081#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000082#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000083
Guido van Rossumb6775db1994-08-01 11:34:53 +000084#ifdef HAVE_FCNTL_H
85#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000086#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Guido van Rossuma6535fd2001-10-18 19:44:10 +000088#ifdef HAVE_GRP_H
89#include <grp.h>
90#endif
91
Barry Warsaw5676bd12003-01-07 20:57:09 +000092#ifdef HAVE_SYSEXITS_H
93#include <sysexits.h>
94#endif /* HAVE_SYSEXITS_H */
95
Anthony Baxter8a560de2004-10-13 15:30:56 +000096#ifdef HAVE_SYS_LOADAVG_H
97#include <sys/loadavg.h>
98#endif
99
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000100/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000101/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000103#include <process.h>
104#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000105#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000106#define HAVE_GETCWD 1
107#define HAVE_OPENDIR 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000108#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#if defined(__OS2__)
110#define HAVE_EXECV 1
111#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000112#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000113#include <process.h>
114#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000115#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_EXECV 1
117#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118#define HAVE_OPENDIR 1
119#define HAVE_PIPE 1
120#define HAVE_POPEN 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000121#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000122#define HAVE_WAIT 1
123#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000124#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000125#define HAVE_GETCWD 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000126#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_EXECV 1
128#define HAVE_PIPE 1
129#define HAVE_POPEN 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000130#define HAVE_SYSTEM 1
131#define HAVE_CWAIT 1
132#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000133#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000134#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000135#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
136/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinnerd6f85422010-05-05 23:33:33 +0000137#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000138/* Unix functions that the configure script doesn't check for */
139#define HAVE_EXECV 1
140#define HAVE_FORK 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000141#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000142#define HAVE_FORK1 1
143#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144#define HAVE_GETCWD 1
145#define HAVE_GETEGID 1
146#define HAVE_GETEUID 1
147#define HAVE_GETGID 1
148#define HAVE_GETPPID 1
149#define HAVE_GETUID 1
150#define HAVE_KILL 1
151#define HAVE_OPENDIR 1
152#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000153#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000155#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000156#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157#define HAVE_WAIT 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000158#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000159#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#endif /* _MSC_VER */
161#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000162#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000163#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000164
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000166
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000167#if defined(__sgi)&&_COMPILER_VERSION>=700
168/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
169 (default) */
170extern char *ctermid_r(char *);
171#endif
172
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000173#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000174#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000177#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000179#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000180extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000182#endif
183#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000184extern int chdir(char *);
185extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chdir(const char *);
188extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000189#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000190#ifdef __BORLANDC__
191extern int chmod(const char *, int);
192#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000194#endif
Christian Heimes36281872007-11-30 21:11:28 +0000195/*#ifdef HAVE_FCHMOD
196extern int fchmod(int, mode_t);
197#endif*/
198/*#ifdef HAVE_LCHMOD
199extern int lchmod(const char *, mode_t);
200#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000201extern int chown(const char *, uid_t, gid_t);
202extern char *getcwd(char *, int);
203extern char *strerror(int);
204extern int link(const char *, const char *);
205extern int rename(const char *, const char *);
206extern int stat(const char *, struct stat *);
207extern int unlink(const char *);
208extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000211#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000213extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000216
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#ifdef HAVE_UTIME_H
220#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000223#ifdef HAVE_SYS_UTIME_H
224#include <sys/utime.h>
225#define HAVE_UTIME_H /* pretend we do for the rest of this file */
226#endif /* HAVE_SYS_UTIME_H */
227
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#ifdef HAVE_SYS_TIMES_H
229#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000230#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231
232#ifdef HAVE_SYS_PARAM_H
233#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235
236#ifdef HAVE_SYS_UTSNAME_H
237#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) strlen((dirent)->d_name)
243#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000244#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000245#include <direct.h>
246#define NAMLEN(dirent) strlen((dirent)->d_name)
247#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000250#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000251#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000256#endif
257#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259#endif
260#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000263#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000265#endif
266#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000268#endif
269#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000271#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000272#include "osdefs.h"
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000273#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <windows.h>
Victor Stinnerd6f85422010-05-05 23:33:33 +0000275#include <shellapi.h> /* for ShellExecute() */
276#define popen _popen
277#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279
Guido van Rossumd48f2521997-12-05 22:19:34 +0000280#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000282#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283
Tim Petersbc2e10e2002-03-03 23:17:02 +0000284#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000285#if defined(PATH_MAX) && PATH_MAX > 1024
286#define MAXPATHLEN PATH_MAX
287#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000289#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#endif /* MAXPATHLEN */
291
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000292#ifdef UNION_WAIT
293/* Emulate some macros on systems that have a union instead of macros */
294
295#ifndef WIFEXITED
296#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
297#endif
298
299#ifndef WEXITSTATUS
300#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
301#endif
302
303#ifndef WTERMSIG
304#define WTERMSIG(u_wait) ((u_wait).w_termsig)
305#endif
306
Neal Norwitzd5a37542006-03-20 06:48:34 +0000307#define WAIT_TYPE union wait
308#define WAIT_STATUS_INT(s) (s.w_status)
309
310#else /* !UNION_WAIT */
311#define WAIT_TYPE int
312#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000313#endif /* UNION_WAIT */
314
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000315/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou4fe38582009-05-24 12:15:04 +0000316#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000317#define PARSE_PID "i"
318#define PyLong_FromPid PyInt_FromLong
319#define PyLong_AsPid PyInt_AsLong
320#elif SIZEOF_PID_T == SIZEOF_LONG
321#define PARSE_PID "l"
322#define PyLong_FromPid PyInt_FromLong
323#define PyLong_AsPid PyInt_AsLong
324#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
325#define PARSE_PID "L"
326#define PyLong_FromPid PyLong_FromLongLong
327#define PyLong_AsPid PyInt_AsLongLong
328#else
329#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000330#endif /* SIZEOF_PID_T */
331
Greg Wardb48bc172000-03-01 21:51:56 +0000332/* Don't use the "_r" form if we don't need it (also, won't have a
333 prototype for it, at least on Solaris -- maybe others as well?). */
334#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
335#define USE_CTERMID_R
336#endif
337
338#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
339#define USE_TMPNAM_R
340#endif
341
Tim Peters11b23062003-04-23 02:39:17 +0000342#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000343#include <sys/mkdev.h>
344#else
345#if defined(MAJOR_IN_SYSMACROS)
346#include <sys/sysmacros.h>
347#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000348#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
349#include <sys/mkdev.h>
350#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000351#endif
Fred Drake699f3522000-06-29 21:12:41 +0000352
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200353
354#ifndef MS_WINDOWS
355PyObject *
356_PyInt_FromUid(uid_t uid)
357{
358 if (uid <= LONG_MAX)
359 return PyInt_FromLong(uid);
Benjamin Peterson4d7fc3c2013-03-23 15:35:24 -0500360 return PyLong_FromUnsignedLong(uid);
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200361}
362
363PyObject *
364_PyInt_FromGid(gid_t gid)
365{
366 if (gid <= LONG_MAX)
367 return PyInt_FromLong(gid);
Benjamin Peterson4d7fc3c2013-03-23 15:35:24 -0500368 return PyLong_FromUnsignedLong(gid);
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200369}
370
371int
372_Py_Uid_Converter(PyObject *obj, void *p)
373{
374 int overflow;
375 long result;
376 if (PyFloat_Check(obj)) {
377 PyErr_SetString(PyExc_TypeError,
378 "integer argument expected, got float");
379 return 0;
380 }
381 result = PyLong_AsLongAndOverflow(obj, &overflow);
382 if (overflow < 0)
383 goto OverflowDown;
384 if (!overflow && result == -1) {
385 /* error or -1 */
386 if (PyErr_Occurred())
387 return 0;
388 *(uid_t *)p = (uid_t)-1;
389 }
390 else {
391 /* unsigned uid_t */
392 unsigned long uresult;
393 if (overflow > 0) {
394 uresult = PyLong_AsUnsignedLong(obj);
395 if (PyErr_Occurred()) {
396 if (PyErr_ExceptionMatches(PyExc_OverflowError))
397 goto OverflowUp;
398 return 0;
399 }
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200400 } else {
401 if (result < 0)
402 goto OverflowDown;
403 uresult = result;
404 }
405 if (sizeof(uid_t) < sizeof(long) &&
406 (unsigned long)(uid_t)uresult != uresult)
407 goto OverflowUp;
408 *(uid_t *)p = (uid_t)uresult;
409 }
410 return 1;
411
412OverflowDown:
413 PyErr_SetString(PyExc_OverflowError,
414 "user id is less than minimum");
415 return 0;
416
417OverflowUp:
418 PyErr_SetString(PyExc_OverflowError,
419 "user id is greater than maximum");
420 return 0;
421}
422
423int
424_Py_Gid_Converter(PyObject *obj, void *p)
425{
426 int overflow;
427 long result;
428 if (PyFloat_Check(obj)) {
429 PyErr_SetString(PyExc_TypeError,
430 "integer argument expected, got float");
431 return 0;
432 }
433 result = PyLong_AsLongAndOverflow(obj, &overflow);
434 if (overflow < 0)
435 goto OverflowDown;
436 if (!overflow && result == -1) {
437 /* error or -1 */
438 if (PyErr_Occurred())
439 return 0;
440 *(gid_t *)p = (gid_t)-1;
441 }
442 else {
443 /* unsigned gid_t */
444 unsigned long uresult;
445 if (overflow > 0) {
446 uresult = PyLong_AsUnsignedLong(obj);
447 if (PyErr_Occurred()) {
448 if (PyErr_ExceptionMatches(PyExc_OverflowError))
449 goto OverflowUp;
450 return 0;
451 }
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200452 } else {
453 if (result < 0)
454 goto OverflowDown;
455 uresult = result;
456 }
457 if (sizeof(gid_t) < sizeof(long) &&
458 (unsigned long)(gid_t)uresult != uresult)
459 goto OverflowUp;
460 *(gid_t *)p = (gid_t)uresult;
461 }
462 return 1;
463
464OverflowDown:
465 PyErr_SetString(PyExc_OverflowError,
466 "group id is less than minimum");
467 return 0;
468
469OverflowUp:
470 PyErr_SetString(PyExc_OverflowError,
471 "group id is greater than maximum");
472 return 0;
473}
474#endif /* MS_WINDOWS */
475
476
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000477#if defined _MSC_VER && _MSC_VER >= 1400
478/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200479 * valid and raise an assertion if it isn't.
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000480 * Normally, an invalid fd is likely to be a C program error and therefore
481 * an assertion can be useful, but it does contradict the POSIX standard
482 * which for write(2) states:
483 * "Otherwise, -1 shall be returned and errno set to indicate the error."
484 * "[EBADF] The fildes argument is not a valid file descriptor open for
485 * writing."
486 * Furthermore, python allows the user to enter any old integer
487 * as a fd and should merely raise a python exception on error.
488 * The Microsoft CRT doesn't provide an official way to check for the
489 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinnerd6f85422010-05-05 23:33:33 +0000490 * by using the exported __pinfo data member and knowledge of the
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000491 * internal structures involved.
492 * The structures below must be updated for each version of visual studio
493 * according to the file internal.h in the CRT source, until MS comes
494 * up with a less hacky way to do this.
495 * (all of this is to avoid globally modifying the CRT behaviour using
496 * _set_invalid_parameter_handler() and _CrtSetReportMode())
497 */
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000498/* The actual size of the structure is determined at runtime.
499 * Only the first items must be present.
500 */
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000501typedef struct {
Victor Stinnerd6f85422010-05-05 23:33:33 +0000502 intptr_t osfhnd;
503 char osfile;
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000504} my_ioinfo;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000505
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000506extern __declspec(dllimport) char * __pioinfo[];
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000507#define IOINFO_L2E 5
508#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
509#define IOINFO_ARRAYS 64
510#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
511#define FOPEN 0x01
512#define _NO_CONSOLE_FILENO (intptr_t)-2
513
514/* This function emulates what the windows CRT does to validate file handles */
515int
516_PyVerify_fd(int fd)
517{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000518 const int i1 = fd >> IOINFO_L2E;
519 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000520
Victor Stinnerd6f85422010-05-05 23:33:33 +0000521 static int sizeof_ioinfo = 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000522
Victor Stinnerd6f85422010-05-05 23:33:33 +0000523 /* Determine the actual size of the ioinfo structure,
524 * as used by the CRT loaded in memory
525 */
526 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
527 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
528 }
529 if (sizeof_ioinfo == 0) {
530 /* This should not happen... */
531 goto fail;
532 }
533
534 /* See that it isn't a special CLEAR fileno */
535 if (fd != _NO_CONSOLE_FILENO) {
536 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
537 * we check pointer validity and other info
538 */
539 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
540 /* finally, check that the file is open */
541 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
542 if (info->osfile & FOPEN) {
543 return 1;
544 }
545 }
546 }
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000547 fail:
Victor Stinnerd6f85422010-05-05 23:33:33 +0000548 errno = EBADF;
549 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000550}
551
552/* the special case of checking dup2. The target fd must be in a sensible range */
553static int
554_PyVerify_fd_dup2(int fd1, int fd2)
555{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000556 if (!_PyVerify_fd(fd1))
557 return 0;
558 if (fd2 == _NO_CONSOLE_FILENO)
559 return 0;
560 if ((unsigned)fd2 < _NHANDLE_)
561 return 1;
562 else
563 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000564}
565#else
566/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
567#define _PyVerify_fd_dup2(A, B) (1)
568#endif
569
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100571#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +0000572/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100573** environ directly, we must obtain it with _NSGetEnviron(). See also
574** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +0000575*/
576#include <crt_externs.h>
577static char **environ;
578#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000580#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581
Barry Warsaw53699e91996-12-10 23:23:01 +0000582static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000583convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000585 PyObject *d;
586 char **e;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000587#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +0000588 APIRET rc;
589 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
590#endif
591 d = PyDict_New();
592 if (d == NULL)
593 return NULL;
Ronald Oussoren1c60c7a2013-01-25 17:55:39 +0100594#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinnerd6f85422010-05-05 23:33:33 +0000595 if (environ == NULL)
596 environ = *_NSGetEnviron();
597#endif
598 if (environ == NULL)
599 return d;
600 /* This part ignores errors */
601 for (e = environ; *e != NULL; e++) {
602 PyObject *k;
603 PyObject *v;
604 char *p = strchr(*e, '=');
605 if (p == NULL)
606 continue;
607 k = PyString_FromStringAndSize(*e, (int)(p-*e));
608 if (k == NULL) {
609 PyErr_Clear();
610 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000611 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000612 v = PyString_FromString(p+1);
613 if (v == NULL) {
614 PyErr_Clear();
615 Py_DECREF(k);
616 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000617 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000618 if (PyDict_GetItem(d, k) == NULL) {
619 if (PyDict_SetItem(d, k, v) != 0)
620 PyErr_Clear();
621 }
622 Py_DECREF(k);
623 Py_DECREF(v);
624 }
625#if defined(PYOS_OS2)
626 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
627 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
628 PyObject *v = PyString_FromString(buffer);
629 PyDict_SetItemString(d, "BEGINLIBPATH", v);
630 Py_DECREF(v);
631 }
632 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
633 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
634 PyObject *v = PyString_FromString(buffer);
635 PyDict_SetItemString(d, "ENDLIBPATH", v);
636 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000637 }
638#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000639 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640}
641
642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643/* Set a POSIX-specific error from errno, and return NULL */
644
Barry Warsawd58d7641998-07-23 16:14:40 +0000645static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000647{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000648 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Barry Warsawd58d7641998-07-23 16:14:40 +0000650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000651posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000652{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000654}
655
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000656#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000657static PyObject *
658posix_error_with_unicode_filename(Py_UNICODE* name)
659{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000660 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000662#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000663
664
Mark Hammondef8b6542001-05-13 08:04:26 +0000665static PyObject *
666posix_error_with_allocated_filename(char* name)
667{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000668 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
669 PyMem_Free(name);
670 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000671}
672
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000673#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000674static PyObject *
675win32_error(char* function, char* filename)
676{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000677 /* XXX We should pass the function name along in the future.
678 (_winreg.c also wants to pass the function name.)
679 This would however require an additional param to the
680 Windows error object, which is non-trivial.
681 */
682 errno = GetLastError();
683 if (filename)
684 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
685 else
686 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000687}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000688
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000689static PyObject *
690win32_error_unicode(char* function, Py_UNICODE* filename)
691{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000692 /* XXX - see win32_error for comments on 'function' */
693 errno = GetLastError();
694 if (filename)
695 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
696 else
697 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000698}
699
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000700static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000701convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000702{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000703 if (PyUnicode_CheckExact(*param))
704 Py_INCREF(*param);
705 else if (PyUnicode_Check(*param))
706 /* For a Unicode subtype that's not a Unicode object,
707 return a true Unicode object with the same data. */
708 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
709 PyUnicode_GET_SIZE(*param));
710 else
711 *param = PyUnicode_FromEncodedObject(*param,
712 Py_FileSystemDefaultEncoding,
713 "strict");
714 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000715}
716
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000717#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718
Guido van Rossumd48f2521997-12-05 22:19:34 +0000719#if defined(PYOS_OS2)
720/**********************************************************************
721 * Helper Function to Trim and Format OS/2 Messages
722 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000723static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000724os2_formatmsg(char *msgbuf, int msglen, char *reason)
725{
726 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
727
728 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
Victor Stinner862490a2010-05-06 00:03:44 +0000729 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
Guido van Rossumd48f2521997-12-05 22:19:34 +0000730
Victor Stinner862490a2010-05-06 00:03:44 +0000731 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
732 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000733 }
734
735 /* Add Optional Reason Text */
736 if (reason) {
737 strcat(msgbuf, " : ");
738 strcat(msgbuf, reason);
739 }
740}
741
742/**********************************************************************
743 * Decode an OS/2 Operating System Error Code
744 *
745 * A convenience function to lookup an OS/2 error code and return a
746 * text message we can use to raise a Python exception.
747 *
748 * Notes:
749 * The messages for errors returned from the OS/2 kernel reside in
750 * the file OSO001.MSG in the \OS2 directory hierarchy.
751 *
752 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000753static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000754os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
755{
756 APIRET rc;
757 ULONG msglen;
758
759 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
760 Py_BEGIN_ALLOW_THREADS
761 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
762 errorcode, "oso001.msg", &msglen);
763 Py_END_ALLOW_THREADS
764
765 if (rc == NO_ERROR)
766 os2_formatmsg(msgbuf, msglen, reason);
767 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000768 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000769 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000770
771 return msgbuf;
772}
773
774/* Set an OS/2-specific error and return NULL. OS/2 kernel
775 errors are not in a global variable e.g. 'errno' nor are
776 they congruent with posix error numbers. */
777
Victor Stinnerd6f85422010-05-05 23:33:33 +0000778static PyObject *
779os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000780{
781 char text[1024];
782 PyObject *v;
783
784 os2_strerror(text, sizeof(text), code, "");
785
786 v = Py_BuildValue("(is)", code, text);
787 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000788 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000789 Py_DECREF(v);
790 }
791 return NULL; /* Signal to Python that an Exception is Pending */
792}
793
794#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795
796/* POSIX generic methods */
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000799posix_fildes(PyObject *fdobj, int (*func)(int))
800{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000801 int fd;
802 int res;
803 fd = PyObject_AsFileDescriptor(fdobj);
804 if (fd < 0)
805 return NULL;
806 if (!_PyVerify_fd(fd))
807 return posix_error();
808 Py_BEGIN_ALLOW_THREADS
809 res = (*func)(fd);
810 Py_END_ALLOW_THREADS
811 if (res < 0)
812 return posix_error();
813 Py_INCREF(Py_None);
814 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000815}
Guido van Rossum21142a01999-01-08 21:05:37 +0000816
817static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000818posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000820 char *path1 = NULL;
821 int res;
822 if (!PyArg_ParseTuple(args, format,
823 Py_FileSystemDefaultEncoding, &path1))
824 return NULL;
825 Py_BEGIN_ALLOW_THREADS
826 res = (*func)(path1);
827 Py_END_ALLOW_THREADS
828 if (res < 0)
829 return posix_error_with_allocated_filename(path1);
830 PyMem_Free(path1);
831 Py_INCREF(Py_None);
832 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833}
834
Barry Warsaw53699e91996-12-10 23:23:01 +0000835static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000836posix_2str(PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000837 char *format,
838 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000840 char *path1 = NULL, *path2 = NULL;
841 int res;
842 if (!PyArg_ParseTuple(args, format,
843 Py_FileSystemDefaultEncoding, &path1,
844 Py_FileSystemDefaultEncoding, &path2))
845 return NULL;
846 Py_BEGIN_ALLOW_THREADS
847 res = (*func)(path1, path2);
848 Py_END_ALLOW_THREADS
849 PyMem_Free(path1);
850 PyMem_Free(path2);
851 if (res != 0)
852 /* XXX how to report both path1 and path2??? */
853 return posix_error();
854 Py_INCREF(Py_None);
855 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856}
857
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000858#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000859static PyObject*
Victor Stinnerd6f85422010-05-05 23:33:33 +0000860win32_1str(PyObject* args, char* func,
861 char* format, BOOL (__stdcall *funcA)(LPCSTR),
862 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000863{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000864 PyObject *uni;
865 char *ansi;
866 BOOL result;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +0000867
Victor Stinnerd6f85422010-05-05 23:33:33 +0000868 if (!PyArg_ParseTuple(args, wformat, &uni))
869 PyErr_Clear();
870 else {
871 Py_BEGIN_ALLOW_THREADS
872 result = funcW(PyUnicode_AsUnicode(uni));
873 Py_END_ALLOW_THREADS
874 if (!result)
875 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
876 Py_INCREF(Py_None);
877 return Py_None;
878 }
879 if (!PyArg_ParseTuple(args, format, &ansi))
880 return NULL;
881 Py_BEGIN_ALLOW_THREADS
882 result = funcA(ansi);
883 Py_END_ALLOW_THREADS
884 if (!result)
885 return win32_error(func, ansi);
886 Py_INCREF(Py_None);
887 return Py_None;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000888
889}
890
891/* This is a reimplementation of the C library's chdir function,
892 but one that produces Win32 errors instead of DOS error codes.
893 chdir is essentially a wrapper around SetCurrentDirectory; however,
894 it also needs to set "magic" environment variables indicating
895 the per-drive current directory, which are of the form =<drive>: */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000896static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000897win32_chdir(LPCSTR path)
898{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000899 char new_path[MAX_PATH+1];
900 int result;
901 char env[4] = "=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000902
Victor Stinnerd6f85422010-05-05 23:33:33 +0000903 if(!SetCurrentDirectoryA(path))
904 return FALSE;
905 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
906 if (!result)
907 return FALSE;
908 /* In the ANSI API, there should not be any paths longer
909 than MAX_PATH. */
910 assert(result <= MAX_PATH+1);
911 if (strncmp(new_path, "\\\\", 2) == 0 ||
912 strncmp(new_path, "//", 2) == 0)
913 /* UNC path, nothing to do. */
914 return TRUE;
915 env[1] = new_path[0];
916 return SetEnvironmentVariableA(env, new_path);
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000917}
918
919/* The Unicode version differs from the ANSI version
920 since the current directory might exceed MAX_PATH characters */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000921static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000922win32_wchdir(LPCWSTR path)
923{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000924 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
925 int result;
926 wchar_t env[4] = L"=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000927
Victor Stinnerd6f85422010-05-05 23:33:33 +0000928 if(!SetCurrentDirectoryW(path))
929 return FALSE;
930 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
931 if (!result)
932 return FALSE;
933 if (result > MAX_PATH+1) {
934 new_path = malloc(result * sizeof(wchar_t));
935 if (!new_path) {
936 SetLastError(ERROR_OUTOFMEMORY);
937 return FALSE;
938 }
939 result = GetCurrentDirectoryW(result, new_path);
940 if (!result) {
941 free(new_path);
942 return FALSE;
943 }
944 }
945 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
946 wcsncmp(new_path, L"//", 2) == 0)
947 /* UNC path, nothing to do. */
948 return TRUE;
949 env[1] = new_path[0];
950 result = SetEnvironmentVariableW(env, new_path);
951 if (new_path != _new_path)
952 free(new_path);
953 return result;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000954}
955#endif
956
Antoine Pitrouff48c0a2011-07-01 22:56:03 +0200957/* choose the appropriate stat and fstat functions and return structs */
958#undef STAT
959#undef FSTAT
960#undef STRUCT_STAT
961#if defined(MS_WIN64) || defined(MS_WINDOWS)
962# define STAT win32_stat
963# define FSTAT win32_fstat
964# define STRUCT_STAT struct win32_stat
965#else
966# define STAT stat
967# define FSTAT fstat
968# define STRUCT_STAT struct stat
969#endif
970
Martin v. Löwis14694662006-02-03 12:54:16 +0000971#ifdef MS_WINDOWS
972/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
973 - time stamps are restricted to second resolution
974 - file modification times suffer from forth-and-back conversions between
975 UTC and local time
976 Therefore, we implement our own stat, based on the Win32 API directly.
977*/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000978#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000979
980struct win32_stat{
981 int st_dev;
982 __int64 st_ino;
983 unsigned short st_mode;
984 int st_nlink;
985 int st_uid;
986 int st_gid;
987 int st_rdev;
988 __int64 st_size;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000989 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000990 int st_atime_nsec;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000991 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000992 int st_mtime_nsec;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000993 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 int st_ctime_nsec;
995};
996
997static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
998
999static void
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001000FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001001{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001002 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1003 /* Cannot simply cast and dereference in_ptr,
1004 since it might not be aligned properly */
1005 __int64 in;
1006 memcpy(&in, in_ptr, sizeof(in));
1007 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001008 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001009}
1010
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001011static void
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00001012time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001013{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001014 /* XXX endianness */
1015 __int64 out;
1016 out = time_in + secs_between_epochs;
1017 out = out * 10000000 + nsec_in / 100;
1018 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001019}
1020
Martin v. Löwis14694662006-02-03 12:54:16 +00001021/* Below, we *know* that ugo+r is 0444 */
1022#if _S_IREAD != 0400
1023#error Unsupported C library
1024#endif
1025static int
1026attributes_to_mode(DWORD attr)
1027{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001028 int m = 0;
1029 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1030 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1031 else
1032 m |= _S_IFREG;
1033 if (attr & FILE_ATTRIBUTE_READONLY)
1034 m |= 0444;
1035 else
1036 m |= 0666;
1037 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001038}
1039
1040static int
1041attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
1042{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001043 memset(result, 0, sizeof(*result));
1044 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1045 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1046 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1047 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1048 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +00001049
Victor Stinnerd6f85422010-05-05 23:33:33 +00001050 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001051}
1052
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001053static BOOL
1054attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1055{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001056 HANDLE hFindFile;
1057 WIN32_FIND_DATAA FileData;
1058 hFindFile = FindFirstFileA(pszFile, &FileData);
1059 if (hFindFile == INVALID_HANDLE_VALUE)
1060 return FALSE;
1061 FindClose(hFindFile);
1062 pfad->dwFileAttributes = FileData.dwFileAttributes;
1063 pfad->ftCreationTime = FileData.ftCreationTime;
1064 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1065 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1066 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1067 pfad->nFileSizeLow = FileData.nFileSizeLow;
1068 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001069}
1070
1071static BOOL
1072attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1073{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001074 HANDLE hFindFile;
1075 WIN32_FIND_DATAW FileData;
1076 hFindFile = FindFirstFileW(pszFile, &FileData);
1077 if (hFindFile == INVALID_HANDLE_VALUE)
1078 return FALSE;
1079 FindClose(hFindFile);
1080 pfad->dwFileAttributes = FileData.dwFileAttributes;
1081 pfad->ftCreationTime = FileData.ftCreationTime;
1082 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1083 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1084 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1085 pfad->nFileSizeLow = FileData.nFileSizeLow;
1086 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +00001087}
1088
Victor Stinnerd6f85422010-05-05 23:33:33 +00001089static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001090win32_stat(const char* path, struct win32_stat *result)
1091{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001092 WIN32_FILE_ATTRIBUTE_DATA info;
1093 int code;
1094 char *dot;
1095 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1096 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1097 /* Protocol violation: we explicitly clear errno, instead of
1098 setting it to a POSIX error. Callers should use GetLastError. */
1099 errno = 0;
1100 return -1;
1101 } else {
1102 /* Could not get attributes on open file. Fall back to
1103 reading the directory. */
1104 if (!attributes_from_dir(path, &info)) {
1105 /* Very strange. This should not fail now */
1106 errno = 0;
1107 return -1;
1108 }
1109 }
1110 }
1111 code = attribute_data_to_stat(&info, result);
1112 if (code != 0)
1113 return code;
1114 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1115 dot = strrchr(path, '.');
1116 if (dot) {
1117 if (stricmp(dot, ".bat") == 0 ||
1118 stricmp(dot, ".cmd") == 0 ||
1119 stricmp(dot, ".exe") == 0 ||
1120 stricmp(dot, ".com") == 0)
1121 result->st_mode |= 0111;
1122 }
1123 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001124}
1125
Victor Stinnerd6f85422010-05-05 23:33:33 +00001126static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001127win32_wstat(const wchar_t* path, struct win32_stat *result)
1128{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001129 int code;
1130 const wchar_t *dot;
1131 WIN32_FILE_ATTRIBUTE_DATA info;
1132 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1133 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1134 /* Protocol violation: we explicitly clear errno, instead of
1135 setting it to a POSIX error. Callers should use GetLastError. */
1136 errno = 0;
1137 return -1;
1138 } else {
1139 /* Could not get attributes on open file. Fall back to
1140 reading the directory. */
1141 if (!attributes_from_dir_w(path, &info)) {
1142 /* Very strange. This should not fail now */
1143 errno = 0;
1144 return -1;
1145 }
1146 }
1147 }
1148 code = attribute_data_to_stat(&info, result);
1149 if (code < 0)
1150 return code;
1151 /* Set IFEXEC if it is an .exe, .bat, ... */
1152 dot = wcsrchr(path, '.');
1153 if (dot) {
1154 if (_wcsicmp(dot, L".bat") == 0 ||
1155 _wcsicmp(dot, L".cmd") == 0 ||
1156 _wcsicmp(dot, L".exe") == 0 ||
1157 _wcsicmp(dot, L".com") == 0)
1158 result->st_mode |= 0111;
1159 }
1160 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001161}
1162
1163static int
1164win32_fstat(int file_number, struct win32_stat *result)
1165{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001166 BY_HANDLE_FILE_INFORMATION info;
1167 HANDLE h;
1168 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001169
Victor Stinnerd6f85422010-05-05 23:33:33 +00001170 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001171
Victor Stinnerd6f85422010-05-05 23:33:33 +00001172 /* Protocol violation: we explicitly clear errno, instead of
1173 setting it to a POSIX error. Callers should use GetLastError. */
1174 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001175
Victor Stinnerd6f85422010-05-05 23:33:33 +00001176 if (h == INVALID_HANDLE_VALUE) {
1177 /* This is really a C library error (invalid file handle).
1178 We set the Win32 error to the closes one matching. */
1179 SetLastError(ERROR_INVALID_HANDLE);
1180 return -1;
1181 }
1182 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001183
Victor Stinnerd6f85422010-05-05 23:33:33 +00001184 type = GetFileType(h);
1185 if (type == FILE_TYPE_UNKNOWN) {
1186 DWORD error = GetLastError();
1187 if (error != 0) {
1188 return -1;
1189 }
1190 /* else: valid but unknown file */
1191 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001192
Victor Stinnerd6f85422010-05-05 23:33:33 +00001193 if (type != FILE_TYPE_DISK) {
1194 if (type == FILE_TYPE_CHAR)
1195 result->st_mode = _S_IFCHR;
1196 else if (type == FILE_TYPE_PIPE)
1197 result->st_mode = _S_IFIFO;
1198 return 0;
1199 }
1200
1201 if (!GetFileInformationByHandle(h, &info)) {
1202 return -1;
1203 }
1204
1205 /* similar to stat() */
1206 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1207 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1208 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1209 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1210 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1211 /* specific to fstat() */
1212 result->st_nlink = info.nNumberOfLinks;
1213 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1214 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001215}
1216
1217#endif /* MS_WINDOWS */
1218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001219PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001220"stat_result: Result from stat or lstat.\n\n\
1221This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001222 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001223or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1224\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001225Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1226or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001227\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001228See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001229
1230static PyStructSequence_Field stat_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001231 {"st_mode", "protection bits"},
1232 {"st_ino", "inode"},
1233 {"st_dev", "device"},
1234 {"st_nlink", "number of hard links"},
1235 {"st_uid", "user ID of owner"},
1236 {"st_gid", "group ID of owner"},
1237 {"st_size", "total size, in bytes"},
1238 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1239 {NULL, "integer time of last access"},
1240 {NULL, "integer time of last modification"},
1241 {NULL, "integer time of last change"},
1242 {"st_atime", "time of last access"},
1243 {"st_mtime", "time of last modification"},
1244 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001245#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001246 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001247#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001248#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001249 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001250#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001251#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001252 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001253#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001254#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001255 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001256#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001257#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001258 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001259#endif
1260#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001261 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001262#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001263 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001264};
1265
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001266#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001267#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001268#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001269#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001270#endif
1271
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001272#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001273#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1274#else
1275#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1276#endif
1277
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001278#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1280#else
1281#define ST_RDEV_IDX ST_BLOCKS_IDX
1282#endif
1283
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001284#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1285#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1286#else
1287#define ST_FLAGS_IDX ST_RDEV_IDX
1288#endif
1289
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001290#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001291#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001292#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001293#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001294#endif
1295
1296#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1297#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1298#else
1299#define ST_BIRTHTIME_IDX ST_GEN_IDX
1300#endif
1301
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001302static PyStructSequence_Desc stat_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001303 "stat_result", /* name */
1304 stat_result__doc__, /* doc */
1305 stat_result_fields,
1306 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001307};
1308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001309PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1311This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001312 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001313or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001315See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001316
1317static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001318 {"f_bsize", },
1319 {"f_frsize", },
1320 {"f_blocks", },
1321 {"f_bfree", },
1322 {"f_bavail", },
1323 {"f_files", },
1324 {"f_ffree", },
1325 {"f_favail", },
1326 {"f_flag", },
1327 {"f_namemax",},
1328 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001329};
1330
1331static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001332 "statvfs_result", /* name */
1333 statvfs_result__doc__, /* doc */
1334 statvfs_result_fields,
1335 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336};
1337
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001338static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001339static PyTypeObject StatResultType;
1340static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001341static newfunc structseq_new;
1342
1343static PyObject *
1344statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1345{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001346 PyStructSequence *result;
1347 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001348
Victor Stinnerd6f85422010-05-05 23:33:33 +00001349 result = (PyStructSequence*)structseq_new(type, args, kwds);
1350 if (!result)
1351 return NULL;
1352 /* If we have been initialized from a tuple,
1353 st_?time might be set to None. Initialize it
1354 from the int slots. */
1355 for (i = 7; i <= 9; i++) {
1356 if (result->ob_item[i+3] == Py_None) {
1357 Py_DECREF(Py_None);
1358 Py_INCREF(result->ob_item[i]);
1359 result->ob_item[i+3] = result->ob_item[i];
1360 }
1361 }
1362 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001363}
1364
1365
1366
1367/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001368static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001369
1370PyDoc_STRVAR(stat_float_times__doc__,
1371"stat_float_times([newval]) -> oldval\n\n\
1372Determine whether os.[lf]stat represents time stamps as float objects.\n\
1373If newval is True, future calls to stat() return floats, if it is False,\n\
1374future calls return ints. \n\
1375If newval is omitted, return the current setting.\n");
1376
1377static PyObject*
1378stat_float_times(PyObject* self, PyObject *args)
1379{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001380 int newval = -1;
1381 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1382 return NULL;
1383 if (newval == -1)
1384 /* Return old value */
1385 return PyBool_FromLong(_stat_float_times);
1386 _stat_float_times = newval;
1387 Py_INCREF(Py_None);
1388 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001389}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001390
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001391static void
1392fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1393{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001394 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001395#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinnerd6f85422010-05-05 23:33:33 +00001396 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001397#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001398 ival = PyInt_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001399#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001400 if (!ival)
1401 return;
1402 if (_stat_float_times) {
1403 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1404 } else {
1405 fval = ival;
1406 Py_INCREF(fval);
1407 }
1408 PyStructSequence_SET_ITEM(v, index, ival);
1409 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001410}
1411
Tim Peters5aa91602002-01-30 05:46:57 +00001412/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001413 (used by posix_stat() and posix_fstat()) */
1414static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001415_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001416{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001417 unsigned long ansec, mnsec, cnsec;
1418 PyObject *v = PyStructSequence_New(&StatResultType);
1419 if (v == NULL)
1420 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001421
Victor Stinnerd6f85422010-05-05 23:33:33 +00001422 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001423#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001424 PyStructSequence_SET_ITEM(v, 1,
1425 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001426#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001427 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001428#endif
1429#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001430 PyStructSequence_SET_ITEM(v, 2,
1431 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001432#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001433 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001434#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001435 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02001436#if defined(MS_WINDOWS)
1437 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong(0));
1438 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong(0));
1439#else
1440 PyStructSequence_SET_ITEM(v, 4, _PyInt_FromUid(st->st_uid));
1441 PyStructSequence_SET_ITEM(v, 5, _PyInt_FromGid(st->st_gid));
1442#endif
Fred Drake699f3522000-06-29 21:12:41 +00001443#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001444 PyStructSequence_SET_ITEM(v, 6,
1445 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001446#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001447 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001448#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001449
Martin v. Löwis14694662006-02-03 12:54:16 +00001450#if defined(HAVE_STAT_TV_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001451 ansec = st->st_atim.tv_nsec;
1452 mnsec = st->st_mtim.tv_nsec;
1453 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001454#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001455 ansec = st->st_atimespec.tv_nsec;
1456 mnsec = st->st_mtimespec.tv_nsec;
1457 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001458#elif defined(HAVE_STAT_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001459 ansec = st->st_atime_nsec;
1460 mnsec = st->st_mtime_nsec;
1461 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001462#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001463 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001464#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001465 fill_time(v, 7, st->st_atime, ansec);
1466 fill_time(v, 8, st->st_mtime, mnsec);
1467 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001468
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001469#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001470 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1471 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001472#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001473#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001474 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1475 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001476#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001477#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001478 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1479 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001480#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001481#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001482 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1483 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001484#endif
1485#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001486 {
1487 PyObject *val;
1488 unsigned long bsec,bnsec;
1489 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001490#ifdef HAVE_STAT_TV_NSEC2
Victor Stinnerd6f85422010-05-05 23:33:33 +00001491 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001492#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001493 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001494#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001495 if (_stat_float_times) {
1496 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1497 } else {
1498 val = PyInt_FromLong((long)bsec);
1499 }
1500 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1501 val);
1502 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001503#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001504#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001505 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1506 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001507#endif
Fred Drake699f3522000-06-29 21:12:41 +00001508
Victor Stinnerd6f85422010-05-05 23:33:33 +00001509 if (PyErr_Occurred()) {
1510 Py_DECREF(v);
1511 return NULL;
1512 }
Fred Drake699f3522000-06-29 21:12:41 +00001513
Victor Stinnerd6f85422010-05-05 23:33:33 +00001514 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001515}
1516
Martin v. Löwisd8948722004-06-02 09:57:56 +00001517#ifdef MS_WINDOWS
1518
1519/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1520 where / can be used in place of \ and the trailing slash is optional.
1521 Both SERVER and SHARE must have at least one character.
1522*/
1523
1524#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1525#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001526#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001527#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001528#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001529
Tim Peters4ad82172004-08-30 17:02:04 +00001530static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001531IsUNCRootA(char *path, int pathlen)
1532{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001533 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001534
Victor Stinnerd6f85422010-05-05 23:33:33 +00001535 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001536
Victor Stinnerd6f85422010-05-05 23:33:33 +00001537 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1538 /* minimum UNCRoot is \\x\y */
1539 return FALSE;
1540 for (i = 2; i < pathlen ; i++)
1541 if (ISSLASH(path[i])) break;
1542 if (i == 2 || i == pathlen)
1543 /* do not allow \\\SHARE or \\SERVER */
1544 return FALSE;
1545 share = i+1;
1546 for (i = share; i < pathlen; i++)
1547 if (ISSLASH(path[i])) break;
1548 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001549
Victor Stinnerd6f85422010-05-05 23:33:33 +00001550 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001551}
1552
Tim Peters4ad82172004-08-30 17:02:04 +00001553static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001554IsUNCRootW(Py_UNICODE *path, int pathlen)
1555{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001556 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001557
Victor Stinnerd6f85422010-05-05 23:33:33 +00001558 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001559
Victor Stinnerd6f85422010-05-05 23:33:33 +00001560 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1561 /* minimum UNCRoot is \\x\y */
1562 return FALSE;
1563 for (i = 2; i < pathlen ; i++)
1564 if (ISSLASH(path[i])) break;
1565 if (i == 2 || i == pathlen)
1566 /* do not allow \\\SHARE or \\SERVER */
1567 return FALSE;
1568 share = i+1;
1569 for (i = share; i < pathlen; i++)
1570 if (ISSLASH(path[i])) break;
1571 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001572
Victor Stinnerd6f85422010-05-05 23:33:33 +00001573 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001574}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001575#endif /* MS_WINDOWS */
1576
Barry Warsaw53699e91996-12-10 23:23:01 +00001577static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001578posix_do_stat(PyObject *self, PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +00001579 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001580#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001581 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001582#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001583 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001584#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001585 char *wformat,
1586 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001587{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001588 STRUCT_STAT st;
1589 char *path = NULL; /* pass this to stat; do not free() it */
1590 char *pathfree = NULL; /* this memory must be free'd */
1591 int res;
1592 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001593
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001594#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001595 PyUnicodeObject *po;
1596 if (PyArg_ParseTuple(args, wformat, &po)) {
1597 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001598
Victor Stinnerd6f85422010-05-05 23:33:33 +00001599 Py_BEGIN_ALLOW_THREADS
1600 /* PyUnicode_AS_UNICODE result OK without
1601 thread lock as it is a simple dereference. */
1602 res = wstatfunc(wpath, &st);
1603 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001604
Victor Stinnerd6f85422010-05-05 23:33:33 +00001605 if (res != 0)
1606 return win32_error_unicode("stat", wpath);
1607 return _pystat_fromstructstat(&st);
1608 }
1609 /* Drop the argument parsing error as narrow strings
1610 are also valid. */
1611 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001612#endif
1613
Victor Stinnerd6f85422010-05-05 23:33:33 +00001614 if (!PyArg_ParseTuple(args, format,
1615 Py_FileSystemDefaultEncoding, &path))
1616 return NULL;
1617 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001618
Victor Stinnerd6f85422010-05-05 23:33:33 +00001619 Py_BEGIN_ALLOW_THREADS
1620 res = (*statfunc)(path, &st);
1621 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001622
Victor Stinnerd6f85422010-05-05 23:33:33 +00001623 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001624#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001625 result = win32_error("stat", pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001626#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001627 result = posix_error_with_filename(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001628#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001629 }
1630 else
1631 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001632
Victor Stinnerd6f85422010-05-05 23:33:33 +00001633 PyMem_Free(pathfree);
1634 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635}
1636
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001637/* POSIX methods */
1638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001639PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001640"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001641Use the real uid/gid to test for access to a path. Note that most\n\
1642operations will use the effective uid/gid, therefore this routine can\n\
1643be used in a suid/sgid environment to test if the invoking user has the\n\
1644specified access to the path. The mode argument can be F_OK to test\n\
1645existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001646
1647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001648posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001649{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001650 char *path;
1651 int mode;
1652
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001653#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001654 DWORD attr;
1655 PyUnicodeObject *po;
1656 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1657 Py_BEGIN_ALLOW_THREADS
1658 /* PyUnicode_AS_UNICODE OK without thread lock as
1659 it is a simple dereference. */
1660 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1661 Py_END_ALLOW_THREADS
1662 goto finish;
1663 }
1664 /* Drop the argument parsing error as narrow strings
1665 are also valid. */
1666 PyErr_Clear();
1667 if (!PyArg_ParseTuple(args, "eti:access",
1668 Py_FileSystemDefaultEncoding, &path, &mode))
1669 return NULL;
1670 Py_BEGIN_ALLOW_THREADS
1671 attr = GetFileAttributesA(path);
1672 Py_END_ALLOW_THREADS
1673 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001674finish:
Victor Stinnerd6f85422010-05-05 23:33:33 +00001675 if (attr == 0xFFFFFFFF)
1676 /* File does not exist, or cannot read attributes */
1677 return PyBool_FromLong(0);
1678 /* Access is possible if either write access wasn't requested, or
1679 the file isn't read-only, or if it's a directory, as there are
1680 no read-only directories on Windows. */
1681 return PyBool_FromLong(!(mode & 2)
1682 || !(attr & FILE_ATTRIBUTE_READONLY)
1683 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001684#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001685 int res;
1686 if (!PyArg_ParseTuple(args, "eti:access",
1687 Py_FileSystemDefaultEncoding, &path, &mode))
1688 return NULL;
1689 Py_BEGIN_ALLOW_THREADS
1690 res = access(path, mode);
1691 Py_END_ALLOW_THREADS
1692 PyMem_Free(path);
1693 return PyBool_FromLong(res == 0);
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001694#endif /* MS_WINDOWS */
Guido van Rossum94f6f721999-01-06 18:42:14 +00001695}
1696
Guido van Rossumd371ff11999-01-25 16:12:23 +00001697#ifndef F_OK
1698#define F_OK 0
1699#endif
1700#ifndef R_OK
1701#define R_OK 4
1702#endif
1703#ifndef W_OK
1704#define W_OK 2
1705#endif
1706#ifndef X_OK
1707#define X_OK 1
1708#endif
1709
1710#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001712"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001713Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001714
1715static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001716posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001717{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001718 int id;
1719 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001720
Victor Stinnerd6f85422010-05-05 23:33:33 +00001721 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1722 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001723
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001724#if defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001725 /* file descriptor 0 only, the default input device (stdin) */
1726 if (id == 0) {
1727 ret = ttyname();
1728 }
1729 else {
1730 ret = NULL;
1731 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001732#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001733 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001734#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001735 if (ret == NULL)
1736 return posix_error();
1737 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001738}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001739#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001740
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001741#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001742PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001743"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001744Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001745
1746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001747posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001748{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001749 char *ret;
1750 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001751
Greg Wardb48bc172000-03-01 21:51:56 +00001752#ifdef USE_CTERMID_R
Victor Stinnerd6f85422010-05-05 23:33:33 +00001753 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001754#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001755 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001756#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001757 if (ret == NULL)
1758 return posix_error();
1759 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001760}
1761#endif
1762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001763PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001764"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001766
Barry Warsaw53699e91996-12-10 23:23:01 +00001767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001768posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001769{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001770#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001771 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001772#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001773 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001774#elif defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001775 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001776#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001777 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001778#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001779}
1780
Fred Drake4d1e64b2002-04-15 19:40:07 +00001781#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001784Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001786
1787static PyObject *
1788posix_fchdir(PyObject *self, PyObject *fdobj)
1789{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001790 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001791}
1792#endif /* HAVE_FCHDIR */
1793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001796"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001798
Barry Warsaw53699e91996-12-10 23:23:01 +00001799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001800posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001801{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001802 char *path = NULL;
1803 int i;
1804 int res;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001805#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001806 DWORD attr;
1807 PyUnicodeObject *po;
1808 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1809 Py_BEGIN_ALLOW_THREADS
1810 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1811 if (attr != 0xFFFFFFFF) {
1812 if (i & _S_IWRITE)
1813 attr &= ~FILE_ATTRIBUTE_READONLY;
1814 else
1815 attr |= FILE_ATTRIBUTE_READONLY;
1816 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1817 }
1818 else
1819 res = 0;
1820 Py_END_ALLOW_THREADS
1821 if (!res)
1822 return win32_error_unicode("chmod",
1823 PyUnicode_AS_UNICODE(po));
1824 Py_INCREF(Py_None);
1825 return Py_None;
1826 }
1827 /* Drop the argument parsing error as narrow strings
1828 are also valid. */
1829 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00001830
Victor Stinnerd6f85422010-05-05 23:33:33 +00001831 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1832 &path, &i))
1833 return NULL;
1834 Py_BEGIN_ALLOW_THREADS
1835 attr = GetFileAttributesA(path);
1836 if (attr != 0xFFFFFFFF) {
1837 if (i & _S_IWRITE)
1838 attr &= ~FILE_ATTRIBUTE_READONLY;
1839 else
1840 attr |= FILE_ATTRIBUTE_READONLY;
1841 res = SetFileAttributesA(path, attr);
1842 }
1843 else
1844 res = 0;
1845 Py_END_ALLOW_THREADS
1846 if (!res) {
1847 win32_error("chmod", path);
1848 PyMem_Free(path);
1849 return NULL;
1850 }
1851 PyMem_Free(path);
1852 Py_INCREF(Py_None);
1853 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001854#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001855 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1856 &path, &i))
1857 return NULL;
1858 Py_BEGIN_ALLOW_THREADS
1859 res = chmod(path, i);
1860 Py_END_ALLOW_THREADS
1861 if (res < 0)
1862 return posix_error_with_allocated_filename(path);
1863 PyMem_Free(path);
1864 Py_INCREF(Py_None);
1865 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001866#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001867}
1868
Christian Heimes36281872007-11-30 21:11:28 +00001869#ifdef HAVE_FCHMOD
1870PyDoc_STRVAR(posix_fchmod__doc__,
1871"fchmod(fd, mode)\n\n\
1872Change the access permissions of the file given by file\n\
1873descriptor fd.");
1874
1875static PyObject *
1876posix_fchmod(PyObject *self, PyObject *args)
1877{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001878 int fd, mode, res;
1879 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1880 return NULL;
1881 Py_BEGIN_ALLOW_THREADS
1882 res = fchmod(fd, mode);
1883 Py_END_ALLOW_THREADS
1884 if (res < 0)
1885 return posix_error();
1886 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001887}
1888#endif /* HAVE_FCHMOD */
1889
1890#ifdef HAVE_LCHMOD
1891PyDoc_STRVAR(posix_lchmod__doc__,
1892"lchmod(path, mode)\n\n\
1893Change the access permissions of a file. If path is a symlink, this\n\
1894affects the link itself rather than the target.");
1895
1896static PyObject *
1897posix_lchmod(PyObject *self, PyObject *args)
1898{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001899 char *path = NULL;
1900 int i;
1901 int res;
1902 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1903 &path, &i))
1904 return NULL;
1905 Py_BEGIN_ALLOW_THREADS
1906 res = lchmod(path, i);
1907 Py_END_ALLOW_THREADS
1908 if (res < 0)
1909 return posix_error_with_allocated_filename(path);
1910 PyMem_Free(path);
1911 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001912}
1913#endif /* HAVE_LCHMOD */
1914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001915
Martin v. Löwis382abef2007-02-19 10:55:19 +00001916#ifdef HAVE_CHFLAGS
1917PyDoc_STRVAR(posix_chflags__doc__,
1918"chflags(path, flags)\n\n\
1919Set file flags.");
1920
1921static PyObject *
1922posix_chflags(PyObject *self, PyObject *args)
1923{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001924 char *path;
1925 unsigned long flags;
1926 int res;
1927 if (!PyArg_ParseTuple(args, "etk:chflags",
1928 Py_FileSystemDefaultEncoding, &path, &flags))
1929 return NULL;
1930 Py_BEGIN_ALLOW_THREADS
1931 res = chflags(path, flags);
1932 Py_END_ALLOW_THREADS
1933 if (res < 0)
1934 return posix_error_with_allocated_filename(path);
1935 PyMem_Free(path);
1936 Py_INCREF(Py_None);
1937 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001938}
1939#endif /* HAVE_CHFLAGS */
1940
1941#ifdef HAVE_LCHFLAGS
1942PyDoc_STRVAR(posix_lchflags__doc__,
1943"lchflags(path, flags)\n\n\
1944Set file flags.\n\
1945This function will not follow symbolic links.");
1946
1947static PyObject *
1948posix_lchflags(PyObject *self, PyObject *args)
1949{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001950 char *path;
1951 unsigned long flags;
1952 int res;
1953 if (!PyArg_ParseTuple(args, "etk:lchflags",
1954 Py_FileSystemDefaultEncoding, &path, &flags))
1955 return NULL;
1956 Py_BEGIN_ALLOW_THREADS
1957 res = lchflags(path, flags);
1958 Py_END_ALLOW_THREADS
1959 if (res < 0)
1960 return posix_error_with_allocated_filename(path);
1961 PyMem_Free(path);
1962 Py_INCREF(Py_None);
1963 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001964}
1965#endif /* HAVE_LCHFLAGS */
1966
Martin v. Löwis244edc82001-10-04 22:44:26 +00001967#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001968PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001969"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001970Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001971
1972static PyObject *
1973posix_chroot(PyObject *self, PyObject *args)
1974{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001975 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001976}
1977#endif
1978
Guido van Rossum21142a01999-01-08 21:05:37 +00001979#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001980PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001981"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001983
1984static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001985posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001986{
Stefan Krah93f7a322010-11-26 17:35:50 +00001987 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001988}
1989#endif /* HAVE_FSYNC */
1990
1991#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001992
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001993#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001994extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1995#endif
1996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001997PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001998"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001999force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002000 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002001
2002static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002003posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002004{
Stefan Krah93f7a322010-11-26 17:35:50 +00002005 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002006}
2007#endif /* HAVE_FDATASYNC */
2008
2009
Fredrik Lundh10723342000-07-10 16:38:09 +00002010#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002011PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002012"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002014
Barry Warsaw53699e91996-12-10 23:23:01 +00002015static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002016posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002017{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002018 char *path = NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002019 uid_t uid;
2020 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002021 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002022 if (!PyArg_ParseTuple(args, "etO&O&:chown",
Victor Stinnerd6f85422010-05-05 23:33:33 +00002023 Py_FileSystemDefaultEncoding, &path,
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002024 _Py_Uid_Converter, &uid,
2025 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002026 return NULL;
2027 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002028 res = chown(path, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002029 Py_END_ALLOW_THREADS
2030 if (res < 0)
2031 return posix_error_with_allocated_filename(path);
2032 PyMem_Free(path);
2033 Py_INCREF(Py_None);
2034 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002035}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002036#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002037
Christian Heimes36281872007-11-30 21:11:28 +00002038#ifdef HAVE_FCHOWN
2039PyDoc_STRVAR(posix_fchown__doc__,
2040"fchown(fd, uid, gid)\n\n\
2041Change the owner and group id of the file given by file descriptor\n\
2042fd to the numeric uid and gid.");
2043
2044static PyObject *
2045posix_fchown(PyObject *self, PyObject *args)
2046{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002047 int fd;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002048 uid_t uid;
2049 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002050 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002051 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd,
2052 _Py_Uid_Converter, &uid,
2053 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002054 return NULL;
2055 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002056 res = fchown(fd, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002057 Py_END_ALLOW_THREADS
2058 if (res < 0)
2059 return posix_error();
2060 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00002061}
2062#endif /* HAVE_FCHOWN */
2063
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002064#ifdef HAVE_LCHOWN
2065PyDoc_STRVAR(posix_lchown__doc__,
2066"lchown(path, uid, gid)\n\n\
2067Change the owner and group id of path to the numeric uid and gid.\n\
2068This function will not follow symbolic links.");
2069
2070static PyObject *
2071posix_lchown(PyObject *self, PyObject *args)
2072{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002073 char *path = NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002074 uid_t uid;
2075 gid_t gid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002076 int res;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002077 if (!PyArg_ParseTuple(args, "etO&O&:lchown",
Victor Stinnerd6f85422010-05-05 23:33:33 +00002078 Py_FileSystemDefaultEncoding, &path,
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002079 _Py_Uid_Converter, &uid,
2080 _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00002081 return NULL;
2082 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02002083 res = lchown(path, uid, gid);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002084 Py_END_ALLOW_THREADS
2085 if (res < 0)
2086 return posix_error_with_allocated_filename(path);
2087 PyMem_Free(path);
2088 Py_INCREF(Py_None);
2089 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002090}
2091#endif /* HAVE_LCHOWN */
2092
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002093
Guido van Rossum36bc6801995-06-14 22:54:23 +00002094#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002096"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098
Trent Nelsonda4277a2012-08-29 09:20:41 -04002099#if (defined(__sun) && defined(__SVR4)) || \
2100 defined(__OpenBSD__) || \
2101 defined(__NetBSD__)
Stefan Krah182ae642010-07-13 19:17:08 +00002102/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
2103static PyObject *
2104posix_getcwd(PyObject *self, PyObject *noargs)
2105{
2106 char buf[PATH_MAX+2];
2107 char *res;
2108
2109 Py_BEGIN_ALLOW_THREADS
Stefan Krah8cb9f032010-07-13 19:40:00 +00002110 res = getcwd(buf, sizeof buf);
Stefan Krah182ae642010-07-13 19:17:08 +00002111 Py_END_ALLOW_THREADS
2112
2113 if (res == NULL)
2114 return posix_error();
2115
2116 return PyString_FromString(buf);
2117}
2118#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002119static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002120posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002121{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002122 int bufsize_incr = 1024;
2123 int bufsize = 0;
2124 char *tmpbuf = NULL;
2125 char *res = NULL;
2126 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00002127
Victor Stinnerd6f85422010-05-05 23:33:33 +00002128 Py_BEGIN_ALLOW_THREADS
2129 do {
2130 bufsize = bufsize + bufsize_incr;
2131 tmpbuf = malloc(bufsize);
2132 if (tmpbuf == NULL) {
2133 break;
2134 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002135#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002136 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002137#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002138 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002139#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00002140
Victor Stinnerd6f85422010-05-05 23:33:33 +00002141 if (res == NULL) {
2142 free(tmpbuf);
2143 }
2144 } while ((res == NULL) && (errno == ERANGE));
2145 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002146
Victor Stinnerd6f85422010-05-05 23:33:33 +00002147 if (res == NULL)
2148 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002149
Victor Stinnerd6f85422010-05-05 23:33:33 +00002150 dynamic_return = PyString_FromString(tmpbuf);
2151 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002152
Victor Stinnerd6f85422010-05-05 23:33:33 +00002153 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002154}
Stefan Krah182ae642010-07-13 19:17:08 +00002155#endif /* getcwd() NULL/ERANGE workaround. */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156
Walter Dörwald3b918c32002-11-21 20:18:46 +00002157#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158PyDoc_STRVAR(posix_getcwdu__doc__,
2159"getcwdu() -> path\n\n\
2160Return a unicode string representing the current working directory.");
2161
2162static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002163posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002164{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002165 char buf[1026];
2166 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002167
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002168#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002169 DWORD len;
2170 wchar_t wbuf[1026];
2171 wchar_t *wbuf2 = wbuf;
2172 PyObject *resobj;
2173 Py_BEGIN_ALLOW_THREADS
2174 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2175 /* If the buffer is large enough, len does not include the
2176 terminating \0. If the buffer is too small, len includes
2177 the space needed for the terminator. */
2178 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2179 wbuf2 = malloc(len * sizeof(wchar_t));
2180 if (wbuf2)
2181 len = GetCurrentDirectoryW(len, wbuf2);
2182 }
2183 Py_END_ALLOW_THREADS
2184 if (!wbuf2) {
2185 PyErr_NoMemory();
2186 return NULL;
2187 }
2188 if (!len) {
2189 if (wbuf2 != wbuf) free(wbuf2);
2190 return win32_error("getcwdu", NULL);
2191 }
2192 resobj = PyUnicode_FromWideChar(wbuf2, len);
2193 if (wbuf2 != wbuf) free(wbuf2);
2194 return resobj;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002195#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002196
Victor Stinnerd6f85422010-05-05 23:33:33 +00002197 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002198#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002199 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002200#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002201 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002202#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002203 Py_END_ALLOW_THREADS
2204 if (res == NULL)
2205 return posix_error();
2206 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002207}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002208#endif /* Py_USING_UNICODE */
2209#endif /* HAVE_GETCWD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002211
Guido van Rossumb6775db1994-08-01 11:34:53 +00002212#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002214"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002215Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002216
Barry Warsaw53699e91996-12-10 23:23:01 +00002217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002218posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002220 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002222#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002225PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002226"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002227Return a list containing the names of the entries in the directory.\n\
2228\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00002229 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002230\n\
2231The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002233
Barry Warsaw53699e91996-12-10 23:23:01 +00002234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002235posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002236{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002237 /* XXX Should redo this putting the (now four) versions of opendir
2238 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002239#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002240
Victor Stinnerd6f85422010-05-05 23:33:33 +00002241 PyObject *d, *v;
2242 HANDLE hFindFile;
2243 BOOL result;
2244 WIN32_FIND_DATA FileData;
2245 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2246 char *bufptr = namebuf;
2247 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002248
Victor Stinnerd6f85422010-05-05 23:33:33 +00002249 PyObject *po;
2250 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2251 WIN32_FIND_DATAW wFileData;
2252 Py_UNICODE *wnamebuf;
2253 /* Overallocate for \\*.*\0 */
2254 len = PyUnicode_GET_SIZE(po);
2255 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2256 if (!wnamebuf) {
2257 PyErr_NoMemory();
2258 return NULL;
2259 }
2260 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2261 if (len > 0) {
2262 Py_UNICODE wch = wnamebuf[len-1];
2263 if (wch != L'/' && wch != L'\\' && wch != L':')
2264 wnamebuf[len++] = L'\\';
2265 wcscpy(wnamebuf + len, L"*.*");
2266 }
2267 if ((d = PyList_New(0)) == NULL) {
2268 free(wnamebuf);
2269 return NULL;
2270 }
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002271 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002272 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002273 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002274 if (hFindFile == INVALID_HANDLE_VALUE) {
2275 int error = GetLastError();
2276 if (error == ERROR_FILE_NOT_FOUND) {
2277 free(wnamebuf);
2278 return d;
2279 }
2280 Py_DECREF(d);
2281 win32_error_unicode("FindFirstFileW", wnamebuf);
2282 free(wnamebuf);
2283 return NULL;
2284 }
2285 do {
2286 /* Skip over . and .. */
2287 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2288 wcscmp(wFileData.cFileName, L"..") != 0) {
2289 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2290 if (v == NULL) {
2291 Py_DECREF(d);
2292 d = NULL;
2293 break;
2294 }
2295 if (PyList_Append(d, v) != 0) {
2296 Py_DECREF(v);
2297 Py_DECREF(d);
2298 d = NULL;
2299 break;
2300 }
2301 Py_DECREF(v);
2302 }
2303 Py_BEGIN_ALLOW_THREADS
2304 result = FindNextFileW(hFindFile, &wFileData);
2305 Py_END_ALLOW_THREADS
2306 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2307 it got to the end of the directory. */
2308 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2309 Py_DECREF(d);
2310 win32_error_unicode("FindNextFileW", wnamebuf);
2311 FindClose(hFindFile);
2312 free(wnamebuf);
2313 return NULL;
2314 }
2315 } while (result == TRUE);
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002316
Victor Stinnerd6f85422010-05-05 23:33:33 +00002317 if (FindClose(hFindFile) == FALSE) {
2318 Py_DECREF(d);
2319 win32_error_unicode("FindClose", wnamebuf);
2320 free(wnamebuf);
2321 return NULL;
2322 }
2323 free(wnamebuf);
2324 return d;
2325 }
2326 /* Drop the argument parsing error as narrow strings
2327 are also valid. */
2328 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002329
Victor Stinnerd6f85422010-05-05 23:33:33 +00002330 if (!PyArg_ParseTuple(args, "et#:listdir",
2331 Py_FileSystemDefaultEncoding, &bufptr, &len))
2332 return NULL;
2333 if (len > 0) {
2334 char ch = namebuf[len-1];
2335 if (ch != SEP && ch != ALTSEP && ch != ':')
2336 namebuf[len++] = '/';
2337 strcpy(namebuf + len, "*.*");
2338 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002339
Victor Stinnerd6f85422010-05-05 23:33:33 +00002340 if ((d = PyList_New(0)) == NULL)
2341 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002342
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002343 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002344 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002345 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002346 if (hFindFile == INVALID_HANDLE_VALUE) {
2347 int error = GetLastError();
2348 if (error == ERROR_FILE_NOT_FOUND)
2349 return d;
2350 Py_DECREF(d);
2351 return win32_error("FindFirstFile", namebuf);
2352 }
2353 do {
2354 /* Skip over . and .. */
2355 if (strcmp(FileData.cFileName, ".") != 0 &&
2356 strcmp(FileData.cFileName, "..") != 0) {
2357 v = PyString_FromString(FileData.cFileName);
2358 if (v == NULL) {
2359 Py_DECREF(d);
2360 d = NULL;
2361 break;
2362 }
2363 if (PyList_Append(d, v) != 0) {
2364 Py_DECREF(v);
2365 Py_DECREF(d);
2366 d = NULL;
2367 break;
2368 }
2369 Py_DECREF(v);
2370 }
2371 Py_BEGIN_ALLOW_THREADS
2372 result = FindNextFile(hFindFile, &FileData);
2373 Py_END_ALLOW_THREADS
2374 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2375 it got to the end of the directory. */
2376 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2377 Py_DECREF(d);
2378 win32_error("FindNextFile", namebuf);
2379 FindClose(hFindFile);
2380 return NULL;
2381 }
2382 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002383
Victor Stinnerd6f85422010-05-05 23:33:33 +00002384 if (FindClose(hFindFile) == FALSE) {
2385 Py_DECREF(d);
2386 return win32_error("FindClose", namebuf);
2387 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002388
Victor Stinnerd6f85422010-05-05 23:33:33 +00002389 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002390
Tim Peters0bb44a42000-09-15 07:44:49 +00002391#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002392
2393#ifndef MAX_PATH
2394#define MAX_PATH CCHMAXPATH
2395#endif
2396 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002397 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002398 PyObject *d, *v;
2399 char namebuf[MAX_PATH+5];
2400 HDIR hdir = 1;
2401 ULONG srchcnt = 1;
2402 FILEFINDBUF3 ep;
2403 APIRET rc;
2404
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002405 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002406 return NULL;
2407 if (len >= MAX_PATH) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002408 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002409 return NULL;
2410 }
2411 strcpy(namebuf, name);
2412 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002413 if (*pt == ALTSEP)
2414 *pt = SEP;
2415 if (namebuf[len-1] != SEP)
2416 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002417 strcpy(namebuf + len, "*.*");
2418
Victor Stinnerd6f85422010-05-05 23:33:33 +00002419 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002420 return NULL;
2421
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002422 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2423 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002424 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002425 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2426 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2427 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002428
2429 if (rc != NO_ERROR) {
2430 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002431 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002432 }
2433
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002434 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002435 do {
2436 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002437 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002438 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002439
2440 strcpy(namebuf, ep.achName);
2441
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002442 /* Leave Case of Name Alone -- In Native Form */
2443 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002444
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002445 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002446 if (v == NULL) {
2447 Py_DECREF(d);
2448 d = NULL;
2449 break;
2450 }
2451 if (PyList_Append(d, v) != 0) {
2452 Py_DECREF(v);
2453 Py_DECREF(d);
2454 d = NULL;
2455 break;
2456 }
2457 Py_DECREF(v);
2458 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2459 }
2460
2461 return d;
2462#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002463
Victor Stinnerd6f85422010-05-05 23:33:33 +00002464 char *name = NULL;
2465 PyObject *d, *v;
2466 DIR *dirp;
2467 struct dirent *ep;
2468 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002469
Victor Stinnerd6f85422010-05-05 23:33:33 +00002470 errno = 0;
2471 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2472 arg_is_unicode = 0;
2473 PyErr_Clear();
2474 }
2475 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2476 return NULL;
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002477 Py_BEGIN_ALLOW_THREADS
2478 dirp = opendir(name);
2479 Py_END_ALLOW_THREADS
2480 if (dirp == NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002481 return posix_error_with_allocated_filename(name);
2482 }
2483 if ((d = PyList_New(0)) == NULL) {
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002484 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002485 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002486 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002487 PyMem_Free(name);
2488 return NULL;
2489 }
2490 for (;;) {
2491 errno = 0;
2492 Py_BEGIN_ALLOW_THREADS
2493 ep = readdir(dirp);
2494 Py_END_ALLOW_THREADS
2495 if (ep == NULL) {
2496 if (errno == 0) {
2497 break;
2498 } else {
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002499 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002500 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002501 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002502 Py_DECREF(d);
2503 return posix_error_with_allocated_filename(name);
2504 }
2505 }
2506 if (ep->d_name[0] == '.' &&
2507 (NAMLEN(ep) == 1 ||
2508 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2509 continue;
2510 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2511 if (v == NULL) {
2512 Py_DECREF(d);
2513 d = NULL;
2514 break;
2515 }
Just van Rossum46c97842003-02-25 21:42:15 +00002516#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00002517 if (arg_is_unicode) {
2518 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002519
Victor Stinnerd6f85422010-05-05 23:33:33 +00002520 w = PyUnicode_FromEncodedObject(v,
2521 Py_FileSystemDefaultEncoding,
2522 "strict");
2523 if (w != NULL) {
2524 Py_DECREF(v);
2525 v = w;
2526 }
2527 else {
2528 /* fall back to the original byte string, as
2529 discussed in patch #683592 */
2530 PyErr_Clear();
2531 }
2532 }
Just van Rossum46c97842003-02-25 21:42:15 +00002533#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002534 if (PyList_Append(d, v) != 0) {
2535 Py_DECREF(v);
2536 Py_DECREF(d);
2537 d = NULL;
2538 break;
2539 }
2540 Py_DECREF(v);
2541 }
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002542 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002543 closedir(dirp);
Antoine Pitrou247e2fd2010-09-04 17:27:10 +00002544 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002545 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002546
Victor Stinnerd6f85422010-05-05 23:33:33 +00002547 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002548
Tim Peters0bb44a42000-09-15 07:44:49 +00002549#endif /* which OS */
2550} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002551
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002552#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002553/* A helper function for abspath on win32 */
2554static PyObject *
2555posix__getfullpathname(PyObject *self, PyObject *args)
2556{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002557 /* assume encoded strings won't more than double no of chars */
2558 char inbuf[MAX_PATH*2];
2559 char *inbufp = inbuf;
2560 Py_ssize_t insize = sizeof(inbuf);
2561 char outbuf[MAX_PATH*2];
2562 char *temp;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002563
Victor Stinnerd6f85422010-05-05 23:33:33 +00002564 PyUnicodeObject *po;
2565 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2566 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2567 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2568 Py_UNICODE *wtemp;
2569 DWORD result;
2570 PyObject *v;
2571 result = GetFullPathNameW(wpath,
2572 sizeof(woutbuf)/sizeof(woutbuf[0]),
2573 woutbuf, &wtemp);
2574 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2575 woutbufp = malloc(result * sizeof(Py_UNICODE));
2576 if (!woutbufp)
2577 return PyErr_NoMemory();
2578 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2579 }
2580 if (result)
2581 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2582 else
2583 v = win32_error_unicode("GetFullPathNameW", wpath);
2584 if (woutbufp != woutbuf)
2585 free(woutbufp);
2586 return v;
2587 }
2588 /* Drop the argument parsing error as narrow strings
2589 are also valid. */
2590 PyErr_Clear();
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002591
Victor Stinnerd6f85422010-05-05 23:33:33 +00002592 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2593 Py_FileSystemDefaultEncoding, &inbufp,
2594 &insize))
2595 return NULL;
2596 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2597 outbuf, &temp))
2598 return win32_error("GetFullPathName", inbuf);
2599 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2600 return PyUnicode_Decode(outbuf, strlen(outbuf),
2601 Py_FileSystemDefaultEncoding, NULL);
2602 }
2603 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002604} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002605#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002607PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002608"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610
Barry Warsaw53699e91996-12-10 23:23:01 +00002611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002612posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002613{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002614 int res;
2615 char *path = NULL;
2616 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002617
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002618#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002619 PyUnicodeObject *po;
2620 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2621 Py_BEGIN_ALLOW_THREADS
2622 /* PyUnicode_AS_UNICODE OK without thread lock as
2623 it is a simple dereference. */
2624 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2625 Py_END_ALLOW_THREADS
2626 if (!res)
2627 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2628 Py_INCREF(Py_None);
2629 return Py_None;
2630 }
2631 /* Drop the argument parsing error as narrow strings
2632 are also valid. */
2633 PyErr_Clear();
2634 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2635 Py_FileSystemDefaultEncoding, &path, &mode))
2636 return NULL;
2637 Py_BEGIN_ALLOW_THREADS
2638 /* PyUnicode_AS_UNICODE OK without thread lock as
2639 it is a simple dereference. */
2640 res = CreateDirectoryA(path, NULL);
2641 Py_END_ALLOW_THREADS
2642 if (!res) {
2643 win32_error("mkdir", path);
2644 PyMem_Free(path);
2645 return NULL;
2646 }
2647 PyMem_Free(path);
2648 Py_INCREF(Py_None);
2649 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002650#else /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002651
Victor Stinnerd6f85422010-05-05 23:33:33 +00002652 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2653 Py_FileSystemDefaultEncoding, &path, &mode))
2654 return NULL;
2655 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002656#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002657 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002658#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002659 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002660#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002661 Py_END_ALLOW_THREADS
2662 if (res < 0)
2663 return posix_error_with_allocated_filename(path);
2664 PyMem_Free(path);
2665 Py_INCREF(Py_None);
2666 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002667#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002668}
2669
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002670
Neal Norwitz1818ed72006-03-26 00:29:48 +00002671/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2672#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002673#include <sys/resource.h>
2674#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002675
Neal Norwitz1818ed72006-03-26 00:29:48 +00002676
2677#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002679"nice(inc) -> new_priority\n\n\
2680Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002681
Barry Warsaw53699e91996-12-10 23:23:01 +00002682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002683posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002684{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002685 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002686
Victor Stinnerd6f85422010-05-05 23:33:33 +00002687 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2688 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002689
Victor Stinnerd6f85422010-05-05 23:33:33 +00002690 /* There are two flavours of 'nice': one that returns the new
2691 priority (as required by almost all standards out there) and the
2692 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2693 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002694
Victor Stinnerd6f85422010-05-05 23:33:33 +00002695 If we are of the nice family that returns the new priority, we
2696 need to clear errno before the call, and check if errno is filled
2697 before calling posix_error() on a returnvalue of -1, because the
2698 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002699
Victor Stinnerd6f85422010-05-05 23:33:33 +00002700 errno = 0;
2701 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002702#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002703 if (value == 0)
2704 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002705#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002706 if (value == -1 && errno != 0)
2707 /* either nice() or getpriority() returned an error */
2708 return posix_error();
2709 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002710}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002711#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002714"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002715Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Barry Warsaw53699e91996-12-10 23:23:01 +00002717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002718posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002719{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002720#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002721 PyObject *o1, *o2;
2722 char *p1, *p2;
2723 BOOL result;
2724 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2725 goto error;
2726 if (!convert_to_unicode(&o1))
2727 goto error;
2728 if (!convert_to_unicode(&o2)) {
2729 Py_DECREF(o1);
2730 goto error;
2731 }
2732 Py_BEGIN_ALLOW_THREADS
2733 result = MoveFileW(PyUnicode_AsUnicode(o1),
2734 PyUnicode_AsUnicode(o2));
2735 Py_END_ALLOW_THREADS
2736 Py_DECREF(o1);
2737 Py_DECREF(o2);
2738 if (!result)
2739 return win32_error("rename", NULL);
2740 Py_INCREF(Py_None);
2741 return Py_None;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002742error:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002743 PyErr_Clear();
2744 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2745 return NULL;
2746 Py_BEGIN_ALLOW_THREADS
2747 result = MoveFileA(p1, p2);
2748 Py_END_ALLOW_THREADS
2749 if (!result)
2750 return win32_error("rename", NULL);
2751 Py_INCREF(Py_None);
2752 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002753#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002754 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002755#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002756}
2757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002759PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002760"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002761Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Barry Warsaw53699e91996-12-10 23:23:01 +00002763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002764posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002765{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002766#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002767 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002768#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002769 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002770#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002771}
2772
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002775"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002780{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002781#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002782 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002783#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002784 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002785#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002786}
2787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002789#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002791"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002793
Barry Warsaw53699e91996-12-10 23:23:01 +00002794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002795posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002796{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002797 char *command;
2798 long sts;
2799 if (!PyArg_ParseTuple(args, "s:system", &command))
2800 return NULL;
2801 Py_BEGIN_ALLOW_THREADS
2802 sts = system(command);
2803 Py_END_ALLOW_THREADS
2804 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002805}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002806#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002807
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002809PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002810"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002811Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002812
Barry Warsaw53699e91996-12-10 23:23:01 +00002813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002814posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002815{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002816 int i;
2817 if (!PyArg_ParseTuple(args, "i:umask", &i))
2818 return NULL;
2819 i = (int)umask(i);
2820 if (i < 0)
2821 return posix_error();
2822 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002823}
2824
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002827"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002828Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002831"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002832Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002833
Barry Warsaw53699e91996-12-10 23:23:01 +00002834static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002835posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002836{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002837#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002838 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002839#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002840 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002841#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002842}
2843
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002844
Guido van Rossumb6775db1994-08-01 11:34:53 +00002845#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002846PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002847"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002848Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002849
Barry Warsaw53699e91996-12-10 23:23:01 +00002850static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002851posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002852{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002853 struct utsname u;
2854 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002855
Victor Stinnerd6f85422010-05-05 23:33:33 +00002856 Py_BEGIN_ALLOW_THREADS
2857 res = uname(&u);
2858 Py_END_ALLOW_THREADS
2859 if (res < 0)
2860 return posix_error();
2861 return Py_BuildValue("(sssss)",
2862 u.sysname,
2863 u.nodename,
2864 u.release,
2865 u.version,
2866 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002867}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002868#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002869
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002870static int
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002871extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002872{
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002873 time_t intval;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002874 if (PyFloat_Check(t)) {
2875 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002876 PyObject *intobj = PyNumber_Long(t);
Victor Stinnerd6f85422010-05-05 23:33:33 +00002877 if (!intobj)
2878 return -1;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002879#if SIZEOF_TIME_T > SIZEOF_LONG
2880 intval = PyInt_AsUnsignedLongLongMask(intobj);
2881#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002882 intval = PyInt_AsLong(intobj);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002883#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002884 Py_DECREF(intobj);
2885 if (intval == -1 && PyErr_Occurred())
2886 return -1;
2887 *sec = intval;
2888 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2889 if (*usec < 0)
2890 /* If rounding gave us a negative number,
2891 truncate. */
2892 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002893 return 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002894 }
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002895#if SIZEOF_TIME_T > SIZEOF_LONG
2896 intval = PyInt_AsUnsignedLongLongMask(t);
2897#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002898 intval = PyInt_AsLong(t);
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002899#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002900 if (intval == -1 && PyErr_Occurred())
2901 return -1;
2902 *sec = intval;
2903 *usec = 0;
2904 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002907PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002908"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002909utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002910Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002912
Barry Warsaw53699e91996-12-10 23:23:01 +00002913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002914posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002915{
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002916#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002917 PyObject *arg;
2918 PyUnicodeObject *obwpath;
2919 wchar_t *wpath = NULL;
2920 char *apath = NULL;
2921 HANDLE hFile;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002922 time_t atimesec, mtimesec;
2923 long ausec, musec;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002924 FILETIME atime, mtime;
2925 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002926
Victor Stinnerd6f85422010-05-05 23:33:33 +00002927 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2928 wpath = PyUnicode_AS_UNICODE(obwpath);
2929 Py_BEGIN_ALLOW_THREADS
2930 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2931 NULL, OPEN_EXISTING,
2932 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2933 Py_END_ALLOW_THREADS
2934 if (hFile == INVALID_HANDLE_VALUE)
2935 return win32_error_unicode("utime", wpath);
2936 } else
2937 /* Drop the argument parsing error as narrow strings
2938 are also valid. */
2939 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002940
Victor Stinnerd6f85422010-05-05 23:33:33 +00002941 if (!wpath) {
2942 if (!PyArg_ParseTuple(args, "etO:utime",
2943 Py_FileSystemDefaultEncoding, &apath, &arg))
2944 return NULL;
2945 Py_BEGIN_ALLOW_THREADS
2946 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2947 NULL, OPEN_EXISTING,
2948 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2949 Py_END_ALLOW_THREADS
2950 if (hFile == INVALID_HANDLE_VALUE) {
2951 win32_error("utime", apath);
2952 PyMem_Free(apath);
2953 return NULL;
2954 }
2955 PyMem_Free(apath);
2956 }
2957
2958 if (arg == Py_None) {
2959 SYSTEMTIME now;
2960 GetSystemTime(&now);
2961 if (!SystemTimeToFileTime(&now, &mtime) ||
2962 !SystemTimeToFileTime(&now, &atime)) {
2963 win32_error("utime", NULL);
2964 goto done;
Stefan Krah93f7a322010-11-26 17:35:50 +00002965 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00002966 }
2967 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2968 PyErr_SetString(PyExc_TypeError,
2969 "utime() arg 2 must be a tuple (atime, mtime)");
2970 goto done;
2971 }
2972 else {
2973 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2974 &atimesec, &ausec) == -1)
2975 goto done;
2976 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2977 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2978 &mtimesec, &musec) == -1)
2979 goto done;
2980 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2981 }
2982 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2983 /* Avoid putting the file name into the error here,
2984 as that may confuse the user into believing that
2985 something is wrong with the file, when it also
2986 could be the time stamp that gives a problem. */
2987 win32_error("utime", NULL);
Antoine Pitrou1f1613f2011-01-06 18:30:26 +00002988 goto done;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002989 }
2990 Py_INCREF(Py_None);
2991 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002992done:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002993 CloseHandle(hFile);
2994 return result;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002995#else /* MS_WINDOWS */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002996
Victor Stinnerd6f85422010-05-05 23:33:33 +00002997 char *path = NULL;
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +00002998 time_t atime, mtime;
2999 long ausec, musec;
Victor Stinnerd6f85422010-05-05 23:33:33 +00003000 int res;
3001 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003002
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003003#if defined(HAVE_UTIMES)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003004 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003005#define ATIME buf[0].tv_sec
3006#define MTIME buf[1].tv_sec
3007#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003008/* XXX should define struct utimbuf instead, above */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003009 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003010#define ATIME buf.actime
3011#define MTIME buf.modtime
3012#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003013#else /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003014 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003015#define ATIME buf[0]
3016#define MTIME buf[1]
3017#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003018#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003019
Mark Hammond817c9292003-12-03 01:22:38 +00003020
Victor Stinnerd6f85422010-05-05 23:33:33 +00003021 if (!PyArg_ParseTuple(args, "etO:utime",
3022 Py_FileSystemDefaultEncoding, &path, &arg))
3023 return NULL;
3024 if (arg == Py_None) {
3025 /* optional time values not given */
3026 Py_BEGIN_ALLOW_THREADS
3027 res = utime(path, NULL);
3028 Py_END_ALLOW_THREADS
3029 }
3030 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3031 PyErr_SetString(PyExc_TypeError,
3032 "utime() arg 2 must be a tuple (atime, mtime)");
3033 PyMem_Free(path);
3034 return NULL;
3035 }
3036 else {
3037 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3038 &atime, &ausec) == -1) {
3039 PyMem_Free(path);
3040 return NULL;
3041 }
3042 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3043 &mtime, &musec) == -1) {
3044 PyMem_Free(path);
3045 return NULL;
3046 }
3047 ATIME = atime;
3048 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003049#ifdef HAVE_UTIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00003050 buf[0].tv_usec = ausec;
3051 buf[1].tv_usec = musec;
3052 Py_BEGIN_ALLOW_THREADS
3053 res = utimes(path, buf);
3054 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003055#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003056 Py_BEGIN_ALLOW_THREADS
3057 res = utime(path, UTIME_ARG);
3058 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003059#endif /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003060 }
3061 if (res < 0) {
3062 return posix_error_with_allocated_filename(path);
3063 }
3064 PyMem_Free(path);
3065 Py_INCREF(Py_None);
3066 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003067#undef UTIME_ARG
3068#undef ATIME
3069#undef MTIME
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00003070#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003071}
3072
Guido van Rossum85e3b011991-06-03 12:42:10 +00003073
Guido van Rossum3b066191991-06-04 19:40:25 +00003074/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003076PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003077"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003078Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003079
Barry Warsaw53699e91996-12-10 23:23:01 +00003080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003081posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003082{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003083 int sts;
3084 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3085 return NULL;
3086 _exit(sts);
3087 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003088}
3089
Martin v. Löwis114619e2002-10-07 06:44:21 +00003090#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3091static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003092free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003093{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003094 Py_ssize_t i;
3095 for (i = 0; i < count; i++)
3096 PyMem_Free(array[i]);
3097 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003098}
3099#endif
3100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003101
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003102#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003103PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003104"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003105Execute an executable path with arguments, replacing current process.\n\
3106\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003107 path: path of executable file\n\
3108 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003109
Barry Warsaw53699e91996-12-10 23:23:01 +00003110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003111posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003112{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003113 char *path;
3114 PyObject *argv;
3115 char **argvlist;
3116 Py_ssize_t i, argc;
3117 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003118
Victor Stinnerd6f85422010-05-05 23:33:33 +00003119 /* execv has two arguments: (path, argv), where
3120 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003121
Victor Stinnerd6f85422010-05-05 23:33:33 +00003122 if (!PyArg_ParseTuple(args, "etO:execv",
3123 Py_FileSystemDefaultEncoding,
3124 &path, &argv))
3125 return NULL;
3126 if (PyList_Check(argv)) {
3127 argc = PyList_Size(argv);
3128 getitem = PyList_GetItem;
3129 }
3130 else if (PyTuple_Check(argv)) {
3131 argc = PyTuple_Size(argv);
3132 getitem = PyTuple_GetItem;
3133 }
3134 else {
3135 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3136 PyMem_Free(path);
3137 return NULL;
3138 }
3139 if (argc < 1) {
3140 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3141 PyMem_Free(path);
3142 return NULL;
3143 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003144
Victor Stinnerd6f85422010-05-05 23:33:33 +00003145 argvlist = PyMem_NEW(char *, argc+1);
3146 if (argvlist == NULL) {
3147 PyMem_Free(path);
3148 return PyErr_NoMemory();
3149 }
3150 for (i = 0; i < argc; i++) {
3151 if (!PyArg_Parse((*getitem)(argv, i), "et",
3152 Py_FileSystemDefaultEncoding,
3153 &argvlist[i])) {
3154 free_string_array(argvlist, i);
3155 PyErr_SetString(PyExc_TypeError,
3156 "execv() arg 2 must contain only strings");
3157 PyMem_Free(path);
3158 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003159
Victor Stinnerd6f85422010-05-05 23:33:33 +00003160 }
3161 }
3162 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003163
Victor Stinnerd6f85422010-05-05 23:33:33 +00003164 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003165
Victor Stinnerd6f85422010-05-05 23:33:33 +00003166 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003167
Victor Stinnerd6f85422010-05-05 23:33:33 +00003168 free_string_array(argvlist, argc);
3169 PyMem_Free(path);
3170 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003171}
3172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003174PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003175"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003176Execute a path with arguments and environment, replacing current process.\n\
3177\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003178 path: path of executable file\n\
3179 args: tuple or list of arguments\n\
3180 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181
Barry Warsaw53699e91996-12-10 23:23:01 +00003182static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003183posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003184{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003185 char *path;
3186 PyObject *argv, *env;
3187 char **argvlist;
3188 char **envlist;
3189 PyObject *key, *val, *keys=NULL, *vals=NULL;
3190 Py_ssize_t i, pos, argc, envc;
3191 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3192 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003193
Victor Stinnerd6f85422010-05-05 23:33:33 +00003194 /* execve has three arguments: (path, argv, env), where
3195 argv is a list or tuple of strings and env is a dictionary
3196 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003197
Victor Stinnerd6f85422010-05-05 23:33:33 +00003198 if (!PyArg_ParseTuple(args, "etOO:execve",
3199 Py_FileSystemDefaultEncoding,
3200 &path, &argv, &env))
3201 return NULL;
3202 if (PyList_Check(argv)) {
3203 argc = PyList_Size(argv);
3204 getitem = PyList_GetItem;
3205 }
3206 else if (PyTuple_Check(argv)) {
3207 argc = PyTuple_Size(argv);
3208 getitem = PyTuple_GetItem;
3209 }
3210 else {
3211 PyErr_SetString(PyExc_TypeError,
3212 "execve() arg 2 must be a tuple or list");
3213 goto fail_0;
3214 }
3215 if (!PyMapping_Check(env)) {
3216 PyErr_SetString(PyExc_TypeError,
3217 "execve() arg 3 must be a mapping object");
3218 goto fail_0;
3219 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003220
Victor Stinnerd6f85422010-05-05 23:33:33 +00003221 argvlist = PyMem_NEW(char *, argc+1);
3222 if (argvlist == NULL) {
3223 PyErr_NoMemory();
3224 goto fail_0;
3225 }
3226 for (i = 0; i < argc; i++) {
3227 if (!PyArg_Parse((*getitem)(argv, i),
3228 "et;execve() arg 2 must contain only strings",
3229 Py_FileSystemDefaultEncoding,
3230 &argvlist[i]))
3231 {
3232 lastarg = i;
3233 goto fail_1;
3234 }
3235 }
3236 lastarg = argc;
3237 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003238
Victor Stinnerd6f85422010-05-05 23:33:33 +00003239 i = PyMapping_Size(env);
3240 if (i < 0)
3241 goto fail_1;
3242 envlist = PyMem_NEW(char *, i + 1);
3243 if (envlist == NULL) {
3244 PyErr_NoMemory();
3245 goto fail_1;
3246 }
3247 envc = 0;
3248 keys = PyMapping_Keys(env);
3249 vals = PyMapping_Values(env);
3250 if (!keys || !vals)
3251 goto fail_2;
3252 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3253 PyErr_SetString(PyExc_TypeError,
3254 "execve(): env.keys() or env.values() is not a list");
3255 goto fail_2;
3256 }
Tim Peters5aa91602002-01-30 05:46:57 +00003257
Victor Stinnerd6f85422010-05-05 23:33:33 +00003258 for (pos = 0; pos < i; pos++) {
3259 char *p, *k, *v;
3260 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003261
Victor Stinnerd6f85422010-05-05 23:33:33 +00003262 key = PyList_GetItem(keys, pos);
3263 val = PyList_GetItem(vals, pos);
3264 if (!key || !val)
3265 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003266
Victor Stinnerd6f85422010-05-05 23:33:33 +00003267 if (!PyArg_Parse(
3268 key,
3269 "s;execve() arg 3 contains a non-string key",
3270 &k) ||
3271 !PyArg_Parse(
3272 val,
3273 "s;execve() arg 3 contains a non-string value",
3274 &v))
3275 {
3276 goto fail_2;
3277 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003278
3279#if defined(PYOS_OS2)
3280 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3281 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3282#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003283 len = PyString_Size(key) + PyString_Size(val) + 2;
3284 p = PyMem_NEW(char, len);
3285 if (p == NULL) {
3286 PyErr_NoMemory();
3287 goto fail_2;
3288 }
3289 PyOS_snprintf(p, len, "%s=%s", k, v);
3290 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003291#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003292 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003293#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003294 }
3295 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003296
Victor Stinnerd6f85422010-05-05 23:33:33 +00003297 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003298
Victor Stinnerd6f85422010-05-05 23:33:33 +00003299 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003300
Victor Stinnerd6f85422010-05-05 23:33:33 +00003301 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003302
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003303 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003304 while (--envc >= 0)
3305 PyMem_DEL(envlist[envc]);
3306 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003307 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003308 free_string_array(argvlist, lastarg);
3309 Py_XDECREF(vals);
3310 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003311 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003312 PyMem_Free(path);
3313 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003314}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003315#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003316
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003317
Guido van Rossuma1065681999-01-25 23:20:23 +00003318#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003319PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003320"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003321Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003322\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003323 mode: mode of process creation\n\
3324 path: path of executable file\n\
3325 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003326
3327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003328posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003329{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003330 char *path;
3331 PyObject *argv;
3332 char **argvlist;
3333 int mode, i;
3334 Py_ssize_t argc;
3335 Py_intptr_t spawnval;
3336 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003337
Victor Stinnerd6f85422010-05-05 23:33:33 +00003338 /* spawnv has three arguments: (mode, path, argv), where
3339 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003340
Victor Stinnerd6f85422010-05-05 23:33:33 +00003341 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3342 Py_FileSystemDefaultEncoding,
3343 &path, &argv))
3344 return NULL;
3345 if (PyList_Check(argv)) {
3346 argc = PyList_Size(argv);
3347 getitem = PyList_GetItem;
3348 }
3349 else if (PyTuple_Check(argv)) {
3350 argc = PyTuple_Size(argv);
3351 getitem = PyTuple_GetItem;
3352 }
3353 else {
3354 PyErr_SetString(PyExc_TypeError,
3355 "spawnv() arg 2 must be a tuple or list");
3356 PyMem_Free(path);
3357 return NULL;
3358 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003359
Victor Stinnerd6f85422010-05-05 23:33:33 +00003360 argvlist = PyMem_NEW(char *, argc+1);
3361 if (argvlist == NULL) {
3362 PyMem_Free(path);
3363 return PyErr_NoMemory();
3364 }
3365 for (i = 0; i < argc; i++) {
3366 if (!PyArg_Parse((*getitem)(argv, i), "et",
3367 Py_FileSystemDefaultEncoding,
3368 &argvlist[i])) {
3369 free_string_array(argvlist, i);
3370 PyErr_SetString(
3371 PyExc_TypeError,
3372 "spawnv() arg 2 must contain only strings");
3373 PyMem_Free(path);
3374 return NULL;
3375 }
3376 }
3377 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003378
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003379#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003380 Py_BEGIN_ALLOW_THREADS
3381 spawnval = spawnv(mode, path, argvlist);
3382 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003383#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003384 if (mode == _OLD_P_OVERLAY)
3385 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003386
Victor Stinnerd6f85422010-05-05 23:33:33 +00003387 Py_BEGIN_ALLOW_THREADS
3388 spawnval = _spawnv(mode, path, argvlist);
3389 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003390#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003391
Victor Stinnerd6f85422010-05-05 23:33:33 +00003392 free_string_array(argvlist, argc);
3393 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003394
Victor Stinnerd6f85422010-05-05 23:33:33 +00003395 if (spawnval == -1)
3396 return posix_error();
3397 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003398#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003399 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003400#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003401 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003402#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003403}
3404
3405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003406PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003407"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003408Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003409\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003410 mode: mode of process creation\n\
3411 path: path of executable file\n\
3412 args: tuple or list of arguments\n\
3413 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003414
3415static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003416posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003417{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003418 char *path;
3419 PyObject *argv, *env;
3420 char **argvlist;
3421 char **envlist;
3422 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3423 int mode, pos, envc;
3424 Py_ssize_t argc, i;
3425 Py_intptr_t spawnval;
3426 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3427 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003428
Victor Stinnerd6f85422010-05-05 23:33:33 +00003429 /* spawnve has four arguments: (mode, path, argv, env), where
3430 argv is a list or tuple of strings and env is a dictionary
3431 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003432
Victor Stinnerd6f85422010-05-05 23:33:33 +00003433 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3434 Py_FileSystemDefaultEncoding,
3435 &path, &argv, &env))
3436 return NULL;
3437 if (PyList_Check(argv)) {
3438 argc = PyList_Size(argv);
3439 getitem = PyList_GetItem;
3440 }
3441 else if (PyTuple_Check(argv)) {
3442 argc = PyTuple_Size(argv);
3443 getitem = PyTuple_GetItem;
3444 }
3445 else {
3446 PyErr_SetString(PyExc_TypeError,
3447 "spawnve() arg 2 must be a tuple or list");
3448 goto fail_0;
3449 }
3450 if (!PyMapping_Check(env)) {
3451 PyErr_SetString(PyExc_TypeError,
3452 "spawnve() arg 3 must be a mapping object");
3453 goto fail_0;
3454 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003455
Victor Stinnerd6f85422010-05-05 23:33:33 +00003456 argvlist = PyMem_NEW(char *, argc+1);
3457 if (argvlist == NULL) {
3458 PyErr_NoMemory();
3459 goto fail_0;
3460 }
3461 for (i = 0; i < argc; i++) {
3462 if (!PyArg_Parse((*getitem)(argv, i),
3463 "et;spawnve() arg 2 must contain only strings",
3464 Py_FileSystemDefaultEncoding,
3465 &argvlist[i]))
3466 {
3467 lastarg = i;
3468 goto fail_1;
3469 }
3470 }
3471 lastarg = argc;
3472 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003473
Victor Stinnerd6f85422010-05-05 23:33:33 +00003474 i = PyMapping_Size(env);
3475 if (i < 0)
3476 goto fail_1;
3477 envlist = PyMem_NEW(char *, i + 1);
3478 if (envlist == NULL) {
3479 PyErr_NoMemory();
3480 goto fail_1;
3481 }
3482 envc = 0;
3483 keys = PyMapping_Keys(env);
3484 vals = PyMapping_Values(env);
3485 if (!keys || !vals)
3486 goto fail_2;
3487 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3488 PyErr_SetString(PyExc_TypeError,
3489 "spawnve(): env.keys() or env.values() is not a list");
3490 goto fail_2;
3491 }
Tim Peters5aa91602002-01-30 05:46:57 +00003492
Victor Stinnerd6f85422010-05-05 23:33:33 +00003493 for (pos = 0; pos < i; pos++) {
3494 char *p, *k, *v;
3495 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003496
Victor Stinnerd6f85422010-05-05 23:33:33 +00003497 key = PyList_GetItem(keys, pos);
3498 val = PyList_GetItem(vals, pos);
3499 if (!key || !val)
3500 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003501
Victor Stinnerd6f85422010-05-05 23:33:33 +00003502 if (!PyArg_Parse(
3503 key,
3504 "s;spawnve() arg 3 contains a non-string key",
3505 &k) ||
3506 !PyArg_Parse(
3507 val,
3508 "s;spawnve() arg 3 contains a non-string value",
3509 &v))
3510 {
3511 goto fail_2;
3512 }
3513 len = PyString_Size(key) + PyString_Size(val) + 2;
3514 p = PyMem_NEW(char, len);
3515 if (p == NULL) {
3516 PyErr_NoMemory();
3517 goto fail_2;
3518 }
3519 PyOS_snprintf(p, len, "%s=%s", k, v);
3520 envlist[envc++] = p;
3521 }
3522 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003523
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003524#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003525 Py_BEGIN_ALLOW_THREADS
3526 spawnval = spawnve(mode, path, argvlist, envlist);
3527 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003528#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003529 if (mode == _OLD_P_OVERLAY)
3530 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003531
Victor Stinnerd6f85422010-05-05 23:33:33 +00003532 Py_BEGIN_ALLOW_THREADS
3533 spawnval = _spawnve(mode, path, argvlist, envlist);
3534 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003535#endif
Tim Peters25059d32001-12-07 20:35:43 +00003536
Victor Stinnerd6f85422010-05-05 23:33:33 +00003537 if (spawnval == -1)
3538 (void) posix_error();
3539 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003540#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003541 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003542#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003543 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003544#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003545
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003546 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003547 while (--envc >= 0)
3548 PyMem_DEL(envlist[envc]);
3549 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003550 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003551 free_string_array(argvlist, lastarg);
3552 Py_XDECREF(vals);
3553 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003554 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003555 PyMem_Free(path);
3556 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003557}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003558
3559/* OS/2 supports spawnvp & spawnvpe natively */
3560#if defined(PYOS_OS2)
3561PyDoc_STRVAR(posix_spawnvp__doc__,
3562"spawnvp(mode, file, args)\n\n\
3563Execute the program 'file' in a new process, using the environment\n\
3564search path to find the file.\n\
3565\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003566 mode: mode of process creation\n\
3567 file: executable file name\n\
3568 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003569
3570static PyObject *
3571posix_spawnvp(PyObject *self, PyObject *args)
3572{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003573 char *path;
3574 PyObject *argv;
3575 char **argvlist;
3576 int mode, i, argc;
3577 Py_intptr_t spawnval;
3578 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003579
Victor Stinnerd6f85422010-05-05 23:33:33 +00003580 /* spawnvp has three arguments: (mode, path, argv), where
3581 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003582
Victor Stinnerd6f85422010-05-05 23:33:33 +00003583 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3584 Py_FileSystemDefaultEncoding,
3585 &path, &argv))
3586 return NULL;
3587 if (PyList_Check(argv)) {
3588 argc = PyList_Size(argv);
3589 getitem = PyList_GetItem;
3590 }
3591 else if (PyTuple_Check(argv)) {
3592 argc = PyTuple_Size(argv);
3593 getitem = PyTuple_GetItem;
3594 }
3595 else {
3596 PyErr_SetString(PyExc_TypeError,
3597 "spawnvp() arg 2 must be a tuple or list");
3598 PyMem_Free(path);
3599 return NULL;
3600 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003601
Victor Stinnerd6f85422010-05-05 23:33:33 +00003602 argvlist = PyMem_NEW(char *, argc+1);
3603 if (argvlist == NULL) {
3604 PyMem_Free(path);
3605 return PyErr_NoMemory();
3606 }
3607 for (i = 0; i < argc; i++) {
3608 if (!PyArg_Parse((*getitem)(argv, i), "et",
3609 Py_FileSystemDefaultEncoding,
3610 &argvlist[i])) {
3611 free_string_array(argvlist, i);
3612 PyErr_SetString(
3613 PyExc_TypeError,
3614 "spawnvp() arg 2 must contain only strings");
3615 PyMem_Free(path);
3616 return NULL;
3617 }
3618 }
3619 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003620
Victor Stinnerd6f85422010-05-05 23:33:33 +00003621 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003622#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003623 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003624#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003625 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003626#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003627 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003628
Victor Stinnerd6f85422010-05-05 23:33:33 +00003629 free_string_array(argvlist, argc);
3630 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003631
Victor Stinnerd6f85422010-05-05 23:33:33 +00003632 if (spawnval == -1)
3633 return posix_error();
3634 else
3635 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003636}
3637
3638
3639PyDoc_STRVAR(posix_spawnvpe__doc__,
3640"spawnvpe(mode, file, args, env)\n\n\
3641Execute the program 'file' in a new process, using the environment\n\
3642search path to find the file.\n\
3643\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003644 mode: mode of process creation\n\
3645 file: executable file name\n\
3646 args: tuple or list of arguments\n\
3647 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003648
3649static PyObject *
3650posix_spawnvpe(PyObject *self, PyObject *args)
3651{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003652 char *path;
3653 PyObject *argv, *env;
3654 char **argvlist;
3655 char **envlist;
3656 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3657 int mode, i, pos, argc, envc;
3658 Py_intptr_t spawnval;
3659 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3660 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003661
Victor Stinnerd6f85422010-05-05 23:33:33 +00003662 /* spawnvpe has four arguments: (mode, path, argv, env), where
3663 argv is a list or tuple of strings and env is a dictionary
3664 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003665
Victor Stinnerd6f85422010-05-05 23:33:33 +00003666 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3667 Py_FileSystemDefaultEncoding,
3668 &path, &argv, &env))
3669 return NULL;
3670 if (PyList_Check(argv)) {
3671 argc = PyList_Size(argv);
3672 getitem = PyList_GetItem;
3673 }
3674 else if (PyTuple_Check(argv)) {
3675 argc = PyTuple_Size(argv);
3676 getitem = PyTuple_GetItem;
3677 }
3678 else {
3679 PyErr_SetString(PyExc_TypeError,
3680 "spawnvpe() arg 2 must be a tuple or list");
3681 goto fail_0;
3682 }
3683 if (!PyMapping_Check(env)) {
3684 PyErr_SetString(PyExc_TypeError,
3685 "spawnvpe() arg 3 must be a mapping object");
3686 goto fail_0;
3687 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003688
Victor Stinnerd6f85422010-05-05 23:33:33 +00003689 argvlist = PyMem_NEW(char *, argc+1);
3690 if (argvlist == NULL) {
3691 PyErr_NoMemory();
3692 goto fail_0;
3693 }
3694 for (i = 0; i < argc; i++) {
3695 if (!PyArg_Parse((*getitem)(argv, i),
3696 "et;spawnvpe() arg 2 must contain only strings",
3697 Py_FileSystemDefaultEncoding,
3698 &argvlist[i]))
3699 {
3700 lastarg = i;
3701 goto fail_1;
3702 }
3703 }
3704 lastarg = argc;
3705 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003706
Victor Stinnerd6f85422010-05-05 23:33:33 +00003707 i = PyMapping_Size(env);
3708 if (i < 0)
3709 goto fail_1;
3710 envlist = PyMem_NEW(char *, i + 1);
3711 if (envlist == NULL) {
3712 PyErr_NoMemory();
3713 goto fail_1;
3714 }
3715 envc = 0;
3716 keys = PyMapping_Keys(env);
3717 vals = PyMapping_Values(env);
3718 if (!keys || !vals)
3719 goto fail_2;
3720 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3721 PyErr_SetString(PyExc_TypeError,
3722 "spawnvpe(): env.keys() or env.values() is not a list");
3723 goto fail_2;
3724 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003725
Victor Stinnerd6f85422010-05-05 23:33:33 +00003726 for (pos = 0; pos < i; pos++) {
3727 char *p, *k, *v;
3728 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003729
Victor Stinnerd6f85422010-05-05 23:33:33 +00003730 key = PyList_GetItem(keys, pos);
3731 val = PyList_GetItem(vals, pos);
3732 if (!key || !val)
3733 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003734
Victor Stinnerd6f85422010-05-05 23:33:33 +00003735 if (!PyArg_Parse(
3736 key,
3737 "s;spawnvpe() arg 3 contains a non-string key",
3738 &k) ||
3739 !PyArg_Parse(
3740 val,
3741 "s;spawnvpe() arg 3 contains a non-string value",
3742 &v))
3743 {
3744 goto fail_2;
3745 }
3746 len = PyString_Size(key) + PyString_Size(val) + 2;
3747 p = PyMem_NEW(char, len);
3748 if (p == NULL) {
3749 PyErr_NoMemory();
3750 goto fail_2;
3751 }
3752 PyOS_snprintf(p, len, "%s=%s", k, v);
3753 envlist[envc++] = p;
3754 }
3755 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003756
Victor Stinnerd6f85422010-05-05 23:33:33 +00003757 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003758#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003759 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003760#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003761 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003762#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003763 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003764
Victor Stinnerd6f85422010-05-05 23:33:33 +00003765 if (spawnval == -1)
3766 (void) posix_error();
3767 else
3768 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003769
3770 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003771 while (--envc >= 0)
3772 PyMem_DEL(envlist[envc]);
3773 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003774 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003775 free_string_array(argvlist, lastarg);
3776 Py_XDECREF(vals);
3777 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003778 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003779 PyMem_Free(path);
3780 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003781}
3782#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003783#endif /* HAVE_SPAWNV */
3784
3785
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003786#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003789Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3790\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003792
3793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003794posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003795{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003796 pid_t pid;
3797 int result = 0;
3798 _PyImport_AcquireLock();
3799 pid = fork1();
3800 if (pid == 0) {
3801 /* child: this clobbers and resets the import lock. */
3802 PyOS_AfterFork();
3803 } else {
3804 /* parent: release the import lock. */
3805 result = _PyImport_ReleaseLock();
3806 }
3807 if (pid == -1)
3808 return posix_error();
3809 if (result < 0) {
3810 /* Don't clobber the OSError if the fork failed. */
3811 PyErr_SetString(PyExc_RuntimeError,
3812 "not holding the import lock");
3813 return NULL;
3814 }
3815 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003816}
3817#endif
3818
3819
Guido van Rossumad0ee831995-03-01 10:34:45 +00003820#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003821PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003822"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003823Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003825
Barry Warsaw53699e91996-12-10 23:23:01 +00003826static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003827posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003828{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003829 pid_t pid;
3830 int result = 0;
3831 _PyImport_AcquireLock();
3832 pid = fork();
3833 if (pid == 0) {
3834 /* child: this clobbers and resets the import lock. */
3835 PyOS_AfterFork();
3836 } else {
3837 /* parent: release the import lock. */
3838 result = _PyImport_ReleaseLock();
3839 }
3840 if (pid == -1)
3841 return posix_error();
3842 if (result < 0) {
3843 /* Don't clobber the OSError if the fork failed. */
3844 PyErr_SetString(PyExc_RuntimeError,
3845 "not holding the import lock");
3846 return NULL;
3847 }
3848 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003849}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003850#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003851
Neal Norwitzb59798b2003-03-21 01:43:31 +00003852/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003853/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3854#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003855#define DEV_PTY_FILE "/dev/ptc"
3856#define HAVE_DEV_PTMX
3857#else
3858#define DEV_PTY_FILE "/dev/ptmx"
3859#endif
3860
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003861#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003862#ifdef HAVE_PTY_H
3863#include <pty.h>
3864#else
3865#ifdef HAVE_LIBUTIL_H
3866#include <libutil.h>
Ronald Oussorena55af9a2010-01-17 16:25:57 +00003867#else
3868#ifdef HAVE_UTIL_H
3869#include <util.h>
3870#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003871#endif /* HAVE_LIBUTIL_H */
3872#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003873#ifdef HAVE_STROPTS_H
3874#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003875#endif
3876#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003877
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003878#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003879PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003880"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003881Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003882
3883static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003884posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003885{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003886 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003887#ifndef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003888 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003889#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003890#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003891 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003892#ifdef sun
Victor Stinnerd6f85422010-05-05 23:33:33 +00003893 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003894#endif
3895#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003896
Thomas Wouters70c21a12000-07-14 14:28:33 +00003897#ifdef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003898 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3899 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003900#elif defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003901 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3902 if (slave_name == NULL)
3903 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003904
Victor Stinnerd6f85422010-05-05 23:33:33 +00003905 slave_fd = open(slave_name, O_RDWR);
3906 if (slave_fd < 0)
3907 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003908#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003909 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3910 if (master_fd < 0)
3911 return posix_error();
3912 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3913 /* change permission of slave */
3914 if (grantpt(master_fd) < 0) {
3915 PyOS_setsig(SIGCHLD, sig_saved);
3916 return posix_error();
3917 }
3918 /* unlock slave */
3919 if (unlockpt(master_fd) < 0) {
3920 PyOS_setsig(SIGCHLD, sig_saved);
3921 return posix_error();
3922 }
3923 PyOS_setsig(SIGCHLD, sig_saved);
3924 slave_name = ptsname(master_fd); /* get name of slave */
3925 if (slave_name == NULL)
3926 return posix_error();
3927 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3928 if (slave_fd < 0)
3929 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003930#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003931 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3932 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003933#ifndef __hpux
Victor Stinnerd6f85422010-05-05 23:33:33 +00003934 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003935#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003936#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003937#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003938
Victor Stinnerd6f85422010-05-05 23:33:33 +00003939 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003940
Fred Drake8cef4cf2000-06-28 16:40:38 +00003941}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003942#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003943
3944#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003945PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003946"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003947Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3948Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003949To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003950
3951static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003952posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003953{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003954 int master_fd = -1, result = 0;
3955 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003956
Victor Stinnerd6f85422010-05-05 23:33:33 +00003957 _PyImport_AcquireLock();
3958 pid = forkpty(&master_fd, NULL, NULL, NULL);
3959 if (pid == 0) {
3960 /* child: this clobbers and resets the import lock. */
3961 PyOS_AfterFork();
3962 } else {
3963 /* parent: release the import lock. */
3964 result = _PyImport_ReleaseLock();
3965 }
3966 if (pid == -1)
3967 return posix_error();
3968 if (result < 0) {
3969 /* Don't clobber the OSError if the fork failed. */
3970 PyErr_SetString(PyExc_RuntimeError,
3971 "not holding the import lock");
3972 return NULL;
3973 }
3974 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003975}
3976#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003977
Guido van Rossumad0ee831995-03-01 10:34:45 +00003978#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003979PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003980"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003981Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003982
Barry Warsaw53699e91996-12-10 23:23:01 +00003983static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003984posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003985{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02003986 return _PyInt_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003987}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003988#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003989
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003990
Guido van Rossumad0ee831995-03-01 10:34:45 +00003991#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003992PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003993"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003994Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003995
Barry Warsaw53699e91996-12-10 23:23:01 +00003996static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003997posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003998{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02003999 return _PyInt_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004000}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004001#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004003
Guido van Rossumad0ee831995-03-01 10:34:45 +00004004#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004006"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004007Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004008
Barry Warsaw53699e91996-12-10 23:23:01 +00004009static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004010posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004011{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004012 return _PyInt_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004013}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004014#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004018"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004019Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004020
Barry Warsaw53699e91996-12-10 23:23:01 +00004021static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004022posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004023{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004024 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004025}
4026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004027
Fred Drakec9680921999-12-13 16:37:25 +00004028#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004029PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004030"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004031Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004032
4033static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004034posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004035{
4036 PyObject *result = NULL;
4037
Fred Drakec9680921999-12-13 16:37:25 +00004038#ifdef NGROUPS_MAX
4039#define MAX_GROUPS NGROUPS_MAX
4040#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004041 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004042#define MAX_GROUPS 64
4043#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004044 gid_t grouplist[MAX_GROUPS];
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004045
Victor Stinner59729ff2011-07-05 11:28:19 +02004046 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004047 * This is a helper variable to store the intermediate result when
4048 * that happens.
4049 *
4050 * To keep the code readable the OSX behaviour is unconditional,
4051 * according to the POSIX spec this should be safe on all unix-y
4052 * systems.
4053 */
4054 gid_t* alt_grouplist = grouplist;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004055 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004056
Victor Stinnerd6f85422010-05-05 23:33:33 +00004057 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004058 if (n < 0) {
4059 if (errno == EINVAL) {
4060 n = getgroups(0, NULL);
4061 if (n == -1) {
4062 return posix_error();
4063 }
4064 if (n == 0) {
4065 /* Avoid malloc(0) */
4066 alt_grouplist = grouplist;
4067 } else {
4068 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4069 if (alt_grouplist == NULL) {
4070 errno = EINVAL;
4071 return posix_error();
4072 }
4073 n = getgroups(n, alt_grouplist);
4074 if (n == -1) {
4075 PyMem_Free(alt_grouplist);
4076 return posix_error();
4077 }
4078 }
4079 } else {
4080 return posix_error();
4081 }
4082 }
4083 result = PyList_New(n);
4084 if (result != NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00004085 int i;
4086 for (i = 0; i < n; ++i) {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004087 PyObject *o = _PyInt_FromGid(alt_grouplist[i]);
Victor Stinnerd6f85422010-05-05 23:33:33 +00004088 if (o == NULL) {
Stefan Krah93f7a322010-11-26 17:35:50 +00004089 Py_DECREF(result);
4090 result = NULL;
4091 break;
Fred Drakec9680921999-12-13 16:37:25 +00004092 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00004093 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004094 }
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00004095 }
4096
4097 if (alt_grouplist != grouplist) {
4098 PyMem_Free(alt_grouplist);
Victor Stinnerd6f85422010-05-05 23:33:33 +00004099 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004100
Fred Drakec9680921999-12-13 16:37:25 +00004101 return result;
4102}
4103#endif
4104
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004105#ifdef HAVE_INITGROUPS
4106PyDoc_STRVAR(posix_initgroups__doc__,
4107"initgroups(username, gid) -> None\n\n\
4108Call the system initgroups() to initialize the group access list with all of\n\
4109the groups of which the specified username is a member, plus the specified\n\
4110group id.");
4111
4112static PyObject *
4113posix_initgroups(PyObject *self, PyObject *args)
4114{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004115 char *username;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004116#ifdef __APPLE__
4117 int gid;
4118#else
4119 gid_t gid;
4120#endif
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004121
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004122#ifdef __APPLE__
4123 if (!PyArg_ParseTuple(args, "si:initgroups", &username,
4124 &gid))
4125#else
4126 if (!PyArg_ParseTuple(args, "sO&:initgroups", &username,
4127 _Py_Gid_Converter, &gid))
4128#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004129 return NULL;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004130
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004131 if (initgroups(username, gid) == -1)
Victor Stinnerd6f85422010-05-05 23:33:33 +00004132 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004133
Victor Stinnerd6f85422010-05-05 23:33:33 +00004134 Py_INCREF(Py_None);
4135 return Py_None;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00004136}
4137#endif
4138
Martin v. Löwis606edc12002-06-13 21:09:11 +00004139#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004140PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004141"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004142Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004143
4144static PyObject *
4145posix_getpgid(PyObject *self, PyObject *args)
4146{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004147 pid_t pid, pgid;
4148 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
4149 return NULL;
4150 pgid = getpgid(pid);
4151 if (pgid < 0)
4152 return posix_error();
4153 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004154}
4155#endif /* HAVE_GETPGID */
4156
4157
Guido van Rossumb6775db1994-08-01 11:34:53 +00004158#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004159PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004160"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004161Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004162
Barry Warsaw53699e91996-12-10 23:23:01 +00004163static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004164posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004165{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004166#ifdef GETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004167 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004168#else /* GETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004169 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004170#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004171}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004172#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004174
Guido van Rossumb6775db1994-08-01 11:34:53 +00004175#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004176PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004177"setpgrp()\n\n\
Senthil Kumaran0d6908b2010-06-17 16:38:34 +00004178Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004179
Barry Warsaw53699e91996-12-10 23:23:01 +00004180static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004181posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004182{
Guido van Rossum64933891994-10-20 21:56:42 +00004183#ifdef SETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004184 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004185#else /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004186 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004187#endif /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004188 return posix_error();
4189 Py_INCREF(Py_None);
4190 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004191}
4192
Guido van Rossumb6775db1994-08-01 11:34:53 +00004193#endif /* HAVE_SETPGRP */
4194
Guido van Rossumad0ee831995-03-01 10:34:45 +00004195#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004196PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004197"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004198Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004199
Barry Warsaw53699e91996-12-10 23:23:01 +00004200static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004201posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004202{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004203 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004204}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004205#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004207
Fred Drake12c6e2d1999-12-14 21:25:03 +00004208#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004209PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004210"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004211Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004212
4213static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004214posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004215{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004216 PyObject *result = NULL;
4217 char *name;
4218 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004219
Victor Stinnerd6f85422010-05-05 23:33:33 +00004220 errno = 0;
4221 name = getlogin();
4222 if (name == NULL) {
4223 if (errno)
4224 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004225 else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004226 PyErr_SetString(PyExc_OSError,
4227 "unable to determine login name");
4228 }
4229 else
4230 result = PyString_FromString(name);
4231 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004232
Fred Drake12c6e2d1999-12-14 21:25:03 +00004233 return result;
4234}
4235#endif
4236
Guido van Rossumad0ee831995-03-01 10:34:45 +00004237#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004238PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004239"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004240Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004241
Barry Warsaw53699e91996-12-10 23:23:01 +00004242static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004243posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004244{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02004245 return _PyInt_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004246}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004247#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004249
Guido van Rossumad0ee831995-03-01 10:34:45 +00004250#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004251PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004252"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004253Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004254
Barry Warsaw53699e91996-12-10 23:23:01 +00004255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004256posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004257{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004258 pid_t pid;
4259 int sig;
4260 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4261 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004262#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004263 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4264 APIRET rc;
4265 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004266 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004267
4268 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4269 APIRET rc;
4270 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004271 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004272
4273 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004274 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004275#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004276 if (kill(pid, sig) == -1)
4277 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004278#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004279 Py_INCREF(Py_None);
4280 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004281}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004282#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004283
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004284#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004285PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004286"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004287Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004288
4289static PyObject *
4290posix_killpg(PyObject *self, PyObject *args)
4291{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004292 int sig;
4293 pid_t pgid;
4294 /* XXX some man pages make the `pgid` parameter an int, others
4295 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4296 take the same type. Moreover, pid_t is always at least as wide as
4297 int (else compilation of this module fails), which is safe. */
4298 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4299 return NULL;
4300 if (killpg(pgid, sig) == -1)
4301 return posix_error();
4302 Py_INCREF(Py_None);
4303 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004304}
4305#endif
4306
Brian Curtine5aa8862010-04-02 23:26:06 +00004307#ifdef MS_WINDOWS
4308PyDoc_STRVAR(win32_kill__doc__,
4309"kill(pid, sig)\n\n\
4310Kill a process with a signal.");
4311
4312static PyObject *
4313win32_kill(PyObject *self, PyObject *args)
4314{
Amaury Forgeot d'Arc03acec22010-05-15 21:45:30 +00004315 PyObject *result;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004316 DWORD pid, sig, err;
4317 HANDLE handle;
Brian Curtine5aa8862010-04-02 23:26:06 +00004318
Victor Stinnerd6f85422010-05-05 23:33:33 +00004319 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4320 return NULL;
Brian Curtine5aa8862010-04-02 23:26:06 +00004321
Victor Stinnerd6f85422010-05-05 23:33:33 +00004322 /* Console processes which share a common console can be sent CTRL+C or
4323 CTRL+BREAK events, provided they handle said events. */
4324 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4325 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4326 err = GetLastError();
4327 return PyErr_SetFromWindowsErr(err);
4328 }
4329 else
4330 Py_RETURN_NONE;
4331 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004332
Victor Stinnerd6f85422010-05-05 23:33:33 +00004333 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4334 attempt to open and terminate the process. */
4335 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4336 if (handle == NULL) {
4337 err = GetLastError();
4338 return PyErr_SetFromWindowsErr(err);
4339 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004340
Victor Stinnerd6f85422010-05-05 23:33:33 +00004341 if (TerminateProcess(handle, sig) == 0) {
4342 err = GetLastError();
4343 result = PyErr_SetFromWindowsErr(err);
4344 } else {
4345 Py_INCREF(Py_None);
4346 result = Py_None;
4347 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004348
Victor Stinnerd6f85422010-05-05 23:33:33 +00004349 CloseHandle(handle);
4350 return result;
Brian Curtine5aa8862010-04-02 23:26:06 +00004351}
Brian Curtincaea7e82011-06-08 19:29:53 -05004352
Brian Curtin5446f082011-06-09 10:00:42 -05004353PyDoc_STRVAR(posix__isdir__doc__,
4354"Return true if the pathname refers to an existing directory.");
4355
Brian Curtincaea7e82011-06-08 19:29:53 -05004356static PyObject *
4357posix__isdir(PyObject *self, PyObject *args)
4358{
4359 PyObject *opath;
4360 char *path;
4361 PyUnicodeObject *po;
4362 DWORD attributes;
4363
4364 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
4365 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
4366
4367 attributes = GetFileAttributesW(wpath);
4368 if (attributes == INVALID_FILE_ATTRIBUTES)
4369 Py_RETURN_FALSE;
4370 goto check;
4371 }
4372 /* Drop the argument parsing error as narrow strings
4373 are also valid. */
4374 PyErr_Clear();
4375
4376 if (!PyArg_ParseTuple(args, "et:_isdir",
4377 Py_FileSystemDefaultEncoding, &path))
4378 return NULL;
4379
4380 attributes = GetFileAttributesA(path);
Serhiy Storchaka46f5b352013-01-28 20:19:50 +02004381 PyMem_Free(path);
Brian Curtincaea7e82011-06-08 19:29:53 -05004382 if (attributes == INVALID_FILE_ATTRIBUTES)
4383 Py_RETURN_FALSE;
4384
4385check:
4386 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
4387 Py_RETURN_TRUE;
4388 else
4389 Py_RETURN_FALSE;
4390}
Brian Curtine5aa8862010-04-02 23:26:06 +00004391#endif /* MS_WINDOWS */
4392
Guido van Rossumc0125471996-06-28 18:55:32 +00004393#ifdef HAVE_PLOCK
4394
4395#ifdef HAVE_SYS_LOCK_H
4396#include <sys/lock.h>
4397#endif
4398
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004399PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004400"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004401Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004402
Barry Warsaw53699e91996-12-10 23:23:01 +00004403static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004404posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004405{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004406 int op;
4407 if (!PyArg_ParseTuple(args, "i:plock", &op))
4408 return NULL;
4409 if (plock(op) == -1)
4410 return posix_error();
4411 Py_INCREF(Py_None);
4412 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004413}
4414#endif
4415
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004416
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004417#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004418PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004419"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004420Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004421
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004422#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004423#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004424static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004425async_system(const char *command)
4426{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004427 char errormsg[256], args[1024];
4428 RESULTCODES rcodes;
4429 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004430
Victor Stinnerd6f85422010-05-05 23:33:33 +00004431 char *shell = getenv("COMSPEC");
4432 if (!shell)
4433 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004434
Victor Stinnerd6f85422010-05-05 23:33:33 +00004435 /* avoid overflowing the argument buffer */
4436 if (strlen(shell) + 3 + strlen(command) >= 1024)
4437 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004438
Victor Stinnerd6f85422010-05-05 23:33:33 +00004439 args[0] = '\0';
4440 strcat(args, shell);
4441 strcat(args, "/c ");
4442 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004443
Victor Stinnerd6f85422010-05-05 23:33:33 +00004444 /* execute asynchronously, inheriting the environment */
4445 rc = DosExecPgm(errormsg,
4446 sizeof(errormsg),
4447 EXEC_ASYNC,
4448 args,
4449 NULL,
4450 &rcodes,
4451 shell);
4452 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004453}
4454
Guido van Rossumd48f2521997-12-05 22:19:34 +00004455static FILE *
4456popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004457{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004458 int oldfd, tgtfd;
4459 HFILE pipeh[2];
4460 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004461
Victor Stinnerd6f85422010-05-05 23:33:33 +00004462 /* mode determines which of stdin or stdout is reconnected to
4463 * the pipe to the child
4464 */
4465 if (strchr(mode, 'r') != NULL) {
4466 tgt_fd = 1; /* stdout */
4467 } else if (strchr(mode, 'w')) {
4468 tgt_fd = 0; /* stdin */
4469 } else {
4470 *err = ERROR_INVALID_ACCESS;
4471 return NULL;
4472 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004473
Victor Stinnerd6f85422010-05-05 23:33:33 +00004474 /* setup the pipe */
4475 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4476 *err = rc;
4477 return NULL;
4478 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004479
Victor Stinnerd6f85422010-05-05 23:33:33 +00004480 /* prevent other threads accessing stdio */
4481 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004482
Victor Stinnerd6f85422010-05-05 23:33:33 +00004483 /* reconnect stdio and execute child */
4484 oldfd = dup(tgtfd);
4485 close(tgtfd);
4486 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4487 DosClose(pipeh[tgtfd]);
4488 rc = async_system(command);
4489 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004490
Victor Stinnerd6f85422010-05-05 23:33:33 +00004491 /* restore stdio */
4492 dup2(oldfd, tgtfd);
4493 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004494
Victor Stinnerd6f85422010-05-05 23:33:33 +00004495 /* allow other threads access to stdio */
4496 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004497
Victor Stinnerd6f85422010-05-05 23:33:33 +00004498 /* if execution of child was successful return file stream */
4499 if (rc == NO_ERROR)
4500 return fdopen(pipeh[1 - tgtfd], mode);
4501 else {
4502 DosClose(pipeh[1 - tgtfd]);
4503 *err = rc;
4504 return NULL;
4505 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004506}
4507
4508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004509posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004510{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004511 char *name;
4512 char *mode = "r";
4513 int err, bufsize = -1;
4514 FILE *fp;
4515 PyObject *f;
4516 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4517 return NULL;
4518 Py_BEGIN_ALLOW_THREADS
4519 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4520 Py_END_ALLOW_THREADS
4521 if (fp == NULL)
4522 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004523
Victor Stinnerd6f85422010-05-05 23:33:33 +00004524 f = PyFile_FromFile(fp, name, mode, fclose);
4525 if (f != NULL)
4526 PyFile_SetBufSize(f, bufsize);
4527 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004528}
4529
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004530#elif defined(PYCC_GCC)
4531
4532/* standard posix version of popen() support */
4533static PyObject *
4534posix_popen(PyObject *self, PyObject *args)
4535{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004536 char *name;
4537 char *mode = "r";
4538 int bufsize = -1;
4539 FILE *fp;
4540 PyObject *f;
4541 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4542 return NULL;
4543 Py_BEGIN_ALLOW_THREADS
4544 fp = popen(name, mode);
4545 Py_END_ALLOW_THREADS
4546 if (fp == NULL)
4547 return posix_error();
4548 f = PyFile_FromFile(fp, name, mode, pclose);
4549 if (f != NULL)
4550 PyFile_SetBufSize(f, bufsize);
4551 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004552}
4553
4554/* fork() under OS/2 has lots'o'warts
4555 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4556 * most of this code is a ripoff of the win32 code, but using the
4557 * capabilities of EMX's C library routines
4558 */
4559
4560/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4561#define POPEN_1 1
4562#define POPEN_2 2
4563#define POPEN_3 3
4564#define POPEN_4 4
4565
4566static PyObject *_PyPopen(char *, int, int, int);
4567static int _PyPclose(FILE *file);
4568
4569/*
4570 * Internal dictionary mapping popen* file pointers to process handles,
4571 * for use when retrieving the process exit code. See _PyPclose() below
4572 * for more information on this dictionary's use.
4573 */
4574static PyObject *_PyPopenProcs = NULL;
4575
4576/* os2emx version of popen2()
4577 *
4578 * The result of this function is a pipe (file) connected to the
4579 * process's stdin, and a pipe connected to the process's stdout.
4580 */
4581
4582static PyObject *
4583os2emx_popen2(PyObject *self, PyObject *args)
4584{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004585 PyObject *f;
4586 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004587
Victor Stinnerd6f85422010-05-05 23:33:33 +00004588 char *cmdstring;
4589 char *mode = "t";
4590 int bufsize = -1;
4591 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4592 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004593
Victor Stinnerd6f85422010-05-05 23:33:33 +00004594 if (*mode == 't')
4595 tm = O_TEXT;
4596 else if (*mode != 'b') {
4597 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4598 return NULL;
4599 } else
4600 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004601
Victor Stinnerd6f85422010-05-05 23:33:33 +00004602 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004603
Victor Stinnerd6f85422010-05-05 23:33:33 +00004604 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004605}
4606
4607/*
4608 * Variation on os2emx.popen2
4609 *
4610 * The result of this function is 3 pipes - the process's stdin,
4611 * stdout and stderr
4612 */
4613
4614static PyObject *
4615os2emx_popen3(PyObject *self, PyObject *args)
4616{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004617 PyObject *f;
4618 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004619
Victor Stinnerd6f85422010-05-05 23:33:33 +00004620 char *cmdstring;
4621 char *mode = "t";
4622 int bufsize = -1;
4623 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4624 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004625
Victor Stinnerd6f85422010-05-05 23:33:33 +00004626 if (*mode == 't')
4627 tm = O_TEXT;
4628 else if (*mode != 'b') {
4629 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4630 return NULL;
4631 } else
4632 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004633
Victor Stinnerd6f85422010-05-05 23:33:33 +00004634 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004635
Victor Stinnerd6f85422010-05-05 23:33:33 +00004636 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004637}
4638
4639/*
4640 * Variation on os2emx.popen2
4641 *
Tim Peters11b23062003-04-23 02:39:17 +00004642 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004643 * and stdout+stderr combined as a single pipe.
4644 */
4645
4646static PyObject *
4647os2emx_popen4(PyObject *self, PyObject *args)
4648{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004649 PyObject *f;
4650 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004651
Victor Stinnerd6f85422010-05-05 23:33:33 +00004652 char *cmdstring;
4653 char *mode = "t";
4654 int bufsize = -1;
4655 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4656 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004657
Victor Stinnerd6f85422010-05-05 23:33:33 +00004658 if (*mode == 't')
4659 tm = O_TEXT;
4660 else if (*mode != 'b') {
4661 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4662 return NULL;
4663 } else
4664 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004665
Victor Stinnerd6f85422010-05-05 23:33:33 +00004666 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004667
Victor Stinnerd6f85422010-05-05 23:33:33 +00004668 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004669}
4670
4671/* a couple of structures for convenient handling of multiple
4672 * file handles and pipes
4673 */
4674struct file_ref
4675{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004676 int handle;
4677 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004678};
4679
4680struct pipe_ref
4681{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004682 int rd;
4683 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004684};
4685
4686/* The following code is derived from the win32 code */
4687
4688static PyObject *
4689_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4690{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004691 struct file_ref stdio[3];
4692 struct pipe_ref p_fd[3];
4693 FILE *p_s[3];
4694 int file_count, i, pipe_err;
4695 pid_t pipe_pid;
4696 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4697 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004698
Victor Stinnerd6f85422010-05-05 23:33:33 +00004699 /* file modes for subsequent fdopen's on pipe handles */
4700 if (mode == O_TEXT)
4701 {
4702 rd_mode = "rt";
4703 wr_mode = "wt";
4704 }
4705 else
4706 {
4707 rd_mode = "rb";
4708 wr_mode = "wb";
4709 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004710
Victor Stinnerd6f85422010-05-05 23:33:33 +00004711 /* prepare shell references */
4712 if ((shell = getenv("EMXSHELL")) == NULL)
4713 if ((shell = getenv("COMSPEC")) == NULL)
4714 {
4715 errno = ENOENT;
4716 return posix_error();
4717 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004718
Victor Stinnerd6f85422010-05-05 23:33:33 +00004719 sh_name = _getname(shell);
4720 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4721 opt = "/c";
4722 else
4723 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004724
Victor Stinnerd6f85422010-05-05 23:33:33 +00004725 /* save current stdio fds + their flags, and set not inheritable */
4726 i = pipe_err = 0;
4727 while (pipe_err >= 0 && i < 3)
4728 {
4729 pipe_err = stdio[i].handle = dup(i);
4730 stdio[i].flags = fcntl(i, F_GETFD, 0);
4731 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4732 i++;
4733 }
4734 if (pipe_err < 0)
4735 {
4736 /* didn't get them all saved - clean up and bail out */
4737 int saved_err = errno;
4738 while (i-- > 0)
4739 {
4740 close(stdio[i].handle);
4741 }
4742 errno = saved_err;
4743 return posix_error();
4744 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004745
Victor Stinnerd6f85422010-05-05 23:33:33 +00004746 /* create pipe ends */
4747 file_count = 2;
4748 if (n == POPEN_3)
4749 file_count = 3;
4750 i = pipe_err = 0;
4751 while ((pipe_err == 0) && (i < file_count))
4752 pipe_err = pipe((int *)&p_fd[i++]);
4753 if (pipe_err < 0)
4754 {
4755 /* didn't get them all made - clean up and bail out */
4756 while (i-- > 0)
4757 {
4758 close(p_fd[i].wr);
4759 close(p_fd[i].rd);
4760 }
4761 errno = EPIPE;
4762 return posix_error();
4763 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004764
Victor Stinnerd6f85422010-05-05 23:33:33 +00004765 /* change the actual standard IO streams over temporarily,
4766 * making the retained pipe ends non-inheritable
4767 */
4768 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004769
Victor Stinnerd6f85422010-05-05 23:33:33 +00004770 /* - stdin */
4771 if (dup2(p_fd[0].rd, 0) == 0)
4772 {
4773 close(p_fd[0].rd);
4774 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4775 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4776 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4777 {
4778 close(p_fd[0].wr);
4779 pipe_err = -1;
4780 }
4781 }
4782 else
4783 {
4784 pipe_err = -1;
4785 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004786
Victor Stinnerd6f85422010-05-05 23:33:33 +00004787 /* - stdout */
4788 if (pipe_err == 0)
4789 {
4790 if (dup2(p_fd[1].wr, 1) == 1)
4791 {
4792 close(p_fd[1].wr);
4793 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4794 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4795 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4796 {
4797 close(p_fd[1].rd);
4798 pipe_err = -1;
4799 }
4800 }
4801 else
4802 {
4803 pipe_err = -1;
4804 }
4805 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004806
Victor Stinnerd6f85422010-05-05 23:33:33 +00004807 /* - stderr, as required */
4808 if (pipe_err == 0)
4809 switch (n)
4810 {
4811 case POPEN_3:
4812 {
4813 if (dup2(p_fd[2].wr, 2) == 2)
4814 {
4815 close(p_fd[2].wr);
4816 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4817 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4818 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4819 {
4820 close(p_fd[2].rd);
4821 pipe_err = -1;
4822 }
4823 }
4824 else
4825 {
4826 pipe_err = -1;
4827 }
4828 break;
4829 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004830
Victor Stinnerd6f85422010-05-05 23:33:33 +00004831 case POPEN_4:
4832 {
4833 if (dup2(1, 2) != 2)
4834 {
4835 pipe_err = -1;
4836 }
4837 break;
4838 }
4839 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004840
Victor Stinnerd6f85422010-05-05 23:33:33 +00004841 /* spawn the child process */
4842 if (pipe_err == 0)
4843 {
4844 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4845 if (pipe_pid == -1)
4846 {
4847 pipe_err = -1;
4848 }
4849 else
4850 {
4851 /* save the PID into the FILE structure
4852 * NOTE: this implementation doesn't actually
4853 * take advantage of this, but do it for
4854 * completeness - AIM Apr01
4855 */
4856 for (i = 0; i < file_count; i++)
4857 p_s[i]->_pid = pipe_pid;
4858 }
4859 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004860
Victor Stinnerd6f85422010-05-05 23:33:33 +00004861 /* reset standard IO to normal */
4862 for (i = 0; i < 3; i++)
4863 {
4864 dup2(stdio[i].handle, i);
4865 fcntl(i, F_SETFD, stdio[i].flags);
4866 close(stdio[i].handle);
4867 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004868
Victor Stinnerd6f85422010-05-05 23:33:33 +00004869 /* if any remnant problems, clean up and bail out */
4870 if (pipe_err < 0)
4871 {
4872 for (i = 0; i < 3; i++)
4873 {
4874 close(p_fd[i].rd);
4875 close(p_fd[i].wr);
4876 }
4877 errno = EPIPE;
4878 return posix_error_with_filename(cmdstring);
4879 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004880
Victor Stinnerd6f85422010-05-05 23:33:33 +00004881 /* build tuple of file objects to return */
4882 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4883 PyFile_SetBufSize(p_f[0], bufsize);
4884 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4885 PyFile_SetBufSize(p_f[1], bufsize);
4886 if (n == POPEN_3)
4887 {
4888 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4889 PyFile_SetBufSize(p_f[0], bufsize);
4890 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4891 }
4892 else
4893 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004894
Victor Stinnerd6f85422010-05-05 23:33:33 +00004895 /*
4896 * Insert the files we've created into the process dictionary
4897 * all referencing the list with the process handle and the
4898 * initial number of files (see description below in _PyPclose).
4899 * Since if _PyPclose later tried to wait on a process when all
4900 * handles weren't closed, it could create a deadlock with the
4901 * child, we spend some energy here to try to ensure that we
4902 * either insert all file handles into the dictionary or none
4903 * at all. It's a little clumsy with the various popen modes
4904 * and variable number of files involved.
4905 */
4906 if (!_PyPopenProcs)
4907 {
4908 _PyPopenProcs = PyDict_New();
4909 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004910
Victor Stinnerd6f85422010-05-05 23:33:33 +00004911 if (_PyPopenProcs)
4912 {
4913 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4914 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004915
Victor Stinnerd6f85422010-05-05 23:33:33 +00004916 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4917 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004918
Victor Stinnerd6f85422010-05-05 23:33:33 +00004919 procObj = PyList_New(2);
4920 pidObj = PyLong_FromPid(pipe_pid);
4921 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004922
Victor Stinnerd6f85422010-05-05 23:33:33 +00004923 if (procObj && pidObj && intObj)
4924 {
4925 PyList_SetItem(procObj, 0, pidObj);
4926 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004927
Victor Stinnerd6f85422010-05-05 23:33:33 +00004928 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4929 if (fileObj[0])
4930 {
4931 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4932 fileObj[0],
4933 procObj);
4934 }
4935 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4936 if (fileObj[1])
4937 {
4938 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4939 fileObj[1],
4940 procObj);
4941 }
4942 if (file_count >= 3)
4943 {
4944 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4945 if (fileObj[2])
4946 {
4947 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4948 fileObj[2],
4949 procObj);
4950 }
4951 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004952
Victor Stinnerd6f85422010-05-05 23:33:33 +00004953 if (ins_rc[0] < 0 || !fileObj[0] ||
4954 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4955 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4956 {
4957 /* Something failed - remove any dictionary
4958 * entries that did make it.
4959 */
4960 if (!ins_rc[0] && fileObj[0])
4961 {
4962 PyDict_DelItem(_PyPopenProcs,
4963 fileObj[0]);
4964 }
4965 if (!ins_rc[1] && fileObj[1])
4966 {
4967 PyDict_DelItem(_PyPopenProcs,
4968 fileObj[1]);
4969 }
4970 if (!ins_rc[2] && fileObj[2])
4971 {
4972 PyDict_DelItem(_PyPopenProcs,
4973 fileObj[2]);
4974 }
4975 }
4976 }
Tim Peters11b23062003-04-23 02:39:17 +00004977
Victor Stinnerd6f85422010-05-05 23:33:33 +00004978 /*
4979 * Clean up our localized references for the dictionary keys
4980 * and value since PyDict_SetItem will Py_INCREF any copies
4981 * that got placed in the dictionary.
4982 */
4983 Py_XDECREF(procObj);
4984 Py_XDECREF(fileObj[0]);
4985 Py_XDECREF(fileObj[1]);
4986 Py_XDECREF(fileObj[2]);
4987 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004988
Victor Stinnerd6f85422010-05-05 23:33:33 +00004989 /* Child is launched. */
4990 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004991}
4992
4993/*
4994 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4995 * exit code for the child process and return as a result of the close.
4996 *
4997 * This function uses the _PyPopenProcs dictionary in order to map the
4998 * input file pointer to information about the process that was
4999 * originally created by the popen* call that created the file pointer.
5000 * The dictionary uses the file pointer as a key (with one entry
5001 * inserted for each file returned by the original popen* call) and a
5002 * single list object as the value for all files from a single call.
5003 * The list object contains the Win32 process handle at [0], and a file
5004 * count at [1], which is initialized to the total number of file
5005 * handles using that list.
5006 *
5007 * This function closes whichever handle it is passed, and decrements
5008 * the file count in the dictionary for the process handle pointed to
5009 * by this file. On the last close (when the file count reaches zero),
5010 * this function will wait for the child process and then return its
5011 * exit code as the result of the close() operation. This permits the
5012 * files to be closed in any order - it is always the close() of the
5013 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00005014 *
5015 * NOTE: This function is currently called with the GIL released.
5016 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005017 */
5018
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005019static int _PyPclose(FILE *file)
5020{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005021 int result;
5022 int exit_code;
5023 pid_t pipe_pid;
5024 PyObject *procObj, *pidObj, *intObj, *fileObj;
5025 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005026#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005027 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005028#endif
5029
Victor Stinnerd6f85422010-05-05 23:33:33 +00005030 /* Close the file handle first, to ensure it can't block the
5031 * child from exiting if it's the last handle.
5032 */
5033 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005034
5035#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005036 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005037#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005038 if (_PyPopenProcs)
5039 {
5040 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5041 (procObj = PyDict_GetItem(_PyPopenProcs,
5042 fileObj)) != NULL &&
5043 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
5044 (intObj = PyList_GetItem(procObj,1)) != NULL)
5045 {
5046 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
5047 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005048
Victor Stinnerd6f85422010-05-05 23:33:33 +00005049 if (file_count > 1)
5050 {
5051 /* Still other files referencing process */
5052 file_count--;
5053 PyList_SetItem(procObj,1,
5054 PyInt_FromLong((long) file_count));
5055 }
5056 else
5057 {
5058 /* Last file for this process */
5059 if (result != EOF &&
5060 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
5061 {
5062 /* extract exit status */
5063 if (WIFEXITED(exit_code))
5064 {
5065 result = WEXITSTATUS(exit_code);
5066 }
5067 else
5068 {
5069 errno = EPIPE;
5070 result = -1;
5071 }
5072 }
5073 else
5074 {
5075 /* Indicate failure - this will cause the file object
5076 * to raise an I/O error and translate the last
5077 * error code from errno. We do have a problem with
5078 * last errors that overlap the normal errno table,
5079 * but that's a consistent problem with the file object.
5080 */
5081 result = -1;
5082 }
5083 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005084
Victor Stinnerd6f85422010-05-05 23:33:33 +00005085 /* Remove this file pointer from dictionary */
5086 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005087
Victor Stinnerd6f85422010-05-05 23:33:33 +00005088 if (PyDict_Size(_PyPopenProcs) == 0)
5089 {
5090 Py_DECREF(_PyPopenProcs);
5091 _PyPopenProcs = NULL;
5092 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005093
Victor Stinnerd6f85422010-05-05 23:33:33 +00005094 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005095
Victor Stinnerd6f85422010-05-05 23:33:33 +00005096 Py_XDECREF(fileObj);
5097 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005098
5099#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005100 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005101#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005102 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005103}
5104
5105#endif /* PYCC_??? */
5106
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005107#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005108
5109/*
5110 * Portable 'popen' replacement for Win32.
5111 *
5112 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
5113 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00005114 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005115 */
5116
5117#include <malloc.h>
5118#include <io.h>
5119#include <fcntl.h>
5120
5121/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
5122#define POPEN_1 1
5123#define POPEN_2 2
5124#define POPEN_3 3
5125#define POPEN_4 4
5126
5127static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005128static int _PyPclose(FILE *file);
5129
5130/*
5131 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00005132 * for use when retrieving the process exit code. See _PyPclose() below
5133 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005134 */
5135static PyObject *_PyPopenProcs = NULL;
5136
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005137
5138/* popen that works from a GUI.
5139 *
5140 * The result of this function is a pipe (file) connected to the
5141 * processes stdin or stdout, depending on the requested mode.
5142 */
5143
5144static PyObject *
5145posix_popen(PyObject *self, PyObject *args)
5146{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005147 PyObject *f;
5148 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005149
Victor Stinnerd6f85422010-05-05 23:33:33 +00005150 char *cmdstring;
5151 char *mode = "r";
5152 int bufsize = -1;
5153 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
5154 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005155
Victor Stinnerd6f85422010-05-05 23:33:33 +00005156 if (*mode == 'r')
5157 tm = _O_RDONLY;
5158 else if (*mode != 'w') {
5159 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
5160 return NULL;
5161 } else
5162 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00005163
Victor Stinnerd6f85422010-05-05 23:33:33 +00005164 if (bufsize != -1) {
5165 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
5166 return NULL;
5167 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005168
Victor Stinnerd6f85422010-05-05 23:33:33 +00005169 if (*(mode+1) == 't')
5170 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
5171 else if (*(mode+1) == 'b')
5172 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
5173 else
5174 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005175
Victor Stinnerd6f85422010-05-05 23:33:33 +00005176 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005177}
5178
5179/* Variation on win32pipe.popen
5180 *
5181 * The result of this function is a pipe (file) connected to the
5182 * process's stdin, and a pipe connected to the process's stdout.
5183 */
5184
5185static PyObject *
5186win32_popen2(PyObject *self, PyObject *args)
5187{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005188 PyObject *f;
5189 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00005190
Victor Stinnerd6f85422010-05-05 23:33:33 +00005191 char *cmdstring;
5192 char *mode = "t";
5193 int bufsize = -1;
5194 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
5195 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005196
Victor Stinnerd6f85422010-05-05 23:33:33 +00005197 if (*mode == 't')
5198 tm = _O_TEXT;
5199 else if (*mode != 'b') {
5200 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
5201 return NULL;
5202 } else
5203 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005204
Victor Stinnerd6f85422010-05-05 23:33:33 +00005205 if (bufsize != -1) {
5206 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
5207 return NULL;
5208 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005209
Victor Stinnerd6f85422010-05-05 23:33:33 +00005210 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00005211
Victor Stinnerd6f85422010-05-05 23:33:33 +00005212 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005213}
5214
5215/*
5216 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00005217 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005218 * The result of this function is 3 pipes - the process's stdin,
5219 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005220 */
5221
5222static PyObject *
5223win32_popen3(PyObject *self, PyObject *args)
5224{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005225 PyObject *f;
5226 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005227
Victor Stinnerd6f85422010-05-05 23:33:33 +00005228 char *cmdstring;
5229 char *mode = "t";
5230 int bufsize = -1;
5231 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
5232 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005233
Victor Stinnerd6f85422010-05-05 23:33:33 +00005234 if (*mode == 't')
5235 tm = _O_TEXT;
5236 else if (*mode != 'b') {
5237 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
5238 return NULL;
5239 } else
5240 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005241
Victor Stinnerd6f85422010-05-05 23:33:33 +00005242 if (bufsize != -1) {
5243 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
5244 return NULL;
5245 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005246
Victor Stinnerd6f85422010-05-05 23:33:33 +00005247 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00005248
Victor Stinnerd6f85422010-05-05 23:33:33 +00005249 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005250}
5251
5252/*
5253 * Variation on win32pipe.popen
5254 *
Tim Peters5aa91602002-01-30 05:46:57 +00005255 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005256 * and stdout+stderr combined as a single pipe.
5257 */
5258
5259static PyObject *
5260win32_popen4(PyObject *self, PyObject *args)
5261{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005262 PyObject *f;
5263 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005264
Victor Stinnerd6f85422010-05-05 23:33:33 +00005265 char *cmdstring;
5266 char *mode = "t";
5267 int bufsize = -1;
5268 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
5269 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005270
Victor Stinnerd6f85422010-05-05 23:33:33 +00005271 if (*mode == 't')
5272 tm = _O_TEXT;
5273 else if (*mode != 'b') {
5274 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
5275 return NULL;
5276 } else
5277 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005278
Victor Stinnerd6f85422010-05-05 23:33:33 +00005279 if (bufsize != -1) {
5280 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
5281 return NULL;
5282 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005283
Victor Stinnerd6f85422010-05-05 23:33:33 +00005284 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005285
Victor Stinnerd6f85422010-05-05 23:33:33 +00005286 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005287}
5288
Mark Hammond08501372001-01-31 07:30:29 +00005289static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00005290_PyPopenCreateProcess(char *cmdstring,
Victor Stinnerd6f85422010-05-05 23:33:33 +00005291 HANDLE hStdin,
5292 HANDLE hStdout,
5293 HANDLE hStderr,
5294 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005295{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005296 PROCESS_INFORMATION piProcInfo;
5297 STARTUPINFO siStartInfo;
5298 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
5299 char *s1,*s2, *s3 = " /c ";
5300 const char *szConsoleSpawn = "w9xpopen.exe";
5301 int i;
5302 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005303
Victor Stinnerd6f85422010-05-05 23:33:33 +00005304 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
5305 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005306
Victor Stinnerd6f85422010-05-05 23:33:33 +00005307 s1 = (char *)alloca(i);
5308 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
5309 /* x < i, so x fits into an integer */
5310 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00005311
Victor Stinnerd6f85422010-05-05 23:33:33 +00005312 /* Explicitly check if we are using COMMAND.COM. If we are
5313 * then use the w9xpopen hack.
5314 */
5315 comshell = s1 + x;
5316 while (comshell >= s1 && *comshell != '\\')
5317 --comshell;
5318 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005319
Victor Stinnerd6f85422010-05-05 23:33:33 +00005320 if (GetVersion() < 0x80000000 &&
5321 _stricmp(comshell, "command.com") != 0) {
5322 /* NT/2000 and not using command.com. */
5323 x = i + strlen(s3) + strlen(cmdstring) + 1;
5324 s2 = (char *)alloca(x);
5325 ZeroMemory(s2, x);
5326 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5327 }
5328 else {
5329 /*
5330 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5331 * the workaround listed in KB: Q150956
5332 */
5333 char modulepath[_MAX_PATH];
5334 struct stat statinfo;
5335 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5336 for (x = i = 0; modulepath[i]; i++)
5337 if (modulepath[i] == SEP)
5338 x = i+1;
5339 modulepath[x] = '\0';
5340 /* Create the full-name to w9xpopen, so we can test it exists */
5341 strncat(modulepath,
5342 szConsoleSpawn,
5343 (sizeof(modulepath)/sizeof(modulepath[0]))
5344 -strlen(modulepath));
5345 if (stat(modulepath, &statinfo) != 0) {
5346 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5347 /* Eeek - file-not-found - possibly an embedding
5348 situation - see if we can locate it in sys.prefix
5349 */
5350 strncpy(modulepath,
5351 Py_GetExecPrefix(),
5352 mplen);
5353 modulepath[mplen-1] = '\0';
5354 if (modulepath[strlen(modulepath)-1] != '\\')
5355 strcat(modulepath, "\\");
5356 strncat(modulepath,
5357 szConsoleSpawn,
5358 mplen-strlen(modulepath));
5359 /* No where else to look - raise an easily identifiable
5360 error, rather than leaving Windows to report
5361 "file not found" - as the user is probably blissfully
5362 unaware this shim EXE is used, and it will confuse them.
5363 (well, it confused me for a while ;-)
5364 */
5365 if (stat(modulepath, &statinfo) != 0) {
5366 PyErr_Format(PyExc_RuntimeError,
5367 "Can not locate '%s' which is needed "
5368 "for popen to work with your shell "
5369 "or platform.",
5370 szConsoleSpawn);
5371 return FALSE;
5372 }
5373 }
5374 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5375 strlen(modulepath) +
5376 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005377
Victor Stinnerd6f85422010-05-05 23:33:33 +00005378 s2 = (char *)alloca(x);
5379 ZeroMemory(s2, x);
5380 /* To maintain correct argument passing semantics,
5381 we pass the command-line as it stands, and allow
5382 quoting to be applied. w9xpopen.exe will then
5383 use its argv vector, and re-quote the necessary
5384 args for the ultimate child process.
5385 */
5386 PyOS_snprintf(
5387 s2, x,
5388 "\"%s\" %s%s%s",
5389 modulepath,
5390 s1,
5391 s3,
5392 cmdstring);
5393 /* Not passing CREATE_NEW_CONSOLE has been known to
5394 cause random failures on win9x. Specifically a
5395 dialog:
5396 "Your program accessed mem currently in use at xxx"
5397 and a hopeful warning about the stability of your
5398 system.
5399 Cost is Ctrl+C won't kill children, but anyone
5400 who cares can have a go!
5401 */
5402 dwProcessFlags |= CREATE_NEW_CONSOLE;
5403 }
5404 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005405
Victor Stinnerd6f85422010-05-05 23:33:33 +00005406 /* Could be an else here to try cmd.exe / command.com in the path
5407 Now we'll just error out.. */
5408 else {
5409 PyErr_SetString(PyExc_RuntimeError,
5410 "Cannot locate a COMSPEC environment variable to "
5411 "use as the shell");
5412 return FALSE;
5413 }
Tim Peters5aa91602002-01-30 05:46:57 +00005414
Victor Stinnerd6f85422010-05-05 23:33:33 +00005415 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5416 siStartInfo.cb = sizeof(STARTUPINFO);
5417 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5418 siStartInfo.hStdInput = hStdin;
5419 siStartInfo.hStdOutput = hStdout;
5420 siStartInfo.hStdError = hStderr;
5421 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005422
Victor Stinnerd6f85422010-05-05 23:33:33 +00005423 if (CreateProcess(NULL,
5424 s2,
5425 NULL,
5426 NULL,
5427 TRUE,
5428 dwProcessFlags,
5429 NULL,
5430 NULL,
5431 &siStartInfo,
5432 &piProcInfo) ) {
5433 /* Close the handles now so anyone waiting is woken. */
5434 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005435
Victor Stinnerd6f85422010-05-05 23:33:33 +00005436 /* Return process handle */
5437 *hProcess = piProcInfo.hProcess;
5438 return TRUE;
5439 }
5440 win32_error("CreateProcess", s2);
5441 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005442}
5443
5444/* The following code is based off of KB: Q190351 */
5445
5446static PyObject *
5447_PyPopen(char *cmdstring, int mode, int n)
5448{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005449 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5450 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5451 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005452
Victor Stinnerd6f85422010-05-05 23:33:33 +00005453 SECURITY_ATTRIBUTES saAttr;
5454 BOOL fSuccess;
5455 int fd1, fd2, fd3;
5456 FILE *f1, *f2, *f3;
5457 long file_count;
5458 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005459
Victor Stinnerd6f85422010-05-05 23:33:33 +00005460 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5461 saAttr.bInheritHandle = TRUE;
5462 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005463
Victor Stinnerd6f85422010-05-05 23:33:33 +00005464 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5465 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005466
Victor Stinnerd6f85422010-05-05 23:33:33 +00005467 /* Create new output read handle and the input write handle. Set
5468 * the inheritance properties to FALSE. Otherwise, the child inherits
5469 * these handles; resulting in non-closeable handles to the pipes
5470 * being created. */
5471 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5472 GetCurrentProcess(), &hChildStdinWrDup, 0,
5473 FALSE,
5474 DUPLICATE_SAME_ACCESS);
5475 if (!fSuccess)
5476 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005477
Victor Stinnerd6f85422010-05-05 23:33:33 +00005478 /* Close the inheritable version of ChildStdin
5479 that we're using. */
5480 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005481
Victor Stinnerd6f85422010-05-05 23:33:33 +00005482 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5483 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005484
Victor Stinnerd6f85422010-05-05 23:33:33 +00005485 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5486 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5487 FALSE, DUPLICATE_SAME_ACCESS);
5488 if (!fSuccess)
5489 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005490
Victor Stinnerd6f85422010-05-05 23:33:33 +00005491 /* Close the inheritable version of ChildStdout
5492 that we're using. */
5493 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005494
Victor Stinnerd6f85422010-05-05 23:33:33 +00005495 if (n != POPEN_4) {
5496 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5497 return win32_error("CreatePipe", NULL);
5498 fSuccess = DuplicateHandle(GetCurrentProcess(),
5499 hChildStderrRd,
5500 GetCurrentProcess(),
5501 &hChildStderrRdDup, 0,
5502 FALSE, DUPLICATE_SAME_ACCESS);
5503 if (!fSuccess)
5504 return win32_error("DuplicateHandle", NULL);
5505 /* Close the inheritable version of ChildStdErr that we're using. */
5506 CloseHandle(hChildStderrRd);
5507 }
Tim Peters5aa91602002-01-30 05:46:57 +00005508
Victor Stinnerd6f85422010-05-05 23:33:33 +00005509 switch (n) {
5510 case POPEN_1:
5511 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5512 case _O_WRONLY | _O_TEXT:
5513 /* Case for writing to child Stdin in text mode. */
5514 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5515 f1 = _fdopen(fd1, "w");
5516 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5517 PyFile_SetBufSize(f, 0);
5518 /* We don't care about these pipes anymore, so close them. */
5519 CloseHandle(hChildStdoutRdDup);
5520 CloseHandle(hChildStderrRdDup);
5521 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005522
Victor Stinnerd6f85422010-05-05 23:33:33 +00005523 case _O_RDONLY | _O_TEXT:
5524 /* Case for reading from child Stdout in text mode. */
5525 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5526 f1 = _fdopen(fd1, "r");
5527 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5528 PyFile_SetBufSize(f, 0);
5529 /* We don't care about these pipes anymore, so close them. */
5530 CloseHandle(hChildStdinWrDup);
5531 CloseHandle(hChildStderrRdDup);
5532 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005533
Victor Stinnerd6f85422010-05-05 23:33:33 +00005534 case _O_RDONLY | _O_BINARY:
5535 /* Case for readinig from child Stdout in binary mode. */
5536 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5537 f1 = _fdopen(fd1, "rb");
5538 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5539 PyFile_SetBufSize(f, 0);
5540 /* We don't care about these pipes anymore, so close them. */
5541 CloseHandle(hChildStdinWrDup);
5542 CloseHandle(hChildStderrRdDup);
5543 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005544
Victor Stinnerd6f85422010-05-05 23:33:33 +00005545 case _O_WRONLY | _O_BINARY:
5546 /* Case for writing to child Stdin in binary mode. */
5547 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5548 f1 = _fdopen(fd1, "wb");
5549 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5550 PyFile_SetBufSize(f, 0);
5551 /* We don't care about these pipes anymore, so close them. */
5552 CloseHandle(hChildStdoutRdDup);
5553 CloseHandle(hChildStderrRdDup);
5554 break;
5555 }
5556 file_count = 1;
5557 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005558
Victor Stinnerd6f85422010-05-05 23:33:33 +00005559 case POPEN_2:
5560 case POPEN_4:
5561 {
5562 char *m1, *m2;
5563 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005564
Victor Stinnerd6f85422010-05-05 23:33:33 +00005565 if (mode & _O_TEXT) {
5566 m1 = "r";
5567 m2 = "w";
5568 } else {
5569 m1 = "rb";
5570 m2 = "wb";
5571 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005572
Victor Stinnerd6f85422010-05-05 23:33:33 +00005573 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5574 f1 = _fdopen(fd1, m2);
5575 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5576 f2 = _fdopen(fd2, m1);
5577 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5578 PyFile_SetBufSize(p1, 0);
5579 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5580 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005581
Victor Stinnerd6f85422010-05-05 23:33:33 +00005582 if (n != 4)
5583 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005584
Victor Stinnerd6f85422010-05-05 23:33:33 +00005585 f = PyTuple_Pack(2,p1,p2);
5586 Py_XDECREF(p1);
5587 Py_XDECREF(p2);
5588 file_count = 2;
5589 break;
5590 }
Tim Peters5aa91602002-01-30 05:46:57 +00005591
Victor Stinnerd6f85422010-05-05 23:33:33 +00005592 case POPEN_3:
5593 {
5594 char *m1, *m2;
5595 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005596
Victor Stinnerd6f85422010-05-05 23:33:33 +00005597 if (mode & _O_TEXT) {
5598 m1 = "r";
5599 m2 = "w";
5600 } else {
5601 m1 = "rb";
5602 m2 = "wb";
5603 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005604
Victor Stinnerd6f85422010-05-05 23:33:33 +00005605 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5606 f1 = _fdopen(fd1, m2);
5607 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5608 f2 = _fdopen(fd2, m1);
5609 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5610 f3 = _fdopen(fd3, m1);
5611 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5612 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5613 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5614 PyFile_SetBufSize(p1, 0);
5615 PyFile_SetBufSize(p2, 0);
5616 PyFile_SetBufSize(p3, 0);
5617 f = PyTuple_Pack(3,p1,p2,p3);
5618 Py_XDECREF(p1);
5619 Py_XDECREF(p2);
5620 Py_XDECREF(p3);
5621 file_count = 3;
5622 break;
5623 }
5624 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005625
Victor Stinnerd6f85422010-05-05 23:33:33 +00005626 if (n == POPEN_4) {
5627 if (!_PyPopenCreateProcess(cmdstring,
5628 hChildStdinRd,
5629 hChildStdoutWr,
5630 hChildStdoutWr,
5631 &hProcess))
5632 return NULL;
5633 }
5634 else {
5635 if (!_PyPopenCreateProcess(cmdstring,
5636 hChildStdinRd,
5637 hChildStdoutWr,
5638 hChildStderrWr,
5639 &hProcess))
5640 return NULL;
5641 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005642
Victor Stinnerd6f85422010-05-05 23:33:33 +00005643 /*
5644 * Insert the files we've created into the process dictionary
5645 * all referencing the list with the process handle and the
5646 * initial number of files (see description below in _PyPclose).
5647 * Since if _PyPclose later tried to wait on a process when all
5648 * handles weren't closed, it could create a deadlock with the
5649 * child, we spend some energy here to try to ensure that we
5650 * either insert all file handles into the dictionary or none
5651 * at all. It's a little clumsy with the various popen modes
5652 * and variable number of files involved.
5653 */
5654 if (!_PyPopenProcs) {
5655 _PyPopenProcs = PyDict_New();
5656 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005657
Victor Stinnerd6f85422010-05-05 23:33:33 +00005658 if (_PyPopenProcs) {
5659 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5660 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005661
Victor Stinnerd6f85422010-05-05 23:33:33 +00005662 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5663 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005664
Victor Stinnerd6f85422010-05-05 23:33:33 +00005665 procObj = PyList_New(2);
5666 hProcessObj = PyLong_FromVoidPtr(hProcess);
5667 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005668
Victor Stinnerd6f85422010-05-05 23:33:33 +00005669 if (procObj && hProcessObj && intObj) {
5670 PyList_SetItem(procObj,0,hProcessObj);
5671 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005672
Victor Stinnerd6f85422010-05-05 23:33:33 +00005673 fileObj[0] = PyLong_FromVoidPtr(f1);
5674 if (fileObj[0]) {
5675 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5676 fileObj[0],
5677 procObj);
5678 }
5679 if (file_count >= 2) {
5680 fileObj[1] = PyLong_FromVoidPtr(f2);
5681 if (fileObj[1]) {
5682 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5683 fileObj[1],
5684 procObj);
5685 }
5686 }
5687 if (file_count >= 3) {
5688 fileObj[2] = PyLong_FromVoidPtr(f3);
5689 if (fileObj[2]) {
5690 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5691 fileObj[2],
5692 procObj);
5693 }
5694 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005695
Victor Stinnerd6f85422010-05-05 23:33:33 +00005696 if (ins_rc[0] < 0 || !fileObj[0] ||
5697 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5698 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5699 /* Something failed - remove any dictionary
5700 * entries that did make it.
5701 */
5702 if (!ins_rc[0] && fileObj[0]) {
5703 PyDict_DelItem(_PyPopenProcs,
5704 fileObj[0]);
5705 }
5706 if (!ins_rc[1] && fileObj[1]) {
5707 PyDict_DelItem(_PyPopenProcs,
5708 fileObj[1]);
5709 }
5710 if (!ins_rc[2] && fileObj[2]) {
5711 PyDict_DelItem(_PyPopenProcs,
5712 fileObj[2]);
5713 }
5714 }
5715 }
Tim Peters5aa91602002-01-30 05:46:57 +00005716
Victor Stinnerd6f85422010-05-05 23:33:33 +00005717 /*
5718 * Clean up our localized references for the dictionary keys
5719 * and value since PyDict_SetItem will Py_INCREF any copies
5720 * that got placed in the dictionary.
5721 */
5722 Py_XDECREF(procObj);
5723 Py_XDECREF(fileObj[0]);
5724 Py_XDECREF(fileObj[1]);
5725 Py_XDECREF(fileObj[2]);
5726 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005727
Victor Stinnerd6f85422010-05-05 23:33:33 +00005728 /* Child is launched. Close the parents copy of those pipe
5729 * handles that only the child should have open. You need to
5730 * make sure that no handles to the write end of the output pipe
5731 * are maintained in this process or else the pipe will not close
5732 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005733
Victor Stinnerd6f85422010-05-05 23:33:33 +00005734 if (!CloseHandle(hChildStdinRd))
5735 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005736
Victor Stinnerd6f85422010-05-05 23:33:33 +00005737 if (!CloseHandle(hChildStdoutWr))
5738 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005739
Victor Stinnerd6f85422010-05-05 23:33:33 +00005740 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5741 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005742
Victor Stinnerd6f85422010-05-05 23:33:33 +00005743 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005744}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005745
5746/*
5747 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5748 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005749 *
5750 * This function uses the _PyPopenProcs dictionary in order to map the
5751 * input file pointer to information about the process that was
5752 * originally created by the popen* call that created the file pointer.
5753 * The dictionary uses the file pointer as a key (with one entry
5754 * inserted for each file returned by the original popen* call) and a
5755 * single list object as the value for all files from a single call.
5756 * The list object contains the Win32 process handle at [0], and a file
5757 * count at [1], which is initialized to the total number of file
5758 * handles using that list.
5759 *
5760 * This function closes whichever handle it is passed, and decrements
5761 * the file count in the dictionary for the process handle pointed to
5762 * by this file. On the last close (when the file count reaches zero),
5763 * this function will wait for the child process and then return its
5764 * exit code as the result of the close() operation. This permits the
5765 * files to be closed in any order - it is always the close() of the
5766 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005767 *
5768 * NOTE: This function is currently called with the GIL released.
5769 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005770 */
Tim Peters736aa322000-09-01 06:51:24 +00005771
Fredrik Lundh56055a42000-07-23 19:47:12 +00005772static int _PyPclose(FILE *file)
5773{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005774 int result;
5775 DWORD exit_code;
5776 HANDLE hProcess;
5777 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5778 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005779#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005780 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005781#endif
5782
Victor Stinnerd6f85422010-05-05 23:33:33 +00005783 /* Close the file handle first, to ensure it can't block the
5784 * child from exiting if it's the last handle.
5785 */
5786 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005787#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005788 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005789#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005790 if (_PyPopenProcs) {
5791 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5792 (procObj = PyDict_GetItem(_PyPopenProcs,
5793 fileObj)) != NULL &&
5794 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5795 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005796
Victor Stinnerd6f85422010-05-05 23:33:33 +00005797 hProcess = PyLong_AsVoidPtr(hProcessObj);
5798 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005799
Victor Stinnerd6f85422010-05-05 23:33:33 +00005800 if (file_count > 1) {
5801 /* Still other files referencing process */
5802 file_count--;
5803 PyList_SetItem(procObj,1,
5804 PyInt_FromLong(file_count));
5805 } else {
5806 /* Last file for this process */
5807 if (result != EOF &&
5808 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5809 GetExitCodeProcess(hProcess, &exit_code)) {
5810 /* Possible truncation here in 16-bit environments, but
5811 * real exit codes are just the lower byte in any event.
5812 */
5813 result = exit_code;
5814 } else {
5815 /* Indicate failure - this will cause the file object
5816 * to raise an I/O error and translate the last Win32
5817 * error code from errno. We do have a problem with
5818 * last errors that overlap the normal errno table,
5819 * but that's a consistent problem with the file object.
5820 */
5821 if (result != EOF) {
5822 /* If the error wasn't from the fclose(), then
5823 * set errno for the file object error handling.
5824 */
5825 errno = GetLastError();
5826 }
5827 result = -1;
5828 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005829
Victor Stinnerd6f85422010-05-05 23:33:33 +00005830 /* Free up the native handle at this point */
5831 CloseHandle(hProcess);
5832 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005833
Victor Stinnerd6f85422010-05-05 23:33:33 +00005834 /* Remove this file pointer from dictionary */
5835 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005836
Victor Stinnerd6f85422010-05-05 23:33:33 +00005837 if (PyDict_Size(_PyPopenProcs) == 0) {
5838 Py_DECREF(_PyPopenProcs);
5839 _PyPopenProcs = NULL;
5840 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005841
Victor Stinnerd6f85422010-05-05 23:33:33 +00005842 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005843
Victor Stinnerd6f85422010-05-05 23:33:33 +00005844 Py_XDECREF(fileObj);
5845 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005846
Tim Peters736aa322000-09-01 06:51:24 +00005847#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005848 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005849#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005850 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005851}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005852
5853#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005854static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005855posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005856{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005857 char *name;
5858 char *mode = "r";
5859 int bufsize = -1;
5860 FILE *fp;
5861 PyObject *f;
5862 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5863 return NULL;
5864 /* Strip mode of binary or text modifiers */
5865 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5866 mode = "r";
5867 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5868 mode = "w";
5869 Py_BEGIN_ALLOW_THREADS
5870 fp = popen(name, mode);
5871 Py_END_ALLOW_THREADS
5872 if (fp == NULL)
5873 return posix_error();
5874 f = PyFile_FromFile(fp, name, mode, pclose);
5875 if (f != NULL)
5876 PyFile_SetBufSize(f, bufsize);
5877 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005878}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005879
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005880#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005881#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005883
Guido van Rossumb6775db1994-08-01 11:34:53 +00005884#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005885PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005886"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005887Set the current process's user id.");
5888
Barry Warsaw53699e91996-12-10 23:23:01 +00005889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005890posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005891{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005892 uid_t uid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005893 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005894 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005895 if (setuid(uid) < 0)
5896 return posix_error();
5897 Py_INCREF(Py_None);
5898 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005899}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005900#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005902
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005903#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005904PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005905"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005906Set the current process's effective user id.");
5907
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005908static PyObject *
5909posix_seteuid (PyObject *self, PyObject *args)
5910{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005911 uid_t euid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005912 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005913 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005914 if (seteuid(euid) < 0) {
5915 return posix_error();
5916 } else {
5917 Py_INCREF(Py_None);
5918 return Py_None;
5919 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005920}
5921#endif /* HAVE_SETEUID */
5922
5923#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005924PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005925"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005926Set the current process's effective group id.");
5927
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005928static PyObject *
5929posix_setegid (PyObject *self, PyObject *args)
5930{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005931 gid_t egid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005932 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005933 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005934 if (setegid(egid) < 0) {
5935 return posix_error();
5936 } else {
5937 Py_INCREF(Py_None);
5938 return Py_None;
5939 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005940}
5941#endif /* HAVE_SETEGID */
5942
5943#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005944PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005945"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005946Set the current process's real and effective user ids.");
5947
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005948static PyObject *
5949posix_setreuid (PyObject *self, PyObject *args)
5950{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005951 uid_t ruid, euid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005952 if (!PyArg_ParseTuple(args, "O&O&:setreuid",
5953 _Py_Uid_Converter, &ruid,
5954 _Py_Uid_Converter, &euid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005955 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005956 if (setreuid(ruid, euid) < 0) {
5957 return posix_error();
5958 } else {
5959 Py_INCREF(Py_None);
5960 return Py_None;
5961 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005962}
5963#endif /* HAVE_SETREUID */
5964
5965#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005966PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005967"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005968Set the current process's real and effective group ids.");
5969
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005970static PyObject *
5971posix_setregid (PyObject *self, PyObject *args)
5972{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005973 gid_t rgid, egid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005974 if (!PyArg_ParseTuple(args, "O&O&:setregid",
5975 _Py_Gid_Converter, &rgid,
5976 _Py_Gid_Converter, &egid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005977 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005978 if (setregid(rgid, egid) < 0) {
5979 return posix_error();
5980 } else {
5981 Py_INCREF(Py_None);
5982 return Py_None;
5983 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005984}
5985#endif /* HAVE_SETREGID */
5986
Guido van Rossumb6775db1994-08-01 11:34:53 +00005987#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005988PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005989"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005990Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005991
Barry Warsaw53699e91996-12-10 23:23:01 +00005992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005993posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005994{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005995 gid_t gid;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02005996 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00005997 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00005998 if (setgid(gid) < 0)
5999 return posix_error();
6000 Py_INCREF(Py_None);
6001 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006002}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006003#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006004
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006005#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006006PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006007"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006008Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006009
6010static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00006011posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006012{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006013 int i, len;
6014 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006015
Victor Stinnerd6f85422010-05-05 23:33:33 +00006016 if (!PySequence_Check(groups)) {
6017 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6018 return NULL;
6019 }
6020 len = PySequence_Size(groups);
6021 if (len > MAX_GROUPS) {
6022 PyErr_SetString(PyExc_ValueError, "too many groups");
6023 return NULL;
6024 }
6025 for(i = 0; i < len; i++) {
6026 PyObject *elem;
6027 elem = PySequence_GetItem(groups, i);
6028 if (!elem)
6029 return NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006030 if (!PyInt_Check(elem) && !PyLong_Check(elem)) {
6031 PyErr_SetString(PyExc_TypeError,
6032 "groups must be integers");
6033 Py_DECREF(elem);
6034 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006035 } else {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006036 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00006037 Py_DECREF(elem);
6038 return NULL;
6039 }
6040 }
6041 Py_DECREF(elem);
6042 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006043
Victor Stinnerd6f85422010-05-05 23:33:33 +00006044 if (setgroups(len, grouplist) < 0)
6045 return posix_error();
6046 Py_INCREF(Py_None);
6047 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006048}
6049#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006050
Neal Norwitz6c2f9132006-03-20 07:25:26 +00006051#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00006052static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00006053wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00006054{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006055 PyObject *result;
6056 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00006057
Victor Stinnerd6f85422010-05-05 23:33:33 +00006058 if (pid == -1)
6059 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00006060
Victor Stinnerd6f85422010-05-05 23:33:33 +00006061 if (struct_rusage == NULL) {
6062 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6063 if (m == NULL)
6064 return NULL;
6065 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6066 Py_DECREF(m);
6067 if (struct_rusage == NULL)
6068 return NULL;
6069 }
Neal Norwitz05a45592006-03-20 06:30:08 +00006070
Victor Stinnerd6f85422010-05-05 23:33:33 +00006071 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6072 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6073 if (!result)
6074 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006075
6076#ifndef doubletime
6077#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6078#endif
6079
Victor Stinnerd6f85422010-05-05 23:33:33 +00006080 PyStructSequence_SET_ITEM(result, 0,
6081 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6082 PyStructSequence_SET_ITEM(result, 1,
6083 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00006084#define SET_INT(result, index, value)\
Victor Stinnerd6f85422010-05-05 23:33:33 +00006085 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
6086 SET_INT(result, 2, ru->ru_maxrss);
6087 SET_INT(result, 3, ru->ru_ixrss);
6088 SET_INT(result, 4, ru->ru_idrss);
6089 SET_INT(result, 5, ru->ru_isrss);
6090 SET_INT(result, 6, ru->ru_minflt);
6091 SET_INT(result, 7, ru->ru_majflt);
6092 SET_INT(result, 8, ru->ru_nswap);
6093 SET_INT(result, 9, ru->ru_inblock);
6094 SET_INT(result, 10, ru->ru_oublock);
6095 SET_INT(result, 11, ru->ru_msgsnd);
6096 SET_INT(result, 12, ru->ru_msgrcv);
6097 SET_INT(result, 13, ru->ru_nsignals);
6098 SET_INT(result, 14, ru->ru_nvcsw);
6099 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00006100#undef SET_INT
6101
Victor Stinnerd6f85422010-05-05 23:33:33 +00006102 if (PyErr_Occurred()) {
6103 Py_DECREF(result);
6104 return NULL;
6105 }
Neal Norwitz05a45592006-03-20 06:30:08 +00006106
Victor Stinnerd6f85422010-05-05 23:33:33 +00006107 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00006108}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00006109#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00006110
6111#ifdef HAVE_WAIT3
6112PyDoc_STRVAR(posix_wait3__doc__,
6113"wait3(options) -> (pid, status, rusage)\n\n\
6114Wait for completion of a child process.");
6115
6116static PyObject *
6117posix_wait3(PyObject *self, PyObject *args)
6118{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006119 pid_t pid;
6120 int options;
6121 struct rusage ru;
6122 WAIT_TYPE status;
6123 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006124
Victor Stinnerd6f85422010-05-05 23:33:33 +00006125 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6126 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006127
Victor Stinnerd6f85422010-05-05 23:33:33 +00006128 Py_BEGIN_ALLOW_THREADS
6129 pid = wait3(&status, options, &ru);
6130 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006131
Victor Stinnerd6f85422010-05-05 23:33:33 +00006132 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006133}
6134#endif /* HAVE_WAIT3 */
6135
6136#ifdef HAVE_WAIT4
6137PyDoc_STRVAR(posix_wait4__doc__,
6138"wait4(pid, options) -> (pid, status, rusage)\n\n\
6139Wait for completion of a given child process.");
6140
6141static PyObject *
6142posix_wait4(PyObject *self, PyObject *args)
6143{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006144 pid_t pid;
6145 int options;
6146 struct rusage ru;
6147 WAIT_TYPE status;
6148 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006149
Victor Stinnerd6f85422010-05-05 23:33:33 +00006150 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
6151 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006152
Victor Stinnerd6f85422010-05-05 23:33:33 +00006153 Py_BEGIN_ALLOW_THREADS
6154 pid = wait4(pid, &status, options, &ru);
6155 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006156
Victor Stinnerd6f85422010-05-05 23:33:33 +00006157 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006158}
6159#endif /* HAVE_WAIT4 */
6160
Guido van Rossumb6775db1994-08-01 11:34:53 +00006161#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006162PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006163"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006164Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006165
Barry Warsaw53699e91996-12-10 23:23:01 +00006166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006167posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006168{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006169 pid_t pid;
6170 int options;
6171 WAIT_TYPE status;
6172 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006173
Victor Stinnerd6f85422010-05-05 23:33:33 +00006174 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6175 return NULL;
6176 Py_BEGIN_ALLOW_THREADS
6177 pid = waitpid(pid, &status, options);
6178 Py_END_ALLOW_THREADS
6179 if (pid == -1)
6180 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006181
Victor Stinnerd6f85422010-05-05 23:33:33 +00006182 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006183}
6184
Tim Petersab034fa2002-02-01 11:27:43 +00006185#elif defined(HAVE_CWAIT)
6186
6187/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006188PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006189"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006190"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006191
6192static PyObject *
6193posix_waitpid(PyObject *self, PyObject *args)
6194{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006195 Py_intptr_t pid;
6196 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006197
Victor Stinnerd6f85422010-05-05 23:33:33 +00006198 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6199 return NULL;
6200 Py_BEGIN_ALLOW_THREADS
6201 pid = _cwait(&status, pid, options);
6202 Py_END_ALLOW_THREADS
6203 if (pid == -1)
6204 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006205
Victor Stinnerd6f85422010-05-05 23:33:33 +00006206 /* shift the status left a byte so this is more like the POSIX waitpid */
6207 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006208}
6209#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006210
Guido van Rossumad0ee831995-03-01 10:34:45 +00006211#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006212PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006213"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006214Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006215
Barry Warsaw53699e91996-12-10 23:23:01 +00006216static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006217posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006218{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006219 pid_t pid;
6220 WAIT_TYPE status;
6221 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006222
Victor Stinnerd6f85422010-05-05 23:33:33 +00006223 Py_BEGIN_ALLOW_THREADS
6224 pid = wait(&status);
6225 Py_END_ALLOW_THREADS
6226 if (pid == -1)
6227 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006228
Victor Stinnerd6f85422010-05-05 23:33:33 +00006229 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006230}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006231#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006232
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006235"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Barry Warsaw53699e91996-12-10 23:23:01 +00006238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006239posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006240{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006241#ifdef HAVE_LSTAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00006242 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006243#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006244#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006245 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006246#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006247 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006248#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006249#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006250}
6251
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006252
Guido van Rossumb6775db1994-08-01 11:34:53 +00006253#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006254PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006255"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006256Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006257
Barry Warsaw53699e91996-12-10 23:23:01 +00006258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006259posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006260{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006261 PyObject* v;
6262 char buf[MAXPATHLEN];
6263 char *path;
6264 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006265#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006266 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006267#endif
6268
Victor Stinnerd6f85422010-05-05 23:33:33 +00006269 if (!PyArg_ParseTuple(args, "et:readlink",
6270 Py_FileSystemDefaultEncoding, &path))
6271 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006272#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006273 v = PySequence_GetItem(args, 0);
6274 if (v == NULL) {
6275 PyMem_Free(path);
6276 return NULL;
6277 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006278
Victor Stinnerd6f85422010-05-05 23:33:33 +00006279 if (PyUnicode_Check(v)) {
6280 arg_is_unicode = 1;
6281 }
6282 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006283#endif
6284
Victor Stinnerd6f85422010-05-05 23:33:33 +00006285 Py_BEGIN_ALLOW_THREADS
6286 n = readlink(path, buf, (int) sizeof buf);
6287 Py_END_ALLOW_THREADS
6288 if (n < 0)
6289 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006290
Victor Stinnerd6f85422010-05-05 23:33:33 +00006291 PyMem_Free(path);
6292 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006293#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006294 if (arg_is_unicode) {
6295 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006296
Victor Stinnerd6f85422010-05-05 23:33:33 +00006297 w = PyUnicode_FromEncodedObject(v,
6298 Py_FileSystemDefaultEncoding,
6299 "strict");
6300 if (w != NULL) {
6301 Py_DECREF(v);
6302 v = w;
6303 }
6304 else {
6305 /* fall back to the original byte string, as
6306 discussed in patch #683592 */
6307 PyErr_Clear();
6308 }
6309 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006310#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006311 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006312}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006313#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006314
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006315
Guido van Rossumb6775db1994-08-01 11:34:53 +00006316#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006317PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006318"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006319Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006320
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006322posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006323{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006324 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006325}
6326#endif /* HAVE_SYMLINK */
6327
6328
6329#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006330#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6331static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006332system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006333{
6334 ULONG value = 0;
6335
6336 Py_BEGIN_ALLOW_THREADS
6337 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6338 Py_END_ALLOW_THREADS
6339
6340 return value;
6341}
6342
6343static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006344posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006345{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006346 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006347 return Py_BuildValue("ddddd",
6348 (double)0 /* t.tms_utime / HZ */,
6349 (double)0 /* t.tms_stime / HZ */,
6350 (double)0 /* t.tms_cutime / HZ */,
6351 (double)0 /* t.tms_cstime / HZ */,
6352 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006353}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006354#else /* not OS2 */
Martin v. Löwis03824e42008-12-29 18:17:34 +00006355#define NEED_TICKS_PER_SECOND
6356static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006357static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006358posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006359{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006360 struct tms t;
6361 clock_t c;
6362 errno = 0;
6363 c = times(&t);
6364 if (c == (clock_t) -1)
6365 return posix_error();
6366 return Py_BuildValue("ddddd",
6367 (double)t.tms_utime / ticks_per_second,
6368 (double)t.tms_stime / ticks_per_second,
6369 (double)t.tms_cutime / ticks_per_second,
6370 (double)t.tms_cstime / ticks_per_second,
6371 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006372}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006373#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006374#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006375
6376
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006377#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006378#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006379static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006380posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006381{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006382 FILETIME create, exit, kernel, user;
6383 HANDLE hProc;
6384 hProc = GetCurrentProcess();
6385 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6386 /* The fields of a FILETIME structure are the hi and lo part
6387 of a 64-bit value expressed in 100 nanosecond units.
6388 1e7 is one second in such units; 1e-7 the inverse.
6389 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6390 */
6391 return Py_BuildValue(
6392 "ddddd",
6393 (double)(user.dwHighDateTime*429.4967296 +
6394 user.dwLowDateTime*1e-7),
6395 (double)(kernel.dwHighDateTime*429.4967296 +
6396 kernel.dwLowDateTime*1e-7),
6397 (double)0,
6398 (double)0,
6399 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006400}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006401#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006402
6403#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006404PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006405"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006406Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006407#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006409
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006410#ifdef HAVE_GETSID
6411PyDoc_STRVAR(posix_getsid__doc__,
6412"getsid(pid) -> sid\n\n\
6413Call the system call getsid().");
6414
6415static PyObject *
6416posix_getsid(PyObject *self, PyObject *args)
6417{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006418 pid_t pid;
6419 int sid;
6420 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6421 return NULL;
6422 sid = getsid(pid);
6423 if (sid < 0)
6424 return posix_error();
6425 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006426}
6427#endif /* HAVE_GETSID */
6428
6429
Guido van Rossumb6775db1994-08-01 11:34:53 +00006430#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006431PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006432"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006433Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006434
Barry Warsaw53699e91996-12-10 23:23:01 +00006435static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006436posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006437{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006438 if (setsid() < 0)
6439 return posix_error();
6440 Py_INCREF(Py_None);
6441 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006442}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006443#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006444
Guido van Rossumb6775db1994-08-01 11:34:53 +00006445#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006446PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006447"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006448Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006449
Barry Warsaw53699e91996-12-10 23:23:01 +00006450static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006451posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006452{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006453 pid_t pid;
6454 int pgrp;
6455 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6456 return NULL;
6457 if (setpgid(pid, pgrp) < 0)
6458 return posix_error();
6459 Py_INCREF(Py_None);
6460 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006461}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006462#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006463
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006464
Guido van Rossumb6775db1994-08-01 11:34:53 +00006465#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006466PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006467"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006468Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006469
Barry Warsaw53699e91996-12-10 23:23:01 +00006470static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006471posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006472{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006473 int fd;
6474 pid_t pgid;
6475 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6476 return NULL;
6477 pgid = tcgetpgrp(fd);
6478 if (pgid < 0)
6479 return posix_error();
6480 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006481}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006482#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006483
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006484
Guido van Rossumb6775db1994-08-01 11:34:53 +00006485#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006486PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006487"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006488Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006489
Barry Warsaw53699e91996-12-10 23:23:01 +00006490static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006491posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006492{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006493 int fd;
6494 pid_t pgid;
6495 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6496 return NULL;
6497 if (tcsetpgrp(fd, pgid) < 0)
6498 return posix_error();
6499 Py_INCREF(Py_None);
6500 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006501}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006502#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006503
Guido van Rossum687dd131993-05-17 08:34:16 +00006504/* Functions acting on file descriptors */
6505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006506PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006507"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006508Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006509
Barry Warsaw53699e91996-12-10 23:23:01 +00006510static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006511posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006512{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006513 char *file = NULL;
6514 int flag;
6515 int mode = 0777;
6516 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006517
6518#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006519 PyUnicodeObject *po;
6520 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6521 Py_BEGIN_ALLOW_THREADS
6522 /* PyUnicode_AS_UNICODE OK without thread
6523 lock as it is a simple dereference. */
6524 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6525 Py_END_ALLOW_THREADS
6526 if (fd < 0)
6527 return posix_error();
6528 return PyInt_FromLong((long)fd);
6529 }
6530 /* Drop the argument parsing error as narrow strings
6531 are also valid. */
6532 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006533#endif
6534
Victor Stinnerd6f85422010-05-05 23:33:33 +00006535 if (!PyArg_ParseTuple(args, "eti|i",
6536 Py_FileSystemDefaultEncoding, &file,
6537 &flag, &mode))
6538 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006539
Victor Stinnerd6f85422010-05-05 23:33:33 +00006540 Py_BEGIN_ALLOW_THREADS
6541 fd = open(file, flag, mode);
6542 Py_END_ALLOW_THREADS
6543 if (fd < 0)
6544 return posix_error_with_allocated_filename(file);
6545 PyMem_Free(file);
6546 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006547}
6548
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006550PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006551"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006552Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006553
Barry Warsaw53699e91996-12-10 23:23:01 +00006554static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006555posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006556{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006557 int fd, res;
6558 if (!PyArg_ParseTuple(args, "i:close", &fd))
6559 return NULL;
6560 if (!_PyVerify_fd(fd))
6561 return posix_error();
6562 Py_BEGIN_ALLOW_THREADS
6563 res = close(fd);
6564 Py_END_ALLOW_THREADS
6565 if (res < 0)
6566 return posix_error();
6567 Py_INCREF(Py_None);
6568 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006569}
6570
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006571
Victor Stinnerd6f85422010-05-05 23:33:33 +00006572PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006573"closerange(fd_low, fd_high)\n\n\
6574Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6575
6576static PyObject *
6577posix_closerange(PyObject *self, PyObject *args)
6578{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006579 int fd_from, fd_to, i;
6580 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6581 return NULL;
6582 Py_BEGIN_ALLOW_THREADS
6583 for (i = fd_from; i < fd_to; i++)
6584 if (_PyVerify_fd(i))
6585 close(i);
6586 Py_END_ALLOW_THREADS
6587 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006588}
6589
6590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006591PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006592"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006593Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006594
Barry Warsaw53699e91996-12-10 23:23:01 +00006595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006596posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006597{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006598 int fd;
6599 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6600 return NULL;
6601 if (!_PyVerify_fd(fd))
6602 return posix_error();
6603 Py_BEGIN_ALLOW_THREADS
6604 fd = dup(fd);
6605 Py_END_ALLOW_THREADS
6606 if (fd < 0)
6607 return posix_error();
6608 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006609}
6610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006612PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006613"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006614Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006615
Barry Warsaw53699e91996-12-10 23:23:01 +00006616static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006617posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006618{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006619 int fd, fd2, res;
6620 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6621 return NULL;
6622 if (!_PyVerify_fd_dup2(fd, fd2))
6623 return posix_error();
6624 Py_BEGIN_ALLOW_THREADS
6625 res = dup2(fd, fd2);
6626 Py_END_ALLOW_THREADS
6627 if (res < 0)
6628 return posix_error();
6629 Py_INCREF(Py_None);
6630 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006631}
6632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006634PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006635"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006636Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006637
Barry Warsaw53699e91996-12-10 23:23:01 +00006638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006639posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006640{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006641 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006642#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006643 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006644#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006645 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006646#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006647 PyObject *posobj;
6648 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6649 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006650#ifdef SEEK_SET
Victor Stinnerd6f85422010-05-05 23:33:33 +00006651 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6652 switch (how) {
6653 case 0: how = SEEK_SET; break;
6654 case 1: how = SEEK_CUR; break;
6655 case 2: how = SEEK_END; break;
6656 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006657#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006658
6659#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006660 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006661#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006662 pos = PyLong_Check(posobj) ?
6663 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006664#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006665 if (PyErr_Occurred())
6666 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006667
Victor Stinnerd6f85422010-05-05 23:33:33 +00006668 if (!_PyVerify_fd(fd))
6669 return posix_error();
6670 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006671#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006672 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006673#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006674 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006675#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006676 Py_END_ALLOW_THREADS
6677 if (res < 0)
6678 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006679
6680#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006681 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006682#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006683 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006684#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006685}
6686
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006687
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006688PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006689"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006690Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006691
Barry Warsaw53699e91996-12-10 23:23:01 +00006692static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006693posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006694{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006695 int fd, size, n;
6696 PyObject *buffer;
6697 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6698 return NULL;
6699 if (size < 0) {
6700 errno = EINVAL;
6701 return posix_error();
6702 }
6703 buffer = PyString_FromStringAndSize((char *)NULL, size);
6704 if (buffer == NULL)
6705 return NULL;
Stefan Krahacaab2a2010-11-26 15:06:27 +00006706 if (!_PyVerify_fd(fd)) {
6707 Py_DECREF(buffer);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006708 return posix_error();
Stefan Krahacaab2a2010-11-26 15:06:27 +00006709 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00006710 Py_BEGIN_ALLOW_THREADS
6711 n = read(fd, PyString_AsString(buffer), size);
6712 Py_END_ALLOW_THREADS
6713 if (n < 0) {
6714 Py_DECREF(buffer);
6715 return posix_error();
6716 }
6717 if (n != size)
6718 _PyString_Resize(&buffer, n);
6719 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006720}
6721
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006723PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006724"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006725Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006726
Barry Warsaw53699e91996-12-10 23:23:01 +00006727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006728posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006729{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006730 Py_buffer pbuf;
6731 int fd;
Victor Stinner59729ff2011-07-05 11:28:19 +02006732 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006733
Victor Stinnerd6f85422010-05-05 23:33:33 +00006734 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6735 return NULL;
Stefan Krahacaab2a2010-11-26 15:06:27 +00006736 if (!_PyVerify_fd(fd)) {
6737 PyBuffer_Release(&pbuf);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006738 return posix_error();
Stefan Krahacaab2a2010-11-26 15:06:27 +00006739 }
Victor Stinner59729ff2011-07-05 11:28:19 +02006740 len = pbuf.len;
Victor Stinnerd6f85422010-05-05 23:33:33 +00006741 Py_BEGIN_ALLOW_THREADS
Victor Stinner59729ff2011-07-05 11:28:19 +02006742#if defined(MS_WIN64) || defined(MS_WINDOWS)
6743 if (len > INT_MAX)
6744 len = INT_MAX;
6745 size = write(fd, pbuf.buf, (int)len);
6746#else
6747 size = write(fd, pbuf.buf, len);
6748#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006749 Py_END_ALLOW_THREADS
Stefan Krahacaab2a2010-11-26 15:06:27 +00006750 PyBuffer_Release(&pbuf);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006751 if (size < 0)
6752 return posix_error();
6753 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006754}
6755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006757PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006758"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006759Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006760
Barry Warsaw53699e91996-12-10 23:23:01 +00006761static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006762posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006763{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006764 int fd;
6765 STRUCT_STAT st;
6766 int res;
6767 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6768 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006769#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006770 /* on OpenVMS we must ensure that all bytes are written to the file */
6771 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006772#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006773 if (!_PyVerify_fd(fd))
6774 return posix_error();
6775 Py_BEGIN_ALLOW_THREADS
6776 res = FSTAT(fd, &st);
6777 Py_END_ALLOW_THREADS
6778 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006779#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006780 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006781#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006782 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006783#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006784 }
Tim Peters5aa91602002-01-30 05:46:57 +00006785
Victor Stinnerd6f85422010-05-05 23:33:33 +00006786 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006787}
6788
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006790PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006791"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006792Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006793
Barry Warsaw53699e91996-12-10 23:23:01 +00006794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006795posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006796{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006797 int fd;
6798 char *orgmode = "r";
6799 int bufsize = -1;
6800 FILE *fp;
6801 PyObject *f;
6802 char *mode;
6803 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6804 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006805
Victor Stinnerd6f85422010-05-05 23:33:33 +00006806 /* Sanitize mode. See fileobject.c */
6807 mode = PyMem_MALLOC(strlen(orgmode)+3);
6808 if (!mode) {
6809 PyErr_NoMemory();
6810 return NULL;
6811 }
6812 strcpy(mode, orgmode);
6813 if (_PyFile_SanitizeMode(mode)) {
6814 PyMem_FREE(mode);
6815 return NULL;
6816 }
6817 if (!_PyVerify_fd(fd))
6818 return posix_error();
6819 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006820#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006821 if (mode[0] == 'a') {
6822 /* try to make sure the O_APPEND flag is set */
6823 int flags;
6824 flags = fcntl(fd, F_GETFL);
6825 if (flags != -1)
6826 fcntl(fd, F_SETFL, flags | O_APPEND);
6827 fp = fdopen(fd, mode);
6828 if (fp == NULL && flags != -1)
6829 /* restore old mode if fdopen failed */
6830 fcntl(fd, F_SETFL, flags);
6831 } else {
6832 fp = fdopen(fd, mode);
6833 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006834#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006835 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006836#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006837 Py_END_ALLOW_THREADS
6838 PyMem_FREE(mode);
6839 if (fp == NULL)
6840 return posix_error();
Nadeem Vawdad7664de2012-01-19 00:40:46 +02006841 /* The dummy filename used here must be kept in sync with the value
6842 tested against in gzip.GzipFile.__init__() - see issue #13781. */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006843 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6844 if (f != NULL)
6845 PyFile_SetBufSize(f, bufsize);
6846 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006847}
6848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006849PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006850"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006851Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006852connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006853
6854static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006855posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006856{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006857 int fd;
6858 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6859 return NULL;
6860 if (!_PyVerify_fd(fd))
6861 return PyBool_FromLong(0);
6862 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006863}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006864
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006865#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006866PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006867"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006868Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006869
Barry Warsaw53699e91996-12-10 23:23:01 +00006870static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006871posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006872{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006873#if defined(PYOS_OS2)
6874 HFILE read, write;
6875 APIRET rc;
6876
Victor Stinnerd6f85422010-05-05 23:33:33 +00006877 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006878 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006879 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006880 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006881 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006882
6883 return Py_BuildValue("(ii)", read, write);
6884#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006885#if !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006886 int fds[2];
6887 int res;
6888 Py_BEGIN_ALLOW_THREADS
6889 res = pipe(fds);
6890 Py_END_ALLOW_THREADS
6891 if (res != 0)
6892 return posix_error();
6893 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006894#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006895 HANDLE read, write;
6896 int read_fd, write_fd;
6897 BOOL ok;
6898 Py_BEGIN_ALLOW_THREADS
6899 ok = CreatePipe(&read, &write, NULL, 0);
6900 Py_END_ALLOW_THREADS
6901 if (!ok)
6902 return win32_error("CreatePipe", NULL);
6903 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6904 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6905 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006906#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006907#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006908}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006909#endif /* HAVE_PIPE */
6910
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006911
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006912#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006913PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006914"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006915Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006916
Barry Warsaw53699e91996-12-10 23:23:01 +00006917static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006918posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006919{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006920 char *filename;
6921 int mode = 0666;
6922 int res;
6923 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6924 return NULL;
6925 Py_BEGIN_ALLOW_THREADS
6926 res = mkfifo(filename, mode);
6927 Py_END_ALLOW_THREADS
6928 if (res < 0)
6929 return posix_error();
6930 Py_INCREF(Py_None);
6931 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006932}
6933#endif
6934
6935
Neal Norwitz11690112002-07-30 01:08:28 +00006936#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006937PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006938"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006939Create a filesystem node (file, device special file or named pipe)\n\
6940named filename. mode specifies both the permissions to use and the\n\
6941type of node to be created, being combined (bitwise OR) with one of\n\
6942S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006943device defines the newly created device special file (probably using\n\
6944os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006945
6946
6947static PyObject *
6948posix_mknod(PyObject *self, PyObject *args)
6949{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006950 char *filename;
6951 int mode = 0600;
6952 int device = 0;
6953 int res;
6954 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6955 return NULL;
6956 Py_BEGIN_ALLOW_THREADS
6957 res = mknod(filename, mode, device);
6958 Py_END_ALLOW_THREADS
6959 if (res < 0)
6960 return posix_error();
6961 Py_INCREF(Py_None);
6962 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006963}
6964#endif
6965
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006966#ifdef HAVE_DEVICE_MACROS
6967PyDoc_STRVAR(posix_major__doc__,
6968"major(device) -> major number\n\
6969Extracts a device major number from a raw device number.");
6970
6971static PyObject *
6972posix_major(PyObject *self, PyObject *args)
6973{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006974 int device;
6975 if (!PyArg_ParseTuple(args, "i:major", &device))
6976 return NULL;
6977 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006978}
6979
6980PyDoc_STRVAR(posix_minor__doc__,
6981"minor(device) -> minor number\n\
6982Extracts a device minor number from a raw device number.");
6983
6984static PyObject *
6985posix_minor(PyObject *self, PyObject *args)
6986{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006987 int device;
6988 if (!PyArg_ParseTuple(args, "i:minor", &device))
6989 return NULL;
6990 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006991}
6992
6993PyDoc_STRVAR(posix_makedev__doc__,
6994"makedev(major, minor) -> device number\n\
6995Composes a raw device number from the major and minor device numbers.");
6996
6997static PyObject *
6998posix_makedev(PyObject *self, PyObject *args)
6999{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007000 int major, minor;
7001 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7002 return NULL;
7003 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007004}
7005#endif /* device macros */
7006
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007007
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007008#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007009PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007010"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007011Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007012
Barry Warsaw53699e91996-12-10 23:23:01 +00007013static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007014posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007015{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007016 int fd;
7017 off_t length;
7018 int res;
7019 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007020
Victor Stinnerd6f85422010-05-05 23:33:33 +00007021 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
7022 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007023
7024#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007025 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007026#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007027 length = PyLong_Check(lenobj) ?
7028 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007029#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007030 if (PyErr_Occurred())
7031 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007032
Victor Stinnerd6f85422010-05-05 23:33:33 +00007033 Py_BEGIN_ALLOW_THREADS
7034 res = ftruncate(fd, length);
7035 Py_END_ALLOW_THREADS
7036 if (res < 0)
7037 return posix_error();
7038 Py_INCREF(Py_None);
7039 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007040}
7041#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007042
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007043#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007044PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007045"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007046Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007047
Fred Drake762e2061999-08-26 17:23:54 +00007048/* Save putenv() parameters as values here, so we can collect them when they
7049 * get re-set with another call for the same key. */
7050static PyObject *posix_putenv_garbage;
7051
Tim Peters5aa91602002-01-30 05:46:57 +00007052static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007053posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007054{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007055 char *s1, *s2;
7056 char *newenv;
7057 PyObject *newstr;
7058 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007059
Victor Stinnerd6f85422010-05-05 23:33:33 +00007060 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
7061 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007062
7063#if defined(PYOS_OS2)
7064 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7065 APIRET rc;
7066
Guido van Rossumd48f2521997-12-05 22:19:34 +00007067 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
7068 if (rc != NO_ERROR)
7069 return os2_error(rc);
7070
7071 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7072 APIRET rc;
7073
Guido van Rossumd48f2521997-12-05 22:19:34 +00007074 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
7075 if (rc != NO_ERROR)
7076 return os2_error(rc);
7077 } else {
7078#endif
7079
Victor Stinnerd6f85422010-05-05 23:33:33 +00007080 /* XXX This can leak memory -- not easy to fix :-( */
7081 len = strlen(s1) + strlen(s2) + 2;
Victor Stinner53853c32011-11-22 22:20:13 +01007082#ifdef MS_WINDOWS
7083 if (_MAX_ENV < (len - 1)) {
7084 PyErr_Format(PyExc_ValueError,
7085 "the environment variable is longer than %u bytes",
7086 _MAX_ENV);
7087 return NULL;
7088 }
7089#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007090 /* len includes space for a trailing \0; the size arg to
7091 PyString_FromStringAndSize does not count that */
7092 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
7093 if (newstr == NULL)
7094 return PyErr_NoMemory();
7095 newenv = PyString_AS_STRING(newstr);
7096 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7097 if (putenv(newenv)) {
7098 Py_DECREF(newstr);
7099 posix_error();
7100 return NULL;
7101 }
7102 /* Install the first arg and newstr in posix_putenv_garbage;
7103 * this will cause previous value to be collected. This has to
7104 * happen after the real putenv() call because the old value
7105 * was still accessible until then. */
7106 if (PyDict_SetItem(posix_putenv_garbage,
7107 PyTuple_GET_ITEM(args, 0), newstr)) {
7108 /* really not much we can do; just leak */
7109 PyErr_Clear();
7110 }
7111 else {
7112 Py_DECREF(newstr);
7113 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007114
7115#if defined(PYOS_OS2)
7116 }
7117#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007118 Py_INCREF(Py_None);
7119 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007120}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007121#endif /* putenv */
7122
Guido van Rossumc524d952001-10-19 01:31:59 +00007123#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007124PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007125"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007126Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007127
7128static PyObject *
7129posix_unsetenv(PyObject *self, PyObject *args)
7130{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007131 char *s1;
Charles-François Natali93a11752011-11-27 13:01:35 +01007132#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner53853c32011-11-22 22:20:13 +01007133 int err;
Charles-François Natali93a11752011-11-27 13:01:35 +01007134#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007135
Victor Stinnerd6f85422010-05-05 23:33:33 +00007136 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7137 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007138
Charles-François Natali93a11752011-11-27 13:01:35 +01007139#ifdef HAVE_BROKEN_UNSETENV
7140 unsetenv(s1);
7141#else
Victor Stinner53853c32011-11-22 22:20:13 +01007142 err = unsetenv(s1);
Benjamin Peterson42d96dc2011-11-22 23:56:06 -06007143 if (err)
Victor Stinner53853c32011-11-22 22:20:13 +01007144 return posix_error();
Charles-François Natali93a11752011-11-27 13:01:35 +01007145#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007146
Victor Stinnerd6f85422010-05-05 23:33:33 +00007147 /* Remove the key from posix_putenv_garbage;
7148 * this will cause it to be collected. This has to
7149 * happen after the real unsetenv() call because the
7150 * old value was still accessible until then.
7151 */
7152 if (PyDict_DelItem(posix_putenv_garbage,
7153 PyTuple_GET_ITEM(args, 0))) {
7154 /* really not much we can do; just leak */
7155 PyErr_Clear();
7156 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007157
Victor Stinnerd6f85422010-05-05 23:33:33 +00007158 Py_INCREF(Py_None);
7159 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00007160}
7161#endif /* unsetenv */
7162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007163PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007164"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007165Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007166
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007167static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007168posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007169{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007170 int code;
7171 char *message;
7172 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7173 return NULL;
7174 message = strerror(code);
7175 if (message == NULL) {
7176 PyErr_SetString(PyExc_ValueError,
7177 "strerror() argument out of range");
7178 return NULL;
7179 }
7180 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007181}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007182
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007183
Guido van Rossumc9641791998-08-04 15:26:23 +00007184#ifdef HAVE_SYS_WAIT_H
7185
Fred Drake106c1a02002-04-23 15:58:02 +00007186#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007187PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007188"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007189Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007190
7191static PyObject *
7192posix_WCOREDUMP(PyObject *self, PyObject *args)
7193{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007194 WAIT_TYPE status;
7195 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007196
Victor Stinnerd6f85422010-05-05 23:33:33 +00007197 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7198 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007199
Victor Stinnerd6f85422010-05-05 23:33:33 +00007200 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007201}
7202#endif /* WCOREDUMP */
7203
7204#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007205PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007206"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007207Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007208job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007209
7210static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007211posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007212{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007213 WAIT_TYPE status;
7214 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007215
Victor Stinnerd6f85422010-05-05 23:33:33 +00007216 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7217 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007218
Victor Stinnerd6f85422010-05-05 23:33:33 +00007219 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007220}
7221#endif /* WIFCONTINUED */
7222
Guido van Rossumc9641791998-08-04 15:26:23 +00007223#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007224PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007225"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007226Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007227
7228static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007229posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007230{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007231 WAIT_TYPE status;
7232 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007233
Victor Stinnerd6f85422010-05-05 23:33:33 +00007234 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7235 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007236
Victor Stinnerd6f85422010-05-05 23:33:33 +00007237 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007238}
7239#endif /* WIFSTOPPED */
7240
7241#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007242PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007243"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007244Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007245
7246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007247posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007248{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007249 WAIT_TYPE status;
7250 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007251
Victor Stinnerd6f85422010-05-05 23:33:33 +00007252 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7253 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007254
Victor Stinnerd6f85422010-05-05 23:33:33 +00007255 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007256}
7257#endif /* WIFSIGNALED */
7258
7259#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007260PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007261"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007262Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007263system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007264
7265static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007266posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007267{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007268 WAIT_TYPE status;
7269 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007270
Victor Stinnerd6f85422010-05-05 23:33:33 +00007271 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7272 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007273
Victor Stinnerd6f85422010-05-05 23:33:33 +00007274 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007275}
7276#endif /* WIFEXITED */
7277
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007278#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007279PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007280"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007281Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007282
7283static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007284posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007285{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007286 WAIT_TYPE status;
7287 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007288
Victor Stinnerd6f85422010-05-05 23:33:33 +00007289 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7290 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007291
Victor Stinnerd6f85422010-05-05 23:33:33 +00007292 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007293}
7294#endif /* WEXITSTATUS */
7295
7296#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007297PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007298"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007299Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007300value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007301
7302static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007303posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007304{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007305 WAIT_TYPE status;
7306 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007307
Victor Stinnerd6f85422010-05-05 23:33:33 +00007308 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7309 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007310
Victor Stinnerd6f85422010-05-05 23:33:33 +00007311 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007312}
7313#endif /* WTERMSIG */
7314
7315#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007316PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007317"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007318Return the signal that stopped the process that provided\n\
7319the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007320
7321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007322posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007323{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007324 WAIT_TYPE status;
7325 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007326
Victor Stinnerd6f85422010-05-05 23:33:33 +00007327 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7328 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007329
Victor Stinnerd6f85422010-05-05 23:33:33 +00007330 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007331}
7332#endif /* WSTOPSIG */
7333
7334#endif /* HAVE_SYS_WAIT_H */
7335
7336
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007337#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007338#ifdef _SCO_DS
7339/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7340 needed definitions in sys/statvfs.h */
7341#define _SVID3
7342#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007343#include <sys/statvfs.h>
7344
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007345static PyObject*
7346_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007347 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7348 if (v == NULL)
7349 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007350
7351#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007352 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7353 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7354 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7355 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7356 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7357 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7358 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7359 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7360 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7361 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007362#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007363 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7364 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7365 PyStructSequence_SET_ITEM(v, 2,
7366 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7367 PyStructSequence_SET_ITEM(v, 3,
7368 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7369 PyStructSequence_SET_ITEM(v, 4,
7370 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7371 PyStructSequence_SET_ITEM(v, 5,
7372 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7373 PyStructSequence_SET_ITEM(v, 6,
7374 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7375 PyStructSequence_SET_ITEM(v, 7,
7376 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7377 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7378 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007379#endif
7380
Victor Stinnerd6f85422010-05-05 23:33:33 +00007381 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007382}
7383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007384PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007385"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007386Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007387
7388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007389posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007390{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007391 int fd, res;
7392 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007393
Victor Stinnerd6f85422010-05-05 23:33:33 +00007394 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7395 return NULL;
7396 Py_BEGIN_ALLOW_THREADS
7397 res = fstatvfs(fd, &st);
7398 Py_END_ALLOW_THREADS
7399 if (res != 0)
7400 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007401
Victor Stinnerd6f85422010-05-05 23:33:33 +00007402 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007403}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007404#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007405
7406
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007407#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007408#include <sys/statvfs.h>
7409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007410PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007411"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007412Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007413
7414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007415posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007416{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007417 char *path;
7418 int res;
7419 struct statvfs st;
7420 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7421 return NULL;
7422 Py_BEGIN_ALLOW_THREADS
7423 res = statvfs(path, &st);
7424 Py_END_ALLOW_THREADS
7425 if (res != 0)
7426 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007427
Victor Stinnerd6f85422010-05-05 23:33:33 +00007428 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007429}
7430#endif /* HAVE_STATVFS */
7431
7432
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007433#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007434PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007435"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007436Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007437The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007438or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007439
7440static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007441posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007442{
7443 PyObject *result = NULL;
7444 char *dir = NULL;
7445 char *pfx = NULL;
7446 char *name;
7447
7448 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinnerd6f85422010-05-05 23:33:33 +00007449 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007450
7451 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007452 "tempnam is a potential security risk to your program") < 0)
7453 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007454
Antoine Pitroub0614612011-01-02 20:04:52 +00007455 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
7456 "use the tempfile module", 1) < 0)
7457 return NULL;
7458
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007459#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007460 name = _tempnam(dir, pfx);
7461#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007462 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007463#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007464 if (name == NULL)
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007465 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007466 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007467 free(name);
7468 return result;
7469}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007470#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007471
7472
7473#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007474PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007475"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007476Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007477
7478static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007479posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007480{
7481 FILE *fp;
7482
Antoine Pitroub0614612011-01-02 20:04:52 +00007483 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
7484 "use the tempfile module", 1) < 0)
7485 return NULL;
7486
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007487 fp = tmpfile();
7488 if (fp == NULL)
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007489 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007490 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007491}
7492#endif
7493
7494
7495#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007496PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007497"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007498Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007499
7500static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007501posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007502{
7503 char buffer[L_tmpnam];
7504 char *name;
7505
Skip Montanaro95618b52001-08-18 18:52:10 +00007506 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007507 "tmpnam is a potential security risk to your program") < 0)
7508 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007509
Antoine Pitroub0614612011-01-02 20:04:52 +00007510 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
7511 "use the tempfile module", 1) < 0)
7512 return NULL;
7513
Greg Wardb48bc172000-03-01 21:51:56 +00007514#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007515 name = tmpnam_r(buffer);
7516#else
7517 name = tmpnam(buffer);
7518#endif
7519 if (name == NULL) {
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007520 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007521#ifdef USE_TMPNAM_R
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007522 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007523#else
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007524 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007525#endif
Antoine Pitrouda4a5f02011-01-02 20:06:12 +00007526 );
7527 PyErr_SetObject(PyExc_OSError, err);
7528 Py_XDECREF(err);
7529 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007530 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007531 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007532}
7533#endif
7534
7535
Fred Drakec9680921999-12-13 16:37:25 +00007536/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7537 * It maps strings representing configuration variable names to
7538 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007539 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007540 * rarely-used constants. There are three separate tables that use
7541 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007542 *
7543 * This code is always included, even if none of the interfaces that
7544 * need it are included. The #if hackery needed to avoid it would be
7545 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007546 */
7547struct constdef {
7548 char *name;
7549 long value;
7550};
7551
Fred Drake12c6e2d1999-12-14 21:25:03 +00007552static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007553conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007554 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007555{
7556 if (PyInt_Check(arg)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007557 *valuep = PyInt_AS_LONG(arg);
7558 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007559 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007560 if (PyString_Check(arg)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007561 /* look up the value in the table using a binary search */
7562 size_t lo = 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00007563 size_t mid;
Stefan Krah93f7a322010-11-26 17:35:50 +00007564 size_t hi = tablesize;
7565 int cmp;
7566 char *confname = PyString_AS_STRING(arg);
7567 while (lo < hi) {
7568 mid = (lo + hi) / 2;
7569 cmp = strcmp(confname, table[mid].name);
7570 if (cmp < 0)
7571 hi = mid;
7572 else if (cmp > 0)
7573 lo = mid + 1;
7574 else {
7575 *valuep = table[mid].value;
7576 return 1;
7577 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007578 }
Stefan Krah93f7a322010-11-26 17:35:50 +00007579 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007580 }
7581 else
Stefan Krah93f7a322010-11-26 17:35:50 +00007582 PyErr_SetString(PyExc_TypeError,
7583 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007584 return 0;
7585}
7586
7587
7588#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7589static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007590#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007591 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007592#endif
7593#ifdef _PC_ABI_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007594 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007595#endif
Fred Drakec9680921999-12-13 16:37:25 +00007596#ifdef _PC_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007597 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007598#endif
7599#ifdef _PC_CHOWN_RESTRICTED
Victor Stinnerd6f85422010-05-05 23:33:33 +00007600 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007601#endif
7602#ifdef _PC_FILESIZEBITS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007603 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007604#endif
7605#ifdef _PC_LAST
Victor Stinnerd6f85422010-05-05 23:33:33 +00007606 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007607#endif
7608#ifdef _PC_LINK_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007609 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007610#endif
7611#ifdef _PC_MAX_CANON
Victor Stinnerd6f85422010-05-05 23:33:33 +00007612 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007613#endif
7614#ifdef _PC_MAX_INPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007615 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007616#endif
7617#ifdef _PC_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007618 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007619#endif
7620#ifdef _PC_NO_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007621 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007622#endif
7623#ifdef _PC_PATH_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007624 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007625#endif
7626#ifdef _PC_PIPE_BUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007627 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007628#endif
7629#ifdef _PC_PRIO_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007630 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007631#endif
7632#ifdef _PC_SOCK_MAXBUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007633 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007634#endif
7635#ifdef _PC_SYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007636 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007637#endif
7638#ifdef _PC_VDISABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007639 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007640#endif
7641};
7642
Fred Drakec9680921999-12-13 16:37:25 +00007643static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007644conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007645{
7646 return conv_confname(arg, valuep, posix_constants_pathconf,
7647 sizeof(posix_constants_pathconf)
7648 / sizeof(struct constdef));
7649}
7650#endif
7651
7652#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007653PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007654"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007655Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007656If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007657
7658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007659posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007660{
7661 PyObject *result = NULL;
7662 int name, fd;
7663
Fred Drake12c6e2d1999-12-14 21:25:03 +00007664 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7665 conv_path_confname, &name)) {
Stefan Krah93f7a322010-11-26 17:35:50 +00007666 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007667
Stefan Krah93f7a322010-11-26 17:35:50 +00007668 errno = 0;
7669 limit = fpathconf(fd, name);
7670 if (limit == -1 && errno != 0)
7671 posix_error();
7672 else
7673 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007674 }
7675 return result;
7676}
7677#endif
7678
7679
7680#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007681PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007682"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007683Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007684If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007685
7686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007687posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007688{
7689 PyObject *result = NULL;
7690 int name;
7691 char *path;
7692
7693 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7694 conv_path_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007695 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007696
Victor Stinnerd6f85422010-05-05 23:33:33 +00007697 errno = 0;
7698 limit = pathconf(path, name);
7699 if (limit == -1 && errno != 0) {
7700 if (errno == EINVAL)
Stefan Krahacaab2a2010-11-26 15:06:27 +00007701 /* could be a path or name problem */
7702 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007703 else
Stefan Krahacaab2a2010-11-26 15:06:27 +00007704 posix_error_with_filename(path);
Victor Stinnerd6f85422010-05-05 23:33:33 +00007705 }
7706 else
7707 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007708 }
7709 return result;
7710}
7711#endif
7712
7713#ifdef HAVE_CONFSTR
7714static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007715#ifdef _CS_ARCHITECTURE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007716 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007717#endif
7718#ifdef _CS_HOSTNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007719 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007720#endif
7721#ifdef _CS_HW_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007722 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007723#endif
7724#ifdef _CS_HW_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007725 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007726#endif
7727#ifdef _CS_INITTAB_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007728 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007729#endif
Fred Drakec9680921999-12-13 16:37:25 +00007730#ifdef _CS_LFS64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007731 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007732#endif
7733#ifdef _CS_LFS64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007734 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007735#endif
7736#ifdef _CS_LFS64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007737 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007738#endif
7739#ifdef _CS_LFS64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007740 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007741#endif
7742#ifdef _CS_LFS_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007743 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007744#endif
7745#ifdef _CS_LFS_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007746 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007747#endif
7748#ifdef _CS_LFS_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007749 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007750#endif
7751#ifdef _CS_LFS_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007752 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007753#endif
Fred Draked86ed291999-12-15 15:34:33 +00007754#ifdef _CS_MACHINE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007755 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007756#endif
Fred Drakec9680921999-12-13 16:37:25 +00007757#ifdef _CS_PATH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007758 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007759#endif
Fred Draked86ed291999-12-15 15:34:33 +00007760#ifdef _CS_RELEASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007761 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007762#endif
7763#ifdef _CS_SRPC_DOMAIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007764 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007765#endif
7766#ifdef _CS_SYSNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007767 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007768#endif
7769#ifdef _CS_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007770 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007771#endif
Fred Drakec9680921999-12-13 16:37:25 +00007772#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007773 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007774#endif
7775#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007776 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007777#endif
7778#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007779 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007780#endif
7781#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007782 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007783#endif
7784#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007785 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007786#endif
7787#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007788 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007789#endif
7790#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007791 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007792#endif
7793#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007794 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007795#endif
7796#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007797 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007798#endif
7799#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007800 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007801#endif
7802#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007803 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007804#endif
7805#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007806 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007807#endif
7808#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007809 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007810#endif
7811#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007812 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007813#endif
7814#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007815 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007816#endif
7817#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007818 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007819#endif
Fred Draked86ed291999-12-15 15:34:33 +00007820#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007821 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007822#endif
7823#ifdef _MIPS_CS_BASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007824 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007825#endif
7826#ifdef _MIPS_CS_HOSTID
Victor Stinnerd6f85422010-05-05 23:33:33 +00007827 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007828#endif
7829#ifdef _MIPS_CS_HW_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007830 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007831#endif
7832#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007833 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007834#endif
7835#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007836 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007837#endif
7838#ifdef _MIPS_CS_OSREL_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007839 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007840#endif
7841#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007842 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007843#endif
7844#ifdef _MIPS_CS_OS_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007845 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007846#endif
7847#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007848 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007849#endif
7850#ifdef _MIPS_CS_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007851 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007852#endif
7853#ifdef _MIPS_CS_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007854 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007855#endif
7856#ifdef _MIPS_CS_VENDOR
Victor Stinnerd6f85422010-05-05 23:33:33 +00007857 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007858#endif
Fred Drakec9680921999-12-13 16:37:25 +00007859};
7860
7861static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007862conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007863{
7864 return conv_confname(arg, valuep, posix_constants_confstr,
7865 sizeof(posix_constants_confstr)
7866 / sizeof(struct constdef));
7867}
7868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007869PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007870"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007871Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007872
7873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007874posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007875{
7876 PyObject *result = NULL;
7877 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007878 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007879
7880 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007881 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007882
Victor Stinnerd6f85422010-05-05 23:33:33 +00007883 errno = 0;
7884 len = confstr(name, buffer, sizeof(buffer));
7885 if (len == 0) {
7886 if (errno) {
7887 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007888 }
7889 else {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007890 result = Py_None;
7891 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007892 }
7893 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00007894 else {
7895 if ((unsigned int)len >= sizeof(buffer)) {
7896 result = PyString_FromStringAndSize(NULL, len-1);
7897 if (result != NULL)
7898 confstr(name, PyString_AS_STRING(result), len);
7899 }
7900 else
7901 result = PyString_FromStringAndSize(buffer, len-1);
7902 }
7903 }
Fred Drakec9680921999-12-13 16:37:25 +00007904 return result;
7905}
7906#endif
7907
7908
7909#ifdef HAVE_SYSCONF
7910static struct constdef posix_constants_sysconf[] = {
7911#ifdef _SC_2_CHAR_TERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00007912 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007913#endif
7914#ifdef _SC_2_C_BIND
Victor Stinnerd6f85422010-05-05 23:33:33 +00007915 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007916#endif
7917#ifdef _SC_2_C_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007918 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007919#endif
7920#ifdef _SC_2_C_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007921 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007922#endif
7923#ifdef _SC_2_FORT_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007924 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007925#endif
7926#ifdef _SC_2_FORT_RUN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007927 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007928#endif
7929#ifdef _SC_2_LOCALEDEF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007930 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007931#endif
7932#ifdef _SC_2_SW_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007933 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007934#endif
7935#ifdef _SC_2_UPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007936 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007937#endif
7938#ifdef _SC_2_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007939 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007940#endif
Fred Draked86ed291999-12-15 15:34:33 +00007941#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007942 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007943#endif
7944#ifdef _SC_ACL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007945 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007946#endif
Fred Drakec9680921999-12-13 16:37:25 +00007947#ifdef _SC_AIO_LISTIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007948 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007949#endif
Fred Drakec9680921999-12-13 16:37:25 +00007950#ifdef _SC_AIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007951 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007952#endif
7953#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007954 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007955#endif
7956#ifdef _SC_ARG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007957 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007958#endif
7959#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007960 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007961#endif
7962#ifdef _SC_ATEXIT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007963 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007964#endif
Fred Draked86ed291999-12-15 15:34:33 +00007965#ifdef _SC_AUDIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007966 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007967#endif
Fred Drakec9680921999-12-13 16:37:25 +00007968#ifdef _SC_AVPHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00007969 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007970#endif
7971#ifdef _SC_BC_BASE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007972 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007973#endif
7974#ifdef _SC_BC_DIM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007975 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007976#endif
7977#ifdef _SC_BC_SCALE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007978 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007979#endif
7980#ifdef _SC_BC_STRING_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007981 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007982#endif
Fred Draked86ed291999-12-15 15:34:33 +00007983#ifdef _SC_CAP
Victor Stinnerd6f85422010-05-05 23:33:33 +00007984 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007985#endif
Fred Drakec9680921999-12-13 16:37:25 +00007986#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007987 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007988#endif
7989#ifdef _SC_CHAR_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007990 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007991#endif
7992#ifdef _SC_CHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007993 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007994#endif
7995#ifdef _SC_CHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007996 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007997#endif
7998#ifdef _SC_CHILD_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007999 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008000#endif
8001#ifdef _SC_CLK_TCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008002 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008003#endif
8004#ifdef _SC_COHER_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008005 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008006#endif
8007#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008008 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008009#endif
8010#ifdef _SC_DCACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008011 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008012#endif
8013#ifdef _SC_DCACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008014 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008015#endif
8016#ifdef _SC_DCACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008017 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008018#endif
8019#ifdef _SC_DCACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008020 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008021#endif
8022#ifdef _SC_DCACHE_TBLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008023 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008024#endif
8025#ifdef _SC_DELAYTIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008026 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008027#endif
8028#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008029 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008030#endif
8031#ifdef _SC_EXPR_NEST_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008032 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008033#endif
8034#ifdef _SC_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008035 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008036#endif
8037#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008038 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008039#endif
8040#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008041 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008042#endif
8043#ifdef _SC_ICACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008044 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008045#endif
8046#ifdef _SC_ICACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008047 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008048#endif
8049#ifdef _SC_ICACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008050 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008051#endif
8052#ifdef _SC_ICACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00008053 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008054#endif
Fred Draked86ed291999-12-15 15:34:33 +00008055#ifdef _SC_INF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008056 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008057#endif
Fred Drakec9680921999-12-13 16:37:25 +00008058#ifdef _SC_INT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008059 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008060#endif
8061#ifdef _SC_INT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008062 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008063#endif
8064#ifdef _SC_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008065 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008066#endif
Fred Draked86ed291999-12-15 15:34:33 +00008067#ifdef _SC_IP_SECOPTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008068 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008069#endif
Fred Drakec9680921999-12-13 16:37:25 +00008070#ifdef _SC_JOB_CONTROL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008071 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008072#endif
Fred Draked86ed291999-12-15 15:34:33 +00008073#ifdef _SC_KERN_POINTERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008074 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008075#endif
8076#ifdef _SC_KERN_SIM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008077 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008078#endif
Fred Drakec9680921999-12-13 16:37:25 +00008079#ifdef _SC_LINE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008080 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008081#endif
8082#ifdef _SC_LOGIN_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008083 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008084#endif
8085#ifdef _SC_LOGNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008086 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008087#endif
8088#ifdef _SC_LONG_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008089 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008090#endif
Fred Draked86ed291999-12-15 15:34:33 +00008091#ifdef _SC_MAC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008092 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008093#endif
Fred Drakec9680921999-12-13 16:37:25 +00008094#ifdef _SC_MAPPED_FILES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008095 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008096#endif
8097#ifdef _SC_MAXPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008098 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008099#endif
8100#ifdef _SC_MB_LEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008101 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008102#endif
8103#ifdef _SC_MEMLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008104 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008105#endif
8106#ifdef _SC_MEMLOCK_RANGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008107 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008108#endif
8109#ifdef _SC_MEMORY_PROTECTION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008110 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008111#endif
8112#ifdef _SC_MESSAGE_PASSING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008113 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008114#endif
Fred Draked86ed291999-12-15 15:34:33 +00008115#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008116 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008117#endif
Fred Drakec9680921999-12-13 16:37:25 +00008118#ifdef _SC_MQ_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008119 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008120#endif
8121#ifdef _SC_MQ_PRIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008122 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008123#endif
Fred Draked86ed291999-12-15 15:34:33 +00008124#ifdef _SC_NACLS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008125 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008126#endif
Fred Drakec9680921999-12-13 16:37:25 +00008127#ifdef _SC_NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008128 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008129#endif
8130#ifdef _SC_NL_ARGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008131 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008132#endif
8133#ifdef _SC_NL_LANGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008134 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008135#endif
8136#ifdef _SC_NL_MSGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008137 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008138#endif
8139#ifdef _SC_NL_NMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008140 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008141#endif
8142#ifdef _SC_NL_SETMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008143 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008144#endif
8145#ifdef _SC_NL_TEXTMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008146 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008147#endif
8148#ifdef _SC_NPROCESSORS_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008149 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008150#endif
8151#ifdef _SC_NPROCESSORS_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008152 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008153#endif
Fred Draked86ed291999-12-15 15:34:33 +00008154#ifdef _SC_NPROC_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008155 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008156#endif
8157#ifdef _SC_NPROC_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008158 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008159#endif
Fred Drakec9680921999-12-13 16:37:25 +00008160#ifdef _SC_NZERO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008161 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008162#endif
8163#ifdef _SC_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008164 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008165#endif
8166#ifdef _SC_PAGESIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008167 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008168#endif
8169#ifdef _SC_PAGE_SIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008170 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008171#endif
8172#ifdef _SC_PASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008173 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008174#endif
8175#ifdef _SC_PHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008176 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008177#endif
8178#ifdef _SC_PII
Victor Stinnerd6f85422010-05-05 23:33:33 +00008179 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008180#endif
8181#ifdef _SC_PII_INTERNET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008182 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008183#endif
8184#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008185 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008186#endif
8187#ifdef _SC_PII_INTERNET_STREAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008188 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008189#endif
8190#ifdef _SC_PII_OSI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008191 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008192#endif
8193#ifdef _SC_PII_OSI_CLTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008194 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008195#endif
8196#ifdef _SC_PII_OSI_COTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008197 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008198#endif
8199#ifdef _SC_PII_OSI_M
Victor Stinnerd6f85422010-05-05 23:33:33 +00008200 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008201#endif
8202#ifdef _SC_PII_SOCKET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008203 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008204#endif
8205#ifdef _SC_PII_XTI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008206 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008207#endif
8208#ifdef _SC_POLL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008209 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008210#endif
8211#ifdef _SC_PRIORITIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008212 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008213#endif
8214#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008215 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008216#endif
8217#ifdef _SC_REALTIME_SIGNALS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008218 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008219#endif
8220#ifdef _SC_RE_DUP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008221 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008222#endif
8223#ifdef _SC_RTSIG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008224 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008225#endif
8226#ifdef _SC_SAVED_IDS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008227 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008228#endif
8229#ifdef _SC_SCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008230 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008231#endif
8232#ifdef _SC_SCHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008233 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008234#endif
8235#ifdef _SC_SELECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008236 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008237#endif
8238#ifdef _SC_SEMAPHORES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008239 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008240#endif
8241#ifdef _SC_SEM_NSEMS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008242 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008243#endif
8244#ifdef _SC_SEM_VALUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008245 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008246#endif
8247#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008248 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008249#endif
8250#ifdef _SC_SHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008251 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008252#endif
8253#ifdef _SC_SHRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008254 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008255#endif
8256#ifdef _SC_SIGQUEUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008257 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008258#endif
8259#ifdef _SC_SIGRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008260 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008261#endif
8262#ifdef _SC_SIGRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008263 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008264#endif
Fred Draked86ed291999-12-15 15:34:33 +00008265#ifdef _SC_SOFTPOWER
Victor Stinnerd6f85422010-05-05 23:33:33 +00008266 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008267#endif
Fred Drakec9680921999-12-13 16:37:25 +00008268#ifdef _SC_SPLIT_CACHE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008269 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008270#endif
8271#ifdef _SC_SSIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008272 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008273#endif
8274#ifdef _SC_STACK_PROT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008275 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008276#endif
8277#ifdef _SC_STREAM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008278 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008279#endif
8280#ifdef _SC_SYNCHRONIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008281 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008282#endif
8283#ifdef _SC_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008284 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008285#endif
8286#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008287 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008288#endif
8289#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008290 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008291#endif
8292#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008293 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008294#endif
8295#ifdef _SC_THREAD_KEYS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008296 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008297#endif
8298#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008299 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008300#endif
8301#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008302 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008303#endif
8304#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008305 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008306#endif
8307#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008308 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008309#endif
8310#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008311 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008312#endif
8313#ifdef _SC_THREAD_STACK_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008314 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008315#endif
8316#ifdef _SC_THREAD_THREADS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008317 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008318#endif
8319#ifdef _SC_TIMERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008320 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008321#endif
8322#ifdef _SC_TIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008323 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008324#endif
8325#ifdef _SC_TTY_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008326 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008327#endif
8328#ifdef _SC_TZNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008329 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008330#endif
8331#ifdef _SC_T_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008332 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008333#endif
8334#ifdef _SC_UCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008335 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008336#endif
8337#ifdef _SC_UINT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008338 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008339#endif
8340#ifdef _SC_UIO_MAXIOV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008341 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008342#endif
8343#ifdef _SC_ULONG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008344 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008345#endif
8346#ifdef _SC_USHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008347 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008348#endif
8349#ifdef _SC_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008350 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008351#endif
8352#ifdef _SC_WORD_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008353 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008354#endif
8355#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinnerd6f85422010-05-05 23:33:33 +00008356 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008357#endif
8358#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008359 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008360#endif
8361#ifdef _SC_XBS5_LP64_OFF64
Victor Stinnerd6f85422010-05-05 23:33:33 +00008362 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008363#endif
8364#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008365 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008366#endif
8367#ifdef _SC_XOPEN_CRYPT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008368 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008369#endif
8370#ifdef _SC_XOPEN_ENH_I18N
Victor Stinnerd6f85422010-05-05 23:33:33 +00008371 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008372#endif
8373#ifdef _SC_XOPEN_LEGACY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008374 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008375#endif
8376#ifdef _SC_XOPEN_REALTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008377 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008378#endif
8379#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008380 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008381#endif
8382#ifdef _SC_XOPEN_SHM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008383 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008384#endif
8385#ifdef _SC_XOPEN_UNIX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008386 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008387#endif
8388#ifdef _SC_XOPEN_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008389 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008390#endif
8391#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008392 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008393#endif
8394#ifdef _SC_XOPEN_XPG2
Victor Stinnerd6f85422010-05-05 23:33:33 +00008395 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008396#endif
8397#ifdef _SC_XOPEN_XPG3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008398 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008399#endif
8400#ifdef _SC_XOPEN_XPG4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008401 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008402#endif
8403};
8404
8405static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008406conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008407{
8408 return conv_confname(arg, valuep, posix_constants_sysconf,
8409 sizeof(posix_constants_sysconf)
8410 / sizeof(struct constdef));
8411}
8412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008413PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008414"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008415Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008416
8417static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008418posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008419{
8420 PyObject *result = NULL;
8421 int name;
8422
8423 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner862490a2010-05-06 00:03:44 +00008424 int value;
Fred Drakec9680921999-12-13 16:37:25 +00008425
Victor Stinner862490a2010-05-06 00:03:44 +00008426 errno = 0;
8427 value = sysconf(name);
8428 if (value == -1 && errno != 0)
8429 posix_error();
8430 else
8431 result = PyInt_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008432 }
8433 return result;
8434}
8435#endif
8436
8437
Fred Drakebec628d1999-12-15 18:31:10 +00008438/* This code is used to ensure that the tables of configuration value names
8439 * are in sorted order as required by conv_confname(), and also to build the
8440 * the exported dictionaries that are used to publish information about the
8441 * names available on the host platform.
8442 *
8443 * Sorting the table at runtime ensures that the table is properly ordered
8444 * when used, even for platforms we're not able to test on. It also makes
8445 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008446 */
Fred Drakebec628d1999-12-15 18:31:10 +00008447
8448static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008449cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008450{
8451 const struct constdef *c1 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008452 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008453 const struct constdef *c2 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008454 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008455
8456 return strcmp(c1->name, c2->name);
8457}
8458
8459static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008460setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinnerd6f85422010-05-05 23:33:33 +00008461 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008462{
Fred Drakebec628d1999-12-15 18:31:10 +00008463 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008464 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008465
8466 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8467 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008468 if (d == NULL)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008469 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008470
Barry Warsaw3155db32000-04-13 15:20:40 +00008471 for (i=0; i < tablesize; ++i) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008472 PyObject *o = PyInt_FromLong(table[i].value);
8473 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8474 Py_XDECREF(o);
8475 Py_DECREF(d);
8476 return -1;
8477 }
8478 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008479 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008480 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008481}
8482
Fred Drakebec628d1999-12-15 18:31:10 +00008483/* Return -1 on failure, 0 on success. */
8484static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008485setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008486{
8487#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008488 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008489 sizeof(posix_constants_pathconf)
8490 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008491 "pathconf_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008492 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008493#endif
8494#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008495 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008496 sizeof(posix_constants_confstr)
8497 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008498 "confstr_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008499 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008500#endif
8501#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008502 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008503 sizeof(posix_constants_sysconf)
8504 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008505 "sysconf_names", module))
Stefan Krah93f7a322010-11-26 17:35:50 +00008506 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008507#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008508 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008509}
Fred Draked86ed291999-12-15 15:34:33 +00008510
8511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008512PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008513"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008514Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008515in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008516
8517static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008518posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008519{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008520 abort();
8521 /*NOTREACHED*/
8522 Py_FatalError("abort() called from Python code didn't abort!");
8523 return NULL;
8524}
Fred Drakebec628d1999-12-15 18:31:10 +00008525
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008526#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008527PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008528"startfile(filepath [, operation]) - Start a file with its associated\n\
8529application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008530\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008531When \"operation\" is not specified or \"open\", this acts like\n\
8532double-clicking the file in Explorer, or giving the file name as an\n\
8533argument to the DOS \"start\" command: the file is opened with whatever\n\
8534application (if any) its extension is associated.\n\
8535When another \"operation\" is given, it specifies what should be done with\n\
8536the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008537\n\
8538startfile returns as soon as the associated application is launched.\n\
8539There is no option to wait for the application to close, and no way\n\
8540to retrieve the application's exit status.\n\
8541\n\
8542The filepath is relative to the current directory. If you want to use\n\
8543an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008544the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008545
8546static PyObject *
8547win32_startfile(PyObject *self, PyObject *args)
8548{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008549 char *filepath;
8550 char *operation = NULL;
8551 HINSTANCE rc;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008552
Victor Stinnerd6f85422010-05-05 23:33:33 +00008553 PyObject *unipath, *woperation = NULL;
8554 if (!PyArg_ParseTuple(args, "U|s:startfile",
8555 &unipath, &operation)) {
8556 PyErr_Clear();
8557 goto normal;
8558 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008559
Victor Stinnerd6f85422010-05-05 23:33:33 +00008560 if (operation) {
8561 woperation = PyUnicode_DecodeASCII(operation,
8562 strlen(operation), NULL);
8563 if (!woperation) {
8564 PyErr_Clear();
8565 operation = NULL;
8566 goto normal;
8567 }
8568 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008569
Victor Stinnerd6f85422010-05-05 23:33:33 +00008570 Py_BEGIN_ALLOW_THREADS
8571 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8572 PyUnicode_AS_UNICODE(unipath),
8573 NULL, NULL, SW_SHOWNORMAL);
8574 Py_END_ALLOW_THREADS
8575
8576 Py_XDECREF(woperation);
8577 if (rc <= (HINSTANCE)32) {
8578 PyObject *errval = win32_error_unicode("startfile",
8579 PyUnicode_AS_UNICODE(unipath));
8580 return errval;
8581 }
8582 Py_INCREF(Py_None);
8583 return Py_None;
Georg Brandlad89dc82006-04-03 12:26:26 +00008584
8585normal:
Victor Stinnerd6f85422010-05-05 23:33:33 +00008586 if (!PyArg_ParseTuple(args, "et|s:startfile",
8587 Py_FileSystemDefaultEncoding, &filepath,
8588 &operation))
8589 return NULL;
8590 Py_BEGIN_ALLOW_THREADS
8591 rc = ShellExecute((HWND)0, operation, filepath,
8592 NULL, NULL, SW_SHOWNORMAL);
8593 Py_END_ALLOW_THREADS
8594 if (rc <= (HINSTANCE)32) {
8595 PyObject *errval = win32_error("startfile", filepath);
8596 PyMem_Free(filepath);
8597 return errval;
8598 }
8599 PyMem_Free(filepath);
8600 Py_INCREF(Py_None);
8601 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008602}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008603#endif /* MS_WINDOWS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008604
Martin v. Löwis438b5342002-12-27 10:16:42 +00008605#ifdef HAVE_GETLOADAVG
8606PyDoc_STRVAR(posix_getloadavg__doc__,
8607"getloadavg() -> (float, float, float)\n\n\
8608Return the number of processes in the system run queue averaged over\n\
8609the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8610was unobtainable");
8611
8612static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008613posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008614{
8615 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008616 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah93f7a322010-11-26 17:35:50 +00008617 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8618 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008619 } else
Stefan Krah93f7a322010-11-26 17:35:50 +00008620 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008621}
8622#endif
8623
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008624PyDoc_STRVAR(posix_urandom__doc__,
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008625"urandom(n) -> str\n\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008626Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008627
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008628static PyObject *
8629posix_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008630{
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008631 Py_ssize_t size;
8632 PyObject *result;
8633 int ret;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008634
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008635 /* Read arguments */
8636 if (!PyArg_ParseTuple(args, "n:urandom", &size))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008637 return NULL;
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008638 if (size < 0)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008639 return PyErr_Format(PyExc_ValueError,
8640 "negative argument not allowed");
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008641 result = PyBytes_FromStringAndSize(NULL, size);
8642 if (result == NULL)
8643 return NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008644
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008645 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
8646 PyBytes_GET_SIZE(result));
8647 if (ret == -1) {
8648 Py_DECREF(result);
8649 return NULL;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008650 }
8651 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008652}
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008653
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008654#ifdef HAVE_SETRESUID
8655PyDoc_STRVAR(posix_setresuid__doc__,
8656"setresuid(ruid, euid, suid)\n\n\
8657Set the current process's real, effective, and saved user ids.");
8658
8659static PyObject*
8660posix_setresuid (PyObject *self, PyObject *args)
8661{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008662 uid_t ruid, euid, suid;
8663 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
8664 _Py_Uid_Converter, &ruid,
8665 _Py_Uid_Converter, &euid,
8666 _Py_Uid_Converter, &suid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008667 return NULL;
8668 if (setresuid(ruid, euid, suid) < 0)
8669 return posix_error();
8670 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008671}
8672#endif
8673
8674#ifdef HAVE_SETRESGID
8675PyDoc_STRVAR(posix_setresgid__doc__,
8676"setresgid(rgid, egid, sgid)\n\n\
8677Set the current process's real, effective, and saved group ids.");
8678
8679static PyObject*
8680posix_setresgid (PyObject *self, PyObject *args)
8681{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008682 gid_t rgid, egid, sgid;
8683 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
8684 _Py_Gid_Converter, &rgid,
8685 _Py_Gid_Converter, &egid,
8686 _Py_Gid_Converter, &sgid))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008687 return NULL;
8688 if (setresgid(rgid, egid, sgid) < 0)
8689 return posix_error();
8690 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008691}
8692#endif
8693
8694#ifdef HAVE_GETRESUID
8695PyDoc_STRVAR(posix_getresuid__doc__,
8696"getresuid() -> (ruid, euid, suid)\n\n\
8697Get tuple of the current process's real, effective, and saved user ids.");
8698
8699static PyObject*
8700posix_getresuid (PyObject *self, PyObject *noargs)
8701{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008702 uid_t ruid, euid, suid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008703 if (getresuid(&ruid, &euid, &suid) < 0)
8704 return posix_error();
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008705 return Py_BuildValue("(NNN)", _PyInt_FromUid(ruid),
8706 _PyInt_FromUid(euid),
8707 _PyInt_FromUid(suid));
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008708}
8709#endif
8710
8711#ifdef HAVE_GETRESGID
8712PyDoc_STRVAR(posix_getresgid__doc__,
8713"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandl21946af2010-10-06 09:28:45 +00008714Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008715
8716static PyObject*
8717posix_getresgid (PyObject *self, PyObject *noargs)
8718{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008719 uid_t rgid, egid, sgid;
Victor Stinnerd6f85422010-05-05 23:33:33 +00008720 if (getresgid(&rgid, &egid, &sgid) < 0)
8721 return posix_error();
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02008722 return Py_BuildValue("(NNN)", _PyInt_FromGid(rgid),
8723 _PyInt_FromGid(egid),
8724 _PyInt_FromGid(sgid));
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008725}
8726#endif
8727
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008728static PyMethodDef posix_methods[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008729 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008730#ifdef HAVE_TTYNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008731 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008732#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008733 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008734#ifdef HAVE_CHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008735 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008736#endif /* HAVE_CHFLAGS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008737 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008738#ifdef HAVE_FCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008739 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008740#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008741#ifdef HAVE_CHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008742 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008743#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008744#ifdef HAVE_LCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008745 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008746#endif /* HAVE_LCHMOD */
8747#ifdef HAVE_FCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008748 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008749#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008750#ifdef HAVE_LCHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008751 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008752#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008753#ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008754 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008755#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008756#ifdef HAVE_CHROOT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008757 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008758#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008759#ifdef HAVE_CTERMID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008760 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008761#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008762#ifdef HAVE_GETCWD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008763 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008764#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008765 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008766#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008767#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008768#ifdef HAVE_LINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008769 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008770#endif /* HAVE_LINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008771 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8772 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8773 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008774#ifdef HAVE_NICE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008775 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008776#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008777#ifdef HAVE_READLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008778 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008779#endif /* HAVE_READLINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008780 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8781 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8782 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8783 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008784#ifdef HAVE_SYMLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008785 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008786#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008787#ifdef HAVE_SYSTEM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008788 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008789#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008790 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008791#ifdef HAVE_UNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008792 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008793#endif /* HAVE_UNAME */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008794 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8795 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8796 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008797#ifdef HAVE_TIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008798 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008799#endif /* HAVE_TIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008800 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008801#ifdef HAVE_EXECV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008802 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8803 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008804#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008805#ifdef HAVE_SPAWNV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008806 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8807 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008808#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008809 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8810 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008811#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008812#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008813#ifdef HAVE_FORK1
Victor Stinnerd6f85422010-05-05 23:33:33 +00008814 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008815#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008816#ifdef HAVE_FORK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008817 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008818#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008819#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008820 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008821#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008822#ifdef HAVE_FORKPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008823 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008824#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008825#ifdef HAVE_GETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008826 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008827#endif /* HAVE_GETEGID */
8828#ifdef HAVE_GETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008829 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008830#endif /* HAVE_GETEUID */
8831#ifdef HAVE_GETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008832 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008833#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008834#ifdef HAVE_GETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008835 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008836#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008837 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008838#ifdef HAVE_GETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008839 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008840#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008841#ifdef HAVE_GETPPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008842 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008843#endif /* HAVE_GETPPID */
8844#ifdef HAVE_GETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008845 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008846#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008847#ifdef HAVE_GETLOGIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008848 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008849#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008850#ifdef HAVE_KILL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008851 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008852#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008853#ifdef HAVE_KILLPG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008854 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008855#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008856#ifdef HAVE_PLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008857 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008858#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008859#ifdef HAVE_POPEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008860 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008861#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008862 {"popen2", win32_popen2, METH_VARARGS},
8863 {"popen3", win32_popen3, METH_VARARGS},
8864 {"popen4", win32_popen4, METH_VARARGS},
8865 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8866 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008867#else
8868#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008869 {"popen2", os2emx_popen2, METH_VARARGS},
8870 {"popen3", os2emx_popen3, METH_VARARGS},
8871 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008872#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008873#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008874#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008875#ifdef HAVE_SETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008876 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008877#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008878#ifdef HAVE_SETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008879 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008880#endif /* HAVE_SETEUID */
8881#ifdef HAVE_SETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008882 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008883#endif /* HAVE_SETEGID */
8884#ifdef HAVE_SETREUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008885 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008886#endif /* HAVE_SETREUID */
8887#ifdef HAVE_SETREGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008888 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008889#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008890#ifdef HAVE_SETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008891 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008892#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008893#ifdef HAVE_SETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008894 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008895#endif /* HAVE_SETGROUPS */
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008896#ifdef HAVE_INITGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008897 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008898#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008899#ifdef HAVE_GETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008900 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008901#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008902#ifdef HAVE_SETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008903 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008904#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008905#ifdef HAVE_WAIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008906 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008907#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008908#ifdef HAVE_WAIT3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008909 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008910#endif /* HAVE_WAIT3 */
8911#ifdef HAVE_WAIT4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008912 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008913#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008914#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008915 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008916#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008917#ifdef HAVE_GETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008918 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008919#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008920#ifdef HAVE_SETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008921 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008922#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008923#ifdef HAVE_SETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008924 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008925#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008926#ifdef HAVE_TCGETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008927 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008928#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008929#ifdef HAVE_TCSETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008930 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008931#endif /* HAVE_TCSETPGRP */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008932 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8933 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8934 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8935 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8936 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8937 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8938 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8939 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8940 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8941 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8942 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008943#ifdef HAVE_PIPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008944 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008945#endif
8946#ifdef HAVE_MKFIFO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008947 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008948#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008949#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008950 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008951#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008952#ifdef HAVE_DEVICE_MACROS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008953 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8954 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8955 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008956#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008957#ifdef HAVE_FTRUNCATE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008958 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008959#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008960#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008961 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008962#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008963#ifdef HAVE_UNSETENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008964 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008965#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008966 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008967#ifdef HAVE_FCHDIR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008968 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008969#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008970#ifdef HAVE_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008971 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008972#endif
8973#ifdef HAVE_FDATASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008974 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008975#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008976#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008977#ifdef WCOREDUMP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008978 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008979#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008980#ifdef WIFCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008981 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008982#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008983#ifdef WIFSTOPPED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008984 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008985#endif /* WIFSTOPPED */
8986#ifdef WIFSIGNALED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008987 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008988#endif /* WIFSIGNALED */
8989#ifdef WIFEXITED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008990 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008991#endif /* WIFEXITED */
8992#ifdef WEXITSTATUS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008993 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008994#endif /* WEXITSTATUS */
8995#ifdef WTERMSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008996 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008997#endif /* WTERMSIG */
8998#ifdef WSTOPSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008999 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009000#endif /* WSTOPSIG */
9001#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00009002#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009003 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009004#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00009005#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009006 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009007#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00009008#ifdef HAVE_TMPFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009009 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009010#endif
9011#ifdef HAVE_TEMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009012 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009013#endif
9014#ifdef HAVE_TMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009015 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009016#endif
Fred Drakec9680921999-12-13 16:37:25 +00009017#ifdef HAVE_CONFSTR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009018 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009019#endif
9020#ifdef HAVE_SYSCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009021 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009022#endif
9023#ifdef HAVE_FPATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009024 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009025#endif
9026#ifdef HAVE_PATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00009027 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009028#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009029 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009030#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009031 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtin5446f082011-06-09 10:00:42 -05009032 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00009033#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009034#ifdef HAVE_GETLOADAVG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009035 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009036#endif
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009037#ifdef HAVE_SETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009038 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009039#endif
9040#ifdef HAVE_SETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009041 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009042#endif
9043#ifdef HAVE_GETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009044 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009045#endif
9046#ifdef HAVE_GETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00009047 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00009048#endif
Barry Warsaw1e13eb02012-02-20 20:42:21 -05009049 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Victor Stinnerd6f85422010-05-05 23:33:33 +00009050 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009051};
9052
9053
Barry Warsaw4a342091996-12-19 23:50:02 +00009054static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009055ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009056{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009057 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009058}
9059
Guido van Rossumd48f2521997-12-05 22:19:34 +00009060#if defined(PYOS_OS2)
9061/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009062static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009063{
9064 APIRET rc;
9065 ULONG values[QSV_MAX+1];
9066 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009067 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009068
9069 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009070 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009071 Py_END_ALLOW_THREADS
9072
9073 if (rc != NO_ERROR) {
9074 os2_error(rc);
9075 return -1;
9076 }
9077
Fred Drake4d1e64b2002-04-15 19:40:07 +00009078 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9079 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9080 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9081 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9082 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9083 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9084 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009085
9086 switch (values[QSV_VERSION_MINOR]) {
9087 case 0: ver = "2.00"; break;
9088 case 10: ver = "2.10"; break;
9089 case 11: ver = "2.11"; break;
9090 case 30: ver = "3.00"; break;
9091 case 40: ver = "4.00"; break;
9092 case 50: ver = "5.00"; break;
9093 default:
Tim Peters885d4572001-11-28 20:27:42 +00009094 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinnerd6f85422010-05-05 23:33:33 +00009095 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009096 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009097 ver = &tmp[0];
9098 }
9099
9100 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009101 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009102 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009103
9104 /* Add Indicator of Which Drive was Used to Boot the System */
9105 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9106 tmp[1] = ':';
9107 tmp[2] = '\0';
9108
Fred Drake4d1e64b2002-04-15 19:40:07 +00009109 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009110}
9111#endif
9112
Barry Warsaw4a342091996-12-19 23:50:02 +00009113static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009114all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009115{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009116#ifdef F_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009117 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009118#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009119#ifdef R_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009120 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009121#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009122#ifdef W_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009123 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009124#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009125#ifdef X_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009126 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009127#endif
Fred Drakec9680921999-12-13 16:37:25 +00009128#ifdef NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009129 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009130#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009131#ifdef TMP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009132 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009133#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009134#ifdef WCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009135 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009136#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009137#ifdef WNOHANG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009138 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009139#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009140#ifdef WUNTRACED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009141 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009142#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009143#ifdef O_RDONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009144 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009145#endif
9146#ifdef O_WRONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009147 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009148#endif
9149#ifdef O_RDWR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009150 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009151#endif
9152#ifdef O_NDELAY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009153 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009154#endif
9155#ifdef O_NONBLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009156 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009157#endif
9158#ifdef O_APPEND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009159 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009160#endif
9161#ifdef O_DSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009162 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009163#endif
9164#ifdef O_RSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009165 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009166#endif
9167#ifdef O_SYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009168 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009169#endif
9170#ifdef O_NOCTTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009171 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009172#endif
9173#ifdef O_CREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009174 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009175#endif
9176#ifdef O_EXCL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009177 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009178#endif
9179#ifdef O_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009180 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009181#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009182#ifdef O_BINARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009183 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009184#endif
9185#ifdef O_TEXT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009186 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009187#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009188#ifdef O_LARGEFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009189 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009190#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009191#ifdef O_SHLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009192 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009193#endif
9194#ifdef O_EXLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009195 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009196#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009197
Tim Peters5aa91602002-01-30 05:46:57 +00009198/* MS Windows */
9199#ifdef O_NOINHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009200 /* Don't inherit in child processes. */
9201 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009202#endif
9203#ifdef _O_SHORT_LIVED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009204 /* Optimize for short life (keep in memory). */
9205 /* MS forgot to define this one with a non-underscore form too. */
9206 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009207#endif
9208#ifdef O_TEMPORARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009209 /* Automatically delete when last handle is closed. */
9210 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009211#endif
9212#ifdef O_RANDOM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009213 /* Optimize for random access. */
9214 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009215#endif
9216#ifdef O_SEQUENTIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009217 /* Optimize for sequential access. */
9218 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009219#endif
9220
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009221/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00009222#ifdef O_ASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009223 /* Send a SIGIO signal whenever input or output
9224 becomes available on file descriptor */
9225 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00009226#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009227#ifdef O_DIRECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009228 /* Direct disk access. */
9229 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009230#endif
9231#ifdef O_DIRECTORY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009232 /* Must be a directory. */
9233 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009234#endif
9235#ifdef O_NOFOLLOW
Victor Stinnerd6f85422010-05-05 23:33:33 +00009236 /* Do not follow links. */
9237 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009238#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00009239#ifdef O_NOATIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00009240 /* Do not update the access time. */
9241 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00009242#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009243
Victor Stinnerd6f85422010-05-05 23:33:33 +00009244 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009245#ifdef EX_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009246 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009247#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009248#ifdef EX_USAGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009249 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009250#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009251#ifdef EX_DATAERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009252 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009253#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009254#ifdef EX_NOINPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009255 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009256#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009257#ifdef EX_NOUSER
Victor Stinnerd6f85422010-05-05 23:33:33 +00009258 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009259#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009260#ifdef EX_NOHOST
Victor Stinnerd6f85422010-05-05 23:33:33 +00009261 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009262#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009263#ifdef EX_UNAVAILABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009264 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009265#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009266#ifdef EX_SOFTWARE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009267 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009268#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009269#ifdef EX_OSERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009270 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009271#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009272#ifdef EX_OSFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009273 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009274#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009275#ifdef EX_CANTCREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009276 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009277#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009278#ifdef EX_IOERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009279 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009280#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009281#ifdef EX_TEMPFAIL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009282 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009283#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009284#ifdef EX_PROTOCOL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009285 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009286#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009287#ifdef EX_NOPERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009288 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009289#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009290#ifdef EX_CONFIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009291 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009292#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009293#ifdef EX_NOTFOUND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009294 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009295#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009296
Guido van Rossum246bc171999-02-01 23:54:31 +00009297#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009298#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009299 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9300 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9301 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9302 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9303 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9304 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9305 if (ins(d, "P_PM", (long)P_PM)) return -1;
9306 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9307 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9308 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9309 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9310 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9311 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9312 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9313 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9314 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9315 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9316 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9317 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9318 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009319#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009320 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9321 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9322 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9323 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9324 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009325#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009326#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009327
Guido van Rossumd48f2521997-12-05 22:19:34 +00009328#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009329 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009330#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009331 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009332}
9333
9334
Tim Peters5aa91602002-01-30 05:46:57 +00009335#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009336#define INITFUNC initnt
9337#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009338
9339#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009340#define INITFUNC initos2
9341#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009342
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009343#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009344#define INITFUNC initposix
9345#define MODNAME "posix"
9346#endif
9347
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009348PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009349INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009350{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009351 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009352
Victor Stinnerd6f85422010-05-05 23:33:33 +00009353 m = Py_InitModule3(MODNAME,
9354 posix_methods,
9355 posix__doc__);
9356 if (m == NULL)
9357 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009358
Victor Stinnerd6f85422010-05-05 23:33:33 +00009359 /* Initialize environ dictionary */
9360 v = convertenviron();
9361 Py_XINCREF(v);
9362 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9363 return;
9364 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009365
Victor Stinnerd6f85422010-05-05 23:33:33 +00009366 if (all_ins(m))
9367 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009368
Victor Stinnerd6f85422010-05-05 23:33:33 +00009369 if (setup_confname_tables(m))
9370 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009371
Victor Stinnerd6f85422010-05-05 23:33:33 +00009372 Py_INCREF(PyExc_OSError);
9373 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009374
Guido van Rossumb3d39562000-01-31 18:41:26 +00009375#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00009376 if (posix_putenv_garbage == NULL)
9377 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009378#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009379
Victor Stinnerd6f85422010-05-05 23:33:33 +00009380 if (!initialized) {
9381 stat_result_desc.name = MODNAME ".stat_result";
9382 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9383 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9384 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9385 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9386 structseq_new = StatResultType.tp_new;
9387 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009388
Victor Stinnerd6f85422010-05-05 23:33:33 +00009389 statvfs_result_desc.name = MODNAME ".statvfs_result";
9390 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009391#ifdef NEED_TICKS_PER_SECOND
9392# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009393 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009394# elif defined(HZ)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009395 ticks_per_second = HZ;
Martin v. Löwis03824e42008-12-29 18:17:34 +00009396# else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009397 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis03824e42008-12-29 18:17:34 +00009398# endif
9399#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009400 }
9401 Py_INCREF((PyObject*) &StatResultType);
9402 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9403 Py_INCREF((PyObject*) &StatVFSResultType);
9404 PyModule_AddObject(m, "statvfs_result",
9405 (PyObject*) &StatVFSResultType);
9406 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009407
9408#ifdef __APPLE__
Victor Stinnerd6f85422010-05-05 23:33:33 +00009409 /*
9410 * Step 2 of weak-linking support on Mac OS X.
9411 *
9412 * The code below removes functions that are not available on the
9413 * currently active platform.
9414 *
9415 * This block allow one to use a python binary that was build on
9416 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9417 * OSX 10.4.
9418 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009419#ifdef HAVE_FSTATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009420 if (fstatvfs == NULL) {
9421 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9422 return;
9423 }
9424 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009425#endif /* HAVE_FSTATVFS */
9426
9427#ifdef HAVE_STATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009428 if (statvfs == NULL) {
9429 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9430 return;
9431 }
9432 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009433#endif /* HAVE_STATVFS */
9434
9435# ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00009436 if (lchown == NULL) {
9437 if (PyObject_DelAttrString(m, "lchown") == -1) {
9438 return;
9439 }
9440 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009441#endif /* HAVE_LCHOWN */
9442
9443
9444#endif /* __APPLE__ */
9445
Guido van Rossumb6775db1994-08-01 11:34:53 +00009446}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009447
9448#ifdef __cplusplus
9449}
9450#endif
9451
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009452