blob: eb93ae0e83598449f248744fe0f4bea48a4d8998 [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
Barry Warsaw53699e91996-12-10 23:23:01 +00001973static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001974posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001975{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001976 int bufsize_incr = 1024;
1977 int bufsize = 0;
1978 char *tmpbuf = NULL;
1979 char *res = NULL;
1980 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001981
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001982 Py_BEGIN_ALLOW_THREADS
1983 do {
1984 bufsize = bufsize + bufsize_incr;
1985 tmpbuf = malloc(bufsize);
1986 if (tmpbuf == NULL) {
1987 break;
1988 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001989#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001990 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001991#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001992 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001993#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00001994
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001995 if (res == NULL) {
1996 free(tmpbuf);
1997 }
1998 } while ((res == NULL) && (errno == ERANGE));
1999 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002000
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002001 if (res == NULL)
2002 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002003
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002004 dynamic_return = PyString_FromString(tmpbuf);
2005 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002006
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002007 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002008}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002009
Walter Dörwald3b918c32002-11-21 20:18:46 +00002010#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011PyDoc_STRVAR(posix_getcwdu__doc__,
2012"getcwdu() -> path\n\n\
2013Return a unicode string representing the current working directory.");
2014
2015static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002016posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002018 char buf[1026];
2019 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002020
2021#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002022 DWORD len;
2023 if (unicode_file_names()) {
2024 wchar_t wbuf[1026];
2025 wchar_t *wbuf2 = wbuf;
2026 PyObject *resobj;
2027 Py_BEGIN_ALLOW_THREADS
2028 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2029 /* If the buffer is large enough, len does not include the
2030 terminating \0. If the buffer is too small, len includes
2031 the space needed for the terminator. */
2032 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2033 wbuf2 = malloc(len * sizeof(wchar_t));
2034 if (wbuf2)
2035 len = GetCurrentDirectoryW(len, wbuf2);
2036 }
2037 Py_END_ALLOW_THREADS
2038 if (!wbuf2) {
2039 PyErr_NoMemory();
2040 return NULL;
2041 }
2042 if (!len) {
2043 if (wbuf2 != wbuf) free(wbuf2);
2044 return win32_error("getcwdu", NULL);
2045 }
2046 resobj = PyUnicode_FromWideChar(wbuf2, len);
2047 if (wbuf2 != wbuf) free(wbuf2);
2048 return resobj;
2049 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002050#endif
2051
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002052 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002053#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002054 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002056 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002058 Py_END_ALLOW_THREADS
2059 if (res == NULL)
2060 return posix_error();
2061 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002062}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002063#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00002064#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
Guido van Rossumb6775db1994-08-01 11:34:53 +00002067#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002069"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002071
Barry Warsaw53699e91996-12-10 23:23:01 +00002072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002073posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002075 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002076}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002077#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002080PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002081"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002082Return a list containing the names of the entries in the directory.\n\
2083\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002084 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085\n\
2086The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002087entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088
Barry Warsaw53699e91996-12-10 23:23:01 +00002089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002090posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002091{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002092 /* XXX Should redo this putting the (now four) versions of opendir
2093 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002094#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002095
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002096 PyObject *d, *v;
2097 HANDLE hFindFile;
2098 BOOL result;
2099 WIN32_FIND_DATA FileData;
2100 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2101 char *bufptr = namebuf;
2102 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002104#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002105 /* If on wide-character-capable OS see if argument
2106 is Unicode and if so use wide API. */
2107 if (unicode_file_names()) {
2108 PyObject *po;
2109 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2110 WIN32_FIND_DATAW wFileData;
2111 Py_UNICODE *wnamebuf;
2112 Py_UNICODE wch;
2113 /* Overallocate for \\*.*\0 */
2114 len = PyUnicode_GET_SIZE(po);
2115 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2116 if (!wnamebuf) {
2117 PyErr_NoMemory();
2118 return NULL;
2119 }
2120 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2121 wch = len > 0 ? wnamebuf[len-1] : '\0';
2122 if (wch != L'/' && wch != L'\\' && wch != L':')
2123 wnamebuf[len++] = L'\\';
2124 wcscpy(wnamebuf + len, L"*.*");
2125 if ((d = PyList_New(0)) == NULL) {
2126 free(wnamebuf);
2127 return NULL;
2128 }
2129 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2130 if (hFindFile == INVALID_HANDLE_VALUE) {
2131 int error = GetLastError();
2132 if (error == ERROR_FILE_NOT_FOUND) {
2133 free(wnamebuf);
2134 return d;
2135 }
2136 Py_DECREF(d);
2137 win32_error_unicode("FindFirstFileW", wnamebuf);
2138 free(wnamebuf);
2139 return NULL;
2140 }
2141 do {
2142 /* Skip over . and .. */
2143 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2144 wcscmp(wFileData.cFileName, L"..") != 0) {
2145 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2146 if (v == NULL) {
2147 Py_DECREF(d);
2148 d = NULL;
2149 break;
2150 }
2151 if (PyList_Append(d, v) != 0) {
2152 Py_DECREF(v);
2153 Py_DECREF(d);
2154 d = NULL;
2155 break;
2156 }
2157 Py_DECREF(v);
2158 }
2159 Py_BEGIN_ALLOW_THREADS
2160 result = FindNextFileW(hFindFile, &wFileData);
2161 Py_END_ALLOW_THREADS
2162 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2163 it got to the end of the directory. */
2164 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2165 Py_DECREF(d);
2166 win32_error_unicode("FindNextFileW", wnamebuf);
2167 FindClose(hFindFile);
2168 free(wnamebuf);
2169 return NULL;
2170 }
2171 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002172
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002173 if (FindClose(hFindFile) == FALSE) {
2174 Py_DECREF(d);
2175 win32_error_unicode("FindClose", wnamebuf);
2176 free(wnamebuf);
2177 return NULL;
2178 }
2179 free(wnamebuf);
2180 return d;
2181 }
2182 /* Drop the argument parsing error as narrow strings
2183 are also valid. */
2184 PyErr_Clear();
2185 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002186#endif
2187
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002188 if (!PyArg_ParseTuple(args, "et#:listdir",
2189 Py_FileSystemDefaultEncoding, &bufptr, &len))
2190 return NULL;
2191 if (len > 0) {
2192 char ch = namebuf[len-1];
2193 if (ch != SEP && ch != ALTSEP && ch != ':')
2194 namebuf[len++] = '/';
2195 }
2196 strcpy(namebuf + len, "*.*");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002197
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002198 if ((d = PyList_New(0)) == NULL)
2199 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002200
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002201 hFindFile = FindFirstFile(namebuf, &FileData);
2202 if (hFindFile == INVALID_HANDLE_VALUE) {
2203 int error = GetLastError();
2204 if (error == ERROR_FILE_NOT_FOUND)
2205 return d;
2206 Py_DECREF(d);
2207 return win32_error("FindFirstFile", namebuf);
2208 }
2209 do {
2210 /* Skip over . and .. */
2211 if (strcmp(FileData.cFileName, ".") != 0 &&
2212 strcmp(FileData.cFileName, "..") != 0) {
2213 v = PyString_FromString(FileData.cFileName);
2214 if (v == NULL) {
2215 Py_DECREF(d);
2216 d = NULL;
2217 break;
2218 }
2219 if (PyList_Append(d, v) != 0) {
2220 Py_DECREF(v);
2221 Py_DECREF(d);
2222 d = NULL;
2223 break;
2224 }
2225 Py_DECREF(v);
2226 }
2227 Py_BEGIN_ALLOW_THREADS
2228 result = FindNextFile(hFindFile, &FileData);
2229 Py_END_ALLOW_THREADS
2230 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2231 it got to the end of the directory. */
2232 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2233 Py_DECREF(d);
2234 win32_error("FindNextFile", namebuf);
2235 FindClose(hFindFile);
2236 return NULL;
2237 }
2238 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002239
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002240 if (FindClose(hFindFile) == FALSE) {
2241 Py_DECREF(d);
2242 return win32_error("FindClose", namebuf);
2243 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002244
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002245 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002246
Tim Peters0bb44a42000-09-15 07:44:49 +00002247#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002248
2249#ifndef MAX_PATH
2250#define MAX_PATH CCHMAXPATH
2251#endif
2252 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002253 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002254 PyObject *d, *v;
2255 char namebuf[MAX_PATH+5];
2256 HDIR hdir = 1;
2257 ULONG srchcnt = 1;
2258 FILEFINDBUF3 ep;
2259 APIRET rc;
2260
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002261 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262 return NULL;
2263 if (len >= MAX_PATH) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002264 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002265 return NULL;
2266 }
2267 strcpy(namebuf, name);
2268 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002269 if (*pt == ALTSEP)
2270 *pt = SEP;
2271 if (namebuf[len-1] != SEP)
2272 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 strcpy(namebuf + len, "*.*");
2274
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002275 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002276 return NULL;
2277
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002278 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2279 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002280 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002281 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2282 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2283 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002284
2285 if (rc != NO_ERROR) {
2286 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002287 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002288 }
2289
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002290 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002291 do {
2292 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002293 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002294 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002295
2296 strcpy(namebuf, ep.achName);
2297
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002298 /* Leave Case of Name Alone -- In Native Form */
2299 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002300
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002301 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002302 if (v == NULL) {
2303 Py_DECREF(d);
2304 d = NULL;
2305 break;
2306 }
2307 if (PyList_Append(d, v) != 0) {
2308 Py_DECREF(v);
2309 Py_DECREF(d);
2310 d = NULL;
2311 break;
2312 }
2313 Py_DECREF(v);
2314 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2315 }
2316
2317 return d;
2318#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002319
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002320 char *name = NULL;
2321 PyObject *d, *v;
2322 DIR *dirp;
2323 struct dirent *ep;
2324 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002325
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002326 errno = 0;
2327 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2328 arg_is_unicode = 0;
2329 PyErr_Clear();
2330 }
2331 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2332 return NULL;
2333 if ((dirp = opendir(name)) == NULL) {
2334 return posix_error_with_allocated_filename(name);
2335 }
2336 if ((d = PyList_New(0)) == NULL) {
2337 closedir(dirp);
2338 PyMem_Free(name);
2339 return NULL;
2340 }
2341 for (;;) {
2342 errno = 0;
2343 Py_BEGIN_ALLOW_THREADS
2344 ep = readdir(dirp);
2345 Py_END_ALLOW_THREADS
2346 if (ep == NULL) {
2347 if (errno == 0) {
2348 break;
2349 } else {
2350 closedir(dirp);
2351 Py_DECREF(d);
2352 return posix_error_with_allocated_filename(name);
2353 }
2354 }
2355 if (ep->d_name[0] == '.' &&
2356 (NAMLEN(ep) == 1 ||
2357 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2358 continue;
2359 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2360 if (v == NULL) {
2361 Py_DECREF(d);
2362 d = NULL;
2363 break;
2364 }
Just van Rossum46c97842003-02-25 21:42:15 +00002365#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002366 if (arg_is_unicode) {
2367 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002368
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002369 w = PyUnicode_FromEncodedObject(v,
2370 Py_FileSystemDefaultEncoding,
2371 "strict");
2372 if (w != NULL) {
2373 Py_DECREF(v);
2374 v = w;
2375 }
2376 else {
2377 /* fall back to the original byte string, as
2378 discussed in patch #683592 */
2379 PyErr_Clear();
2380 }
2381 }
Just van Rossum46c97842003-02-25 21:42:15 +00002382#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002383 if (PyList_Append(d, v) != 0) {
2384 Py_DECREF(v);
2385 Py_DECREF(d);
2386 d = NULL;
2387 break;
2388 }
2389 Py_DECREF(v);
2390 }
2391 closedir(dirp);
2392 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002393
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002394 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002395
Tim Peters0bb44a42000-09-15 07:44:49 +00002396#endif /* which OS */
2397} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002398
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002399#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002400/* A helper function for abspath on win32 */
2401static PyObject *
2402posix__getfullpathname(PyObject *self, PyObject *args)
2403{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002404 /* assume encoded strings won't more than double no of chars */
2405 char inbuf[MAX_PATH*2];
2406 char *inbufp = inbuf;
2407 Py_ssize_t insize = sizeof(inbuf);
2408 char outbuf[MAX_PATH*2];
2409 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002410#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002411 if (unicode_file_names()) {
2412 PyUnicodeObject *po;
2413 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2414 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2415 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2416 Py_UNICODE *wtemp;
2417 DWORD result;
2418 PyObject *v;
2419 result = GetFullPathNameW(wpath,
2420 sizeof(woutbuf)/sizeof(woutbuf[0]),
2421 woutbuf, &wtemp);
2422 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2423 woutbufp = malloc(result * sizeof(Py_UNICODE));
2424 if (!woutbufp)
2425 return PyErr_NoMemory();
2426 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2427 }
2428 if (result)
2429 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2430 else
2431 v = win32_error_unicode("GetFullPathNameW", wpath);
2432 if (woutbufp != woutbuf)
2433 free(woutbufp);
2434 return v;
2435 }
2436 /* Drop the argument parsing error as narrow strings
2437 are also valid. */
2438 PyErr_Clear();
2439 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002440#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002441 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2442 Py_FileSystemDefaultEncoding, &inbufp,
2443 &insize))
2444 return NULL;
2445 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2446 outbuf, &temp))
2447 return win32_error("GetFullPathName", inbuf);
2448 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2449 return PyUnicode_Decode(outbuf, strlen(outbuf),
2450 Py_FileSystemDefaultEncoding, NULL);
2451 }
2452 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002453} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002454#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002456PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002457"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002458Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002459
Barry Warsaw53699e91996-12-10 23:23:01 +00002460static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002461posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002462{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002463 int res;
2464 char *path = NULL;
2465 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002466
2467#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002468 if (unicode_file_names()) {
2469 PyUnicodeObject *po;
2470 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2471 Py_BEGIN_ALLOW_THREADS
2472 /* PyUnicode_AS_UNICODE OK without thread lock as
2473 it is a simple dereference. */
2474 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2475 Py_END_ALLOW_THREADS
2476 if (!res)
2477 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2478 Py_INCREF(Py_None);
2479 return Py_None;
2480 }
2481 /* Drop the argument parsing error as narrow strings
2482 are also valid. */
2483 PyErr_Clear();
2484 }
2485 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2486 Py_FileSystemDefaultEncoding, &path, &mode))
2487 return NULL;
2488 Py_BEGIN_ALLOW_THREADS
2489 /* PyUnicode_AS_UNICODE OK without thread lock as
2490 it is a simple dereference. */
2491 res = CreateDirectoryA(path, NULL);
2492 Py_END_ALLOW_THREADS
2493 if (!res) {
2494 win32_error("mkdir", path);
2495 PyMem_Free(path);
2496 return NULL;
2497 }
2498 PyMem_Free(path);
2499 Py_INCREF(Py_None);
2500 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002501#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002502
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002503 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2504 Py_FileSystemDefaultEncoding, &path, &mode))
2505 return NULL;
2506 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002507#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002508 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002509#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002510 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002511#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002512 Py_END_ALLOW_THREADS
2513 if (res < 0)
2514 return posix_error_with_allocated_filename(path);
2515 PyMem_Free(path);
2516 Py_INCREF(Py_None);
2517 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002518#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002519}
2520
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002521
Neal Norwitz1818ed72006-03-26 00:29:48 +00002522/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2523#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002524#include <sys/resource.h>
2525#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002526
Neal Norwitz1818ed72006-03-26 00:29:48 +00002527
2528#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002529PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002530"nice(inc) -> new_priority\n\n\
2531Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002532
Barry Warsaw53699e91996-12-10 23:23:01 +00002533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002534posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002535{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002536 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002537
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002538 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2539 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002540
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002541 /* There are two flavours of 'nice': one that returns the new
2542 priority (as required by almost all standards out there) and the
2543 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2544 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002545
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002546 If we are of the nice family that returns the new priority, we
2547 need to clear errno before the call, and check if errno is filled
2548 before calling posix_error() on a returnvalue of -1, because the
2549 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002550
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002551 errno = 0;
2552 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002553#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002554 if (value == 0)
2555 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002556#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002557 if (value == -1 && errno != 0)
2558 /* either nice() or getpriority() returned an error */
2559 return posix_error();
2560 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002561}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002562#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002564PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002565"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002566Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002567
Barry Warsaw53699e91996-12-10 23:23:01 +00002568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002569posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002570{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002571#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002572 PyObject *o1, *o2;
2573 char *p1, *p2;
2574 BOOL result;
2575 if (unicode_file_names()) {
2576 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2577 goto error;
2578 if (!convert_to_unicode(&o1))
2579 goto error;
2580 if (!convert_to_unicode(&o2)) {
2581 Py_DECREF(o1);
2582 goto error;
2583 }
2584 Py_BEGIN_ALLOW_THREADS
2585 result = MoveFileW(PyUnicode_AsUnicode(o1),
2586 PyUnicode_AsUnicode(o2));
2587 Py_END_ALLOW_THREADS
2588 Py_DECREF(o1);
2589 Py_DECREF(o2);
2590 if (!result)
2591 return win32_error("rename", NULL);
2592 Py_INCREF(Py_None);
2593 return Py_None;
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002594error:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002595 PyErr_Clear();
2596 }
2597 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2598 return NULL;
2599 Py_BEGIN_ALLOW_THREADS
2600 result = MoveFileA(p1, p2);
2601 Py_END_ALLOW_THREADS
2602 if (!result)
2603 return win32_error("rename", NULL);
2604 Py_INCREF(Py_None);
2605 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002606#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002607 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002608#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002609}
2610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002612PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002613"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002614Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002615
Barry Warsaw53699e91996-12-10 23:23:01 +00002616static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002617posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002618{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002619#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002620 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002621#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002622 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002623#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002624}
2625
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002627PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002628"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Barry Warsaw53699e91996-12-10 23:23:01 +00002631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002632posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002633{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002634#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002635 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002636#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002637 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002639}
2640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002641
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002642#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002643PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002644"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002645Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002646
Barry Warsaw53699e91996-12-10 23:23:01 +00002647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002648posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002649{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002650 char *command;
2651 long sts;
2652 if (!PyArg_ParseTuple(args, "s:system", &command))
2653 return NULL;
2654 Py_BEGIN_ALLOW_THREADS
2655 sts = system(command);
2656 Py_END_ALLOW_THREADS
2657 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002658}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002659#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002662PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002663"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002664Set the current numeric umask and return the previous umask.");
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_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002668{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002669 int i;
2670 if (!PyArg_ParseTuple(args, "i:umask", &i))
2671 return NULL;
2672 i = (int)umask(i);
2673 if (i < 0)
2674 return posix_error();
2675 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002676}
2677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002680"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002682
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002683PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002684"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002685Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002686
Barry Warsaw53699e91996-12-10 23:23:01 +00002687static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002688posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002689{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002690#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002691 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002692#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002693 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002694#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695}
2696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Guido van Rossumb6775db1994-08-01 11:34:53 +00002698#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002699PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002700"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002701Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002702
Barry Warsaw53699e91996-12-10 23:23:01 +00002703static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002704posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002705{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002706 struct utsname u;
2707 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002708
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002709 Py_BEGIN_ALLOW_THREADS
2710 res = uname(&u);
2711 Py_END_ALLOW_THREADS
2712 if (res < 0)
2713 return posix_error();
2714 return Py_BuildValue("(sssss)",
2715 u.sysname,
2716 u.nodename,
2717 u.release,
2718 u.version,
2719 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002720}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002721#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002722
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002723static int
2724extract_time(PyObject *t, long* sec, long* usec)
2725{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002726 long intval;
2727 if (PyFloat_Check(t)) {
2728 double tval = PyFloat_AsDouble(t);
2729 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2730 if (!intobj)
2731 return -1;
2732 intval = PyInt_AsLong(intobj);
2733 Py_DECREF(intobj);
2734 if (intval == -1 && PyErr_Occurred())
2735 return -1;
2736 *sec = intval;
2737 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2738 if (*usec < 0)
2739 /* If rounding gave us a negative number,
2740 truncate. */
2741 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002742 return 0;
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002743 }
2744 intval = PyInt_AsLong(t);
2745 if (intval == -1 && PyErr_Occurred())
2746 return -1;
2747 *sec = intval;
2748 *usec = 0;
2749 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002750}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002752PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002753"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002754utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002755Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002756second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002757
Barry Warsaw53699e91996-12-10 23:23:01 +00002758static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002759posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002760{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002761#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002762 PyObject *arg;
2763 PyUnicodeObject *obwpath;
2764 wchar_t *wpath = NULL;
2765 char *apath = NULL;
2766 HANDLE hFile;
2767 long atimesec, mtimesec, ausec, musec;
2768 FILETIME atime, mtime;
2769 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002770
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002771 if (unicode_file_names()) {
2772 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2773 wpath = PyUnicode_AS_UNICODE(obwpath);
2774 Py_BEGIN_ALLOW_THREADS
2775 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2776 NULL, OPEN_EXISTING,
2777 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2778 Py_END_ALLOW_THREADS
2779 if (hFile == INVALID_HANDLE_VALUE)
2780 return win32_error_unicode("utime", wpath);
2781 } else
2782 /* Drop the argument parsing error as narrow strings
2783 are also valid. */
2784 PyErr_Clear();
2785 }
2786 if (!wpath) {
2787 if (!PyArg_ParseTuple(args, "etO:utime",
2788 Py_FileSystemDefaultEncoding, &apath, &arg))
2789 return NULL;
2790 Py_BEGIN_ALLOW_THREADS
2791 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2792 NULL, OPEN_EXISTING,
2793 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2794 Py_END_ALLOW_THREADS
2795 if (hFile == INVALID_HANDLE_VALUE) {
2796 win32_error("utime", apath);
2797 PyMem_Free(apath);
2798 return NULL;
2799 }
2800 PyMem_Free(apath);
2801 }
2802
2803 if (arg == Py_None) {
2804 SYSTEMTIME now;
2805 GetSystemTime(&now);
2806 if (!SystemTimeToFileTime(&now, &mtime) ||
2807 !SystemTimeToFileTime(&now, &atime)) {
2808 win32_error("utime", NULL);
2809 goto done;
2810 }
2811 }
2812 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2813 PyErr_SetString(PyExc_TypeError,
2814 "utime() arg 2 must be a tuple (atime, mtime)");
2815 goto done;
2816 }
2817 else {
2818 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2819 &atimesec, &ausec) == -1)
2820 goto done;
2821 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2822 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2823 &mtimesec, &musec) == -1)
2824 goto done;
2825 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2826 }
2827 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2828 /* Avoid putting the file name into the error here,
2829 as that may confuse the user into believing that
2830 something is wrong with the file, when it also
2831 could be the time stamp that gives a problem. */
2832 win32_error("utime", NULL);
2833 }
2834 Py_INCREF(Py_None);
2835 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002836done:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002837 CloseHandle(hFile);
2838 return result;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002839#else /* Py_WIN_WIDE_FILENAMES */
2840
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002841 char *path = NULL;
2842 long atime, mtime, ausec, musec;
2843 int res;
2844 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002845
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002846#if defined(HAVE_UTIMES)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002847 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002848#define ATIME buf[0].tv_sec
2849#define MTIME buf[1].tv_sec
2850#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002851/* XXX should define struct utimbuf instead, above */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002852 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002853#define ATIME buf.actime
2854#define MTIME buf.modtime
2855#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002856#else /* HAVE_UTIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002857 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002858#define ATIME buf[0]
2859#define MTIME buf[1]
2860#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002861#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002862
Mark Hammond817c9292003-12-03 01:22:38 +00002863
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002864 if (!PyArg_ParseTuple(args, "etO:utime",
2865 Py_FileSystemDefaultEncoding, &path, &arg))
2866 return NULL;
2867 if (arg == Py_None) {
2868 /* optional time values not given */
2869 Py_BEGIN_ALLOW_THREADS
2870 res = utime(path, NULL);
2871 Py_END_ALLOW_THREADS
2872 }
2873 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2874 PyErr_SetString(PyExc_TypeError,
2875 "utime() arg 2 must be a tuple (atime, mtime)");
2876 PyMem_Free(path);
2877 return NULL;
2878 }
2879 else {
2880 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2881 &atime, &ausec) == -1) {
2882 PyMem_Free(path);
2883 return NULL;
2884 }
2885 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2886 &mtime, &musec) == -1) {
2887 PyMem_Free(path);
2888 return NULL;
2889 }
2890 ATIME = atime;
2891 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002892#ifdef HAVE_UTIMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002893 buf[0].tv_usec = ausec;
2894 buf[1].tv_usec = musec;
2895 Py_BEGIN_ALLOW_THREADS
2896 res = utimes(path, buf);
2897 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002898#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002899 Py_BEGIN_ALLOW_THREADS
2900 res = utime(path, UTIME_ARG);
2901 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002902#endif /* HAVE_UTIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002903 }
2904 if (res < 0) {
2905 return posix_error_with_allocated_filename(path);
2906 }
2907 PyMem_Free(path);
2908 Py_INCREF(Py_None);
2909 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002910#undef UTIME_ARG
2911#undef ATIME
2912#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002913#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002914}
2915
Guido van Rossum85e3b011991-06-03 12:42:10 +00002916
Guido van Rossum3b066191991-06-04 19:40:25 +00002917/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002919PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002920"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002921Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002922
Barry Warsaw53699e91996-12-10 23:23:01 +00002923static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002924posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002925{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002926 int sts;
2927 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2928 return NULL;
2929 _exit(sts);
2930 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002931}
2932
Martin v. Löwis114619e2002-10-07 06:44:21 +00002933#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2934static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002935free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002936{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002937 Py_ssize_t i;
2938 for (i = 0; i < count; i++)
2939 PyMem_Free(array[i]);
2940 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002941}
2942#endif
2943
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002944
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002945#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002946PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002947"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948Execute an executable path with arguments, replacing current process.\n\
2949\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002950 path: path of executable file\n\
2951 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002952
Barry Warsaw53699e91996-12-10 23:23:01 +00002953static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002954posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002955{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002956 char *path;
2957 PyObject *argv;
2958 char **argvlist;
2959 Py_ssize_t i, argc;
2960 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002961
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002962 /* execv has two arguments: (path, argv), where
2963 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002964
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002965 if (!PyArg_ParseTuple(args, "etO:execv",
2966 Py_FileSystemDefaultEncoding,
2967 &path, &argv))
2968 return NULL;
2969 if (PyList_Check(argv)) {
2970 argc = PyList_Size(argv);
2971 getitem = PyList_GetItem;
2972 }
2973 else if (PyTuple_Check(argv)) {
2974 argc = PyTuple_Size(argv);
2975 getitem = PyTuple_GetItem;
2976 }
2977 else {
2978 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
2979 PyMem_Free(path);
2980 return NULL;
2981 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002982
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002983 argvlist = PyMem_NEW(char *, argc+1);
2984 if (argvlist == NULL) {
2985 PyMem_Free(path);
2986 return PyErr_NoMemory();
2987 }
2988 for (i = 0; i < argc; i++) {
2989 if (!PyArg_Parse((*getitem)(argv, i), "et",
2990 Py_FileSystemDefaultEncoding,
2991 &argvlist[i])) {
2992 free_string_array(argvlist, i);
2993 PyErr_SetString(PyExc_TypeError,
2994 "execv() arg 2 must contain only strings");
2995 PyMem_Free(path);
2996 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002997
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002998 }
2999 }
3000 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003001
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003002 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003003
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003004 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003005
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003006 free_string_array(argvlist, argc);
3007 PyMem_Free(path);
3008 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003009}
3010
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003011
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003013"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003014Execute a path with arguments and environment, replacing current process.\n\
3015\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003016 path: path of executable file\n\
3017 args: tuple or list of arguments\n\
3018 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003019
Barry Warsaw53699e91996-12-10 23:23:01 +00003020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003021posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003022{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003023 char *path;
3024 PyObject *argv, *env;
3025 char **argvlist;
3026 char **envlist;
3027 PyObject *key, *val, *keys=NULL, *vals=NULL;
3028 Py_ssize_t i, pos, argc, envc;
3029 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3030 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003031
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003032 /* execve has three arguments: (path, argv, env), where
3033 argv is a list or tuple of strings and env is a dictionary
3034 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003035
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003036 if (!PyArg_ParseTuple(args, "etOO:execve",
3037 Py_FileSystemDefaultEncoding,
3038 &path, &argv, &env))
3039 return NULL;
3040 if (PyList_Check(argv)) {
3041 argc = PyList_Size(argv);
3042 getitem = PyList_GetItem;
3043 }
3044 else if (PyTuple_Check(argv)) {
3045 argc = PyTuple_Size(argv);
3046 getitem = PyTuple_GetItem;
3047 }
3048 else {
3049 PyErr_SetString(PyExc_TypeError,
3050 "execve() arg 2 must be a tuple or list");
3051 goto fail_0;
3052 }
3053 if (!PyMapping_Check(env)) {
3054 PyErr_SetString(PyExc_TypeError,
3055 "execve() arg 3 must be a mapping object");
3056 goto fail_0;
3057 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003058
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003059 argvlist = PyMem_NEW(char *, argc+1);
3060 if (argvlist == NULL) {
3061 PyErr_NoMemory();
3062 goto fail_0;
3063 }
3064 for (i = 0; i < argc; i++) {
3065 if (!PyArg_Parse((*getitem)(argv, i),
3066 "et;execve() arg 2 must contain only strings",
3067 Py_FileSystemDefaultEncoding,
3068 &argvlist[i]))
3069 {
3070 lastarg = i;
3071 goto fail_1;
3072 }
3073 }
3074 lastarg = argc;
3075 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003076
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003077 i = PyMapping_Size(env);
3078 if (i < 0)
3079 goto fail_1;
3080 envlist = PyMem_NEW(char *, i + 1);
3081 if (envlist == NULL) {
3082 PyErr_NoMemory();
3083 goto fail_1;
3084 }
3085 envc = 0;
3086 keys = PyMapping_Keys(env);
3087 vals = PyMapping_Values(env);
3088 if (!keys || !vals)
3089 goto fail_2;
3090 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3091 PyErr_SetString(PyExc_TypeError,
3092 "execve(): env.keys() or env.values() is not a list");
3093 goto fail_2;
3094 }
Tim Peters5aa91602002-01-30 05:46:57 +00003095
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003096 for (pos = 0; pos < i; pos++) {
3097 char *p, *k, *v;
3098 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003099
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003100 key = PyList_GetItem(keys, pos);
3101 val = PyList_GetItem(vals, pos);
3102 if (!key || !val)
3103 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003104
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003105 if (!PyArg_Parse(
3106 key,
3107 "s;execve() arg 3 contains a non-string key",
3108 &k) ||
3109 !PyArg_Parse(
3110 val,
3111 "s;execve() arg 3 contains a non-string value",
3112 &v))
3113 {
3114 goto fail_2;
3115 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003116
3117#if defined(PYOS_OS2)
3118 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3119 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3120#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003121 len = PyString_Size(key) + PyString_Size(val) + 2;
3122 p = PyMem_NEW(char, len);
3123 if (p == NULL) {
3124 PyErr_NoMemory();
3125 goto fail_2;
3126 }
3127 PyOS_snprintf(p, len, "%s=%s", k, v);
3128 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003129#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003130 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003131#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003132 }
3133 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003134
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003135 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003136
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003137 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003138
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003139 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003140
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003141 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003142 while (--envc >= 0)
3143 PyMem_DEL(envlist[envc]);
3144 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003145 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003146 free_string_array(argvlist, lastarg);
3147 Py_XDECREF(vals);
3148 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003150 PyMem_Free(path);
3151 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003152}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003153#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003154
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003155
Guido van Rossuma1065681999-01-25 23:20:23 +00003156#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003157PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003158"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003159Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003160\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003161 mode: mode of process creation\n\
3162 path: path of executable file\n\
3163 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003164
3165static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003166posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003167{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003168 char *path;
3169 PyObject *argv;
3170 char **argvlist;
3171 int mode, i;
3172 Py_ssize_t argc;
3173 Py_intptr_t spawnval;
3174 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003175
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003176 /* spawnv has three arguments: (mode, path, argv), where
3177 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003178
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003179 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3180 Py_FileSystemDefaultEncoding,
3181 &path, &argv))
3182 return NULL;
3183 if (PyList_Check(argv)) {
3184 argc = PyList_Size(argv);
3185 getitem = PyList_GetItem;
3186 }
3187 else if (PyTuple_Check(argv)) {
3188 argc = PyTuple_Size(argv);
3189 getitem = PyTuple_GetItem;
3190 }
3191 else {
3192 PyErr_SetString(PyExc_TypeError,
3193 "spawnv() arg 2 must be a tuple or list");
3194 PyMem_Free(path);
3195 return NULL;
3196 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003197
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003198 argvlist = PyMem_NEW(char *, argc+1);
3199 if (argvlist == NULL) {
3200 PyMem_Free(path);
3201 return PyErr_NoMemory();
3202 }
3203 for (i = 0; i < argc; i++) {
3204 if (!PyArg_Parse((*getitem)(argv, i), "et",
3205 Py_FileSystemDefaultEncoding,
3206 &argvlist[i])) {
3207 free_string_array(argvlist, i);
3208 PyErr_SetString(
3209 PyExc_TypeError,
3210 "spawnv() arg 2 must contain only strings");
3211 PyMem_Free(path);
3212 return NULL;
3213 }
3214 }
3215 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003216
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003217#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003218 Py_BEGIN_ALLOW_THREADS
3219 spawnval = spawnv(mode, path, argvlist);
3220 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003221#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003222 if (mode == _OLD_P_OVERLAY)
3223 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003224
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003225 Py_BEGIN_ALLOW_THREADS
3226 spawnval = _spawnv(mode, path, argvlist);
3227 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003228#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003229
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003230 free_string_array(argvlist, argc);
3231 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003232
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003233 if (spawnval == -1)
3234 return posix_error();
3235 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003236#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003237 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003238#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003239 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003240#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003241}
3242
3243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003244PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003245"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003246Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003247\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003248 mode: mode of process creation\n\
3249 path: path of executable file\n\
3250 args: tuple or list of arguments\n\
3251 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003252
3253static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003254posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003255{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003256 char *path;
3257 PyObject *argv, *env;
3258 char **argvlist;
3259 char **envlist;
3260 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3261 int mode, pos, envc;
3262 Py_ssize_t argc, i;
3263 Py_intptr_t spawnval;
3264 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3265 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003266
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003267 /* spawnve has four arguments: (mode, path, argv, env), where
3268 argv is a list or tuple of strings and env is a dictionary
3269 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003270
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003271 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3272 Py_FileSystemDefaultEncoding,
3273 &path, &argv, &env))
3274 return NULL;
3275 if (PyList_Check(argv)) {
3276 argc = PyList_Size(argv);
3277 getitem = PyList_GetItem;
3278 }
3279 else if (PyTuple_Check(argv)) {
3280 argc = PyTuple_Size(argv);
3281 getitem = PyTuple_GetItem;
3282 }
3283 else {
3284 PyErr_SetString(PyExc_TypeError,
3285 "spawnve() arg 2 must be a tuple or list");
3286 goto fail_0;
3287 }
3288 if (!PyMapping_Check(env)) {
3289 PyErr_SetString(PyExc_TypeError,
3290 "spawnve() arg 3 must be a mapping object");
3291 goto fail_0;
3292 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003293
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003294 argvlist = PyMem_NEW(char *, argc+1);
3295 if (argvlist == NULL) {
3296 PyErr_NoMemory();
3297 goto fail_0;
3298 }
3299 for (i = 0; i < argc; i++) {
3300 if (!PyArg_Parse((*getitem)(argv, i),
3301 "et;spawnve() arg 2 must contain only strings",
3302 Py_FileSystemDefaultEncoding,
3303 &argvlist[i]))
3304 {
3305 lastarg = i;
3306 goto fail_1;
3307 }
3308 }
3309 lastarg = argc;
3310 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003311
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003312 i = PyMapping_Size(env);
3313 if (i < 0)
3314 goto fail_1;
3315 envlist = PyMem_NEW(char *, i + 1);
3316 if (envlist == NULL) {
3317 PyErr_NoMemory();
3318 goto fail_1;
3319 }
3320 envc = 0;
3321 keys = PyMapping_Keys(env);
3322 vals = PyMapping_Values(env);
3323 if (!keys || !vals)
3324 goto fail_2;
3325 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3326 PyErr_SetString(PyExc_TypeError,
3327 "spawnve(): env.keys() or env.values() is not a list");
3328 goto fail_2;
3329 }
Tim Peters5aa91602002-01-30 05:46:57 +00003330
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003331 for (pos = 0; pos < i; pos++) {
3332 char *p, *k, *v;
3333 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003334
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003335 key = PyList_GetItem(keys, pos);
3336 val = PyList_GetItem(vals, pos);
3337 if (!key || !val)
3338 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003339
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003340 if (!PyArg_Parse(
3341 key,
3342 "s;spawnve() arg 3 contains a non-string key",
3343 &k) ||
3344 !PyArg_Parse(
3345 val,
3346 "s;spawnve() arg 3 contains a non-string value",
3347 &v))
3348 {
3349 goto fail_2;
3350 }
3351 len = PyString_Size(key) + PyString_Size(val) + 2;
3352 p = PyMem_NEW(char, len);
3353 if (p == NULL) {
3354 PyErr_NoMemory();
3355 goto fail_2;
3356 }
3357 PyOS_snprintf(p, len, "%s=%s", k, v);
3358 envlist[envc++] = p;
3359 }
3360 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003361
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003362#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003363 Py_BEGIN_ALLOW_THREADS
3364 spawnval = spawnve(mode, path, argvlist, envlist);
3365 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003366#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003367 if (mode == _OLD_P_OVERLAY)
3368 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003369
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003370 Py_BEGIN_ALLOW_THREADS
3371 spawnval = _spawnve(mode, path, argvlist, envlist);
3372 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003373#endif
Tim Peters25059d32001-12-07 20:35:43 +00003374
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003375 if (spawnval == -1)
3376 (void) posix_error();
3377 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003378#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003379 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003380#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003381 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003382#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003383
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003384 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003385 while (--envc >= 0)
3386 PyMem_DEL(envlist[envc]);
3387 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003388 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003389 free_string_array(argvlist, lastarg);
3390 Py_XDECREF(vals);
3391 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003392 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003393 PyMem_Free(path);
3394 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003395}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003396
3397/* OS/2 supports spawnvp & spawnvpe natively */
3398#if defined(PYOS_OS2)
3399PyDoc_STRVAR(posix_spawnvp__doc__,
3400"spawnvp(mode, file, args)\n\n\
3401Execute the program 'file' in a new process, using the environment\n\
3402search path to find the file.\n\
3403\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003404 mode: mode of process creation\n\
3405 file: executable file name\n\
3406 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003407
3408static PyObject *
3409posix_spawnvp(PyObject *self, PyObject *args)
3410{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003411 char *path;
3412 PyObject *argv;
3413 char **argvlist;
3414 int mode, i, argc;
3415 Py_intptr_t spawnval;
3416 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003417
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003418 /* spawnvp has three arguments: (mode, path, argv), where
3419 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003420
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003421 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3422 Py_FileSystemDefaultEncoding,
3423 &path, &argv))
3424 return NULL;
3425 if (PyList_Check(argv)) {
3426 argc = PyList_Size(argv);
3427 getitem = PyList_GetItem;
3428 }
3429 else if (PyTuple_Check(argv)) {
3430 argc = PyTuple_Size(argv);
3431 getitem = PyTuple_GetItem;
3432 }
3433 else {
3434 PyErr_SetString(PyExc_TypeError,
3435 "spawnvp() arg 2 must be a tuple or list");
3436 PyMem_Free(path);
3437 return NULL;
3438 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003439
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003440 argvlist = PyMem_NEW(char *, argc+1);
3441 if (argvlist == NULL) {
3442 PyMem_Free(path);
3443 return PyErr_NoMemory();
3444 }
3445 for (i = 0; i < argc; i++) {
3446 if (!PyArg_Parse((*getitem)(argv, i), "et",
3447 Py_FileSystemDefaultEncoding,
3448 &argvlist[i])) {
3449 free_string_array(argvlist, i);
3450 PyErr_SetString(
3451 PyExc_TypeError,
3452 "spawnvp() arg 2 must contain only strings");
3453 PyMem_Free(path);
3454 return NULL;
3455 }
3456 }
3457 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003458
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003459 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003460#if defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003461 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003462#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003463 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003464#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003465 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003466
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003467 free_string_array(argvlist, argc);
3468 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003469
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003470 if (spawnval == -1)
3471 return posix_error();
3472 else
3473 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003474}
3475
3476
3477PyDoc_STRVAR(posix_spawnvpe__doc__,
3478"spawnvpe(mode, file, args, env)\n\n\
3479Execute the program 'file' in a new process, using the environment\n\
3480search path to find the file.\n\
3481\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003482 mode: mode of process creation\n\
3483 file: executable file name\n\
3484 args: tuple or list of arguments\n\
3485 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003486
3487static PyObject *
3488posix_spawnvpe(PyObject *self, PyObject *args)
3489{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003490 char *path;
3491 PyObject *argv, *env;
3492 char **argvlist;
3493 char **envlist;
3494 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3495 int mode, i, pos, argc, envc;
3496 Py_intptr_t spawnval;
3497 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3498 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003499
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003500 /* spawnvpe has four arguments: (mode, path, argv, env), where
3501 argv is a list or tuple of strings and env is a dictionary
3502 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003503
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003504 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3505 Py_FileSystemDefaultEncoding,
3506 &path, &argv, &env))
3507 return NULL;
3508 if (PyList_Check(argv)) {
3509 argc = PyList_Size(argv);
3510 getitem = PyList_GetItem;
3511 }
3512 else if (PyTuple_Check(argv)) {
3513 argc = PyTuple_Size(argv);
3514 getitem = PyTuple_GetItem;
3515 }
3516 else {
3517 PyErr_SetString(PyExc_TypeError,
3518 "spawnvpe() arg 2 must be a tuple or list");
3519 goto fail_0;
3520 }
3521 if (!PyMapping_Check(env)) {
3522 PyErr_SetString(PyExc_TypeError,
3523 "spawnvpe() arg 3 must be a mapping object");
3524 goto fail_0;
3525 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003526
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003527 argvlist = PyMem_NEW(char *, argc+1);
3528 if (argvlist == NULL) {
3529 PyErr_NoMemory();
3530 goto fail_0;
3531 }
3532 for (i = 0; i < argc; i++) {
3533 if (!PyArg_Parse((*getitem)(argv, i),
3534 "et;spawnvpe() arg 2 must contain only strings",
3535 Py_FileSystemDefaultEncoding,
3536 &argvlist[i]))
3537 {
3538 lastarg = i;
3539 goto fail_1;
3540 }
3541 }
3542 lastarg = argc;
3543 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003544
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003545 i = PyMapping_Size(env);
3546 if (i < 0)
3547 goto fail_1;
3548 envlist = PyMem_NEW(char *, i + 1);
3549 if (envlist == NULL) {
3550 PyErr_NoMemory();
3551 goto fail_1;
3552 }
3553 envc = 0;
3554 keys = PyMapping_Keys(env);
3555 vals = PyMapping_Values(env);
3556 if (!keys || !vals)
3557 goto fail_2;
3558 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3559 PyErr_SetString(PyExc_TypeError,
3560 "spawnvpe(): env.keys() or env.values() is not a list");
3561 goto fail_2;
3562 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003563
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003564 for (pos = 0; pos < i; pos++) {
3565 char *p, *k, *v;
3566 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003567
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003568 key = PyList_GetItem(keys, pos);
3569 val = PyList_GetItem(vals, pos);
3570 if (!key || !val)
3571 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003572
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003573 if (!PyArg_Parse(
3574 key,
3575 "s;spawnvpe() arg 3 contains a non-string key",
3576 &k) ||
3577 !PyArg_Parse(
3578 val,
3579 "s;spawnvpe() arg 3 contains a non-string value",
3580 &v))
3581 {
3582 goto fail_2;
3583 }
3584 len = PyString_Size(key) + PyString_Size(val) + 2;
3585 p = PyMem_NEW(char, len);
3586 if (p == NULL) {
3587 PyErr_NoMemory();
3588 goto fail_2;
3589 }
3590 PyOS_snprintf(p, len, "%s=%s", k, v);
3591 envlist[envc++] = p;
3592 }
3593 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003594
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003595 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003596#if defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003597 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003598#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003599 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003600#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003601 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003602
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003603 if (spawnval == -1)
3604 (void) posix_error();
3605 else
3606 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003607
3608 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003609 while (--envc >= 0)
3610 PyMem_DEL(envlist[envc]);
3611 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003612 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003613 free_string_array(argvlist, lastarg);
3614 Py_XDECREF(vals);
3615 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003616 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003617 PyMem_Free(path);
3618 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003619}
3620#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003621#endif /* HAVE_SPAWNV */
3622
3623
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003624#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003625PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003626"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003627Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3628\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003629Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003630
3631static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003632posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003633{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003634 pid_t pid;
3635 int result = 0;
3636 _PyImport_AcquireLock();
3637 pid = fork1();
3638 if (pid == 0) {
3639 /* child: this clobbers and resets the import lock. */
3640 PyOS_AfterFork();
3641 } else {
3642 /* parent: release the import lock. */
3643 result = _PyImport_ReleaseLock();
3644 }
3645 if (pid == -1)
3646 return posix_error();
3647 if (result < 0) {
3648 /* Don't clobber the OSError if the fork failed. */
3649 PyErr_SetString(PyExc_RuntimeError,
3650 "not holding the import lock");
3651 return NULL;
3652 }
3653 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003654}
3655#endif
3656
3657
Guido van Rossumad0ee831995-03-01 10:34:45 +00003658#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003659PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003660"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003661Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003662Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003663
Barry Warsaw53699e91996-12-10 23:23:01 +00003664static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003665posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003666{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003667 pid_t pid;
3668 int result = 0;
3669 _PyImport_AcquireLock();
3670 pid = fork();
3671 if (pid == 0) {
3672 /* child: this clobbers and resets the import lock. */
3673 PyOS_AfterFork();
3674 } else {
3675 /* parent: release the import lock. */
3676 result = _PyImport_ReleaseLock();
3677 }
3678 if (pid == -1)
3679 return posix_error();
3680 if (result < 0) {
3681 /* Don't clobber the OSError if the fork failed. */
3682 PyErr_SetString(PyExc_RuntimeError,
3683 "not holding the import lock");
3684 return NULL;
3685 }
3686 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003687}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003688#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003689
Neal Norwitzb59798b2003-03-21 01:43:31 +00003690/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003691/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3692#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003693#define DEV_PTY_FILE "/dev/ptc"
3694#define HAVE_DEV_PTMX
3695#else
3696#define DEV_PTY_FILE "/dev/ptmx"
3697#endif
3698
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003699#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003700#ifdef HAVE_PTY_H
3701#include <pty.h>
3702#else
3703#ifdef HAVE_LIBUTIL_H
3704#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003705#endif /* HAVE_LIBUTIL_H */
3706#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003707#ifdef HAVE_STROPTS_H
3708#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003709#endif
3710#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003711
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003712#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003713PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003714"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003715Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003716
3717static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003718posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003719{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003720 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003721#ifndef HAVE_OPENPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003722 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003723#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003724#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003725 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003726#ifdef sun
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003727 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003728#endif
3729#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003730
Thomas Wouters70c21a12000-07-14 14:28:33 +00003731#ifdef HAVE_OPENPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003732 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3733 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003734#elif defined(HAVE__GETPTY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003735 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3736 if (slave_name == NULL)
3737 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003738
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003739 slave_fd = open(slave_name, O_RDWR);
3740 if (slave_fd < 0)
3741 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003742#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003743 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3744 if (master_fd < 0)
3745 return posix_error();
3746 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3747 /* change permission of slave */
3748 if (grantpt(master_fd) < 0) {
3749 PyOS_setsig(SIGCHLD, sig_saved);
3750 return posix_error();
3751 }
3752 /* unlock slave */
3753 if (unlockpt(master_fd) < 0) {
3754 PyOS_setsig(SIGCHLD, sig_saved);
3755 return posix_error();
3756 }
3757 PyOS_setsig(SIGCHLD, sig_saved);
3758 slave_name = ptsname(master_fd); /* get name of slave */
3759 if (slave_name == NULL)
3760 return posix_error();
3761 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3762 if (slave_fd < 0)
3763 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003764#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003765 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3766 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003767#ifndef __hpux
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003768 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003769#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003770#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003771#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003772
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003773 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003774
Fred Drake8cef4cf2000-06-28 16:40:38 +00003775}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003776#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003777
3778#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003780"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003781Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3782Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003783To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003784
3785static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003786posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003787{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003788 int master_fd = -1, result = 0;
3789 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003790
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003791 _PyImport_AcquireLock();
3792 pid = forkpty(&master_fd, NULL, NULL, NULL);
3793 if (pid == 0) {
3794 /* child: this clobbers and resets the import lock. */
3795 PyOS_AfterFork();
3796 } else {
3797 /* parent: release the import lock. */
3798 result = _PyImport_ReleaseLock();
3799 }
3800 if (pid == -1)
3801 return posix_error();
3802 if (result < 0) {
3803 /* Don't clobber the OSError if the fork failed. */
3804 PyErr_SetString(PyExc_RuntimeError,
3805 "not holding the import lock");
3806 return NULL;
3807 }
3808 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003809}
3810#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003811
Guido van Rossumad0ee831995-03-01 10:34:45 +00003812#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003813PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003814"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003815Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003816
Barry Warsaw53699e91996-12-10 23:23:01 +00003817static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003818posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003819{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003820 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003821}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003822#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003824
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003827"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003829
Barry Warsaw53699e91996-12-10 23:23:01 +00003830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003831posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003832{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003833 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003834}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003835#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003836
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003837
Guido van Rossumad0ee831995-03-01 10:34:45 +00003838#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003839PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003840"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003841Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003842
Barry Warsaw53699e91996-12-10 23:23:01 +00003843static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003844posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003845{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003846 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003847}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003848#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003849
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003851PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003852"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003853Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003854
Barry Warsaw53699e91996-12-10 23:23:01 +00003855static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003856posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003857{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003858 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003859}
3860
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003861
Fred Drakec9680921999-12-13 16:37:25 +00003862#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003863PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003864"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003865Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003866
3867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003868posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003869{
3870 PyObject *result = NULL;
3871
Fred Drakec9680921999-12-13 16:37:25 +00003872#ifdef NGROUPS_MAX
3873#define MAX_GROUPS NGROUPS_MAX
3874#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003875 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003876#define MAX_GROUPS 64
3877#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003878 gid_t grouplist[MAX_GROUPS];
3879 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003880
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003881 n = getgroups(MAX_GROUPS, grouplist);
3882 if (n < 0)
3883 posix_error();
3884 else {
3885 result = PyList_New(n);
3886 if (result != NULL) {
3887 int i;
3888 for (i = 0; i < n; ++i) {
3889 PyObject *o = PyInt_FromLong((long)grouplist[i]);
3890 if (o == NULL) {
3891 Py_DECREF(result);
3892 result = NULL;
3893 break;
Fred Drakec9680921999-12-13 16:37:25 +00003894 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003895 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003896 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003897 }
3898 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003899
Fred Drakec9680921999-12-13 16:37:25 +00003900 return result;
3901}
3902#endif
3903
Martin v. Löwis606edc12002-06-13 21:09:11 +00003904#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003905PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003906"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003907Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003908
3909static PyObject *
3910posix_getpgid(PyObject *self, PyObject *args)
3911{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003912 pid_t pid, pgid;
3913 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
3914 return NULL;
3915 pgid = getpgid(pid);
3916 if (pgid < 0)
3917 return posix_error();
3918 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003919}
3920#endif /* HAVE_GETPGID */
3921
3922
Guido van Rossumb6775db1994-08-01 11:34:53 +00003923#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003924PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003925"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003926Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003927
Barry Warsaw53699e91996-12-10 23:23:01 +00003928static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003929posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003930{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003931#ifdef GETPGRP_HAVE_ARG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003932 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003933#else /* GETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003934 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003935#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003936}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003937#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003939
Guido van Rossumb6775db1994-08-01 11:34:53 +00003940#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003941PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003942"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003943Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003944
Barry Warsaw53699e91996-12-10 23:23:01 +00003945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003946posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003947{
Guido van Rossum64933891994-10-20 21:56:42 +00003948#ifdef SETPGRP_HAVE_ARG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003949 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003950#else /* SETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003951 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003952#endif /* SETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003953 return posix_error();
3954 Py_INCREF(Py_None);
3955 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003956}
3957
Guido van Rossumb6775db1994-08-01 11:34:53 +00003958#endif /* HAVE_SETPGRP */
3959
Guido van Rossumad0ee831995-03-01 10:34:45 +00003960#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003961PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003962"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003963Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003964
Barry Warsaw53699e91996-12-10 23:23:01 +00003965static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003966posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003967{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003968 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003969}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003970#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003971
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003972
Fred Drake12c6e2d1999-12-14 21:25:03 +00003973#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003974PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003975"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003977
3978static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003979posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003980{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003981 PyObject *result = NULL;
3982 char *name;
3983 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003984
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003985 errno = 0;
3986 name = getlogin();
3987 if (name == NULL) {
3988 if (errno)
3989 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00003990 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003991 PyErr_SetString(PyExc_OSError,
3992 "unable to determine login name");
3993 }
3994 else
3995 result = PyString_FromString(name);
3996 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003997
Fred Drake12c6e2d1999-12-14 21:25:03 +00003998 return result;
3999}
4000#endif
4001
Guido van Rossumad0ee831995-03-01 10:34:45 +00004002#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004004"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004006
Barry Warsaw53699e91996-12-10 23:23:01 +00004007static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004008posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004009{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004010 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004011}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004012#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004014
Guido van Rossumad0ee831995-03-01 10:34:45 +00004015#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004017"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004018Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004019
Barry Warsaw53699e91996-12-10 23:23:01 +00004020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004021posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004022{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004023 pid_t pid;
4024 int sig;
4025 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4026 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004027#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004028 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4029 APIRET rc;
4030 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004031 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004032
4033 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4034 APIRET rc;
4035 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004036 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004037
4038 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004039 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004040#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004041 if (kill(pid, sig) == -1)
4042 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004043#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004044 Py_INCREF(Py_None);
4045 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004046}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004047#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004048
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004049#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004050PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004051"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004052Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004053
4054static PyObject *
4055posix_killpg(PyObject *self, PyObject *args)
4056{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004057 int sig;
4058 pid_t pgid;
4059 /* XXX some man pages make the `pgid` parameter an int, others
4060 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4061 take the same type. Moreover, pid_t is always at least as wide as
4062 int (else compilation of this module fails), which is safe. */
4063 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4064 return NULL;
4065 if (killpg(pgid, sig) == -1)
4066 return posix_error();
4067 Py_INCREF(Py_None);
4068 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004069}
4070#endif
4071
Guido van Rossumc0125471996-06-28 18:55:32 +00004072#ifdef HAVE_PLOCK
4073
4074#ifdef HAVE_SYS_LOCK_H
4075#include <sys/lock.h>
4076#endif
4077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004078PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004079"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004080Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004081
Barry Warsaw53699e91996-12-10 23:23:01 +00004082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004083posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004084{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004085 int op;
4086 if (!PyArg_ParseTuple(args, "i:plock", &op))
4087 return NULL;
4088 if (plock(op) == -1)
4089 return posix_error();
4090 Py_INCREF(Py_None);
4091 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004092}
4093#endif
4094
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004095
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004096#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004097PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004098"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004099Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004100
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004101#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004102#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004103static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004104async_system(const char *command)
4105{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004106 char errormsg[256], args[1024];
4107 RESULTCODES rcodes;
4108 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004109
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004110 char *shell = getenv("COMSPEC");
4111 if (!shell)
4112 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004113
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004114 /* avoid overflowing the argument buffer */
4115 if (strlen(shell) + 3 + strlen(command) >= 1024)
4116 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004117
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004118 args[0] = '\0';
4119 strcat(args, shell);
4120 strcat(args, "/c ");
4121 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004122
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004123 /* execute asynchronously, inheriting the environment */
4124 rc = DosExecPgm(errormsg,
4125 sizeof(errormsg),
4126 EXEC_ASYNC,
4127 args,
4128 NULL,
4129 &rcodes,
4130 shell);
4131 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004132}
4133
Guido van Rossumd48f2521997-12-05 22:19:34 +00004134static FILE *
4135popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004136{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004137 int oldfd, tgtfd;
4138 HFILE pipeh[2];
4139 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004140
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004141 /* mode determines which of stdin or stdout is reconnected to
4142 * the pipe to the child
4143 */
4144 if (strchr(mode, 'r') != NULL) {
4145 tgt_fd = 1; /* stdout */
4146 } else if (strchr(mode, 'w')) {
4147 tgt_fd = 0; /* stdin */
4148 } else {
4149 *err = ERROR_INVALID_ACCESS;
4150 return NULL;
4151 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004152
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004153 /* setup the pipe */
4154 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4155 *err = rc;
4156 return NULL;
4157 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004158
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004159 /* prevent other threads accessing stdio */
4160 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004161
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004162 /* reconnect stdio and execute child */
4163 oldfd = dup(tgtfd);
4164 close(tgtfd);
4165 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4166 DosClose(pipeh[tgtfd]);
4167 rc = async_system(command);
4168 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004169
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004170 /* restore stdio */
4171 dup2(oldfd, tgtfd);
4172 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004173
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004174 /* allow other threads access to stdio */
4175 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004176
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004177 /* if execution of child was successful return file stream */
4178 if (rc == NO_ERROR)
4179 return fdopen(pipeh[1 - tgtfd], mode);
4180 else {
4181 DosClose(pipeh[1 - tgtfd]);
4182 *err = rc;
4183 return NULL;
4184 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004185}
4186
4187static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004188posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004189{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004190 char *name;
4191 char *mode = "r";
4192 int err, bufsize = -1;
4193 FILE *fp;
4194 PyObject *f;
4195 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4196 return NULL;
4197 Py_BEGIN_ALLOW_THREADS
4198 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4199 Py_END_ALLOW_THREADS
4200 if (fp == NULL)
4201 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004202
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004203 f = PyFile_FromFile(fp, name, mode, fclose);
4204 if (f != NULL)
4205 PyFile_SetBufSize(f, bufsize);
4206 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004207}
4208
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004209#elif defined(PYCC_GCC)
4210
4211/* standard posix version of popen() support */
4212static PyObject *
4213posix_popen(PyObject *self, PyObject *args)
4214{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004215 char *name;
4216 char *mode = "r";
4217 int bufsize = -1;
4218 FILE *fp;
4219 PyObject *f;
4220 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4221 return NULL;
4222 Py_BEGIN_ALLOW_THREADS
4223 fp = popen(name, mode);
4224 Py_END_ALLOW_THREADS
4225 if (fp == NULL)
4226 return posix_error();
4227 f = PyFile_FromFile(fp, name, mode, pclose);
4228 if (f != NULL)
4229 PyFile_SetBufSize(f, bufsize);
4230 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004231}
4232
4233/* fork() under OS/2 has lots'o'warts
4234 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4235 * most of this code is a ripoff of the win32 code, but using the
4236 * capabilities of EMX's C library routines
4237 */
4238
4239/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4240#define POPEN_1 1
4241#define POPEN_2 2
4242#define POPEN_3 3
4243#define POPEN_4 4
4244
4245static PyObject *_PyPopen(char *, int, int, int);
4246static int _PyPclose(FILE *file);
4247
4248/*
4249 * Internal dictionary mapping popen* file pointers to process handles,
4250 * for use when retrieving the process exit code. See _PyPclose() below
4251 * for more information on this dictionary's use.
4252 */
4253static PyObject *_PyPopenProcs = NULL;
4254
4255/* os2emx version of popen2()
4256 *
4257 * The result of this function is a pipe (file) connected to the
4258 * process's stdin, and a pipe connected to the process's stdout.
4259 */
4260
4261static PyObject *
4262os2emx_popen2(PyObject *self, PyObject *args)
4263{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004264 PyObject *f;
4265 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004266
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004267 char *cmdstring;
4268 char *mode = "t";
4269 int bufsize = -1;
4270 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4271 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004272
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004273 if (*mode == 't')
4274 tm = O_TEXT;
4275 else if (*mode != 'b') {
4276 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4277 return NULL;
4278 } else
4279 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004280
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004281 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004282
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004283 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004284}
4285
4286/*
4287 * Variation on os2emx.popen2
4288 *
4289 * The result of this function is 3 pipes - the process's stdin,
4290 * stdout and stderr
4291 */
4292
4293static PyObject *
4294os2emx_popen3(PyObject *self, PyObject *args)
4295{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004296 PyObject *f;
4297 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004298
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004299 char *cmdstring;
4300 char *mode = "t";
4301 int bufsize = -1;
4302 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4303 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004304
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004305 if (*mode == 't')
4306 tm = O_TEXT;
4307 else if (*mode != 'b') {
4308 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4309 return NULL;
4310 } else
4311 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004312
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004313 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004314
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004315 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004316}
4317
4318/*
4319 * Variation on os2emx.popen2
4320 *
Tim Peters11b23062003-04-23 02:39:17 +00004321 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004322 * and stdout+stderr combined as a single pipe.
4323 */
4324
4325static PyObject *
4326os2emx_popen4(PyObject *self, PyObject *args)
4327{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004328 PyObject *f;
4329 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004330
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004331 char *cmdstring;
4332 char *mode = "t";
4333 int bufsize = -1;
4334 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4335 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004336
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004337 if (*mode == 't')
4338 tm = O_TEXT;
4339 else if (*mode != 'b') {
4340 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4341 return NULL;
4342 } else
4343 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004344
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004345 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004346
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004347 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004348}
4349
4350/* a couple of structures for convenient handling of multiple
4351 * file handles and pipes
4352 */
4353struct file_ref
4354{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004355 int handle;
4356 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004357};
4358
4359struct pipe_ref
4360{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004361 int rd;
4362 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004363};
4364
4365/* The following code is derived from the win32 code */
4366
4367static PyObject *
4368_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4369{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004370 struct file_ref stdio[3];
4371 struct pipe_ref p_fd[3];
4372 FILE *p_s[3];
4373 int file_count, i, pipe_err;
4374 pid_t pipe_pid;
4375 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4376 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004377
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004378 /* file modes for subsequent fdopen's on pipe handles */
4379 if (mode == O_TEXT)
4380 {
4381 rd_mode = "rt";
4382 wr_mode = "wt";
4383 }
4384 else
4385 {
4386 rd_mode = "rb";
4387 wr_mode = "wb";
4388 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004389
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004390 /* prepare shell references */
4391 if ((shell = getenv("EMXSHELL")) == NULL)
4392 if ((shell = getenv("COMSPEC")) == NULL)
4393 {
4394 errno = ENOENT;
4395 return posix_error();
4396 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004397
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004398 sh_name = _getname(shell);
4399 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4400 opt = "/c";
4401 else
4402 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004403
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004404 /* save current stdio fds + their flags, and set not inheritable */
4405 i = pipe_err = 0;
4406 while (pipe_err >= 0 && i < 3)
4407 {
4408 pipe_err = stdio[i].handle = dup(i);
4409 stdio[i].flags = fcntl(i, F_GETFD, 0);
4410 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4411 i++;
4412 }
4413 if (pipe_err < 0)
4414 {
4415 /* didn't get them all saved - clean up and bail out */
4416 int saved_err = errno;
4417 while (i-- > 0)
4418 {
4419 close(stdio[i].handle);
4420 }
4421 errno = saved_err;
4422 return posix_error();
4423 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004424
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004425 /* create pipe ends */
4426 file_count = 2;
4427 if (n == POPEN_3)
4428 file_count = 3;
4429 i = pipe_err = 0;
4430 while ((pipe_err == 0) && (i < file_count))
4431 pipe_err = pipe((int *)&p_fd[i++]);
4432 if (pipe_err < 0)
4433 {
4434 /* didn't get them all made - clean up and bail out */
4435 while (i-- > 0)
4436 {
4437 close(p_fd[i].wr);
4438 close(p_fd[i].rd);
4439 }
4440 errno = EPIPE;
4441 return posix_error();
4442 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004443
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004444 /* change the actual standard IO streams over temporarily,
4445 * making the retained pipe ends non-inheritable
4446 */
4447 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004448
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004449 /* - stdin */
4450 if (dup2(p_fd[0].rd, 0) == 0)
4451 {
4452 close(p_fd[0].rd);
4453 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4454 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4455 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4456 {
4457 close(p_fd[0].wr);
4458 pipe_err = -1;
4459 }
4460 }
4461 else
4462 {
4463 pipe_err = -1;
4464 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004465
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004466 /* - stdout */
4467 if (pipe_err == 0)
4468 {
4469 if (dup2(p_fd[1].wr, 1) == 1)
4470 {
4471 close(p_fd[1].wr);
4472 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4473 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4474 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4475 {
4476 close(p_fd[1].rd);
4477 pipe_err = -1;
4478 }
4479 }
4480 else
4481 {
4482 pipe_err = -1;
4483 }
4484 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004485
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004486 /* - stderr, as required */
4487 if (pipe_err == 0)
4488 switch (n)
4489 {
4490 case POPEN_3:
4491 {
4492 if (dup2(p_fd[2].wr, 2) == 2)
4493 {
4494 close(p_fd[2].wr);
4495 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4496 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4497 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4498 {
4499 close(p_fd[2].rd);
4500 pipe_err = -1;
4501 }
4502 }
4503 else
4504 {
4505 pipe_err = -1;
4506 }
4507 break;
4508 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004509
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004510 case POPEN_4:
4511 {
4512 if (dup2(1, 2) != 2)
4513 {
4514 pipe_err = -1;
4515 }
4516 break;
4517 }
4518 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004519
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004520 /* spawn the child process */
4521 if (pipe_err == 0)
4522 {
4523 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4524 if (pipe_pid == -1)
4525 {
4526 pipe_err = -1;
4527 }
4528 else
4529 {
4530 /* save the PID into the FILE structure
4531 * NOTE: this implementation doesn't actually
4532 * take advantage of this, but do it for
4533 * completeness - AIM Apr01
4534 */
4535 for (i = 0; i < file_count; i++)
4536 p_s[i]->_pid = pipe_pid;
4537 }
4538 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004539
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004540 /* reset standard IO to normal */
4541 for (i = 0; i < 3; i++)
4542 {
4543 dup2(stdio[i].handle, i);
4544 fcntl(i, F_SETFD, stdio[i].flags);
4545 close(stdio[i].handle);
4546 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004547
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004548 /* if any remnant problems, clean up and bail out */
4549 if (pipe_err < 0)
4550 {
4551 for (i = 0; i < 3; i++)
4552 {
4553 close(p_fd[i].rd);
4554 close(p_fd[i].wr);
4555 }
4556 errno = EPIPE;
4557 return posix_error_with_filename(cmdstring);
4558 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004559
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004560 /* build tuple of file objects to return */
4561 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4562 PyFile_SetBufSize(p_f[0], bufsize);
4563 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4564 PyFile_SetBufSize(p_f[1], bufsize);
4565 if (n == POPEN_3)
4566 {
4567 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4568 PyFile_SetBufSize(p_f[0], bufsize);
4569 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4570 }
4571 else
4572 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004573
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004574 /*
4575 * Insert the files we've created into the process dictionary
4576 * all referencing the list with the process handle and the
4577 * initial number of files (see description below in _PyPclose).
4578 * Since if _PyPclose later tried to wait on a process when all
4579 * handles weren't closed, it could create a deadlock with the
4580 * child, we spend some energy here to try to ensure that we
4581 * either insert all file handles into the dictionary or none
4582 * at all. It's a little clumsy with the various popen modes
4583 * and variable number of files involved.
4584 */
4585 if (!_PyPopenProcs)
4586 {
4587 _PyPopenProcs = PyDict_New();
4588 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004589
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004590 if (_PyPopenProcs)
4591 {
4592 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4593 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004594
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004595 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4596 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004597
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004598 procObj = PyList_New(2);
4599 pidObj = PyLong_FromPid(pipe_pid);
4600 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004601
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004602 if (procObj && pidObj && intObj)
4603 {
4604 PyList_SetItem(procObj, 0, pidObj);
4605 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004606
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004607 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4608 if (fileObj[0])
4609 {
4610 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4611 fileObj[0],
4612 procObj);
4613 }
4614 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4615 if (fileObj[1])
4616 {
4617 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4618 fileObj[1],
4619 procObj);
4620 }
4621 if (file_count >= 3)
4622 {
4623 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4624 if (fileObj[2])
4625 {
4626 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4627 fileObj[2],
4628 procObj);
4629 }
4630 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004631
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004632 if (ins_rc[0] < 0 || !fileObj[0] ||
4633 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4634 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4635 {
4636 /* Something failed - remove any dictionary
4637 * entries that did make it.
4638 */
4639 if (!ins_rc[0] && fileObj[0])
4640 {
4641 PyDict_DelItem(_PyPopenProcs,
4642 fileObj[0]);
4643 }
4644 if (!ins_rc[1] && fileObj[1])
4645 {
4646 PyDict_DelItem(_PyPopenProcs,
4647 fileObj[1]);
4648 }
4649 if (!ins_rc[2] && fileObj[2])
4650 {
4651 PyDict_DelItem(_PyPopenProcs,
4652 fileObj[2]);
4653 }
4654 }
4655 }
Tim Peters11b23062003-04-23 02:39:17 +00004656
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004657 /*
4658 * Clean up our localized references for the dictionary keys
4659 * and value since PyDict_SetItem will Py_INCREF any copies
4660 * that got placed in the dictionary.
4661 */
4662 Py_XDECREF(procObj);
4663 Py_XDECREF(fileObj[0]);
4664 Py_XDECREF(fileObj[1]);
4665 Py_XDECREF(fileObj[2]);
4666 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004667
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004668 /* Child is launched. */
4669 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004670}
4671
4672/*
4673 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4674 * exit code for the child process and return as a result of the close.
4675 *
4676 * This function uses the _PyPopenProcs dictionary in order to map the
4677 * input file pointer to information about the process that was
4678 * originally created by the popen* call that created the file pointer.
4679 * The dictionary uses the file pointer as a key (with one entry
4680 * inserted for each file returned by the original popen* call) and a
4681 * single list object as the value for all files from a single call.
4682 * The list object contains the Win32 process handle at [0], and a file
4683 * count at [1], which is initialized to the total number of file
4684 * handles using that list.
4685 *
4686 * This function closes whichever handle it is passed, and decrements
4687 * the file count in the dictionary for the process handle pointed to
4688 * by this file. On the last close (when the file count reaches zero),
4689 * this function will wait for the child process and then return its
4690 * exit code as the result of the close() operation. This permits the
4691 * files to be closed in any order - it is always the close() of the
4692 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004693 *
4694 * NOTE: This function is currently called with the GIL released.
4695 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004696 */
4697
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004698static int _PyPclose(FILE *file)
4699{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004700 int result;
4701 int exit_code;
4702 pid_t pipe_pid;
4703 PyObject *procObj, *pidObj, *intObj, *fileObj;
4704 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004705#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004706 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004707#endif
4708
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004709 /* Close the file handle first, to ensure it can't block the
4710 * child from exiting if it's the last handle.
4711 */
4712 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004713
4714#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004715 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004716#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004717 if (_PyPopenProcs)
4718 {
4719 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4720 (procObj = PyDict_GetItem(_PyPopenProcs,
4721 fileObj)) != NULL &&
4722 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4723 (intObj = PyList_GetItem(procObj,1)) != NULL)
4724 {
4725 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
4726 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004727
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004728 if (file_count > 1)
4729 {
4730 /* Still other files referencing process */
4731 file_count--;
4732 PyList_SetItem(procObj,1,
4733 PyInt_FromLong((long) file_count));
4734 }
4735 else
4736 {
4737 /* Last file for this process */
4738 if (result != EOF &&
4739 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4740 {
4741 /* extract exit status */
4742 if (WIFEXITED(exit_code))
4743 {
4744 result = WEXITSTATUS(exit_code);
4745 }
4746 else
4747 {
4748 errno = EPIPE;
4749 result = -1;
4750 }
4751 }
4752 else
4753 {
4754 /* Indicate failure - this will cause the file object
4755 * to raise an I/O error and translate the last
4756 * error code from errno. We do have a problem with
4757 * last errors that overlap the normal errno table,
4758 * but that's a consistent problem with the file object.
4759 */
4760 result = -1;
4761 }
4762 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004763
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004764 /* Remove this file pointer from dictionary */
4765 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004766
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004767 if (PyDict_Size(_PyPopenProcs) == 0)
4768 {
4769 Py_DECREF(_PyPopenProcs);
4770 _PyPopenProcs = NULL;
4771 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004772
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004773 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004774
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004775 Py_XDECREF(fileObj);
4776 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004777
4778#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004779 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004780#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004781 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004782}
4783
4784#endif /* PYCC_??? */
4785
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004786#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004787
4788/*
4789 * Portable 'popen' replacement for Win32.
4790 *
4791 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4792 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004793 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004794 */
4795
4796#include <malloc.h>
4797#include <io.h>
4798#include <fcntl.h>
4799
4800/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4801#define POPEN_1 1
4802#define POPEN_2 2
4803#define POPEN_3 3
4804#define POPEN_4 4
4805
4806static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004807static int _PyPclose(FILE *file);
4808
4809/*
4810 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004811 * for use when retrieving the process exit code. See _PyPclose() below
4812 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004813 */
4814static PyObject *_PyPopenProcs = NULL;
4815
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004816
4817/* popen that works from a GUI.
4818 *
4819 * The result of this function is a pipe (file) connected to the
4820 * processes stdin or stdout, depending on the requested mode.
4821 */
4822
4823static PyObject *
4824posix_popen(PyObject *self, PyObject *args)
4825{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004826 PyObject *f;
4827 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004828
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004829 char *cmdstring;
4830 char *mode = "r";
4831 int bufsize = -1;
4832 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
4833 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004834
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004835 if (*mode == 'r')
4836 tm = _O_RDONLY;
4837 else if (*mode != 'w') {
4838 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
4839 return NULL;
4840 } else
4841 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004842
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004843 if (bufsize != -1) {
4844 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
4845 return NULL;
4846 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004847
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004848 if (*(mode+1) == 't')
4849 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4850 else if (*(mode+1) == 'b')
4851 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
4852 else
4853 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004854
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004855 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004856}
4857
4858/* Variation on win32pipe.popen
4859 *
4860 * The result of this function is a pipe (file) connected to the
4861 * process's stdin, and a pipe connected to the process's stdout.
4862 */
4863
4864static PyObject *
4865win32_popen2(PyObject *self, PyObject *args)
4866{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004867 PyObject *f;
4868 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004869
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004870 char *cmdstring;
4871 char *mode = "t";
4872 int bufsize = -1;
4873 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4874 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004875
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004876 if (*mode == 't')
4877 tm = _O_TEXT;
4878 else if (*mode != 'b') {
4879 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
4880 return NULL;
4881 } else
4882 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004883
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004884 if (bufsize != -1) {
4885 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
4886 return NULL;
4887 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004888
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004889 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004890
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004891 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004892}
4893
4894/*
4895 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004896 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004897 * The result of this function is 3 pipes - the process's stdin,
4898 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004899 */
4900
4901static PyObject *
4902win32_popen3(PyObject *self, PyObject *args)
4903{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004904 PyObject *f;
4905 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004906
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004907 char *cmdstring;
4908 char *mode = "t";
4909 int bufsize = -1;
4910 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4911 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004912
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004913 if (*mode == 't')
4914 tm = _O_TEXT;
4915 else if (*mode != 'b') {
4916 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
4917 return NULL;
4918 } else
4919 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004920
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004921 if (bufsize != -1) {
4922 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
4923 return NULL;
4924 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004925
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004926 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004927
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004928 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004929}
4930
4931/*
4932 * Variation on win32pipe.popen
4933 *
Tim Peters5aa91602002-01-30 05:46:57 +00004934 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004935 * and stdout+stderr combined as a single pipe.
4936 */
4937
4938static PyObject *
4939win32_popen4(PyObject *self, PyObject *args)
4940{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004941 PyObject *f;
4942 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004943
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004944 char *cmdstring;
4945 char *mode = "t";
4946 int bufsize = -1;
4947 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4948 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004949
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004950 if (*mode == 't')
4951 tm = _O_TEXT;
4952 else if (*mode != 'b') {
4953 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
4954 return NULL;
4955 } else
4956 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004957
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004958 if (bufsize != -1) {
4959 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
4960 return NULL;
4961 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004962
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004963 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004964
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004965 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004966}
4967
Mark Hammond08501372001-01-31 07:30:29 +00004968static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004969_PyPopenCreateProcess(char *cmdstring,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004970 HANDLE hStdin,
4971 HANDLE hStdout,
4972 HANDLE hStderr,
4973 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004974{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004975 PROCESS_INFORMATION piProcInfo;
4976 STARTUPINFO siStartInfo;
4977 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
4978 char *s1,*s2, *s3 = " /c ";
4979 const char *szConsoleSpawn = "w9xpopen.exe";
4980 int i;
4981 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004982
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004983 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
4984 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00004985
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004986 s1 = (char *)alloca(i);
4987 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4988 /* x < i, so x fits into an integer */
4989 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004990
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004991 /* Explicitly check if we are using COMMAND.COM. If we are
4992 * then use the w9xpopen hack.
4993 */
4994 comshell = s1 + x;
4995 while (comshell >= s1 && *comshell != '\\')
4996 --comshell;
4997 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00004998
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004999 if (GetVersion() < 0x80000000 &&
5000 _stricmp(comshell, "command.com") != 0) {
5001 /* NT/2000 and not using command.com. */
5002 x = i + strlen(s3) + strlen(cmdstring) + 1;
5003 s2 = (char *)alloca(x);
5004 ZeroMemory(s2, x);
5005 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5006 }
5007 else {
5008 /*
5009 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5010 * the workaround listed in KB: Q150956
5011 */
5012 char modulepath[_MAX_PATH];
5013 struct stat statinfo;
5014 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5015 for (x = i = 0; modulepath[i]; i++)
5016 if (modulepath[i] == SEP)
5017 x = i+1;
5018 modulepath[x] = '\0';
5019 /* Create the full-name to w9xpopen, so we can test it exists */
5020 strncat(modulepath,
5021 szConsoleSpawn,
5022 (sizeof(modulepath)/sizeof(modulepath[0]))
5023 -strlen(modulepath));
5024 if (stat(modulepath, &statinfo) != 0) {
5025 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5026 /* Eeek - file-not-found - possibly an embedding
5027 situation - see if we can locate it in sys.prefix
5028 */
5029 strncpy(modulepath,
5030 Py_GetExecPrefix(),
5031 mplen);
5032 modulepath[mplen-1] = '\0';
5033 if (modulepath[strlen(modulepath)-1] != '\\')
5034 strcat(modulepath, "\\");
5035 strncat(modulepath,
5036 szConsoleSpawn,
5037 mplen-strlen(modulepath));
5038 /* No where else to look - raise an easily identifiable
5039 error, rather than leaving Windows to report
5040 "file not found" - as the user is probably blissfully
5041 unaware this shim EXE is used, and it will confuse them.
5042 (well, it confused me for a while ;-)
5043 */
5044 if (stat(modulepath, &statinfo) != 0) {
5045 PyErr_Format(PyExc_RuntimeError,
5046 "Can not locate '%s' which is needed "
5047 "for popen to work with your shell "
5048 "or platform.",
5049 szConsoleSpawn);
5050 return FALSE;
5051 }
5052 }
5053 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5054 strlen(modulepath) +
5055 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005056
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005057 s2 = (char *)alloca(x);
5058 ZeroMemory(s2, x);
5059 /* To maintain correct argument passing semantics,
5060 we pass the command-line as it stands, and allow
5061 quoting to be applied. w9xpopen.exe will then
5062 use its argv vector, and re-quote the necessary
5063 args for the ultimate child process.
5064 */
5065 PyOS_snprintf(
5066 s2, x,
5067 "\"%s\" %s%s%s",
5068 modulepath,
5069 s1,
5070 s3,
5071 cmdstring);
5072 /* Not passing CREATE_NEW_CONSOLE has been known to
5073 cause random failures on win9x. Specifically a
5074 dialog:
5075 "Your program accessed mem currently in use at xxx"
5076 and a hopeful warning about the stability of your
5077 system.
5078 Cost is Ctrl+C won't kill children, but anyone
5079 who cares can have a go!
5080 */
5081 dwProcessFlags |= CREATE_NEW_CONSOLE;
5082 }
5083 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005084
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005085 /* Could be an else here to try cmd.exe / command.com in the path
5086 Now we'll just error out.. */
5087 else {
5088 PyErr_SetString(PyExc_RuntimeError,
5089 "Cannot locate a COMSPEC environment variable to "
5090 "use as the shell");
5091 return FALSE;
5092 }
Tim Peters5aa91602002-01-30 05:46:57 +00005093
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005094 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5095 siStartInfo.cb = sizeof(STARTUPINFO);
5096 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5097 siStartInfo.hStdInput = hStdin;
5098 siStartInfo.hStdOutput = hStdout;
5099 siStartInfo.hStdError = hStderr;
5100 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005101
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005102 if (CreateProcess(NULL,
5103 s2,
5104 NULL,
5105 NULL,
5106 TRUE,
5107 dwProcessFlags,
5108 NULL,
5109 NULL,
5110 &siStartInfo,
5111 &piProcInfo) ) {
5112 /* Close the handles now so anyone waiting is woken. */
5113 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005114
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005115 /* Return process handle */
5116 *hProcess = piProcInfo.hProcess;
5117 return TRUE;
5118 }
5119 win32_error("CreateProcess", s2);
5120 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005121}
5122
5123/* The following code is based off of KB: Q190351 */
5124
5125static PyObject *
5126_PyPopen(char *cmdstring, int mode, int n)
5127{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005128 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5129 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5130 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005131
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005132 SECURITY_ATTRIBUTES saAttr;
5133 BOOL fSuccess;
5134 int fd1, fd2, fd3;
5135 FILE *f1, *f2, *f3;
5136 long file_count;
5137 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005138
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005139 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5140 saAttr.bInheritHandle = TRUE;
5141 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005142
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005143 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5144 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005145
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005146 /* Create new output read handle and the input write handle. Set
5147 * the inheritance properties to FALSE. Otherwise, the child inherits
5148 * these handles; resulting in non-closeable handles to the pipes
5149 * being created. */
5150 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5151 GetCurrentProcess(), &hChildStdinWrDup, 0,
5152 FALSE,
5153 DUPLICATE_SAME_ACCESS);
5154 if (!fSuccess)
5155 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005156
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005157 /* Close the inheritable version of ChildStdin
5158 that we're using. */
5159 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005160
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005161 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5162 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005163
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005164 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5165 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5166 FALSE, DUPLICATE_SAME_ACCESS);
5167 if (!fSuccess)
5168 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005169
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005170 /* Close the inheritable version of ChildStdout
5171 that we're using. */
5172 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005173
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005174 if (n != POPEN_4) {
5175 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5176 return win32_error("CreatePipe", NULL);
5177 fSuccess = DuplicateHandle(GetCurrentProcess(),
5178 hChildStderrRd,
5179 GetCurrentProcess(),
5180 &hChildStderrRdDup, 0,
5181 FALSE, DUPLICATE_SAME_ACCESS);
5182 if (!fSuccess)
5183 return win32_error("DuplicateHandle", NULL);
5184 /* Close the inheritable version of ChildStdErr that we're using. */
5185 CloseHandle(hChildStderrRd);
5186 }
Tim Peters5aa91602002-01-30 05:46:57 +00005187
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005188 switch (n) {
5189 case POPEN_1:
5190 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5191 case _O_WRONLY | _O_TEXT:
5192 /* Case for writing to child Stdin in text mode. */
5193 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5194 f1 = _fdopen(fd1, "w");
5195 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5196 PyFile_SetBufSize(f, 0);
5197 /* We don't care about these pipes anymore, so close them. */
5198 CloseHandle(hChildStdoutRdDup);
5199 CloseHandle(hChildStderrRdDup);
5200 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005201
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005202 case _O_RDONLY | _O_TEXT:
5203 /* Case for reading from child Stdout in text mode. */
5204 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5205 f1 = _fdopen(fd1, "r");
5206 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5207 PyFile_SetBufSize(f, 0);
5208 /* We don't care about these pipes anymore, so close them. */
5209 CloseHandle(hChildStdinWrDup);
5210 CloseHandle(hChildStderrRdDup);
5211 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005212
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005213 case _O_RDONLY | _O_BINARY:
5214 /* Case for readinig from child Stdout in binary mode. */
5215 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5216 f1 = _fdopen(fd1, "rb");
5217 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5218 PyFile_SetBufSize(f, 0);
5219 /* We don't care about these pipes anymore, so close them. */
5220 CloseHandle(hChildStdinWrDup);
5221 CloseHandle(hChildStderrRdDup);
5222 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005223
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005224 case _O_WRONLY | _O_BINARY:
5225 /* Case for writing to child Stdin in binary mode. */
5226 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5227 f1 = _fdopen(fd1, "wb");
5228 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5229 PyFile_SetBufSize(f, 0);
5230 /* We don't care about these pipes anymore, so close them. */
5231 CloseHandle(hChildStdoutRdDup);
5232 CloseHandle(hChildStderrRdDup);
5233 break;
5234 }
5235 file_count = 1;
5236 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005237
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005238 case POPEN_2:
5239 case POPEN_4:
5240 {
5241 char *m1, *m2;
5242 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005243
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005244 if (mode & _O_TEXT) {
5245 m1 = "r";
5246 m2 = "w";
5247 } else {
5248 m1 = "rb";
5249 m2 = "wb";
5250 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005251
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005252 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5253 f1 = _fdopen(fd1, m2);
5254 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5255 f2 = _fdopen(fd2, m1);
5256 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5257 PyFile_SetBufSize(p1, 0);
5258 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5259 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005260
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005261 if (n != 4)
5262 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005263
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005264 f = PyTuple_Pack(2,p1,p2);
5265 Py_XDECREF(p1);
5266 Py_XDECREF(p2);
5267 file_count = 2;
5268 break;
5269 }
Tim Peters5aa91602002-01-30 05:46:57 +00005270
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005271 case POPEN_3:
5272 {
5273 char *m1, *m2;
5274 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005275
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005276 if (mode & _O_TEXT) {
5277 m1 = "r";
5278 m2 = "w";
5279 } else {
5280 m1 = "rb";
5281 m2 = "wb";
5282 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005283
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005284 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5285 f1 = _fdopen(fd1, m2);
5286 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5287 f2 = _fdopen(fd2, m1);
5288 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5289 f3 = _fdopen(fd3, m1);
5290 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5291 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5292 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5293 PyFile_SetBufSize(p1, 0);
5294 PyFile_SetBufSize(p2, 0);
5295 PyFile_SetBufSize(p3, 0);
5296 f = PyTuple_Pack(3,p1,p2,p3);
5297 Py_XDECREF(p1);
5298 Py_XDECREF(p2);
5299 Py_XDECREF(p3);
5300 file_count = 3;
5301 break;
5302 }
5303 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005304
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005305 if (n == POPEN_4) {
5306 if (!_PyPopenCreateProcess(cmdstring,
5307 hChildStdinRd,
5308 hChildStdoutWr,
5309 hChildStdoutWr,
5310 &hProcess))
5311 return NULL;
5312 }
5313 else {
5314 if (!_PyPopenCreateProcess(cmdstring,
5315 hChildStdinRd,
5316 hChildStdoutWr,
5317 hChildStderrWr,
5318 &hProcess))
5319 return NULL;
5320 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005321
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005322 /*
5323 * Insert the files we've created into the process dictionary
5324 * all referencing the list with the process handle and the
5325 * initial number of files (see description below in _PyPclose).
5326 * Since if _PyPclose later tried to wait on a process when all
5327 * handles weren't closed, it could create a deadlock with the
5328 * child, we spend some energy here to try to ensure that we
5329 * either insert all file handles into the dictionary or none
5330 * at all. It's a little clumsy with the various popen modes
5331 * and variable number of files involved.
5332 */
5333 if (!_PyPopenProcs) {
5334 _PyPopenProcs = PyDict_New();
5335 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005336
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005337 if (_PyPopenProcs) {
5338 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5339 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005340
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005341 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5342 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005343
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005344 procObj = PyList_New(2);
5345 hProcessObj = PyLong_FromVoidPtr(hProcess);
5346 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005347
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005348 if (procObj && hProcessObj && intObj) {
5349 PyList_SetItem(procObj,0,hProcessObj);
5350 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005351
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005352 fileObj[0] = PyLong_FromVoidPtr(f1);
5353 if (fileObj[0]) {
5354 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5355 fileObj[0],
5356 procObj);
5357 }
5358 if (file_count >= 2) {
5359 fileObj[1] = PyLong_FromVoidPtr(f2);
5360 if (fileObj[1]) {
5361 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5362 fileObj[1],
5363 procObj);
5364 }
5365 }
5366 if (file_count >= 3) {
5367 fileObj[2] = PyLong_FromVoidPtr(f3);
5368 if (fileObj[2]) {
5369 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5370 fileObj[2],
5371 procObj);
5372 }
5373 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005374
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005375 if (ins_rc[0] < 0 || !fileObj[0] ||
5376 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5377 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5378 /* Something failed - remove any dictionary
5379 * entries that did make it.
5380 */
5381 if (!ins_rc[0] && fileObj[0]) {
5382 PyDict_DelItem(_PyPopenProcs,
5383 fileObj[0]);
5384 }
5385 if (!ins_rc[1] && fileObj[1]) {
5386 PyDict_DelItem(_PyPopenProcs,
5387 fileObj[1]);
5388 }
5389 if (!ins_rc[2] && fileObj[2]) {
5390 PyDict_DelItem(_PyPopenProcs,
5391 fileObj[2]);
5392 }
5393 }
5394 }
Tim Peters5aa91602002-01-30 05:46:57 +00005395
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005396 /*
5397 * Clean up our localized references for the dictionary keys
5398 * and value since PyDict_SetItem will Py_INCREF any copies
5399 * that got placed in the dictionary.
5400 */
5401 Py_XDECREF(procObj);
5402 Py_XDECREF(fileObj[0]);
5403 Py_XDECREF(fileObj[1]);
5404 Py_XDECREF(fileObj[2]);
5405 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005406
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005407 /* Child is launched. Close the parents copy of those pipe
5408 * handles that only the child should have open. You need to
5409 * make sure that no handles to the write end of the output pipe
5410 * are maintained in this process or else the pipe will not close
5411 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005412
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005413 if (!CloseHandle(hChildStdinRd))
5414 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005415
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005416 if (!CloseHandle(hChildStdoutWr))
5417 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005418
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005419 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5420 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005421
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005422 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005423}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005424
5425/*
5426 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5427 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005428 *
5429 * This function uses the _PyPopenProcs dictionary in order to map the
5430 * input file pointer to information about the process that was
5431 * originally created by the popen* call that created the file pointer.
5432 * The dictionary uses the file pointer as a key (with one entry
5433 * inserted for each file returned by the original popen* call) and a
5434 * single list object as the value for all files from a single call.
5435 * The list object contains the Win32 process handle at [0], and a file
5436 * count at [1], which is initialized to the total number of file
5437 * handles using that list.
5438 *
5439 * This function closes whichever handle it is passed, and decrements
5440 * the file count in the dictionary for the process handle pointed to
5441 * by this file. On the last close (when the file count reaches zero),
5442 * this function will wait for the child process and then return its
5443 * exit code as the result of the close() operation. This permits the
5444 * files to be closed in any order - it is always the close() of the
5445 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005446 *
5447 * NOTE: This function is currently called with the GIL released.
5448 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005449 */
Tim Peters736aa322000-09-01 06:51:24 +00005450
Fredrik Lundh56055a42000-07-23 19:47:12 +00005451static int _PyPclose(FILE *file)
5452{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005453 int result;
5454 DWORD exit_code;
5455 HANDLE hProcess;
5456 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5457 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005458#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005459 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005460#endif
5461
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005462 /* Close the file handle first, to ensure it can't block the
5463 * child from exiting if it's the last handle.
5464 */
5465 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005466#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005467 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005468#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005469 if (_PyPopenProcs) {
5470 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5471 (procObj = PyDict_GetItem(_PyPopenProcs,
5472 fileObj)) != NULL &&
5473 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5474 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005475
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005476 hProcess = PyLong_AsVoidPtr(hProcessObj);
5477 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005478
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005479 if (file_count > 1) {
5480 /* Still other files referencing process */
5481 file_count--;
5482 PyList_SetItem(procObj,1,
5483 PyInt_FromLong(file_count));
5484 } else {
5485 /* Last file for this process */
5486 if (result != EOF &&
5487 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5488 GetExitCodeProcess(hProcess, &exit_code)) {
5489 /* Possible truncation here in 16-bit environments, but
5490 * real exit codes are just the lower byte in any event.
5491 */
5492 result = exit_code;
5493 } else {
5494 /* Indicate failure - this will cause the file object
5495 * to raise an I/O error and translate the last Win32
5496 * error code from errno. We do have a problem with
5497 * last errors that overlap the normal errno table,
5498 * but that's a consistent problem with the file object.
5499 */
5500 if (result != EOF) {
5501 /* If the error wasn't from the fclose(), then
5502 * set errno for the file object error handling.
5503 */
5504 errno = GetLastError();
5505 }
5506 result = -1;
5507 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005508
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005509 /* Free up the native handle at this point */
5510 CloseHandle(hProcess);
5511 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005512
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005513 /* Remove this file pointer from dictionary */
5514 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005515
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005516 if (PyDict_Size(_PyPopenProcs) == 0) {
5517 Py_DECREF(_PyPopenProcs);
5518 _PyPopenProcs = NULL;
5519 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005520
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005521 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005522
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005523 Py_XDECREF(fileObj);
5524 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005525
Tim Peters736aa322000-09-01 06:51:24 +00005526#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005527 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005528#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005529 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005530}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005531
5532#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005534posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005535{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005536 char *name;
5537 char *mode = "r";
5538 int bufsize = -1;
5539 FILE *fp;
5540 PyObject *f;
5541 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5542 return NULL;
5543 /* Strip mode of binary or text modifiers */
5544 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5545 mode = "r";
5546 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5547 mode = "w";
5548 Py_BEGIN_ALLOW_THREADS
5549 fp = popen(name, mode);
5550 Py_END_ALLOW_THREADS
5551 if (fp == NULL)
5552 return posix_error();
5553 f = PyFile_FromFile(fp, name, mode, pclose);
5554 if (f != NULL)
5555 PyFile_SetBufSize(f, bufsize);
5556 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005557}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005558
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005559#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005560#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005562
Guido van Rossumb6775db1994-08-01 11:34:53 +00005563#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005565"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Set the current process's user id.");
5567
Barry Warsaw53699e91996-12-10 23:23:01 +00005568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005569posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005570{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005571 long uid_arg;
5572 uid_t uid;
5573 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5574 return NULL;
5575 uid = uid_arg;
5576 if (uid != uid_arg) {
5577 PyErr_SetString(PyExc_OverflowError, "user id too big");
5578 return NULL;
5579 }
5580 if (setuid(uid) < 0)
5581 return posix_error();
5582 Py_INCREF(Py_None);
5583 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005584}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005585#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005586
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005587
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005588#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005589PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005590"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005591Set the current process's effective user id.");
5592
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005593static PyObject *
5594posix_seteuid (PyObject *self, PyObject *args)
5595{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005596 long euid_arg;
5597 uid_t euid;
5598 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5599 return NULL;
5600 euid = euid_arg;
5601 if (euid != euid_arg) {
5602 PyErr_SetString(PyExc_OverflowError, "user id too big");
5603 return NULL;
5604 }
5605 if (seteuid(euid) < 0) {
5606 return posix_error();
5607 } else {
5608 Py_INCREF(Py_None);
5609 return Py_None;
5610 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005611}
5612#endif /* HAVE_SETEUID */
5613
5614#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005615PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005616"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617Set the current process's effective group id.");
5618
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005619static PyObject *
5620posix_setegid (PyObject *self, PyObject *args)
5621{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005622 long egid_arg;
5623 gid_t egid;
5624 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5625 return NULL;
5626 egid = egid_arg;
5627 if (egid != egid_arg) {
5628 PyErr_SetString(PyExc_OverflowError, "group id too big");
5629 return NULL;
5630 }
5631 if (setegid(egid) < 0) {
5632 return posix_error();
5633 } else {
5634 Py_INCREF(Py_None);
5635 return Py_None;
5636 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005637}
5638#endif /* HAVE_SETEGID */
5639
5640#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005641PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005642"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005643Set the current process's real and effective user ids.");
5644
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005645static PyObject *
5646posix_setreuid (PyObject *self, PyObject *args)
5647{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005648 long ruid_arg, euid_arg;
5649 uid_t ruid, euid;
5650 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5651 return NULL;
5652 if (ruid_arg == -1)
5653 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5654 else
5655 ruid = ruid_arg; /* otherwise, assign from our long */
5656 if (euid_arg == -1)
5657 euid = (uid_t)-1;
5658 else
5659 euid = euid_arg;
5660 if ((euid_arg != -1 && euid != euid_arg) ||
5661 (ruid_arg != -1 && ruid != ruid_arg)) {
5662 PyErr_SetString(PyExc_OverflowError, "user id too big");
5663 return NULL;
5664 }
5665 if (setreuid(ruid, euid) < 0) {
5666 return posix_error();
5667 } else {
5668 Py_INCREF(Py_None);
5669 return Py_None;
5670 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005671}
5672#endif /* HAVE_SETREUID */
5673
5674#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005676"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677Set the current process's real and effective group ids.");
5678
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005679static PyObject *
5680posix_setregid (PyObject *self, PyObject *args)
5681{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005682 long rgid_arg, egid_arg;
5683 gid_t rgid, egid;
5684 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5685 return NULL;
5686 if (rgid_arg == -1)
5687 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5688 else
5689 rgid = rgid_arg; /* otherwise, assign from our long */
5690 if (egid_arg == -1)
5691 egid = (gid_t)-1;
5692 else
5693 egid = egid_arg;
5694 if ((egid_arg != -1 && egid != egid_arg) ||
5695 (rgid_arg != -1 && rgid != rgid_arg)) {
5696 PyErr_SetString(PyExc_OverflowError, "group id too big");
5697 return NULL;
5698 }
5699 if (setregid(rgid, egid) < 0) {
5700 return posix_error();
5701 } else {
5702 Py_INCREF(Py_None);
5703 return Py_None;
5704 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005705}
5706#endif /* HAVE_SETREGID */
5707
Guido van Rossumb6775db1994-08-01 11:34:53 +00005708#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005709PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005710"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005711Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005712
Barry Warsaw53699e91996-12-10 23:23:01 +00005713static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005714posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005715{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005716 long gid_arg;
5717 gid_t gid;
5718 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5719 return NULL;
5720 gid = gid_arg;
5721 if (gid != gid_arg) {
5722 PyErr_SetString(PyExc_OverflowError, "group id too big");
5723 return NULL;
5724 }
5725 if (setgid(gid) < 0)
5726 return posix_error();
5727 Py_INCREF(Py_None);
5728 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005729}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005730#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005731
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005732#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005733PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005734"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005735Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005736
5737static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005738posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005739{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005740 int i, len;
5741 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005742
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005743 if (!PySequence_Check(groups)) {
5744 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5745 return NULL;
5746 }
5747 len = PySequence_Size(groups);
5748 if (len > MAX_GROUPS) {
5749 PyErr_SetString(PyExc_ValueError, "too many groups");
5750 return NULL;
5751 }
5752 for(i = 0; i < len; i++) {
5753 PyObject *elem;
5754 elem = PySequence_GetItem(groups, i);
5755 if (!elem)
5756 return NULL;
5757 if (!PyInt_Check(elem)) {
5758 if (!PyLong_Check(elem)) {
5759 PyErr_SetString(PyExc_TypeError,
5760 "groups must be integers");
5761 Py_DECREF(elem);
5762 return NULL;
5763 } else {
5764 unsigned long x = PyLong_AsUnsignedLong(elem);
5765 if (PyErr_Occurred()) {
5766 PyErr_SetString(PyExc_TypeError,
5767 "group id too big");
5768 Py_DECREF(elem);
5769 return NULL;
5770 }
5771 grouplist[i] = x;
5772 /* read back to see if it fits in gid_t */
5773 if (grouplist[i] != x) {
5774 PyErr_SetString(PyExc_TypeError,
5775 "group id too big");
5776 Py_DECREF(elem);
5777 return NULL;
5778 }
5779 }
5780 } else {
5781 long x = PyInt_AsLong(elem);
5782 grouplist[i] = x;
5783 if (grouplist[i] != x) {
5784 PyErr_SetString(PyExc_TypeError,
5785 "group id too big");
5786 Py_DECREF(elem);
5787 return NULL;
5788 }
5789 }
5790 Py_DECREF(elem);
5791 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005792
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005793 if (setgroups(len, grouplist) < 0)
5794 return posix_error();
5795 Py_INCREF(Py_None);
5796 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005797}
5798#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005799
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005800#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005801static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005802wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005803{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005804 PyObject *result;
5805 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00005806
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005807 if (pid == -1)
5808 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00005809
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005810 if (struct_rusage == NULL) {
5811 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5812 if (m == NULL)
5813 return NULL;
5814 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5815 Py_DECREF(m);
5816 if (struct_rusage == NULL)
5817 return NULL;
5818 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005819
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005820 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5821 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5822 if (!result)
5823 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005824
5825#ifndef doubletime
5826#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5827#endif
5828
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005829 PyStructSequence_SET_ITEM(result, 0,
5830 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5831 PyStructSequence_SET_ITEM(result, 1,
5832 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00005833#define SET_INT(result, index, value)\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005834 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5835 SET_INT(result, 2, ru->ru_maxrss);
5836 SET_INT(result, 3, ru->ru_ixrss);
5837 SET_INT(result, 4, ru->ru_idrss);
5838 SET_INT(result, 5, ru->ru_isrss);
5839 SET_INT(result, 6, ru->ru_minflt);
5840 SET_INT(result, 7, ru->ru_majflt);
5841 SET_INT(result, 8, ru->ru_nswap);
5842 SET_INT(result, 9, ru->ru_inblock);
5843 SET_INT(result, 10, ru->ru_oublock);
5844 SET_INT(result, 11, ru->ru_msgsnd);
5845 SET_INT(result, 12, ru->ru_msgrcv);
5846 SET_INT(result, 13, ru->ru_nsignals);
5847 SET_INT(result, 14, ru->ru_nvcsw);
5848 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00005849#undef SET_INT
5850
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005851 if (PyErr_Occurred()) {
5852 Py_DECREF(result);
5853 return NULL;
5854 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005855
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005856 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005857}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005858#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005859
5860#ifdef HAVE_WAIT3
5861PyDoc_STRVAR(posix_wait3__doc__,
5862"wait3(options) -> (pid, status, rusage)\n\n\
5863Wait for completion of a child process.");
5864
5865static PyObject *
5866posix_wait3(PyObject *self, PyObject *args)
5867{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005868 pid_t pid;
5869 int options;
5870 struct rusage ru;
5871 WAIT_TYPE status;
5872 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005873
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005874 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5875 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005876
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005877 Py_BEGIN_ALLOW_THREADS
5878 pid = wait3(&status, options, &ru);
5879 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005880
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005881 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005882}
5883#endif /* HAVE_WAIT3 */
5884
5885#ifdef HAVE_WAIT4
5886PyDoc_STRVAR(posix_wait4__doc__,
5887"wait4(pid, options) -> (pid, status, rusage)\n\n\
5888Wait for completion of a given child process.");
5889
5890static PyObject *
5891posix_wait4(PyObject *self, PyObject *args)
5892{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005893 pid_t pid;
5894 int options;
5895 struct rusage ru;
5896 WAIT_TYPE status;
5897 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005898
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005899 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
5900 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005901
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005902 Py_BEGIN_ALLOW_THREADS
5903 pid = wait4(pid, &status, options, &ru);
5904 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005905
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005906 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005907}
5908#endif /* HAVE_WAIT4 */
5909
Guido van Rossumb6775db1994-08-01 11:34:53 +00005910#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005911PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005912"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005913Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005914
Barry Warsaw53699e91996-12-10 23:23:01 +00005915static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005916posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005917{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005918 pid_t pid;
5919 int options;
5920 WAIT_TYPE status;
5921 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005922
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005923 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
5924 return NULL;
5925 Py_BEGIN_ALLOW_THREADS
5926 pid = waitpid(pid, &status, options);
5927 Py_END_ALLOW_THREADS
5928 if (pid == -1)
5929 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005930
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005931 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005932}
5933
Tim Petersab034fa2002-02-01 11:27:43 +00005934#elif defined(HAVE_CWAIT)
5935
5936/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005937PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005938"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005939"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005940
5941static PyObject *
5942posix_waitpid(PyObject *self, PyObject *args)
5943{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005944 Py_intptr_t pid;
5945 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005946
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005947 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
5948 return NULL;
5949 Py_BEGIN_ALLOW_THREADS
5950 pid = _cwait(&status, pid, options);
5951 Py_END_ALLOW_THREADS
5952 if (pid == -1)
5953 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005954
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005955 /* shift the status left a byte so this is more like the POSIX waitpid */
5956 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005957}
5958#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005959
Guido van Rossumad0ee831995-03-01 10:34:45 +00005960#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005961PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005962"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005963Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005964
Barry Warsaw53699e91996-12-10 23:23:01 +00005965static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005966posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005967{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005968 pid_t pid;
5969 WAIT_TYPE status;
5970 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005971
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005972 Py_BEGIN_ALLOW_THREADS
5973 pid = wait(&status);
5974 Py_END_ALLOW_THREADS
5975 if (pid == -1)
5976 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005977
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005978 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005979}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005980#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005983PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005984"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005985Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005986
Barry Warsaw53699e91996-12-10 23:23:01 +00005987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005988posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005989{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005990#ifdef HAVE_LSTAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005991 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005992#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005993#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005994 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005995#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005996 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005997#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005998#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005999}
6000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Guido van Rossumb6775db1994-08-01 11:34:53 +00006002#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006003PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006004"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006005Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006006
Barry Warsaw53699e91996-12-10 23:23:01 +00006007static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006008posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006009{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006010 PyObject* v;
6011 char buf[MAXPATHLEN];
6012 char *path;
6013 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006014#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006015 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006016#endif
6017
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006018 if (!PyArg_ParseTuple(args, "et:readlink",
6019 Py_FileSystemDefaultEncoding, &path))
6020 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006021#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006022 v = PySequence_GetItem(args, 0);
6023 if (v == NULL) {
6024 PyMem_Free(path);
6025 return NULL;
6026 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006027
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006028 if (PyUnicode_Check(v)) {
6029 arg_is_unicode = 1;
6030 }
6031 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006032#endif
6033
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006034 Py_BEGIN_ALLOW_THREADS
6035 n = readlink(path, buf, (int) sizeof buf);
6036 Py_END_ALLOW_THREADS
6037 if (n < 0)
6038 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006039
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006040 PyMem_Free(path);
6041 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006042#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006043 if (arg_is_unicode) {
6044 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006045
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006046 w = PyUnicode_FromEncodedObject(v,
6047 Py_FileSystemDefaultEncoding,
6048 "strict");
6049 if (w != NULL) {
6050 Py_DECREF(v);
6051 v = w;
6052 }
6053 else {
6054 /* fall back to the original byte string, as
6055 discussed in patch #683592 */
6056 PyErr_Clear();
6057 }
6058 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006059#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006060 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006061}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006062#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006063
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006064
Guido van Rossumb6775db1994-08-01 11:34:53 +00006065#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006066PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006067"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006068Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006069
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006070static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006071posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006072{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006073 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006074}
6075#endif /* HAVE_SYMLINK */
6076
6077
6078#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006079#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6080static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006081system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006082{
6083 ULONG value = 0;
6084
6085 Py_BEGIN_ALLOW_THREADS
6086 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6087 Py_END_ALLOW_THREADS
6088
6089 return value;
6090}
6091
6092static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006093posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006094{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006095 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006096 return Py_BuildValue("ddddd",
6097 (double)0 /* t.tms_utime / HZ */,
6098 (double)0 /* t.tms_stime / HZ */,
6099 (double)0 /* t.tms_cutime / HZ */,
6100 (double)0 /* t.tms_cstime / HZ */,
6101 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006102}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006103#else /* not OS2 */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00006104#define NEED_TICKS_PER_SECOND
6105static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006106static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006107posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006108{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006109 struct tms t;
6110 clock_t c;
6111 errno = 0;
6112 c = times(&t);
6113 if (c == (clock_t) -1)
6114 return posix_error();
6115 return Py_BuildValue("ddddd",
6116 (double)t.tms_utime / ticks_per_second,
6117 (double)t.tms_stime / ticks_per_second,
6118 (double)t.tms_cutime / ticks_per_second,
6119 (double)t.tms_cstime / ticks_per_second,
6120 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006121}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006122#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006123#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006124
6125
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006126#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006127#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006128static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006129posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006130{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006131 FILETIME create, exit, kernel, user;
6132 HANDLE hProc;
6133 hProc = GetCurrentProcess();
6134 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6135 /* The fields of a FILETIME structure are the hi and lo part
6136 of a 64-bit value expressed in 100 nanosecond units.
6137 1e7 is one second in such units; 1e-7 the inverse.
6138 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6139 */
6140 return Py_BuildValue(
6141 "ddddd",
6142 (double)(user.dwHighDateTime*429.4967296 +
6143 user.dwLowDateTime*1e-7),
6144 (double)(kernel.dwHighDateTime*429.4967296 +
6145 kernel.dwLowDateTime*1e-7),
6146 (double)0,
6147 (double)0,
6148 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006149}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006150#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006151
6152#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006153PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006154"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006155Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006156#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006157
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006158
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006159#ifdef HAVE_GETSID
6160PyDoc_STRVAR(posix_getsid__doc__,
6161"getsid(pid) -> sid\n\n\
6162Call the system call getsid().");
6163
6164static PyObject *
6165posix_getsid(PyObject *self, PyObject *args)
6166{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006167 pid_t pid;
6168 int sid;
6169 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6170 return NULL;
6171 sid = getsid(pid);
6172 if (sid < 0)
6173 return posix_error();
6174 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006175}
6176#endif /* HAVE_GETSID */
6177
6178
Guido van Rossumb6775db1994-08-01 11:34:53 +00006179#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006180PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006181"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006182Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006183
Barry Warsaw53699e91996-12-10 23:23:01 +00006184static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006185posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006186{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006187 if (setsid() < 0)
6188 return posix_error();
6189 Py_INCREF(Py_None);
6190 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006191}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006192#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006193
Guido van Rossumb6775db1994-08-01 11:34:53 +00006194#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006196"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006197Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006198
Barry Warsaw53699e91996-12-10 23:23:01 +00006199static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006200posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006201{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006202 pid_t pid;
6203 int pgrp;
6204 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6205 return NULL;
6206 if (setpgid(pid, pgrp) < 0)
6207 return posix_error();
6208 Py_INCREF(Py_None);
6209 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006210}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006211#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006212
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006213
Guido van Rossumb6775db1994-08-01 11:34:53 +00006214#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006215PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006216"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006217Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006218
Barry Warsaw53699e91996-12-10 23:23:01 +00006219static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006220posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006221{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006222 int fd;
6223 pid_t pgid;
6224 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6225 return NULL;
6226 pgid = tcgetpgrp(fd);
6227 if (pgid < 0)
6228 return posix_error();
6229 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006230}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006231#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006232
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006233
Guido van Rossumb6775db1994-08-01 11:34:53 +00006234#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006235PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006236"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006237Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006238
Barry Warsaw53699e91996-12-10 23:23:01 +00006239static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006240posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006241{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006242 int fd;
6243 pid_t pgid;
6244 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6245 return NULL;
6246 if (tcsetpgrp(fd, pgid) < 0)
6247 return posix_error();
6248 Py_INCREF(Py_None);
6249 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006250}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006251#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006252
Guido van Rossum687dd131993-05-17 08:34:16 +00006253/* Functions acting on file descriptors */
6254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006255PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006256"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006257Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006258
Barry Warsaw53699e91996-12-10 23:23:01 +00006259static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006260posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006261{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006262 char *file = NULL;
6263 int flag;
6264 int mode = 0777;
6265 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006266
6267#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006268 if (unicode_file_names()) {
6269 PyUnicodeObject *po;
6270 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6271 Py_BEGIN_ALLOW_THREADS
6272 /* PyUnicode_AS_UNICODE OK without thread
6273 lock as it is a simple dereference. */
6274 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6275 Py_END_ALLOW_THREADS
6276 if (fd < 0)
6277 return posix_error();
6278 return PyInt_FromLong((long)fd);
6279 }
6280 /* Drop the argument parsing error as narrow strings
6281 are also valid. */
6282 PyErr_Clear();
6283 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006284#endif
6285
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006286 if (!PyArg_ParseTuple(args, "eti|i",
6287 Py_FileSystemDefaultEncoding, &file,
6288 &flag, &mode))
6289 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006290
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006291 Py_BEGIN_ALLOW_THREADS
6292 fd = open(file, flag, mode);
6293 Py_END_ALLOW_THREADS
6294 if (fd < 0)
6295 return posix_error_with_allocated_filename(file);
6296 PyMem_Free(file);
6297 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006298}
6299
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006300
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006301PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006302"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006303Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006304
Barry Warsaw53699e91996-12-10 23:23:01 +00006305static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006306posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006307{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006308 int fd, res;
6309 if (!PyArg_ParseTuple(args, "i:close", &fd))
6310 return NULL;
6311 Py_BEGIN_ALLOW_THREADS
6312 res = close(fd);
6313 Py_END_ALLOW_THREADS
6314 if (res < 0)
6315 return posix_error();
6316 Py_INCREF(Py_None);
6317 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006318}
6319
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006320
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006321PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006322"closerange(fd_low, fd_high)\n\n\
6323Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6324
6325static PyObject *
6326posix_closerange(PyObject *self, PyObject *args)
6327{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006328 int fd_from, fd_to, i;
6329 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6330 return NULL;
6331 Py_BEGIN_ALLOW_THREADS
6332 for (i = fd_from; i < fd_to; i++)
6333 close(i);
6334 Py_END_ALLOW_THREADS
6335 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006336}
6337
6338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006339PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006340"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006341Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006342
Barry Warsaw53699e91996-12-10 23:23:01 +00006343static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006344posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006345{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006346 int fd;
6347 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6348 return NULL;
6349 Py_BEGIN_ALLOW_THREADS
6350 fd = dup(fd);
6351 Py_END_ALLOW_THREADS
6352 if (fd < 0)
6353 return posix_error();
6354 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006355}
6356
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006359"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006360Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006361
Barry Warsaw53699e91996-12-10 23:23:01 +00006362static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006363posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006364{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006365 int fd, fd2, res;
6366 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6367 return NULL;
6368 Py_BEGIN_ALLOW_THREADS
6369 res = dup2(fd, fd2);
6370 Py_END_ALLOW_THREADS
6371 if (res < 0)
6372 return posix_error();
6373 Py_INCREF(Py_None);
6374 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006375}
6376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006377
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006378PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006379"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006380Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006381
Barry Warsaw53699e91996-12-10 23:23:01 +00006382static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006383posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006384{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006385 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006386#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006387 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006388#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006389 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006390#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006391 PyObject *posobj;
6392 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6393 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006394#ifdef SEEK_SET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006395 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6396 switch (how) {
6397 case 0: how = SEEK_SET; break;
6398 case 1: how = SEEK_CUR; break;
6399 case 2: how = SEEK_END; break;
6400 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006401#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006402
6403#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006404 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006405#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006406 pos = PyLong_Check(posobj) ?
6407 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006408#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006409 if (PyErr_Occurred())
6410 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006411
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006412 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006413#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006414 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006415#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006416 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006417#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006418 Py_END_ALLOW_THREADS
6419 if (res < 0)
6420 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006421
6422#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006423 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006424#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006425 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006426#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006427}
6428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006429
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006430PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006431"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006432Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006433
Barry Warsaw53699e91996-12-10 23:23:01 +00006434static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006435posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006436{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006437 int fd, size, n;
6438 PyObject *buffer;
6439 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6440 return NULL;
6441 if (size < 0) {
6442 errno = EINVAL;
6443 return posix_error();
6444 }
6445 buffer = PyString_FromStringAndSize((char *)NULL, size);
6446 if (buffer == NULL)
6447 return NULL;
6448 Py_BEGIN_ALLOW_THREADS
6449 n = read(fd, PyString_AsString(buffer), size);
6450 Py_END_ALLOW_THREADS
6451 if (n < 0) {
6452 Py_DECREF(buffer);
6453 return posix_error();
6454 }
6455 if (n != size)
6456 _PyString_Resize(&buffer, n);
6457 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006458}
6459
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006461PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006462"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006463Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006464
Barry Warsaw53699e91996-12-10 23:23:01 +00006465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006466posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006467{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006468 Py_buffer pbuf;
6469 int fd;
6470 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006471
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006472 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6473 return NULL;
6474 Py_BEGIN_ALLOW_THREADS
6475 size = write(fd, pbuf.buf, (size_t)pbuf.len);
6476 Py_END_ALLOW_THREADS
6477 PyBuffer_Release(&pbuf);
6478 if (size < 0)
6479 return posix_error();
6480 return PyInt_FromSsize_t(size);
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_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006485"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006486Like stat(), but for an open 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_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006490{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006491 int fd;
6492 STRUCT_STAT st;
6493 int res;
6494 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6495 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006496#ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006497 /* on OpenVMS we must ensure that all bytes are written to the file */
6498 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006499#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006500 Py_BEGIN_ALLOW_THREADS
6501 res = FSTAT(fd, &st);
6502 Py_END_ALLOW_THREADS
6503 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006504#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006505 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006506#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006507 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006508#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006509 }
Tim Peters5aa91602002-01-30 05:46:57 +00006510
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006511 return _pystat_fromstructstat(&st);
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_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006516"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006517Return an open file object connected 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_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006521{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006522 int fd;
6523 char *orgmode = "r";
6524 int bufsize = -1;
6525 FILE *fp;
6526 PyObject *f;
6527 char *mode;
6528 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6529 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006530
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006531 /* Sanitize mode. See fileobject.c */
6532 mode = PyMem_MALLOC(strlen(orgmode)+3);
6533 if (!mode) {
6534 PyErr_NoMemory();
6535 return NULL;
6536 }
6537 strcpy(mode, orgmode);
6538 if (_PyFile_SanitizeMode(mode)) {
6539 PyMem_FREE(mode);
6540 return NULL;
6541 }
6542 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006543#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006544 if (mode[0] == 'a') {
6545 /* try to make sure the O_APPEND flag is set */
6546 int flags;
6547 flags = fcntl(fd, F_GETFL);
6548 if (flags != -1)
6549 fcntl(fd, F_SETFL, flags | O_APPEND);
6550 fp = fdopen(fd, mode);
6551 if (fp == NULL && flags != -1)
6552 /* restore old mode if fdopen failed */
6553 fcntl(fd, F_SETFL, flags);
6554 } else {
6555 fp = fdopen(fd, mode);
6556 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006557#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006558 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006559#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006560 Py_END_ALLOW_THREADS
6561 PyMem_FREE(mode);
6562 if (fp == NULL)
6563 return posix_error();
6564 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6565 if (f != NULL)
6566 PyFile_SetBufSize(f, bufsize);
6567 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006568}
6569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006570PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006571"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006572Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006573connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006574
6575static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006576posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006577{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006578 int fd;
6579 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6580 return NULL;
6581 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006582}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006583
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006584#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006585PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006586"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006588
Barry Warsaw53699e91996-12-10 23:23:01 +00006589static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006590posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006591{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006592#if defined(PYOS_OS2)
6593 HFILE read, write;
6594 APIRET rc;
6595
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006596 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006597 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006598 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006599 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006600 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006601
6602 return Py_BuildValue("(ii)", read, write);
6603#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006604#if !defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006605 int fds[2];
6606 int res;
6607 Py_BEGIN_ALLOW_THREADS
6608 res = pipe(fds);
6609 Py_END_ALLOW_THREADS
6610 if (res != 0)
6611 return posix_error();
6612 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006613#else /* MS_WINDOWS */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006614 HANDLE read, write;
6615 int read_fd, write_fd;
6616 BOOL ok;
6617 Py_BEGIN_ALLOW_THREADS
6618 ok = CreatePipe(&read, &write, NULL, 0);
6619 Py_END_ALLOW_THREADS
6620 if (!ok)
6621 return win32_error("CreatePipe", NULL);
6622 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6623 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6624 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006625#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006626#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006627}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006628#endif /* HAVE_PIPE */
6629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006630
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006631#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006632PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006633"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006634Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006635
Barry Warsaw53699e91996-12-10 23:23:01 +00006636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006637posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006638{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006639 char *filename;
6640 int mode = 0666;
6641 int res;
6642 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6643 return NULL;
6644 Py_BEGIN_ALLOW_THREADS
6645 res = mkfifo(filename, mode);
6646 Py_END_ALLOW_THREADS
6647 if (res < 0)
6648 return posix_error();
6649 Py_INCREF(Py_None);
6650 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006651}
6652#endif
6653
6654
Neal Norwitz11690112002-07-30 01:08:28 +00006655#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006657"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006658Create a filesystem node (file, device special file or named pipe)\n\
6659named filename. mode specifies both the permissions to use and the\n\
6660type of node to be created, being combined (bitwise OR) with one of\n\
6661S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006662device defines the newly created device special file (probably using\n\
6663os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006664
6665
6666static PyObject *
6667posix_mknod(PyObject *self, PyObject *args)
6668{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006669 char *filename;
6670 int mode = 0600;
6671 int device = 0;
6672 int res;
6673 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6674 return NULL;
6675 Py_BEGIN_ALLOW_THREADS
6676 res = mknod(filename, mode, device);
6677 Py_END_ALLOW_THREADS
6678 if (res < 0)
6679 return posix_error();
6680 Py_INCREF(Py_None);
6681 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006682}
6683#endif
6684
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006685#ifdef HAVE_DEVICE_MACROS
6686PyDoc_STRVAR(posix_major__doc__,
6687"major(device) -> major number\n\
6688Extracts a device major number from a raw device number.");
6689
6690static PyObject *
6691posix_major(PyObject *self, PyObject *args)
6692{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006693 int device;
6694 if (!PyArg_ParseTuple(args, "i:major", &device))
6695 return NULL;
6696 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006697}
6698
6699PyDoc_STRVAR(posix_minor__doc__,
6700"minor(device) -> minor number\n\
6701Extracts a device minor number from a raw device number.");
6702
6703static PyObject *
6704posix_minor(PyObject *self, PyObject *args)
6705{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006706 int device;
6707 if (!PyArg_ParseTuple(args, "i:minor", &device))
6708 return NULL;
6709 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006710}
6711
6712PyDoc_STRVAR(posix_makedev__doc__,
6713"makedev(major, minor) -> device number\n\
6714Composes a raw device number from the major and minor device numbers.");
6715
6716static PyObject *
6717posix_makedev(PyObject *self, PyObject *args)
6718{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006719 int major, minor;
6720 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6721 return NULL;
6722 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006723}
6724#endif /* device macros */
6725
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006726
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006727#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006728PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006729"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006730Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006731
Barry Warsaw53699e91996-12-10 23:23:01 +00006732static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006733posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006734{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006735 int fd;
6736 off_t length;
6737 int res;
6738 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006739
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006740 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6741 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006742
6743#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006744 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006745#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006746 length = PyLong_Check(lenobj) ?
6747 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006748#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006749 if (PyErr_Occurred())
6750 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006751
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006752 Py_BEGIN_ALLOW_THREADS
6753 res = ftruncate(fd, length);
6754 Py_END_ALLOW_THREADS
6755 if (res < 0) {
6756 PyErr_SetFromErrno(PyExc_IOError);
6757 return NULL;
6758 }
6759 Py_INCREF(Py_None);
6760 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006761}
6762#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006763
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006764#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006765PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006766"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006767Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006768
Fred Drake762e2061999-08-26 17:23:54 +00006769/* Save putenv() parameters as values here, so we can collect them when they
6770 * get re-set with another call for the same key. */
6771static PyObject *posix_putenv_garbage;
6772
Tim Peters5aa91602002-01-30 05:46:57 +00006773static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006774posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006775{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006776 char *s1, *s2;
6777 char *newenv;
6778 PyObject *newstr;
6779 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006780
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006781 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6782 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006783
6784#if defined(PYOS_OS2)
6785 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6786 APIRET rc;
6787
Guido van Rossumd48f2521997-12-05 22:19:34 +00006788 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6789 if (rc != NO_ERROR)
6790 return os2_error(rc);
6791
6792 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6793 APIRET rc;
6794
Guido van Rossumd48f2521997-12-05 22:19:34 +00006795 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6796 if (rc != NO_ERROR)
6797 return os2_error(rc);
6798 } else {
6799#endif
6800
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006801 /* XXX This can leak memory -- not easy to fix :-( */
6802 len = strlen(s1) + strlen(s2) + 2;
6803 /* len includes space for a trailing \0; the size arg to
6804 PyString_FromStringAndSize does not count that */
6805 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6806 if (newstr == NULL)
6807 return PyErr_NoMemory();
6808 newenv = PyString_AS_STRING(newstr);
6809 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6810 if (putenv(newenv)) {
6811 Py_DECREF(newstr);
6812 posix_error();
6813 return NULL;
6814 }
6815 /* Install the first arg and newstr in posix_putenv_garbage;
6816 * this will cause previous value to be collected. This has to
6817 * happen after the real putenv() call because the old value
6818 * was still accessible until then. */
6819 if (PyDict_SetItem(posix_putenv_garbage,
6820 PyTuple_GET_ITEM(args, 0), newstr)) {
6821 /* really not much we can do; just leak */
6822 PyErr_Clear();
6823 }
6824 else {
6825 Py_DECREF(newstr);
6826 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006827
6828#if defined(PYOS_OS2)
6829 }
6830#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006831 Py_INCREF(Py_None);
6832 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006833}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006834#endif /* putenv */
6835
Guido van Rossumc524d952001-10-19 01:31:59 +00006836#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006837PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006838"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006839Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006840
6841static PyObject *
6842posix_unsetenv(PyObject *self, PyObject *args)
6843{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006844 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006845
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006846 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6847 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00006848
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006849 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006850
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006851 /* Remove the key from posix_putenv_garbage;
6852 * this will cause it to be collected. This has to
6853 * happen after the real unsetenv() call because the
6854 * old value was still accessible until then.
6855 */
6856 if (PyDict_DelItem(posix_putenv_garbage,
6857 PyTuple_GET_ITEM(args, 0))) {
6858 /* really not much we can do; just leak */
6859 PyErr_Clear();
6860 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006861
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006862 Py_INCREF(Py_None);
6863 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00006864}
6865#endif /* unsetenv */
6866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006867PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006868"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006869Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006870
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006871static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006872posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006873{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006874 int code;
6875 char *message;
6876 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6877 return NULL;
6878 message = strerror(code);
6879 if (message == NULL) {
6880 PyErr_SetString(PyExc_ValueError,
6881 "strerror() argument out of range");
6882 return NULL;
6883 }
6884 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006885}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006886
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006887
Guido van Rossumc9641791998-08-04 15:26:23 +00006888#ifdef HAVE_SYS_WAIT_H
6889
Fred Drake106c1a02002-04-23 15:58:02 +00006890#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006891PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006892"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006893Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006894
6895static PyObject *
6896posix_WCOREDUMP(PyObject *self, PyObject *args)
6897{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006898 WAIT_TYPE status;
6899 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006900
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006901 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6902 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006903
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006904 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006905}
6906#endif /* WCOREDUMP */
6907
6908#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006909PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006910"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006911Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006912job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006913
6914static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006915posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006916{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006917 WAIT_TYPE status;
6918 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006919
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006920 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6921 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006922
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006923 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006924}
6925#endif /* WIFCONTINUED */
6926
Guido van Rossumc9641791998-08-04 15:26:23 +00006927#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006928PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006929"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006930Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006931
6932static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006933posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006934{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006935 WAIT_TYPE status;
6936 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006937
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006938 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6939 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006940
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006941 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006942}
6943#endif /* WIFSTOPPED */
6944
6945#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006946PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006947"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006948Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006949
6950static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006951posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006952{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006953 WAIT_TYPE status;
6954 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006955
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006956 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6957 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006958
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006959 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006960}
6961#endif /* WIFSIGNALED */
6962
6963#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006964PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006965"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006966Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006967system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006968
6969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006970posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006971{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006972 WAIT_TYPE status;
6973 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006974
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006975 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6976 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006977
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006978 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006979}
6980#endif /* WIFEXITED */
6981
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006982#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006983PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006984"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006985Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006986
6987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006988posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006989{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006990 WAIT_TYPE status;
6991 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006992
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006993 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6994 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006995
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006996 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006997}
6998#endif /* WEXITSTATUS */
6999
7000#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007001PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007002"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007003Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007004value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007005
7006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007007posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007008{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007009 WAIT_TYPE status;
7010 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007011
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007012 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7013 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007014
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007015 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007016}
7017#endif /* WTERMSIG */
7018
7019#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007020PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007021"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007022Return the signal that stopped the process that provided\n\
7023the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007024
7025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007026posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007027{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007028 WAIT_TYPE status;
7029 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007030
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007031 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7032 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007033
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007034 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007035}
7036#endif /* WSTOPSIG */
7037
7038#endif /* HAVE_SYS_WAIT_H */
7039
7040
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007041#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007042#ifdef _SCO_DS
7043/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7044 needed definitions in sys/statvfs.h */
7045#define _SVID3
7046#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007047#include <sys/statvfs.h>
7048
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007049static PyObject*
7050_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007051 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7052 if (v == NULL)
7053 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007054
7055#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007056 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7057 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7058 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7059 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7060 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7061 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7062 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7063 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7064 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7065 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007066#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007067 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7068 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7069 PyStructSequence_SET_ITEM(v, 2,
7070 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7071 PyStructSequence_SET_ITEM(v, 3,
7072 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7073 PyStructSequence_SET_ITEM(v, 4,
7074 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7075 PyStructSequence_SET_ITEM(v, 5,
7076 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7077 PyStructSequence_SET_ITEM(v, 6,
7078 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7079 PyStructSequence_SET_ITEM(v, 7,
7080 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7081 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7082 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007083#endif
7084
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007085 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007086}
7087
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007088PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007089"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007090Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007091
7092static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007093posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007094{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007095 int fd, res;
7096 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007097
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007098 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7099 return NULL;
7100 Py_BEGIN_ALLOW_THREADS
7101 res = fstatvfs(fd, &st);
7102 Py_END_ALLOW_THREADS
7103 if (res != 0)
7104 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007105
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007106 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007107}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007108#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007109
7110
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007111#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007112#include <sys/statvfs.h>
7113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007114PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007115"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007116Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007117
7118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007119posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007120{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007121 char *path;
7122 int res;
7123 struct statvfs st;
7124 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7125 return NULL;
7126 Py_BEGIN_ALLOW_THREADS
7127 res = statvfs(path, &st);
7128 Py_END_ALLOW_THREADS
7129 if (res != 0)
7130 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007131
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007132 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007133}
7134#endif /* HAVE_STATVFS */
7135
7136
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007137#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007138PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007139"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007140Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007141The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007142or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007143
7144static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007145posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007146{
7147 PyObject *result = NULL;
7148 char *dir = NULL;
7149 char *pfx = NULL;
7150 char *name;
7151
7152 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007153 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007154
7155 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007156 "tempnam is a potential security risk to your program") < 0)
7157 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007158
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007159#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007160 name = _tempnam(dir, pfx);
7161#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007162 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007163#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007164 if (name == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007165 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007166 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007167 free(name);
7168 return result;
7169}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007170#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007171
7172
7173#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007174PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007175"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007176Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007177
7178static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007179posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007180{
7181 FILE *fp;
7182
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007183 fp = tmpfile();
7184 if (fp == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007185 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007186 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007187}
7188#endif
7189
7190
7191#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007192PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007193"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007194Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007195
7196static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007197posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007198{
7199 char buffer[L_tmpnam];
7200 char *name;
7201
Skip Montanaro95618b52001-08-18 18:52:10 +00007202 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007203 "tmpnam is a potential security risk to your program") < 0)
7204 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007205
Greg Wardb48bc172000-03-01 21:51:56 +00007206#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007207 name = tmpnam_r(buffer);
7208#else
7209 name = tmpnam(buffer);
7210#endif
7211 if (name == NULL) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007212 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007213#ifdef USE_TMPNAM_R
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007214 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007215#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007216 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007217#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007218 );
7219 PyErr_SetObject(PyExc_OSError, err);
7220 Py_XDECREF(err);
7221 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007222 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007223 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007224}
7225#endif
7226
7227
Fred Drakec9680921999-12-13 16:37:25 +00007228/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7229 * It maps strings representing configuration variable names to
7230 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007231 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007232 * rarely-used constants. There are three separate tables that use
7233 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007234 *
7235 * This code is always included, even if none of the interfaces that
7236 * need it are included. The #if hackery needed to avoid it would be
7237 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007238 */
7239struct constdef {
7240 char *name;
7241 long value;
7242};
7243
Fred Drake12c6e2d1999-12-14 21:25:03 +00007244static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007245conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007246 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007247{
7248 if (PyInt_Check(arg)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007249 *valuep = PyInt_AS_LONG(arg);
7250 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007251 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007252 if (PyString_Check(arg)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007253 /* look up the value in the table using a binary search */
7254 size_t lo = 0;
7255 size_t mid;
7256 size_t hi = tablesize;
7257 int cmp;
7258 char *confname = PyString_AS_STRING(arg);
7259 while (lo < hi) {
7260 mid = (lo + hi) / 2;
7261 cmp = strcmp(confname, table[mid].name);
7262 if (cmp < 0)
7263 hi = mid;
7264 else if (cmp > 0)
7265 lo = mid + 1;
7266 else {
7267 *valuep = table[mid].value;
7268 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007269 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007270 }
7271 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007272 }
7273 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007274 PyErr_SetString(PyExc_TypeError,
7275 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007276 return 0;
7277}
7278
7279
7280#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7281static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007282#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007283 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007284#endif
7285#ifdef _PC_ABI_ASYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007286 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007287#endif
Fred Drakec9680921999-12-13 16:37:25 +00007288#ifdef _PC_ASYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007289 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007290#endif
7291#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007292 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007293#endif
7294#ifdef _PC_FILESIZEBITS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007295 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007296#endif
7297#ifdef _PC_LAST
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007298 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007299#endif
7300#ifdef _PC_LINK_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007301 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007302#endif
7303#ifdef _PC_MAX_CANON
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007304 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007305#endif
7306#ifdef _PC_MAX_INPUT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007307 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007308#endif
7309#ifdef _PC_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007310 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007311#endif
7312#ifdef _PC_NO_TRUNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007313 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007314#endif
7315#ifdef _PC_PATH_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007316 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007317#endif
7318#ifdef _PC_PIPE_BUF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007319 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007320#endif
7321#ifdef _PC_PRIO_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007322 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007323#endif
7324#ifdef _PC_SOCK_MAXBUF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007325 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007326#endif
7327#ifdef _PC_SYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007328 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007329#endif
7330#ifdef _PC_VDISABLE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007331 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007332#endif
7333};
7334
Fred Drakec9680921999-12-13 16:37:25 +00007335static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007336conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007337{
7338 return conv_confname(arg, valuep, posix_constants_pathconf,
7339 sizeof(posix_constants_pathconf)
7340 / sizeof(struct constdef));
7341}
7342#endif
7343
7344#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007345PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007346"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007347Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007348If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007349
7350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007351posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007352{
7353 PyObject *result = NULL;
7354 int name, fd;
7355
Fred Drake12c6e2d1999-12-14 21:25:03 +00007356 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7357 conv_path_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007358 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007359
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007360 errno = 0;
7361 limit = fpathconf(fd, name);
7362 if (limit == -1 && errno != 0)
7363 posix_error();
7364 else
7365 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007366 }
7367 return result;
7368}
7369#endif
7370
7371
7372#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007373PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007374"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007375Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007376If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007377
7378static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007379posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007380{
7381 PyObject *result = NULL;
7382 int name;
7383 char *path;
7384
7385 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7386 conv_path_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007387 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007388
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007389 errno = 0;
7390 limit = pathconf(path, name);
7391 if (limit == -1 && errno != 0) {
7392 if (errno == EINVAL)
7393 /* could be a path or name problem */
7394 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007395 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007396 posix_error_with_filename(path);
7397 }
7398 else
7399 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007400 }
7401 return result;
7402}
7403#endif
7404
7405#ifdef HAVE_CONFSTR
7406static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007407#ifdef _CS_ARCHITECTURE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007408 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007409#endif
7410#ifdef _CS_HOSTNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007411 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007412#endif
7413#ifdef _CS_HW_PROVIDER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007414 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007415#endif
7416#ifdef _CS_HW_SERIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007417 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007418#endif
7419#ifdef _CS_INITTAB_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007420 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007421#endif
Fred Drakec9680921999-12-13 16:37:25 +00007422#ifdef _CS_LFS64_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007423 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007424#endif
7425#ifdef _CS_LFS64_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007426 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007427#endif
7428#ifdef _CS_LFS64_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007429 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007430#endif
7431#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007432 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007433#endif
7434#ifdef _CS_LFS_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007435 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007436#endif
7437#ifdef _CS_LFS_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007438 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007439#endif
7440#ifdef _CS_LFS_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007441 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007442#endif
7443#ifdef _CS_LFS_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007444 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007445#endif
Fred Draked86ed291999-12-15 15:34:33 +00007446#ifdef _CS_MACHINE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007447 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007448#endif
Fred Drakec9680921999-12-13 16:37:25 +00007449#ifdef _CS_PATH
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007450 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007451#endif
Fred Draked86ed291999-12-15 15:34:33 +00007452#ifdef _CS_RELEASE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007453 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007454#endif
7455#ifdef _CS_SRPC_DOMAIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007456 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007457#endif
7458#ifdef _CS_SYSNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007459 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007460#endif
7461#ifdef _CS_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007462 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007463#endif
Fred Drakec9680921999-12-13 16:37:25 +00007464#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007465 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007466#endif
7467#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007468 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007469#endif
7470#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007471 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007472#endif
7473#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007474 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007475#endif
7476#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007477 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007478#endif
7479#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007480 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007481#endif
7482#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007483 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007484#endif
7485#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007486 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007487#endif
7488#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007489 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007490#endif
7491#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007492 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007493#endif
7494#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007495 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007496#endif
7497#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007498 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007499#endif
7500#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007501 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007502#endif
7503#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007504 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007505#endif
7506#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007507 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007508#endif
7509#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007510 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007511#endif
Fred Draked86ed291999-12-15 15:34:33 +00007512#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007513 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007514#endif
7515#ifdef _MIPS_CS_BASE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007516 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007517#endif
7518#ifdef _MIPS_CS_HOSTID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007519 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007520#endif
7521#ifdef _MIPS_CS_HW_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007522 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007523#endif
7524#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007525 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007526#endif
7527#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007528 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007529#endif
7530#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007531 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007532#endif
7533#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007534 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007535#endif
7536#ifdef _MIPS_CS_OS_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007537 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007538#endif
7539#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007540 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007541#endif
7542#ifdef _MIPS_CS_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007543 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007544#endif
7545#ifdef _MIPS_CS_SERIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007546 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007547#endif
7548#ifdef _MIPS_CS_VENDOR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007549 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007550#endif
Fred Drakec9680921999-12-13 16:37:25 +00007551};
7552
7553static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007554conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007555{
7556 return conv_confname(arg, valuep, posix_constants_confstr,
7557 sizeof(posix_constants_confstr)
7558 / sizeof(struct constdef));
7559}
7560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007561PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007562"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007563Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007564
7565static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007566posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007567{
7568 PyObject *result = NULL;
7569 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007570 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007571
7572 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007573 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007574
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007575 errno = 0;
7576 len = confstr(name, buffer, sizeof(buffer));
7577 if (len == 0) {
7578 if (errno) {
7579 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007580 }
7581 else {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007582 result = Py_None;
7583 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007584 }
7585 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007586 else {
7587 if ((unsigned int)len >= sizeof(buffer)) {
7588 result = PyString_FromStringAndSize(NULL, len-1);
7589 if (result != NULL)
7590 confstr(name, PyString_AS_STRING(result), len);
7591 }
7592 else
7593 result = PyString_FromStringAndSize(buffer, len-1);
7594 }
7595 }
Fred Drakec9680921999-12-13 16:37:25 +00007596 return result;
7597}
7598#endif
7599
7600
7601#ifdef HAVE_SYSCONF
7602static struct constdef posix_constants_sysconf[] = {
7603#ifdef _SC_2_CHAR_TERM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007604 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007605#endif
7606#ifdef _SC_2_C_BIND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007607 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007608#endif
7609#ifdef _SC_2_C_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007610 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007611#endif
7612#ifdef _SC_2_C_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007613 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007614#endif
7615#ifdef _SC_2_FORT_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007616 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007617#endif
7618#ifdef _SC_2_FORT_RUN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007619 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007620#endif
7621#ifdef _SC_2_LOCALEDEF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007622 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007623#endif
7624#ifdef _SC_2_SW_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007625 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007626#endif
7627#ifdef _SC_2_UPE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007628 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007629#endif
7630#ifdef _SC_2_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007631 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007632#endif
Fred Draked86ed291999-12-15 15:34:33 +00007633#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007634 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007635#endif
7636#ifdef _SC_ACL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007637 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007638#endif
Fred Drakec9680921999-12-13 16:37:25 +00007639#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007640 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007641#endif
Fred Drakec9680921999-12-13 16:37:25 +00007642#ifdef _SC_AIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007643 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007644#endif
7645#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007646 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007647#endif
7648#ifdef _SC_ARG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007649 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007650#endif
7651#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007652 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007653#endif
7654#ifdef _SC_ATEXIT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007655 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007656#endif
Fred Draked86ed291999-12-15 15:34:33 +00007657#ifdef _SC_AUDIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007658 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007659#endif
Fred Drakec9680921999-12-13 16:37:25 +00007660#ifdef _SC_AVPHYS_PAGES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007661 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007662#endif
7663#ifdef _SC_BC_BASE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007664 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007665#endif
7666#ifdef _SC_BC_DIM_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007667 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007668#endif
7669#ifdef _SC_BC_SCALE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007670 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007671#endif
7672#ifdef _SC_BC_STRING_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007673 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007674#endif
Fred Draked86ed291999-12-15 15:34:33 +00007675#ifdef _SC_CAP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007676 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007677#endif
Fred Drakec9680921999-12-13 16:37:25 +00007678#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007679 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007680#endif
7681#ifdef _SC_CHAR_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007682 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007683#endif
7684#ifdef _SC_CHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007685 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007686#endif
7687#ifdef _SC_CHAR_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007688 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007689#endif
7690#ifdef _SC_CHILD_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007691 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007692#endif
7693#ifdef _SC_CLK_TCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007694 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007695#endif
7696#ifdef _SC_COHER_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007697 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007698#endif
7699#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007700 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007701#endif
7702#ifdef _SC_DCACHE_ASSOC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007703 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007704#endif
7705#ifdef _SC_DCACHE_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007706 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007707#endif
7708#ifdef _SC_DCACHE_LINESZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007709 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007710#endif
7711#ifdef _SC_DCACHE_SZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007712 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007713#endif
7714#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007715 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007716#endif
7717#ifdef _SC_DELAYTIMER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007718 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007719#endif
7720#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007721 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007722#endif
7723#ifdef _SC_EXPR_NEST_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007724 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007725#endif
7726#ifdef _SC_FSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007727 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007728#endif
7729#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007730 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007731#endif
7732#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007733 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007734#endif
7735#ifdef _SC_ICACHE_ASSOC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007736 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007737#endif
7738#ifdef _SC_ICACHE_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007739 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007740#endif
7741#ifdef _SC_ICACHE_LINESZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007742 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007743#endif
7744#ifdef _SC_ICACHE_SZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007745 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007746#endif
Fred Draked86ed291999-12-15 15:34:33 +00007747#ifdef _SC_INF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007748 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007749#endif
Fred Drakec9680921999-12-13 16:37:25 +00007750#ifdef _SC_INT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007751 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007752#endif
7753#ifdef _SC_INT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007754 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007755#endif
7756#ifdef _SC_IOV_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007757 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007758#endif
Fred Draked86ed291999-12-15 15:34:33 +00007759#ifdef _SC_IP_SECOPTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007760 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007761#endif
Fred Drakec9680921999-12-13 16:37:25 +00007762#ifdef _SC_JOB_CONTROL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007763 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007764#endif
Fred Draked86ed291999-12-15 15:34:33 +00007765#ifdef _SC_KERN_POINTERS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007766 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007767#endif
7768#ifdef _SC_KERN_SIM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007769 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007770#endif
Fred Drakec9680921999-12-13 16:37:25 +00007771#ifdef _SC_LINE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007772 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007773#endif
7774#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007775 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007776#endif
7777#ifdef _SC_LOGNAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007778 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007779#endif
7780#ifdef _SC_LONG_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007781 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007782#endif
Fred Draked86ed291999-12-15 15:34:33 +00007783#ifdef _SC_MAC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007784 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007785#endif
Fred Drakec9680921999-12-13 16:37:25 +00007786#ifdef _SC_MAPPED_FILES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007787 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _SC_MAXPID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007790 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _SC_MB_LEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007793 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _SC_MEMLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007796 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _SC_MEMLOCK_RANGE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007799 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
7801#ifdef _SC_MEMORY_PROTECTION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007802 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007803#endif
7804#ifdef _SC_MESSAGE_PASSING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007805 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
Fred Draked86ed291999-12-15 15:34:33 +00007807#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007808 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007809#endif
Fred Drakec9680921999-12-13 16:37:25 +00007810#ifdef _SC_MQ_OPEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007811 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007812#endif
7813#ifdef _SC_MQ_PRIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007814 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007815#endif
Fred Draked86ed291999-12-15 15:34:33 +00007816#ifdef _SC_NACLS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007817 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007818#endif
Fred Drakec9680921999-12-13 16:37:25 +00007819#ifdef _SC_NGROUPS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007820 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007821#endif
7822#ifdef _SC_NL_ARGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007823 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007824#endif
7825#ifdef _SC_NL_LANGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007826 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007827#endif
7828#ifdef _SC_NL_MSGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007829 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _SC_NL_NMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007832 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _SC_NL_SETMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007835 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
7837#ifdef _SC_NL_TEXTMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007838 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007839#endif
7840#ifdef _SC_NPROCESSORS_CONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007841 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007842#endif
7843#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007844 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
Fred Draked86ed291999-12-15 15:34:33 +00007846#ifdef _SC_NPROC_CONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007847 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007848#endif
7849#ifdef _SC_NPROC_ONLN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007850 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007851#endif
Fred Drakec9680921999-12-13 16:37:25 +00007852#ifdef _SC_NZERO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007853 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007854#endif
7855#ifdef _SC_OPEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007856 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007857#endif
7858#ifdef _SC_PAGESIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007859 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007860#endif
7861#ifdef _SC_PAGE_SIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007862 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007863#endif
7864#ifdef _SC_PASS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007865 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007866#endif
7867#ifdef _SC_PHYS_PAGES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007868 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007869#endif
7870#ifdef _SC_PII
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007871 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007872#endif
7873#ifdef _SC_PII_INTERNET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007874 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007875#endif
7876#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007877 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007878#endif
7879#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007880 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007881#endif
7882#ifdef _SC_PII_OSI
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007883 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007884#endif
7885#ifdef _SC_PII_OSI_CLTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007886 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007887#endif
7888#ifdef _SC_PII_OSI_COTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007889 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
7891#ifdef _SC_PII_OSI_M
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007892 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007893#endif
7894#ifdef _SC_PII_SOCKET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007895 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007896#endif
7897#ifdef _SC_PII_XTI
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007898 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007899#endif
7900#ifdef _SC_POLL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007901 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007902#endif
7903#ifdef _SC_PRIORITIZED_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007904 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007905#endif
7906#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007907 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007908#endif
7909#ifdef _SC_REALTIME_SIGNALS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007910 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007911#endif
7912#ifdef _SC_RE_DUP_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007913 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007914#endif
7915#ifdef _SC_RTSIG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007916 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007917#endif
7918#ifdef _SC_SAVED_IDS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007919 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_SCHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007922 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
7924#ifdef _SC_SCHAR_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007925 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007926#endif
7927#ifdef _SC_SELECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007928 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_SEMAPHORES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007931 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
7933#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007934 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007935#endif
7936#ifdef _SC_SEM_VALUE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007937 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
7939#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007940 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007941#endif
7942#ifdef _SC_SHRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007943 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007944#endif
7945#ifdef _SC_SHRT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007946 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007947#endif
7948#ifdef _SC_SIGQUEUE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007949 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007950#endif
7951#ifdef _SC_SIGRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007952 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
7954#ifdef _SC_SIGRT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007955 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007956#endif
Fred Draked86ed291999-12-15 15:34:33 +00007957#ifdef _SC_SOFTPOWER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007958 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007959#endif
Fred Drakec9680921999-12-13 16:37:25 +00007960#ifdef _SC_SPLIT_CACHE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007961 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_SSIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007964 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_STACK_PROT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007967 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
7969#ifdef _SC_STREAM_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007970 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007971#endif
7972#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007973 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007974#endif
7975#ifdef _SC_THREADS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007976 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007977#endif
7978#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007979 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007982 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007985 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
7987#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007988 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007989#endif
7990#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007991 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007994 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007997 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008000 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008003 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_THREAD_STACK_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008006 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008009 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef _SC_TIMERS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008012 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
8014#ifdef _SC_TIMER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008015 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_TTY_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008018 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_TZNAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008021 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_T_IOV_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008024 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_UCHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008027 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_UINT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008030 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_UIO_MAXIOV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008033 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_ULONG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008036 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_USHRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008039 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008042 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_WORD_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008045 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008048 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008051 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
8053#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008054 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
8056#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008057 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008058#endif
8059#ifdef _SC_XOPEN_CRYPT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008060 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008061#endif
8062#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008063 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
8065#ifdef _SC_XOPEN_LEGACY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008066 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008067#endif
8068#ifdef _SC_XOPEN_REALTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008069 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
8071#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008072 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008073#endif
8074#ifdef _SC_XOPEN_SHM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008075 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
8077#ifdef _SC_XOPEN_UNIX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008078 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef _SC_XOPEN_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008081 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008084 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef _SC_XOPEN_XPG2
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008087 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_XOPEN_XPG3
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008090 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_XOPEN_XPG4
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008093 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095};
8096
8097static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008098conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008099{
8100 return conv_confname(arg, valuep, posix_constants_sysconf,
8101 sizeof(posix_constants_sysconf)
8102 / sizeof(struct constdef));
8103}
8104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008105PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008106"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008107Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008108
8109static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008110posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008111{
8112 PyObject *result = NULL;
8113 int name;
8114
8115 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8116 int value;
8117
8118 errno = 0;
8119 value = sysconf(name);
8120 if (value == -1 && errno != 0)
8121 posix_error();
8122 else
8123 result = PyInt_FromLong(value);
8124 }
8125 return result;
8126}
8127#endif
8128
8129
Fred Drakebec628d1999-12-15 18:31:10 +00008130/* This code is used to ensure that the tables of configuration value names
8131 * are in sorted order as required by conv_confname(), and also to build the
8132 * the exported dictionaries that are used to publish information about the
8133 * names available on the host platform.
8134 *
8135 * Sorting the table at runtime ensures that the table is properly ordered
8136 * when used, even for platforms we're not able to test on. It also makes
8137 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008138 */
Fred Drakebec628d1999-12-15 18:31:10 +00008139
8140static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008141cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008142{
8143 const struct constdef *c1 =
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008144 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008145 const struct constdef *c2 =
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008146 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008147
8148 return strcmp(c1->name, c2->name);
8149}
8150
8151static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008152setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008153 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008154{
Fred Drakebec628d1999-12-15 18:31:10 +00008155 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008156 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008157
8158 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8159 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008160 if (d == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008161 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008162
Barry Warsaw3155db32000-04-13 15:20:40 +00008163 for (i=0; i < tablesize; ++i) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008164 PyObject *o = PyInt_FromLong(table[i].value);
8165 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8166 Py_XDECREF(o);
8167 Py_DECREF(d);
8168 return -1;
8169 }
8170 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008171 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008172 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008173}
8174
Fred Drakebec628d1999-12-15 18:31:10 +00008175/* Return -1 on failure, 0 on success. */
8176static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008177setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008178{
8179#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008180 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008181 sizeof(posix_constants_pathconf)
8182 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008183 "pathconf_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008184 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008185#endif
8186#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008187 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008188 sizeof(posix_constants_confstr)
8189 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008190 "confstr_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008191 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008192#endif
8193#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008194 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008195 sizeof(posix_constants_sysconf)
8196 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008197 "sysconf_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008198 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008199#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008200 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008201}
Fred Draked86ed291999-12-15 15:34:33 +00008202
8203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008204PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008205"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008206Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008207in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008208
8209static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008210posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008211{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008212 abort();
8213 /*NOTREACHED*/
8214 Py_FatalError("abort() called from Python code didn't abort!");
8215 return NULL;
8216}
Fred Drakebec628d1999-12-15 18:31:10 +00008217
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008218#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008219PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008220"startfile(filepath [, operation]) - Start a file with its associated\n\
8221application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008222\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008223When \"operation\" is not specified or \"open\", this acts like\n\
8224double-clicking the file in Explorer, or giving the file name as an\n\
8225argument to the DOS \"start\" command: the file is opened with whatever\n\
8226application (if any) its extension is associated.\n\
8227When another \"operation\" is given, it specifies what should be done with\n\
8228the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008229\n\
8230startfile returns as soon as the associated application is launched.\n\
8231There is no option to wait for the application to close, and no way\n\
8232to retrieve the application's exit status.\n\
8233\n\
8234The filepath is relative to the current directory. If you want to use\n\
8235an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008236the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008237
8238static PyObject *
8239win32_startfile(PyObject *self, PyObject *args)
8240{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008241 char *filepath;
8242 char *operation = NULL;
8243 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00008244#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008245 if (unicode_file_names()) {
8246 PyObject *unipath, *woperation = NULL;
8247 if (!PyArg_ParseTuple(args, "U|s:startfile",
8248 &unipath, &operation)) {
8249 PyErr_Clear();
8250 goto normal;
8251 }
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008252
Georg Brandlad89dc82006-04-03 12:26:26 +00008253
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008254 if (operation) {
8255 woperation = PyUnicode_DecodeASCII(operation,
8256 strlen(operation), NULL);
8257 if (!woperation) {
8258 PyErr_Clear();
8259 operation = NULL;
8260 goto normal;
8261 }
8262 }
8263
8264 Py_BEGIN_ALLOW_THREADS
8265 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8266 PyUnicode_AS_UNICODE(unipath),
8267 NULL, NULL, SW_SHOWNORMAL);
8268 Py_END_ALLOW_THREADS
8269
8270 Py_XDECREF(woperation);
8271 if (rc <= (HINSTANCE)32) {
8272 PyObject *errval = win32_error_unicode("startfile",
8273 PyUnicode_AS_UNICODE(unipath));
8274 return errval;
8275 }
8276 Py_INCREF(Py_None);
8277 return Py_None;
8278 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008279#endif
8280
8281normal:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008282 if (!PyArg_ParseTuple(args, "et|s:startfile",
8283 Py_FileSystemDefaultEncoding, &filepath,
8284 &operation))
8285 return NULL;
8286 Py_BEGIN_ALLOW_THREADS
8287 rc = ShellExecute((HWND)0, operation, filepath,
8288 NULL, NULL, SW_SHOWNORMAL);
8289 Py_END_ALLOW_THREADS
8290 if (rc <= (HINSTANCE)32) {
8291 PyObject *errval = win32_error("startfile", filepath);
8292 PyMem_Free(filepath);
8293 return errval;
8294 }
8295 PyMem_Free(filepath);
8296 Py_INCREF(Py_None);
8297 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008298}
8299#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008300
Martin v. Löwis438b5342002-12-27 10:16:42 +00008301#ifdef HAVE_GETLOADAVG
8302PyDoc_STRVAR(posix_getloadavg__doc__,
8303"getloadavg() -> (float, float, float)\n\n\
8304Return the number of processes in the system run queue averaged over\n\
8305the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8306was unobtainable");
8307
8308static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008309posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008310{
8311 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008312 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008313 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8314 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008315 } else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008316 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008317}
8318#endif
8319
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008320#ifdef MS_WINDOWS
8321
8322PyDoc_STRVAR(win32_urandom__doc__,
8323"urandom(n) -> str\n\n\
8324Return a string of n random bytes suitable for cryptographic use.");
8325
8326typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8327 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8328 DWORD dwFlags );
8329typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8330 BYTE *pbBuffer );
8331
8332static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008333/* This handle is never explicitly released. Instead, the operating
8334 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008335static HCRYPTPROV hCryptProv = 0;
8336
Tim Peters4ad82172004-08-30 17:02:04 +00008337static PyObject*
8338win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008339{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008340 int howMany;
8341 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008342
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008343 /* Read arguments */
8344 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8345 return NULL;
8346 if (howMany < 0)
8347 return PyErr_Format(PyExc_ValueError,
8348 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008349
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008350 if (hCryptProv == 0) {
8351 HINSTANCE hAdvAPI32 = NULL;
8352 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008353
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008354 /* Obtain handle to the DLL containing CryptoAPI
8355 This should not fail */
8356 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8357 if(hAdvAPI32 == NULL)
8358 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008359
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008360 /* Obtain pointers to the CryptoAPI functions
8361 This will fail on some early versions of Win95 */
8362 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8363 hAdvAPI32,
8364 "CryptAcquireContextA");
8365 if (pCryptAcquireContext == NULL)
8366 return PyErr_Format(PyExc_NotImplementedError,
8367 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008368
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008369 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8370 hAdvAPI32, "CryptGenRandom");
8371 if (pCryptGenRandom == NULL)
8372 return PyErr_Format(PyExc_NotImplementedError,
8373 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008374
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008375 /* Acquire context */
8376 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8377 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8378 return win32_error("CryptAcquireContext", NULL);
8379 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008380
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008381 /* Allocate bytes */
8382 result = PyString_FromStringAndSize(NULL, howMany);
8383 if (result != NULL) {
8384 /* Get random data */
8385 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
8386 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8387 PyString_AS_STRING(result))) {
8388 Py_DECREF(result);
8389 return win32_error("CryptGenRandom", NULL);
8390 }
8391 }
8392 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008393}
8394#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008395
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008396#ifdef __VMS
8397/* Use openssl random routine */
8398#include <openssl/rand.h>
8399PyDoc_STRVAR(vms_urandom__doc__,
8400"urandom(n) -> str\n\n\
8401Return a string of n random bytes suitable for cryptographic use.");
8402
8403static PyObject*
8404vms_urandom(PyObject *self, PyObject *args)
8405{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008406 int howMany;
8407 PyObject* result;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008408
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008409 /* Read arguments */
8410 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8411 return NULL;
8412 if (howMany < 0)
8413 return PyErr_Format(PyExc_ValueError,
8414 "negative argument not allowed");
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008415
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008416 /* Allocate bytes */
8417 result = PyString_FromStringAndSize(NULL, howMany);
8418 if (result != NULL) {
8419 /* Get random data */
8420 if (RAND_pseudo_bytes((unsigned char*)
8421 PyString_AS_STRING(result),
8422 howMany) < 0) {
8423 Py_DECREF(result);
8424 return PyErr_Format(PyExc_ValueError,
8425 "RAND_pseudo_bytes");
8426 }
8427 }
8428 return result;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008429}
8430#endif
8431
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008432static PyMethodDef posix_methods[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008433 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008434#ifdef HAVE_TTYNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008435 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008436#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008437 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008438#ifdef HAVE_CHFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008439 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008440#endif /* HAVE_CHFLAGS */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008441 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008442#ifdef HAVE_FCHMOD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008443 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008444#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008445#ifdef HAVE_CHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008446 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008447#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008448#ifdef HAVE_LCHMOD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008449 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008450#endif /* HAVE_LCHMOD */
8451#ifdef HAVE_FCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008452 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008453#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008454#ifdef HAVE_LCHFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008455 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008456#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008457#ifdef HAVE_LCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008458 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008459#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008460#ifdef HAVE_CHROOT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008461 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008462#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008463#ifdef HAVE_CTERMID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008464 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008465#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008466#ifdef HAVE_GETCWD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008467 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008468#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008469 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008470#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008471#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008472#ifdef HAVE_LINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008473 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008474#endif /* HAVE_LINK */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008475 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8476 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8477 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008478#ifdef HAVE_NICE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008479 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008480#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008481#ifdef HAVE_READLINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008482 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008483#endif /* HAVE_READLINK */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008484 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8485 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8486 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8487 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008488#ifdef HAVE_SYMLINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008489 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008490#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008491#ifdef HAVE_SYSTEM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008492 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008493#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008494 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008495#ifdef HAVE_UNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008496 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008497#endif /* HAVE_UNAME */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008498 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8499 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8500 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008501#ifdef HAVE_TIMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008502 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008503#endif /* HAVE_TIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008504 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008505#ifdef HAVE_EXECV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008506 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8507 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008508#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008509#ifdef HAVE_SPAWNV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008510 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8511 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008512#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008513 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8514 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008515#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008516#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008517#ifdef HAVE_FORK1
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008518 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008519#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008520#ifdef HAVE_FORK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008521 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008522#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008523#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008524 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008525#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008526#ifdef HAVE_FORKPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008527 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008528#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008529#ifdef HAVE_GETEGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008530 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008531#endif /* HAVE_GETEGID */
8532#ifdef HAVE_GETEUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008533 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008534#endif /* HAVE_GETEUID */
8535#ifdef HAVE_GETGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008536 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008537#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008538#ifdef HAVE_GETGROUPS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008539 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008540#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008541 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008542#ifdef HAVE_GETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008543 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008544#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008545#ifdef HAVE_GETPPID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008546 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008547#endif /* HAVE_GETPPID */
8548#ifdef HAVE_GETUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008549 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008550#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008551#ifdef HAVE_GETLOGIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008552 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008553#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008554#ifdef HAVE_KILL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008555 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008556#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008557#ifdef HAVE_KILLPG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008558 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008559#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008560#ifdef HAVE_PLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008561 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008562#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008563#ifdef HAVE_POPEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008564 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008565#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008566 {"popen2", win32_popen2, METH_VARARGS},
8567 {"popen3", win32_popen3, METH_VARARGS},
8568 {"popen4", win32_popen4, METH_VARARGS},
8569 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008570#else
8571#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008572 {"popen2", os2emx_popen2, METH_VARARGS},
8573 {"popen3", os2emx_popen3, METH_VARARGS},
8574 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008575#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008576#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008577#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008578#ifdef HAVE_SETUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008579 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008580#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008581#ifdef HAVE_SETEUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008582 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008583#endif /* HAVE_SETEUID */
8584#ifdef HAVE_SETEGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008585 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008586#endif /* HAVE_SETEGID */
8587#ifdef HAVE_SETREUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008588 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008589#endif /* HAVE_SETREUID */
8590#ifdef HAVE_SETREGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008591 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008592#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008593#ifdef HAVE_SETGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008594 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008595#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008596#ifdef HAVE_SETGROUPS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008597 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008598#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008599#ifdef HAVE_GETPGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008600 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008601#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008602#ifdef HAVE_SETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008603 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008604#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008605#ifdef HAVE_WAIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008606 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008607#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008608#ifdef HAVE_WAIT3
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008609 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008610#endif /* HAVE_WAIT3 */
8611#ifdef HAVE_WAIT4
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008612 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008613#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008614#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008615 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008616#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008617#ifdef HAVE_GETSID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008618 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008619#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008620#ifdef HAVE_SETSID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008621 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008622#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008623#ifdef HAVE_SETPGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008624 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008625#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008626#ifdef HAVE_TCGETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008627 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008628#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008629#ifdef HAVE_TCSETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008630 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008631#endif /* HAVE_TCSETPGRP */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008632 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8633 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8634 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8635 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8636 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8637 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8638 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8639 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8640 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8641 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8642 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008643#ifdef HAVE_PIPE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008644 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008645#endif
8646#ifdef HAVE_MKFIFO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008647 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008648#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008649#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008650 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008651#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008652#ifdef HAVE_DEVICE_MACROS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008653 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8654 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8655 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008656#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008657#ifdef HAVE_FTRUNCATE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008658 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008659#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008660#ifdef HAVE_PUTENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008661 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008662#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008663#ifdef HAVE_UNSETENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008664 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008665#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008666 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008667#ifdef HAVE_FCHDIR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008668 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008669#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008670#ifdef HAVE_FSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008671 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008672#endif
8673#ifdef HAVE_FDATASYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008674 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008675#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008676#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008677#ifdef WCOREDUMP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008678 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008679#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008680#ifdef WIFCONTINUED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008681 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008682#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008683#ifdef WIFSTOPPED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008684 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008685#endif /* WIFSTOPPED */
8686#ifdef WIFSIGNALED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008687 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008688#endif /* WIFSIGNALED */
8689#ifdef WIFEXITED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008690 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008691#endif /* WIFEXITED */
8692#ifdef WEXITSTATUS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008693 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008694#endif /* WEXITSTATUS */
8695#ifdef WTERMSIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008696 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008697#endif /* WTERMSIG */
8698#ifdef WSTOPSIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008699 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008700#endif /* WSTOPSIG */
8701#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008702#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008703 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008704#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008705#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008706 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008707#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008708#ifdef HAVE_TMPFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008709 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008710#endif
8711#ifdef HAVE_TEMPNAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008712 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008713#endif
8714#ifdef HAVE_TMPNAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008715 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008716#endif
Fred Drakec9680921999-12-13 16:37:25 +00008717#ifdef HAVE_CONFSTR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008718 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008719#endif
8720#ifdef HAVE_SYSCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008721 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008722#endif
8723#ifdef HAVE_FPATHCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008724 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008725#endif
8726#ifdef HAVE_PATHCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008727 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008728#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008729 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008730#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008731 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008732#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008733#ifdef HAVE_GETLOADAVG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008734 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008735#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008736 #ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008737 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008738 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008739 #ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008740 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008741 #endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008742 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008743};
8744
8745
Barry Warsaw4a342091996-12-19 23:50:02 +00008746static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008747ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008748{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008749 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008750}
8751
Guido van Rossumd48f2521997-12-05 22:19:34 +00008752#if defined(PYOS_OS2)
8753/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008754static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008755{
8756 APIRET rc;
8757 ULONG values[QSV_MAX+1];
8758 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008759 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008760
8761 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008762 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008763 Py_END_ALLOW_THREADS
8764
8765 if (rc != NO_ERROR) {
8766 os2_error(rc);
8767 return -1;
8768 }
8769
Fred Drake4d1e64b2002-04-15 19:40:07 +00008770 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8771 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8772 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8773 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8774 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8775 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8776 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008777
8778 switch (values[QSV_VERSION_MINOR]) {
8779 case 0: ver = "2.00"; break;
8780 case 10: ver = "2.10"; break;
8781 case 11: ver = "2.11"; break;
8782 case 30: ver = "3.00"; break;
8783 case 40: ver = "4.00"; break;
8784 case 50: ver = "5.00"; break;
8785 default:
Tim Peters885d4572001-11-28 20:27:42 +00008786 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008787 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008788 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008789 ver = &tmp[0];
8790 }
8791
8792 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008793 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008794 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008795
8796 /* Add Indicator of Which Drive was Used to Boot the System */
8797 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8798 tmp[1] = ':';
8799 tmp[2] = '\0';
8800
Fred Drake4d1e64b2002-04-15 19:40:07 +00008801 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008802}
8803#endif
8804
Barry Warsaw4a342091996-12-19 23:50:02 +00008805static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008806all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008807{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008808#ifdef F_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008809 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008810#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008811#ifdef R_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008812 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008813#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008814#ifdef W_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008815 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008816#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008817#ifdef X_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008818 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008819#endif
Fred Drakec9680921999-12-13 16:37:25 +00008820#ifdef NGROUPS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008821 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008822#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008823#ifdef TMP_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008824 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008825#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008826#ifdef WCONTINUED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008827 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008828#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008829#ifdef WNOHANG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008830 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008831#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008832#ifdef WUNTRACED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008833 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008834#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008835#ifdef O_RDONLY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008836 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008837#endif
8838#ifdef O_WRONLY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008839 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008840#endif
8841#ifdef O_RDWR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008842 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008843#endif
8844#ifdef O_NDELAY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008845 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008846#endif
8847#ifdef O_NONBLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008848 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008849#endif
8850#ifdef O_APPEND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008851 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008852#endif
8853#ifdef O_DSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008854 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008855#endif
8856#ifdef O_RSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008857 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008858#endif
8859#ifdef O_SYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008860 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008861#endif
8862#ifdef O_NOCTTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008863 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008864#endif
8865#ifdef O_CREAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008866 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008867#endif
8868#ifdef O_EXCL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008869 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008870#endif
8871#ifdef O_TRUNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008872 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008873#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008874#ifdef O_BINARY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008875 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008876#endif
8877#ifdef O_TEXT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008878 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008879#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008880#ifdef O_LARGEFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008881 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008882#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008883#ifdef O_SHLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008884 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008885#endif
8886#ifdef O_EXLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008887 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008888#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008889
Tim Peters5aa91602002-01-30 05:46:57 +00008890/* MS Windows */
8891#ifdef O_NOINHERIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008892 /* Don't inherit in child processes. */
8893 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008894#endif
8895#ifdef _O_SHORT_LIVED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008896 /* Optimize for short life (keep in memory). */
8897 /* MS forgot to define this one with a non-underscore form too. */
8898 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008899#endif
8900#ifdef O_TEMPORARY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008901 /* Automatically delete when last handle is closed. */
8902 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008903#endif
8904#ifdef O_RANDOM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008905 /* Optimize for random access. */
8906 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008907#endif
8908#ifdef O_SEQUENTIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008909 /* Optimize for sequential access. */
8910 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008911#endif
8912
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008913/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00008914#ifdef O_ASYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008915 /* Send a SIGIO signal whenever input or output
8916 becomes available on file descriptor */
8917 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00008918#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008919#ifdef O_DIRECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008920 /* Direct disk access. */
8921 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008922#endif
8923#ifdef O_DIRECTORY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008924 /* Must be a directory. */
8925 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008926#endif
8927#ifdef O_NOFOLLOW
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008928 /* Do not follow links. */
8929 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008930#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008931#ifdef O_NOATIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008932 /* Do not update the access time. */
8933 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00008934#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008935
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008936 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008937#ifdef EX_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008938 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008939#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008940#ifdef EX_USAGE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008941 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008942#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008943#ifdef EX_DATAERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008944 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008945#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008946#ifdef EX_NOINPUT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008947 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008948#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008949#ifdef EX_NOUSER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008950 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008951#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008952#ifdef EX_NOHOST
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008953 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008954#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008955#ifdef EX_UNAVAILABLE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008956 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008957#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008958#ifdef EX_SOFTWARE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008959 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008960#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008961#ifdef EX_OSERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008962 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008963#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008964#ifdef EX_OSFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008965 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008966#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008967#ifdef EX_CANTCREAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008968 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008969#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008970#ifdef EX_IOERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008971 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008972#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008973#ifdef EX_TEMPFAIL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008974 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008975#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008976#ifdef EX_PROTOCOL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008977 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008978#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008979#ifdef EX_NOPERM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008980 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008981#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008982#ifdef EX_CONFIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008983 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008984#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008985#ifdef EX_NOTFOUND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008986 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008987#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008988
Guido van Rossum246bc171999-02-01 23:54:31 +00008989#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008990#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008991 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8992 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8993 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8994 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8995 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8996 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8997 if (ins(d, "P_PM", (long)P_PM)) return -1;
8998 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8999 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9000 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9001 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9002 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9003 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9004 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9005 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9006 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9007 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9008 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9009 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9010 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009011#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009012 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9013 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9014 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9015 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9016 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009017#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009018#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009019
Guido van Rossumd48f2521997-12-05 22:19:34 +00009020#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009021 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009022#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009023 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009024}
9025
9026
Tim Peters5aa91602002-01-30 05:46:57 +00009027#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009028#define INITFUNC initnt
9029#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009030
9031#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009032#define INITFUNC initos2
9033#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009034
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009035#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009036#define INITFUNC initposix
9037#define MODNAME "posix"
9038#endif
9039
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009040PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009041INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009042{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009043 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009044
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009045 m = Py_InitModule3(MODNAME,
9046 posix_methods,
9047 posix__doc__);
9048 if (m == NULL)
9049 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009050
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009051 /* Initialize environ dictionary */
9052 v = convertenviron();
9053 Py_XINCREF(v);
9054 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9055 return;
9056 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009057
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009058 if (all_ins(m))
9059 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009060
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009061 if (setup_confname_tables(m))
9062 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009063
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009064 Py_INCREF(PyExc_OSError);
9065 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009066
Guido van Rossumb3d39562000-01-31 18:41:26 +00009067#ifdef HAVE_PUTENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009068 if (posix_putenv_garbage == NULL)
9069 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009070#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009071
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009072 if (!initialized) {
9073 stat_result_desc.name = MODNAME ".stat_result";
9074 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9075 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9076 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9077 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9078 structseq_new = StatResultType.tp_new;
9079 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009080
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009081 statvfs_result_desc.name = MODNAME ".statvfs_result";
9082 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009083#ifdef NEED_TICKS_PER_SECOND
9084# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009085 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009086# elif defined(HZ)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009087 ticks_per_second = HZ;
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009088# else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009089 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009090# endif
9091#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009092 }
9093 Py_INCREF((PyObject*) &StatResultType);
9094 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9095 Py_INCREF((PyObject*) &StatVFSResultType);
9096 PyModule_AddObject(m, "statvfs_result",
9097 (PyObject*) &StatVFSResultType);
9098 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009099
9100#ifdef __APPLE__
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009101 /*
9102 * Step 2 of weak-linking support on Mac OS X.
9103 *
9104 * The code below removes functions that are not available on the
9105 * currently active platform.
9106 *
9107 * This block allow one to use a python binary that was build on
9108 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9109 * OSX 10.4.
9110 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009111#ifdef HAVE_FSTATVFS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009112 if (fstatvfs == NULL) {
9113 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9114 return;
9115 }
9116 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009117#endif /* HAVE_FSTATVFS */
9118
9119#ifdef HAVE_STATVFS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009120 if (statvfs == NULL) {
9121 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9122 return;
9123 }
9124 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009125#endif /* HAVE_STATVFS */
9126
9127# ifdef HAVE_LCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009128 if (lchown == NULL) {
9129 if (PyObject_DelAttrString(m, "lchown") == -1) {
9130 return;
9131 }
9132 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009133#endif /* HAVE_LCHOWN */
9134
9135
9136#endif /* __APPLE__ */
9137
Guido van Rossumb6775db1994-08-01 11:34:53 +00009138}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009139
9140#ifdef __cplusplus
9141}
9142#endif
9143
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009144