blob: eedd2ea2e9d4e775271af0c2d16d99ae8e117208 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Ronald Oussorend06b6f22006-04-23 11:59:25 +000016#ifdef __APPLE__
17 /*
Victor Stinner43cdd0a2010-05-06 00:20:44 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Ronald Oussorend06b6f22006-04-23 11:59:25 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Anthony Baxterac6bd462006-04-13 02:06:09 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000069#endif /* HAVE_SYS_TYPES_H */
70
71#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000073#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000074
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#ifdef HAVE_SYS_WAIT_H
Victor Stinner43cdd0a2010-05-06 00:20:44 +000076#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000077#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000081#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000082
Guido van Rossumb6775db1994-08-01 11:34:53 +000083#ifdef HAVE_FCNTL_H
84#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000085#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000086
Guido van Rossuma6535fd2001-10-18 19:44:10 +000087#ifdef HAVE_GRP_H
88#include <grp.h>
89#endif
90
Barry Warsaw5676bd12003-01-07 20:57:09 +000091#ifdef HAVE_SYSEXITS_H
92#include <sysexits.h>
93#endif /* HAVE_SYSEXITS_H */
94
Anthony Baxter8a560de2004-10-13 15:30:56 +000095#ifdef HAVE_SYS_LOADAVG_H
96#include <sys/loadavg.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
119#define HAVE_POPEN 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000120#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000121#define HAVE_WAIT 1
122#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000123#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000124#define HAVE_GETCWD 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000129#define HAVE_SYSTEM 1
130#define HAVE_CWAIT 1
131#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000136#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000152#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000154#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000155#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_WAIT 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000157#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000158#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#endif /* _MSC_VER */
160#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000161#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000163
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000165
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000166#if defined(__sgi)&&_COMPILER_VERSION>=700
167/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169extern char *ctermid_r(char *);
170#endif
171
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000172#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000173#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000176#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
182#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(char *);
184extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chdir(const char *);
187extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#ifdef __BORLANDC__
190extern int chmod(const char *, int);
191#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000193#endif
Christian Heimes36281872007-11-30 21:11:28 +0000194/*#ifdef HAVE_FCHMOD
195extern int fchmod(int, mode_t);
196#endif*/
197/*#ifdef HAVE_LCHMOD
198extern int lchmod(const char *, mode_t);
199#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int chown(const char *, uid_t, gid_t);
201extern char *getcwd(char *, int);
202extern char *strerror(int);
203extern int link(const char *, const char *);
204extern int rename(const char *, const char *);
205extern int stat(const char *, struct stat *);
206extern int unlink(const char *);
207extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000210#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000215
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#ifdef HAVE_UTIME_H
219#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000222#ifdef HAVE_SYS_UTIME_H
223#include <sys/utime.h>
224#define HAVE_UTIME_H /* pretend we do for the rest of this file */
225#endif /* HAVE_SYS_UTIME_H */
226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_SYS_TIMES_H
228#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
231#ifdef HAVE_SYS_PARAM_H
232#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
235#ifdef HAVE_SYS_UTSNAME_H
236#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000241#define NAMLEN(dirent) strlen((dirent)->d_name)
242#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000243#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#include <direct.h>
245#define NAMLEN(dirent) strlen((dirent)->d_name)
246#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#endif
256#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#endif
259#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000262#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
265#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000267#endif
268#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000270#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000271#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <windows.h>
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000273#include <shellapi.h> /* for ShellExecute() */
274#define popen _popen
275#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossumd48f2521997-12-05 22:19:34 +0000278#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000280#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
Tim Petersbc2e10e2002-03-03 23:17:02 +0000282#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000283#if defined(PATH_MAX) && PATH_MAX > 1024
284#define MAXPATHLEN PATH_MAX
285#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000286#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000287#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#endif /* MAXPATHLEN */
289
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000290#ifdef UNION_WAIT
291/* Emulate some macros on systems that have a union instead of macros */
292
293#ifndef WIFEXITED
294#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
295#endif
296
297#ifndef WEXITSTATUS
298#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
299#endif
300
301#ifndef WTERMSIG
302#define WTERMSIG(u_wait) ((u_wait).w_termsig)
303#endif
304
Neal Norwitzd5a37542006-03-20 06:48:34 +0000305#define WAIT_TYPE union wait
306#define WAIT_STATUS_INT(s) (s.w_status)
307
308#else /* !UNION_WAIT */
309#define WAIT_TYPE int
310#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000311#endif /* UNION_WAIT */
312
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000313/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou6c95acb2009-05-24 12:17:07 +0000314#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000315#define PARSE_PID "i"
316#define PyLong_FromPid PyInt_FromLong
317#define PyLong_AsPid PyInt_AsLong
318#elif SIZEOF_PID_T == SIZEOF_LONG
319#define PARSE_PID "l"
320#define PyLong_FromPid PyInt_FromLong
321#define PyLong_AsPid PyInt_AsLong
322#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
323#define PARSE_PID "L"
324#define PyLong_FromPid PyLong_FromLongLong
325#define PyLong_AsPid PyInt_AsLongLong
326#else
327#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000328#endif /* SIZEOF_PID_T */
329
Greg Wardb48bc172000-03-01 21:51:56 +0000330/* Don't use the "_r" form if we don't need it (also, won't have a
331 prototype for it, at least on Solaris -- maybe others as well?). */
332#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
333#define USE_CTERMID_R
334#endif
335
336#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
337#define USE_TMPNAM_R
338#endif
339
Fred Drake699f3522000-06-29 21:12:41 +0000340/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000341#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000342#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000343# define STAT win32_stat
344# define FSTAT win32_fstat
345# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000346#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000347# define STAT stat
348# define FSTAT fstat
349# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000350#endif
351
Tim Peters11b23062003-04-23 02:39:17 +0000352#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000353#include <sys/mkdev.h>
354#else
355#if defined(MAJOR_IN_SYSMACROS)
356#include <sys/sysmacros.h>
357#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000358#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
359#include <sys/mkdev.h>
360#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000361#endif
Fred Drake699f3522000-06-29 21:12:41 +0000362
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000364#ifdef WITH_NEXT_FRAMEWORK
365/* On Darwin/MacOSX a shared library or framework has no access to
366** environ directly, we must obtain it with _NSGetEnviron().
367*/
368#include <crt_externs.h>
369static char **environ;
370#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000372#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373
Barry Warsaw53699e91996-12-10 23:23:01 +0000374static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000375convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000377 PyObject *d;
378 char **e;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000379#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000380 APIRET rc;
381 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
382#endif
383 d = PyDict_New();
384 if (d == NULL)
385 return NULL;
386#ifdef WITH_NEXT_FRAMEWORK
387 if (environ == NULL)
388 environ = *_NSGetEnviron();
389#endif
390 if (environ == NULL)
391 return d;
392 /* This part ignores errors */
393 for (e = environ; *e != NULL; e++) {
394 PyObject *k;
395 PyObject *v;
396 char *p = strchr(*e, '=');
397 if (p == NULL)
398 continue;
399 k = PyString_FromStringAndSize(*e, (int)(p-*e));
400 if (k == NULL) {
401 PyErr_Clear();
402 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000403 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000404 v = PyString_FromString(p+1);
405 if (v == NULL) {
406 PyErr_Clear();
407 Py_DECREF(k);
408 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000409 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000410 if (PyDict_GetItem(d, k) == NULL) {
411 if (PyDict_SetItem(d, k, v) != 0)
412 PyErr_Clear();
413 }
414 Py_DECREF(k);
415 Py_DECREF(v);
416 }
417#if defined(PYOS_OS2)
418 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
419 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
420 PyObject *v = PyString_FromString(buffer);
421 PyDict_SetItemString(d, "BEGINLIBPATH", v);
422 Py_DECREF(v);
423 }
424 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
425 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
426 PyObject *v = PyString_FromString(buffer);
427 PyDict_SetItemString(d, "ENDLIBPATH", v);
428 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000429 }
430#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000431 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432}
433
434
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435/* Set a POSIX-specific error from errno, and return NULL */
436
Barry Warsawd58d7641998-07-23 16:14:40 +0000437static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000438posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000439{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000440 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
Barry Warsawd58d7641998-07-23 16:14:40 +0000442static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000443posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000444{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000445 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000446}
447
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000448#ifdef Py_WIN_WIDE_FILENAMES
449static PyObject *
450posix_error_with_unicode_filename(Py_UNICODE* name)
451{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000452 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000453}
454#endif /* Py_WIN_WIDE_FILENAMES */
455
456
Mark Hammondef8b6542001-05-13 08:04:26 +0000457static PyObject *
458posix_error_with_allocated_filename(char* name)
459{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000460 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
461 PyMem_Free(name);
462 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000463}
464
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000465#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000466static PyObject *
467win32_error(char* function, char* filename)
468{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000469 /* XXX We should pass the function name along in the future.
470 (_winreg.c also wants to pass the function name.)
471 This would however require an additional param to the
472 Windows error object, which is non-trivial.
473 */
474 errno = GetLastError();
475 if (filename)
476 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
477 else
478 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000479}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000480
481#ifdef Py_WIN_WIDE_FILENAMES
482static PyObject *
483win32_error_unicode(char* function, Py_UNICODE* filename)
484{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000485 /* XXX - see win32_error for comments on 'function' */
486 errno = GetLastError();
487 if (filename)
488 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
489 else
490 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000491}
492
493static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
494{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000495}
496
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000497static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000498convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000499{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000500 if (PyUnicode_CheckExact(*param))
501 Py_INCREF(*param);
502 else if (PyUnicode_Check(*param))
503 /* For a Unicode subtype that's not a Unicode object,
504 return a true Unicode object with the same data. */
505 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
506 PyUnicode_GET_SIZE(*param));
507 else
508 *param = PyUnicode_FromEncodedObject(*param,
509 Py_FileSystemDefaultEncoding,
510 "strict");
511 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000512}
513
514#endif /* Py_WIN_WIDE_FILENAMES */
515
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000516#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518#if defined(PYOS_OS2)
519/**********************************************************************
520 * Helper Function to Trim and Format OS/2 Messages
521 **********************************************************************/
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000522static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000523os2_formatmsg(char *msgbuf, int msglen, char *reason)
524{
525 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
526
527 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
528 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
529
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000530 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000531 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
532 }
533
534 /* Add Optional Reason Text */
535 if (reason) {
536 strcat(msgbuf, " : ");
537 strcat(msgbuf, reason);
538 }
539}
540
541/**********************************************************************
542 * Decode an OS/2 Operating System Error Code
543 *
544 * A convenience function to lookup an OS/2 error code and return a
545 * text message we can use to raise a Python exception.
546 *
547 * Notes:
548 * The messages for errors returned from the OS/2 kernel reside in
549 * the file OSO001.MSG in the \OS2 directory hierarchy.
550 *
551 **********************************************************************/
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000552static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000553os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
554{
555 APIRET rc;
556 ULONG msglen;
557
558 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
559 Py_BEGIN_ALLOW_THREADS
560 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
561 errorcode, "oso001.msg", &msglen);
562 Py_END_ALLOW_THREADS
563
564 if (rc == NO_ERROR)
565 os2_formatmsg(msgbuf, msglen, reason);
566 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000567 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000568 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000569
570 return msgbuf;
571}
572
573/* Set an OS/2-specific error and return NULL. OS/2 kernel
574 errors are not in a global variable e.g. 'errno' nor are
575 they congruent with posix error numbers. */
576
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000577static PyObject *
578os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000579{
580 char text[1024];
581 PyObject *v;
582
583 os2_strerror(text, sizeof(text), code, "");
584
585 v = Py_BuildValue("(is)", code, text);
586 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000587 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000588 Py_DECREF(v);
589 }
590 return NULL; /* Signal to Python that an Exception is Pending */
591}
592
593#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594
595/* POSIX generic methods */
596
Barry Warsaw53699e91996-12-10 23:23:01 +0000597static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000598posix_fildes(PyObject *fdobj, int (*func)(int))
599{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000600 int fd;
601 int res;
602 fd = PyObject_AsFileDescriptor(fdobj);
603 if (fd < 0)
604 return NULL;
605 Py_BEGIN_ALLOW_THREADS
606 res = (*func)(fd);
607 Py_END_ALLOW_THREADS
608 if (res < 0)
609 return posix_error();
610 Py_INCREF(Py_None);
611 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000612}
Guido van Rossum21142a01999-01-08 21:05:37 +0000613
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000615static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000616unicode_file_names(void)
617{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000618 static int canusewide = -1;
619 if (canusewide == -1) {
620 /* As per doc for ::GetVersion(), this is the correct test for
621 the Windows NT family. */
622 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
623 }
624 return canusewide;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000625}
626#endif
Tim Peters11b23062003-04-23 02:39:17 +0000627
Guido van Rossum21142a01999-01-08 21:05:37 +0000628static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000629posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000631 char *path1 = NULL;
632 int res;
633 if (!PyArg_ParseTuple(args, format,
634 Py_FileSystemDefaultEncoding, &path1))
635 return NULL;
636 Py_BEGIN_ALLOW_THREADS
637 res = (*func)(path1);
638 Py_END_ALLOW_THREADS
639 if (res < 0)
640 return posix_error_with_allocated_filename(path1);
641 PyMem_Free(path1);
642 Py_INCREF(Py_None);
643 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
Barry Warsaw53699e91996-12-10 23:23:01 +0000646static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000647posix_2str(PyObject *args,
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000648 char *format,
649 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000651 char *path1 = NULL, *path2 = NULL;
652 int res;
653 if (!PyArg_ParseTuple(args, format,
654 Py_FileSystemDefaultEncoding, &path1,
655 Py_FileSystemDefaultEncoding, &path2))
656 return NULL;
657 Py_BEGIN_ALLOW_THREADS
658 res = (*func)(path1, path2);
659 Py_END_ALLOW_THREADS
660 PyMem_Free(path1);
661 PyMem_Free(path2);
662 if (res != 0)
663 /* XXX how to report both path1 and path2??? */
664 return posix_error();
665 Py_INCREF(Py_None);
666 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000669#ifdef Py_WIN_WIDE_FILENAMES
670static PyObject*
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000671win32_1str(PyObject* args, char* func,
672 char* format, BOOL (__stdcall *funcA)(LPCSTR),
673 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000674{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000675 PyObject *uni;
676 char *ansi;
677 BOOL result;
678 if (unicode_file_names()) {
679 if (!PyArg_ParseTuple(args, wformat, &uni))
680 PyErr_Clear();
681 else {
682 Py_BEGIN_ALLOW_THREADS
683 result = funcW(PyUnicode_AsUnicode(uni));
684 Py_END_ALLOW_THREADS
685 if (!result)
686 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
687 Py_INCREF(Py_None);
688 return Py_None;
689 }
690 }
691 if (!PyArg_ParseTuple(args, format, &ansi))
692 return NULL;
693 Py_BEGIN_ALLOW_THREADS
694 result = funcA(ansi);
695 Py_END_ALLOW_THREADS
696 if (!result)
697 return win32_error(func, ansi);
698 Py_INCREF(Py_None);
699 return Py_None;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000700
701}
702
703/* This is a reimplementation of the C library's chdir function,
704 but one that produces Win32 errors instead of DOS error codes.
705 chdir is essentially a wrapper around SetCurrentDirectory; however,
706 it also needs to set "magic" environment variables indicating
707 the per-drive current directory, which are of the form =<drive>: */
708BOOL __stdcall
709win32_chdir(LPCSTR path)
710{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000711 char new_path[MAX_PATH+1];
712 int result;
713 char env[4] = "=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000714
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000715 if(!SetCurrentDirectoryA(path))
716 return FALSE;
717 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 /* In the ANSI API, there should not be any paths longer
721 than MAX_PATH. */
722 assert(result <= MAX_PATH+1);
723 if (strncmp(new_path, "\\\\", 2) == 0 ||
724 strncmp(new_path, "//", 2) == 0)
725 /* UNC path, nothing to do. */
726 return TRUE;
727 env[1] = new_path[0];
728 return SetEnvironmentVariableA(env, new_path);
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000729}
730
731/* The Unicode version differs from the ANSI version
732 since the current directory might exceed MAX_PATH characters */
733BOOL __stdcall
734win32_wchdir(LPCWSTR path)
735{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000736 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
737 int result;
738 wchar_t env[4] = L"=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000739
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000740 if(!SetCurrentDirectoryW(path))
741 return FALSE;
742 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
743 if (!result)
744 return FALSE;
745 if (result > MAX_PATH+1) {
746 new_path = malloc(result * sizeof(wchar_t));
747 if (!new_path) {
748 SetLastError(ERROR_OUTOFMEMORY);
749 return FALSE;
750 }
751 result = GetCurrentDirectoryW(result, new_path);
752 if (!result) {
753 free(new_path);
754 return FALSE;
755 }
756 }
757 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
758 wcsncmp(new_path, L"//", 2) == 0)
759 /* UNC path, nothing to do. */
760 return TRUE;
761 env[1] = new_path[0];
762 result = SetEnvironmentVariableW(env, new_path);
763 if (new_path != _new_path)
764 free(new_path);
765 return result;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000766}
767#endif
768
Martin v. Löwis14694662006-02-03 12:54:16 +0000769#ifdef MS_WINDOWS
770/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
771 - time stamps are restricted to second resolution
772 - file modification times suffer from forth-and-back conversions between
773 UTC and local time
774 Therefore, we implement our own stat, based on the Win32 API directly.
775*/
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000776#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000777
778struct win32_stat{
779 int st_dev;
780 __int64 st_ino;
781 unsigned short st_mode;
782 int st_nlink;
783 int st_uid;
784 int st_gid;
785 int st_rdev;
786 __int64 st_size;
787 int st_atime;
788 int st_atime_nsec;
789 int st_mtime;
790 int st_mtime_nsec;
791 int st_ctime;
792 int st_ctime_nsec;
793};
794
795static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
796
797static void
798FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
799{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000800 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
801 /* Cannot simply cast and dereference in_ptr,
802 since it might not be aligned properly */
803 __int64 in;
804 memcpy(&in, in_ptr, sizeof(in));
805 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
806 /* XXX Win32 supports time stamps past 2038; we currently don't */
807 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000808}
809
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000810static void
811time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
812{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000813 /* XXX endianness */
814 __int64 out;
815 out = time_in + secs_between_epochs;
816 out = out * 10000000 + nsec_in / 100;
817 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000818}
819
Martin v. Löwis14694662006-02-03 12:54:16 +0000820/* Below, we *know* that ugo+r is 0444 */
821#if _S_IREAD != 0400
822#error Unsupported C library
823#endif
824static int
825attributes_to_mode(DWORD attr)
826{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000827 int m = 0;
828 if (attr & FILE_ATTRIBUTE_DIRECTORY)
829 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
830 else
831 m |= _S_IFREG;
832 if (attr & FILE_ATTRIBUTE_READONLY)
833 m |= 0444;
834 else
835 m |= 0666;
836 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000837}
838
839static int
840attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
841{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000842 memset(result, 0, sizeof(*result));
843 result->st_mode = attributes_to_mode(info->dwFileAttributes);
844 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
845 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
846 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
847 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000848
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000849 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000850}
851
Martin v. Löwis012bc722006-10-15 09:43:39 +0000852/* Emulate GetFileAttributesEx[AW] on Windows 95 */
853static int checked = 0;
854static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
855static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
856static void
857check_gfax()
858{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000859 HINSTANCE hKernel32;
860 if (checked)
861 return;
862 checked = 1;
863 hKernel32 = GetModuleHandle("KERNEL32");
864 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
865 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
Martin v. Löwis012bc722006-10-15 09:43:39 +0000866}
867
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000868static BOOL
869attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
870{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000871 HANDLE hFindFile;
872 WIN32_FIND_DATAA FileData;
873 hFindFile = FindFirstFileA(pszFile, &FileData);
874 if (hFindFile == INVALID_HANDLE_VALUE)
875 return FALSE;
876 FindClose(hFindFile);
877 pfad->dwFileAttributes = FileData.dwFileAttributes;
878 pfad->ftCreationTime = FileData.ftCreationTime;
879 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
880 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
881 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
882 pfad->nFileSizeLow = FileData.nFileSizeLow;
883 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000884}
885
886static BOOL
887attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
888{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000889 HANDLE hFindFile;
890 WIN32_FIND_DATAW FileData;
891 hFindFile = FindFirstFileW(pszFile, &FileData);
892 if (hFindFile == INVALID_HANDLE_VALUE)
893 return FALSE;
894 FindClose(hFindFile);
895 pfad->dwFileAttributes = FileData.dwFileAttributes;
896 pfad->ftCreationTime = FileData.ftCreationTime;
897 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
898 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
899 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
900 pfad->nFileSizeLow = FileData.nFileSizeLow;
901 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000902}
903
Martin v. Löwis012bc722006-10-15 09:43:39 +0000904static BOOL WINAPI
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000905Py_GetFileAttributesExA(LPCSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
Martin v. Löwis012bc722006-10-15 09:43:39 +0000907 LPVOID pv)
908{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000909 BOOL result;
910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxa(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
930 return FALSE;
931 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000932}
933
934static BOOL WINAPI
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000935Py_GetFileAttributesExW(LPCWSTR pszFile,
936 GET_FILEEX_INFO_LEVELS level,
Martin v. Löwis012bc722006-10-15 09:43:39 +0000937 LPVOID pv)
938{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000939 BOOL result;
940 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
941 /* First try to use the system's implementation, if that is
942 available and either succeeds to gives an error other than
943 that it isn't implemented. */
944 check_gfax();
945 if (gfaxa) {
946 result = gfaxw(pszFile, level, pv);
947 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
948 return result;
949 }
950 /* It's either not present, or not implemented.
951 Emulate using FindFirstFile. */
952 if (level != GetFileExInfoStandard) {
953 SetLastError(ERROR_INVALID_PARAMETER);
954 return FALSE;
955 }
956 /* Use GetFileAttributes to validate that the file name
957 does not contain wildcards (which FindFirstFile would
958 accept). */
959 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
960 return FALSE;
961 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000962}
963
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000964static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000965win32_stat(const char* path, struct win32_stat *result)
966{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000967 WIN32_FILE_ATTRIBUTE_DATA info;
968 int code;
969 char *dot;
970 /* XXX not supported on Win95 and NT 3.x */
971 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
972 if (GetLastError() != ERROR_SHARING_VIOLATION) {
973 /* Protocol violation: we explicitly clear errno, instead of
974 setting it to a POSIX error. Callers should use GetLastError. */
975 errno = 0;
976 return -1;
977 } else {
978 /* Could not get attributes on open file. Fall back to
979 reading the directory. */
980 if (!attributes_from_dir(path, &info)) {
981 /* Very strange. This should not fail now */
982 errno = 0;
983 return -1;
984 }
985 }
986 }
987 code = attribute_data_to_stat(&info, result);
988 if (code != 0)
989 return code;
990 /* Set S_IFEXEC if it is an .exe, .bat, ... */
991 dot = strrchr(path, '.');
992 if (dot) {
993 if (stricmp(dot, ".bat") == 0 ||
994 stricmp(dot, ".cmd") == 0 ||
995 stricmp(dot, ".exe") == 0 ||
996 stricmp(dot, ".com") == 0)
997 result->st_mode |= 0111;
998 }
999 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001000}
1001
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001002static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001003win32_wstat(const wchar_t* path, struct win32_stat *result)
1004{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001005 int code;
1006 const wchar_t *dot;
1007 WIN32_FILE_ATTRIBUTE_DATA info;
1008 /* XXX not supported on Win95 and NT 3.x */
1009 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1010 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1011 /* Protocol violation: we explicitly clear errno, instead of
1012 setting it to a POSIX error. Callers should use GetLastError. */
1013 errno = 0;
1014 return -1;
1015 } else {
1016 /* Could not get attributes on open file. Fall back to
1017 reading the directory. */
1018 if (!attributes_from_dir_w(path, &info)) {
1019 /* Very strange. This should not fail now */
1020 errno = 0;
1021 return -1;
1022 }
1023 }
1024 }
1025 code = attribute_data_to_stat(&info, result);
1026 if (code < 0)
1027 return code;
1028 /* Set IFEXEC if it is an .exe, .bat, ... */
1029 dot = wcsrchr(path, '.');
1030 if (dot) {
1031 if (_wcsicmp(dot, L".bat") == 0 ||
1032 _wcsicmp(dot, L".cmd") == 0 ||
1033 _wcsicmp(dot, L".exe") == 0 ||
1034 _wcsicmp(dot, L".com") == 0)
1035 result->st_mode |= 0111;
1036 }
1037 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001038}
1039
1040static int
1041win32_fstat(int file_number, struct win32_stat *result)
1042{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001043 BY_HANDLE_FILE_INFORMATION info;
1044 HANDLE h;
1045 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001046
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001047 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001048
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001049 /* Protocol violation: we explicitly clear errno, instead of
1050 setting it to a POSIX error. Callers should use GetLastError. */
1051 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001052
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001053 if (h == INVALID_HANDLE_VALUE) {
1054 /* This is really a C library error (invalid file handle).
1055 We set the Win32 error to the closes one matching. */
1056 SetLastError(ERROR_INVALID_HANDLE);
1057 return -1;
1058 }
1059 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001060
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001061 type = GetFileType(h);
1062 if (type == FILE_TYPE_UNKNOWN) {
1063 DWORD error = GetLastError();
1064 if (error != 0) {
1065 return -1;
1066 }
1067 /* else: valid but unknown file */
1068 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001069
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001070 if (type != FILE_TYPE_DISK) {
1071 if (type == FILE_TYPE_CHAR)
1072 result->st_mode = _S_IFCHR;
1073 else if (type == FILE_TYPE_PIPE)
1074 result->st_mode = _S_IFIFO;
1075 return 0;
1076 }
1077
1078 if (!GetFileInformationByHandle(h, &info)) {
1079 return -1;
1080 }
1081
1082 /* similar to stat() */
1083 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1084 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1085 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1086 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1087 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1088 /* specific to fstat() */
1089 result->st_nlink = info.nNumberOfLinks;
1090 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1091 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001092}
1093
1094#endif /* MS_WINDOWS */
1095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001097"stat_result: Result from stat or lstat.\n\n\
1098This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001099 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001100or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1101\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001102Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1103or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106
1107static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001108 {"st_mode", "protection bits"},
1109 {"st_ino", "inode"},
1110 {"st_dev", "device"},
1111 {"st_nlink", "number of hard links"},
1112 {"st_uid", "user ID of owner"},
1113 {"st_gid", "group ID of owner"},
1114 {"st_size", "total size, in bytes"},
1115 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1116 {NULL, "integer time of last access"},
1117 {NULL, "integer time of last modification"},
1118 {NULL, "integer time of last change"},
1119 {"st_atime", "time of last access"},
1120 {"st_mtime", "time of last modification"},
1121 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001122#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001123 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001124#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001126 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001127#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001128#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001129 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001130#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001132 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001133#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001134#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001135 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001136#endif
1137#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001138 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001139#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001140 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141};
1142
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001143#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001144#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001145#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001146#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001147#endif
1148
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001149#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001150#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1151#else
1152#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1153#endif
1154
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001155#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1157#else
1158#define ST_RDEV_IDX ST_BLOCKS_IDX
1159#endif
1160
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001161#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1162#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1163#else
1164#define ST_FLAGS_IDX ST_RDEV_IDX
1165#endif
1166
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001167#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001168#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001169#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001170#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001171#endif
1172
1173#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1174#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1175#else
1176#define ST_BIRTHTIME_IDX ST_GEN_IDX
1177#endif
1178
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001179static PyStructSequence_Desc stat_result_desc = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001180 "stat_result", /* name */
1181 stat_result__doc__, /* doc */
1182 stat_result_fields,
1183 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001184};
1185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001187"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1188This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001189 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001190or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001193
1194static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001195 {"f_bsize", },
1196 {"f_frsize", },
1197 {"f_blocks", },
1198 {"f_bfree", },
1199 {"f_bavail", },
1200 {"f_files", },
1201 {"f_ffree", },
1202 {"f_favail", },
1203 {"f_flag", },
1204 {"f_namemax",},
1205 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206};
1207
1208static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001209 "statvfs_result", /* name */
1210 statvfs_result__doc__, /* doc */
1211 statvfs_result_fields,
1212 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001213};
1214
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001215static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001216static PyTypeObject StatResultType;
1217static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001218static newfunc structseq_new;
1219
1220static PyObject *
1221statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1222{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001223 PyStructSequence *result;
1224 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001225
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001226 result = (PyStructSequence*)structseq_new(type, args, kwds);
1227 if (!result)
1228 return NULL;
1229 /* If we have been initialized from a tuple,
1230 st_?time might be set to None. Initialize it
1231 from the int slots. */
1232 for (i = 7; i <= 9; i++) {
1233 if (result->ob_item[i+3] == Py_None) {
1234 Py_DECREF(Py_None);
1235 Py_INCREF(result->ob_item[i]);
1236 result->ob_item[i+3] = result->ob_item[i];
1237 }
1238 }
1239 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001240}
1241
1242
1243
1244/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001245static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001246
1247PyDoc_STRVAR(stat_float_times__doc__,
1248"stat_float_times([newval]) -> oldval\n\n\
1249Determine whether os.[lf]stat represents time stamps as float objects.\n\
1250If newval is True, future calls to stat() return floats, if it is False,\n\
1251future calls return ints. \n\
1252If newval is omitted, return the current setting.\n");
1253
1254static PyObject*
1255stat_float_times(PyObject* self, PyObject *args)
1256{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001257 int newval = -1;
1258 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1259 return NULL;
1260 if (newval == -1)
1261 /* Return old value */
1262 return PyBool_FromLong(_stat_float_times);
1263 _stat_float_times = newval;
1264 Py_INCREF(Py_None);
1265 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001266}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001267
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001268static void
1269fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1270{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001271 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001272#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001273 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001274#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001275 ival = PyInt_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001276#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001277 if (!ival)
1278 return;
1279 if (_stat_float_times) {
1280 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1281 } else {
1282 fval = ival;
1283 Py_INCREF(fval);
1284 }
1285 PyStructSequence_SET_ITEM(v, index, ival);
1286 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001287}
1288
Tim Peters5aa91602002-01-30 05:46:57 +00001289/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001290 (used by posix_stat() and posix_fstat()) */
1291static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001292_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001293{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001294 unsigned long ansec, mnsec, cnsec;
1295 PyObject *v = PyStructSequence_New(&StatResultType);
1296 if (v == NULL)
1297 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001298
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001299 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001300#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001301 PyStructSequence_SET_ITEM(v, 1,
1302 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001303#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001304 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001305#endif
1306#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001307 PyStructSequence_SET_ITEM(v, 2,
1308 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001309#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001310 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001311#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001312 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1313 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1314 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001315#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001316 PyStructSequence_SET_ITEM(v, 6,
1317 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001319 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001321
Martin v. Löwis14694662006-02-03 12:54:16 +00001322#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001323 ansec = st->st_atim.tv_nsec;
1324 mnsec = st->st_mtim.tv_nsec;
1325 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001326#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001327 ansec = st->st_atimespec.tv_nsec;
1328 mnsec = st->st_mtimespec.tv_nsec;
1329 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001330#elif defined(HAVE_STAT_NSEC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001331 ansec = st->st_atime_nsec;
1332 mnsec = st->st_mtime_nsec;
1333 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001335 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001337 fill_time(v, 7, st->st_atime, ansec);
1338 fill_time(v, 8, st->st_mtime, mnsec);
1339 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001341#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001342 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1343 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001344#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001345#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001346 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1347 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001349#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001350 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1351 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001352#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001353#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001354 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1355 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001356#endif
1357#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001358 {
1359 PyObject *val;
1360 unsigned long bsec,bnsec;
1361 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001362#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001363 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001364#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001365 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001366#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001367 if (_stat_float_times) {
1368 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1369 } else {
1370 val = PyInt_FromLong((long)bsec);
1371 }
1372 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1373 val);
1374 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001376#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001377 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1378 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001379#endif
Fred Drake699f3522000-06-29 21:12:41 +00001380
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001381 if (PyErr_Occurred()) {
1382 Py_DECREF(v);
1383 return NULL;
1384 }
Fred Drake699f3522000-06-29 21:12:41 +00001385
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001386 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001387}
1388
Martin v. Löwisd8948722004-06-02 09:57:56 +00001389#ifdef MS_WINDOWS
1390
1391/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1392 where / can be used in place of \ and the trailing slash is optional.
1393 Both SERVER and SHARE must have at least one character.
1394*/
1395
1396#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1397#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001398#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001399#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001400#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001401
Tim Peters4ad82172004-08-30 17:02:04 +00001402static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001403IsUNCRootA(char *path, int pathlen)
1404{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001405 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001406
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001407 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001408
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001409 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1410 /* minimum UNCRoot is \\x\y */
1411 return FALSE;
1412 for (i = 2; i < pathlen ; i++)
1413 if (ISSLASH(path[i])) break;
1414 if (i == 2 || i == pathlen)
1415 /* do not allow \\\SHARE or \\SERVER */
1416 return FALSE;
1417 share = i+1;
1418 for (i = share; i < pathlen; i++)
1419 if (ISSLASH(path[i])) break;
1420 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001421
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001422 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001423}
1424
1425#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001426static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001427IsUNCRootW(Py_UNICODE *path, int pathlen)
1428{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001429 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001430
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001431 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001432
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001433 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1434 /* minimum UNCRoot is \\x\y */
1435 return FALSE;
1436 for (i = 2; i < pathlen ; i++)
1437 if (ISSLASH(path[i])) break;
1438 if (i == 2 || i == pathlen)
1439 /* do not allow \\\SHARE or \\SERVER */
1440 return FALSE;
1441 share = i+1;
1442 for (i = share; i < pathlen; i++)
1443 if (ISSLASH(path[i])) break;
1444 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001445
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001446 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001447}
1448#endif /* Py_WIN_WIDE_FILENAMES */
1449#endif /* MS_WINDOWS */
1450
Barry Warsaw53699e91996-12-10 23:23:01 +00001451static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001452posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001453 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001454#ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001455 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001456#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001457 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001458#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001459 char *wformat,
1460 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001461{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001462 STRUCT_STAT st;
1463 char *path = NULL; /* pass this to stat; do not free() it */
1464 char *pathfree = NULL; /* this memory must be free'd */
1465 int res;
1466 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001467
1468#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001469 /* If on wide-character-capable OS see if argument
1470 is Unicode and if so use wide API. */
1471 if (unicode_file_names()) {
1472 PyUnicodeObject *po;
1473 if (PyArg_ParseTuple(args, wformat, &po)) {
1474 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001475
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001476 Py_BEGIN_ALLOW_THREADS
1477 /* PyUnicode_AS_UNICODE result OK without
1478 thread lock as it is a simple dereference. */
1479 res = wstatfunc(wpath, &st);
1480 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001482 if (res != 0)
1483 return win32_error_unicode("stat", wpath);
1484 return _pystat_fromstructstat(&st);
1485 }
1486 /* Drop the argument parsing error as narrow strings
1487 are also valid. */
1488 PyErr_Clear();
1489 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001490#endif
1491
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001492 if (!PyArg_ParseTuple(args, format,
1493 Py_FileSystemDefaultEncoding, &path))
1494 return NULL;
1495 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001496
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001497 Py_BEGIN_ALLOW_THREADS
1498 res = (*statfunc)(path, &st);
1499 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001500
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001501 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001502#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001503 result = win32_error("stat", pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001504#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001505 result = posix_error_with_filename(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001506#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001507 }
1508 else
1509 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001510
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001511 PyMem_Free(pathfree);
1512 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001513}
1514
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001515/* POSIX methods */
1516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001518"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001519Use the real uid/gid to test for access to a path. Note that most\n\
1520operations will use the effective uid/gid, therefore this routine can\n\
1521be used in a suid/sgid environment to test if the invoking user has the\n\
1522specified access to the path. The mode argument can be F_OK to test\n\
1523existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001524
1525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001526posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001527{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001528 char *path;
1529 int mode;
1530
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001531#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001532 DWORD attr;
1533 if (unicode_file_names()) {
1534 PyUnicodeObject *po;
1535 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1536 Py_BEGIN_ALLOW_THREADS
1537 /* PyUnicode_AS_UNICODE OK without thread lock as
1538 it is a simple dereference. */
1539 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1540 Py_END_ALLOW_THREADS
1541 goto finish;
1542 }
1543 /* Drop the argument parsing error as narrow strings
1544 are also valid. */
1545 PyErr_Clear();
1546 }
1547 if (!PyArg_ParseTuple(args, "eti:access",
1548 Py_FileSystemDefaultEncoding, &path, &mode))
1549 return 0;
1550 Py_BEGIN_ALLOW_THREADS
1551 attr = GetFileAttributesA(path);
1552 Py_END_ALLOW_THREADS
1553 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001554finish:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001555 if (attr == 0xFFFFFFFF)
1556 /* File does not exist, or cannot read attributes */
1557 return PyBool_FromLong(0);
1558 /* Access is possible if either write access wasn't requested, or
1559 the file isn't read-only, or if it's a directory, as there are
1560 no read-only directories on Windows. */
1561 return PyBool_FromLong(!(mode & 2)
1562 || !(attr & FILE_ATTRIBUTE_READONLY)
1563 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001564#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001565 int res;
1566 if (!PyArg_ParseTuple(args, "eti:access",
1567 Py_FileSystemDefaultEncoding, &path, &mode))
1568 return NULL;
1569 Py_BEGIN_ALLOW_THREADS
1570 res = access(path, mode);
1571 Py_END_ALLOW_THREADS
1572 PyMem_Free(path);
1573 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001574#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001575}
1576
Guido van Rossumd371ff11999-01-25 16:12:23 +00001577#ifndef F_OK
1578#define F_OK 0
1579#endif
1580#ifndef R_OK
1581#define R_OK 4
1582#endif
1583#ifndef W_OK
1584#define W_OK 2
1585#endif
1586#ifndef X_OK
1587#define X_OK 1
1588#endif
1589
1590#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001592"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001594
1595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001596posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001597{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001598 int id;
1599 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001600
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001601 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1602 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001604#if defined(__VMS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001605 /* file descriptor 0 only, the default input device (stdin) */
1606 if (id == 0) {
1607 ret = ttyname();
1608 }
1609 else {
1610 ret = NULL;
1611 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001612#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001613 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001614#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001615 if (ret == NULL)
1616 return posix_error();
1617 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001618}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001620
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001621#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001623"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001625
1626static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001627posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001628{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001629 char *ret;
1630 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001631
Greg Wardb48bc172000-03-01 21:51:56 +00001632#ifdef USE_CTERMID_R
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001633 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001635 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001636#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001637 if (ret == NULL)
1638 return posix_error();
1639 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001640}
1641#endif
1642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001644"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001645Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001646
Barry Warsaw53699e91996-12-10 23:23:01 +00001647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001648posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001649{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001650#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001651 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001652#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001653 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001654#elif defined(__VMS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001655 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001657 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001658#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659}
1660
Fred Drake4d1e64b2002-04-15 19:40:07 +00001661#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001663"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001664Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001665opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001666
1667static PyObject *
1668posix_fchdir(PyObject *self, PyObject *fdobj)
1669{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001670 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001671}
1672#endif /* HAVE_FCHDIR */
1673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001675PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001676"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001677Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678
Barry Warsaw53699e91996-12-10 23:23:01 +00001679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001680posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001681{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001682 char *path = NULL;
1683 int i;
1684 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001685#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001686 DWORD attr;
1687 if (unicode_file_names()) {
1688 PyUnicodeObject *po;
1689 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1690 Py_BEGIN_ALLOW_THREADS
1691 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1692 if (attr != 0xFFFFFFFF) {
1693 if (i & _S_IWRITE)
1694 attr &= ~FILE_ATTRIBUTE_READONLY;
1695 else
1696 attr |= FILE_ATTRIBUTE_READONLY;
1697 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1698 }
1699 else
1700 res = 0;
1701 Py_END_ALLOW_THREADS
1702 if (!res)
1703 return win32_error_unicode("chmod",
1704 PyUnicode_AS_UNICODE(po));
1705 Py_INCREF(Py_None);
1706 return Py_None;
1707 }
1708 /* Drop the argument parsing error as narrow strings
1709 are also valid. */
1710 PyErr_Clear();
1711 }
1712 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1713 &path, &i))
1714 return NULL;
1715 Py_BEGIN_ALLOW_THREADS
1716 attr = GetFileAttributesA(path);
1717 if (attr != 0xFFFFFFFF) {
1718 if (i & _S_IWRITE)
1719 attr &= ~FILE_ATTRIBUTE_READONLY;
1720 else
1721 attr |= FILE_ATTRIBUTE_READONLY;
1722 res = SetFileAttributesA(path, attr);
1723 }
1724 else
1725 res = 0;
1726 Py_END_ALLOW_THREADS
1727 if (!res) {
1728 win32_error("chmod", path);
1729 PyMem_Free(path);
1730 return NULL;
1731 }
1732 PyMem_Free(path);
1733 Py_INCREF(Py_None);
1734 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001735#else /* Py_WIN_WIDE_FILENAMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001736 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1737 &path, &i))
1738 return NULL;
1739 Py_BEGIN_ALLOW_THREADS
1740 res = chmod(path, i);
1741 Py_END_ALLOW_THREADS
1742 if (res < 0)
1743 return posix_error_with_allocated_filename(path);
1744 PyMem_Free(path);
1745 Py_INCREF(Py_None);
1746 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001747#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001748}
1749
Christian Heimes36281872007-11-30 21:11:28 +00001750#ifdef HAVE_FCHMOD
1751PyDoc_STRVAR(posix_fchmod__doc__,
1752"fchmod(fd, mode)\n\n\
1753Change the access permissions of the file given by file\n\
1754descriptor fd.");
1755
1756static PyObject *
1757posix_fchmod(PyObject *self, PyObject *args)
1758{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001759 int fd, mode, res;
1760 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1761 return NULL;
1762 Py_BEGIN_ALLOW_THREADS
1763 res = fchmod(fd, mode);
1764 Py_END_ALLOW_THREADS
1765 if (res < 0)
1766 return posix_error();
1767 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001768}
1769#endif /* HAVE_FCHMOD */
1770
1771#ifdef HAVE_LCHMOD
1772PyDoc_STRVAR(posix_lchmod__doc__,
1773"lchmod(path, mode)\n\n\
1774Change the access permissions of a file. If path is a symlink, this\n\
1775affects the link itself rather than the target.");
1776
1777static PyObject *
1778posix_lchmod(PyObject *self, PyObject *args)
1779{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001780 char *path = NULL;
1781 int i;
1782 int res;
1783 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1784 &path, &i))
1785 return NULL;
1786 Py_BEGIN_ALLOW_THREADS
1787 res = lchmod(path, i);
1788 Py_END_ALLOW_THREADS
1789 if (res < 0)
1790 return posix_error_with_allocated_filename(path);
1791 PyMem_Free(path);
1792 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001793}
1794#endif /* HAVE_LCHMOD */
1795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001796
Martin v. Löwis382abef2007-02-19 10:55:19 +00001797#ifdef HAVE_CHFLAGS
1798PyDoc_STRVAR(posix_chflags__doc__,
1799"chflags(path, flags)\n\n\
1800Set file flags.");
1801
1802static PyObject *
1803posix_chflags(PyObject *self, PyObject *args)
1804{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001805 char *path;
1806 unsigned long flags;
1807 int res;
1808 if (!PyArg_ParseTuple(args, "etk:chflags",
1809 Py_FileSystemDefaultEncoding, &path, &flags))
1810 return NULL;
1811 Py_BEGIN_ALLOW_THREADS
1812 res = chflags(path, flags);
1813 Py_END_ALLOW_THREADS
1814 if (res < 0)
1815 return posix_error_with_allocated_filename(path);
1816 PyMem_Free(path);
1817 Py_INCREF(Py_None);
1818 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001819}
1820#endif /* HAVE_CHFLAGS */
1821
1822#ifdef HAVE_LCHFLAGS
1823PyDoc_STRVAR(posix_lchflags__doc__,
1824"lchflags(path, flags)\n\n\
1825Set file flags.\n\
1826This function will not follow symbolic links.");
1827
1828static PyObject *
1829posix_lchflags(PyObject *self, PyObject *args)
1830{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001831 char *path;
1832 unsigned long flags;
1833 int res;
1834 if (!PyArg_ParseTuple(args, "etk:lchflags",
1835 Py_FileSystemDefaultEncoding, &path, &flags))
1836 return NULL;
1837 Py_BEGIN_ALLOW_THREADS
1838 res = lchflags(path, flags);
1839 Py_END_ALLOW_THREADS
1840 if (res < 0)
1841 return posix_error_with_allocated_filename(path);
1842 PyMem_Free(path);
1843 Py_INCREF(Py_None);
1844 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001845}
1846#endif /* HAVE_LCHFLAGS */
1847
Martin v. Löwis244edc82001-10-04 22:44:26 +00001848#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001850"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001851Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001852
1853static PyObject *
1854posix_chroot(PyObject *self, PyObject *args)
1855{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001856 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001857}
1858#endif
1859
Guido van Rossum21142a01999-01-08 21:05:37 +00001860#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001862"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001864
1865static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001866posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001867{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001868 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001869}
1870#endif /* HAVE_FSYNC */
1871
1872#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001873
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001874#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001875extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1876#endif
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001880force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001882
1883static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001884posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001885{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001886 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001887}
1888#endif /* HAVE_FDATASYNC */
1889
1890
Fredrik Lundh10723342000-07-10 16:38:09 +00001891#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001892PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001893"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001895
Barry Warsaw53699e91996-12-10 23:23:01 +00001896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001897posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001898{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001899 char *path = NULL;
1900 long uid, gid;
1901 int res;
1902 if (!PyArg_ParseTuple(args, "etll:chown",
1903 Py_FileSystemDefaultEncoding, &path,
1904 &uid, &gid))
1905 return NULL;
1906 Py_BEGIN_ALLOW_THREADS
1907 res = chown(path, (uid_t) uid, (gid_t) gid);
1908 Py_END_ALLOW_THREADS
1909 if (res < 0)
1910 return posix_error_with_allocated_filename(path);
1911 PyMem_Free(path);
1912 Py_INCREF(Py_None);
1913 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001914}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001915#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001916
Christian Heimes36281872007-11-30 21:11:28 +00001917#ifdef HAVE_FCHOWN
1918PyDoc_STRVAR(posix_fchown__doc__,
1919"fchown(fd, uid, gid)\n\n\
1920Change the owner and group id of the file given by file descriptor\n\
1921fd to the numeric uid and gid.");
1922
1923static PyObject *
1924posix_fchown(PyObject *self, PyObject *args)
1925{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001926 int fd;
1927 long uid, gid;
1928 int res;
1929 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1930 return NULL;
1931 Py_BEGIN_ALLOW_THREADS
1932 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1933 Py_END_ALLOW_THREADS
1934 if (res < 0)
1935 return posix_error();
1936 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001937}
1938#endif /* HAVE_FCHOWN */
1939
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001940#ifdef HAVE_LCHOWN
1941PyDoc_STRVAR(posix_lchown__doc__,
1942"lchown(path, uid, gid)\n\n\
1943Change the owner and group id of path to the numeric uid and gid.\n\
1944This function will not follow symbolic links.");
1945
1946static PyObject *
1947posix_lchown(PyObject *self, PyObject *args)
1948{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001949 char *path = NULL;
1950 long uid, gid;
1951 int res;
1952 if (!PyArg_ParseTuple(args, "etll:lchown",
1953 Py_FileSystemDefaultEncoding, &path,
1954 &uid, &gid))
1955 return NULL;
1956 Py_BEGIN_ALLOW_THREADS
1957 res = lchown(path, (uid_t) uid, (gid_t) gid);
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öwis0cec0ff2002-07-28 16:33:45 +00001964}
1965#endif /* HAVE_LCHOWN */
1966
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001967
Guido van Rossum36bc6801995-06-14 22:54:23 +00001968#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001970"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001971Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001972
Stefan Krah36db84d2010-07-19 15:43:23 +00001973#if (defined(__sun) && defined(__SVR4)) || defined(__OpenBSD__)
1974/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
1975static PyObject *
1976posix_getcwd(PyObject *self, PyObject *noargs)
1977{
1978 char buf[PATH_MAX+2];
1979 char *res;
1980
1981 Py_BEGIN_ALLOW_THREADS
1982 res = getcwd(buf, sizeof buf);
1983 Py_END_ALLOW_THREADS
1984
1985 if (res == NULL)
1986 return posix_error();
1987
1988 return PyString_FromString(buf);
1989}
1990#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001991static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001992posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001993{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001994 int bufsize_incr = 1024;
1995 int bufsize = 0;
1996 char *tmpbuf = NULL;
1997 char *res = NULL;
1998 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001999
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002000 Py_BEGIN_ALLOW_THREADS
2001 do {
2002 bufsize = bufsize + bufsize_incr;
2003 tmpbuf = malloc(bufsize);
2004 if (tmpbuf == NULL) {
2005 break;
2006 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002007#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002008 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002009#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002010 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002011#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00002012
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002013 if (res == NULL) {
2014 free(tmpbuf);
2015 }
2016 } while ((res == NULL) && (errno == ERANGE));
2017 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002018
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002019 if (res == NULL)
2020 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002021
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002022 dynamic_return = PyString_FromString(tmpbuf);
2023 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002024
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002025 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002026}
Stefan Krah36db84d2010-07-19 15:43:23 +00002027#endif /* getcwd() NULL/ERANGE workaround. */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028
Walter Dörwald3b918c32002-11-21 20:18:46 +00002029#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030PyDoc_STRVAR(posix_getcwdu__doc__,
2031"getcwdu() -> path\n\n\
2032Return a unicode string representing the current working directory.");
2033
2034static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002035posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002036{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002037 char buf[1026];
2038 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002039
2040#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002041 DWORD len;
2042 if (unicode_file_names()) {
2043 wchar_t wbuf[1026];
2044 wchar_t *wbuf2 = wbuf;
2045 PyObject *resobj;
2046 Py_BEGIN_ALLOW_THREADS
2047 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2048 /* If the buffer is large enough, len does not include the
2049 terminating \0. If the buffer is too small, len includes
2050 the space needed for the terminator. */
2051 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2052 wbuf2 = malloc(len * sizeof(wchar_t));
2053 if (wbuf2)
2054 len = GetCurrentDirectoryW(len, wbuf2);
2055 }
2056 Py_END_ALLOW_THREADS
2057 if (!wbuf2) {
2058 PyErr_NoMemory();
2059 return NULL;
2060 }
2061 if (!len) {
2062 if (wbuf2 != wbuf) free(wbuf2);
2063 return win32_error("getcwdu", NULL);
2064 }
2065 resobj = PyUnicode_FromWideChar(wbuf2, len);
2066 if (wbuf2 != wbuf) free(wbuf2);
2067 return resobj;
2068 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002069#endif
2070
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002071 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002072#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002073 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002074#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002075 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002076#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002077 Py_END_ALLOW_THREADS
2078 if (res == NULL)
2079 return posix_error();
2080 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002081}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002082#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00002083#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085
Guido van Rossumb6775db1994-08-01 11:34:53 +00002086#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002087PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002088"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002089Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002090
Barry Warsaw53699e91996-12-10 23:23:01 +00002091static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002092posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002093{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002094 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002096#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002099PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002100"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002101Return a list containing the names of the entries in the directory.\n\
2102\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002103 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002104\n\
2105The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107
Barry Warsaw53699e91996-12-10 23:23:01 +00002108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002109posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002110{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002111 /* XXX Should redo this putting the (now four) versions of opendir
2112 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002113#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002114
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002115 PyObject *d, *v;
2116 HANDLE hFindFile;
2117 BOOL result;
2118 WIN32_FIND_DATA FileData;
2119 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2120 char *bufptr = namebuf;
2121 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002123#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002124 /* If on wide-character-capable OS see if argument
2125 is Unicode and if so use wide API. */
2126 if (unicode_file_names()) {
2127 PyObject *po;
2128 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2129 WIN32_FIND_DATAW wFileData;
2130 Py_UNICODE *wnamebuf;
2131 Py_UNICODE wch;
2132 /* Overallocate for \\*.*\0 */
2133 len = PyUnicode_GET_SIZE(po);
2134 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2135 if (!wnamebuf) {
2136 PyErr_NoMemory();
2137 return NULL;
2138 }
2139 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2140 wch = len > 0 ? wnamebuf[len-1] : '\0';
2141 if (wch != L'/' && wch != L'\\' && wch != L':')
2142 wnamebuf[len++] = L'\\';
2143 wcscpy(wnamebuf + len, L"*.*");
2144 if ((d = PyList_New(0)) == NULL) {
2145 free(wnamebuf);
2146 return NULL;
2147 }
2148 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2149 if (hFindFile == INVALID_HANDLE_VALUE) {
2150 int error = GetLastError();
2151 if (error == ERROR_FILE_NOT_FOUND) {
2152 free(wnamebuf);
2153 return d;
2154 }
2155 Py_DECREF(d);
2156 win32_error_unicode("FindFirstFileW", wnamebuf);
2157 free(wnamebuf);
2158 return NULL;
2159 }
2160 do {
2161 /* Skip over . and .. */
2162 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2163 wcscmp(wFileData.cFileName, L"..") != 0) {
2164 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2165 if (v == NULL) {
2166 Py_DECREF(d);
2167 d = NULL;
2168 break;
2169 }
2170 if (PyList_Append(d, v) != 0) {
2171 Py_DECREF(v);
2172 Py_DECREF(d);
2173 d = NULL;
2174 break;
2175 }
2176 Py_DECREF(v);
2177 }
2178 Py_BEGIN_ALLOW_THREADS
2179 result = FindNextFileW(hFindFile, &wFileData);
2180 Py_END_ALLOW_THREADS
2181 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2182 it got to the end of the directory. */
2183 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2184 Py_DECREF(d);
2185 win32_error_unicode("FindNextFileW", wnamebuf);
2186 FindClose(hFindFile);
2187 free(wnamebuf);
2188 return NULL;
2189 }
2190 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002191
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002192 if (FindClose(hFindFile) == FALSE) {
2193 Py_DECREF(d);
2194 win32_error_unicode("FindClose", wnamebuf);
2195 free(wnamebuf);
2196 return NULL;
2197 }
2198 free(wnamebuf);
2199 return d;
2200 }
2201 /* Drop the argument parsing error as narrow strings
2202 are also valid. */
2203 PyErr_Clear();
2204 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002205#endif
2206
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002207 if (!PyArg_ParseTuple(args, "et#:listdir",
2208 Py_FileSystemDefaultEncoding, &bufptr, &len))
2209 return NULL;
2210 if (len > 0) {
2211 char ch = namebuf[len-1];
2212 if (ch != SEP && ch != ALTSEP && ch != ':')
2213 namebuf[len++] = '/';
2214 }
2215 strcpy(namebuf + len, "*.*");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002216
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002217 if ((d = PyList_New(0)) == NULL)
2218 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002219
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002220 hFindFile = FindFirstFile(namebuf, &FileData);
2221 if (hFindFile == INVALID_HANDLE_VALUE) {
2222 int error = GetLastError();
2223 if (error == ERROR_FILE_NOT_FOUND)
2224 return d;
2225 Py_DECREF(d);
2226 return win32_error("FindFirstFile", namebuf);
2227 }
2228 do {
2229 /* Skip over . and .. */
2230 if (strcmp(FileData.cFileName, ".") != 0 &&
2231 strcmp(FileData.cFileName, "..") != 0) {
2232 v = PyString_FromString(FileData.cFileName);
2233 if (v == NULL) {
2234 Py_DECREF(d);
2235 d = NULL;
2236 break;
2237 }
2238 if (PyList_Append(d, v) != 0) {
2239 Py_DECREF(v);
2240 Py_DECREF(d);
2241 d = NULL;
2242 break;
2243 }
2244 Py_DECREF(v);
2245 }
2246 Py_BEGIN_ALLOW_THREADS
2247 result = FindNextFile(hFindFile, &FileData);
2248 Py_END_ALLOW_THREADS
2249 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2250 it got to the end of the directory. */
2251 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2252 Py_DECREF(d);
2253 win32_error("FindNextFile", namebuf);
2254 FindClose(hFindFile);
2255 return NULL;
2256 }
2257 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002258
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002259 if (FindClose(hFindFile) == FALSE) {
2260 Py_DECREF(d);
2261 return win32_error("FindClose", namebuf);
2262 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002263
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002264 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002265
Tim Peters0bb44a42000-09-15 07:44:49 +00002266#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002267
2268#ifndef MAX_PATH
2269#define MAX_PATH CCHMAXPATH
2270#endif
2271 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002272 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 PyObject *d, *v;
2274 char namebuf[MAX_PATH+5];
2275 HDIR hdir = 1;
2276 ULONG srchcnt = 1;
2277 FILEFINDBUF3 ep;
2278 APIRET rc;
2279
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002280 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281 return NULL;
2282 if (len >= MAX_PATH) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002283 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002284 return NULL;
2285 }
2286 strcpy(namebuf, name);
2287 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002288 if (*pt == ALTSEP)
2289 *pt = SEP;
2290 if (namebuf[len-1] != SEP)
2291 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002292 strcpy(namebuf + len, "*.*");
2293
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002294 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002295 return NULL;
2296
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002297 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2298 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002299 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002300 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2301 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2302 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002303
2304 if (rc != NO_ERROR) {
2305 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002306 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002307 }
2308
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002309 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002310 do {
2311 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002312 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002313 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002314
2315 strcpy(namebuf, ep.achName);
2316
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002317 /* Leave Case of Name Alone -- In Native Form */
2318 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002319
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002320 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002321 if (v == NULL) {
2322 Py_DECREF(d);
2323 d = NULL;
2324 break;
2325 }
2326 if (PyList_Append(d, v) != 0) {
2327 Py_DECREF(v);
2328 Py_DECREF(d);
2329 d = NULL;
2330 break;
2331 }
2332 Py_DECREF(v);
2333 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2334 }
2335
2336 return d;
2337#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002338
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002339 char *name = NULL;
2340 PyObject *d, *v;
2341 DIR *dirp;
2342 struct dirent *ep;
2343 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002344
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002345 errno = 0;
2346 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2347 arg_is_unicode = 0;
2348 PyErr_Clear();
2349 }
2350 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2351 return NULL;
2352 if ((dirp = opendir(name)) == NULL) {
2353 return posix_error_with_allocated_filename(name);
2354 }
2355 if ((d = PyList_New(0)) == NULL) {
2356 closedir(dirp);
2357 PyMem_Free(name);
2358 return NULL;
2359 }
2360 for (;;) {
2361 errno = 0;
2362 Py_BEGIN_ALLOW_THREADS
2363 ep = readdir(dirp);
2364 Py_END_ALLOW_THREADS
2365 if (ep == NULL) {
2366 if (errno == 0) {
2367 break;
2368 } else {
2369 closedir(dirp);
2370 Py_DECREF(d);
2371 return posix_error_with_allocated_filename(name);
2372 }
2373 }
2374 if (ep->d_name[0] == '.' &&
2375 (NAMLEN(ep) == 1 ||
2376 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2377 continue;
2378 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2379 if (v == NULL) {
2380 Py_DECREF(d);
2381 d = NULL;
2382 break;
2383 }
Just van Rossum46c97842003-02-25 21:42:15 +00002384#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002385 if (arg_is_unicode) {
2386 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002387
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002388 w = PyUnicode_FromEncodedObject(v,
2389 Py_FileSystemDefaultEncoding,
2390 "strict");
2391 if (w != NULL) {
2392 Py_DECREF(v);
2393 v = w;
2394 }
2395 else {
2396 /* fall back to the original byte string, as
2397 discussed in patch #683592 */
2398 PyErr_Clear();
2399 }
2400 }
Just van Rossum46c97842003-02-25 21:42:15 +00002401#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002402 if (PyList_Append(d, v) != 0) {
2403 Py_DECREF(v);
2404 Py_DECREF(d);
2405 d = NULL;
2406 break;
2407 }
2408 Py_DECREF(v);
2409 }
2410 closedir(dirp);
2411 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002412
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002413 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002414
Tim Peters0bb44a42000-09-15 07:44:49 +00002415#endif /* which OS */
2416} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002417
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002418#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002419/* A helper function for abspath on win32 */
2420static PyObject *
2421posix__getfullpathname(PyObject *self, PyObject *args)
2422{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002423 /* assume encoded strings won't more than double no of chars */
2424 char inbuf[MAX_PATH*2];
2425 char *inbufp = inbuf;
2426 Py_ssize_t insize = sizeof(inbuf);
2427 char outbuf[MAX_PATH*2];
2428 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002429#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002430 if (unicode_file_names()) {
2431 PyUnicodeObject *po;
2432 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2433 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2434 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2435 Py_UNICODE *wtemp;
2436 DWORD result;
2437 PyObject *v;
2438 result = GetFullPathNameW(wpath,
2439 sizeof(woutbuf)/sizeof(woutbuf[0]),
2440 woutbuf, &wtemp);
2441 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2442 woutbufp = malloc(result * sizeof(Py_UNICODE));
2443 if (!woutbufp)
2444 return PyErr_NoMemory();
2445 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2446 }
2447 if (result)
2448 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2449 else
2450 v = win32_error_unicode("GetFullPathNameW", wpath);
2451 if (woutbufp != woutbuf)
2452 free(woutbufp);
2453 return v;
2454 }
2455 /* Drop the argument parsing error as narrow strings
2456 are also valid. */
2457 PyErr_Clear();
2458 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002460 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2461 Py_FileSystemDefaultEncoding, &inbufp,
2462 &insize))
2463 return NULL;
2464 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2465 outbuf, &temp))
2466 return win32_error("GetFullPathName", inbuf);
2467 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2468 return PyUnicode_Decode(outbuf, strlen(outbuf),
2469 Py_FileSystemDefaultEncoding, NULL);
2470 }
2471 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002472} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002473#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002476"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002477Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002478
Barry Warsaw53699e91996-12-10 23:23:01 +00002479static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002480posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002481{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002482 int res;
2483 char *path = NULL;
2484 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002485
2486#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002487 if (unicode_file_names()) {
2488 PyUnicodeObject *po;
2489 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2490 Py_BEGIN_ALLOW_THREADS
2491 /* PyUnicode_AS_UNICODE OK without thread lock as
2492 it is a simple dereference. */
2493 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2494 Py_END_ALLOW_THREADS
2495 if (!res)
2496 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2497 Py_INCREF(Py_None);
2498 return Py_None;
2499 }
2500 /* Drop the argument parsing error as narrow strings
2501 are also valid. */
2502 PyErr_Clear();
2503 }
2504 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2505 Py_FileSystemDefaultEncoding, &path, &mode))
2506 return NULL;
2507 Py_BEGIN_ALLOW_THREADS
2508 /* PyUnicode_AS_UNICODE OK without thread lock as
2509 it is a simple dereference. */
2510 res = CreateDirectoryA(path, NULL);
2511 Py_END_ALLOW_THREADS
2512 if (!res) {
2513 win32_error("mkdir", path);
2514 PyMem_Free(path);
2515 return NULL;
2516 }
2517 PyMem_Free(path);
2518 Py_INCREF(Py_None);
2519 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002520#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002521
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002522 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2523 Py_FileSystemDefaultEncoding, &path, &mode))
2524 return NULL;
2525 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002526#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002527 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002528#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002529 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002530#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002531 Py_END_ALLOW_THREADS
2532 if (res < 0)
2533 return posix_error_with_allocated_filename(path);
2534 PyMem_Free(path);
2535 Py_INCREF(Py_None);
2536 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002537#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002538}
2539
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002540
Neal Norwitz1818ed72006-03-26 00:29:48 +00002541/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2542#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002543#include <sys/resource.h>
2544#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002545
Neal Norwitz1818ed72006-03-26 00:29:48 +00002546
2547#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002549"nice(inc) -> new_priority\n\n\
2550Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Barry Warsaw53699e91996-12-10 23:23:01 +00002552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002553posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002554{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002555 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002556
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002557 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2558 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002559
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002560 /* There are two flavours of 'nice': one that returns the new
2561 priority (as required by almost all standards out there) and the
2562 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2563 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002564
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002565 If we are of the nice family that returns the new priority, we
2566 need to clear errno before the call, and check if errno is filled
2567 before calling posix_error() on a returnvalue of -1, because the
2568 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002569
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002570 errno = 0;
2571 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002572#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002573 if (value == 0)
2574 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002575#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002576 if (value == -1 && errno != 0)
2577 /* either nice() or getpriority() returned an error */
2578 return posix_error();
2579 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002580}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002581#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002582
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002583PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002584"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002585Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002586
Barry Warsaw53699e91996-12-10 23:23:01 +00002587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002588posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002589{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002590#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002591 PyObject *o1, *o2;
2592 char *p1, *p2;
2593 BOOL result;
2594 if (unicode_file_names()) {
2595 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2596 goto error;
2597 if (!convert_to_unicode(&o1))
2598 goto error;
2599 if (!convert_to_unicode(&o2)) {
2600 Py_DECREF(o1);
2601 goto error;
2602 }
2603 Py_BEGIN_ALLOW_THREADS
2604 result = MoveFileW(PyUnicode_AsUnicode(o1),
2605 PyUnicode_AsUnicode(o2));
2606 Py_END_ALLOW_THREADS
2607 Py_DECREF(o1);
2608 Py_DECREF(o2);
2609 if (!result)
2610 return win32_error("rename", NULL);
2611 Py_INCREF(Py_None);
2612 return Py_None;
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002613error:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002614 PyErr_Clear();
2615 }
2616 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2617 return NULL;
2618 Py_BEGIN_ALLOW_THREADS
2619 result = MoveFileA(p1, p2);
2620 Py_END_ALLOW_THREADS
2621 if (!result)
2622 return win32_error("rename", NULL);
2623 Py_INCREF(Py_None);
2624 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002626 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002627#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002628}
2629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002632"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002634
Barry Warsaw53699e91996-12-10 23:23:01 +00002635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002636posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002637{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002639 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002640#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002641 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002643}
2644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002646PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002647"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002648Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002649
Barry Warsaw53699e91996-12-10 23:23:01 +00002650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002651posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002653#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002654 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002655#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002656 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002657#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002658}
2659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002661#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002662PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002663"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002664Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002665
Barry Warsaw53699e91996-12-10 23:23:01 +00002666static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002667posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002668{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002669 char *command;
2670 long sts;
2671 if (!PyArg_ParseTuple(args, "s:system", &command))
2672 return NULL;
2673 Py_BEGIN_ALLOW_THREADS
2674 sts = system(command);
2675 Py_END_ALLOW_THREADS
2676 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002677}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002682"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002683Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002684
Barry Warsaw53699e91996-12-10 23:23:01 +00002685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002686posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002687{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002688 int i;
2689 if (!PyArg_ParseTuple(args, "i:umask", &i))
2690 return NULL;
2691 i = (int)umask(i);
2692 if (i < 0)
2693 return posix_error();
2694 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695}
2696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002699"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002703"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002705
Barry Warsaw53699e91996-12-10 23:23:01 +00002706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002707posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002708{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002709#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002710 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002711#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002712 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002713#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002714}
2715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Guido van Rossumb6775db1994-08-01 11:34:53 +00002717#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002719"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002720Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721
Barry Warsaw53699e91996-12-10 23:23:01 +00002722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002723posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002724{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002725 struct utsname u;
2726 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002727
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002728 Py_BEGIN_ALLOW_THREADS
2729 res = uname(&u);
2730 Py_END_ALLOW_THREADS
2731 if (res < 0)
2732 return posix_error();
2733 return Py_BuildValue("(sssss)",
2734 u.sysname,
2735 u.nodename,
2736 u.release,
2737 u.version,
2738 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002739}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002740#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002741
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002742static int
2743extract_time(PyObject *t, long* sec, long* usec)
2744{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002745 long intval;
2746 if (PyFloat_Check(t)) {
2747 double tval = PyFloat_AsDouble(t);
2748 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2749 if (!intobj)
2750 return -1;
2751 intval = PyInt_AsLong(intobj);
2752 Py_DECREF(intobj);
2753 if (intval == -1 && PyErr_Occurred())
2754 return -1;
2755 *sec = intval;
2756 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2757 if (*usec < 0)
2758 /* If rounding gave us a negative number,
2759 truncate. */
2760 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002761 return 0;
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002762 }
2763 intval = PyInt_AsLong(t);
2764 if (intval == -1 && PyErr_Occurred())
2765 return -1;
2766 *sec = intval;
2767 *usec = 0;
2768 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002769}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002772"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002773utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002774Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Barry Warsaw53699e91996-12-10 23:23:01 +00002777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002778posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002779{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002780#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002781 PyObject *arg;
2782 PyUnicodeObject *obwpath;
2783 wchar_t *wpath = NULL;
2784 char *apath = NULL;
2785 HANDLE hFile;
2786 long atimesec, mtimesec, ausec, musec;
2787 FILETIME atime, mtime;
2788 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002789
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002790 if (unicode_file_names()) {
2791 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2792 wpath = PyUnicode_AS_UNICODE(obwpath);
2793 Py_BEGIN_ALLOW_THREADS
2794 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2795 NULL, OPEN_EXISTING,
2796 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2797 Py_END_ALLOW_THREADS
2798 if (hFile == INVALID_HANDLE_VALUE)
2799 return win32_error_unicode("utime", wpath);
2800 } else
2801 /* Drop the argument parsing error as narrow strings
2802 are also valid. */
2803 PyErr_Clear();
2804 }
2805 if (!wpath) {
2806 if (!PyArg_ParseTuple(args, "etO:utime",
2807 Py_FileSystemDefaultEncoding, &apath, &arg))
2808 return NULL;
2809 Py_BEGIN_ALLOW_THREADS
2810 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2811 NULL, OPEN_EXISTING,
2812 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2813 Py_END_ALLOW_THREADS
2814 if (hFile == INVALID_HANDLE_VALUE) {
2815 win32_error("utime", apath);
2816 PyMem_Free(apath);
2817 return NULL;
2818 }
2819 PyMem_Free(apath);
2820 }
2821
2822 if (arg == Py_None) {
2823 SYSTEMTIME now;
2824 GetSystemTime(&now);
2825 if (!SystemTimeToFileTime(&now, &mtime) ||
2826 !SystemTimeToFileTime(&now, &atime)) {
2827 win32_error("utime", NULL);
2828 goto done;
2829 }
2830 }
2831 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2832 PyErr_SetString(PyExc_TypeError,
2833 "utime() arg 2 must be a tuple (atime, mtime)");
2834 goto done;
2835 }
2836 else {
2837 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2838 &atimesec, &ausec) == -1)
2839 goto done;
2840 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2841 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2842 &mtimesec, &musec) == -1)
2843 goto done;
2844 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2845 }
2846 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2847 /* Avoid putting the file name into the error here,
2848 as that may confuse the user into believing that
2849 something is wrong with the file, when it also
2850 could be the time stamp that gives a problem. */
2851 win32_error("utime", NULL);
2852 }
2853 Py_INCREF(Py_None);
2854 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002855done:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002856 CloseHandle(hFile);
2857 return result;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002858#else /* Py_WIN_WIDE_FILENAMES */
2859
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002860 char *path = NULL;
2861 long atime, mtime, ausec, musec;
2862 int res;
2863 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002864
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002865#if defined(HAVE_UTIMES)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002866 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002867#define ATIME buf[0].tv_sec
2868#define MTIME buf[1].tv_sec
2869#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002870/* XXX should define struct utimbuf instead, above */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002871 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002872#define ATIME buf.actime
2873#define MTIME buf.modtime
2874#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002875#else /* HAVE_UTIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002876 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002877#define ATIME buf[0]
2878#define MTIME buf[1]
2879#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002880#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002881
Mark Hammond817c9292003-12-03 01:22:38 +00002882
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002883 if (!PyArg_ParseTuple(args, "etO:utime",
2884 Py_FileSystemDefaultEncoding, &path, &arg))
2885 return NULL;
2886 if (arg == Py_None) {
2887 /* optional time values not given */
2888 Py_BEGIN_ALLOW_THREADS
2889 res = utime(path, NULL);
2890 Py_END_ALLOW_THREADS
2891 }
2892 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2893 PyErr_SetString(PyExc_TypeError,
2894 "utime() arg 2 must be a tuple (atime, mtime)");
2895 PyMem_Free(path);
2896 return NULL;
2897 }
2898 else {
2899 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2900 &atime, &ausec) == -1) {
2901 PyMem_Free(path);
2902 return NULL;
2903 }
2904 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2905 &mtime, &musec) == -1) {
2906 PyMem_Free(path);
2907 return NULL;
2908 }
2909 ATIME = atime;
2910 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002911#ifdef HAVE_UTIMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002912 buf[0].tv_usec = ausec;
2913 buf[1].tv_usec = musec;
2914 Py_BEGIN_ALLOW_THREADS
2915 res = utimes(path, buf);
2916 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002917#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002918 Py_BEGIN_ALLOW_THREADS
2919 res = utime(path, UTIME_ARG);
2920 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002921#endif /* HAVE_UTIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002922 }
2923 if (res < 0) {
2924 return posix_error_with_allocated_filename(path);
2925 }
2926 PyMem_Free(path);
2927 Py_INCREF(Py_None);
2928 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002929#undef UTIME_ARG
2930#undef ATIME
2931#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002932#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002933}
2934
Guido van Rossum85e3b011991-06-03 12:42:10 +00002935
Guido van Rossum3b066191991-06-04 19:40:25 +00002936/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002937
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002938PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002939"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002941
Barry Warsaw53699e91996-12-10 23:23:01 +00002942static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002943posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002944{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002945 int sts;
2946 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2947 return NULL;
2948 _exit(sts);
2949 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002950}
2951
Martin v. Löwis114619e2002-10-07 06:44:21 +00002952#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2953static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002954free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002955{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002956 Py_ssize_t i;
2957 for (i = 0; i < count; i++)
2958 PyMem_Free(array[i]);
2959 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002960}
2961#endif
2962
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002963
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002964#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002965PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002966"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002967Execute an executable path with arguments, replacing current process.\n\
2968\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002969 path: path of executable file\n\
2970 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002971
Barry Warsaw53699e91996-12-10 23:23:01 +00002972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002973posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002974{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002975 char *path;
2976 PyObject *argv;
2977 char **argvlist;
2978 Py_ssize_t i, argc;
2979 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002980
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002981 /* execv has two arguments: (path, argv), where
2982 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002983
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002984 if (!PyArg_ParseTuple(args, "etO:execv",
2985 Py_FileSystemDefaultEncoding,
2986 &path, &argv))
2987 return NULL;
2988 if (PyList_Check(argv)) {
2989 argc = PyList_Size(argv);
2990 getitem = PyList_GetItem;
2991 }
2992 else if (PyTuple_Check(argv)) {
2993 argc = PyTuple_Size(argv);
2994 getitem = PyTuple_GetItem;
2995 }
2996 else {
2997 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
2998 PyMem_Free(path);
2999 return NULL;
3000 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003001
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003002 argvlist = PyMem_NEW(char *, argc+1);
3003 if (argvlist == NULL) {
3004 PyMem_Free(path);
3005 return PyErr_NoMemory();
3006 }
3007 for (i = 0; i < argc; i++) {
3008 if (!PyArg_Parse((*getitem)(argv, i), "et",
3009 Py_FileSystemDefaultEncoding,
3010 &argvlist[i])) {
3011 free_string_array(argvlist, i);
3012 PyErr_SetString(PyExc_TypeError,
3013 "execv() arg 2 must contain only strings");
3014 PyMem_Free(path);
3015 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003016
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003017 }
3018 }
3019 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003020
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003021 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003022
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003023 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003024
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003025 free_string_array(argvlist, argc);
3026 PyMem_Free(path);
3027 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003028}
3029
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003031PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003032"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003033Execute a path with arguments and environment, replacing current process.\n\
3034\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003035 path: path of executable file\n\
3036 args: tuple or list of arguments\n\
3037 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003038
Barry Warsaw53699e91996-12-10 23:23:01 +00003039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003040posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003041{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003042 char *path;
3043 PyObject *argv, *env;
3044 char **argvlist;
3045 char **envlist;
3046 PyObject *key, *val, *keys=NULL, *vals=NULL;
3047 Py_ssize_t i, pos, argc, envc;
3048 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3049 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003050
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003051 /* execve has three arguments: (path, argv, env), where
3052 argv is a list or tuple of strings and env is a dictionary
3053 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003054
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003055 if (!PyArg_ParseTuple(args, "etOO:execve",
3056 Py_FileSystemDefaultEncoding,
3057 &path, &argv, &env))
3058 return NULL;
3059 if (PyList_Check(argv)) {
3060 argc = PyList_Size(argv);
3061 getitem = PyList_GetItem;
3062 }
3063 else if (PyTuple_Check(argv)) {
3064 argc = PyTuple_Size(argv);
3065 getitem = PyTuple_GetItem;
3066 }
3067 else {
3068 PyErr_SetString(PyExc_TypeError,
3069 "execve() arg 2 must be a tuple or list");
3070 goto fail_0;
3071 }
3072 if (!PyMapping_Check(env)) {
3073 PyErr_SetString(PyExc_TypeError,
3074 "execve() arg 3 must be a mapping object");
3075 goto fail_0;
3076 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003077
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003078 argvlist = PyMem_NEW(char *, argc+1);
3079 if (argvlist == NULL) {
3080 PyErr_NoMemory();
3081 goto fail_0;
3082 }
3083 for (i = 0; i < argc; i++) {
3084 if (!PyArg_Parse((*getitem)(argv, i),
3085 "et;execve() arg 2 must contain only strings",
3086 Py_FileSystemDefaultEncoding,
3087 &argvlist[i]))
3088 {
3089 lastarg = i;
3090 goto fail_1;
3091 }
3092 }
3093 lastarg = argc;
3094 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003095
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003096 i = PyMapping_Size(env);
3097 if (i < 0)
3098 goto fail_1;
3099 envlist = PyMem_NEW(char *, i + 1);
3100 if (envlist == NULL) {
3101 PyErr_NoMemory();
3102 goto fail_1;
3103 }
3104 envc = 0;
3105 keys = PyMapping_Keys(env);
3106 vals = PyMapping_Values(env);
3107 if (!keys || !vals)
3108 goto fail_2;
3109 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3110 PyErr_SetString(PyExc_TypeError,
3111 "execve(): env.keys() or env.values() is not a list");
3112 goto fail_2;
3113 }
Tim Peters5aa91602002-01-30 05:46:57 +00003114
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003115 for (pos = 0; pos < i; pos++) {
3116 char *p, *k, *v;
3117 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003118
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003119 key = PyList_GetItem(keys, pos);
3120 val = PyList_GetItem(vals, pos);
3121 if (!key || !val)
3122 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003123
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003124 if (!PyArg_Parse(
3125 key,
3126 "s;execve() arg 3 contains a non-string key",
3127 &k) ||
3128 !PyArg_Parse(
3129 val,
3130 "s;execve() arg 3 contains a non-string value",
3131 &v))
3132 {
3133 goto fail_2;
3134 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003135
3136#if defined(PYOS_OS2)
3137 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3138 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3139#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003140 len = PyString_Size(key) + PyString_Size(val) + 2;
3141 p = PyMem_NEW(char, len);
3142 if (p == NULL) {
3143 PyErr_NoMemory();
3144 goto fail_2;
3145 }
3146 PyOS_snprintf(p, len, "%s=%s", k, v);
3147 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003148#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003149 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003150#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003151 }
3152 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003154 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003155
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003156 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003157
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003158 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003159
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003160 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003161 while (--envc >= 0)
3162 PyMem_DEL(envlist[envc]);
3163 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003164 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003165 free_string_array(argvlist, lastarg);
3166 Py_XDECREF(vals);
3167 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003168 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003169 PyMem_Free(path);
3170 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003171}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003172#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174
Guido van Rossuma1065681999-01-25 23:20:23 +00003175#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003176PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003177"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003178Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003179\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003180 mode: mode of process creation\n\
3181 path: path of executable file\n\
3182 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003183
3184static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003185posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003186{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003187 char *path;
3188 PyObject *argv;
3189 char **argvlist;
3190 int mode, i;
3191 Py_ssize_t argc;
3192 Py_intptr_t spawnval;
3193 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003194
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003195 /* spawnv has three arguments: (mode, path, argv), where
3196 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003197
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003198 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3199 Py_FileSystemDefaultEncoding,
3200 &path, &argv))
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 "spawnv() arg 2 must be a tuple or list");
3213 PyMem_Free(path);
3214 return NULL;
3215 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003216
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003217 argvlist = PyMem_NEW(char *, argc+1);
3218 if (argvlist == NULL) {
3219 PyMem_Free(path);
3220 return PyErr_NoMemory();
3221 }
3222 for (i = 0; i < argc; i++) {
3223 if (!PyArg_Parse((*getitem)(argv, i), "et",
3224 Py_FileSystemDefaultEncoding,
3225 &argvlist[i])) {
3226 free_string_array(argvlist, i);
3227 PyErr_SetString(
3228 PyExc_TypeError,
3229 "spawnv() arg 2 must contain only strings");
3230 PyMem_Free(path);
3231 return NULL;
3232 }
3233 }
3234 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003235
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003236#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003237 Py_BEGIN_ALLOW_THREADS
3238 spawnval = spawnv(mode, path, argvlist);
3239 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003240#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003241 if (mode == _OLD_P_OVERLAY)
3242 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003243
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003244 Py_BEGIN_ALLOW_THREADS
3245 spawnval = _spawnv(mode, path, argvlist);
3246 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003247#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003248
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003249 free_string_array(argvlist, argc);
3250 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003251
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003252 if (spawnval == -1)
3253 return posix_error();
3254 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003255#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003256 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003257#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003258 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003259#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003260}
3261
3262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003263PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003264"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003265Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003266\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003267 mode: mode of process creation\n\
3268 path: path of executable file\n\
3269 args: tuple or list of arguments\n\
3270 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003271
3272static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003273posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003274{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003275 char *path;
3276 PyObject *argv, *env;
3277 char **argvlist;
3278 char **envlist;
3279 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3280 int mode, pos, envc;
3281 Py_ssize_t argc, i;
3282 Py_intptr_t spawnval;
3283 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3284 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003285
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003286 /* spawnve has four arguments: (mode, path, argv, env), where
3287 argv is a list or tuple of strings and env is a dictionary
3288 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003289
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003290 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3291 Py_FileSystemDefaultEncoding,
3292 &path, &argv, &env))
3293 return NULL;
3294 if (PyList_Check(argv)) {
3295 argc = PyList_Size(argv);
3296 getitem = PyList_GetItem;
3297 }
3298 else if (PyTuple_Check(argv)) {
3299 argc = PyTuple_Size(argv);
3300 getitem = PyTuple_GetItem;
3301 }
3302 else {
3303 PyErr_SetString(PyExc_TypeError,
3304 "spawnve() arg 2 must be a tuple or list");
3305 goto fail_0;
3306 }
3307 if (!PyMapping_Check(env)) {
3308 PyErr_SetString(PyExc_TypeError,
3309 "spawnve() arg 3 must be a mapping object");
3310 goto fail_0;
3311 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003312
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003313 argvlist = PyMem_NEW(char *, argc+1);
3314 if (argvlist == NULL) {
3315 PyErr_NoMemory();
3316 goto fail_0;
3317 }
3318 for (i = 0; i < argc; i++) {
3319 if (!PyArg_Parse((*getitem)(argv, i),
3320 "et;spawnve() arg 2 must contain only strings",
3321 Py_FileSystemDefaultEncoding,
3322 &argvlist[i]))
3323 {
3324 lastarg = i;
3325 goto fail_1;
3326 }
3327 }
3328 lastarg = argc;
3329 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003330
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003331 i = PyMapping_Size(env);
3332 if (i < 0)
3333 goto fail_1;
3334 envlist = PyMem_NEW(char *, i + 1);
3335 if (envlist == NULL) {
3336 PyErr_NoMemory();
3337 goto fail_1;
3338 }
3339 envc = 0;
3340 keys = PyMapping_Keys(env);
3341 vals = PyMapping_Values(env);
3342 if (!keys || !vals)
3343 goto fail_2;
3344 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3345 PyErr_SetString(PyExc_TypeError,
3346 "spawnve(): env.keys() or env.values() is not a list");
3347 goto fail_2;
3348 }
Tim Peters5aa91602002-01-30 05:46:57 +00003349
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003350 for (pos = 0; pos < i; pos++) {
3351 char *p, *k, *v;
3352 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003353
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003354 key = PyList_GetItem(keys, pos);
3355 val = PyList_GetItem(vals, pos);
3356 if (!key || !val)
3357 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003358
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003359 if (!PyArg_Parse(
3360 key,
3361 "s;spawnve() arg 3 contains a non-string key",
3362 &k) ||
3363 !PyArg_Parse(
3364 val,
3365 "s;spawnve() arg 3 contains a non-string value",
3366 &v))
3367 {
3368 goto fail_2;
3369 }
3370 len = PyString_Size(key) + PyString_Size(val) + 2;
3371 p = PyMem_NEW(char, len);
3372 if (p == NULL) {
3373 PyErr_NoMemory();
3374 goto fail_2;
3375 }
3376 PyOS_snprintf(p, len, "%s=%s", k, v);
3377 envlist[envc++] = p;
3378 }
3379 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003380
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003381#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003382 Py_BEGIN_ALLOW_THREADS
3383 spawnval = spawnve(mode, path, argvlist, envlist);
3384 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003385#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003386 if (mode == _OLD_P_OVERLAY)
3387 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003388
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003389 Py_BEGIN_ALLOW_THREADS
3390 spawnval = _spawnve(mode, path, argvlist, envlist);
3391 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003392#endif
Tim Peters25059d32001-12-07 20:35:43 +00003393
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003394 if (spawnval == -1)
3395 (void) posix_error();
3396 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003397#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003398 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003399#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003400 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003401#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003402
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003403 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003404 while (--envc >= 0)
3405 PyMem_DEL(envlist[envc]);
3406 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003407 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003408 free_string_array(argvlist, lastarg);
3409 Py_XDECREF(vals);
3410 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003411 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003412 PyMem_Free(path);
3413 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003414}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003415
3416/* OS/2 supports spawnvp & spawnvpe natively */
3417#if defined(PYOS_OS2)
3418PyDoc_STRVAR(posix_spawnvp__doc__,
3419"spawnvp(mode, file, args)\n\n\
3420Execute the program 'file' in a new process, using the environment\n\
3421search path to find the file.\n\
3422\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003423 mode: mode of process creation\n\
3424 file: executable file name\n\
3425 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003426
3427static PyObject *
3428posix_spawnvp(PyObject *self, PyObject *args)
3429{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003430 char *path;
3431 PyObject *argv;
3432 char **argvlist;
3433 int mode, i, argc;
3434 Py_intptr_t spawnval;
3435 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003436
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003437 /* spawnvp has three arguments: (mode, path, argv), where
3438 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003439
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003440 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3441 Py_FileSystemDefaultEncoding,
3442 &path, &argv))
3443 return NULL;
3444 if (PyList_Check(argv)) {
3445 argc = PyList_Size(argv);
3446 getitem = PyList_GetItem;
3447 }
3448 else if (PyTuple_Check(argv)) {
3449 argc = PyTuple_Size(argv);
3450 getitem = PyTuple_GetItem;
3451 }
3452 else {
3453 PyErr_SetString(PyExc_TypeError,
3454 "spawnvp() arg 2 must be a tuple or list");
3455 PyMem_Free(path);
3456 return NULL;
3457 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003458
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003459 argvlist = PyMem_NEW(char *, argc+1);
3460 if (argvlist == NULL) {
3461 PyMem_Free(path);
3462 return PyErr_NoMemory();
3463 }
3464 for (i = 0; i < argc; i++) {
3465 if (!PyArg_Parse((*getitem)(argv, i), "et",
3466 Py_FileSystemDefaultEncoding,
3467 &argvlist[i])) {
3468 free_string_array(argvlist, i);
3469 PyErr_SetString(
3470 PyExc_TypeError,
3471 "spawnvp() arg 2 must contain only strings");
3472 PyMem_Free(path);
3473 return NULL;
3474 }
3475 }
3476 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003477
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003478 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003479#if defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003480 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003481#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003482 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003483#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003484 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003485
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003486 free_string_array(argvlist, argc);
3487 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003488
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003489 if (spawnval == -1)
3490 return posix_error();
3491 else
3492 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003493}
3494
3495
3496PyDoc_STRVAR(posix_spawnvpe__doc__,
3497"spawnvpe(mode, file, args, env)\n\n\
3498Execute the program 'file' in a new process, using the environment\n\
3499search path to find the file.\n\
3500\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003501 mode: mode of process creation\n\
3502 file: executable file name\n\
3503 args: tuple or list of arguments\n\
3504 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003505
3506static PyObject *
3507posix_spawnvpe(PyObject *self, PyObject *args)
3508{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003509 char *path;
3510 PyObject *argv, *env;
3511 char **argvlist;
3512 char **envlist;
3513 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3514 int mode, i, pos, argc, envc;
3515 Py_intptr_t spawnval;
3516 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3517 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003518
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003519 /* spawnvpe has four arguments: (mode, path, argv, env), where
3520 argv is a list or tuple of strings and env is a dictionary
3521 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003522
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003523 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3524 Py_FileSystemDefaultEncoding,
3525 &path, &argv, &env))
3526 return NULL;
3527 if (PyList_Check(argv)) {
3528 argc = PyList_Size(argv);
3529 getitem = PyList_GetItem;
3530 }
3531 else if (PyTuple_Check(argv)) {
3532 argc = PyTuple_Size(argv);
3533 getitem = PyTuple_GetItem;
3534 }
3535 else {
3536 PyErr_SetString(PyExc_TypeError,
3537 "spawnvpe() arg 2 must be a tuple or list");
3538 goto fail_0;
3539 }
3540 if (!PyMapping_Check(env)) {
3541 PyErr_SetString(PyExc_TypeError,
3542 "spawnvpe() arg 3 must be a mapping object");
3543 goto fail_0;
3544 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003545
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003546 argvlist = PyMem_NEW(char *, argc+1);
3547 if (argvlist == NULL) {
3548 PyErr_NoMemory();
3549 goto fail_0;
3550 }
3551 for (i = 0; i < argc; i++) {
3552 if (!PyArg_Parse((*getitem)(argv, i),
3553 "et;spawnvpe() arg 2 must contain only strings",
3554 Py_FileSystemDefaultEncoding,
3555 &argvlist[i]))
3556 {
3557 lastarg = i;
3558 goto fail_1;
3559 }
3560 }
3561 lastarg = argc;
3562 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003563
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003564 i = PyMapping_Size(env);
3565 if (i < 0)
3566 goto fail_1;
3567 envlist = PyMem_NEW(char *, i + 1);
3568 if (envlist == NULL) {
3569 PyErr_NoMemory();
3570 goto fail_1;
3571 }
3572 envc = 0;
3573 keys = PyMapping_Keys(env);
3574 vals = PyMapping_Values(env);
3575 if (!keys || !vals)
3576 goto fail_2;
3577 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3578 PyErr_SetString(PyExc_TypeError,
3579 "spawnvpe(): env.keys() or env.values() is not a list");
3580 goto fail_2;
3581 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003582
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003583 for (pos = 0; pos < i; pos++) {
3584 char *p, *k, *v;
3585 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003586
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003587 key = PyList_GetItem(keys, pos);
3588 val = PyList_GetItem(vals, pos);
3589 if (!key || !val)
3590 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003591
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003592 if (!PyArg_Parse(
3593 key,
3594 "s;spawnvpe() arg 3 contains a non-string key",
3595 &k) ||
3596 !PyArg_Parse(
3597 val,
3598 "s;spawnvpe() arg 3 contains a non-string value",
3599 &v))
3600 {
3601 goto fail_2;
3602 }
3603 len = PyString_Size(key) + PyString_Size(val) + 2;
3604 p = PyMem_NEW(char, len);
3605 if (p == NULL) {
3606 PyErr_NoMemory();
3607 goto fail_2;
3608 }
3609 PyOS_snprintf(p, len, "%s=%s", k, v);
3610 envlist[envc++] = p;
3611 }
3612 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003613
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003614 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003615#if defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003616 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003617#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003618 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003619#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003620 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003621
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003622 if (spawnval == -1)
3623 (void) posix_error();
3624 else
3625 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003626
3627 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003628 while (--envc >= 0)
3629 PyMem_DEL(envlist[envc]);
3630 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003631 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003632 free_string_array(argvlist, lastarg);
3633 Py_XDECREF(vals);
3634 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003635 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003636 PyMem_Free(path);
3637 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003638}
3639#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003640#endif /* HAVE_SPAWNV */
3641
3642
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003643#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003644PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003645"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003646Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3647\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003648Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003649
3650static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003651posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003652{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003653 pid_t pid;
3654 int result = 0;
3655 _PyImport_AcquireLock();
3656 pid = fork1();
3657 if (pid == 0) {
3658 /* child: this clobbers and resets the import lock. */
3659 PyOS_AfterFork();
3660 } else {
3661 /* parent: release the import lock. */
3662 result = _PyImport_ReleaseLock();
3663 }
3664 if (pid == -1)
3665 return posix_error();
3666 if (result < 0) {
3667 /* Don't clobber the OSError if the fork failed. */
3668 PyErr_SetString(PyExc_RuntimeError,
3669 "not holding the import lock");
3670 return NULL;
3671 }
3672 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003673}
3674#endif
3675
3676
Guido van Rossumad0ee831995-03-01 10:34:45 +00003677#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003678PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003679"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003680Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003681Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003682
Barry Warsaw53699e91996-12-10 23:23:01 +00003683static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003684posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003685{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003686 pid_t pid;
3687 int result = 0;
3688 _PyImport_AcquireLock();
3689 pid = fork();
3690 if (pid == 0) {
3691 /* child: this clobbers and resets the import lock. */
3692 PyOS_AfterFork();
3693 } else {
3694 /* parent: release the import lock. */
3695 result = _PyImport_ReleaseLock();
3696 }
3697 if (pid == -1)
3698 return posix_error();
3699 if (result < 0) {
3700 /* Don't clobber the OSError if the fork failed. */
3701 PyErr_SetString(PyExc_RuntimeError,
3702 "not holding the import lock");
3703 return NULL;
3704 }
3705 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003706}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003707#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003708
Neal Norwitzb59798b2003-03-21 01:43:31 +00003709/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003710/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3711#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003712#define DEV_PTY_FILE "/dev/ptc"
3713#define HAVE_DEV_PTMX
3714#else
3715#define DEV_PTY_FILE "/dev/ptmx"
3716#endif
3717
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003718#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003719#ifdef HAVE_PTY_H
3720#include <pty.h>
3721#else
3722#ifdef HAVE_LIBUTIL_H
3723#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003724#endif /* HAVE_LIBUTIL_H */
3725#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003726#ifdef HAVE_STROPTS_H
3727#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003728#endif
3729#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003730
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003731#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003732PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003733"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003734Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003735
3736static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003737posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003738{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003739 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003740#ifndef HAVE_OPENPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003741 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003742#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003743#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003744 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003745#ifdef sun
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003746 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003747#endif
3748#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003749
Thomas Wouters70c21a12000-07-14 14:28:33 +00003750#ifdef HAVE_OPENPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003751 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3752 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003753#elif defined(HAVE__GETPTY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003754 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3755 if (slave_name == NULL)
3756 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003757
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003758 slave_fd = open(slave_name, O_RDWR);
3759 if (slave_fd < 0)
3760 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003761#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003762 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3763 if (master_fd < 0)
3764 return posix_error();
3765 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3766 /* change permission of slave */
3767 if (grantpt(master_fd) < 0) {
3768 PyOS_setsig(SIGCHLD, sig_saved);
3769 return posix_error();
3770 }
3771 /* unlock slave */
3772 if (unlockpt(master_fd) < 0) {
3773 PyOS_setsig(SIGCHLD, sig_saved);
3774 return posix_error();
3775 }
3776 PyOS_setsig(SIGCHLD, sig_saved);
3777 slave_name = ptsname(master_fd); /* get name of slave */
3778 if (slave_name == NULL)
3779 return posix_error();
3780 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3781 if (slave_fd < 0)
3782 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003783#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003784 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3785 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003786#ifndef __hpux
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003787 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003788#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003789#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003790#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003791
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003792 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003793
Fred Drake8cef4cf2000-06-28 16:40:38 +00003794}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003795#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003796
3797#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003798PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003799"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003800Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3801Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003802To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003803
3804static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003805posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003806{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003807 int master_fd = -1, result = 0;
3808 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003809
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003810 _PyImport_AcquireLock();
3811 pid = forkpty(&master_fd, NULL, NULL, NULL);
3812 if (pid == 0) {
3813 /* child: this clobbers and resets the import lock. */
3814 PyOS_AfterFork();
3815 } else {
3816 /* parent: release the import lock. */
3817 result = _PyImport_ReleaseLock();
3818 }
3819 if (pid == -1)
3820 return posix_error();
3821 if (result < 0) {
3822 /* Don't clobber the OSError if the fork failed. */
3823 PyErr_SetString(PyExc_RuntimeError,
3824 "not holding the import lock");
3825 return NULL;
3826 }
3827 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003828}
3829#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003830
Guido van Rossumad0ee831995-03-01 10:34:45 +00003831#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003832PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003833"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003834Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003835
Barry Warsaw53699e91996-12-10 23:23:01 +00003836static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003837posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003838{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003839 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003840}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003841#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003842
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003843
Guido van Rossumad0ee831995-03-01 10:34:45 +00003844#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003845PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003846"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003847Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003848
Barry Warsaw53699e91996-12-10 23:23:01 +00003849static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003850posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003851{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003852 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003853}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003854#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003855
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003856
Guido van Rossumad0ee831995-03-01 10:34:45 +00003857#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003858PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003859"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003860Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003861
Barry Warsaw53699e91996-12-10 23:23:01 +00003862static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003863posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003864{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003865 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003866}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003867#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003868
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003870PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003871"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003872Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003873
Barry Warsaw53699e91996-12-10 23:23:01 +00003874static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003875posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003876{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003877 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003878}
3879
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003880
Fred Drakec9680921999-12-13 16:37:25 +00003881#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003882PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003883"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003884Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003885
3886static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003887posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003888{
3889 PyObject *result = NULL;
3890
Fred Drakec9680921999-12-13 16:37:25 +00003891#ifdef NGROUPS_MAX
3892#define MAX_GROUPS NGROUPS_MAX
3893#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003894 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003895#define MAX_GROUPS 64
3896#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003897 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenac08e302010-07-24 10:05:19 +00003898
3899 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
3900 * This is a helper variable to store the intermediate result when
3901 * that happens.
3902 *
3903 * To keep the code readable the OSX behaviour is unconditional,
3904 * according to the POSIX spec this should be safe on all unix-y
3905 * systems.
3906 */
3907 gid_t* alt_grouplist = grouplist;
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003908 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003909
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003910 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenac08e302010-07-24 10:05:19 +00003911 if (n < 0) {
3912 if (errno == EINVAL) {
3913 n = getgroups(0, NULL);
3914 if (n == -1) {
3915 return posix_error();
3916 }
3917 if (n == 0) {
3918 /* Avoid malloc(0) */
3919 alt_grouplist = grouplist;
3920 } else {
3921 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
3922 if (alt_grouplist == NULL) {
3923 errno = EINVAL;
3924 return posix_error();
3925 }
3926 n = getgroups(n, alt_grouplist);
3927 if (n == -1) {
3928 PyMem_Free(alt_grouplist);
3929 return posix_error();
3930 }
3931 }
3932 } else {
3933 return posix_error();
3934 }
3935 }
3936 result = PyList_New(n);
3937 if (result != NULL) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003938 int i;
3939 for (i = 0; i < n; ++i) {
Ronald Oussorenac08e302010-07-24 10:05:19 +00003940 PyObject *o = PyInt_FromLong((long)alt_grouplist[i]);
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003941 if (o == NULL) {
3942 Py_DECREF(result);
3943 result = NULL;
3944 break;
Fred Drakec9680921999-12-13 16:37:25 +00003945 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003946 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003947 }
Ronald Oussorenac08e302010-07-24 10:05:19 +00003948 }
3949
3950 if (alt_grouplist != grouplist) {
3951 PyMem_Free(alt_grouplist);
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003952 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003953
Fred Drakec9680921999-12-13 16:37:25 +00003954 return result;
3955}
3956#endif
3957
Martin v. Löwis606edc12002-06-13 21:09:11 +00003958#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003959PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003960"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003961Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003962
3963static PyObject *
3964posix_getpgid(PyObject *self, PyObject *args)
3965{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003966 pid_t pid, pgid;
3967 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
3968 return NULL;
3969 pgid = getpgid(pid);
3970 if (pgid < 0)
3971 return posix_error();
3972 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003973}
3974#endif /* HAVE_GETPGID */
3975
3976
Guido van Rossumb6775db1994-08-01 11:34:53 +00003977#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003978PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003979"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003980Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003981
Barry Warsaw53699e91996-12-10 23:23:01 +00003982static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003983posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003984{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003985#ifdef GETPGRP_HAVE_ARG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003986 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003987#else /* GETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003988 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003989#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003990}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003991#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003992
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003993
Guido van Rossumb6775db1994-08-01 11:34:53 +00003994#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003995PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003996"setpgrp()\n\n\
Senthil Kumaran4fb51b42010-06-17 16:41:47 +00003997Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003998
Barry Warsaw53699e91996-12-10 23:23:01 +00003999static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004000posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004001{
Guido van Rossum64933891994-10-20 21:56:42 +00004002#ifdef SETPGRP_HAVE_ARG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004003 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004004#else /* SETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004005 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004006#endif /* SETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004007 return posix_error();
4008 Py_INCREF(Py_None);
4009 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004010}
4011
Guido van Rossumb6775db1994-08-01 11:34:53 +00004012#endif /* HAVE_SETPGRP */
4013
Guido van Rossumad0ee831995-03-01 10:34:45 +00004014#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004016"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004018
Barry Warsaw53699e91996-12-10 23:23:01 +00004019static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004020posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004021{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004022 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004023}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004024#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004026
Fred Drake12c6e2d1999-12-14 21:25:03 +00004027#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004029"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004030Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004031
4032static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004033posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004034{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004035 PyObject *result = NULL;
4036 char *name;
4037 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004038
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004039 errno = 0;
4040 name = getlogin();
4041 if (name == NULL) {
4042 if (errno)
4043 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004044 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004045 PyErr_SetString(PyExc_OSError,
4046 "unable to determine login name");
4047 }
4048 else
4049 result = PyString_FromString(name);
4050 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004051
Fred Drake12c6e2d1999-12-14 21:25:03 +00004052 return result;
4053}
4054#endif
4055
Guido van Rossumad0ee831995-03-01 10:34:45 +00004056#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004057PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004058"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004059Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004060
Barry Warsaw53699e91996-12-10 23:23:01 +00004061static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004062posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004063{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004064 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004065}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004066#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004067
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004068
Guido van Rossumad0ee831995-03-01 10:34:45 +00004069#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004070PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004071"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004072Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004073
Barry Warsaw53699e91996-12-10 23:23:01 +00004074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004075posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004076{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004077 pid_t pid;
4078 int sig;
4079 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4080 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004081#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004082 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4083 APIRET rc;
4084 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004085 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004086
4087 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4088 APIRET rc;
4089 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004090 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004091
4092 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004093 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004094#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004095 if (kill(pid, sig) == -1)
4096 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004097#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004098 Py_INCREF(Py_None);
4099 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004100}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004101#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004102
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004103#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004104PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004105"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004106Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004107
4108static PyObject *
4109posix_killpg(PyObject *self, PyObject *args)
4110{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004111 int sig;
4112 pid_t pgid;
4113 /* XXX some man pages make the `pgid` parameter an int, others
4114 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4115 take the same type. Moreover, pid_t is always at least as wide as
4116 int (else compilation of this module fails), which is safe. */
4117 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4118 return NULL;
4119 if (killpg(pgid, sig) == -1)
4120 return posix_error();
4121 Py_INCREF(Py_None);
4122 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004123}
4124#endif
4125
Guido van Rossumc0125471996-06-28 18:55:32 +00004126#ifdef HAVE_PLOCK
4127
4128#ifdef HAVE_SYS_LOCK_H
4129#include <sys/lock.h>
4130#endif
4131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004132PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004133"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004134Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004135
Barry Warsaw53699e91996-12-10 23:23:01 +00004136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004137posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004138{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004139 int op;
4140 if (!PyArg_ParseTuple(args, "i:plock", &op))
4141 return NULL;
4142 if (plock(op) == -1)
4143 return posix_error();
4144 Py_INCREF(Py_None);
4145 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004146}
4147#endif
4148
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004149
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004150#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004151PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004152"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004153Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004154
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004155#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004156#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004157static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004158async_system(const char *command)
4159{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004160 char errormsg[256], args[1024];
4161 RESULTCODES rcodes;
4162 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004163
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004164 char *shell = getenv("COMSPEC");
4165 if (!shell)
4166 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004167
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004168 /* avoid overflowing the argument buffer */
4169 if (strlen(shell) + 3 + strlen(command) >= 1024)
4170 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004171
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004172 args[0] = '\0';
4173 strcat(args, shell);
4174 strcat(args, "/c ");
4175 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004176
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004177 /* execute asynchronously, inheriting the environment */
4178 rc = DosExecPgm(errormsg,
4179 sizeof(errormsg),
4180 EXEC_ASYNC,
4181 args,
4182 NULL,
4183 &rcodes,
4184 shell);
4185 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004186}
4187
Guido van Rossumd48f2521997-12-05 22:19:34 +00004188static FILE *
4189popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004190{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004191 int oldfd, tgtfd;
4192 HFILE pipeh[2];
4193 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004194
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004195 /* mode determines which of stdin or stdout is reconnected to
4196 * the pipe to the child
4197 */
4198 if (strchr(mode, 'r') != NULL) {
4199 tgt_fd = 1; /* stdout */
4200 } else if (strchr(mode, 'w')) {
4201 tgt_fd = 0; /* stdin */
4202 } else {
4203 *err = ERROR_INVALID_ACCESS;
4204 return NULL;
4205 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004206
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004207 /* setup the pipe */
4208 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4209 *err = rc;
4210 return NULL;
4211 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004212
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004213 /* prevent other threads accessing stdio */
4214 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004215
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004216 /* reconnect stdio and execute child */
4217 oldfd = dup(tgtfd);
4218 close(tgtfd);
4219 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4220 DosClose(pipeh[tgtfd]);
4221 rc = async_system(command);
4222 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004223
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004224 /* restore stdio */
4225 dup2(oldfd, tgtfd);
4226 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004227
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004228 /* allow other threads access to stdio */
4229 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004230
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004231 /* if execution of child was successful return file stream */
4232 if (rc == NO_ERROR)
4233 return fdopen(pipeh[1 - tgtfd], mode);
4234 else {
4235 DosClose(pipeh[1 - tgtfd]);
4236 *err = rc;
4237 return NULL;
4238 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004239}
4240
4241static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004242posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004243{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004244 char *name;
4245 char *mode = "r";
4246 int err, bufsize = -1;
4247 FILE *fp;
4248 PyObject *f;
4249 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4250 return NULL;
4251 Py_BEGIN_ALLOW_THREADS
4252 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4253 Py_END_ALLOW_THREADS
4254 if (fp == NULL)
4255 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004256
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004257 f = PyFile_FromFile(fp, name, mode, fclose);
4258 if (f != NULL)
4259 PyFile_SetBufSize(f, bufsize);
4260 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004261}
4262
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004263#elif defined(PYCC_GCC)
4264
4265/* standard posix version of popen() support */
4266static PyObject *
4267posix_popen(PyObject *self, PyObject *args)
4268{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004269 char *name;
4270 char *mode = "r";
4271 int bufsize = -1;
4272 FILE *fp;
4273 PyObject *f;
4274 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4275 return NULL;
4276 Py_BEGIN_ALLOW_THREADS
4277 fp = popen(name, mode);
4278 Py_END_ALLOW_THREADS
4279 if (fp == NULL)
4280 return posix_error();
4281 f = PyFile_FromFile(fp, name, mode, pclose);
4282 if (f != NULL)
4283 PyFile_SetBufSize(f, bufsize);
4284 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004285}
4286
4287/* fork() under OS/2 has lots'o'warts
4288 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4289 * most of this code is a ripoff of the win32 code, but using the
4290 * capabilities of EMX's C library routines
4291 */
4292
4293/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4294#define POPEN_1 1
4295#define POPEN_2 2
4296#define POPEN_3 3
4297#define POPEN_4 4
4298
4299static PyObject *_PyPopen(char *, int, int, int);
4300static int _PyPclose(FILE *file);
4301
4302/*
4303 * Internal dictionary mapping popen* file pointers to process handles,
4304 * for use when retrieving the process exit code. See _PyPclose() below
4305 * for more information on this dictionary's use.
4306 */
4307static PyObject *_PyPopenProcs = NULL;
4308
4309/* os2emx version of popen2()
4310 *
4311 * The result of this function is a pipe (file) connected to the
4312 * process's stdin, and a pipe connected to the process's stdout.
4313 */
4314
4315static PyObject *
4316os2emx_popen2(PyObject *self, PyObject *args)
4317{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004318 PyObject *f;
4319 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004320
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004321 char *cmdstring;
4322 char *mode = "t";
4323 int bufsize = -1;
4324 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4325 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004326
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004327 if (*mode == 't')
4328 tm = O_TEXT;
4329 else if (*mode != 'b') {
4330 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4331 return NULL;
4332 } else
4333 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004334
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004335 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004336
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004337 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004338}
4339
4340/*
4341 * Variation on os2emx.popen2
4342 *
4343 * The result of this function is 3 pipes - the process's stdin,
4344 * stdout and stderr
4345 */
4346
4347static PyObject *
4348os2emx_popen3(PyObject *self, PyObject *args)
4349{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004350 PyObject *f;
4351 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004352
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004353 char *cmdstring;
4354 char *mode = "t";
4355 int bufsize = -1;
4356 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4357 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004358
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004359 if (*mode == 't')
4360 tm = O_TEXT;
4361 else if (*mode != 'b') {
4362 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4363 return NULL;
4364 } else
4365 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004366
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004367 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004368
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004369 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004370}
4371
4372/*
4373 * Variation on os2emx.popen2
4374 *
Tim Peters11b23062003-04-23 02:39:17 +00004375 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004376 * and stdout+stderr combined as a single pipe.
4377 */
4378
4379static PyObject *
4380os2emx_popen4(PyObject *self, PyObject *args)
4381{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004382 PyObject *f;
4383 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004384
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004385 char *cmdstring;
4386 char *mode = "t";
4387 int bufsize = -1;
4388 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4389 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004390
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004391 if (*mode == 't')
4392 tm = O_TEXT;
4393 else if (*mode != 'b') {
4394 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4395 return NULL;
4396 } else
4397 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004398
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004399 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004400
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004401 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004402}
4403
4404/* a couple of structures for convenient handling of multiple
4405 * file handles and pipes
4406 */
4407struct file_ref
4408{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004409 int handle;
4410 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004411};
4412
4413struct pipe_ref
4414{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004415 int rd;
4416 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004417};
4418
4419/* The following code is derived from the win32 code */
4420
4421static PyObject *
4422_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4423{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004424 struct file_ref stdio[3];
4425 struct pipe_ref p_fd[3];
4426 FILE *p_s[3];
4427 int file_count, i, pipe_err;
4428 pid_t pipe_pid;
4429 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4430 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004431
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004432 /* file modes for subsequent fdopen's on pipe handles */
4433 if (mode == O_TEXT)
4434 {
4435 rd_mode = "rt";
4436 wr_mode = "wt";
4437 }
4438 else
4439 {
4440 rd_mode = "rb";
4441 wr_mode = "wb";
4442 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004443
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004444 /* prepare shell references */
4445 if ((shell = getenv("EMXSHELL")) == NULL)
4446 if ((shell = getenv("COMSPEC")) == NULL)
4447 {
4448 errno = ENOENT;
4449 return posix_error();
4450 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004451
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004452 sh_name = _getname(shell);
4453 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4454 opt = "/c";
4455 else
4456 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004457
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004458 /* save current stdio fds + their flags, and set not inheritable */
4459 i = pipe_err = 0;
4460 while (pipe_err >= 0 && i < 3)
4461 {
4462 pipe_err = stdio[i].handle = dup(i);
4463 stdio[i].flags = fcntl(i, F_GETFD, 0);
4464 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4465 i++;
4466 }
4467 if (pipe_err < 0)
4468 {
4469 /* didn't get them all saved - clean up and bail out */
4470 int saved_err = errno;
4471 while (i-- > 0)
4472 {
4473 close(stdio[i].handle);
4474 }
4475 errno = saved_err;
4476 return posix_error();
4477 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004478
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004479 /* create pipe ends */
4480 file_count = 2;
4481 if (n == POPEN_3)
4482 file_count = 3;
4483 i = pipe_err = 0;
4484 while ((pipe_err == 0) && (i < file_count))
4485 pipe_err = pipe((int *)&p_fd[i++]);
4486 if (pipe_err < 0)
4487 {
4488 /* didn't get them all made - clean up and bail out */
4489 while (i-- > 0)
4490 {
4491 close(p_fd[i].wr);
4492 close(p_fd[i].rd);
4493 }
4494 errno = EPIPE;
4495 return posix_error();
4496 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004497
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004498 /* change the actual standard IO streams over temporarily,
4499 * making the retained pipe ends non-inheritable
4500 */
4501 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004502
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004503 /* - stdin */
4504 if (dup2(p_fd[0].rd, 0) == 0)
4505 {
4506 close(p_fd[0].rd);
4507 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4508 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4509 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4510 {
4511 close(p_fd[0].wr);
4512 pipe_err = -1;
4513 }
4514 }
4515 else
4516 {
4517 pipe_err = -1;
4518 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004519
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004520 /* - stdout */
4521 if (pipe_err == 0)
4522 {
4523 if (dup2(p_fd[1].wr, 1) == 1)
4524 {
4525 close(p_fd[1].wr);
4526 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4527 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4528 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4529 {
4530 close(p_fd[1].rd);
4531 pipe_err = -1;
4532 }
4533 }
4534 else
4535 {
4536 pipe_err = -1;
4537 }
4538 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004539
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004540 /* - stderr, as required */
4541 if (pipe_err == 0)
4542 switch (n)
4543 {
4544 case POPEN_3:
4545 {
4546 if (dup2(p_fd[2].wr, 2) == 2)
4547 {
4548 close(p_fd[2].wr);
4549 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4550 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4551 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4552 {
4553 close(p_fd[2].rd);
4554 pipe_err = -1;
4555 }
4556 }
4557 else
4558 {
4559 pipe_err = -1;
4560 }
4561 break;
4562 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004563
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004564 case POPEN_4:
4565 {
4566 if (dup2(1, 2) != 2)
4567 {
4568 pipe_err = -1;
4569 }
4570 break;
4571 }
4572 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004573
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004574 /* spawn the child process */
4575 if (pipe_err == 0)
4576 {
4577 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4578 if (pipe_pid == -1)
4579 {
4580 pipe_err = -1;
4581 }
4582 else
4583 {
4584 /* save the PID into the FILE structure
4585 * NOTE: this implementation doesn't actually
4586 * take advantage of this, but do it for
4587 * completeness - AIM Apr01
4588 */
4589 for (i = 0; i < file_count; i++)
4590 p_s[i]->_pid = pipe_pid;
4591 }
4592 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004593
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004594 /* reset standard IO to normal */
4595 for (i = 0; i < 3; i++)
4596 {
4597 dup2(stdio[i].handle, i);
4598 fcntl(i, F_SETFD, stdio[i].flags);
4599 close(stdio[i].handle);
4600 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004601
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004602 /* if any remnant problems, clean up and bail out */
4603 if (pipe_err < 0)
4604 {
4605 for (i = 0; i < 3; i++)
4606 {
4607 close(p_fd[i].rd);
4608 close(p_fd[i].wr);
4609 }
4610 errno = EPIPE;
4611 return posix_error_with_filename(cmdstring);
4612 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004613
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004614 /* build tuple of file objects to return */
4615 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4616 PyFile_SetBufSize(p_f[0], bufsize);
4617 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4618 PyFile_SetBufSize(p_f[1], bufsize);
4619 if (n == POPEN_3)
4620 {
4621 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4622 PyFile_SetBufSize(p_f[0], bufsize);
4623 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4624 }
4625 else
4626 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004627
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004628 /*
4629 * Insert the files we've created into the process dictionary
4630 * all referencing the list with the process handle and the
4631 * initial number of files (see description below in _PyPclose).
4632 * Since if _PyPclose later tried to wait on a process when all
4633 * handles weren't closed, it could create a deadlock with the
4634 * child, we spend some energy here to try to ensure that we
4635 * either insert all file handles into the dictionary or none
4636 * at all. It's a little clumsy with the various popen modes
4637 * and variable number of files involved.
4638 */
4639 if (!_PyPopenProcs)
4640 {
4641 _PyPopenProcs = PyDict_New();
4642 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004643
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004644 if (_PyPopenProcs)
4645 {
4646 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4647 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004648
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004649 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4650 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004651
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004652 procObj = PyList_New(2);
4653 pidObj = PyLong_FromPid(pipe_pid);
4654 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004655
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004656 if (procObj && pidObj && intObj)
4657 {
4658 PyList_SetItem(procObj, 0, pidObj);
4659 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004660
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004661 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4662 if (fileObj[0])
4663 {
4664 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4665 fileObj[0],
4666 procObj);
4667 }
4668 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4669 if (fileObj[1])
4670 {
4671 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4672 fileObj[1],
4673 procObj);
4674 }
4675 if (file_count >= 3)
4676 {
4677 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4678 if (fileObj[2])
4679 {
4680 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4681 fileObj[2],
4682 procObj);
4683 }
4684 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004685
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004686 if (ins_rc[0] < 0 || !fileObj[0] ||
4687 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4688 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4689 {
4690 /* Something failed - remove any dictionary
4691 * entries that did make it.
4692 */
4693 if (!ins_rc[0] && fileObj[0])
4694 {
4695 PyDict_DelItem(_PyPopenProcs,
4696 fileObj[0]);
4697 }
4698 if (!ins_rc[1] && fileObj[1])
4699 {
4700 PyDict_DelItem(_PyPopenProcs,
4701 fileObj[1]);
4702 }
4703 if (!ins_rc[2] && fileObj[2])
4704 {
4705 PyDict_DelItem(_PyPopenProcs,
4706 fileObj[2]);
4707 }
4708 }
4709 }
Tim Peters11b23062003-04-23 02:39:17 +00004710
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004711 /*
4712 * Clean up our localized references for the dictionary keys
4713 * and value since PyDict_SetItem will Py_INCREF any copies
4714 * that got placed in the dictionary.
4715 */
4716 Py_XDECREF(procObj);
4717 Py_XDECREF(fileObj[0]);
4718 Py_XDECREF(fileObj[1]);
4719 Py_XDECREF(fileObj[2]);
4720 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004721
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004722 /* Child is launched. */
4723 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004724}
4725
4726/*
4727 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4728 * exit code for the child process and return as a result of the close.
4729 *
4730 * This function uses the _PyPopenProcs dictionary in order to map the
4731 * input file pointer to information about the process that was
4732 * originally created by the popen* call that created the file pointer.
4733 * The dictionary uses the file pointer as a key (with one entry
4734 * inserted for each file returned by the original popen* call) and a
4735 * single list object as the value for all files from a single call.
4736 * The list object contains the Win32 process handle at [0], and a file
4737 * count at [1], which is initialized to the total number of file
4738 * handles using that list.
4739 *
4740 * This function closes whichever handle it is passed, and decrements
4741 * the file count in the dictionary for the process handle pointed to
4742 * by this file. On the last close (when the file count reaches zero),
4743 * this function will wait for the child process and then return its
4744 * exit code as the result of the close() operation. This permits the
4745 * files to be closed in any order - it is always the close() of the
4746 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004747 *
4748 * NOTE: This function is currently called with the GIL released.
4749 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004750 */
4751
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004752static int _PyPclose(FILE *file)
4753{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004754 int result;
4755 int exit_code;
4756 pid_t pipe_pid;
4757 PyObject *procObj, *pidObj, *intObj, *fileObj;
4758 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004759#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004760 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004761#endif
4762
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004763 /* Close the file handle first, to ensure it can't block the
4764 * child from exiting if it's the last handle.
4765 */
4766 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004767
4768#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004769 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004770#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004771 if (_PyPopenProcs)
4772 {
4773 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4774 (procObj = PyDict_GetItem(_PyPopenProcs,
4775 fileObj)) != NULL &&
4776 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4777 (intObj = PyList_GetItem(procObj,1)) != NULL)
4778 {
4779 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
4780 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004781
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004782 if (file_count > 1)
4783 {
4784 /* Still other files referencing process */
4785 file_count--;
4786 PyList_SetItem(procObj,1,
4787 PyInt_FromLong((long) file_count));
4788 }
4789 else
4790 {
4791 /* Last file for this process */
4792 if (result != EOF &&
4793 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4794 {
4795 /* extract exit status */
4796 if (WIFEXITED(exit_code))
4797 {
4798 result = WEXITSTATUS(exit_code);
4799 }
4800 else
4801 {
4802 errno = EPIPE;
4803 result = -1;
4804 }
4805 }
4806 else
4807 {
4808 /* Indicate failure - this will cause the file object
4809 * to raise an I/O error and translate the last
4810 * error code from errno. We do have a problem with
4811 * last errors that overlap the normal errno table,
4812 * but that's a consistent problem with the file object.
4813 */
4814 result = -1;
4815 }
4816 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004817
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004818 /* Remove this file pointer from dictionary */
4819 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004820
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004821 if (PyDict_Size(_PyPopenProcs) == 0)
4822 {
4823 Py_DECREF(_PyPopenProcs);
4824 _PyPopenProcs = NULL;
4825 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004826
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004827 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004828
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004829 Py_XDECREF(fileObj);
4830 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004831
4832#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004833 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004834#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004835 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004836}
4837
4838#endif /* PYCC_??? */
4839
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004840#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004841
4842/*
4843 * Portable 'popen' replacement for Win32.
4844 *
4845 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4846 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004847 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004848 */
4849
4850#include <malloc.h>
4851#include <io.h>
4852#include <fcntl.h>
4853
4854/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4855#define POPEN_1 1
4856#define POPEN_2 2
4857#define POPEN_3 3
4858#define POPEN_4 4
4859
4860static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004861static int _PyPclose(FILE *file);
4862
4863/*
4864 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004865 * for use when retrieving the process exit code. See _PyPclose() below
4866 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004867 */
4868static PyObject *_PyPopenProcs = NULL;
4869
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004870
4871/* popen that works from a GUI.
4872 *
4873 * The result of this function is a pipe (file) connected to the
4874 * processes stdin or stdout, depending on the requested mode.
4875 */
4876
4877static PyObject *
4878posix_popen(PyObject *self, PyObject *args)
4879{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004880 PyObject *f;
4881 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004882
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004883 char *cmdstring;
4884 char *mode = "r";
4885 int bufsize = -1;
4886 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
4887 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004888
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004889 if (*mode == 'r')
4890 tm = _O_RDONLY;
4891 else if (*mode != 'w') {
4892 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
4893 return NULL;
4894 } else
4895 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004896
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004897 if (bufsize != -1) {
4898 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
4899 return NULL;
4900 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004901
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004902 if (*(mode+1) == 't')
4903 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4904 else if (*(mode+1) == 'b')
4905 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
4906 else
4907 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004908
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004909 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004910}
4911
4912/* Variation on win32pipe.popen
4913 *
4914 * The result of this function is a pipe (file) connected to the
4915 * process's stdin, and a pipe connected to the process's stdout.
4916 */
4917
4918static PyObject *
4919win32_popen2(PyObject *self, PyObject *args)
4920{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004921 PyObject *f;
4922 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004923
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004924 char *cmdstring;
4925 char *mode = "t";
4926 int bufsize = -1;
4927 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4928 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004929
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004930 if (*mode == 't')
4931 tm = _O_TEXT;
4932 else if (*mode != 'b') {
4933 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
4934 return NULL;
4935 } else
4936 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004937
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004938 if (bufsize != -1) {
4939 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
4940 return NULL;
4941 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004942
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004943 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004944
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004945 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004946}
4947
4948/*
4949 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004950 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004951 * The result of this function is 3 pipes - the process's stdin,
4952 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004953 */
4954
4955static PyObject *
4956win32_popen3(PyObject *self, PyObject *args)
4957{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004958 PyObject *f;
4959 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004960
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004961 char *cmdstring;
4962 char *mode = "t";
4963 int bufsize = -1;
4964 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4965 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004966
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004967 if (*mode == 't')
4968 tm = _O_TEXT;
4969 else if (*mode != 'b') {
4970 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
4971 return NULL;
4972 } else
4973 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004974
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004975 if (bufsize != -1) {
4976 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
4977 return NULL;
4978 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004979
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004980 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004981
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004982 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004983}
4984
4985/*
4986 * Variation on win32pipe.popen
4987 *
Tim Peters5aa91602002-01-30 05:46:57 +00004988 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004989 * and stdout+stderr combined as a single pipe.
4990 */
4991
4992static PyObject *
4993win32_popen4(PyObject *self, PyObject *args)
4994{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004995 PyObject *f;
4996 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004997
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004998 char *cmdstring;
4999 char *mode = "t";
5000 int bufsize = -1;
5001 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
5002 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005003
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005004 if (*mode == 't')
5005 tm = _O_TEXT;
5006 else if (*mode != 'b') {
5007 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
5008 return NULL;
5009 } else
5010 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005011
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005012 if (bufsize != -1) {
5013 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
5014 return NULL;
5015 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005016
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005017 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005018
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005019 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005020}
5021
Mark Hammond08501372001-01-31 07:30:29 +00005022static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00005023_PyPopenCreateProcess(char *cmdstring,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005024 HANDLE hStdin,
5025 HANDLE hStdout,
5026 HANDLE hStderr,
5027 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005028{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005029 PROCESS_INFORMATION piProcInfo;
5030 STARTUPINFO siStartInfo;
5031 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
5032 char *s1,*s2, *s3 = " /c ";
5033 const char *szConsoleSpawn = "w9xpopen.exe";
5034 int i;
5035 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005036
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005037 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
5038 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005039
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005040 s1 = (char *)alloca(i);
5041 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
5042 /* x < i, so x fits into an integer */
5043 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00005044
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005045 /* Explicitly check if we are using COMMAND.COM. If we are
5046 * then use the w9xpopen hack.
5047 */
5048 comshell = s1 + x;
5049 while (comshell >= s1 && *comshell != '\\')
5050 --comshell;
5051 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005052
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005053 if (GetVersion() < 0x80000000 &&
5054 _stricmp(comshell, "command.com") != 0) {
5055 /* NT/2000 and not using command.com. */
5056 x = i + strlen(s3) + strlen(cmdstring) + 1;
5057 s2 = (char *)alloca(x);
5058 ZeroMemory(s2, x);
5059 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5060 }
5061 else {
5062 /*
5063 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5064 * the workaround listed in KB: Q150956
5065 */
5066 char modulepath[_MAX_PATH];
5067 struct stat statinfo;
5068 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5069 for (x = i = 0; modulepath[i]; i++)
5070 if (modulepath[i] == SEP)
5071 x = i+1;
5072 modulepath[x] = '\0';
5073 /* Create the full-name to w9xpopen, so we can test it exists */
5074 strncat(modulepath,
5075 szConsoleSpawn,
5076 (sizeof(modulepath)/sizeof(modulepath[0]))
5077 -strlen(modulepath));
5078 if (stat(modulepath, &statinfo) != 0) {
5079 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5080 /* Eeek - file-not-found - possibly an embedding
5081 situation - see if we can locate it in sys.prefix
5082 */
5083 strncpy(modulepath,
5084 Py_GetExecPrefix(),
5085 mplen);
5086 modulepath[mplen-1] = '\0';
5087 if (modulepath[strlen(modulepath)-1] != '\\')
5088 strcat(modulepath, "\\");
5089 strncat(modulepath,
5090 szConsoleSpawn,
5091 mplen-strlen(modulepath));
5092 /* No where else to look - raise an easily identifiable
5093 error, rather than leaving Windows to report
5094 "file not found" - as the user is probably blissfully
5095 unaware this shim EXE is used, and it will confuse them.
5096 (well, it confused me for a while ;-)
5097 */
5098 if (stat(modulepath, &statinfo) != 0) {
5099 PyErr_Format(PyExc_RuntimeError,
5100 "Can not locate '%s' which is needed "
5101 "for popen to work with your shell "
5102 "or platform.",
5103 szConsoleSpawn);
5104 return FALSE;
5105 }
5106 }
5107 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5108 strlen(modulepath) +
5109 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005110
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005111 s2 = (char *)alloca(x);
5112 ZeroMemory(s2, x);
5113 /* To maintain correct argument passing semantics,
5114 we pass the command-line as it stands, and allow
5115 quoting to be applied. w9xpopen.exe will then
5116 use its argv vector, and re-quote the necessary
5117 args for the ultimate child process.
5118 */
5119 PyOS_snprintf(
5120 s2, x,
5121 "\"%s\" %s%s%s",
5122 modulepath,
5123 s1,
5124 s3,
5125 cmdstring);
5126 /* Not passing CREATE_NEW_CONSOLE has been known to
5127 cause random failures on win9x. Specifically a
5128 dialog:
5129 "Your program accessed mem currently in use at xxx"
5130 and a hopeful warning about the stability of your
5131 system.
5132 Cost is Ctrl+C won't kill children, but anyone
5133 who cares can have a go!
5134 */
5135 dwProcessFlags |= CREATE_NEW_CONSOLE;
5136 }
5137 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005138
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005139 /* Could be an else here to try cmd.exe / command.com in the path
5140 Now we'll just error out.. */
5141 else {
5142 PyErr_SetString(PyExc_RuntimeError,
5143 "Cannot locate a COMSPEC environment variable to "
5144 "use as the shell");
5145 return FALSE;
5146 }
Tim Peters5aa91602002-01-30 05:46:57 +00005147
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005148 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5149 siStartInfo.cb = sizeof(STARTUPINFO);
5150 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5151 siStartInfo.hStdInput = hStdin;
5152 siStartInfo.hStdOutput = hStdout;
5153 siStartInfo.hStdError = hStderr;
5154 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005155
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005156 if (CreateProcess(NULL,
5157 s2,
5158 NULL,
5159 NULL,
5160 TRUE,
5161 dwProcessFlags,
5162 NULL,
5163 NULL,
5164 &siStartInfo,
5165 &piProcInfo) ) {
5166 /* Close the handles now so anyone waiting is woken. */
5167 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005168
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005169 /* Return process handle */
5170 *hProcess = piProcInfo.hProcess;
5171 return TRUE;
5172 }
5173 win32_error("CreateProcess", s2);
5174 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005175}
5176
5177/* The following code is based off of KB: Q190351 */
5178
5179static PyObject *
5180_PyPopen(char *cmdstring, int mode, int n)
5181{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005182 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5183 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5184 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005185
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005186 SECURITY_ATTRIBUTES saAttr;
5187 BOOL fSuccess;
5188 int fd1, fd2, fd3;
5189 FILE *f1, *f2, *f3;
5190 long file_count;
5191 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005192
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005193 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5194 saAttr.bInheritHandle = TRUE;
5195 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005196
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005197 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5198 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005199
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005200 /* Create new output read handle and the input write handle. Set
5201 * the inheritance properties to FALSE. Otherwise, the child inherits
5202 * these handles; resulting in non-closeable handles to the pipes
5203 * being created. */
5204 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5205 GetCurrentProcess(), &hChildStdinWrDup, 0,
5206 FALSE,
5207 DUPLICATE_SAME_ACCESS);
5208 if (!fSuccess)
5209 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005210
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005211 /* Close the inheritable version of ChildStdin
5212 that we're using. */
5213 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005214
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005215 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5216 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005217
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005218 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5219 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5220 FALSE, DUPLICATE_SAME_ACCESS);
5221 if (!fSuccess)
5222 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005223
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005224 /* Close the inheritable version of ChildStdout
5225 that we're using. */
5226 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005227
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005228 if (n != POPEN_4) {
5229 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5230 return win32_error("CreatePipe", NULL);
5231 fSuccess = DuplicateHandle(GetCurrentProcess(),
5232 hChildStderrRd,
5233 GetCurrentProcess(),
5234 &hChildStderrRdDup, 0,
5235 FALSE, DUPLICATE_SAME_ACCESS);
5236 if (!fSuccess)
5237 return win32_error("DuplicateHandle", NULL);
5238 /* Close the inheritable version of ChildStdErr that we're using. */
5239 CloseHandle(hChildStderrRd);
5240 }
Tim Peters5aa91602002-01-30 05:46:57 +00005241
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005242 switch (n) {
5243 case POPEN_1:
5244 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5245 case _O_WRONLY | _O_TEXT:
5246 /* Case for writing to child Stdin in text mode. */
5247 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5248 f1 = _fdopen(fd1, "w");
5249 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5250 PyFile_SetBufSize(f, 0);
5251 /* We don't care about these pipes anymore, so close them. */
5252 CloseHandle(hChildStdoutRdDup);
5253 CloseHandle(hChildStderrRdDup);
5254 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005255
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005256 case _O_RDONLY | _O_TEXT:
5257 /* Case for reading from child Stdout in text mode. */
5258 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5259 f1 = _fdopen(fd1, "r");
5260 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5261 PyFile_SetBufSize(f, 0);
5262 /* We don't care about these pipes anymore, so close them. */
5263 CloseHandle(hChildStdinWrDup);
5264 CloseHandle(hChildStderrRdDup);
5265 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005266
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005267 case _O_RDONLY | _O_BINARY:
5268 /* Case for readinig from child Stdout in binary mode. */
5269 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5270 f1 = _fdopen(fd1, "rb");
5271 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5272 PyFile_SetBufSize(f, 0);
5273 /* We don't care about these pipes anymore, so close them. */
5274 CloseHandle(hChildStdinWrDup);
5275 CloseHandle(hChildStderrRdDup);
5276 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005277
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005278 case _O_WRONLY | _O_BINARY:
5279 /* Case for writing to child Stdin in binary mode. */
5280 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5281 f1 = _fdopen(fd1, "wb");
5282 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5283 PyFile_SetBufSize(f, 0);
5284 /* We don't care about these pipes anymore, so close them. */
5285 CloseHandle(hChildStdoutRdDup);
5286 CloseHandle(hChildStderrRdDup);
5287 break;
5288 }
5289 file_count = 1;
5290 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005291
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005292 case POPEN_2:
5293 case POPEN_4:
5294 {
5295 char *m1, *m2;
5296 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005297
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005298 if (mode & _O_TEXT) {
5299 m1 = "r";
5300 m2 = "w";
5301 } else {
5302 m1 = "rb";
5303 m2 = "wb";
5304 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005305
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005306 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5307 f1 = _fdopen(fd1, m2);
5308 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5309 f2 = _fdopen(fd2, m1);
5310 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5311 PyFile_SetBufSize(p1, 0);
5312 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5313 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005314
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005315 if (n != 4)
5316 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005317
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005318 f = PyTuple_Pack(2,p1,p2);
5319 Py_XDECREF(p1);
5320 Py_XDECREF(p2);
5321 file_count = 2;
5322 break;
5323 }
Tim Peters5aa91602002-01-30 05:46:57 +00005324
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005325 case POPEN_3:
5326 {
5327 char *m1, *m2;
5328 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005329
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005330 if (mode & _O_TEXT) {
5331 m1 = "r";
5332 m2 = "w";
5333 } else {
5334 m1 = "rb";
5335 m2 = "wb";
5336 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005337
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005338 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5339 f1 = _fdopen(fd1, m2);
5340 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5341 f2 = _fdopen(fd2, m1);
5342 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5343 f3 = _fdopen(fd3, m1);
5344 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5345 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5346 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5347 PyFile_SetBufSize(p1, 0);
5348 PyFile_SetBufSize(p2, 0);
5349 PyFile_SetBufSize(p3, 0);
5350 f = PyTuple_Pack(3,p1,p2,p3);
5351 Py_XDECREF(p1);
5352 Py_XDECREF(p2);
5353 Py_XDECREF(p3);
5354 file_count = 3;
5355 break;
5356 }
5357 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005358
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005359 if (n == POPEN_4) {
5360 if (!_PyPopenCreateProcess(cmdstring,
5361 hChildStdinRd,
5362 hChildStdoutWr,
5363 hChildStdoutWr,
5364 &hProcess))
5365 return NULL;
5366 }
5367 else {
5368 if (!_PyPopenCreateProcess(cmdstring,
5369 hChildStdinRd,
5370 hChildStdoutWr,
5371 hChildStderrWr,
5372 &hProcess))
5373 return NULL;
5374 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005375
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005376 /*
5377 * Insert the files we've created into the process dictionary
5378 * all referencing the list with the process handle and the
5379 * initial number of files (see description below in _PyPclose).
5380 * Since if _PyPclose later tried to wait on a process when all
5381 * handles weren't closed, it could create a deadlock with the
5382 * child, we spend some energy here to try to ensure that we
5383 * either insert all file handles into the dictionary or none
5384 * at all. It's a little clumsy with the various popen modes
5385 * and variable number of files involved.
5386 */
5387 if (!_PyPopenProcs) {
5388 _PyPopenProcs = PyDict_New();
5389 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005390
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005391 if (_PyPopenProcs) {
5392 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5393 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005394
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005395 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5396 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005397
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005398 procObj = PyList_New(2);
5399 hProcessObj = PyLong_FromVoidPtr(hProcess);
5400 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005401
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005402 if (procObj && hProcessObj && intObj) {
5403 PyList_SetItem(procObj,0,hProcessObj);
5404 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005405
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005406 fileObj[0] = PyLong_FromVoidPtr(f1);
5407 if (fileObj[0]) {
5408 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5409 fileObj[0],
5410 procObj);
5411 }
5412 if (file_count >= 2) {
5413 fileObj[1] = PyLong_FromVoidPtr(f2);
5414 if (fileObj[1]) {
5415 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5416 fileObj[1],
5417 procObj);
5418 }
5419 }
5420 if (file_count >= 3) {
5421 fileObj[2] = PyLong_FromVoidPtr(f3);
5422 if (fileObj[2]) {
5423 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5424 fileObj[2],
5425 procObj);
5426 }
5427 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005428
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005429 if (ins_rc[0] < 0 || !fileObj[0] ||
5430 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5431 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5432 /* Something failed - remove any dictionary
5433 * entries that did make it.
5434 */
5435 if (!ins_rc[0] && fileObj[0]) {
5436 PyDict_DelItem(_PyPopenProcs,
5437 fileObj[0]);
5438 }
5439 if (!ins_rc[1] && fileObj[1]) {
5440 PyDict_DelItem(_PyPopenProcs,
5441 fileObj[1]);
5442 }
5443 if (!ins_rc[2] && fileObj[2]) {
5444 PyDict_DelItem(_PyPopenProcs,
5445 fileObj[2]);
5446 }
5447 }
5448 }
Tim Peters5aa91602002-01-30 05:46:57 +00005449
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005450 /*
5451 * Clean up our localized references for the dictionary keys
5452 * and value since PyDict_SetItem will Py_INCREF any copies
5453 * that got placed in the dictionary.
5454 */
5455 Py_XDECREF(procObj);
5456 Py_XDECREF(fileObj[0]);
5457 Py_XDECREF(fileObj[1]);
5458 Py_XDECREF(fileObj[2]);
5459 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005460
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005461 /* Child is launched. Close the parents copy of those pipe
5462 * handles that only the child should have open. You need to
5463 * make sure that no handles to the write end of the output pipe
5464 * are maintained in this process or else the pipe will not close
5465 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005466
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005467 if (!CloseHandle(hChildStdinRd))
5468 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005469
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005470 if (!CloseHandle(hChildStdoutWr))
5471 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005472
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005473 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5474 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005475
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005476 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005477}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005478
5479/*
5480 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5481 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005482 *
5483 * This function uses the _PyPopenProcs dictionary in order to map the
5484 * input file pointer to information about the process that was
5485 * originally created by the popen* call that created the file pointer.
5486 * The dictionary uses the file pointer as a key (with one entry
5487 * inserted for each file returned by the original popen* call) and a
5488 * single list object as the value for all files from a single call.
5489 * The list object contains the Win32 process handle at [0], and a file
5490 * count at [1], which is initialized to the total number of file
5491 * handles using that list.
5492 *
5493 * This function closes whichever handle it is passed, and decrements
5494 * the file count in the dictionary for the process handle pointed to
5495 * by this file. On the last close (when the file count reaches zero),
5496 * this function will wait for the child process and then return its
5497 * exit code as the result of the close() operation. This permits the
5498 * files to be closed in any order - it is always the close() of the
5499 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005500 *
5501 * NOTE: This function is currently called with the GIL released.
5502 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005503 */
Tim Peters736aa322000-09-01 06:51:24 +00005504
Fredrik Lundh56055a42000-07-23 19:47:12 +00005505static int _PyPclose(FILE *file)
5506{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005507 int result;
5508 DWORD exit_code;
5509 HANDLE hProcess;
5510 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5511 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005512#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005513 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005514#endif
5515
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005516 /* Close the file handle first, to ensure it can't block the
5517 * child from exiting if it's the last handle.
5518 */
5519 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005520#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005521 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005522#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005523 if (_PyPopenProcs) {
5524 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5525 (procObj = PyDict_GetItem(_PyPopenProcs,
5526 fileObj)) != NULL &&
5527 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5528 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005529
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005530 hProcess = PyLong_AsVoidPtr(hProcessObj);
5531 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005532
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005533 if (file_count > 1) {
5534 /* Still other files referencing process */
5535 file_count--;
5536 PyList_SetItem(procObj,1,
5537 PyInt_FromLong(file_count));
5538 } else {
5539 /* Last file for this process */
5540 if (result != EOF &&
5541 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5542 GetExitCodeProcess(hProcess, &exit_code)) {
5543 /* Possible truncation here in 16-bit environments, but
5544 * real exit codes are just the lower byte in any event.
5545 */
5546 result = exit_code;
5547 } else {
5548 /* Indicate failure - this will cause the file object
5549 * to raise an I/O error and translate the last Win32
5550 * error code from errno. We do have a problem with
5551 * last errors that overlap the normal errno table,
5552 * but that's a consistent problem with the file object.
5553 */
5554 if (result != EOF) {
5555 /* If the error wasn't from the fclose(), then
5556 * set errno for the file object error handling.
5557 */
5558 errno = GetLastError();
5559 }
5560 result = -1;
5561 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005562
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005563 /* Free up the native handle at this point */
5564 CloseHandle(hProcess);
5565 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005566
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005567 /* Remove this file pointer from dictionary */
5568 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005569
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005570 if (PyDict_Size(_PyPopenProcs) == 0) {
5571 Py_DECREF(_PyPopenProcs);
5572 _PyPopenProcs = NULL;
5573 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005574
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005575 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005576
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005577 Py_XDECREF(fileObj);
5578 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005579
Tim Peters736aa322000-09-01 06:51:24 +00005580#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005581 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005582#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005583 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005584}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005585
5586#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005588posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005589{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005590 char *name;
5591 char *mode = "r";
5592 int bufsize = -1;
5593 FILE *fp;
5594 PyObject *f;
5595 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5596 return NULL;
5597 /* Strip mode of binary or text modifiers */
5598 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5599 mode = "r";
5600 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5601 mode = "w";
5602 Py_BEGIN_ALLOW_THREADS
5603 fp = popen(name, mode);
5604 Py_END_ALLOW_THREADS
5605 if (fp == NULL)
5606 return posix_error();
5607 f = PyFile_FromFile(fp, name, mode, pclose);
5608 if (f != NULL)
5609 PyFile_SetBufSize(f, bufsize);
5610 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005611}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005612
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005613#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005614#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005616
Guido van Rossumb6775db1994-08-01 11:34:53 +00005617#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005619"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620Set the current process's user id.");
5621
Barry Warsaw53699e91996-12-10 23:23:01 +00005622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005623posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005624{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005625 long uid_arg;
5626 uid_t uid;
5627 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5628 return NULL;
5629 uid = uid_arg;
5630 if (uid != uid_arg) {
5631 PyErr_SetString(PyExc_OverflowError, "user id too big");
5632 return NULL;
5633 }
5634 if (setuid(uid) < 0)
5635 return posix_error();
5636 Py_INCREF(Py_None);
5637 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005638}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005639#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005641
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005642#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005643PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005644"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645Set the current process's effective user id.");
5646
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005647static PyObject *
5648posix_seteuid (PyObject *self, PyObject *args)
5649{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005650 long euid_arg;
5651 uid_t euid;
5652 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5653 return NULL;
5654 euid = euid_arg;
5655 if (euid != euid_arg) {
5656 PyErr_SetString(PyExc_OverflowError, "user id too big");
5657 return NULL;
5658 }
5659 if (seteuid(euid) < 0) {
5660 return posix_error();
5661 } else {
5662 Py_INCREF(Py_None);
5663 return Py_None;
5664 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005665}
5666#endif /* HAVE_SETEUID */
5667
5668#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005669PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005670"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005671Set the current process's effective group id.");
5672
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005673static PyObject *
5674posix_setegid (PyObject *self, PyObject *args)
5675{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005676 long egid_arg;
5677 gid_t egid;
5678 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5679 return NULL;
5680 egid = egid_arg;
5681 if (egid != egid_arg) {
5682 PyErr_SetString(PyExc_OverflowError, "group id too big");
5683 return NULL;
5684 }
5685 if (setegid(egid) < 0) {
5686 return posix_error();
5687 } else {
5688 Py_INCREF(Py_None);
5689 return Py_None;
5690 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005691}
5692#endif /* HAVE_SETEGID */
5693
5694#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005695PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005696"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005697Set the current process's real and effective user ids.");
5698
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005699static PyObject *
5700posix_setreuid (PyObject *self, PyObject *args)
5701{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005702 long ruid_arg, euid_arg;
5703 uid_t ruid, euid;
5704 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5705 return NULL;
5706 if (ruid_arg == -1)
5707 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5708 else
5709 ruid = ruid_arg; /* otherwise, assign from our long */
5710 if (euid_arg == -1)
5711 euid = (uid_t)-1;
5712 else
5713 euid = euid_arg;
5714 if ((euid_arg != -1 && euid != euid_arg) ||
5715 (ruid_arg != -1 && ruid != ruid_arg)) {
5716 PyErr_SetString(PyExc_OverflowError, "user id too big");
5717 return NULL;
5718 }
5719 if (setreuid(ruid, euid) < 0) {
5720 return posix_error();
5721 } else {
5722 Py_INCREF(Py_None);
5723 return Py_None;
5724 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005725}
5726#endif /* HAVE_SETREUID */
5727
5728#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005729PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005730"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005731Set the current process's real and effective group ids.");
5732
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005733static PyObject *
5734posix_setregid (PyObject *self, PyObject *args)
5735{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005736 long rgid_arg, egid_arg;
5737 gid_t rgid, egid;
5738 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5739 return NULL;
5740 if (rgid_arg == -1)
5741 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5742 else
5743 rgid = rgid_arg; /* otherwise, assign from our long */
5744 if (egid_arg == -1)
5745 egid = (gid_t)-1;
5746 else
5747 egid = egid_arg;
5748 if ((egid_arg != -1 && egid != egid_arg) ||
5749 (rgid_arg != -1 && rgid != rgid_arg)) {
5750 PyErr_SetString(PyExc_OverflowError, "group id too big");
5751 return NULL;
5752 }
5753 if (setregid(rgid, egid) < 0) {
5754 return posix_error();
5755 } else {
5756 Py_INCREF(Py_None);
5757 return Py_None;
5758 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005759}
5760#endif /* HAVE_SETREGID */
5761
Guido van Rossumb6775db1994-08-01 11:34:53 +00005762#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005763PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005764"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005765Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005766
Barry Warsaw53699e91996-12-10 23:23:01 +00005767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005768posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005769{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005770 long gid_arg;
5771 gid_t gid;
5772 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5773 return NULL;
5774 gid = gid_arg;
5775 if (gid != gid_arg) {
5776 PyErr_SetString(PyExc_OverflowError, "group id too big");
5777 return NULL;
5778 }
5779 if (setgid(gid) < 0)
5780 return posix_error();
5781 Py_INCREF(Py_None);
5782 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005783}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005784#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005785
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005786#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005787PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005788"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005789Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005790
5791static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005792posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005793{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005794 int i, len;
5795 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005796
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005797 if (!PySequence_Check(groups)) {
5798 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5799 return NULL;
5800 }
5801 len = PySequence_Size(groups);
5802 if (len > MAX_GROUPS) {
5803 PyErr_SetString(PyExc_ValueError, "too many groups");
5804 return NULL;
5805 }
5806 for(i = 0; i < len; i++) {
5807 PyObject *elem;
5808 elem = PySequence_GetItem(groups, i);
5809 if (!elem)
5810 return NULL;
5811 if (!PyInt_Check(elem)) {
5812 if (!PyLong_Check(elem)) {
5813 PyErr_SetString(PyExc_TypeError,
5814 "groups must be integers");
5815 Py_DECREF(elem);
5816 return NULL;
5817 } else {
5818 unsigned long x = PyLong_AsUnsignedLong(elem);
5819 if (PyErr_Occurred()) {
5820 PyErr_SetString(PyExc_TypeError,
5821 "group id too big");
5822 Py_DECREF(elem);
5823 return NULL;
5824 }
5825 grouplist[i] = x;
5826 /* read back to see if it fits in gid_t */
5827 if (grouplist[i] != x) {
5828 PyErr_SetString(PyExc_TypeError,
5829 "group id too big");
5830 Py_DECREF(elem);
5831 return NULL;
5832 }
5833 }
5834 } else {
5835 long x = PyInt_AsLong(elem);
5836 grouplist[i] = x;
5837 if (grouplist[i] != x) {
5838 PyErr_SetString(PyExc_TypeError,
5839 "group id too big");
5840 Py_DECREF(elem);
5841 return NULL;
5842 }
5843 }
5844 Py_DECREF(elem);
5845 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005846
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005847 if (setgroups(len, grouplist) < 0)
5848 return posix_error();
5849 Py_INCREF(Py_None);
5850 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005851}
5852#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005853
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005854#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005855static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005856wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005857{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005858 PyObject *result;
5859 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00005860
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005861 if (pid == -1)
5862 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00005863
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005864 if (struct_rusage == NULL) {
5865 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5866 if (m == NULL)
5867 return NULL;
5868 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5869 Py_DECREF(m);
5870 if (struct_rusage == NULL)
5871 return NULL;
5872 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005873
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005874 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5875 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5876 if (!result)
5877 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005878
5879#ifndef doubletime
5880#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5881#endif
5882
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005883 PyStructSequence_SET_ITEM(result, 0,
5884 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5885 PyStructSequence_SET_ITEM(result, 1,
5886 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00005887#define SET_INT(result, index, value)\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005888 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5889 SET_INT(result, 2, ru->ru_maxrss);
5890 SET_INT(result, 3, ru->ru_ixrss);
5891 SET_INT(result, 4, ru->ru_idrss);
5892 SET_INT(result, 5, ru->ru_isrss);
5893 SET_INT(result, 6, ru->ru_minflt);
5894 SET_INT(result, 7, ru->ru_majflt);
5895 SET_INT(result, 8, ru->ru_nswap);
5896 SET_INT(result, 9, ru->ru_inblock);
5897 SET_INT(result, 10, ru->ru_oublock);
5898 SET_INT(result, 11, ru->ru_msgsnd);
5899 SET_INT(result, 12, ru->ru_msgrcv);
5900 SET_INT(result, 13, ru->ru_nsignals);
5901 SET_INT(result, 14, ru->ru_nvcsw);
5902 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00005903#undef SET_INT
5904
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005905 if (PyErr_Occurred()) {
5906 Py_DECREF(result);
5907 return NULL;
5908 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005909
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005910 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005911}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005912#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005913
5914#ifdef HAVE_WAIT3
5915PyDoc_STRVAR(posix_wait3__doc__,
5916"wait3(options) -> (pid, status, rusage)\n\n\
5917Wait for completion of a child process.");
5918
5919static PyObject *
5920posix_wait3(PyObject *self, PyObject *args)
5921{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005922 pid_t pid;
5923 int options;
5924 struct rusage ru;
5925 WAIT_TYPE status;
5926 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005927
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005928 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5929 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005930
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005931 Py_BEGIN_ALLOW_THREADS
5932 pid = wait3(&status, options, &ru);
5933 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005934
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005935 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005936}
5937#endif /* HAVE_WAIT3 */
5938
5939#ifdef HAVE_WAIT4
5940PyDoc_STRVAR(posix_wait4__doc__,
5941"wait4(pid, options) -> (pid, status, rusage)\n\n\
5942Wait for completion of a given child process.");
5943
5944static PyObject *
5945posix_wait4(PyObject *self, PyObject *args)
5946{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005947 pid_t pid;
5948 int options;
5949 struct rusage ru;
5950 WAIT_TYPE status;
5951 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005952
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005953 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
5954 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005955
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005956 Py_BEGIN_ALLOW_THREADS
5957 pid = wait4(pid, &status, options, &ru);
5958 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005959
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005960 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005961}
5962#endif /* HAVE_WAIT4 */
5963
Guido van Rossumb6775db1994-08-01 11:34:53 +00005964#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005965PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005966"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005967Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005968
Barry Warsaw53699e91996-12-10 23:23:01 +00005969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005970posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005971{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005972 pid_t pid;
5973 int options;
5974 WAIT_TYPE status;
5975 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005976
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005977 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
5978 return NULL;
5979 Py_BEGIN_ALLOW_THREADS
5980 pid = waitpid(pid, &status, options);
5981 Py_END_ALLOW_THREADS
5982 if (pid == -1)
5983 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005984
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005985 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005986}
5987
Tim Petersab034fa2002-02-01 11:27:43 +00005988#elif defined(HAVE_CWAIT)
5989
5990/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005991PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005992"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005993"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005994
5995static PyObject *
5996posix_waitpid(PyObject *self, PyObject *args)
5997{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005998 Py_intptr_t pid;
5999 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006000
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006001 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6002 return NULL;
6003 Py_BEGIN_ALLOW_THREADS
6004 pid = _cwait(&status, pid, options);
6005 Py_END_ALLOW_THREADS
6006 if (pid == -1)
6007 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006008
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006009 /* shift the status left a byte so this is more like the POSIX waitpid */
6010 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006011}
6012#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006013
Guido van Rossumad0ee831995-03-01 10:34:45 +00006014#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006016"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006017Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006018
Barry Warsaw53699e91996-12-10 23:23:01 +00006019static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006020posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006021{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006022 pid_t pid;
6023 WAIT_TYPE status;
6024 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006025
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006026 Py_BEGIN_ALLOW_THREADS
6027 pid = wait(&status);
6028 Py_END_ALLOW_THREADS
6029 if (pid == -1)
6030 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006031
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006032 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006033}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006034#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006035
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006037PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006038"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006039Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006040
Barry Warsaw53699e91996-12-10 23:23:01 +00006041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006042posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006043{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006044#ifdef HAVE_LSTAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006045 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006046#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006047#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006048 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006049#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006050 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006051#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006052#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006053}
6054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006055
Guido van Rossumb6775db1994-08-01 11:34:53 +00006056#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006057PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006058"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006059Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006060
Barry Warsaw53699e91996-12-10 23:23:01 +00006061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006062posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006063{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006064 PyObject* v;
6065 char buf[MAXPATHLEN];
6066 char *path;
6067 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006068#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006069 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006070#endif
6071
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006072 if (!PyArg_ParseTuple(args, "et:readlink",
6073 Py_FileSystemDefaultEncoding, &path))
6074 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006075#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006076 v = PySequence_GetItem(args, 0);
6077 if (v == NULL) {
6078 PyMem_Free(path);
6079 return NULL;
6080 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006081
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006082 if (PyUnicode_Check(v)) {
6083 arg_is_unicode = 1;
6084 }
6085 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006086#endif
6087
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006088 Py_BEGIN_ALLOW_THREADS
6089 n = readlink(path, buf, (int) sizeof buf);
6090 Py_END_ALLOW_THREADS
6091 if (n < 0)
6092 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006093
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006094 PyMem_Free(path);
6095 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006096#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006097 if (arg_is_unicode) {
6098 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006099
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006100 w = PyUnicode_FromEncodedObject(v,
6101 Py_FileSystemDefaultEncoding,
6102 "strict");
6103 if (w != NULL) {
6104 Py_DECREF(v);
6105 v = w;
6106 }
6107 else {
6108 /* fall back to the original byte string, as
6109 discussed in patch #683592 */
6110 PyErr_Clear();
6111 }
6112 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006113#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006114 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006115}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006116#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006117
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006118
Guido van Rossumb6775db1994-08-01 11:34:53 +00006119#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006120PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006121"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006122Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006123
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006124static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006125posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006126{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006127 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006128}
6129#endif /* HAVE_SYMLINK */
6130
6131
6132#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006133#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6134static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006135system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006136{
6137 ULONG value = 0;
6138
6139 Py_BEGIN_ALLOW_THREADS
6140 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6141 Py_END_ALLOW_THREADS
6142
6143 return value;
6144}
6145
6146static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006147posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006148{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006149 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006150 return Py_BuildValue("ddddd",
6151 (double)0 /* t.tms_utime / HZ */,
6152 (double)0 /* t.tms_stime / HZ */,
6153 (double)0 /* t.tms_cutime / HZ */,
6154 (double)0 /* t.tms_cstime / HZ */,
6155 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006156}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006157#else /* not OS2 */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00006158#define NEED_TICKS_PER_SECOND
6159static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006160static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006161posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006162{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006163 struct tms t;
6164 clock_t c;
6165 errno = 0;
6166 c = times(&t);
6167 if (c == (clock_t) -1)
6168 return posix_error();
6169 return Py_BuildValue("ddddd",
6170 (double)t.tms_utime / ticks_per_second,
6171 (double)t.tms_stime / ticks_per_second,
6172 (double)t.tms_cutime / ticks_per_second,
6173 (double)t.tms_cstime / ticks_per_second,
6174 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006175}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006176#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006177#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006178
6179
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006180#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006181#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006182static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006183posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006184{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006185 FILETIME create, exit, kernel, user;
6186 HANDLE hProc;
6187 hProc = GetCurrentProcess();
6188 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6189 /* The fields of a FILETIME structure are the hi and lo part
6190 of a 64-bit value expressed in 100 nanosecond units.
6191 1e7 is one second in such units; 1e-7 the inverse.
6192 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6193 */
6194 return Py_BuildValue(
6195 "ddddd",
6196 (double)(user.dwHighDateTime*429.4967296 +
6197 user.dwLowDateTime*1e-7),
6198 (double)(kernel.dwHighDateTime*429.4967296 +
6199 kernel.dwLowDateTime*1e-7),
6200 (double)0,
6201 (double)0,
6202 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006203}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006204#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006205
6206#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006207PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006208"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006209Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006210#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006211
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006212
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006213#ifdef HAVE_GETSID
6214PyDoc_STRVAR(posix_getsid__doc__,
6215"getsid(pid) -> sid\n\n\
6216Call the system call getsid().");
6217
6218static PyObject *
6219posix_getsid(PyObject *self, PyObject *args)
6220{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006221 pid_t pid;
6222 int sid;
6223 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6224 return NULL;
6225 sid = getsid(pid);
6226 if (sid < 0)
6227 return posix_error();
6228 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006229}
6230#endif /* HAVE_GETSID */
6231
6232
Guido van Rossumb6775db1994-08-01 11:34:53 +00006233#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006235"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Barry Warsaw53699e91996-12-10 23:23:01 +00006238static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006239posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006240{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006241 if (setsid() < 0)
6242 return posix_error();
6243 Py_INCREF(Py_None);
6244 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006245}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006246#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006247
Guido van Rossumb6775db1994-08-01 11:34:53 +00006248#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006249PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006250"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006251Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006252
Barry Warsaw53699e91996-12-10 23:23:01 +00006253static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006254posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006255{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006256 pid_t pid;
6257 int pgrp;
6258 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6259 return NULL;
6260 if (setpgid(pid, pgrp) < 0)
6261 return posix_error();
6262 Py_INCREF(Py_None);
6263 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006264}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006265#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006267
Guido van Rossumb6775db1994-08-01 11:34:53 +00006268#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006269PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006270"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006271Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006272
Barry Warsaw53699e91996-12-10 23:23:01 +00006273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006274posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006275{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006276 int fd;
6277 pid_t pgid;
6278 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6279 return NULL;
6280 pgid = tcgetpgrp(fd);
6281 if (pgid < 0)
6282 return posix_error();
6283 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006284}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006285#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006287
Guido van Rossumb6775db1994-08-01 11:34:53 +00006288#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006289PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006290"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006291Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006292
Barry Warsaw53699e91996-12-10 23:23:01 +00006293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006294posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006295{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006296 int fd;
6297 pid_t pgid;
6298 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6299 return NULL;
6300 if (tcsetpgrp(fd, pgid) < 0)
6301 return posix_error();
6302 Py_INCREF(Py_None);
6303 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006304}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006305#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006306
Guido van Rossum687dd131993-05-17 08:34:16 +00006307/* Functions acting on file descriptors */
6308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006309PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006310"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006311Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006312
Barry Warsaw53699e91996-12-10 23:23:01 +00006313static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006314posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006315{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006316 char *file = NULL;
6317 int flag;
6318 int mode = 0777;
6319 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006320
6321#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006322 if (unicode_file_names()) {
6323 PyUnicodeObject *po;
6324 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6325 Py_BEGIN_ALLOW_THREADS
6326 /* PyUnicode_AS_UNICODE OK without thread
6327 lock as it is a simple dereference. */
6328 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6329 Py_END_ALLOW_THREADS
6330 if (fd < 0)
6331 return posix_error();
6332 return PyInt_FromLong((long)fd);
6333 }
6334 /* Drop the argument parsing error as narrow strings
6335 are also valid. */
6336 PyErr_Clear();
6337 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006338#endif
6339
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006340 if (!PyArg_ParseTuple(args, "eti|i",
6341 Py_FileSystemDefaultEncoding, &file,
6342 &flag, &mode))
6343 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006344
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006345 Py_BEGIN_ALLOW_THREADS
6346 fd = open(file, flag, mode);
6347 Py_END_ALLOW_THREADS
6348 if (fd < 0)
6349 return posix_error_with_allocated_filename(file);
6350 PyMem_Free(file);
6351 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006352}
6353
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006355PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006356"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006357Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006358
Barry Warsaw53699e91996-12-10 23:23:01 +00006359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006360posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006361{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006362 int fd, res;
6363 if (!PyArg_ParseTuple(args, "i:close", &fd))
6364 return NULL;
6365 Py_BEGIN_ALLOW_THREADS
6366 res = close(fd);
6367 Py_END_ALLOW_THREADS
6368 if (res < 0)
6369 return posix_error();
6370 Py_INCREF(Py_None);
6371 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006372}
6373
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006374
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006375PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006376"closerange(fd_low, fd_high)\n\n\
6377Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6378
6379static PyObject *
6380posix_closerange(PyObject *self, PyObject *args)
6381{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006382 int fd_from, fd_to, i;
6383 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6384 return NULL;
6385 Py_BEGIN_ALLOW_THREADS
6386 for (i = fd_from; i < fd_to; i++)
6387 close(i);
6388 Py_END_ALLOW_THREADS
6389 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006390}
6391
6392
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006393PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006394"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006395Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006396
Barry Warsaw53699e91996-12-10 23:23:01 +00006397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006398posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006399{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006400 int fd;
6401 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6402 return NULL;
6403 Py_BEGIN_ALLOW_THREADS
6404 fd = dup(fd);
6405 Py_END_ALLOW_THREADS
6406 if (fd < 0)
6407 return posix_error();
6408 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006409}
6410
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006412PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006413"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006414Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006415
Barry Warsaw53699e91996-12-10 23:23:01 +00006416static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006417posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006418{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006419 int fd, fd2, res;
6420 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6421 return NULL;
6422 Py_BEGIN_ALLOW_THREADS
6423 res = dup2(fd, fd2);
6424 Py_END_ALLOW_THREADS
6425 if (res < 0)
6426 return posix_error();
6427 Py_INCREF(Py_None);
6428 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006429}
6430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006432PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006433"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006435
Barry Warsaw53699e91996-12-10 23:23:01 +00006436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006437posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006438{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006439 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006440#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006441 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006442#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006443 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006444#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006445 PyObject *posobj;
6446 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6447 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006448#ifdef SEEK_SET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006449 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6450 switch (how) {
6451 case 0: how = SEEK_SET; break;
6452 case 1: how = SEEK_CUR; break;
6453 case 2: how = SEEK_END; break;
6454 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006455#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006456
6457#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006458 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006459#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006460 pos = PyLong_Check(posobj) ?
6461 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006462#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006463 if (PyErr_Occurred())
6464 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006465
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006466 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006467#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006468 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006469#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006470 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006471#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006472 Py_END_ALLOW_THREADS
6473 if (res < 0)
6474 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006475
6476#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006477 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006478#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006479 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006480#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006481}
6482
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006483
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006484PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006485"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006486Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006487
Barry Warsaw53699e91996-12-10 23:23:01 +00006488static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006489posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006490{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006491 int fd, size, n;
6492 PyObject *buffer;
6493 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6494 return NULL;
6495 if (size < 0) {
6496 errno = EINVAL;
6497 return posix_error();
6498 }
6499 buffer = PyString_FromStringAndSize((char *)NULL, size);
6500 if (buffer == NULL)
6501 return NULL;
6502 Py_BEGIN_ALLOW_THREADS
6503 n = read(fd, PyString_AsString(buffer), size);
6504 Py_END_ALLOW_THREADS
6505 if (n < 0) {
6506 Py_DECREF(buffer);
6507 return posix_error();
6508 }
6509 if (n != size)
6510 _PyString_Resize(&buffer, n);
6511 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006512}
6513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006515PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006516"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006517Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006518
Barry Warsaw53699e91996-12-10 23:23:01 +00006519static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006520posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006521{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006522 Py_buffer pbuf;
6523 int fd;
6524 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006525
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006526 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6527 return NULL;
6528 Py_BEGIN_ALLOW_THREADS
6529 size = write(fd, pbuf.buf, (size_t)pbuf.len);
6530 Py_END_ALLOW_THREADS
6531 PyBuffer_Release(&pbuf);
6532 if (size < 0)
6533 return posix_error();
6534 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006535}
6536
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006538PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006539"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006540Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006541
Barry Warsaw53699e91996-12-10 23:23:01 +00006542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006543posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006544{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006545 int fd;
6546 STRUCT_STAT st;
6547 int res;
6548 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6549 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006550#ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006551 /* on OpenVMS we must ensure that all bytes are written to the file */
6552 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006553#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006554 Py_BEGIN_ALLOW_THREADS
6555 res = FSTAT(fd, &st);
6556 Py_END_ALLOW_THREADS
6557 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006558#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006559 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006560#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006561 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006562#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006563 }
Tim Peters5aa91602002-01-30 05:46:57 +00006564
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006565 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006566}
6567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006569PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006570"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006571Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006572
Barry Warsaw53699e91996-12-10 23:23:01 +00006573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006574posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006575{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006576 int fd;
6577 char *orgmode = "r";
6578 int bufsize = -1;
6579 FILE *fp;
6580 PyObject *f;
6581 char *mode;
6582 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6583 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006584
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006585 /* Sanitize mode. See fileobject.c */
6586 mode = PyMem_MALLOC(strlen(orgmode)+3);
6587 if (!mode) {
6588 PyErr_NoMemory();
6589 return NULL;
6590 }
6591 strcpy(mode, orgmode);
6592 if (_PyFile_SanitizeMode(mode)) {
6593 PyMem_FREE(mode);
6594 return NULL;
6595 }
6596 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006597#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006598 if (mode[0] == 'a') {
6599 /* try to make sure the O_APPEND flag is set */
6600 int flags;
6601 flags = fcntl(fd, F_GETFL);
6602 if (flags != -1)
6603 fcntl(fd, F_SETFL, flags | O_APPEND);
6604 fp = fdopen(fd, mode);
6605 if (fp == NULL && flags != -1)
6606 /* restore old mode if fdopen failed */
6607 fcntl(fd, F_SETFL, flags);
6608 } else {
6609 fp = fdopen(fd, mode);
6610 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006611#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006612 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006613#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006614 Py_END_ALLOW_THREADS
6615 PyMem_FREE(mode);
6616 if (fp == NULL)
6617 return posix_error();
6618 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6619 if (f != NULL)
6620 PyFile_SetBufSize(f, bufsize);
6621 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006622}
6623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006624PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006625"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006626Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006627connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006628
6629static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006630posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006631{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006632 int fd;
6633 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6634 return NULL;
6635 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006636}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006637
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006638#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006639PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006640"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006641Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006642
Barry Warsaw53699e91996-12-10 23:23:01 +00006643static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006644posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006645{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006646#if defined(PYOS_OS2)
6647 HFILE read, write;
6648 APIRET rc;
6649
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006650 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006651 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006652 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006653 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006654 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006655
6656 return Py_BuildValue("(ii)", read, write);
6657#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006658#if !defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006659 int fds[2];
6660 int res;
6661 Py_BEGIN_ALLOW_THREADS
6662 res = pipe(fds);
6663 Py_END_ALLOW_THREADS
6664 if (res != 0)
6665 return posix_error();
6666 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006667#else /* MS_WINDOWS */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006668 HANDLE read, write;
6669 int read_fd, write_fd;
6670 BOOL ok;
6671 Py_BEGIN_ALLOW_THREADS
6672 ok = CreatePipe(&read, &write, NULL, 0);
6673 Py_END_ALLOW_THREADS
6674 if (!ok)
6675 return win32_error("CreatePipe", NULL);
6676 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6677 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6678 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006679#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006680#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006681}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006682#endif /* HAVE_PIPE */
6683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006684
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006685#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006686PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006687"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006688Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006689
Barry Warsaw53699e91996-12-10 23:23:01 +00006690static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006691posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006692{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006693 char *filename;
6694 int mode = 0666;
6695 int res;
6696 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6697 return NULL;
6698 Py_BEGIN_ALLOW_THREADS
6699 res = mkfifo(filename, mode);
6700 Py_END_ALLOW_THREADS
6701 if (res < 0)
6702 return posix_error();
6703 Py_INCREF(Py_None);
6704 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006705}
6706#endif
6707
6708
Neal Norwitz11690112002-07-30 01:08:28 +00006709#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006710PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006711"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006712Create a filesystem node (file, device special file or named pipe)\n\
6713named filename. mode specifies both the permissions to use and the\n\
6714type of node to be created, being combined (bitwise OR) with one of\n\
6715S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006716device defines the newly created device special file (probably using\n\
6717os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006718
6719
6720static PyObject *
6721posix_mknod(PyObject *self, PyObject *args)
6722{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006723 char *filename;
6724 int mode = 0600;
6725 int device = 0;
6726 int res;
6727 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6728 return NULL;
6729 Py_BEGIN_ALLOW_THREADS
6730 res = mknod(filename, mode, device);
6731 Py_END_ALLOW_THREADS
6732 if (res < 0)
6733 return posix_error();
6734 Py_INCREF(Py_None);
6735 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006736}
6737#endif
6738
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006739#ifdef HAVE_DEVICE_MACROS
6740PyDoc_STRVAR(posix_major__doc__,
6741"major(device) -> major number\n\
6742Extracts a device major number from a raw device number.");
6743
6744static PyObject *
6745posix_major(PyObject *self, PyObject *args)
6746{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006747 int device;
6748 if (!PyArg_ParseTuple(args, "i:major", &device))
6749 return NULL;
6750 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006751}
6752
6753PyDoc_STRVAR(posix_minor__doc__,
6754"minor(device) -> minor number\n\
6755Extracts a device minor number from a raw device number.");
6756
6757static PyObject *
6758posix_minor(PyObject *self, PyObject *args)
6759{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006760 int device;
6761 if (!PyArg_ParseTuple(args, "i:minor", &device))
6762 return NULL;
6763 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006764}
6765
6766PyDoc_STRVAR(posix_makedev__doc__,
6767"makedev(major, minor) -> device number\n\
6768Composes a raw device number from the major and minor device numbers.");
6769
6770static PyObject *
6771posix_makedev(PyObject *self, PyObject *args)
6772{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006773 int major, minor;
6774 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6775 return NULL;
6776 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006777}
6778#endif /* device macros */
6779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006780
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006781#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006782PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006783"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006784Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006785
Barry Warsaw53699e91996-12-10 23:23:01 +00006786static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006787posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006788{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006789 int fd;
6790 off_t length;
6791 int res;
6792 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006793
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006794 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6795 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006796
6797#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006798 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006799#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006800 length = PyLong_Check(lenobj) ?
6801 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006802#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006803 if (PyErr_Occurred())
6804 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006805
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006806 Py_BEGIN_ALLOW_THREADS
6807 res = ftruncate(fd, length);
6808 Py_END_ALLOW_THREADS
6809 if (res < 0) {
6810 PyErr_SetFromErrno(PyExc_IOError);
6811 return NULL;
6812 }
6813 Py_INCREF(Py_None);
6814 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006815}
6816#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006817
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006818#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006819PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006820"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006821Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006822
Fred Drake762e2061999-08-26 17:23:54 +00006823/* Save putenv() parameters as values here, so we can collect them when they
6824 * get re-set with another call for the same key. */
6825static PyObject *posix_putenv_garbage;
6826
Tim Peters5aa91602002-01-30 05:46:57 +00006827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006828posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006829{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006830 char *s1, *s2;
6831 char *newenv;
6832 PyObject *newstr;
6833 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006834
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006835 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6836 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006837
6838#if defined(PYOS_OS2)
6839 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6840 APIRET rc;
6841
Guido van Rossumd48f2521997-12-05 22:19:34 +00006842 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6843 if (rc != NO_ERROR)
6844 return os2_error(rc);
6845
6846 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6847 APIRET rc;
6848
Guido van Rossumd48f2521997-12-05 22:19:34 +00006849 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6850 if (rc != NO_ERROR)
6851 return os2_error(rc);
6852 } else {
6853#endif
6854
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006855 /* XXX This can leak memory -- not easy to fix :-( */
6856 len = strlen(s1) + strlen(s2) + 2;
6857 /* len includes space for a trailing \0; the size arg to
6858 PyString_FromStringAndSize does not count that */
6859 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6860 if (newstr == NULL)
6861 return PyErr_NoMemory();
6862 newenv = PyString_AS_STRING(newstr);
6863 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6864 if (putenv(newenv)) {
6865 Py_DECREF(newstr);
6866 posix_error();
6867 return NULL;
6868 }
6869 /* Install the first arg and newstr in posix_putenv_garbage;
6870 * this will cause previous value to be collected. This has to
6871 * happen after the real putenv() call because the old value
6872 * was still accessible until then. */
6873 if (PyDict_SetItem(posix_putenv_garbage,
6874 PyTuple_GET_ITEM(args, 0), newstr)) {
6875 /* really not much we can do; just leak */
6876 PyErr_Clear();
6877 }
6878 else {
6879 Py_DECREF(newstr);
6880 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006881
6882#if defined(PYOS_OS2)
6883 }
6884#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006885 Py_INCREF(Py_None);
6886 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006887}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006888#endif /* putenv */
6889
Guido van Rossumc524d952001-10-19 01:31:59 +00006890#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006891PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006892"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006893Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006894
6895static PyObject *
6896posix_unsetenv(PyObject *self, PyObject *args)
6897{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006898 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006899
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006900 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6901 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00006902
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006903 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006904
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006905 /* Remove the key from posix_putenv_garbage;
6906 * this will cause it to be collected. This has to
6907 * happen after the real unsetenv() call because the
6908 * old value was still accessible until then.
6909 */
6910 if (PyDict_DelItem(posix_putenv_garbage,
6911 PyTuple_GET_ITEM(args, 0))) {
6912 /* really not much we can do; just leak */
6913 PyErr_Clear();
6914 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006915
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006916 Py_INCREF(Py_None);
6917 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00006918}
6919#endif /* unsetenv */
6920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006921PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006922"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006923Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006924
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006926posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006927{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006928 int code;
6929 char *message;
6930 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6931 return NULL;
6932 message = strerror(code);
6933 if (message == NULL) {
6934 PyErr_SetString(PyExc_ValueError,
6935 "strerror() argument out of range");
6936 return NULL;
6937 }
6938 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006939}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006940
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006941
Guido van Rossumc9641791998-08-04 15:26:23 +00006942#ifdef HAVE_SYS_WAIT_H
6943
Fred Drake106c1a02002-04-23 15:58:02 +00006944#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006945PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006946"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006947Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006948
6949static PyObject *
6950posix_WCOREDUMP(PyObject *self, PyObject *args)
6951{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006952 WAIT_TYPE status;
6953 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006954
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006955 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6956 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006957
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006958 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006959}
6960#endif /* WCOREDUMP */
6961
6962#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006963PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006964"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006965Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006966job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006967
6968static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006969posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006970{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006971 WAIT_TYPE status;
6972 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006973
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006974 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6975 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006976
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006977 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006978}
6979#endif /* WIFCONTINUED */
6980
Guido van Rossumc9641791998-08-04 15:26:23 +00006981#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006982PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006983"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006984Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006985
6986static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006987posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006988{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006989 WAIT_TYPE status;
6990 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006991
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006992 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6993 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006994
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006995 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006996}
6997#endif /* WIFSTOPPED */
6998
6999#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007000PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007001"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007002Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007003
7004static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007005posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007006{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007007 WAIT_TYPE status;
7008 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007009
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007010 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7011 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007012
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007013 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007014}
7015#endif /* WIFSIGNALED */
7016
7017#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007018PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007019"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007020Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007021system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007022
7023static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007024posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007025{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007026 WAIT_TYPE status;
7027 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007028
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007029 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7030 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007031
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007032 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007033}
7034#endif /* WIFEXITED */
7035
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007036#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007037PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007038"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007039Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007040
7041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007042posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007043{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007044 WAIT_TYPE status;
7045 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007046
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007047 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7048 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007049
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007050 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007051}
7052#endif /* WEXITSTATUS */
7053
7054#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007055PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007056"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007057Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007058value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007059
7060static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007061posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007062{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007063 WAIT_TYPE status;
7064 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007065
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007066 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7067 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007068
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007069 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007070}
7071#endif /* WTERMSIG */
7072
7073#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007074PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007075"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007076Return the signal that stopped the process that provided\n\
7077the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007078
7079static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007080posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007081{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007082 WAIT_TYPE status;
7083 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007084
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007085 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7086 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007087
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007088 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007089}
7090#endif /* WSTOPSIG */
7091
7092#endif /* HAVE_SYS_WAIT_H */
7093
7094
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007095#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007096#ifdef _SCO_DS
7097/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7098 needed definitions in sys/statvfs.h */
7099#define _SVID3
7100#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007101#include <sys/statvfs.h>
7102
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007103static PyObject*
7104_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007105 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7106 if (v == NULL)
7107 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007108
7109#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007110 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7111 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7112 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7113 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7114 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7115 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7116 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7117 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7118 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7119 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007120#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007121 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7122 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7123 PyStructSequence_SET_ITEM(v, 2,
7124 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7125 PyStructSequence_SET_ITEM(v, 3,
7126 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7127 PyStructSequence_SET_ITEM(v, 4,
7128 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7129 PyStructSequence_SET_ITEM(v, 5,
7130 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7131 PyStructSequence_SET_ITEM(v, 6,
7132 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7133 PyStructSequence_SET_ITEM(v, 7,
7134 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7135 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7136 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007137#endif
7138
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007139 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007140}
7141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007142PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007143"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007144Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007145
7146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007147posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007148{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007149 int fd, res;
7150 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007151
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007152 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7153 return NULL;
7154 Py_BEGIN_ALLOW_THREADS
7155 res = fstatvfs(fd, &st);
7156 Py_END_ALLOW_THREADS
7157 if (res != 0)
7158 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007159
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007160 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007161}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007162#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007163
7164
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007165#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007166#include <sys/statvfs.h>
7167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007168PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007169"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007170Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007171
7172static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007173posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007174{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007175 char *path;
7176 int res;
7177 struct statvfs st;
7178 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7179 return NULL;
7180 Py_BEGIN_ALLOW_THREADS
7181 res = statvfs(path, &st);
7182 Py_END_ALLOW_THREADS
7183 if (res != 0)
7184 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007185
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007186 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007187}
7188#endif /* HAVE_STATVFS */
7189
7190
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007191#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007192PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007193"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007194Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007195The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007196or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007197
7198static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007199posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007200{
7201 PyObject *result = NULL;
7202 char *dir = NULL;
7203 char *pfx = NULL;
7204 char *name;
7205
7206 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007207 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007208
7209 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007210 "tempnam is a potential security risk to your program") < 0)
7211 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007212
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007213#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007214 name = _tempnam(dir, pfx);
7215#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007216 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007217#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007218 if (name == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007219 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007220 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007221 free(name);
7222 return result;
7223}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007224#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007225
7226
7227#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007228PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007229"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007230Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007231
7232static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007233posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007234{
7235 FILE *fp;
7236
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007237 fp = tmpfile();
7238 if (fp == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007239 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007240 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007241}
7242#endif
7243
7244
7245#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007246PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007247"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007248Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007249
7250static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007251posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007252{
7253 char buffer[L_tmpnam];
7254 char *name;
7255
Skip Montanaro95618b52001-08-18 18:52:10 +00007256 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007257 "tmpnam is a potential security risk to your program") < 0)
7258 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007259
Greg Wardb48bc172000-03-01 21:51:56 +00007260#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007261 name = tmpnam_r(buffer);
7262#else
7263 name = tmpnam(buffer);
7264#endif
7265 if (name == NULL) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007266 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007267#ifdef USE_TMPNAM_R
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007268 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007269#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007270 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007271#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007272 );
7273 PyErr_SetObject(PyExc_OSError, err);
7274 Py_XDECREF(err);
7275 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007276 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007277 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007278}
7279#endif
7280
7281
Fred Drakec9680921999-12-13 16:37:25 +00007282/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7283 * It maps strings representing configuration variable names to
7284 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007285 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007286 * rarely-used constants. There are three separate tables that use
7287 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007288 *
7289 * This code is always included, even if none of the interfaces that
7290 * need it are included. The #if hackery needed to avoid it would be
7291 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007292 */
7293struct constdef {
7294 char *name;
7295 long value;
7296};
7297
Fred Drake12c6e2d1999-12-14 21:25:03 +00007298static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007299conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007300 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007301{
7302 if (PyInt_Check(arg)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007303 *valuep = PyInt_AS_LONG(arg);
7304 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007305 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007306 if (PyString_Check(arg)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007307 /* look up the value in the table using a binary search */
7308 size_t lo = 0;
7309 size_t mid;
7310 size_t hi = tablesize;
7311 int cmp;
7312 char *confname = PyString_AS_STRING(arg);
7313 while (lo < hi) {
7314 mid = (lo + hi) / 2;
7315 cmp = strcmp(confname, table[mid].name);
7316 if (cmp < 0)
7317 hi = mid;
7318 else if (cmp > 0)
7319 lo = mid + 1;
7320 else {
7321 *valuep = table[mid].value;
7322 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007323 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007324 }
7325 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007326 }
7327 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007328 PyErr_SetString(PyExc_TypeError,
7329 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007330 return 0;
7331}
7332
7333
7334#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7335static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007336#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007337 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007338#endif
7339#ifdef _PC_ABI_ASYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007340 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007341#endif
Fred Drakec9680921999-12-13 16:37:25 +00007342#ifdef _PC_ASYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007343 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007344#endif
7345#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007346 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007347#endif
7348#ifdef _PC_FILESIZEBITS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007349 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007350#endif
7351#ifdef _PC_LAST
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007352 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007353#endif
7354#ifdef _PC_LINK_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007355 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007356#endif
7357#ifdef _PC_MAX_CANON
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007358 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007359#endif
7360#ifdef _PC_MAX_INPUT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007361 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007362#endif
7363#ifdef _PC_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007364 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007365#endif
7366#ifdef _PC_NO_TRUNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007367 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007368#endif
7369#ifdef _PC_PATH_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007370 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007371#endif
7372#ifdef _PC_PIPE_BUF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007373 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007374#endif
7375#ifdef _PC_PRIO_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007376 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007377#endif
7378#ifdef _PC_SOCK_MAXBUF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007379 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007380#endif
7381#ifdef _PC_SYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007382 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007383#endif
7384#ifdef _PC_VDISABLE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007385 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007386#endif
7387};
7388
Fred Drakec9680921999-12-13 16:37:25 +00007389static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007390conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007391{
7392 return conv_confname(arg, valuep, posix_constants_pathconf,
7393 sizeof(posix_constants_pathconf)
7394 / sizeof(struct constdef));
7395}
7396#endif
7397
7398#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007399PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007400"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007401Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007402If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007403
7404static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007405posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007406{
7407 PyObject *result = NULL;
7408 int name, fd;
7409
Fred Drake12c6e2d1999-12-14 21:25:03 +00007410 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7411 conv_path_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007412 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007413
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007414 errno = 0;
7415 limit = fpathconf(fd, name);
7416 if (limit == -1 && errno != 0)
7417 posix_error();
7418 else
7419 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007420 }
7421 return result;
7422}
7423#endif
7424
7425
7426#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007427PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007428"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007429Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007430If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007431
7432static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007433posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007434{
7435 PyObject *result = NULL;
7436 int name;
7437 char *path;
7438
7439 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7440 conv_path_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007441 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007442
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007443 errno = 0;
7444 limit = pathconf(path, name);
7445 if (limit == -1 && errno != 0) {
7446 if (errno == EINVAL)
7447 /* could be a path or name problem */
7448 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007449 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007450 posix_error_with_filename(path);
7451 }
7452 else
7453 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007454 }
7455 return result;
7456}
7457#endif
7458
7459#ifdef HAVE_CONFSTR
7460static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007461#ifdef _CS_ARCHITECTURE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007462 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007463#endif
7464#ifdef _CS_HOSTNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007465 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007466#endif
7467#ifdef _CS_HW_PROVIDER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007468 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007469#endif
7470#ifdef _CS_HW_SERIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007471 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007472#endif
7473#ifdef _CS_INITTAB_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007474 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007475#endif
Fred Drakec9680921999-12-13 16:37:25 +00007476#ifdef _CS_LFS64_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007477 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007478#endif
7479#ifdef _CS_LFS64_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007480 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007481#endif
7482#ifdef _CS_LFS64_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007483 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007484#endif
7485#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007486 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007487#endif
7488#ifdef _CS_LFS_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007489 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007490#endif
7491#ifdef _CS_LFS_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007492 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007493#endif
7494#ifdef _CS_LFS_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007495 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007496#endif
7497#ifdef _CS_LFS_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007498 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007499#endif
Fred Draked86ed291999-12-15 15:34:33 +00007500#ifdef _CS_MACHINE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007501 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007502#endif
Fred Drakec9680921999-12-13 16:37:25 +00007503#ifdef _CS_PATH
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007504 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007505#endif
Fred Draked86ed291999-12-15 15:34:33 +00007506#ifdef _CS_RELEASE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007507 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007508#endif
7509#ifdef _CS_SRPC_DOMAIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007510 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007511#endif
7512#ifdef _CS_SYSNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007513 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007514#endif
7515#ifdef _CS_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007516 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007517#endif
Fred Drakec9680921999-12-13 16:37:25 +00007518#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007519 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007520#endif
7521#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007522 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007523#endif
7524#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007525 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007526#endif
7527#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007528 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007529#endif
7530#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007531 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007532#endif
7533#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007534 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007535#endif
7536#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007537 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007538#endif
7539#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007540 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007541#endif
7542#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007543 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007544#endif
7545#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007546 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007547#endif
7548#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007549 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007550#endif
7551#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007552 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007553#endif
7554#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007555 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007556#endif
7557#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007558 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007559#endif
7560#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007561 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007562#endif
7563#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007564 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007565#endif
Fred Draked86ed291999-12-15 15:34:33 +00007566#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007567 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007568#endif
7569#ifdef _MIPS_CS_BASE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007570 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007571#endif
7572#ifdef _MIPS_CS_HOSTID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007573 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007574#endif
7575#ifdef _MIPS_CS_HW_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007576 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007577#endif
7578#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007579 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007580#endif
7581#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007582 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007583#endif
7584#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007585 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007586#endif
7587#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007588 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007589#endif
7590#ifdef _MIPS_CS_OS_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007591 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007592#endif
7593#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007594 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007595#endif
7596#ifdef _MIPS_CS_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007597 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007598#endif
7599#ifdef _MIPS_CS_SERIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007600 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007601#endif
7602#ifdef _MIPS_CS_VENDOR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007603 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007604#endif
Fred Drakec9680921999-12-13 16:37:25 +00007605};
7606
7607static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007608conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007609{
7610 return conv_confname(arg, valuep, posix_constants_confstr,
7611 sizeof(posix_constants_confstr)
7612 / sizeof(struct constdef));
7613}
7614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007615PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007616"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007617Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007618
7619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007620posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007621{
7622 PyObject *result = NULL;
7623 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007624 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007625
7626 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007627 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007628
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007629 errno = 0;
7630 len = confstr(name, buffer, sizeof(buffer));
7631 if (len == 0) {
7632 if (errno) {
7633 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007634 }
7635 else {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007636 result = Py_None;
7637 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007638 }
7639 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007640 else {
7641 if ((unsigned int)len >= sizeof(buffer)) {
7642 result = PyString_FromStringAndSize(NULL, len-1);
7643 if (result != NULL)
7644 confstr(name, PyString_AS_STRING(result), len);
7645 }
7646 else
7647 result = PyString_FromStringAndSize(buffer, len-1);
7648 }
7649 }
Fred Drakec9680921999-12-13 16:37:25 +00007650 return result;
7651}
7652#endif
7653
7654
7655#ifdef HAVE_SYSCONF
7656static struct constdef posix_constants_sysconf[] = {
7657#ifdef _SC_2_CHAR_TERM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007658 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007659#endif
7660#ifdef _SC_2_C_BIND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007661 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007662#endif
7663#ifdef _SC_2_C_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007664 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007665#endif
7666#ifdef _SC_2_C_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007667 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007668#endif
7669#ifdef _SC_2_FORT_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007670 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007671#endif
7672#ifdef _SC_2_FORT_RUN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007673 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007674#endif
7675#ifdef _SC_2_LOCALEDEF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007676 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007677#endif
7678#ifdef _SC_2_SW_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007679 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007680#endif
7681#ifdef _SC_2_UPE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007682 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007683#endif
7684#ifdef _SC_2_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007685 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007686#endif
Fred Draked86ed291999-12-15 15:34:33 +00007687#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007688 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007689#endif
7690#ifdef _SC_ACL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007691 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007692#endif
Fred Drakec9680921999-12-13 16:37:25 +00007693#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007694 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007695#endif
Fred Drakec9680921999-12-13 16:37:25 +00007696#ifdef _SC_AIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007697 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007698#endif
7699#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007700 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007701#endif
7702#ifdef _SC_ARG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007703 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007704#endif
7705#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007706 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007707#endif
7708#ifdef _SC_ATEXIT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007709 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007710#endif
Fred Draked86ed291999-12-15 15:34:33 +00007711#ifdef _SC_AUDIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007712 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007713#endif
Fred Drakec9680921999-12-13 16:37:25 +00007714#ifdef _SC_AVPHYS_PAGES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007715 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007716#endif
7717#ifdef _SC_BC_BASE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007718 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007719#endif
7720#ifdef _SC_BC_DIM_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007721 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007722#endif
7723#ifdef _SC_BC_SCALE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007724 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007725#endif
7726#ifdef _SC_BC_STRING_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007727 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007728#endif
Fred Draked86ed291999-12-15 15:34:33 +00007729#ifdef _SC_CAP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007730 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007731#endif
Fred Drakec9680921999-12-13 16:37:25 +00007732#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007733 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007734#endif
7735#ifdef _SC_CHAR_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007736 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007737#endif
7738#ifdef _SC_CHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007739 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007740#endif
7741#ifdef _SC_CHAR_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007742 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007743#endif
7744#ifdef _SC_CHILD_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007745 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007746#endif
7747#ifdef _SC_CLK_TCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007748 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007749#endif
7750#ifdef _SC_COHER_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007751 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007752#endif
7753#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007754 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007755#endif
7756#ifdef _SC_DCACHE_ASSOC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007757 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007758#endif
7759#ifdef _SC_DCACHE_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007760 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007761#endif
7762#ifdef _SC_DCACHE_LINESZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007763 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007764#endif
7765#ifdef _SC_DCACHE_SZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007766 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007767#endif
7768#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007769 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007770#endif
7771#ifdef _SC_DELAYTIMER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007772 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007773#endif
7774#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007775 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007776#endif
7777#ifdef _SC_EXPR_NEST_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007778 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007779#endif
7780#ifdef _SC_FSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007781 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007782#endif
7783#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007784 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007785#endif
7786#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007787 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _SC_ICACHE_ASSOC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007790 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _SC_ICACHE_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007793 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _SC_ICACHE_LINESZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007796 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _SC_ICACHE_SZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007799 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
Fred Draked86ed291999-12-15 15:34:33 +00007801#ifdef _SC_INF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007802 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007803#endif
Fred Drakec9680921999-12-13 16:37:25 +00007804#ifdef _SC_INT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007805 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
7807#ifdef _SC_INT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007808 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007809#endif
7810#ifdef _SC_IOV_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007811 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007812#endif
Fred Draked86ed291999-12-15 15:34:33 +00007813#ifdef _SC_IP_SECOPTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007814 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007815#endif
Fred Drakec9680921999-12-13 16:37:25 +00007816#ifdef _SC_JOB_CONTROL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007817 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007818#endif
Fred Draked86ed291999-12-15 15:34:33 +00007819#ifdef _SC_KERN_POINTERS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007820 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007821#endif
7822#ifdef _SC_KERN_SIM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007823 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007824#endif
Fred Drakec9680921999-12-13 16:37:25 +00007825#ifdef _SC_LINE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007826 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007827#endif
7828#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007829 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _SC_LOGNAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007832 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _SC_LONG_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007835 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
Fred Draked86ed291999-12-15 15:34:33 +00007837#ifdef _SC_MAC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007838 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007839#endif
Fred Drakec9680921999-12-13 16:37:25 +00007840#ifdef _SC_MAPPED_FILES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007841 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007842#endif
7843#ifdef _SC_MAXPID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007844 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
7846#ifdef _SC_MB_LEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007847 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007848#endif
7849#ifdef _SC_MEMLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007850 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007851#endif
7852#ifdef _SC_MEMLOCK_RANGE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007853 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007854#endif
7855#ifdef _SC_MEMORY_PROTECTION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007856 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007857#endif
7858#ifdef _SC_MESSAGE_PASSING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007859 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007860#endif
Fred Draked86ed291999-12-15 15:34:33 +00007861#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007862 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007863#endif
Fred Drakec9680921999-12-13 16:37:25 +00007864#ifdef _SC_MQ_OPEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007865 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007866#endif
7867#ifdef _SC_MQ_PRIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007868 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007869#endif
Fred Draked86ed291999-12-15 15:34:33 +00007870#ifdef _SC_NACLS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007871 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007872#endif
Fred Drakec9680921999-12-13 16:37:25 +00007873#ifdef _SC_NGROUPS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007874 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007875#endif
7876#ifdef _SC_NL_ARGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007877 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007878#endif
7879#ifdef _SC_NL_LANGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007880 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007881#endif
7882#ifdef _SC_NL_MSGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007883 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007884#endif
7885#ifdef _SC_NL_NMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007886 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007887#endif
7888#ifdef _SC_NL_SETMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007889 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
7891#ifdef _SC_NL_TEXTMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007892 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007893#endif
7894#ifdef _SC_NPROCESSORS_CONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007895 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007896#endif
7897#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007898 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007899#endif
Fred Draked86ed291999-12-15 15:34:33 +00007900#ifdef _SC_NPROC_CONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007901 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007902#endif
7903#ifdef _SC_NPROC_ONLN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007904 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007905#endif
Fred Drakec9680921999-12-13 16:37:25 +00007906#ifdef _SC_NZERO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007907 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007908#endif
7909#ifdef _SC_OPEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007910 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007911#endif
7912#ifdef _SC_PAGESIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007913 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007914#endif
7915#ifdef _SC_PAGE_SIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007916 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007917#endif
7918#ifdef _SC_PASS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007919 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_PHYS_PAGES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007922 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
7924#ifdef _SC_PII
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007925 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007926#endif
7927#ifdef _SC_PII_INTERNET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007928 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007931 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
7933#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007934 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007935#endif
7936#ifdef _SC_PII_OSI
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007937 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
7939#ifdef _SC_PII_OSI_CLTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007940 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007941#endif
7942#ifdef _SC_PII_OSI_COTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007943 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007944#endif
7945#ifdef _SC_PII_OSI_M
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007946 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007947#endif
7948#ifdef _SC_PII_SOCKET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007949 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007950#endif
7951#ifdef _SC_PII_XTI
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007952 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
7954#ifdef _SC_POLL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007955 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007956#endif
7957#ifdef _SC_PRIORITIZED_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007958 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007959#endif
7960#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007961 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_REALTIME_SIGNALS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007964 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_RE_DUP_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007967 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
7969#ifdef _SC_RTSIG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007970 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007971#endif
7972#ifdef _SC_SAVED_IDS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007973 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007974#endif
7975#ifdef _SC_SCHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007976 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007977#endif
7978#ifdef _SC_SCHAR_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007979 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_SELECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007982 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_SEMAPHORES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007985 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
7987#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007988 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007989#endif
7990#ifdef _SC_SEM_VALUE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007991 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007994 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_SHRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007997 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_SHRT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008000 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_SIGQUEUE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008003 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_SIGRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008006 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_SIGRT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008009 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
Fred Draked86ed291999-12-15 15:34:33 +00008011#ifdef _SC_SOFTPOWER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008012 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008013#endif
Fred Drakec9680921999-12-13 16:37:25 +00008014#ifdef _SC_SPLIT_CACHE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008015 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_SSIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008018 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_STACK_PROT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008021 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_STREAM_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008024 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008027 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_THREADS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008030 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008033 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008036 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008039 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008042 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008045 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008048 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008051 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
8053#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008054 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
8056#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008057 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008058#endif
8059#ifdef _SC_THREAD_STACK_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008060 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008061#endif
8062#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008063 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
8065#ifdef _SC_TIMERS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008066 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008067#endif
8068#ifdef _SC_TIMER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008069 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
8071#ifdef _SC_TTY_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008072 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008073#endif
8074#ifdef _SC_TZNAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008075 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
8077#ifdef _SC_T_IOV_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008078 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef _SC_UCHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008081 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef _SC_UINT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008084 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef _SC_UIO_MAXIOV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008087 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_ULONG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008090 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_USHRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008093 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095#ifdef _SC_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008096 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008097#endif
8098#ifdef _SC_WORD_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008099 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008102 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008105 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008108 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008111 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _SC_XOPEN_CRYPT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008114 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008117 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _SC_XOPEN_LEGACY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008120 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _SC_XOPEN_REALTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008123 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008126 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _SC_XOPEN_SHM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008129 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _SC_XOPEN_UNIX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008132 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _SC_XOPEN_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008135 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008138 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _SC_XOPEN_XPG2
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008141 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _SC_XOPEN_XPG3
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008144 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _SC_XOPEN_XPG4
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008147 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149};
8150
8151static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008152conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008153{
8154 return conv_confname(arg, valuep, posix_constants_sysconf,
8155 sizeof(posix_constants_sysconf)
8156 / sizeof(struct constdef));
8157}
8158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008159PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008160"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008161Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008162
8163static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008164posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008165{
8166 PyObject *result = NULL;
8167 int name;
8168
8169 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8170 int value;
8171
8172 errno = 0;
8173 value = sysconf(name);
8174 if (value == -1 && errno != 0)
8175 posix_error();
8176 else
8177 result = PyInt_FromLong(value);
8178 }
8179 return result;
8180}
8181#endif
8182
8183
Fred Drakebec628d1999-12-15 18:31:10 +00008184/* This code is used to ensure that the tables of configuration value names
8185 * are in sorted order as required by conv_confname(), and also to build the
8186 * the exported dictionaries that are used to publish information about the
8187 * names available on the host platform.
8188 *
8189 * Sorting the table at runtime ensures that the table is properly ordered
8190 * when used, even for platforms we're not able to test on. It also makes
8191 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008192 */
Fred Drakebec628d1999-12-15 18:31:10 +00008193
8194static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008195cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008196{
8197 const struct constdef *c1 =
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008198 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008199 const struct constdef *c2 =
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008200 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008201
8202 return strcmp(c1->name, c2->name);
8203}
8204
8205static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008206setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008207 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008208{
Fred Drakebec628d1999-12-15 18:31:10 +00008209 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008210 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008211
8212 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8213 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008214 if (d == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008215 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008216
Barry Warsaw3155db32000-04-13 15:20:40 +00008217 for (i=0; i < tablesize; ++i) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008218 PyObject *o = PyInt_FromLong(table[i].value);
8219 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8220 Py_XDECREF(o);
8221 Py_DECREF(d);
8222 return -1;
8223 }
8224 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008225 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008226 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008227}
8228
Fred Drakebec628d1999-12-15 18:31:10 +00008229/* Return -1 on failure, 0 on success. */
8230static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008231setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008232{
8233#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008234 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008235 sizeof(posix_constants_pathconf)
8236 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008237 "pathconf_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008238 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008239#endif
8240#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008241 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008242 sizeof(posix_constants_confstr)
8243 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008244 "confstr_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008245 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008246#endif
8247#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008248 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008249 sizeof(posix_constants_sysconf)
8250 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008251 "sysconf_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008252 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008253#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008254 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008255}
Fred Draked86ed291999-12-15 15:34:33 +00008256
8257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008258PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008259"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008260Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008261in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008262
8263static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008264posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008265{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008266 abort();
8267 /*NOTREACHED*/
8268 Py_FatalError("abort() called from Python code didn't abort!");
8269 return NULL;
8270}
Fred Drakebec628d1999-12-15 18:31:10 +00008271
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008272#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008273PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008274"startfile(filepath [, operation]) - Start a file with its associated\n\
8275application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008276\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008277When \"operation\" is not specified or \"open\", this acts like\n\
8278double-clicking the file in Explorer, or giving the file name as an\n\
8279argument to the DOS \"start\" command: the file is opened with whatever\n\
8280application (if any) its extension is associated.\n\
8281When another \"operation\" is given, it specifies what should be done with\n\
8282the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008283\n\
8284startfile returns as soon as the associated application is launched.\n\
8285There is no option to wait for the application to close, and no way\n\
8286to retrieve the application's exit status.\n\
8287\n\
8288The filepath is relative to the current directory. If you want to use\n\
8289an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008290the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008291
8292static PyObject *
8293win32_startfile(PyObject *self, PyObject *args)
8294{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008295 char *filepath;
8296 char *operation = NULL;
8297 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00008298#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008299 if (unicode_file_names()) {
8300 PyObject *unipath, *woperation = NULL;
8301 if (!PyArg_ParseTuple(args, "U|s:startfile",
8302 &unipath, &operation)) {
8303 PyErr_Clear();
8304 goto normal;
8305 }
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008306
Georg Brandlad89dc82006-04-03 12:26:26 +00008307
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008308 if (operation) {
8309 woperation = PyUnicode_DecodeASCII(operation,
8310 strlen(operation), NULL);
8311 if (!woperation) {
8312 PyErr_Clear();
8313 operation = NULL;
8314 goto normal;
8315 }
8316 }
8317
8318 Py_BEGIN_ALLOW_THREADS
8319 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8320 PyUnicode_AS_UNICODE(unipath),
8321 NULL, NULL, SW_SHOWNORMAL);
8322 Py_END_ALLOW_THREADS
8323
8324 Py_XDECREF(woperation);
8325 if (rc <= (HINSTANCE)32) {
8326 PyObject *errval = win32_error_unicode("startfile",
8327 PyUnicode_AS_UNICODE(unipath));
8328 return errval;
8329 }
8330 Py_INCREF(Py_None);
8331 return Py_None;
8332 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008333#endif
8334
8335normal:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008336 if (!PyArg_ParseTuple(args, "et|s:startfile",
8337 Py_FileSystemDefaultEncoding, &filepath,
8338 &operation))
8339 return NULL;
8340 Py_BEGIN_ALLOW_THREADS
8341 rc = ShellExecute((HWND)0, operation, filepath,
8342 NULL, NULL, SW_SHOWNORMAL);
8343 Py_END_ALLOW_THREADS
8344 if (rc <= (HINSTANCE)32) {
8345 PyObject *errval = win32_error("startfile", filepath);
8346 PyMem_Free(filepath);
8347 return errval;
8348 }
8349 PyMem_Free(filepath);
8350 Py_INCREF(Py_None);
8351 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008352}
8353#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008354
Martin v. Löwis438b5342002-12-27 10:16:42 +00008355#ifdef HAVE_GETLOADAVG
8356PyDoc_STRVAR(posix_getloadavg__doc__,
8357"getloadavg() -> (float, float, float)\n\n\
8358Return the number of processes in the system run queue averaged over\n\
8359the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8360was unobtainable");
8361
8362static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008363posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008364{
8365 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008366 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008367 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8368 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008369 } else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008370 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008371}
8372#endif
8373
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008374PyDoc_STRVAR(posix_urandom__doc__,
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008375"urandom(n) -> str\n\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008376Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008377
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008378static PyObject *
8379posix_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008380{
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008381 Py_ssize_t size;
8382 PyObject *result;
8383 int ret;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008384
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008385 /* Read arguments */
8386 if (!PyArg_ParseTuple(args, "n:urandom", &size))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008387 return NULL;
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008388 if (size < 0)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008389 return PyErr_Format(PyExc_ValueError,
8390 "negative argument not allowed");
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008391 result = PyBytes_FromStringAndSize(NULL, size);
8392 if (result == NULL)
8393 return NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008394
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008395 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
8396 PyBytes_GET_SIZE(result));
8397 if (ret == -1) {
8398 Py_DECREF(result);
8399 return NULL;
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008400 }
8401 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008402}
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008403
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008404static PyMethodDef posix_methods[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008405 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008406#ifdef HAVE_TTYNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008407 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008408#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008409 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008410#ifdef HAVE_CHFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008411 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008412#endif /* HAVE_CHFLAGS */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008413 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008414#ifdef HAVE_FCHMOD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008415 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008416#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008417#ifdef HAVE_CHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008418 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008419#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008420#ifdef HAVE_LCHMOD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008421 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008422#endif /* HAVE_LCHMOD */
8423#ifdef HAVE_FCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008424 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008425#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008426#ifdef HAVE_LCHFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008427 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008428#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008429#ifdef HAVE_LCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008430 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008431#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008432#ifdef HAVE_CHROOT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008433 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008434#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008435#ifdef HAVE_CTERMID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008436 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008437#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008438#ifdef HAVE_GETCWD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008439 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008440#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008441 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008442#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008443#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008444#ifdef HAVE_LINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008445 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008446#endif /* HAVE_LINK */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008447 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8448 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8449 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008450#ifdef HAVE_NICE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008451 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008452#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008453#ifdef HAVE_READLINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008454 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008455#endif /* HAVE_READLINK */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008456 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8457 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8458 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8459 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008460#ifdef HAVE_SYMLINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008461 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008462#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008463#ifdef HAVE_SYSTEM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008464 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008465#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008466 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008467#ifdef HAVE_UNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008468 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008469#endif /* HAVE_UNAME */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008470 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8471 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8472 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008473#ifdef HAVE_TIMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008474 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008475#endif /* HAVE_TIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008476 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008477#ifdef HAVE_EXECV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008478 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8479 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008480#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008481#ifdef HAVE_SPAWNV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008482 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8483 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008484#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008485 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8486 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008487#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008488#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008489#ifdef HAVE_FORK1
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008490 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008491#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008492#ifdef HAVE_FORK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008493 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008494#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008495#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008496 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008497#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008498#ifdef HAVE_FORKPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008499 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008500#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008501#ifdef HAVE_GETEGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008502 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008503#endif /* HAVE_GETEGID */
8504#ifdef HAVE_GETEUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008505 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008506#endif /* HAVE_GETEUID */
8507#ifdef HAVE_GETGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008508 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008509#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008510#ifdef HAVE_GETGROUPS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008511 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008512#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008513 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008514#ifdef HAVE_GETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008515 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008516#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008517#ifdef HAVE_GETPPID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008518 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008519#endif /* HAVE_GETPPID */
8520#ifdef HAVE_GETUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008521 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008522#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008523#ifdef HAVE_GETLOGIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008524 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008525#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008526#ifdef HAVE_KILL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008527 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008528#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008529#ifdef HAVE_KILLPG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008530 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008531#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008532#ifdef HAVE_PLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008533 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008534#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008535#ifdef HAVE_POPEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008536 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008537#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008538 {"popen2", win32_popen2, METH_VARARGS},
8539 {"popen3", win32_popen3, METH_VARARGS},
8540 {"popen4", win32_popen4, METH_VARARGS},
8541 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008542#else
8543#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008544 {"popen2", os2emx_popen2, METH_VARARGS},
8545 {"popen3", os2emx_popen3, METH_VARARGS},
8546 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008547#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008548#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008549#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008550#ifdef HAVE_SETUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008551 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008552#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008553#ifdef HAVE_SETEUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008554 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008555#endif /* HAVE_SETEUID */
8556#ifdef HAVE_SETEGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008557 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008558#endif /* HAVE_SETEGID */
8559#ifdef HAVE_SETREUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008560 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008561#endif /* HAVE_SETREUID */
8562#ifdef HAVE_SETREGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008563 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008564#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008565#ifdef HAVE_SETGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008566 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008567#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008568#ifdef HAVE_SETGROUPS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008569 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008570#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008571#ifdef HAVE_GETPGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008572 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008573#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008574#ifdef HAVE_SETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008575 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008576#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008577#ifdef HAVE_WAIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008578 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008579#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008580#ifdef HAVE_WAIT3
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008581 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008582#endif /* HAVE_WAIT3 */
8583#ifdef HAVE_WAIT4
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008584 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008585#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008586#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008587 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008588#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008589#ifdef HAVE_GETSID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008590 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008591#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008592#ifdef HAVE_SETSID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008593 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008594#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008595#ifdef HAVE_SETPGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008596 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008597#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008598#ifdef HAVE_TCGETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008599 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008600#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008601#ifdef HAVE_TCSETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008602 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008603#endif /* HAVE_TCSETPGRP */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008604 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8605 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8606 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8607 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8608 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8609 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8610 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8611 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8612 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8613 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8614 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008615#ifdef HAVE_PIPE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008616 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008617#endif
8618#ifdef HAVE_MKFIFO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008619 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008620#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008621#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008622 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008623#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008624#ifdef HAVE_DEVICE_MACROS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008625 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8626 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8627 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008628#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008629#ifdef HAVE_FTRUNCATE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008630 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008631#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008632#ifdef HAVE_PUTENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008633 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008634#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008635#ifdef HAVE_UNSETENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008636 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008637#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008638 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008639#ifdef HAVE_FCHDIR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008640 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008641#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008642#ifdef HAVE_FSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008643 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008644#endif
8645#ifdef HAVE_FDATASYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008646 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008647#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008648#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008649#ifdef WCOREDUMP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008650 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008651#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008652#ifdef WIFCONTINUED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008653 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008654#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008655#ifdef WIFSTOPPED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008656 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008657#endif /* WIFSTOPPED */
8658#ifdef WIFSIGNALED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008659 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008660#endif /* WIFSIGNALED */
8661#ifdef WIFEXITED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008662 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008663#endif /* WIFEXITED */
8664#ifdef WEXITSTATUS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008665 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008666#endif /* WEXITSTATUS */
8667#ifdef WTERMSIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008668 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008669#endif /* WTERMSIG */
8670#ifdef WSTOPSIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008671 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008672#endif /* WSTOPSIG */
8673#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008674#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008675 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008676#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008677#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008678 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008679#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008680#ifdef HAVE_TMPFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008681 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008682#endif
8683#ifdef HAVE_TEMPNAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008684 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008685#endif
8686#ifdef HAVE_TMPNAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008687 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008688#endif
Fred Drakec9680921999-12-13 16:37:25 +00008689#ifdef HAVE_CONFSTR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008690 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008691#endif
8692#ifdef HAVE_SYSCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008693 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008694#endif
8695#ifdef HAVE_FPATHCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008696 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008697#endif
8698#ifdef HAVE_PATHCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008699 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008700#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008701 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008702#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008703 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008704#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008705#ifdef HAVE_GETLOADAVG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008706 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008707#endif
Barry Warsaw1e13eb02012-02-20 20:42:21 -05008708 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008709 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008710};
8711
8712
Barry Warsaw4a342091996-12-19 23:50:02 +00008713static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008714ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008715{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008716 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008717}
8718
Guido van Rossumd48f2521997-12-05 22:19:34 +00008719#if defined(PYOS_OS2)
8720/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008721static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008722{
8723 APIRET rc;
8724 ULONG values[QSV_MAX+1];
8725 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008726 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008727
8728 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008729 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008730 Py_END_ALLOW_THREADS
8731
8732 if (rc != NO_ERROR) {
8733 os2_error(rc);
8734 return -1;
8735 }
8736
Fred Drake4d1e64b2002-04-15 19:40:07 +00008737 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8738 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8739 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8740 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8741 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8742 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8743 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008744
8745 switch (values[QSV_VERSION_MINOR]) {
8746 case 0: ver = "2.00"; break;
8747 case 10: ver = "2.10"; break;
8748 case 11: ver = "2.11"; break;
8749 case 30: ver = "3.00"; break;
8750 case 40: ver = "4.00"; break;
8751 case 50: ver = "5.00"; break;
8752 default:
Tim Peters885d4572001-11-28 20:27:42 +00008753 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008754 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008755 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008756 ver = &tmp[0];
8757 }
8758
8759 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008760 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008761 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008762
8763 /* Add Indicator of Which Drive was Used to Boot the System */
8764 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8765 tmp[1] = ':';
8766 tmp[2] = '\0';
8767
Fred Drake4d1e64b2002-04-15 19:40:07 +00008768 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008769}
8770#endif
8771
Barry Warsaw4a342091996-12-19 23:50:02 +00008772static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008773all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008774{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008775#ifdef F_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008776 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008777#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008778#ifdef R_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008779 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008780#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008781#ifdef W_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008782 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008783#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008784#ifdef X_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008785 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008786#endif
Fred Drakec9680921999-12-13 16:37:25 +00008787#ifdef NGROUPS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008788 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008790#ifdef TMP_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008791 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008792#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008793#ifdef WCONTINUED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008794 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008795#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008796#ifdef WNOHANG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008797 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008798#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008799#ifdef WUNTRACED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008800 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008801#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008802#ifdef O_RDONLY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008803 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008804#endif
8805#ifdef O_WRONLY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008806 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008807#endif
8808#ifdef O_RDWR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008809 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008810#endif
8811#ifdef O_NDELAY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008812 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008813#endif
8814#ifdef O_NONBLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008815 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008816#endif
8817#ifdef O_APPEND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008818 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008819#endif
8820#ifdef O_DSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008821 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008822#endif
8823#ifdef O_RSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008824 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008825#endif
8826#ifdef O_SYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008827 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008828#endif
8829#ifdef O_NOCTTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008830 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008831#endif
8832#ifdef O_CREAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008833 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008834#endif
8835#ifdef O_EXCL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008836 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008837#endif
8838#ifdef O_TRUNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008839 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008840#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008841#ifdef O_BINARY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008842 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008843#endif
8844#ifdef O_TEXT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008845 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008846#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008847#ifdef O_LARGEFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008848 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008849#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008850#ifdef O_SHLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008851 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008852#endif
8853#ifdef O_EXLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008854 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008855#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008856
Tim Peters5aa91602002-01-30 05:46:57 +00008857/* MS Windows */
8858#ifdef O_NOINHERIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008859 /* Don't inherit in child processes. */
8860 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008861#endif
8862#ifdef _O_SHORT_LIVED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008863 /* Optimize for short life (keep in memory). */
8864 /* MS forgot to define this one with a non-underscore form too. */
8865 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008866#endif
8867#ifdef O_TEMPORARY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008868 /* Automatically delete when last handle is closed. */
8869 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008870#endif
8871#ifdef O_RANDOM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008872 /* Optimize for random access. */
8873 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008874#endif
8875#ifdef O_SEQUENTIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008876 /* Optimize for sequential access. */
8877 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008878#endif
8879
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008880/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00008881#ifdef O_ASYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008882 /* Send a SIGIO signal whenever input or output
8883 becomes available on file descriptor */
8884 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00008885#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008886#ifdef O_DIRECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008887 /* Direct disk access. */
8888 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008889#endif
8890#ifdef O_DIRECTORY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008891 /* Must be a directory. */
8892 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008893#endif
8894#ifdef O_NOFOLLOW
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008895 /* Do not follow links. */
8896 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008897#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008898#ifdef O_NOATIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008899 /* Do not update the access time. */
8900 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00008901#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008902
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008903 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008904#ifdef EX_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008905 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008906#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008907#ifdef EX_USAGE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008908 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008909#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008910#ifdef EX_DATAERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008911 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008912#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008913#ifdef EX_NOINPUT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008914 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008915#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008916#ifdef EX_NOUSER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008917 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008918#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008919#ifdef EX_NOHOST
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008920 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008921#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008922#ifdef EX_UNAVAILABLE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008923 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008924#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008925#ifdef EX_SOFTWARE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008926 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008927#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008928#ifdef EX_OSERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008929 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008930#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008931#ifdef EX_OSFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008932 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008933#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008934#ifdef EX_CANTCREAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008935 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008936#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008937#ifdef EX_IOERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008938 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008939#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008940#ifdef EX_TEMPFAIL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008941 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008942#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008943#ifdef EX_PROTOCOL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008944 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008945#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008946#ifdef EX_NOPERM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008947 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008948#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008949#ifdef EX_CONFIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008950 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008951#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008952#ifdef EX_NOTFOUND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008953 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008954#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008955
Guido van Rossum246bc171999-02-01 23:54:31 +00008956#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008957#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008958 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8959 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8960 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8961 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8962 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8963 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8964 if (ins(d, "P_PM", (long)P_PM)) return -1;
8965 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8966 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8967 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8968 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8969 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8970 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8971 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8972 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8973 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8974 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8975 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8976 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8977 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008978#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008979 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8980 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8981 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8982 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8983 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008984#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008985#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008986
Guido van Rossumd48f2521997-12-05 22:19:34 +00008987#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008988 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008989#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008990 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008991}
8992
8993
Tim Peters5aa91602002-01-30 05:46:57 +00008994#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008995#define INITFUNC initnt
8996#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008997
8998#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008999#define INITFUNC initos2
9000#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009001
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009002#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009003#define INITFUNC initposix
9004#define MODNAME "posix"
9005#endif
9006
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009007PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009008INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009009{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009010 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009011
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009012 m = Py_InitModule3(MODNAME,
9013 posix_methods,
9014 posix__doc__);
9015 if (m == NULL)
9016 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009017
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009018 /* Initialize environ dictionary */
9019 v = convertenviron();
9020 Py_XINCREF(v);
9021 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9022 return;
9023 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009024
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009025 if (all_ins(m))
9026 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009027
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009028 if (setup_confname_tables(m))
9029 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009030
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009031 Py_INCREF(PyExc_OSError);
9032 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009033
Guido van Rossumb3d39562000-01-31 18:41:26 +00009034#ifdef HAVE_PUTENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009035 if (posix_putenv_garbage == NULL)
9036 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009037#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009038
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009039 if (!initialized) {
9040 stat_result_desc.name = MODNAME ".stat_result";
9041 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9042 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9043 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9044 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9045 structseq_new = StatResultType.tp_new;
9046 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009047
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009048 statvfs_result_desc.name = MODNAME ".statvfs_result";
9049 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009050#ifdef NEED_TICKS_PER_SECOND
9051# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009052 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009053# elif defined(HZ)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009054 ticks_per_second = HZ;
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009055# else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009056 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009057# endif
9058#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009059 }
9060 Py_INCREF((PyObject*) &StatResultType);
9061 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9062 Py_INCREF((PyObject*) &StatVFSResultType);
9063 PyModule_AddObject(m, "statvfs_result",
9064 (PyObject*) &StatVFSResultType);
9065 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009066
9067#ifdef __APPLE__
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009068 /*
9069 * Step 2 of weak-linking support on Mac OS X.
9070 *
9071 * The code below removes functions that are not available on the
9072 * currently active platform.
9073 *
9074 * This block allow one to use a python binary that was build on
9075 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9076 * OSX 10.4.
9077 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009078#ifdef HAVE_FSTATVFS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009079 if (fstatvfs == NULL) {
9080 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9081 return;
9082 }
9083 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009084#endif /* HAVE_FSTATVFS */
9085
9086#ifdef HAVE_STATVFS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009087 if (statvfs == NULL) {
9088 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9089 return;
9090 }
9091 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009092#endif /* HAVE_STATVFS */
9093
9094# ifdef HAVE_LCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009095 if (lchown == NULL) {
9096 if (PyObject_DelAttrString(m, "lchown") == -1) {
9097 return;
9098 }
9099 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009100#endif /* HAVE_LCHOWN */
9101
9102
9103#endif /* __APPLE__ */
9104
Guido van Rossumb6775db1994-08-01 11:34:53 +00009105}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009106
9107#ifdef __cplusplus
9108}
9109#endif
9110
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009111