blob: a7f738256774e0b7e76975ec96d6640ff3cea3bd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Ronald Oussorend06b6f22006-04-23 11:59:25 +000016#ifdef __APPLE__
17 /*
Victor Stinner43cdd0a2010-05-06 00:20:44 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Ronald Oussorend06b6f22006-04-23 11:59:25 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Anthony Baxterac6bd462006-04-13 02:06:09 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000069#endif /* HAVE_SYS_TYPES_H */
70
71#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000073#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000074
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#ifdef HAVE_SYS_WAIT_H
Victor Stinner43cdd0a2010-05-06 00:20:44 +000076#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000077#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000081#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000082
Guido van Rossumb6775db1994-08-01 11:34:53 +000083#ifdef HAVE_FCNTL_H
84#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000085#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000086
Guido van Rossuma6535fd2001-10-18 19:44:10 +000087#ifdef HAVE_GRP_H
88#include <grp.h>
89#endif
90
Barry Warsaw5676bd12003-01-07 20:57:09 +000091#ifdef HAVE_SYSEXITS_H
92#include <sysexits.h>
93#endif /* HAVE_SYSEXITS_H */
94
Anthony Baxter8a560de2004-10-13 15:30:56 +000095#ifdef HAVE_SYS_LOADAVG_H
96#include <sys/loadavg.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
119#define HAVE_POPEN 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000120#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000121#define HAVE_WAIT 1
122#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000123#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000124#define HAVE_GETCWD 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000129#define HAVE_SYSTEM 1
130#define HAVE_CWAIT 1
131#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000136#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000152#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000154#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000155#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_WAIT 1
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000157#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000158#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#endif /* _MSC_VER */
160#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000161#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000163
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000165
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000166#if defined(__sgi)&&_COMPILER_VERSION>=700
167/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169extern char *ctermid_r(char *);
170#endif
171
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000172#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000173#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000176#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
182#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(char *);
184extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chdir(const char *);
187extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#ifdef __BORLANDC__
190extern int chmod(const char *, int);
191#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000193#endif
Christian Heimes36281872007-11-30 21:11:28 +0000194/*#ifdef HAVE_FCHMOD
195extern int fchmod(int, mode_t);
196#endif*/
197/*#ifdef HAVE_LCHMOD
198extern int lchmod(const char *, mode_t);
199#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int chown(const char *, uid_t, gid_t);
201extern char *getcwd(char *, int);
202extern char *strerror(int);
203extern int link(const char *, const char *);
204extern int rename(const char *, const char *);
205extern int stat(const char *, struct stat *);
206extern int unlink(const char *);
207extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000210#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000215
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#ifdef HAVE_UTIME_H
219#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000222#ifdef HAVE_SYS_UTIME_H
223#include <sys/utime.h>
224#define HAVE_UTIME_H /* pretend we do for the rest of this file */
225#endif /* HAVE_SYS_UTIME_H */
226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_SYS_TIMES_H
228#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
231#ifdef HAVE_SYS_PARAM_H
232#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
235#ifdef HAVE_SYS_UTSNAME_H
236#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000241#define NAMLEN(dirent) strlen((dirent)->d_name)
242#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000243#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#include <direct.h>
245#define NAMLEN(dirent) strlen((dirent)->d_name)
246#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#endif
256#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#endif
259#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000262#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
265#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000267#endif
268#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000270#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000271#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <windows.h>
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000273#include <shellapi.h> /* for ShellExecute() */
274#define popen _popen
275#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossumd48f2521997-12-05 22:19:34 +0000278#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000280#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
Tim Petersbc2e10e2002-03-03 23:17:02 +0000282#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000283#if defined(PATH_MAX) && PATH_MAX > 1024
284#define MAXPATHLEN PATH_MAX
285#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000286#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000287#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#endif /* MAXPATHLEN */
289
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000290#ifdef UNION_WAIT
291/* Emulate some macros on systems that have a union instead of macros */
292
293#ifndef WIFEXITED
294#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
295#endif
296
297#ifndef WEXITSTATUS
298#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
299#endif
300
301#ifndef WTERMSIG
302#define WTERMSIG(u_wait) ((u_wait).w_termsig)
303#endif
304
Neal Norwitzd5a37542006-03-20 06:48:34 +0000305#define WAIT_TYPE union wait
306#define WAIT_STATUS_INT(s) (s.w_status)
307
308#else /* !UNION_WAIT */
309#define WAIT_TYPE int
310#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000311#endif /* UNION_WAIT */
312
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000313/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou6c95acb2009-05-24 12:17:07 +0000314#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000315#define PARSE_PID "i"
316#define PyLong_FromPid PyInt_FromLong
317#define PyLong_AsPid PyInt_AsLong
318#elif SIZEOF_PID_T == SIZEOF_LONG
319#define PARSE_PID "l"
320#define PyLong_FromPid PyInt_FromLong
321#define PyLong_AsPid PyInt_AsLong
322#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
323#define PARSE_PID "L"
324#define PyLong_FromPid PyLong_FromLongLong
325#define PyLong_AsPid PyInt_AsLongLong
326#else
327#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrou794b3fc2009-05-23 15:47:13 +0000328#endif /* SIZEOF_PID_T */
329
Greg Wardb48bc172000-03-01 21:51:56 +0000330/* Don't use the "_r" form if we don't need it (also, won't have a
331 prototype for it, at least on Solaris -- maybe others as well?). */
332#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
333#define USE_CTERMID_R
334#endif
335
336#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
337#define USE_TMPNAM_R
338#endif
339
Fred Drake699f3522000-06-29 21:12:41 +0000340/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000341#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000342#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000343# define STAT win32_stat
344# define FSTAT win32_fstat
345# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000346#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000347# define STAT stat
348# define FSTAT fstat
349# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000350#endif
351
Tim Peters11b23062003-04-23 02:39:17 +0000352#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000353#include <sys/mkdev.h>
354#else
355#if defined(MAJOR_IN_SYSMACROS)
356#include <sys/sysmacros.h>
357#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000358#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
359#include <sys/mkdev.h>
360#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000361#endif
Fred Drake699f3522000-06-29 21:12:41 +0000362
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000364#ifdef WITH_NEXT_FRAMEWORK
365/* On Darwin/MacOSX a shared library or framework has no access to
366** environ directly, we must obtain it with _NSGetEnviron().
367*/
368#include <crt_externs.h>
369static char **environ;
370#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000372#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373
Barry Warsaw53699e91996-12-10 23:23:01 +0000374static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000375convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000377 PyObject *d;
378 char **e;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000379#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000380 APIRET rc;
381 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
382#endif
383 d = PyDict_New();
384 if (d == NULL)
385 return NULL;
386#ifdef WITH_NEXT_FRAMEWORK
387 if (environ == NULL)
388 environ = *_NSGetEnviron();
389#endif
390 if (environ == NULL)
391 return d;
392 /* This part ignores errors */
393 for (e = environ; *e != NULL; e++) {
394 PyObject *k;
395 PyObject *v;
396 char *p = strchr(*e, '=');
397 if (p == NULL)
398 continue;
399 k = PyString_FromStringAndSize(*e, (int)(p-*e));
400 if (k == NULL) {
401 PyErr_Clear();
402 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000403 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000404 v = PyString_FromString(p+1);
405 if (v == NULL) {
406 PyErr_Clear();
407 Py_DECREF(k);
408 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000409 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000410 if (PyDict_GetItem(d, k) == NULL) {
411 if (PyDict_SetItem(d, k, v) != 0)
412 PyErr_Clear();
413 }
414 Py_DECREF(k);
415 Py_DECREF(v);
416 }
417#if defined(PYOS_OS2)
418 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
419 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
420 PyObject *v = PyString_FromString(buffer);
421 PyDict_SetItemString(d, "BEGINLIBPATH", v);
422 Py_DECREF(v);
423 }
424 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
425 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
426 PyObject *v = PyString_FromString(buffer);
427 PyDict_SetItemString(d, "ENDLIBPATH", v);
428 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000429 }
430#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000431 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432}
433
434
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435/* Set a POSIX-specific error from errno, and return NULL */
436
Barry Warsawd58d7641998-07-23 16:14:40 +0000437static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000438posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000439{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000440 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
Barry Warsawd58d7641998-07-23 16:14:40 +0000442static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000443posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000444{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000445 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000446}
447
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000448#ifdef Py_WIN_WIDE_FILENAMES
449static PyObject *
450posix_error_with_unicode_filename(Py_UNICODE* name)
451{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000452 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000453}
454#endif /* Py_WIN_WIDE_FILENAMES */
455
456
Mark Hammondef8b6542001-05-13 08:04:26 +0000457static PyObject *
458posix_error_with_allocated_filename(char* name)
459{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000460 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
461 PyMem_Free(name);
462 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000463}
464
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000465#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000466static PyObject *
467win32_error(char* function, char* filename)
468{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000469 /* XXX We should pass the function name along in the future.
470 (_winreg.c also wants to pass the function name.)
471 This would however require an additional param to the
472 Windows error object, which is non-trivial.
473 */
474 errno = GetLastError();
475 if (filename)
476 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
477 else
478 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000479}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000480
481#ifdef Py_WIN_WIDE_FILENAMES
482static PyObject *
483win32_error_unicode(char* function, Py_UNICODE* filename)
484{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000485 /* XXX - see win32_error for comments on 'function' */
486 errno = GetLastError();
487 if (filename)
488 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
489 else
490 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000491}
492
493static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
494{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000495}
496
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000497static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000498convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000499{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000500 if (PyUnicode_CheckExact(*param))
501 Py_INCREF(*param);
502 else if (PyUnicode_Check(*param))
503 /* For a Unicode subtype that's not a Unicode object,
504 return a true Unicode object with the same data. */
505 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
506 PyUnicode_GET_SIZE(*param));
507 else
508 *param = PyUnicode_FromEncodedObject(*param,
509 Py_FileSystemDefaultEncoding,
510 "strict");
511 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000512}
513
514#endif /* Py_WIN_WIDE_FILENAMES */
515
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000516#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518#if defined(PYOS_OS2)
519/**********************************************************************
520 * Helper Function to Trim and Format OS/2 Messages
521 **********************************************************************/
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000522static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000523os2_formatmsg(char *msgbuf, int msglen, char *reason)
524{
525 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
526
527 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
528 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
529
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000530 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000531 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
532 }
533
534 /* Add Optional Reason Text */
535 if (reason) {
536 strcat(msgbuf, " : ");
537 strcat(msgbuf, reason);
538 }
539}
540
541/**********************************************************************
542 * Decode an OS/2 Operating System Error Code
543 *
544 * A convenience function to lookup an OS/2 error code and return a
545 * text message we can use to raise a Python exception.
546 *
547 * Notes:
548 * The messages for errors returned from the OS/2 kernel reside in
549 * the file OSO001.MSG in the \OS2 directory hierarchy.
550 *
551 **********************************************************************/
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000552static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000553os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
554{
555 APIRET rc;
556 ULONG msglen;
557
558 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
559 Py_BEGIN_ALLOW_THREADS
560 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
561 errorcode, "oso001.msg", &msglen);
562 Py_END_ALLOW_THREADS
563
564 if (rc == NO_ERROR)
565 os2_formatmsg(msgbuf, msglen, reason);
566 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000567 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000568 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000569
570 return msgbuf;
571}
572
573/* Set an OS/2-specific error and return NULL. OS/2 kernel
574 errors are not in a global variable e.g. 'errno' nor are
575 they congruent with posix error numbers. */
576
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000577static PyObject *
578os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000579{
580 char text[1024];
581 PyObject *v;
582
583 os2_strerror(text, sizeof(text), code, "");
584
585 v = Py_BuildValue("(is)", code, text);
586 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000587 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000588 Py_DECREF(v);
589 }
590 return NULL; /* Signal to Python that an Exception is Pending */
591}
592
593#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594
595/* POSIX generic methods */
596
Barry Warsaw53699e91996-12-10 23:23:01 +0000597static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000598posix_fildes(PyObject *fdobj, int (*func)(int))
599{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000600 int fd;
601 int res;
602 fd = PyObject_AsFileDescriptor(fdobj);
603 if (fd < 0)
604 return NULL;
605 Py_BEGIN_ALLOW_THREADS
606 res = (*func)(fd);
607 Py_END_ALLOW_THREADS
608 if (res < 0)
609 return posix_error();
610 Py_INCREF(Py_None);
611 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000612}
Guido van Rossum21142a01999-01-08 21:05:37 +0000613
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000615static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000616unicode_file_names(void)
617{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000618 static int canusewide = -1;
619 if (canusewide == -1) {
620 /* As per doc for ::GetVersion(), this is the correct test for
621 the Windows NT family. */
622 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
623 }
624 return canusewide;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000625}
626#endif
Tim Peters11b23062003-04-23 02:39:17 +0000627
Guido van Rossum21142a01999-01-08 21:05:37 +0000628static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000629posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000631 char *path1 = NULL;
632 int res;
633 if (!PyArg_ParseTuple(args, format,
634 Py_FileSystemDefaultEncoding, &path1))
635 return NULL;
636 Py_BEGIN_ALLOW_THREADS
637 res = (*func)(path1);
638 Py_END_ALLOW_THREADS
639 if (res < 0)
640 return posix_error_with_allocated_filename(path1);
641 PyMem_Free(path1);
642 Py_INCREF(Py_None);
643 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
Barry Warsaw53699e91996-12-10 23:23:01 +0000646static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000647posix_2str(PyObject *args,
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000648 char *format,
649 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000651 char *path1 = NULL, *path2 = NULL;
652 int res;
653 if (!PyArg_ParseTuple(args, format,
654 Py_FileSystemDefaultEncoding, &path1,
655 Py_FileSystemDefaultEncoding, &path2))
656 return NULL;
657 Py_BEGIN_ALLOW_THREADS
658 res = (*func)(path1, path2);
659 Py_END_ALLOW_THREADS
660 PyMem_Free(path1);
661 PyMem_Free(path2);
662 if (res != 0)
663 /* XXX how to report both path1 and path2??? */
664 return posix_error();
665 Py_INCREF(Py_None);
666 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000669#ifdef Py_WIN_WIDE_FILENAMES
670static PyObject*
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000671win32_1str(PyObject* args, char* func,
672 char* format, BOOL (__stdcall *funcA)(LPCSTR),
673 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000674{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000675 PyObject *uni;
676 char *ansi;
677 BOOL result;
678 if (unicode_file_names()) {
679 if (!PyArg_ParseTuple(args, wformat, &uni))
680 PyErr_Clear();
681 else {
682 Py_BEGIN_ALLOW_THREADS
683 result = funcW(PyUnicode_AsUnicode(uni));
684 Py_END_ALLOW_THREADS
685 if (!result)
686 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
687 Py_INCREF(Py_None);
688 return Py_None;
689 }
690 }
691 if (!PyArg_ParseTuple(args, format, &ansi))
692 return NULL;
693 Py_BEGIN_ALLOW_THREADS
694 result = funcA(ansi);
695 Py_END_ALLOW_THREADS
696 if (!result)
697 return win32_error(func, ansi);
698 Py_INCREF(Py_None);
699 return Py_None;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000700
701}
702
703/* This is a reimplementation of the C library's chdir function,
704 but one that produces Win32 errors instead of DOS error codes.
705 chdir is essentially a wrapper around SetCurrentDirectory; however,
706 it also needs to set "magic" environment variables indicating
707 the per-drive current directory, which are of the form =<drive>: */
708BOOL __stdcall
709win32_chdir(LPCSTR path)
710{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000711 char new_path[MAX_PATH+1];
712 int result;
713 char env[4] = "=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000714
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000715 if(!SetCurrentDirectoryA(path))
716 return FALSE;
717 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 /* In the ANSI API, there should not be any paths longer
721 than MAX_PATH. */
722 assert(result <= MAX_PATH+1);
723 if (strncmp(new_path, "\\\\", 2) == 0 ||
724 strncmp(new_path, "//", 2) == 0)
725 /* UNC path, nothing to do. */
726 return TRUE;
727 env[1] = new_path[0];
728 return SetEnvironmentVariableA(env, new_path);
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000729}
730
731/* The Unicode version differs from the ANSI version
732 since the current directory might exceed MAX_PATH characters */
733BOOL __stdcall
734win32_wchdir(LPCWSTR path)
735{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000736 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
737 int result;
738 wchar_t env[4] = L"=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000739
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000740 if(!SetCurrentDirectoryW(path))
741 return FALSE;
742 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
743 if (!result)
744 return FALSE;
745 if (result > MAX_PATH+1) {
746 new_path = malloc(result * sizeof(wchar_t));
747 if (!new_path) {
748 SetLastError(ERROR_OUTOFMEMORY);
749 return FALSE;
750 }
751 result = GetCurrentDirectoryW(result, new_path);
752 if (!result) {
753 free(new_path);
754 return FALSE;
755 }
756 }
757 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
758 wcsncmp(new_path, L"//", 2) == 0)
759 /* UNC path, nothing to do. */
760 return TRUE;
761 env[1] = new_path[0];
762 result = SetEnvironmentVariableW(env, new_path);
763 if (new_path != _new_path)
764 free(new_path);
765 return result;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000766}
767#endif
768
Martin v. Löwis14694662006-02-03 12:54:16 +0000769#ifdef MS_WINDOWS
770/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
771 - time stamps are restricted to second resolution
772 - file modification times suffer from forth-and-back conversions between
773 UTC and local time
774 Therefore, we implement our own stat, based on the Win32 API directly.
775*/
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000776#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000777
778struct win32_stat{
779 int st_dev;
780 __int64 st_ino;
781 unsigned short st_mode;
782 int st_nlink;
783 int st_uid;
784 int st_gid;
785 int st_rdev;
786 __int64 st_size;
787 int st_atime;
788 int st_atime_nsec;
789 int st_mtime;
790 int st_mtime_nsec;
791 int st_ctime;
792 int st_ctime_nsec;
793};
794
795static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
796
797static void
798FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
799{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000800 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
801 /* Cannot simply cast and dereference in_ptr,
802 since it might not be aligned properly */
803 __int64 in;
804 memcpy(&in, in_ptr, sizeof(in));
805 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
806 /* XXX Win32 supports time stamps past 2038; we currently don't */
807 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000808}
809
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000810static void
811time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
812{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000813 /* XXX endianness */
814 __int64 out;
815 out = time_in + secs_between_epochs;
816 out = out * 10000000 + nsec_in / 100;
817 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000818}
819
Martin v. Löwis14694662006-02-03 12:54:16 +0000820/* Below, we *know* that ugo+r is 0444 */
821#if _S_IREAD != 0400
822#error Unsupported C library
823#endif
824static int
825attributes_to_mode(DWORD attr)
826{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000827 int m = 0;
828 if (attr & FILE_ATTRIBUTE_DIRECTORY)
829 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
830 else
831 m |= _S_IFREG;
832 if (attr & FILE_ATTRIBUTE_READONLY)
833 m |= 0444;
834 else
835 m |= 0666;
836 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000837}
838
839static int
840attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
841{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000842 memset(result, 0, sizeof(*result));
843 result->st_mode = attributes_to_mode(info->dwFileAttributes);
844 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
845 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
846 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
847 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000848
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000849 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000850}
851
Martin v. Löwis012bc722006-10-15 09:43:39 +0000852/* Emulate GetFileAttributesEx[AW] on Windows 95 */
853static int checked = 0;
854static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
855static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
856static void
857check_gfax()
858{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000859 HINSTANCE hKernel32;
860 if (checked)
861 return;
862 checked = 1;
863 hKernel32 = GetModuleHandle("KERNEL32");
864 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
865 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
Martin v. Löwis012bc722006-10-15 09:43:39 +0000866}
867
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000868static BOOL
869attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
870{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000871 HANDLE hFindFile;
872 WIN32_FIND_DATAA FileData;
873 hFindFile = FindFirstFileA(pszFile, &FileData);
874 if (hFindFile == INVALID_HANDLE_VALUE)
875 return FALSE;
876 FindClose(hFindFile);
877 pfad->dwFileAttributes = FileData.dwFileAttributes;
878 pfad->ftCreationTime = FileData.ftCreationTime;
879 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
880 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
881 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
882 pfad->nFileSizeLow = FileData.nFileSizeLow;
883 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000884}
885
886static BOOL
887attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
888{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000889 HANDLE hFindFile;
890 WIN32_FIND_DATAW FileData;
891 hFindFile = FindFirstFileW(pszFile, &FileData);
892 if (hFindFile == INVALID_HANDLE_VALUE)
893 return FALSE;
894 FindClose(hFindFile);
895 pfad->dwFileAttributes = FileData.dwFileAttributes;
896 pfad->ftCreationTime = FileData.ftCreationTime;
897 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
898 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
899 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
900 pfad->nFileSizeLow = FileData.nFileSizeLow;
901 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000902}
903
Martin v. Löwis012bc722006-10-15 09:43:39 +0000904static BOOL WINAPI
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000905Py_GetFileAttributesExA(LPCSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
Martin v. Löwis012bc722006-10-15 09:43:39 +0000907 LPVOID pv)
908{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000909 BOOL result;
910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxa(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
930 return FALSE;
931 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000932}
933
934static BOOL WINAPI
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000935Py_GetFileAttributesExW(LPCWSTR pszFile,
936 GET_FILEEX_INFO_LEVELS level,
Martin v. Löwis012bc722006-10-15 09:43:39 +0000937 LPVOID pv)
938{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000939 BOOL result;
940 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
941 /* First try to use the system's implementation, if that is
942 available and either succeeds to gives an error other than
943 that it isn't implemented. */
944 check_gfax();
945 if (gfaxa) {
946 result = gfaxw(pszFile, level, pv);
947 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
948 return result;
949 }
950 /* It's either not present, or not implemented.
951 Emulate using FindFirstFile. */
952 if (level != GetFileExInfoStandard) {
953 SetLastError(ERROR_INVALID_PARAMETER);
954 return FALSE;
955 }
956 /* Use GetFileAttributes to validate that the file name
957 does not contain wildcards (which FindFirstFile would
958 accept). */
959 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
960 return FALSE;
961 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000962}
963
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000964static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000965win32_stat(const char* path, struct win32_stat *result)
966{
Victor Stinner43cdd0a2010-05-06 00:20:44 +0000967 WIN32_FILE_ATTRIBUTE_DATA info;
968 int code;
969 char *dot;
970 /* XXX not supported on Win95 and NT 3.x */
971 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
972 if (GetLastError() != ERROR_SHARING_VIOLATION) {
973 /* Protocol violation: we explicitly clear errno, instead of
974 setting it to a POSIX error. Callers should use GetLastError. */
975 errno = 0;
976 return -1;
977 } else {
978 /* Could not get attributes on open file. Fall back to
979 reading the directory. */
980 if (!attributes_from_dir(path, &info)) {
981 /* Very strange. This should not fail now */
982 errno = 0;
983 return -1;
984 }
985 }
986 }
987 code = attribute_data_to_stat(&info, result);
988 if (code != 0)
989 return code;
990 /* Set S_IFEXEC if it is an .exe, .bat, ... */
991 dot = strrchr(path, '.');
992 if (dot) {
993 if (stricmp(dot, ".bat") == 0 ||
994 stricmp(dot, ".cmd") == 0 ||
995 stricmp(dot, ".exe") == 0 ||
996 stricmp(dot, ".com") == 0)
997 result->st_mode |= 0111;
998 }
999 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001000}
1001
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001002static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001003win32_wstat(const wchar_t* path, struct win32_stat *result)
1004{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001005 int code;
1006 const wchar_t *dot;
1007 WIN32_FILE_ATTRIBUTE_DATA info;
1008 /* XXX not supported on Win95 and NT 3.x */
1009 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1010 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1011 /* Protocol violation: we explicitly clear errno, instead of
1012 setting it to a POSIX error. Callers should use GetLastError. */
1013 errno = 0;
1014 return -1;
1015 } else {
1016 /* Could not get attributes on open file. Fall back to
1017 reading the directory. */
1018 if (!attributes_from_dir_w(path, &info)) {
1019 /* Very strange. This should not fail now */
1020 errno = 0;
1021 return -1;
1022 }
1023 }
1024 }
1025 code = attribute_data_to_stat(&info, result);
1026 if (code < 0)
1027 return code;
1028 /* Set IFEXEC if it is an .exe, .bat, ... */
1029 dot = wcsrchr(path, '.');
1030 if (dot) {
1031 if (_wcsicmp(dot, L".bat") == 0 ||
1032 _wcsicmp(dot, L".cmd") == 0 ||
1033 _wcsicmp(dot, L".exe") == 0 ||
1034 _wcsicmp(dot, L".com") == 0)
1035 result->st_mode |= 0111;
1036 }
1037 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001038}
1039
1040static int
1041win32_fstat(int file_number, struct win32_stat *result)
1042{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001043 BY_HANDLE_FILE_INFORMATION info;
1044 HANDLE h;
1045 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001046
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001047 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001048
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001049 /* Protocol violation: we explicitly clear errno, instead of
1050 setting it to a POSIX error. Callers should use GetLastError. */
1051 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001052
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001053 if (h == INVALID_HANDLE_VALUE) {
1054 /* This is really a C library error (invalid file handle).
1055 We set the Win32 error to the closes one matching. */
1056 SetLastError(ERROR_INVALID_HANDLE);
1057 return -1;
1058 }
1059 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001060
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001061 type = GetFileType(h);
1062 if (type == FILE_TYPE_UNKNOWN) {
1063 DWORD error = GetLastError();
1064 if (error != 0) {
1065 return -1;
1066 }
1067 /* else: valid but unknown file */
1068 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001069
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001070 if (type != FILE_TYPE_DISK) {
1071 if (type == FILE_TYPE_CHAR)
1072 result->st_mode = _S_IFCHR;
1073 else if (type == FILE_TYPE_PIPE)
1074 result->st_mode = _S_IFIFO;
1075 return 0;
1076 }
1077
1078 if (!GetFileInformationByHandle(h, &info)) {
1079 return -1;
1080 }
1081
1082 /* similar to stat() */
1083 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1084 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1085 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1086 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1087 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1088 /* specific to fstat() */
1089 result->st_nlink = info.nNumberOfLinks;
1090 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1091 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001092}
1093
1094#endif /* MS_WINDOWS */
1095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001097"stat_result: Result from stat or lstat.\n\n\
1098This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001099 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001100or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1101\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001102Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1103or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106
1107static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001108 {"st_mode", "protection bits"},
1109 {"st_ino", "inode"},
1110 {"st_dev", "device"},
1111 {"st_nlink", "number of hard links"},
1112 {"st_uid", "user ID of owner"},
1113 {"st_gid", "group ID of owner"},
1114 {"st_size", "total size, in bytes"},
1115 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1116 {NULL, "integer time of last access"},
1117 {NULL, "integer time of last modification"},
1118 {NULL, "integer time of last change"},
1119 {"st_atime", "time of last access"},
1120 {"st_mtime", "time of last modification"},
1121 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001122#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001123 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001124#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001126 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001127#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001128#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001129 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001130#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001132 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001133#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001134#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001135 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001136#endif
1137#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001138 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001139#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001140 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141};
1142
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001143#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001144#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001145#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001146#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001147#endif
1148
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001149#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001150#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1151#else
1152#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1153#endif
1154
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001155#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1157#else
1158#define ST_RDEV_IDX ST_BLOCKS_IDX
1159#endif
1160
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001161#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1162#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1163#else
1164#define ST_FLAGS_IDX ST_RDEV_IDX
1165#endif
1166
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001167#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001168#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001169#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001170#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001171#endif
1172
1173#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1174#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1175#else
1176#define ST_BIRTHTIME_IDX ST_GEN_IDX
1177#endif
1178
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001179static PyStructSequence_Desc stat_result_desc = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001180 "stat_result", /* name */
1181 stat_result__doc__, /* doc */
1182 stat_result_fields,
1183 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001184};
1185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001187"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1188This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001189 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001190or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001193
1194static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001195 {"f_bsize", },
1196 {"f_frsize", },
1197 {"f_blocks", },
1198 {"f_bfree", },
1199 {"f_bavail", },
1200 {"f_files", },
1201 {"f_ffree", },
1202 {"f_favail", },
1203 {"f_flag", },
1204 {"f_namemax",},
1205 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206};
1207
1208static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001209 "statvfs_result", /* name */
1210 statvfs_result__doc__, /* doc */
1211 statvfs_result_fields,
1212 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001213};
1214
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001215static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001216static PyTypeObject StatResultType;
1217static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001218static newfunc structseq_new;
1219
1220static PyObject *
1221statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1222{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001223 PyStructSequence *result;
1224 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001225
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001226 result = (PyStructSequence*)structseq_new(type, args, kwds);
1227 if (!result)
1228 return NULL;
1229 /* If we have been initialized from a tuple,
1230 st_?time might be set to None. Initialize it
1231 from the int slots. */
1232 for (i = 7; i <= 9; i++) {
1233 if (result->ob_item[i+3] == Py_None) {
1234 Py_DECREF(Py_None);
1235 Py_INCREF(result->ob_item[i]);
1236 result->ob_item[i+3] = result->ob_item[i];
1237 }
1238 }
1239 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001240}
1241
1242
1243
1244/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001245static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001246
1247PyDoc_STRVAR(stat_float_times__doc__,
1248"stat_float_times([newval]) -> oldval\n\n\
1249Determine whether os.[lf]stat represents time stamps as float objects.\n\
1250If newval is True, future calls to stat() return floats, if it is False,\n\
1251future calls return ints. \n\
1252If newval is omitted, return the current setting.\n");
1253
1254static PyObject*
1255stat_float_times(PyObject* self, PyObject *args)
1256{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001257 int newval = -1;
1258 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1259 return NULL;
1260 if (newval == -1)
1261 /* Return old value */
1262 return PyBool_FromLong(_stat_float_times);
1263 _stat_float_times = newval;
1264 Py_INCREF(Py_None);
1265 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001266}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001267
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001268static void
1269fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1270{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001271 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001272#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001273 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001274#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001275 ival = PyInt_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001276#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001277 if (!ival)
1278 return;
1279 if (_stat_float_times) {
1280 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1281 } else {
1282 fval = ival;
1283 Py_INCREF(fval);
1284 }
1285 PyStructSequence_SET_ITEM(v, index, ival);
1286 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001287}
1288
Tim Peters5aa91602002-01-30 05:46:57 +00001289/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001290 (used by posix_stat() and posix_fstat()) */
1291static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001292_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001293{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001294 unsigned long ansec, mnsec, cnsec;
1295 PyObject *v = PyStructSequence_New(&StatResultType);
1296 if (v == NULL)
1297 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001298
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001299 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001300#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001301 PyStructSequence_SET_ITEM(v, 1,
1302 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001303#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001304 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001305#endif
1306#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001307 PyStructSequence_SET_ITEM(v, 2,
1308 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001309#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001310 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001311#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001312 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1313 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1314 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001315#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001316 PyStructSequence_SET_ITEM(v, 6,
1317 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001319 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001321
Martin v. Löwis14694662006-02-03 12:54:16 +00001322#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001323 ansec = st->st_atim.tv_nsec;
1324 mnsec = st->st_mtim.tv_nsec;
1325 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001326#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001327 ansec = st->st_atimespec.tv_nsec;
1328 mnsec = st->st_mtimespec.tv_nsec;
1329 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001330#elif defined(HAVE_STAT_NSEC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001331 ansec = st->st_atime_nsec;
1332 mnsec = st->st_mtime_nsec;
1333 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001335 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001337 fill_time(v, 7, st->st_atime, ansec);
1338 fill_time(v, 8, st->st_mtime, mnsec);
1339 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001341#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001342 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1343 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001344#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001345#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001346 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1347 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001349#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001350 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1351 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001352#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001353#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001354 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1355 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001356#endif
1357#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001358 {
1359 PyObject *val;
1360 unsigned long bsec,bnsec;
1361 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001362#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001363 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001364#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001365 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001366#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001367 if (_stat_float_times) {
1368 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1369 } else {
1370 val = PyInt_FromLong((long)bsec);
1371 }
1372 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1373 val);
1374 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001376#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001377 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1378 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001379#endif
Fred Drake699f3522000-06-29 21:12:41 +00001380
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001381 if (PyErr_Occurred()) {
1382 Py_DECREF(v);
1383 return NULL;
1384 }
Fred Drake699f3522000-06-29 21:12:41 +00001385
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001386 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001387}
1388
Martin v. Löwisd8948722004-06-02 09:57:56 +00001389#ifdef MS_WINDOWS
1390
1391/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1392 where / can be used in place of \ and the trailing slash is optional.
1393 Both SERVER and SHARE must have at least one character.
1394*/
1395
1396#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1397#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001398#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001399#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001400#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001401
Tim Peters4ad82172004-08-30 17:02:04 +00001402static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001403IsUNCRootA(char *path, int pathlen)
1404{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001405 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001406
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001407 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001408
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001409 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1410 /* minimum UNCRoot is \\x\y */
1411 return FALSE;
1412 for (i = 2; i < pathlen ; i++)
1413 if (ISSLASH(path[i])) break;
1414 if (i == 2 || i == pathlen)
1415 /* do not allow \\\SHARE or \\SERVER */
1416 return FALSE;
1417 share = i+1;
1418 for (i = share; i < pathlen; i++)
1419 if (ISSLASH(path[i])) break;
1420 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001421
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001422 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001423}
1424
1425#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001426static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001427IsUNCRootW(Py_UNICODE *path, int pathlen)
1428{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001429 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001430
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001431 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001432
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001433 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1434 /* minimum UNCRoot is \\x\y */
1435 return FALSE;
1436 for (i = 2; i < pathlen ; i++)
1437 if (ISSLASH(path[i])) break;
1438 if (i == 2 || i == pathlen)
1439 /* do not allow \\\SHARE or \\SERVER */
1440 return FALSE;
1441 share = i+1;
1442 for (i = share; i < pathlen; i++)
1443 if (ISSLASH(path[i])) break;
1444 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001445
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001446 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001447}
1448#endif /* Py_WIN_WIDE_FILENAMES */
1449#endif /* MS_WINDOWS */
1450
Barry Warsaw53699e91996-12-10 23:23:01 +00001451static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001452posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001453 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001454#ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001455 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001456#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001457 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001458#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001459 char *wformat,
1460 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001461{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001462 STRUCT_STAT st;
1463 char *path = NULL; /* pass this to stat; do not free() it */
1464 char *pathfree = NULL; /* this memory must be free'd */
1465 int res;
1466 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001467
1468#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001469 /* If on wide-character-capable OS see if argument
1470 is Unicode and if so use wide API. */
1471 if (unicode_file_names()) {
1472 PyUnicodeObject *po;
1473 if (PyArg_ParseTuple(args, wformat, &po)) {
1474 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001475
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001476 Py_BEGIN_ALLOW_THREADS
1477 /* PyUnicode_AS_UNICODE result OK without
1478 thread lock as it is a simple dereference. */
1479 res = wstatfunc(wpath, &st);
1480 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001482 if (res != 0)
1483 return win32_error_unicode("stat", wpath);
1484 return _pystat_fromstructstat(&st);
1485 }
1486 /* Drop the argument parsing error as narrow strings
1487 are also valid. */
1488 PyErr_Clear();
1489 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001490#endif
1491
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001492 if (!PyArg_ParseTuple(args, format,
1493 Py_FileSystemDefaultEncoding, &path))
1494 return NULL;
1495 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001496
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001497 Py_BEGIN_ALLOW_THREADS
1498 res = (*statfunc)(path, &st);
1499 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001500
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001501 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001502#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001503 result = win32_error("stat", pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001504#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001505 result = posix_error_with_filename(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001506#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001507 }
1508 else
1509 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001510
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001511 PyMem_Free(pathfree);
1512 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001513}
1514
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001515/* POSIX methods */
1516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001518"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001519Use the real uid/gid to test for access to a path. Note that most\n\
1520operations will use the effective uid/gid, therefore this routine can\n\
1521be used in a suid/sgid environment to test if the invoking user has the\n\
1522specified access to the path. The mode argument can be F_OK to test\n\
1523existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001524
1525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001526posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001527{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001528 char *path;
1529 int mode;
1530
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001531#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001532 DWORD attr;
1533 if (unicode_file_names()) {
1534 PyUnicodeObject *po;
1535 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1536 Py_BEGIN_ALLOW_THREADS
1537 /* PyUnicode_AS_UNICODE OK without thread lock as
1538 it is a simple dereference. */
1539 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1540 Py_END_ALLOW_THREADS
1541 goto finish;
1542 }
1543 /* Drop the argument parsing error as narrow strings
1544 are also valid. */
1545 PyErr_Clear();
1546 }
1547 if (!PyArg_ParseTuple(args, "eti:access",
1548 Py_FileSystemDefaultEncoding, &path, &mode))
1549 return 0;
1550 Py_BEGIN_ALLOW_THREADS
1551 attr = GetFileAttributesA(path);
1552 Py_END_ALLOW_THREADS
1553 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001554finish:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001555 if (attr == 0xFFFFFFFF)
1556 /* File does not exist, or cannot read attributes */
1557 return PyBool_FromLong(0);
1558 /* Access is possible if either write access wasn't requested, or
1559 the file isn't read-only, or if it's a directory, as there are
1560 no read-only directories on Windows. */
1561 return PyBool_FromLong(!(mode & 2)
1562 || !(attr & FILE_ATTRIBUTE_READONLY)
1563 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001564#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001565 int res;
1566 if (!PyArg_ParseTuple(args, "eti:access",
1567 Py_FileSystemDefaultEncoding, &path, &mode))
1568 return NULL;
1569 Py_BEGIN_ALLOW_THREADS
1570 res = access(path, mode);
1571 Py_END_ALLOW_THREADS
1572 PyMem_Free(path);
1573 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001574#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001575}
1576
Guido van Rossumd371ff11999-01-25 16:12:23 +00001577#ifndef F_OK
1578#define F_OK 0
1579#endif
1580#ifndef R_OK
1581#define R_OK 4
1582#endif
1583#ifndef W_OK
1584#define W_OK 2
1585#endif
1586#ifndef X_OK
1587#define X_OK 1
1588#endif
1589
1590#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001592"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001594
1595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001596posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001597{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001598 int id;
1599 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001600
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001601 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1602 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001604#if defined(__VMS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001605 /* file descriptor 0 only, the default input device (stdin) */
1606 if (id == 0) {
1607 ret = ttyname();
1608 }
1609 else {
1610 ret = NULL;
1611 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001612#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001613 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001614#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001615 if (ret == NULL)
1616 return posix_error();
1617 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001618}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001620
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001621#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001623"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001625
1626static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001627posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001628{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001629 char *ret;
1630 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001631
Greg Wardb48bc172000-03-01 21:51:56 +00001632#ifdef USE_CTERMID_R
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001633 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001635 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001636#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001637 if (ret == NULL)
1638 return posix_error();
1639 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001640}
1641#endif
1642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001644"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001645Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001646
Barry Warsaw53699e91996-12-10 23:23:01 +00001647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001648posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001649{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001650#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001651 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001652#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001653 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001654#elif defined(__VMS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001655 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001657 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001658#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659}
1660
Fred Drake4d1e64b2002-04-15 19:40:07 +00001661#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001663"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001664Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001665opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001666
1667static PyObject *
1668posix_fchdir(PyObject *self, PyObject *fdobj)
1669{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001670 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001671}
1672#endif /* HAVE_FCHDIR */
1673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001675PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001676"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001677Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678
Barry Warsaw53699e91996-12-10 23:23:01 +00001679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001680posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001681{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001682 char *path = NULL;
1683 int i;
1684 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001685#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001686 DWORD attr;
1687 if (unicode_file_names()) {
1688 PyUnicodeObject *po;
1689 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1690 Py_BEGIN_ALLOW_THREADS
1691 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1692 if (attr != 0xFFFFFFFF) {
1693 if (i & _S_IWRITE)
1694 attr &= ~FILE_ATTRIBUTE_READONLY;
1695 else
1696 attr |= FILE_ATTRIBUTE_READONLY;
1697 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1698 }
1699 else
1700 res = 0;
1701 Py_END_ALLOW_THREADS
1702 if (!res)
1703 return win32_error_unicode("chmod",
1704 PyUnicode_AS_UNICODE(po));
1705 Py_INCREF(Py_None);
1706 return Py_None;
1707 }
1708 /* Drop the argument parsing error as narrow strings
1709 are also valid. */
1710 PyErr_Clear();
1711 }
1712 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1713 &path, &i))
1714 return NULL;
1715 Py_BEGIN_ALLOW_THREADS
1716 attr = GetFileAttributesA(path);
1717 if (attr != 0xFFFFFFFF) {
1718 if (i & _S_IWRITE)
1719 attr &= ~FILE_ATTRIBUTE_READONLY;
1720 else
1721 attr |= FILE_ATTRIBUTE_READONLY;
1722 res = SetFileAttributesA(path, attr);
1723 }
1724 else
1725 res = 0;
1726 Py_END_ALLOW_THREADS
1727 if (!res) {
1728 win32_error("chmod", path);
1729 PyMem_Free(path);
1730 return NULL;
1731 }
1732 PyMem_Free(path);
1733 Py_INCREF(Py_None);
1734 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001735#else /* Py_WIN_WIDE_FILENAMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001736 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1737 &path, &i))
1738 return NULL;
1739 Py_BEGIN_ALLOW_THREADS
1740 res = chmod(path, i);
1741 Py_END_ALLOW_THREADS
1742 if (res < 0)
1743 return posix_error_with_allocated_filename(path);
1744 PyMem_Free(path);
1745 Py_INCREF(Py_None);
1746 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001747#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001748}
1749
Christian Heimes36281872007-11-30 21:11:28 +00001750#ifdef HAVE_FCHMOD
1751PyDoc_STRVAR(posix_fchmod__doc__,
1752"fchmod(fd, mode)\n\n\
1753Change the access permissions of the file given by file\n\
1754descriptor fd.");
1755
1756static PyObject *
1757posix_fchmod(PyObject *self, PyObject *args)
1758{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001759 int fd, mode, res;
1760 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1761 return NULL;
1762 Py_BEGIN_ALLOW_THREADS
1763 res = fchmod(fd, mode);
1764 Py_END_ALLOW_THREADS
1765 if (res < 0)
1766 return posix_error();
1767 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001768}
1769#endif /* HAVE_FCHMOD */
1770
1771#ifdef HAVE_LCHMOD
1772PyDoc_STRVAR(posix_lchmod__doc__,
1773"lchmod(path, mode)\n\n\
1774Change the access permissions of a file. If path is a symlink, this\n\
1775affects the link itself rather than the target.");
1776
1777static PyObject *
1778posix_lchmod(PyObject *self, PyObject *args)
1779{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001780 char *path = NULL;
1781 int i;
1782 int res;
1783 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1784 &path, &i))
1785 return NULL;
1786 Py_BEGIN_ALLOW_THREADS
1787 res = lchmod(path, i);
1788 Py_END_ALLOW_THREADS
1789 if (res < 0)
1790 return posix_error_with_allocated_filename(path);
1791 PyMem_Free(path);
1792 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001793}
1794#endif /* HAVE_LCHMOD */
1795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001796
Martin v. Löwis382abef2007-02-19 10:55:19 +00001797#ifdef HAVE_CHFLAGS
1798PyDoc_STRVAR(posix_chflags__doc__,
1799"chflags(path, flags)\n\n\
1800Set file flags.");
1801
1802static PyObject *
1803posix_chflags(PyObject *self, PyObject *args)
1804{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001805 char *path;
1806 unsigned long flags;
1807 int res;
1808 if (!PyArg_ParseTuple(args, "etk:chflags",
1809 Py_FileSystemDefaultEncoding, &path, &flags))
1810 return NULL;
1811 Py_BEGIN_ALLOW_THREADS
1812 res = chflags(path, flags);
1813 Py_END_ALLOW_THREADS
1814 if (res < 0)
1815 return posix_error_with_allocated_filename(path);
1816 PyMem_Free(path);
1817 Py_INCREF(Py_None);
1818 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001819}
1820#endif /* HAVE_CHFLAGS */
1821
1822#ifdef HAVE_LCHFLAGS
1823PyDoc_STRVAR(posix_lchflags__doc__,
1824"lchflags(path, flags)\n\n\
1825Set file flags.\n\
1826This function will not follow symbolic links.");
1827
1828static PyObject *
1829posix_lchflags(PyObject *self, PyObject *args)
1830{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001831 char *path;
1832 unsigned long flags;
1833 int res;
1834 if (!PyArg_ParseTuple(args, "etk:lchflags",
1835 Py_FileSystemDefaultEncoding, &path, &flags))
1836 return NULL;
1837 Py_BEGIN_ALLOW_THREADS
1838 res = lchflags(path, flags);
1839 Py_END_ALLOW_THREADS
1840 if (res < 0)
1841 return posix_error_with_allocated_filename(path);
1842 PyMem_Free(path);
1843 Py_INCREF(Py_None);
1844 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001845}
1846#endif /* HAVE_LCHFLAGS */
1847
Martin v. Löwis244edc82001-10-04 22:44:26 +00001848#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001850"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001851Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001852
1853static PyObject *
1854posix_chroot(PyObject *self, PyObject *args)
1855{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001856 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001857}
1858#endif
1859
Guido van Rossum21142a01999-01-08 21:05:37 +00001860#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001862"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001864
1865static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001866posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001867{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001868 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001869}
1870#endif /* HAVE_FSYNC */
1871
1872#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001873
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001874#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001875extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1876#endif
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001880force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001882
1883static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001884posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001885{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001886 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001887}
1888#endif /* HAVE_FDATASYNC */
1889
1890
Fredrik Lundh10723342000-07-10 16:38:09 +00001891#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001892PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001893"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001895
Barry Warsaw53699e91996-12-10 23:23:01 +00001896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001897posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001898{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001899 char *path = NULL;
1900 long uid, gid;
1901 int res;
1902 if (!PyArg_ParseTuple(args, "etll:chown",
1903 Py_FileSystemDefaultEncoding, &path,
1904 &uid, &gid))
1905 return NULL;
1906 Py_BEGIN_ALLOW_THREADS
1907 res = chown(path, (uid_t) uid, (gid_t) gid);
1908 Py_END_ALLOW_THREADS
1909 if (res < 0)
1910 return posix_error_with_allocated_filename(path);
1911 PyMem_Free(path);
1912 Py_INCREF(Py_None);
1913 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001914}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001915#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001916
Christian Heimes36281872007-11-30 21:11:28 +00001917#ifdef HAVE_FCHOWN
1918PyDoc_STRVAR(posix_fchown__doc__,
1919"fchown(fd, uid, gid)\n\n\
1920Change the owner and group id of the file given by file descriptor\n\
1921fd to the numeric uid and gid.");
1922
1923static PyObject *
1924posix_fchown(PyObject *self, PyObject *args)
1925{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001926 int fd;
1927 long uid, gid;
1928 int res;
1929 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1930 return NULL;
1931 Py_BEGIN_ALLOW_THREADS
1932 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1933 Py_END_ALLOW_THREADS
1934 if (res < 0)
1935 return posix_error();
1936 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001937}
1938#endif /* HAVE_FCHOWN */
1939
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001940#ifdef HAVE_LCHOWN
1941PyDoc_STRVAR(posix_lchown__doc__,
1942"lchown(path, uid, gid)\n\n\
1943Change the owner and group id of path to the numeric uid and gid.\n\
1944This function will not follow symbolic links.");
1945
1946static PyObject *
1947posix_lchown(PyObject *self, PyObject *args)
1948{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001949 char *path = NULL;
1950 long uid, gid;
1951 int res;
1952 if (!PyArg_ParseTuple(args, "etll:lchown",
1953 Py_FileSystemDefaultEncoding, &path,
1954 &uid, &gid))
1955 return NULL;
1956 Py_BEGIN_ALLOW_THREADS
1957 res = lchown(path, (uid_t) uid, (gid_t) gid);
1958 Py_END_ALLOW_THREADS
1959 if (res < 0)
1960 return posix_error_with_allocated_filename(path);
1961 PyMem_Free(path);
1962 Py_INCREF(Py_None);
1963 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001964}
1965#endif /* HAVE_LCHOWN */
1966
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001967
Guido van Rossum36bc6801995-06-14 22:54:23 +00001968#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001970"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001971Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001972
Stefan Krah36db84d2010-07-19 15:43:23 +00001973#if (defined(__sun) && defined(__SVR4)) || defined(__OpenBSD__)
1974/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
1975static PyObject *
1976posix_getcwd(PyObject *self, PyObject *noargs)
1977{
1978 char buf[PATH_MAX+2];
1979 char *res;
1980
1981 Py_BEGIN_ALLOW_THREADS
1982 res = getcwd(buf, sizeof buf);
1983 Py_END_ALLOW_THREADS
1984
1985 if (res == NULL)
1986 return posix_error();
1987
1988 return PyString_FromString(buf);
1989}
1990#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001991static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001992posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001993{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00001994 int bufsize_incr = 1024;
1995 int bufsize = 0;
1996 char *tmpbuf = NULL;
1997 char *res = NULL;
1998 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001999
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002000 Py_BEGIN_ALLOW_THREADS
2001 do {
2002 bufsize = bufsize + bufsize_incr;
2003 tmpbuf = malloc(bufsize);
2004 if (tmpbuf == NULL) {
2005 break;
2006 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002007#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002008 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002009#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002010 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002011#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00002012
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002013 if (res == NULL) {
2014 free(tmpbuf);
2015 }
2016 } while ((res == NULL) && (errno == ERANGE));
2017 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002018
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002019 if (res == NULL)
2020 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002021
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002022 dynamic_return = PyString_FromString(tmpbuf);
2023 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002024
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002025 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002026}
Stefan Krah36db84d2010-07-19 15:43:23 +00002027#endif /* getcwd() NULL/ERANGE workaround. */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028
Walter Dörwald3b918c32002-11-21 20:18:46 +00002029#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030PyDoc_STRVAR(posix_getcwdu__doc__,
2031"getcwdu() -> path\n\n\
2032Return a unicode string representing the current working directory.");
2033
2034static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002035posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002036{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002037 char buf[1026];
2038 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002039
2040#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002041 DWORD len;
2042 if (unicode_file_names()) {
2043 wchar_t wbuf[1026];
2044 wchar_t *wbuf2 = wbuf;
2045 PyObject *resobj;
2046 Py_BEGIN_ALLOW_THREADS
2047 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2048 /* If the buffer is large enough, len does not include the
2049 terminating \0. If the buffer is too small, len includes
2050 the space needed for the terminator. */
2051 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2052 wbuf2 = malloc(len * sizeof(wchar_t));
2053 if (wbuf2)
2054 len = GetCurrentDirectoryW(len, wbuf2);
2055 }
2056 Py_END_ALLOW_THREADS
2057 if (!wbuf2) {
2058 PyErr_NoMemory();
2059 return NULL;
2060 }
2061 if (!len) {
2062 if (wbuf2 != wbuf) free(wbuf2);
2063 return win32_error("getcwdu", NULL);
2064 }
2065 resobj = PyUnicode_FromWideChar(wbuf2, len);
2066 if (wbuf2 != wbuf) free(wbuf2);
2067 return resobj;
2068 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002069#endif
2070
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002071 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002072#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002073 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002074#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002075 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002076#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002077 Py_END_ALLOW_THREADS
2078 if (res == NULL)
2079 return posix_error();
2080 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002081}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002082#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00002083#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085
Guido van Rossumb6775db1994-08-01 11:34:53 +00002086#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002087PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002088"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002089Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002090
Barry Warsaw53699e91996-12-10 23:23:01 +00002091static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002092posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002093{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002094 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002096#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002099PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002100"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002101Return a list containing the names of the entries in the directory.\n\
2102\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002103 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002104\n\
2105The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107
Barry Warsaw53699e91996-12-10 23:23:01 +00002108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002109posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002110{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002111 /* XXX Should redo this putting the (now four) versions of opendir
2112 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002113#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002114
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002115 PyObject *d, *v;
2116 HANDLE hFindFile;
2117 BOOL result;
2118 WIN32_FIND_DATA FileData;
2119 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2120 char *bufptr = namebuf;
2121 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002123#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002124 /* If on wide-character-capable OS see if argument
2125 is Unicode and if so use wide API. */
2126 if (unicode_file_names()) {
2127 PyObject *po;
2128 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2129 WIN32_FIND_DATAW wFileData;
2130 Py_UNICODE *wnamebuf;
2131 Py_UNICODE wch;
2132 /* Overallocate for \\*.*\0 */
2133 len = PyUnicode_GET_SIZE(po);
2134 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2135 if (!wnamebuf) {
2136 PyErr_NoMemory();
2137 return NULL;
2138 }
2139 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2140 wch = len > 0 ? wnamebuf[len-1] : '\0';
2141 if (wch != L'/' && wch != L'\\' && wch != L':')
2142 wnamebuf[len++] = L'\\';
2143 wcscpy(wnamebuf + len, L"*.*");
2144 if ((d = PyList_New(0)) == NULL) {
2145 free(wnamebuf);
2146 return NULL;
2147 }
2148 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2149 if (hFindFile == INVALID_HANDLE_VALUE) {
2150 int error = GetLastError();
2151 if (error == ERROR_FILE_NOT_FOUND) {
2152 free(wnamebuf);
2153 return d;
2154 }
2155 Py_DECREF(d);
2156 win32_error_unicode("FindFirstFileW", wnamebuf);
2157 free(wnamebuf);
2158 return NULL;
2159 }
2160 do {
2161 /* Skip over . and .. */
2162 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2163 wcscmp(wFileData.cFileName, L"..") != 0) {
2164 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2165 if (v == NULL) {
2166 Py_DECREF(d);
2167 d = NULL;
2168 break;
2169 }
2170 if (PyList_Append(d, v) != 0) {
2171 Py_DECREF(v);
2172 Py_DECREF(d);
2173 d = NULL;
2174 break;
2175 }
2176 Py_DECREF(v);
2177 }
2178 Py_BEGIN_ALLOW_THREADS
2179 result = FindNextFileW(hFindFile, &wFileData);
2180 Py_END_ALLOW_THREADS
2181 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2182 it got to the end of the directory. */
2183 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2184 Py_DECREF(d);
2185 win32_error_unicode("FindNextFileW", wnamebuf);
2186 FindClose(hFindFile);
2187 free(wnamebuf);
2188 return NULL;
2189 }
2190 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002191
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002192 if (FindClose(hFindFile) == FALSE) {
2193 Py_DECREF(d);
2194 win32_error_unicode("FindClose", wnamebuf);
2195 free(wnamebuf);
2196 return NULL;
2197 }
2198 free(wnamebuf);
2199 return d;
2200 }
2201 /* Drop the argument parsing error as narrow strings
2202 are also valid. */
2203 PyErr_Clear();
2204 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002205#endif
2206
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002207 if (!PyArg_ParseTuple(args, "et#:listdir",
2208 Py_FileSystemDefaultEncoding, &bufptr, &len))
2209 return NULL;
2210 if (len > 0) {
2211 char ch = namebuf[len-1];
2212 if (ch != SEP && ch != ALTSEP && ch != ':')
2213 namebuf[len++] = '/';
2214 }
2215 strcpy(namebuf + len, "*.*");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002216
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002217 if ((d = PyList_New(0)) == NULL)
2218 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002219
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002220 hFindFile = FindFirstFile(namebuf, &FileData);
2221 if (hFindFile == INVALID_HANDLE_VALUE) {
2222 int error = GetLastError();
2223 if (error == ERROR_FILE_NOT_FOUND)
2224 return d;
2225 Py_DECREF(d);
2226 return win32_error("FindFirstFile", namebuf);
2227 }
2228 do {
2229 /* Skip over . and .. */
2230 if (strcmp(FileData.cFileName, ".") != 0 &&
2231 strcmp(FileData.cFileName, "..") != 0) {
2232 v = PyString_FromString(FileData.cFileName);
2233 if (v == NULL) {
2234 Py_DECREF(d);
2235 d = NULL;
2236 break;
2237 }
2238 if (PyList_Append(d, v) != 0) {
2239 Py_DECREF(v);
2240 Py_DECREF(d);
2241 d = NULL;
2242 break;
2243 }
2244 Py_DECREF(v);
2245 }
2246 Py_BEGIN_ALLOW_THREADS
2247 result = FindNextFile(hFindFile, &FileData);
2248 Py_END_ALLOW_THREADS
2249 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2250 it got to the end of the directory. */
2251 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2252 Py_DECREF(d);
2253 win32_error("FindNextFile", namebuf);
2254 FindClose(hFindFile);
2255 return NULL;
2256 }
2257 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002258
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002259 if (FindClose(hFindFile) == FALSE) {
2260 Py_DECREF(d);
2261 return win32_error("FindClose", namebuf);
2262 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002263
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002264 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002265
Tim Peters0bb44a42000-09-15 07:44:49 +00002266#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002267
2268#ifndef MAX_PATH
2269#define MAX_PATH CCHMAXPATH
2270#endif
2271 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002272 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 PyObject *d, *v;
2274 char namebuf[MAX_PATH+5];
2275 HDIR hdir = 1;
2276 ULONG srchcnt = 1;
2277 FILEFINDBUF3 ep;
2278 APIRET rc;
2279
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002280 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281 return NULL;
2282 if (len >= MAX_PATH) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002283 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002284 return NULL;
2285 }
2286 strcpy(namebuf, name);
2287 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002288 if (*pt == ALTSEP)
2289 *pt = SEP;
2290 if (namebuf[len-1] != SEP)
2291 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002292 strcpy(namebuf + len, "*.*");
2293
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002294 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002295 return NULL;
2296
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002297 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2298 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002299 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002300 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2301 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2302 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002303
2304 if (rc != NO_ERROR) {
2305 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002306 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002307 }
2308
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002309 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002310 do {
2311 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002312 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002313 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002314
2315 strcpy(namebuf, ep.achName);
2316
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002317 /* Leave Case of Name Alone -- In Native Form */
2318 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002319
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002320 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002321 if (v == NULL) {
2322 Py_DECREF(d);
2323 d = NULL;
2324 break;
2325 }
2326 if (PyList_Append(d, v) != 0) {
2327 Py_DECREF(v);
2328 Py_DECREF(d);
2329 d = NULL;
2330 break;
2331 }
2332 Py_DECREF(v);
2333 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2334 }
2335
2336 return d;
2337#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002338
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002339 char *name = NULL;
2340 PyObject *d, *v;
2341 DIR *dirp;
2342 struct dirent *ep;
2343 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002344
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002345 errno = 0;
2346 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2347 arg_is_unicode = 0;
2348 PyErr_Clear();
2349 }
2350 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2351 return NULL;
2352 if ((dirp = opendir(name)) == NULL) {
2353 return posix_error_with_allocated_filename(name);
2354 }
2355 if ((d = PyList_New(0)) == NULL) {
2356 closedir(dirp);
2357 PyMem_Free(name);
2358 return NULL;
2359 }
2360 for (;;) {
2361 errno = 0;
2362 Py_BEGIN_ALLOW_THREADS
2363 ep = readdir(dirp);
2364 Py_END_ALLOW_THREADS
2365 if (ep == NULL) {
2366 if (errno == 0) {
2367 break;
2368 } else {
2369 closedir(dirp);
2370 Py_DECREF(d);
2371 return posix_error_with_allocated_filename(name);
2372 }
2373 }
2374 if (ep->d_name[0] == '.' &&
2375 (NAMLEN(ep) == 1 ||
2376 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2377 continue;
2378 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2379 if (v == NULL) {
2380 Py_DECREF(d);
2381 d = NULL;
2382 break;
2383 }
Just van Rossum46c97842003-02-25 21:42:15 +00002384#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002385 if (arg_is_unicode) {
2386 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002387
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002388 w = PyUnicode_FromEncodedObject(v,
2389 Py_FileSystemDefaultEncoding,
2390 "strict");
2391 if (w != NULL) {
2392 Py_DECREF(v);
2393 v = w;
2394 }
2395 else {
2396 /* fall back to the original byte string, as
2397 discussed in patch #683592 */
2398 PyErr_Clear();
2399 }
2400 }
Just van Rossum46c97842003-02-25 21:42:15 +00002401#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002402 if (PyList_Append(d, v) != 0) {
2403 Py_DECREF(v);
2404 Py_DECREF(d);
2405 d = NULL;
2406 break;
2407 }
2408 Py_DECREF(v);
2409 }
2410 closedir(dirp);
2411 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002412
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002413 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002414
Tim Peters0bb44a42000-09-15 07:44:49 +00002415#endif /* which OS */
2416} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002417
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002418#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002419/* A helper function for abspath on win32 */
2420static PyObject *
2421posix__getfullpathname(PyObject *self, PyObject *args)
2422{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002423 /* assume encoded strings won't more than double no of chars */
2424 char inbuf[MAX_PATH*2];
2425 char *inbufp = inbuf;
2426 Py_ssize_t insize = sizeof(inbuf);
2427 char outbuf[MAX_PATH*2];
2428 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002429#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002430 if (unicode_file_names()) {
2431 PyUnicodeObject *po;
2432 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2433 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2434 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2435 Py_UNICODE *wtemp;
2436 DWORD result;
2437 PyObject *v;
2438 result = GetFullPathNameW(wpath,
2439 sizeof(woutbuf)/sizeof(woutbuf[0]),
2440 woutbuf, &wtemp);
2441 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2442 woutbufp = malloc(result * sizeof(Py_UNICODE));
2443 if (!woutbufp)
2444 return PyErr_NoMemory();
2445 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2446 }
2447 if (result)
2448 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2449 else
2450 v = win32_error_unicode("GetFullPathNameW", wpath);
2451 if (woutbufp != woutbuf)
2452 free(woutbufp);
2453 return v;
2454 }
2455 /* Drop the argument parsing error as narrow strings
2456 are also valid. */
2457 PyErr_Clear();
2458 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002460 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2461 Py_FileSystemDefaultEncoding, &inbufp,
2462 &insize))
2463 return NULL;
2464 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2465 outbuf, &temp))
2466 return win32_error("GetFullPathName", inbuf);
2467 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2468 return PyUnicode_Decode(outbuf, strlen(outbuf),
2469 Py_FileSystemDefaultEncoding, NULL);
2470 }
2471 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002472} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002473#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002476"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002477Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002478
Barry Warsaw53699e91996-12-10 23:23:01 +00002479static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002480posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002481{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002482 int res;
2483 char *path = NULL;
2484 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002485
2486#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002487 if (unicode_file_names()) {
2488 PyUnicodeObject *po;
2489 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2490 Py_BEGIN_ALLOW_THREADS
2491 /* PyUnicode_AS_UNICODE OK without thread lock as
2492 it is a simple dereference. */
2493 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2494 Py_END_ALLOW_THREADS
2495 if (!res)
2496 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2497 Py_INCREF(Py_None);
2498 return Py_None;
2499 }
2500 /* Drop the argument parsing error as narrow strings
2501 are also valid. */
2502 PyErr_Clear();
2503 }
2504 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2505 Py_FileSystemDefaultEncoding, &path, &mode))
2506 return NULL;
2507 Py_BEGIN_ALLOW_THREADS
2508 /* PyUnicode_AS_UNICODE OK without thread lock as
2509 it is a simple dereference. */
2510 res = CreateDirectoryA(path, NULL);
2511 Py_END_ALLOW_THREADS
2512 if (!res) {
2513 win32_error("mkdir", path);
2514 PyMem_Free(path);
2515 return NULL;
2516 }
2517 PyMem_Free(path);
2518 Py_INCREF(Py_None);
2519 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002520#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002521
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002522 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2523 Py_FileSystemDefaultEncoding, &path, &mode))
2524 return NULL;
2525 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002526#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002527 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002528#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002529 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002530#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002531 Py_END_ALLOW_THREADS
2532 if (res < 0)
2533 return posix_error_with_allocated_filename(path);
2534 PyMem_Free(path);
2535 Py_INCREF(Py_None);
2536 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002537#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002538}
2539
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002540
Neal Norwitz1818ed72006-03-26 00:29:48 +00002541/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2542#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002543#include <sys/resource.h>
2544#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002545
Neal Norwitz1818ed72006-03-26 00:29:48 +00002546
2547#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002549"nice(inc) -> new_priority\n\n\
2550Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Barry Warsaw53699e91996-12-10 23:23:01 +00002552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002553posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002554{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002555 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002556
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002557 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2558 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002559
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002560 /* There are two flavours of 'nice': one that returns the new
2561 priority (as required by almost all standards out there) and the
2562 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2563 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002564
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002565 If we are of the nice family that returns the new priority, we
2566 need to clear errno before the call, and check if errno is filled
2567 before calling posix_error() on a returnvalue of -1, because the
2568 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002569
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002570 errno = 0;
2571 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002572#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002573 if (value == 0)
2574 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002575#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002576 if (value == -1 && errno != 0)
2577 /* either nice() or getpriority() returned an error */
2578 return posix_error();
2579 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002580}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002581#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002582
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002583PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002584"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002585Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002586
Barry Warsaw53699e91996-12-10 23:23:01 +00002587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002588posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002589{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002590#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002591 PyObject *o1, *o2;
2592 char *p1, *p2;
2593 BOOL result;
2594 if (unicode_file_names()) {
2595 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2596 goto error;
2597 if (!convert_to_unicode(&o1))
2598 goto error;
2599 if (!convert_to_unicode(&o2)) {
2600 Py_DECREF(o1);
2601 goto error;
2602 }
2603 Py_BEGIN_ALLOW_THREADS
2604 result = MoveFileW(PyUnicode_AsUnicode(o1),
2605 PyUnicode_AsUnicode(o2));
2606 Py_END_ALLOW_THREADS
2607 Py_DECREF(o1);
2608 Py_DECREF(o2);
2609 if (!result)
2610 return win32_error("rename", NULL);
2611 Py_INCREF(Py_None);
2612 return Py_None;
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002613error:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002614 PyErr_Clear();
2615 }
2616 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2617 return NULL;
2618 Py_BEGIN_ALLOW_THREADS
2619 result = MoveFileA(p1, p2);
2620 Py_END_ALLOW_THREADS
2621 if (!result)
2622 return win32_error("rename", NULL);
2623 Py_INCREF(Py_None);
2624 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002626 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002627#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002628}
2629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002632"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002634
Barry Warsaw53699e91996-12-10 23:23:01 +00002635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002636posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002637{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002639 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002640#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002641 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002643}
2644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002646PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002647"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002648Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002649
Barry Warsaw53699e91996-12-10 23:23:01 +00002650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002651posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002653#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002654 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002655#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002656 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002657#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002658}
2659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002661#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002662PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002663"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002664Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002665
Barry Warsaw53699e91996-12-10 23:23:01 +00002666static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002667posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002668{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002669 char *command;
2670 long sts;
2671 if (!PyArg_ParseTuple(args, "s:system", &command))
2672 return NULL;
2673 Py_BEGIN_ALLOW_THREADS
2674 sts = system(command);
2675 Py_END_ALLOW_THREADS
2676 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002677}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002682"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002683Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002684
Barry Warsaw53699e91996-12-10 23:23:01 +00002685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002686posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002687{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002688 int i;
2689 if (!PyArg_ParseTuple(args, "i:umask", &i))
2690 return NULL;
2691 i = (int)umask(i);
2692 if (i < 0)
2693 return posix_error();
2694 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695}
2696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002699"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002703"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002705
Barry Warsaw53699e91996-12-10 23:23:01 +00002706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002707posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002708{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002709#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002710 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002711#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002712 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002713#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002714}
2715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Guido van Rossumb6775db1994-08-01 11:34:53 +00002717#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002719"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002720Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721
Barry Warsaw53699e91996-12-10 23:23:01 +00002722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002723posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002724{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002725 struct utsname u;
2726 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002727
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002728 Py_BEGIN_ALLOW_THREADS
2729 res = uname(&u);
2730 Py_END_ALLOW_THREADS
2731 if (res < 0)
2732 return posix_error();
2733 return Py_BuildValue("(sssss)",
2734 u.sysname,
2735 u.nodename,
2736 u.release,
2737 u.version,
2738 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002739}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002740#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002741
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002742static int
2743extract_time(PyObject *t, long* sec, long* usec)
2744{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002745 long intval;
2746 if (PyFloat_Check(t)) {
2747 double tval = PyFloat_AsDouble(t);
2748 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2749 if (!intobj)
2750 return -1;
2751 intval = PyInt_AsLong(intobj);
2752 Py_DECREF(intobj);
2753 if (intval == -1 && PyErr_Occurred())
2754 return -1;
2755 *sec = intval;
2756 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2757 if (*usec < 0)
2758 /* If rounding gave us a negative number,
2759 truncate. */
2760 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002761 return 0;
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002762 }
2763 intval = PyInt_AsLong(t);
2764 if (intval == -1 && PyErr_Occurred())
2765 return -1;
2766 *sec = intval;
2767 *usec = 0;
2768 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002769}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002772"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002773utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002774Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Barry Warsaw53699e91996-12-10 23:23:01 +00002777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002778posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002779{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002780#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002781 PyObject *arg;
2782 PyUnicodeObject *obwpath;
2783 wchar_t *wpath = NULL;
2784 char *apath = NULL;
2785 HANDLE hFile;
2786 long atimesec, mtimesec, ausec, musec;
2787 FILETIME atime, mtime;
2788 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002789
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002790 if (unicode_file_names()) {
2791 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2792 wpath = PyUnicode_AS_UNICODE(obwpath);
2793 Py_BEGIN_ALLOW_THREADS
2794 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2795 NULL, OPEN_EXISTING,
2796 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2797 Py_END_ALLOW_THREADS
2798 if (hFile == INVALID_HANDLE_VALUE)
2799 return win32_error_unicode("utime", wpath);
2800 } else
2801 /* Drop the argument parsing error as narrow strings
2802 are also valid. */
2803 PyErr_Clear();
2804 }
2805 if (!wpath) {
2806 if (!PyArg_ParseTuple(args, "etO:utime",
2807 Py_FileSystemDefaultEncoding, &apath, &arg))
2808 return NULL;
2809 Py_BEGIN_ALLOW_THREADS
2810 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2811 NULL, OPEN_EXISTING,
2812 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2813 Py_END_ALLOW_THREADS
2814 if (hFile == INVALID_HANDLE_VALUE) {
2815 win32_error("utime", apath);
2816 PyMem_Free(apath);
2817 return NULL;
2818 }
2819 PyMem_Free(apath);
2820 }
2821
2822 if (arg == Py_None) {
2823 SYSTEMTIME now;
2824 GetSystemTime(&now);
2825 if (!SystemTimeToFileTime(&now, &mtime) ||
2826 !SystemTimeToFileTime(&now, &atime)) {
2827 win32_error("utime", NULL);
2828 goto done;
2829 }
2830 }
2831 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2832 PyErr_SetString(PyExc_TypeError,
2833 "utime() arg 2 must be a tuple (atime, mtime)");
2834 goto done;
2835 }
2836 else {
2837 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2838 &atimesec, &ausec) == -1)
2839 goto done;
2840 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2841 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2842 &mtimesec, &musec) == -1)
2843 goto done;
2844 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2845 }
2846 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2847 /* Avoid putting the file name into the error here,
2848 as that may confuse the user into believing that
2849 something is wrong with the file, when it also
2850 could be the time stamp that gives a problem. */
2851 win32_error("utime", NULL);
2852 }
2853 Py_INCREF(Py_None);
2854 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002855done:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002856 CloseHandle(hFile);
2857 return result;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002858#else /* Py_WIN_WIDE_FILENAMES */
2859
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002860 char *path = NULL;
2861 long atime, mtime, ausec, musec;
2862 int res;
2863 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002864
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002865#if defined(HAVE_UTIMES)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002866 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002867#define ATIME buf[0].tv_sec
2868#define MTIME buf[1].tv_sec
2869#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002870/* XXX should define struct utimbuf instead, above */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002871 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002872#define ATIME buf.actime
2873#define MTIME buf.modtime
2874#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002875#else /* HAVE_UTIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002876 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002877#define ATIME buf[0]
2878#define MTIME buf[1]
2879#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002880#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002881
Mark Hammond817c9292003-12-03 01:22:38 +00002882
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002883 if (!PyArg_ParseTuple(args, "etO:utime",
2884 Py_FileSystemDefaultEncoding, &path, &arg))
2885 return NULL;
2886 if (arg == Py_None) {
2887 /* optional time values not given */
2888 Py_BEGIN_ALLOW_THREADS
2889 res = utime(path, NULL);
2890 Py_END_ALLOW_THREADS
2891 }
2892 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2893 PyErr_SetString(PyExc_TypeError,
2894 "utime() arg 2 must be a tuple (atime, mtime)");
2895 PyMem_Free(path);
2896 return NULL;
2897 }
2898 else {
2899 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2900 &atime, &ausec) == -1) {
2901 PyMem_Free(path);
2902 return NULL;
2903 }
2904 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2905 &mtime, &musec) == -1) {
2906 PyMem_Free(path);
2907 return NULL;
2908 }
2909 ATIME = atime;
2910 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002911#ifdef HAVE_UTIMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002912 buf[0].tv_usec = ausec;
2913 buf[1].tv_usec = musec;
2914 Py_BEGIN_ALLOW_THREADS
2915 res = utimes(path, buf);
2916 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002917#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002918 Py_BEGIN_ALLOW_THREADS
2919 res = utime(path, UTIME_ARG);
2920 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002921#endif /* HAVE_UTIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002922 }
2923 if (res < 0) {
2924 return posix_error_with_allocated_filename(path);
2925 }
2926 PyMem_Free(path);
2927 Py_INCREF(Py_None);
2928 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002929#undef UTIME_ARG
2930#undef ATIME
2931#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002932#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002933}
2934
Guido van Rossum85e3b011991-06-03 12:42:10 +00002935
Guido van Rossum3b066191991-06-04 19:40:25 +00002936/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002937
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002938PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002939"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002941
Barry Warsaw53699e91996-12-10 23:23:01 +00002942static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002943posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002944{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002945 int sts;
2946 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2947 return NULL;
2948 _exit(sts);
2949 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002950}
2951
Martin v. Löwis114619e2002-10-07 06:44:21 +00002952#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2953static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002954free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002955{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002956 Py_ssize_t i;
2957 for (i = 0; i < count; i++)
2958 PyMem_Free(array[i]);
2959 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002960}
2961#endif
2962
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002963
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002964#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002965PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002966"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002967Execute an executable path with arguments, replacing current process.\n\
2968\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002969 path: path of executable file\n\
2970 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002971
Barry Warsaw53699e91996-12-10 23:23:01 +00002972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002973posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002974{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002975 char *path;
2976 PyObject *argv;
2977 char **argvlist;
2978 Py_ssize_t i, argc;
2979 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002980
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002981 /* execv has two arguments: (path, argv), where
2982 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002983
Victor Stinner43cdd0a2010-05-06 00:20:44 +00002984 if (!PyArg_ParseTuple(args, "etO:execv",
2985 Py_FileSystemDefaultEncoding,
2986 &path, &argv))
2987 return NULL;
2988 if (PyList_Check(argv)) {
2989 argc = PyList_Size(argv);
2990 getitem = PyList_GetItem;
2991 }
2992 else if (PyTuple_Check(argv)) {
2993 argc = PyTuple_Size(argv);
2994 getitem = PyTuple_GetItem;
2995 }
2996 else {
2997 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
2998 PyMem_Free(path);
2999 return NULL;
3000 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003001
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003002 argvlist = PyMem_NEW(char *, argc+1);
3003 if (argvlist == NULL) {
3004 PyMem_Free(path);
3005 return PyErr_NoMemory();
3006 }
3007 for (i = 0; i < argc; i++) {
3008 if (!PyArg_Parse((*getitem)(argv, i), "et",
3009 Py_FileSystemDefaultEncoding,
3010 &argvlist[i])) {
3011 free_string_array(argvlist, i);
3012 PyErr_SetString(PyExc_TypeError,
3013 "execv() arg 2 must contain only strings");
3014 PyMem_Free(path);
3015 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003016
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003017 }
3018 }
3019 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003020
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003021 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003022
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003023 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003024
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003025 free_string_array(argvlist, argc);
3026 PyMem_Free(path);
3027 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003028}
3029
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003031PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003032"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003033Execute a path with arguments and environment, replacing current process.\n\
3034\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003035 path: path of executable file\n\
3036 args: tuple or list of arguments\n\
3037 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003038
Barry Warsaw53699e91996-12-10 23:23:01 +00003039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003040posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003041{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003042 char *path;
3043 PyObject *argv, *env;
3044 char **argvlist;
3045 char **envlist;
3046 PyObject *key, *val, *keys=NULL, *vals=NULL;
3047 Py_ssize_t i, pos, argc, envc;
3048 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3049 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003050
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003051 /* execve has three arguments: (path, argv, env), where
3052 argv is a list or tuple of strings and env is a dictionary
3053 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003054
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003055 if (!PyArg_ParseTuple(args, "etOO:execve",
3056 Py_FileSystemDefaultEncoding,
3057 &path, &argv, &env))
3058 return NULL;
3059 if (PyList_Check(argv)) {
3060 argc = PyList_Size(argv);
3061 getitem = PyList_GetItem;
3062 }
3063 else if (PyTuple_Check(argv)) {
3064 argc = PyTuple_Size(argv);
3065 getitem = PyTuple_GetItem;
3066 }
3067 else {
3068 PyErr_SetString(PyExc_TypeError,
3069 "execve() arg 2 must be a tuple or list");
3070 goto fail_0;
3071 }
3072 if (!PyMapping_Check(env)) {
3073 PyErr_SetString(PyExc_TypeError,
3074 "execve() arg 3 must be a mapping object");
3075 goto fail_0;
3076 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003077
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003078 argvlist = PyMem_NEW(char *, argc+1);
3079 if (argvlist == NULL) {
3080 PyErr_NoMemory();
3081 goto fail_0;
3082 }
3083 for (i = 0; i < argc; i++) {
3084 if (!PyArg_Parse((*getitem)(argv, i),
3085 "et;execve() arg 2 must contain only strings",
3086 Py_FileSystemDefaultEncoding,
3087 &argvlist[i]))
3088 {
3089 lastarg = i;
3090 goto fail_1;
3091 }
3092 }
3093 lastarg = argc;
3094 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003095
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003096 i = PyMapping_Size(env);
3097 if (i < 0)
3098 goto fail_1;
3099 envlist = PyMem_NEW(char *, i + 1);
3100 if (envlist == NULL) {
3101 PyErr_NoMemory();
3102 goto fail_1;
3103 }
3104 envc = 0;
3105 keys = PyMapping_Keys(env);
3106 vals = PyMapping_Values(env);
3107 if (!keys || !vals)
3108 goto fail_2;
3109 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3110 PyErr_SetString(PyExc_TypeError,
3111 "execve(): env.keys() or env.values() is not a list");
3112 goto fail_2;
3113 }
Tim Peters5aa91602002-01-30 05:46:57 +00003114
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003115 for (pos = 0; pos < i; pos++) {
3116 char *p, *k, *v;
3117 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003118
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003119 key = PyList_GetItem(keys, pos);
3120 val = PyList_GetItem(vals, pos);
3121 if (!key || !val)
3122 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003123
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003124 if (!PyArg_Parse(
3125 key,
3126 "s;execve() arg 3 contains a non-string key",
3127 &k) ||
3128 !PyArg_Parse(
3129 val,
3130 "s;execve() arg 3 contains a non-string value",
3131 &v))
3132 {
3133 goto fail_2;
3134 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003135
3136#if defined(PYOS_OS2)
3137 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3138 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3139#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003140 len = PyString_Size(key) + PyString_Size(val) + 2;
3141 p = PyMem_NEW(char, len);
3142 if (p == NULL) {
3143 PyErr_NoMemory();
3144 goto fail_2;
3145 }
3146 PyOS_snprintf(p, len, "%s=%s", k, v);
3147 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003148#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003149 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003150#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003151 }
3152 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003154 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003155
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003156 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003157
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003158 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003159
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003160 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003161 while (--envc >= 0)
3162 PyMem_DEL(envlist[envc]);
3163 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003164 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003165 free_string_array(argvlist, lastarg);
3166 Py_XDECREF(vals);
3167 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003168 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003169 PyMem_Free(path);
3170 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003171}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003172#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174
Guido van Rossuma1065681999-01-25 23:20:23 +00003175#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003176PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003177"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003178Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003179\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003180 mode: mode of process creation\n\
3181 path: path of executable file\n\
3182 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003183
3184static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003185posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003186{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003187 char *path;
3188 PyObject *argv;
3189 char **argvlist;
3190 int mode, i;
3191 Py_ssize_t argc;
3192 Py_intptr_t spawnval;
3193 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003194
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003195 /* spawnv has three arguments: (mode, path, argv), where
3196 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003197
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003198 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3199 Py_FileSystemDefaultEncoding,
3200 &path, &argv))
3201 return NULL;
3202 if (PyList_Check(argv)) {
3203 argc = PyList_Size(argv);
3204 getitem = PyList_GetItem;
3205 }
3206 else if (PyTuple_Check(argv)) {
3207 argc = PyTuple_Size(argv);
3208 getitem = PyTuple_GetItem;
3209 }
3210 else {
3211 PyErr_SetString(PyExc_TypeError,
3212 "spawnv() arg 2 must be a tuple or list");
3213 PyMem_Free(path);
3214 return NULL;
3215 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003216
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003217 argvlist = PyMem_NEW(char *, argc+1);
3218 if (argvlist == NULL) {
3219 PyMem_Free(path);
3220 return PyErr_NoMemory();
3221 }
3222 for (i = 0; i < argc; i++) {
3223 if (!PyArg_Parse((*getitem)(argv, i), "et",
3224 Py_FileSystemDefaultEncoding,
3225 &argvlist[i])) {
3226 free_string_array(argvlist, i);
3227 PyErr_SetString(
3228 PyExc_TypeError,
3229 "spawnv() arg 2 must contain only strings");
3230 PyMem_Free(path);
3231 return NULL;
3232 }
3233 }
3234 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003235
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003236#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003237 Py_BEGIN_ALLOW_THREADS
3238 spawnval = spawnv(mode, path, argvlist);
3239 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003240#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003241 if (mode == _OLD_P_OVERLAY)
3242 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003243
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003244 Py_BEGIN_ALLOW_THREADS
3245 spawnval = _spawnv(mode, path, argvlist);
3246 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003247#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003248
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003249 free_string_array(argvlist, argc);
3250 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003251
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003252 if (spawnval == -1)
3253 return posix_error();
3254 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003255#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003256 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003257#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003258 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003259#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003260}
3261
3262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003263PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003264"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003265Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003266\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003267 mode: mode of process creation\n\
3268 path: path of executable file\n\
3269 args: tuple or list of arguments\n\
3270 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003271
3272static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003273posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003274{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003275 char *path;
3276 PyObject *argv, *env;
3277 char **argvlist;
3278 char **envlist;
3279 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3280 int mode, pos, envc;
3281 Py_ssize_t argc, i;
3282 Py_intptr_t spawnval;
3283 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3284 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003285
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003286 /* spawnve has four arguments: (mode, path, argv, env), where
3287 argv is a list or tuple of strings and env is a dictionary
3288 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003289
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003290 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3291 Py_FileSystemDefaultEncoding,
3292 &path, &argv, &env))
3293 return NULL;
3294 if (PyList_Check(argv)) {
3295 argc = PyList_Size(argv);
3296 getitem = PyList_GetItem;
3297 }
3298 else if (PyTuple_Check(argv)) {
3299 argc = PyTuple_Size(argv);
3300 getitem = PyTuple_GetItem;
3301 }
3302 else {
3303 PyErr_SetString(PyExc_TypeError,
3304 "spawnve() arg 2 must be a tuple or list");
3305 goto fail_0;
3306 }
3307 if (!PyMapping_Check(env)) {
3308 PyErr_SetString(PyExc_TypeError,
3309 "spawnve() arg 3 must be a mapping object");
3310 goto fail_0;
3311 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003312
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003313 argvlist = PyMem_NEW(char *, argc+1);
3314 if (argvlist == NULL) {
3315 PyErr_NoMemory();
3316 goto fail_0;
3317 }
3318 for (i = 0; i < argc; i++) {
3319 if (!PyArg_Parse((*getitem)(argv, i),
3320 "et;spawnve() arg 2 must contain only strings",
3321 Py_FileSystemDefaultEncoding,
3322 &argvlist[i]))
3323 {
3324 lastarg = i;
3325 goto fail_1;
3326 }
3327 }
3328 lastarg = argc;
3329 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003330
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003331 i = PyMapping_Size(env);
3332 if (i < 0)
3333 goto fail_1;
3334 envlist = PyMem_NEW(char *, i + 1);
3335 if (envlist == NULL) {
3336 PyErr_NoMemory();
3337 goto fail_1;
3338 }
3339 envc = 0;
3340 keys = PyMapping_Keys(env);
3341 vals = PyMapping_Values(env);
3342 if (!keys || !vals)
3343 goto fail_2;
3344 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3345 PyErr_SetString(PyExc_TypeError,
3346 "spawnve(): env.keys() or env.values() is not a list");
3347 goto fail_2;
3348 }
Tim Peters5aa91602002-01-30 05:46:57 +00003349
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003350 for (pos = 0; pos < i; pos++) {
3351 char *p, *k, *v;
3352 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003353
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003354 key = PyList_GetItem(keys, pos);
3355 val = PyList_GetItem(vals, pos);
3356 if (!key || !val)
3357 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003358
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003359 if (!PyArg_Parse(
3360 key,
3361 "s;spawnve() arg 3 contains a non-string key",
3362 &k) ||
3363 !PyArg_Parse(
3364 val,
3365 "s;spawnve() arg 3 contains a non-string value",
3366 &v))
3367 {
3368 goto fail_2;
3369 }
3370 len = PyString_Size(key) + PyString_Size(val) + 2;
3371 p = PyMem_NEW(char, len);
3372 if (p == NULL) {
3373 PyErr_NoMemory();
3374 goto fail_2;
3375 }
3376 PyOS_snprintf(p, len, "%s=%s", k, v);
3377 envlist[envc++] = p;
3378 }
3379 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003380
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003381#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003382 Py_BEGIN_ALLOW_THREADS
3383 spawnval = spawnve(mode, path, argvlist, envlist);
3384 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003385#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003386 if (mode == _OLD_P_OVERLAY)
3387 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003388
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003389 Py_BEGIN_ALLOW_THREADS
3390 spawnval = _spawnve(mode, path, argvlist, envlist);
3391 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003392#endif
Tim Peters25059d32001-12-07 20:35:43 +00003393
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003394 if (spawnval == -1)
3395 (void) posix_error();
3396 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003397#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003398 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003399#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003400 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003401#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003402
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003403 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003404 while (--envc >= 0)
3405 PyMem_DEL(envlist[envc]);
3406 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003407 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003408 free_string_array(argvlist, lastarg);
3409 Py_XDECREF(vals);
3410 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003411 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003412 PyMem_Free(path);
3413 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003414}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003415
3416/* OS/2 supports spawnvp & spawnvpe natively */
3417#if defined(PYOS_OS2)
3418PyDoc_STRVAR(posix_spawnvp__doc__,
3419"spawnvp(mode, file, args)\n\n\
3420Execute the program 'file' in a new process, using the environment\n\
3421search path to find the file.\n\
3422\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003423 mode: mode of process creation\n\
3424 file: executable file name\n\
3425 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003426
3427static PyObject *
3428posix_spawnvp(PyObject *self, PyObject *args)
3429{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003430 char *path;
3431 PyObject *argv;
3432 char **argvlist;
3433 int mode, i, argc;
3434 Py_intptr_t spawnval;
3435 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003436
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003437 /* spawnvp has three arguments: (mode, path, argv), where
3438 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003439
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003440 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3441 Py_FileSystemDefaultEncoding,
3442 &path, &argv))
3443 return NULL;
3444 if (PyList_Check(argv)) {
3445 argc = PyList_Size(argv);
3446 getitem = PyList_GetItem;
3447 }
3448 else if (PyTuple_Check(argv)) {
3449 argc = PyTuple_Size(argv);
3450 getitem = PyTuple_GetItem;
3451 }
3452 else {
3453 PyErr_SetString(PyExc_TypeError,
3454 "spawnvp() arg 2 must be a tuple or list");
3455 PyMem_Free(path);
3456 return NULL;
3457 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003458
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003459 argvlist = PyMem_NEW(char *, argc+1);
3460 if (argvlist == NULL) {
3461 PyMem_Free(path);
3462 return PyErr_NoMemory();
3463 }
3464 for (i = 0; i < argc; i++) {
3465 if (!PyArg_Parse((*getitem)(argv, i), "et",
3466 Py_FileSystemDefaultEncoding,
3467 &argvlist[i])) {
3468 free_string_array(argvlist, i);
3469 PyErr_SetString(
3470 PyExc_TypeError,
3471 "spawnvp() arg 2 must contain only strings");
3472 PyMem_Free(path);
3473 return NULL;
3474 }
3475 }
3476 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003477
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003478 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003479#if defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003480 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003481#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003482 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003483#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003484 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003485
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003486 free_string_array(argvlist, argc);
3487 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003488
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003489 if (spawnval == -1)
3490 return posix_error();
3491 else
3492 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003493}
3494
3495
3496PyDoc_STRVAR(posix_spawnvpe__doc__,
3497"spawnvpe(mode, file, args, env)\n\n\
3498Execute the program 'file' in a new process, using the environment\n\
3499search path to find the file.\n\
3500\n\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003501 mode: mode of process creation\n\
3502 file: executable file name\n\
3503 args: tuple or list of arguments\n\
3504 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003505
3506static PyObject *
3507posix_spawnvpe(PyObject *self, PyObject *args)
3508{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003509 char *path;
3510 PyObject *argv, *env;
3511 char **argvlist;
3512 char **envlist;
3513 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3514 int mode, i, pos, argc, envc;
3515 Py_intptr_t spawnval;
3516 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3517 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003518
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003519 /* spawnvpe has four arguments: (mode, path, argv, env), where
3520 argv is a list or tuple of strings and env is a dictionary
3521 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003522
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003523 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3524 Py_FileSystemDefaultEncoding,
3525 &path, &argv, &env))
3526 return NULL;
3527 if (PyList_Check(argv)) {
3528 argc = PyList_Size(argv);
3529 getitem = PyList_GetItem;
3530 }
3531 else if (PyTuple_Check(argv)) {
3532 argc = PyTuple_Size(argv);
3533 getitem = PyTuple_GetItem;
3534 }
3535 else {
3536 PyErr_SetString(PyExc_TypeError,
3537 "spawnvpe() arg 2 must be a tuple or list");
3538 goto fail_0;
3539 }
3540 if (!PyMapping_Check(env)) {
3541 PyErr_SetString(PyExc_TypeError,
3542 "spawnvpe() arg 3 must be a mapping object");
3543 goto fail_0;
3544 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003545
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003546 argvlist = PyMem_NEW(char *, argc+1);
3547 if (argvlist == NULL) {
3548 PyErr_NoMemory();
3549 goto fail_0;
3550 }
3551 for (i = 0; i < argc; i++) {
3552 if (!PyArg_Parse((*getitem)(argv, i),
3553 "et;spawnvpe() arg 2 must contain only strings",
3554 Py_FileSystemDefaultEncoding,
3555 &argvlist[i]))
3556 {
3557 lastarg = i;
3558 goto fail_1;
3559 }
3560 }
3561 lastarg = argc;
3562 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003563
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003564 i = PyMapping_Size(env);
3565 if (i < 0)
3566 goto fail_1;
3567 envlist = PyMem_NEW(char *, i + 1);
3568 if (envlist == NULL) {
3569 PyErr_NoMemory();
3570 goto fail_1;
3571 }
3572 envc = 0;
3573 keys = PyMapping_Keys(env);
3574 vals = PyMapping_Values(env);
3575 if (!keys || !vals)
3576 goto fail_2;
3577 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3578 PyErr_SetString(PyExc_TypeError,
3579 "spawnvpe(): env.keys() or env.values() is not a list");
3580 goto fail_2;
3581 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003582
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003583 for (pos = 0; pos < i; pos++) {
3584 char *p, *k, *v;
3585 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003586
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003587 key = PyList_GetItem(keys, pos);
3588 val = PyList_GetItem(vals, pos);
3589 if (!key || !val)
3590 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003591
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003592 if (!PyArg_Parse(
3593 key,
3594 "s;spawnvpe() arg 3 contains a non-string key",
3595 &k) ||
3596 !PyArg_Parse(
3597 val,
3598 "s;spawnvpe() arg 3 contains a non-string value",
3599 &v))
3600 {
3601 goto fail_2;
3602 }
3603 len = PyString_Size(key) + PyString_Size(val) + 2;
3604 p = PyMem_NEW(char, len);
3605 if (p == NULL) {
3606 PyErr_NoMemory();
3607 goto fail_2;
3608 }
3609 PyOS_snprintf(p, len, "%s=%s", k, v);
3610 envlist[envc++] = p;
3611 }
3612 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003613
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003614 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003615#if defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003616 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003617#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003618 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003619#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003620 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003621
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003622 if (spawnval == -1)
3623 (void) posix_error();
3624 else
3625 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003626
3627 fail_2:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003628 while (--envc >= 0)
3629 PyMem_DEL(envlist[envc]);
3630 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003631 fail_1:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003632 free_string_array(argvlist, lastarg);
3633 Py_XDECREF(vals);
3634 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003635 fail_0:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003636 PyMem_Free(path);
3637 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003638}
3639#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003640#endif /* HAVE_SPAWNV */
3641
3642
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003643#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003644PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003645"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003646Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3647\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003648Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003649
3650static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003651posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003652{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003653 pid_t pid;
3654 int result = 0;
3655 _PyImport_AcquireLock();
3656 pid = fork1();
3657 if (pid == 0) {
3658 /* child: this clobbers and resets the import lock. */
3659 PyOS_AfterFork();
3660 } else {
3661 /* parent: release the import lock. */
3662 result = _PyImport_ReleaseLock();
3663 }
3664 if (pid == -1)
3665 return posix_error();
3666 if (result < 0) {
3667 /* Don't clobber the OSError if the fork failed. */
3668 PyErr_SetString(PyExc_RuntimeError,
3669 "not holding the import lock");
3670 return NULL;
3671 }
3672 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003673}
3674#endif
3675
3676
Guido van Rossumad0ee831995-03-01 10:34:45 +00003677#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003678PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003679"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003680Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003681Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003682
Barry Warsaw53699e91996-12-10 23:23:01 +00003683static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003684posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003685{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003686 pid_t pid;
3687 int result = 0;
3688 _PyImport_AcquireLock();
3689 pid = fork();
3690 if (pid == 0) {
3691 /* child: this clobbers and resets the import lock. */
3692 PyOS_AfterFork();
3693 } else {
3694 /* parent: release the import lock. */
3695 result = _PyImport_ReleaseLock();
3696 }
3697 if (pid == -1)
3698 return posix_error();
3699 if (result < 0) {
3700 /* Don't clobber the OSError if the fork failed. */
3701 PyErr_SetString(PyExc_RuntimeError,
3702 "not holding the import lock");
3703 return NULL;
3704 }
3705 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003706}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003707#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003708
Neal Norwitzb59798b2003-03-21 01:43:31 +00003709/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003710/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3711#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003712#define DEV_PTY_FILE "/dev/ptc"
3713#define HAVE_DEV_PTMX
3714#else
3715#define DEV_PTY_FILE "/dev/ptmx"
3716#endif
3717
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003718#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003719#ifdef HAVE_PTY_H
3720#include <pty.h>
3721#else
3722#ifdef HAVE_LIBUTIL_H
3723#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003724#endif /* HAVE_LIBUTIL_H */
3725#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003726#ifdef HAVE_STROPTS_H
3727#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003728#endif
3729#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003730
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003731#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003732PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003733"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003734Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003735
3736static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003737posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003738{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003739 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003740#ifndef HAVE_OPENPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003741 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003742#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003743#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003744 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003745#ifdef sun
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003746 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003747#endif
3748#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003749
Thomas Wouters70c21a12000-07-14 14:28:33 +00003750#ifdef HAVE_OPENPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003751 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3752 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003753#elif defined(HAVE__GETPTY)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003754 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3755 if (slave_name == NULL)
3756 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003757
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003758 slave_fd = open(slave_name, O_RDWR);
3759 if (slave_fd < 0)
3760 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003761#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003762 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3763 if (master_fd < 0)
3764 return posix_error();
3765 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3766 /* change permission of slave */
3767 if (grantpt(master_fd) < 0) {
3768 PyOS_setsig(SIGCHLD, sig_saved);
3769 return posix_error();
3770 }
3771 /* unlock slave */
3772 if (unlockpt(master_fd) < 0) {
3773 PyOS_setsig(SIGCHLD, sig_saved);
3774 return posix_error();
3775 }
3776 PyOS_setsig(SIGCHLD, sig_saved);
3777 slave_name = ptsname(master_fd); /* get name of slave */
3778 if (slave_name == NULL)
3779 return posix_error();
3780 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3781 if (slave_fd < 0)
3782 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003783#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003784 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3785 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003786#ifndef __hpux
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003787 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003788#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003789#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003790#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003791
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003792 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003793
Fred Drake8cef4cf2000-06-28 16:40:38 +00003794}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003795#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003796
3797#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003798PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003799"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003800Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3801Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003802To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003803
3804static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003805posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003806{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003807 int master_fd = -1, result = 0;
3808 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003809
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003810 _PyImport_AcquireLock();
3811 pid = forkpty(&master_fd, NULL, NULL, NULL);
3812 if (pid == 0) {
3813 /* child: this clobbers and resets the import lock. */
3814 PyOS_AfterFork();
3815 } else {
3816 /* parent: release the import lock. */
3817 result = _PyImport_ReleaseLock();
3818 }
3819 if (pid == -1)
3820 return posix_error();
3821 if (result < 0) {
3822 /* Don't clobber the OSError if the fork failed. */
3823 PyErr_SetString(PyExc_RuntimeError,
3824 "not holding the import lock");
3825 return NULL;
3826 }
3827 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003828}
3829#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003830
Guido van Rossumad0ee831995-03-01 10:34:45 +00003831#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003832PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003833"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003834Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003835
Barry Warsaw53699e91996-12-10 23:23:01 +00003836static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003837posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003838{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003839 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003840}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003841#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003842
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003843
Guido van Rossumad0ee831995-03-01 10:34:45 +00003844#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003845PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003846"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003847Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003848
Barry Warsaw53699e91996-12-10 23:23:01 +00003849static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003850posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003851{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003852 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003853}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003854#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003855
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003856
Guido van Rossumad0ee831995-03-01 10:34:45 +00003857#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003858PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003859"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003860Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003861
Barry Warsaw53699e91996-12-10 23:23:01 +00003862static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003863posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003864{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003865 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003866}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003867#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003868
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003870PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003871"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003872Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003873
Barry Warsaw53699e91996-12-10 23:23:01 +00003874static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003875posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003876{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003877 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003878}
3879
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003880
Fred Drakec9680921999-12-13 16:37:25 +00003881#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003882PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003883"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003884Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003885
3886static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003887posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003888{
3889 PyObject *result = NULL;
3890
Fred Drakec9680921999-12-13 16:37:25 +00003891#ifdef NGROUPS_MAX
3892#define MAX_GROUPS NGROUPS_MAX
3893#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003894 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003895#define MAX_GROUPS 64
3896#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003897 gid_t grouplist[MAX_GROUPS];
3898 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003899
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003900 n = getgroups(MAX_GROUPS, grouplist);
3901 if (n < 0)
3902 posix_error();
3903 else {
3904 result = PyList_New(n);
3905 if (result != NULL) {
3906 int i;
3907 for (i = 0; i < n; ++i) {
3908 PyObject *o = PyInt_FromLong((long)grouplist[i]);
3909 if (o == NULL) {
3910 Py_DECREF(result);
3911 result = NULL;
3912 break;
Fred Drakec9680921999-12-13 16:37:25 +00003913 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003914 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003915 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003916 }
3917 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003918
Fred Drakec9680921999-12-13 16:37:25 +00003919 return result;
3920}
3921#endif
3922
Martin v. Löwis606edc12002-06-13 21:09:11 +00003923#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003924PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003925"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003926Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003927
3928static PyObject *
3929posix_getpgid(PyObject *self, PyObject *args)
3930{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003931 pid_t pid, pgid;
3932 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
3933 return NULL;
3934 pgid = getpgid(pid);
3935 if (pgid < 0)
3936 return posix_error();
3937 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003938}
3939#endif /* HAVE_GETPGID */
3940
3941
Guido van Rossumb6775db1994-08-01 11:34:53 +00003942#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003943PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003944"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003945Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003946
Barry Warsaw53699e91996-12-10 23:23:01 +00003947static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003948posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003949{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003950#ifdef GETPGRP_HAVE_ARG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003951 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003952#else /* GETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003953 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003954#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003955}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003956#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003957
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003958
Guido van Rossumb6775db1994-08-01 11:34:53 +00003959#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003960PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003961"setpgrp()\n\n\
Senthil Kumaran4fb51b42010-06-17 16:41:47 +00003962Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003963
Barry Warsaw53699e91996-12-10 23:23:01 +00003964static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003965posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003966{
Guido van Rossum64933891994-10-20 21:56:42 +00003967#ifdef SETPGRP_HAVE_ARG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003968 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003969#else /* SETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003970 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003971#endif /* SETPGRP_HAVE_ARG */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003972 return posix_error();
3973 Py_INCREF(Py_None);
3974 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003975}
3976
Guido van Rossumb6775db1994-08-01 11:34:53 +00003977#endif /* HAVE_SETPGRP */
3978
Guido van Rossumad0ee831995-03-01 10:34:45 +00003979#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003980PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003981"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003982Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003983
Barry Warsaw53699e91996-12-10 23:23:01 +00003984static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003985posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003986{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00003987 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003988}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003989#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003991
Fred Drake12c6e2d1999-12-14 21:25:03 +00003992#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003993PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003994"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003995Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003996
3997static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003998posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003999{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004000 PyObject *result = NULL;
4001 char *name;
4002 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004003
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004004 errno = 0;
4005 name = getlogin();
4006 if (name == NULL) {
4007 if (errno)
4008 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004009 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004010 PyErr_SetString(PyExc_OSError,
4011 "unable to determine login name");
4012 }
4013 else
4014 result = PyString_FromString(name);
4015 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004016
Fred Drake12c6e2d1999-12-14 21:25:03 +00004017 return result;
4018}
4019#endif
4020
Guido van Rossumad0ee831995-03-01 10:34:45 +00004021#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004022PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004023"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004024Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004025
Barry Warsaw53699e91996-12-10 23:23:01 +00004026static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004027posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004028{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004029 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004030}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004031#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004033
Guido van Rossumad0ee831995-03-01 10:34:45 +00004034#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004035PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004036"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004037Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004038
Barry Warsaw53699e91996-12-10 23:23:01 +00004039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004040posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004041{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004042 pid_t pid;
4043 int sig;
4044 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4045 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004046#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004047 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4048 APIRET rc;
4049 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004050 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004051
4052 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4053 APIRET rc;
4054 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004055 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004056
4057 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004058 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004059#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004060 if (kill(pid, sig) == -1)
4061 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004062#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004063 Py_INCREF(Py_None);
4064 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004065}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004066#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004067
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004068#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004069PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004070"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004071Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004072
4073static PyObject *
4074posix_killpg(PyObject *self, PyObject *args)
4075{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004076 int sig;
4077 pid_t pgid;
4078 /* XXX some man pages make the `pgid` parameter an int, others
4079 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4080 take the same type. Moreover, pid_t is always at least as wide as
4081 int (else compilation of this module fails), which is safe. */
4082 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4083 return NULL;
4084 if (killpg(pgid, sig) == -1)
4085 return posix_error();
4086 Py_INCREF(Py_None);
4087 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004088}
4089#endif
4090
Guido van Rossumc0125471996-06-28 18:55:32 +00004091#ifdef HAVE_PLOCK
4092
4093#ifdef HAVE_SYS_LOCK_H
4094#include <sys/lock.h>
4095#endif
4096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004097PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004098"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004099Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004100
Barry Warsaw53699e91996-12-10 23:23:01 +00004101static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004102posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004103{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004104 int op;
4105 if (!PyArg_ParseTuple(args, "i:plock", &op))
4106 return NULL;
4107 if (plock(op) == -1)
4108 return posix_error();
4109 Py_INCREF(Py_None);
4110 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004111}
4112#endif
4113
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004114
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004115#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004116PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004117"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004118Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004119
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004120#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004121#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004122static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004123async_system(const char *command)
4124{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004125 char errormsg[256], args[1024];
4126 RESULTCODES rcodes;
4127 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004128
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004129 char *shell = getenv("COMSPEC");
4130 if (!shell)
4131 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004132
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004133 /* avoid overflowing the argument buffer */
4134 if (strlen(shell) + 3 + strlen(command) >= 1024)
4135 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004136
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004137 args[0] = '\0';
4138 strcat(args, shell);
4139 strcat(args, "/c ");
4140 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004141
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004142 /* execute asynchronously, inheriting the environment */
4143 rc = DosExecPgm(errormsg,
4144 sizeof(errormsg),
4145 EXEC_ASYNC,
4146 args,
4147 NULL,
4148 &rcodes,
4149 shell);
4150 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004151}
4152
Guido van Rossumd48f2521997-12-05 22:19:34 +00004153static FILE *
4154popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004155{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004156 int oldfd, tgtfd;
4157 HFILE pipeh[2];
4158 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004159
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004160 /* mode determines which of stdin or stdout is reconnected to
4161 * the pipe to the child
4162 */
4163 if (strchr(mode, 'r') != NULL) {
4164 tgt_fd = 1; /* stdout */
4165 } else if (strchr(mode, 'w')) {
4166 tgt_fd = 0; /* stdin */
4167 } else {
4168 *err = ERROR_INVALID_ACCESS;
4169 return NULL;
4170 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004171
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004172 /* setup the pipe */
4173 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4174 *err = rc;
4175 return NULL;
4176 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004177
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004178 /* prevent other threads accessing stdio */
4179 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004180
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004181 /* reconnect stdio and execute child */
4182 oldfd = dup(tgtfd);
4183 close(tgtfd);
4184 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4185 DosClose(pipeh[tgtfd]);
4186 rc = async_system(command);
4187 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004188
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004189 /* restore stdio */
4190 dup2(oldfd, tgtfd);
4191 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004192
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004193 /* allow other threads access to stdio */
4194 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004195
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004196 /* if execution of child was successful return file stream */
4197 if (rc == NO_ERROR)
4198 return fdopen(pipeh[1 - tgtfd], mode);
4199 else {
4200 DosClose(pipeh[1 - tgtfd]);
4201 *err = rc;
4202 return NULL;
4203 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004204}
4205
4206static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004207posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004208{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004209 char *name;
4210 char *mode = "r";
4211 int err, bufsize = -1;
4212 FILE *fp;
4213 PyObject *f;
4214 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4215 return NULL;
4216 Py_BEGIN_ALLOW_THREADS
4217 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4218 Py_END_ALLOW_THREADS
4219 if (fp == NULL)
4220 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004221
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004222 f = PyFile_FromFile(fp, name, mode, fclose);
4223 if (f != NULL)
4224 PyFile_SetBufSize(f, bufsize);
4225 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004226}
4227
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004228#elif defined(PYCC_GCC)
4229
4230/* standard posix version of popen() support */
4231static PyObject *
4232posix_popen(PyObject *self, PyObject *args)
4233{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004234 char *name;
4235 char *mode = "r";
4236 int bufsize = -1;
4237 FILE *fp;
4238 PyObject *f;
4239 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4240 return NULL;
4241 Py_BEGIN_ALLOW_THREADS
4242 fp = popen(name, mode);
4243 Py_END_ALLOW_THREADS
4244 if (fp == NULL)
4245 return posix_error();
4246 f = PyFile_FromFile(fp, name, mode, pclose);
4247 if (f != NULL)
4248 PyFile_SetBufSize(f, bufsize);
4249 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004250}
4251
4252/* fork() under OS/2 has lots'o'warts
4253 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4254 * most of this code is a ripoff of the win32 code, but using the
4255 * capabilities of EMX's C library routines
4256 */
4257
4258/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4259#define POPEN_1 1
4260#define POPEN_2 2
4261#define POPEN_3 3
4262#define POPEN_4 4
4263
4264static PyObject *_PyPopen(char *, int, int, int);
4265static int _PyPclose(FILE *file);
4266
4267/*
4268 * Internal dictionary mapping popen* file pointers to process handles,
4269 * for use when retrieving the process exit code. See _PyPclose() below
4270 * for more information on this dictionary's use.
4271 */
4272static PyObject *_PyPopenProcs = NULL;
4273
4274/* os2emx version of popen2()
4275 *
4276 * The result of this function is a pipe (file) connected to the
4277 * process's stdin, and a pipe connected to the process's stdout.
4278 */
4279
4280static PyObject *
4281os2emx_popen2(PyObject *self, PyObject *args)
4282{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004283 PyObject *f;
4284 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004285
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004286 char *cmdstring;
4287 char *mode = "t";
4288 int bufsize = -1;
4289 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4290 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004291
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004292 if (*mode == 't')
4293 tm = O_TEXT;
4294 else if (*mode != 'b') {
4295 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4296 return NULL;
4297 } else
4298 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004299
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004300 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004301
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004302 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004303}
4304
4305/*
4306 * Variation on os2emx.popen2
4307 *
4308 * The result of this function is 3 pipes - the process's stdin,
4309 * stdout and stderr
4310 */
4311
4312static PyObject *
4313os2emx_popen3(PyObject *self, PyObject *args)
4314{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004315 PyObject *f;
4316 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004317
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004318 char *cmdstring;
4319 char *mode = "t";
4320 int bufsize = -1;
4321 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4322 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004323
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004324 if (*mode == 't')
4325 tm = O_TEXT;
4326 else if (*mode != 'b') {
4327 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4328 return NULL;
4329 } else
4330 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004331
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004332 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004333
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004334 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004335}
4336
4337/*
4338 * Variation on os2emx.popen2
4339 *
Tim Peters11b23062003-04-23 02:39:17 +00004340 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004341 * and stdout+stderr combined as a single pipe.
4342 */
4343
4344static PyObject *
4345os2emx_popen4(PyObject *self, PyObject *args)
4346{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004347 PyObject *f;
4348 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004349
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004350 char *cmdstring;
4351 char *mode = "t";
4352 int bufsize = -1;
4353 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4354 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004355
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004356 if (*mode == 't')
4357 tm = O_TEXT;
4358 else if (*mode != 'b') {
4359 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4360 return NULL;
4361 } else
4362 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004363
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004364 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004365
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004366 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004367}
4368
4369/* a couple of structures for convenient handling of multiple
4370 * file handles and pipes
4371 */
4372struct file_ref
4373{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004374 int handle;
4375 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004376};
4377
4378struct pipe_ref
4379{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004380 int rd;
4381 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004382};
4383
4384/* The following code is derived from the win32 code */
4385
4386static PyObject *
4387_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4388{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004389 struct file_ref stdio[3];
4390 struct pipe_ref p_fd[3];
4391 FILE *p_s[3];
4392 int file_count, i, pipe_err;
4393 pid_t pipe_pid;
4394 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4395 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004396
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004397 /* file modes for subsequent fdopen's on pipe handles */
4398 if (mode == O_TEXT)
4399 {
4400 rd_mode = "rt";
4401 wr_mode = "wt";
4402 }
4403 else
4404 {
4405 rd_mode = "rb";
4406 wr_mode = "wb";
4407 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004408
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004409 /* prepare shell references */
4410 if ((shell = getenv("EMXSHELL")) == NULL)
4411 if ((shell = getenv("COMSPEC")) == NULL)
4412 {
4413 errno = ENOENT;
4414 return posix_error();
4415 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004416
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004417 sh_name = _getname(shell);
4418 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4419 opt = "/c";
4420 else
4421 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004422
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004423 /* save current stdio fds + their flags, and set not inheritable */
4424 i = pipe_err = 0;
4425 while (pipe_err >= 0 && i < 3)
4426 {
4427 pipe_err = stdio[i].handle = dup(i);
4428 stdio[i].flags = fcntl(i, F_GETFD, 0);
4429 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4430 i++;
4431 }
4432 if (pipe_err < 0)
4433 {
4434 /* didn't get them all saved - clean up and bail out */
4435 int saved_err = errno;
4436 while (i-- > 0)
4437 {
4438 close(stdio[i].handle);
4439 }
4440 errno = saved_err;
4441 return posix_error();
4442 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004443
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004444 /* create pipe ends */
4445 file_count = 2;
4446 if (n == POPEN_3)
4447 file_count = 3;
4448 i = pipe_err = 0;
4449 while ((pipe_err == 0) && (i < file_count))
4450 pipe_err = pipe((int *)&p_fd[i++]);
4451 if (pipe_err < 0)
4452 {
4453 /* didn't get them all made - clean up and bail out */
4454 while (i-- > 0)
4455 {
4456 close(p_fd[i].wr);
4457 close(p_fd[i].rd);
4458 }
4459 errno = EPIPE;
4460 return posix_error();
4461 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004462
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004463 /* change the actual standard IO streams over temporarily,
4464 * making the retained pipe ends non-inheritable
4465 */
4466 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004467
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004468 /* - stdin */
4469 if (dup2(p_fd[0].rd, 0) == 0)
4470 {
4471 close(p_fd[0].rd);
4472 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4473 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4474 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4475 {
4476 close(p_fd[0].wr);
4477 pipe_err = -1;
4478 }
4479 }
4480 else
4481 {
4482 pipe_err = -1;
4483 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004484
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004485 /* - stdout */
4486 if (pipe_err == 0)
4487 {
4488 if (dup2(p_fd[1].wr, 1) == 1)
4489 {
4490 close(p_fd[1].wr);
4491 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4492 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4493 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4494 {
4495 close(p_fd[1].rd);
4496 pipe_err = -1;
4497 }
4498 }
4499 else
4500 {
4501 pipe_err = -1;
4502 }
4503 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004504
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004505 /* - stderr, as required */
4506 if (pipe_err == 0)
4507 switch (n)
4508 {
4509 case POPEN_3:
4510 {
4511 if (dup2(p_fd[2].wr, 2) == 2)
4512 {
4513 close(p_fd[2].wr);
4514 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4515 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4516 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4517 {
4518 close(p_fd[2].rd);
4519 pipe_err = -1;
4520 }
4521 }
4522 else
4523 {
4524 pipe_err = -1;
4525 }
4526 break;
4527 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004528
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004529 case POPEN_4:
4530 {
4531 if (dup2(1, 2) != 2)
4532 {
4533 pipe_err = -1;
4534 }
4535 break;
4536 }
4537 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004538
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004539 /* spawn the child process */
4540 if (pipe_err == 0)
4541 {
4542 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4543 if (pipe_pid == -1)
4544 {
4545 pipe_err = -1;
4546 }
4547 else
4548 {
4549 /* save the PID into the FILE structure
4550 * NOTE: this implementation doesn't actually
4551 * take advantage of this, but do it for
4552 * completeness - AIM Apr01
4553 */
4554 for (i = 0; i < file_count; i++)
4555 p_s[i]->_pid = pipe_pid;
4556 }
4557 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004558
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004559 /* reset standard IO to normal */
4560 for (i = 0; i < 3; i++)
4561 {
4562 dup2(stdio[i].handle, i);
4563 fcntl(i, F_SETFD, stdio[i].flags);
4564 close(stdio[i].handle);
4565 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004566
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004567 /* if any remnant problems, clean up and bail out */
4568 if (pipe_err < 0)
4569 {
4570 for (i = 0; i < 3; i++)
4571 {
4572 close(p_fd[i].rd);
4573 close(p_fd[i].wr);
4574 }
4575 errno = EPIPE;
4576 return posix_error_with_filename(cmdstring);
4577 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004578
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004579 /* build tuple of file objects to return */
4580 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4581 PyFile_SetBufSize(p_f[0], bufsize);
4582 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4583 PyFile_SetBufSize(p_f[1], bufsize);
4584 if (n == POPEN_3)
4585 {
4586 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4587 PyFile_SetBufSize(p_f[0], bufsize);
4588 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4589 }
4590 else
4591 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004592
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004593 /*
4594 * Insert the files we've created into the process dictionary
4595 * all referencing the list with the process handle and the
4596 * initial number of files (see description below in _PyPclose).
4597 * Since if _PyPclose later tried to wait on a process when all
4598 * handles weren't closed, it could create a deadlock with the
4599 * child, we spend some energy here to try to ensure that we
4600 * either insert all file handles into the dictionary or none
4601 * at all. It's a little clumsy with the various popen modes
4602 * and variable number of files involved.
4603 */
4604 if (!_PyPopenProcs)
4605 {
4606 _PyPopenProcs = PyDict_New();
4607 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004608
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004609 if (_PyPopenProcs)
4610 {
4611 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4612 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004613
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004614 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4615 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004616
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004617 procObj = PyList_New(2);
4618 pidObj = PyLong_FromPid(pipe_pid);
4619 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004620
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004621 if (procObj && pidObj && intObj)
4622 {
4623 PyList_SetItem(procObj, 0, pidObj);
4624 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004625
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004626 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4627 if (fileObj[0])
4628 {
4629 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4630 fileObj[0],
4631 procObj);
4632 }
4633 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4634 if (fileObj[1])
4635 {
4636 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4637 fileObj[1],
4638 procObj);
4639 }
4640 if (file_count >= 3)
4641 {
4642 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4643 if (fileObj[2])
4644 {
4645 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4646 fileObj[2],
4647 procObj);
4648 }
4649 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004650
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004651 if (ins_rc[0] < 0 || !fileObj[0] ||
4652 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4653 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4654 {
4655 /* Something failed - remove any dictionary
4656 * entries that did make it.
4657 */
4658 if (!ins_rc[0] && fileObj[0])
4659 {
4660 PyDict_DelItem(_PyPopenProcs,
4661 fileObj[0]);
4662 }
4663 if (!ins_rc[1] && fileObj[1])
4664 {
4665 PyDict_DelItem(_PyPopenProcs,
4666 fileObj[1]);
4667 }
4668 if (!ins_rc[2] && fileObj[2])
4669 {
4670 PyDict_DelItem(_PyPopenProcs,
4671 fileObj[2]);
4672 }
4673 }
4674 }
Tim Peters11b23062003-04-23 02:39:17 +00004675
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004676 /*
4677 * Clean up our localized references for the dictionary keys
4678 * and value since PyDict_SetItem will Py_INCREF any copies
4679 * that got placed in the dictionary.
4680 */
4681 Py_XDECREF(procObj);
4682 Py_XDECREF(fileObj[0]);
4683 Py_XDECREF(fileObj[1]);
4684 Py_XDECREF(fileObj[2]);
4685 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004686
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004687 /* Child is launched. */
4688 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004689}
4690
4691/*
4692 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4693 * exit code for the child process and return as a result of the close.
4694 *
4695 * This function uses the _PyPopenProcs dictionary in order to map the
4696 * input file pointer to information about the process that was
4697 * originally created by the popen* call that created the file pointer.
4698 * The dictionary uses the file pointer as a key (with one entry
4699 * inserted for each file returned by the original popen* call) and a
4700 * single list object as the value for all files from a single call.
4701 * The list object contains the Win32 process handle at [0], and a file
4702 * count at [1], which is initialized to the total number of file
4703 * handles using that list.
4704 *
4705 * This function closes whichever handle it is passed, and decrements
4706 * the file count in the dictionary for the process handle pointed to
4707 * by this file. On the last close (when the file count reaches zero),
4708 * this function will wait for the child process and then return its
4709 * exit code as the result of the close() operation. This permits the
4710 * files to be closed in any order - it is always the close() of the
4711 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004712 *
4713 * NOTE: This function is currently called with the GIL released.
4714 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004715 */
4716
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004717static int _PyPclose(FILE *file)
4718{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004719 int result;
4720 int exit_code;
4721 pid_t pipe_pid;
4722 PyObject *procObj, *pidObj, *intObj, *fileObj;
4723 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004724#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004725 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004726#endif
4727
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004728 /* Close the file handle first, to ensure it can't block the
4729 * child from exiting if it's the last handle.
4730 */
4731 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004732
4733#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004734 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004735#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004736 if (_PyPopenProcs)
4737 {
4738 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4739 (procObj = PyDict_GetItem(_PyPopenProcs,
4740 fileObj)) != NULL &&
4741 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4742 (intObj = PyList_GetItem(procObj,1)) != NULL)
4743 {
4744 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
4745 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004746
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004747 if (file_count > 1)
4748 {
4749 /* Still other files referencing process */
4750 file_count--;
4751 PyList_SetItem(procObj,1,
4752 PyInt_FromLong((long) file_count));
4753 }
4754 else
4755 {
4756 /* Last file for this process */
4757 if (result != EOF &&
4758 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4759 {
4760 /* extract exit status */
4761 if (WIFEXITED(exit_code))
4762 {
4763 result = WEXITSTATUS(exit_code);
4764 }
4765 else
4766 {
4767 errno = EPIPE;
4768 result = -1;
4769 }
4770 }
4771 else
4772 {
4773 /* Indicate failure - this will cause the file object
4774 * to raise an I/O error and translate the last
4775 * error code from errno. We do have a problem with
4776 * last errors that overlap the normal errno table,
4777 * but that's a consistent problem with the file object.
4778 */
4779 result = -1;
4780 }
4781 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004782
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004783 /* Remove this file pointer from dictionary */
4784 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004785
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004786 if (PyDict_Size(_PyPopenProcs) == 0)
4787 {
4788 Py_DECREF(_PyPopenProcs);
4789 _PyPopenProcs = NULL;
4790 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004791
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004792 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004793
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004794 Py_XDECREF(fileObj);
4795 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004796
4797#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004798 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004799#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004800 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004801}
4802
4803#endif /* PYCC_??? */
4804
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004805#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004806
4807/*
4808 * Portable 'popen' replacement for Win32.
4809 *
4810 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4811 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004812 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004813 */
4814
4815#include <malloc.h>
4816#include <io.h>
4817#include <fcntl.h>
4818
4819/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4820#define POPEN_1 1
4821#define POPEN_2 2
4822#define POPEN_3 3
4823#define POPEN_4 4
4824
4825static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004826static int _PyPclose(FILE *file);
4827
4828/*
4829 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004830 * for use when retrieving the process exit code. See _PyPclose() below
4831 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004832 */
4833static PyObject *_PyPopenProcs = NULL;
4834
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004835
4836/* popen that works from a GUI.
4837 *
4838 * The result of this function is a pipe (file) connected to the
4839 * processes stdin or stdout, depending on the requested mode.
4840 */
4841
4842static PyObject *
4843posix_popen(PyObject *self, PyObject *args)
4844{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004845 PyObject *f;
4846 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004847
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004848 char *cmdstring;
4849 char *mode = "r";
4850 int bufsize = -1;
4851 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
4852 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004853
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004854 if (*mode == 'r')
4855 tm = _O_RDONLY;
4856 else if (*mode != 'w') {
4857 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
4858 return NULL;
4859 } else
4860 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004861
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004862 if (bufsize != -1) {
4863 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
4864 return NULL;
4865 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004866
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004867 if (*(mode+1) == 't')
4868 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4869 else if (*(mode+1) == 'b')
4870 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
4871 else
4872 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004873
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004874 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004875}
4876
4877/* Variation on win32pipe.popen
4878 *
4879 * The result of this function is a pipe (file) connected to the
4880 * process's stdin, and a pipe connected to the process's stdout.
4881 */
4882
4883static PyObject *
4884win32_popen2(PyObject *self, PyObject *args)
4885{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004886 PyObject *f;
4887 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004888
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004889 char *cmdstring;
4890 char *mode = "t";
4891 int bufsize = -1;
4892 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4893 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004894
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004895 if (*mode == 't')
4896 tm = _O_TEXT;
4897 else if (*mode != 'b') {
4898 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
4899 return NULL;
4900 } else
4901 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004902
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004903 if (bufsize != -1) {
4904 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
4905 return NULL;
4906 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004907
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004908 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004909
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004910 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004911}
4912
4913/*
4914 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004915 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004916 * The result of this function is 3 pipes - the process's stdin,
4917 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004918 */
4919
4920static PyObject *
4921win32_popen3(PyObject *self, PyObject *args)
4922{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004923 PyObject *f;
4924 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004925
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004926 char *cmdstring;
4927 char *mode = "t";
4928 int bufsize = -1;
4929 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4930 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004931
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004932 if (*mode == 't')
4933 tm = _O_TEXT;
4934 else if (*mode != 'b') {
4935 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
4936 return NULL;
4937 } else
4938 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004939
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004940 if (bufsize != -1) {
4941 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
4942 return NULL;
4943 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004944
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004945 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004946
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004947 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004948}
4949
4950/*
4951 * Variation on win32pipe.popen
4952 *
Tim Peters5aa91602002-01-30 05:46:57 +00004953 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004954 * and stdout+stderr combined as a single pipe.
4955 */
4956
4957static PyObject *
4958win32_popen4(PyObject *self, PyObject *args)
4959{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004960 PyObject *f;
4961 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004962
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004963 char *cmdstring;
4964 char *mode = "t";
4965 int bufsize = -1;
4966 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4967 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004968
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004969 if (*mode == 't')
4970 tm = _O_TEXT;
4971 else if (*mode != 'b') {
4972 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
4973 return NULL;
4974 } else
4975 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004976
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004977 if (bufsize != -1) {
4978 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
4979 return NULL;
4980 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004981
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004982 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004983
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004984 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004985}
4986
Mark Hammond08501372001-01-31 07:30:29 +00004987static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004988_PyPopenCreateProcess(char *cmdstring,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004989 HANDLE hStdin,
4990 HANDLE hStdout,
4991 HANDLE hStderr,
4992 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004993{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00004994 PROCESS_INFORMATION piProcInfo;
4995 STARTUPINFO siStartInfo;
4996 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
4997 char *s1,*s2, *s3 = " /c ";
4998 const char *szConsoleSpawn = "w9xpopen.exe";
4999 int i;
5000 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005001
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005002 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
5003 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005004
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005005 s1 = (char *)alloca(i);
5006 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
5007 /* x < i, so x fits into an integer */
5008 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00005009
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005010 /* Explicitly check if we are using COMMAND.COM. If we are
5011 * then use the w9xpopen hack.
5012 */
5013 comshell = s1 + x;
5014 while (comshell >= s1 && *comshell != '\\')
5015 --comshell;
5016 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005017
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005018 if (GetVersion() < 0x80000000 &&
5019 _stricmp(comshell, "command.com") != 0) {
5020 /* NT/2000 and not using command.com. */
5021 x = i + strlen(s3) + strlen(cmdstring) + 1;
5022 s2 = (char *)alloca(x);
5023 ZeroMemory(s2, x);
5024 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5025 }
5026 else {
5027 /*
5028 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5029 * the workaround listed in KB: Q150956
5030 */
5031 char modulepath[_MAX_PATH];
5032 struct stat statinfo;
5033 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5034 for (x = i = 0; modulepath[i]; i++)
5035 if (modulepath[i] == SEP)
5036 x = i+1;
5037 modulepath[x] = '\0';
5038 /* Create the full-name to w9xpopen, so we can test it exists */
5039 strncat(modulepath,
5040 szConsoleSpawn,
5041 (sizeof(modulepath)/sizeof(modulepath[0]))
5042 -strlen(modulepath));
5043 if (stat(modulepath, &statinfo) != 0) {
5044 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5045 /* Eeek - file-not-found - possibly an embedding
5046 situation - see if we can locate it in sys.prefix
5047 */
5048 strncpy(modulepath,
5049 Py_GetExecPrefix(),
5050 mplen);
5051 modulepath[mplen-1] = '\0';
5052 if (modulepath[strlen(modulepath)-1] != '\\')
5053 strcat(modulepath, "\\");
5054 strncat(modulepath,
5055 szConsoleSpawn,
5056 mplen-strlen(modulepath));
5057 /* No where else to look - raise an easily identifiable
5058 error, rather than leaving Windows to report
5059 "file not found" - as the user is probably blissfully
5060 unaware this shim EXE is used, and it will confuse them.
5061 (well, it confused me for a while ;-)
5062 */
5063 if (stat(modulepath, &statinfo) != 0) {
5064 PyErr_Format(PyExc_RuntimeError,
5065 "Can not locate '%s' which is needed "
5066 "for popen to work with your shell "
5067 "or platform.",
5068 szConsoleSpawn);
5069 return FALSE;
5070 }
5071 }
5072 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5073 strlen(modulepath) +
5074 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005075
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005076 s2 = (char *)alloca(x);
5077 ZeroMemory(s2, x);
5078 /* To maintain correct argument passing semantics,
5079 we pass the command-line as it stands, and allow
5080 quoting to be applied. w9xpopen.exe will then
5081 use its argv vector, and re-quote the necessary
5082 args for the ultimate child process.
5083 */
5084 PyOS_snprintf(
5085 s2, x,
5086 "\"%s\" %s%s%s",
5087 modulepath,
5088 s1,
5089 s3,
5090 cmdstring);
5091 /* Not passing CREATE_NEW_CONSOLE has been known to
5092 cause random failures on win9x. Specifically a
5093 dialog:
5094 "Your program accessed mem currently in use at xxx"
5095 and a hopeful warning about the stability of your
5096 system.
5097 Cost is Ctrl+C won't kill children, but anyone
5098 who cares can have a go!
5099 */
5100 dwProcessFlags |= CREATE_NEW_CONSOLE;
5101 }
5102 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005103
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005104 /* Could be an else here to try cmd.exe / command.com in the path
5105 Now we'll just error out.. */
5106 else {
5107 PyErr_SetString(PyExc_RuntimeError,
5108 "Cannot locate a COMSPEC environment variable to "
5109 "use as the shell");
5110 return FALSE;
5111 }
Tim Peters5aa91602002-01-30 05:46:57 +00005112
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005113 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5114 siStartInfo.cb = sizeof(STARTUPINFO);
5115 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5116 siStartInfo.hStdInput = hStdin;
5117 siStartInfo.hStdOutput = hStdout;
5118 siStartInfo.hStdError = hStderr;
5119 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005120
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005121 if (CreateProcess(NULL,
5122 s2,
5123 NULL,
5124 NULL,
5125 TRUE,
5126 dwProcessFlags,
5127 NULL,
5128 NULL,
5129 &siStartInfo,
5130 &piProcInfo) ) {
5131 /* Close the handles now so anyone waiting is woken. */
5132 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005133
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005134 /* Return process handle */
5135 *hProcess = piProcInfo.hProcess;
5136 return TRUE;
5137 }
5138 win32_error("CreateProcess", s2);
5139 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005140}
5141
5142/* The following code is based off of KB: Q190351 */
5143
5144static PyObject *
5145_PyPopen(char *cmdstring, int mode, int n)
5146{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005147 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5148 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5149 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005150
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005151 SECURITY_ATTRIBUTES saAttr;
5152 BOOL fSuccess;
5153 int fd1, fd2, fd3;
5154 FILE *f1, *f2, *f3;
5155 long file_count;
5156 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005157
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005158 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5159 saAttr.bInheritHandle = TRUE;
5160 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005161
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005162 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5163 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005164
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005165 /* Create new output read handle and the input write handle. Set
5166 * the inheritance properties to FALSE. Otherwise, the child inherits
5167 * these handles; resulting in non-closeable handles to the pipes
5168 * being created. */
5169 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5170 GetCurrentProcess(), &hChildStdinWrDup, 0,
5171 FALSE,
5172 DUPLICATE_SAME_ACCESS);
5173 if (!fSuccess)
5174 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005175
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005176 /* Close the inheritable version of ChildStdin
5177 that we're using. */
5178 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005179
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005180 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5181 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005182
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005183 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5184 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5185 FALSE, DUPLICATE_SAME_ACCESS);
5186 if (!fSuccess)
5187 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005188
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005189 /* Close the inheritable version of ChildStdout
5190 that we're using. */
5191 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005192
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005193 if (n != POPEN_4) {
5194 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5195 return win32_error("CreatePipe", NULL);
5196 fSuccess = DuplicateHandle(GetCurrentProcess(),
5197 hChildStderrRd,
5198 GetCurrentProcess(),
5199 &hChildStderrRdDup, 0,
5200 FALSE, DUPLICATE_SAME_ACCESS);
5201 if (!fSuccess)
5202 return win32_error("DuplicateHandle", NULL);
5203 /* Close the inheritable version of ChildStdErr that we're using. */
5204 CloseHandle(hChildStderrRd);
5205 }
Tim Peters5aa91602002-01-30 05:46:57 +00005206
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005207 switch (n) {
5208 case POPEN_1:
5209 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5210 case _O_WRONLY | _O_TEXT:
5211 /* Case for writing to child Stdin in text mode. */
5212 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5213 f1 = _fdopen(fd1, "w");
5214 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5215 PyFile_SetBufSize(f, 0);
5216 /* We don't care about these pipes anymore, so close them. */
5217 CloseHandle(hChildStdoutRdDup);
5218 CloseHandle(hChildStderrRdDup);
5219 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005220
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005221 case _O_RDONLY | _O_TEXT:
5222 /* Case for reading from child Stdout in text mode. */
5223 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5224 f1 = _fdopen(fd1, "r");
5225 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5226 PyFile_SetBufSize(f, 0);
5227 /* We don't care about these pipes anymore, so close them. */
5228 CloseHandle(hChildStdinWrDup);
5229 CloseHandle(hChildStderrRdDup);
5230 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005231
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005232 case _O_RDONLY | _O_BINARY:
5233 /* Case for readinig from child Stdout in binary mode. */
5234 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5235 f1 = _fdopen(fd1, "rb");
5236 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5237 PyFile_SetBufSize(f, 0);
5238 /* We don't care about these pipes anymore, so close them. */
5239 CloseHandle(hChildStdinWrDup);
5240 CloseHandle(hChildStderrRdDup);
5241 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005242
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005243 case _O_WRONLY | _O_BINARY:
5244 /* Case for writing to child Stdin in binary mode. */
5245 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5246 f1 = _fdopen(fd1, "wb");
5247 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5248 PyFile_SetBufSize(f, 0);
5249 /* We don't care about these pipes anymore, so close them. */
5250 CloseHandle(hChildStdoutRdDup);
5251 CloseHandle(hChildStderrRdDup);
5252 break;
5253 }
5254 file_count = 1;
5255 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005256
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005257 case POPEN_2:
5258 case POPEN_4:
5259 {
5260 char *m1, *m2;
5261 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005262
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005263 if (mode & _O_TEXT) {
5264 m1 = "r";
5265 m2 = "w";
5266 } else {
5267 m1 = "rb";
5268 m2 = "wb";
5269 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005270
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005271 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5272 f1 = _fdopen(fd1, m2);
5273 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5274 f2 = _fdopen(fd2, m1);
5275 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5276 PyFile_SetBufSize(p1, 0);
5277 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5278 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005279
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005280 if (n != 4)
5281 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005282
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005283 f = PyTuple_Pack(2,p1,p2);
5284 Py_XDECREF(p1);
5285 Py_XDECREF(p2);
5286 file_count = 2;
5287 break;
5288 }
Tim Peters5aa91602002-01-30 05:46:57 +00005289
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005290 case POPEN_3:
5291 {
5292 char *m1, *m2;
5293 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005294
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005295 if (mode & _O_TEXT) {
5296 m1 = "r";
5297 m2 = "w";
5298 } else {
5299 m1 = "rb";
5300 m2 = "wb";
5301 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005302
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005303 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5304 f1 = _fdopen(fd1, m2);
5305 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5306 f2 = _fdopen(fd2, m1);
5307 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5308 f3 = _fdopen(fd3, m1);
5309 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5310 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5311 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5312 PyFile_SetBufSize(p1, 0);
5313 PyFile_SetBufSize(p2, 0);
5314 PyFile_SetBufSize(p3, 0);
5315 f = PyTuple_Pack(3,p1,p2,p3);
5316 Py_XDECREF(p1);
5317 Py_XDECREF(p2);
5318 Py_XDECREF(p3);
5319 file_count = 3;
5320 break;
5321 }
5322 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005323
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005324 if (n == POPEN_4) {
5325 if (!_PyPopenCreateProcess(cmdstring,
5326 hChildStdinRd,
5327 hChildStdoutWr,
5328 hChildStdoutWr,
5329 &hProcess))
5330 return NULL;
5331 }
5332 else {
5333 if (!_PyPopenCreateProcess(cmdstring,
5334 hChildStdinRd,
5335 hChildStdoutWr,
5336 hChildStderrWr,
5337 &hProcess))
5338 return NULL;
5339 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005340
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005341 /*
5342 * Insert the files we've created into the process dictionary
5343 * all referencing the list with the process handle and the
5344 * initial number of files (see description below in _PyPclose).
5345 * Since if _PyPclose later tried to wait on a process when all
5346 * handles weren't closed, it could create a deadlock with the
5347 * child, we spend some energy here to try to ensure that we
5348 * either insert all file handles into the dictionary or none
5349 * at all. It's a little clumsy with the various popen modes
5350 * and variable number of files involved.
5351 */
5352 if (!_PyPopenProcs) {
5353 _PyPopenProcs = PyDict_New();
5354 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005355
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005356 if (_PyPopenProcs) {
5357 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5358 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005359
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005360 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5361 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005362
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005363 procObj = PyList_New(2);
5364 hProcessObj = PyLong_FromVoidPtr(hProcess);
5365 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005366
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005367 if (procObj && hProcessObj && intObj) {
5368 PyList_SetItem(procObj,0,hProcessObj);
5369 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005370
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005371 fileObj[0] = PyLong_FromVoidPtr(f1);
5372 if (fileObj[0]) {
5373 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5374 fileObj[0],
5375 procObj);
5376 }
5377 if (file_count >= 2) {
5378 fileObj[1] = PyLong_FromVoidPtr(f2);
5379 if (fileObj[1]) {
5380 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5381 fileObj[1],
5382 procObj);
5383 }
5384 }
5385 if (file_count >= 3) {
5386 fileObj[2] = PyLong_FromVoidPtr(f3);
5387 if (fileObj[2]) {
5388 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5389 fileObj[2],
5390 procObj);
5391 }
5392 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005393
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005394 if (ins_rc[0] < 0 || !fileObj[0] ||
5395 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5396 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5397 /* Something failed - remove any dictionary
5398 * entries that did make it.
5399 */
5400 if (!ins_rc[0] && fileObj[0]) {
5401 PyDict_DelItem(_PyPopenProcs,
5402 fileObj[0]);
5403 }
5404 if (!ins_rc[1] && fileObj[1]) {
5405 PyDict_DelItem(_PyPopenProcs,
5406 fileObj[1]);
5407 }
5408 if (!ins_rc[2] && fileObj[2]) {
5409 PyDict_DelItem(_PyPopenProcs,
5410 fileObj[2]);
5411 }
5412 }
5413 }
Tim Peters5aa91602002-01-30 05:46:57 +00005414
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005415 /*
5416 * Clean up our localized references for the dictionary keys
5417 * and value since PyDict_SetItem will Py_INCREF any copies
5418 * that got placed in the dictionary.
5419 */
5420 Py_XDECREF(procObj);
5421 Py_XDECREF(fileObj[0]);
5422 Py_XDECREF(fileObj[1]);
5423 Py_XDECREF(fileObj[2]);
5424 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005425
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005426 /* Child is launched. Close the parents copy of those pipe
5427 * handles that only the child should have open. You need to
5428 * make sure that no handles to the write end of the output pipe
5429 * are maintained in this process or else the pipe will not close
5430 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005431
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005432 if (!CloseHandle(hChildStdinRd))
5433 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005434
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005435 if (!CloseHandle(hChildStdoutWr))
5436 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005437
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005438 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5439 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005440
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005441 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005442}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005443
5444/*
5445 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5446 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005447 *
5448 * This function uses the _PyPopenProcs dictionary in order to map the
5449 * input file pointer to information about the process that was
5450 * originally created by the popen* call that created the file pointer.
5451 * The dictionary uses the file pointer as a key (with one entry
5452 * inserted for each file returned by the original popen* call) and a
5453 * single list object as the value for all files from a single call.
5454 * The list object contains the Win32 process handle at [0], and a file
5455 * count at [1], which is initialized to the total number of file
5456 * handles using that list.
5457 *
5458 * This function closes whichever handle it is passed, and decrements
5459 * the file count in the dictionary for the process handle pointed to
5460 * by this file. On the last close (when the file count reaches zero),
5461 * this function will wait for the child process and then return its
5462 * exit code as the result of the close() operation. This permits the
5463 * files to be closed in any order - it is always the close() of the
5464 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005465 *
5466 * NOTE: This function is currently called with the GIL released.
5467 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005468 */
Tim Peters736aa322000-09-01 06:51:24 +00005469
Fredrik Lundh56055a42000-07-23 19:47:12 +00005470static int _PyPclose(FILE *file)
5471{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005472 int result;
5473 DWORD exit_code;
5474 HANDLE hProcess;
5475 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5476 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005477#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005478 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005479#endif
5480
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005481 /* Close the file handle first, to ensure it can't block the
5482 * child from exiting if it's the last handle.
5483 */
5484 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005485#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005486 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005487#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005488 if (_PyPopenProcs) {
5489 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5490 (procObj = PyDict_GetItem(_PyPopenProcs,
5491 fileObj)) != NULL &&
5492 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5493 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005494
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005495 hProcess = PyLong_AsVoidPtr(hProcessObj);
5496 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005497
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005498 if (file_count > 1) {
5499 /* Still other files referencing process */
5500 file_count--;
5501 PyList_SetItem(procObj,1,
5502 PyInt_FromLong(file_count));
5503 } else {
5504 /* Last file for this process */
5505 if (result != EOF &&
5506 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5507 GetExitCodeProcess(hProcess, &exit_code)) {
5508 /* Possible truncation here in 16-bit environments, but
5509 * real exit codes are just the lower byte in any event.
5510 */
5511 result = exit_code;
5512 } else {
5513 /* Indicate failure - this will cause the file object
5514 * to raise an I/O error and translate the last Win32
5515 * error code from errno. We do have a problem with
5516 * last errors that overlap the normal errno table,
5517 * but that's a consistent problem with the file object.
5518 */
5519 if (result != EOF) {
5520 /* If the error wasn't from the fclose(), then
5521 * set errno for the file object error handling.
5522 */
5523 errno = GetLastError();
5524 }
5525 result = -1;
5526 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005527
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005528 /* Free up the native handle at this point */
5529 CloseHandle(hProcess);
5530 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005531
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005532 /* Remove this file pointer from dictionary */
5533 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005534
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005535 if (PyDict_Size(_PyPopenProcs) == 0) {
5536 Py_DECREF(_PyPopenProcs);
5537 _PyPopenProcs = NULL;
5538 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005539
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005540 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005541
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005542 Py_XDECREF(fileObj);
5543 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005544
Tim Peters736aa322000-09-01 06:51:24 +00005545#ifdef WITH_THREAD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005546 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005547#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005548 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005549}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005550
5551#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005553posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005554{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005555 char *name;
5556 char *mode = "r";
5557 int bufsize = -1;
5558 FILE *fp;
5559 PyObject *f;
5560 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5561 return NULL;
5562 /* Strip mode of binary or text modifiers */
5563 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5564 mode = "r";
5565 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5566 mode = "w";
5567 Py_BEGIN_ALLOW_THREADS
5568 fp = popen(name, mode);
5569 Py_END_ALLOW_THREADS
5570 if (fp == NULL)
5571 return posix_error();
5572 f = PyFile_FromFile(fp, name, mode, pclose);
5573 if (f != NULL)
5574 PyFile_SetBufSize(f, bufsize);
5575 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005576}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005577
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005578#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005579#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005580
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005581
Guido van Rossumb6775db1994-08-01 11:34:53 +00005582#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005583PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005584"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005585Set the current process's user id.");
5586
Barry Warsaw53699e91996-12-10 23:23:01 +00005587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005588posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005589{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005590 long uid_arg;
5591 uid_t uid;
5592 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5593 return NULL;
5594 uid = uid_arg;
5595 if (uid != uid_arg) {
5596 PyErr_SetString(PyExc_OverflowError, "user id too big");
5597 return NULL;
5598 }
5599 if (setuid(uid) < 0)
5600 return posix_error();
5601 Py_INCREF(Py_None);
5602 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005603}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005604#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005605
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005607#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005608PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005609"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005610Set the current process's effective user id.");
5611
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005612static PyObject *
5613posix_seteuid (PyObject *self, PyObject *args)
5614{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005615 long euid_arg;
5616 uid_t euid;
5617 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5618 return NULL;
5619 euid = euid_arg;
5620 if (euid != euid_arg) {
5621 PyErr_SetString(PyExc_OverflowError, "user id too big");
5622 return NULL;
5623 }
5624 if (seteuid(euid) < 0) {
5625 return posix_error();
5626 } else {
5627 Py_INCREF(Py_None);
5628 return Py_None;
5629 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005630}
5631#endif /* HAVE_SETEUID */
5632
5633#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005634PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005635"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005636Set the current process's effective group id.");
5637
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005638static PyObject *
5639posix_setegid (PyObject *self, PyObject *args)
5640{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005641 long egid_arg;
5642 gid_t egid;
5643 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5644 return NULL;
5645 egid = egid_arg;
5646 if (egid != egid_arg) {
5647 PyErr_SetString(PyExc_OverflowError, "group id too big");
5648 return NULL;
5649 }
5650 if (setegid(egid) < 0) {
5651 return posix_error();
5652 } else {
5653 Py_INCREF(Py_None);
5654 return Py_None;
5655 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005656}
5657#endif /* HAVE_SETEGID */
5658
5659#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005660PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005661"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005662Set the current process's real and effective user ids.");
5663
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005664static PyObject *
5665posix_setreuid (PyObject *self, PyObject *args)
5666{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005667 long ruid_arg, euid_arg;
5668 uid_t ruid, euid;
5669 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5670 return NULL;
5671 if (ruid_arg == -1)
5672 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5673 else
5674 ruid = ruid_arg; /* otherwise, assign from our long */
5675 if (euid_arg == -1)
5676 euid = (uid_t)-1;
5677 else
5678 euid = euid_arg;
5679 if ((euid_arg != -1 && euid != euid_arg) ||
5680 (ruid_arg != -1 && ruid != ruid_arg)) {
5681 PyErr_SetString(PyExc_OverflowError, "user id too big");
5682 return NULL;
5683 }
5684 if (setreuid(ruid, euid) < 0) {
5685 return posix_error();
5686 } else {
5687 Py_INCREF(Py_None);
5688 return Py_None;
5689 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005690}
5691#endif /* HAVE_SETREUID */
5692
5693#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005694PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005695"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005696Set the current process's real and effective group ids.");
5697
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005698static PyObject *
5699posix_setregid (PyObject *self, PyObject *args)
5700{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005701 long rgid_arg, egid_arg;
5702 gid_t rgid, egid;
5703 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5704 return NULL;
5705 if (rgid_arg == -1)
5706 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5707 else
5708 rgid = rgid_arg; /* otherwise, assign from our long */
5709 if (egid_arg == -1)
5710 egid = (gid_t)-1;
5711 else
5712 egid = egid_arg;
5713 if ((egid_arg != -1 && egid != egid_arg) ||
5714 (rgid_arg != -1 && rgid != rgid_arg)) {
5715 PyErr_SetString(PyExc_OverflowError, "group id too big");
5716 return NULL;
5717 }
5718 if (setregid(rgid, egid) < 0) {
5719 return posix_error();
5720 } else {
5721 Py_INCREF(Py_None);
5722 return Py_None;
5723 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00005724}
5725#endif /* HAVE_SETREGID */
5726
Guido van Rossumb6775db1994-08-01 11:34:53 +00005727#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005728PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005729"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005730Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005731
Barry Warsaw53699e91996-12-10 23:23:01 +00005732static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005733posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005734{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005735 long gid_arg;
5736 gid_t gid;
5737 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5738 return NULL;
5739 gid = gid_arg;
5740 if (gid != gid_arg) {
5741 PyErr_SetString(PyExc_OverflowError, "group id too big");
5742 return NULL;
5743 }
5744 if (setgid(gid) < 0)
5745 return posix_error();
5746 Py_INCREF(Py_None);
5747 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005748}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005749#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005750
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005751#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005752PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005753"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005755
5756static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005757posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005758{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005759 int i, len;
5760 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005761
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005762 if (!PySequence_Check(groups)) {
5763 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5764 return NULL;
5765 }
5766 len = PySequence_Size(groups);
5767 if (len > MAX_GROUPS) {
5768 PyErr_SetString(PyExc_ValueError, "too many groups");
5769 return NULL;
5770 }
5771 for(i = 0; i < len; i++) {
5772 PyObject *elem;
5773 elem = PySequence_GetItem(groups, i);
5774 if (!elem)
5775 return NULL;
5776 if (!PyInt_Check(elem)) {
5777 if (!PyLong_Check(elem)) {
5778 PyErr_SetString(PyExc_TypeError,
5779 "groups must be integers");
5780 Py_DECREF(elem);
5781 return NULL;
5782 } else {
5783 unsigned long x = PyLong_AsUnsignedLong(elem);
5784 if (PyErr_Occurred()) {
5785 PyErr_SetString(PyExc_TypeError,
5786 "group id too big");
5787 Py_DECREF(elem);
5788 return NULL;
5789 }
5790 grouplist[i] = x;
5791 /* read back to see if it fits in gid_t */
5792 if (grouplist[i] != x) {
5793 PyErr_SetString(PyExc_TypeError,
5794 "group id too big");
5795 Py_DECREF(elem);
5796 return NULL;
5797 }
5798 }
5799 } else {
5800 long x = PyInt_AsLong(elem);
5801 grouplist[i] = x;
5802 if (grouplist[i] != x) {
5803 PyErr_SetString(PyExc_TypeError,
5804 "group id too big");
5805 Py_DECREF(elem);
5806 return NULL;
5807 }
5808 }
5809 Py_DECREF(elem);
5810 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005811
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005812 if (setgroups(len, grouplist) < 0)
5813 return posix_error();
5814 Py_INCREF(Py_None);
5815 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005816}
5817#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005818
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005819#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005820static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005821wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005822{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005823 PyObject *result;
5824 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00005825
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005826 if (pid == -1)
5827 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00005828
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005829 if (struct_rusage == NULL) {
5830 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5831 if (m == NULL)
5832 return NULL;
5833 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5834 Py_DECREF(m);
5835 if (struct_rusage == NULL)
5836 return NULL;
5837 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005838
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005839 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5840 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5841 if (!result)
5842 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005843
5844#ifndef doubletime
5845#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5846#endif
5847
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005848 PyStructSequence_SET_ITEM(result, 0,
5849 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5850 PyStructSequence_SET_ITEM(result, 1,
5851 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00005852#define SET_INT(result, index, value)\
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005853 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5854 SET_INT(result, 2, ru->ru_maxrss);
5855 SET_INT(result, 3, ru->ru_ixrss);
5856 SET_INT(result, 4, ru->ru_idrss);
5857 SET_INT(result, 5, ru->ru_isrss);
5858 SET_INT(result, 6, ru->ru_minflt);
5859 SET_INT(result, 7, ru->ru_majflt);
5860 SET_INT(result, 8, ru->ru_nswap);
5861 SET_INT(result, 9, ru->ru_inblock);
5862 SET_INT(result, 10, ru->ru_oublock);
5863 SET_INT(result, 11, ru->ru_msgsnd);
5864 SET_INT(result, 12, ru->ru_msgrcv);
5865 SET_INT(result, 13, ru->ru_nsignals);
5866 SET_INT(result, 14, ru->ru_nvcsw);
5867 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00005868#undef SET_INT
5869
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005870 if (PyErr_Occurred()) {
5871 Py_DECREF(result);
5872 return NULL;
5873 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005874
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005875 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005876}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005877#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005878
5879#ifdef HAVE_WAIT3
5880PyDoc_STRVAR(posix_wait3__doc__,
5881"wait3(options) -> (pid, status, rusage)\n\n\
5882Wait for completion of a child process.");
5883
5884static PyObject *
5885posix_wait3(PyObject *self, PyObject *args)
5886{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005887 pid_t pid;
5888 int options;
5889 struct rusage ru;
5890 WAIT_TYPE status;
5891 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005892
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005893 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5894 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005895
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005896 Py_BEGIN_ALLOW_THREADS
5897 pid = wait3(&status, options, &ru);
5898 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005899
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005900 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005901}
5902#endif /* HAVE_WAIT3 */
5903
5904#ifdef HAVE_WAIT4
5905PyDoc_STRVAR(posix_wait4__doc__,
5906"wait4(pid, options) -> (pid, status, rusage)\n\n\
5907Wait for completion of a given child process.");
5908
5909static PyObject *
5910posix_wait4(PyObject *self, PyObject *args)
5911{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005912 pid_t pid;
5913 int options;
5914 struct rusage ru;
5915 WAIT_TYPE status;
5916 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005917
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005918 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
5919 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005920
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005921 Py_BEGIN_ALLOW_THREADS
5922 pid = wait4(pid, &status, options, &ru);
5923 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005924
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005925 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005926}
5927#endif /* HAVE_WAIT4 */
5928
Guido van Rossumb6775db1994-08-01 11:34:53 +00005929#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005930PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005931"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005932Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005933
Barry Warsaw53699e91996-12-10 23:23:01 +00005934static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005935posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005936{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005937 pid_t pid;
5938 int options;
5939 WAIT_TYPE status;
5940 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005941
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005942 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
5943 return NULL;
5944 Py_BEGIN_ALLOW_THREADS
5945 pid = waitpid(pid, &status, options);
5946 Py_END_ALLOW_THREADS
5947 if (pid == -1)
5948 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005949
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005950 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005951}
5952
Tim Petersab034fa2002-02-01 11:27:43 +00005953#elif defined(HAVE_CWAIT)
5954
5955/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005956PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005957"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005958"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005959
5960static PyObject *
5961posix_waitpid(PyObject *self, PyObject *args)
5962{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005963 Py_intptr_t pid;
5964 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005965
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005966 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
5967 return NULL;
5968 Py_BEGIN_ALLOW_THREADS
5969 pid = _cwait(&status, pid, options);
5970 Py_END_ALLOW_THREADS
5971 if (pid == -1)
5972 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005973
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005974 /* shift the status left a byte so this is more like the POSIX waitpid */
5975 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005976}
5977#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005978
Guido van Rossumad0ee831995-03-01 10:34:45 +00005979#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005980PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005981"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005982Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005983
Barry Warsaw53699e91996-12-10 23:23:01 +00005984static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005985posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005986{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005987 pid_t pid;
5988 WAIT_TYPE status;
5989 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005990
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005991 Py_BEGIN_ALLOW_THREADS
5992 pid = wait(&status);
5993 Py_END_ALLOW_THREADS
5994 if (pid == -1)
5995 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005996
Victor Stinner43cdd0a2010-05-06 00:20:44 +00005997 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005998}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005999#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006002PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006003"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006004Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006005
Barry Warsaw53699e91996-12-10 23:23:01 +00006006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006007posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006008{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006009#ifdef HAVE_LSTAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006010 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006011#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006012#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006013 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006014#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006015 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006016#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006017#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006018}
6019
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006020
Guido van Rossumb6775db1994-08-01 11:34:53 +00006021#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006022PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006023"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006024Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006025
Barry Warsaw53699e91996-12-10 23:23:01 +00006026static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006027posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006028{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006029 PyObject* v;
6030 char buf[MAXPATHLEN];
6031 char *path;
6032 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006033#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006034 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006035#endif
6036
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006037 if (!PyArg_ParseTuple(args, "et:readlink",
6038 Py_FileSystemDefaultEncoding, &path))
6039 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006040#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006041 v = PySequence_GetItem(args, 0);
6042 if (v == NULL) {
6043 PyMem_Free(path);
6044 return NULL;
6045 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006046
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006047 if (PyUnicode_Check(v)) {
6048 arg_is_unicode = 1;
6049 }
6050 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006051#endif
6052
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006053 Py_BEGIN_ALLOW_THREADS
6054 n = readlink(path, buf, (int) sizeof buf);
6055 Py_END_ALLOW_THREADS
6056 if (n < 0)
6057 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006058
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006059 PyMem_Free(path);
6060 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006061#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006062 if (arg_is_unicode) {
6063 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006064
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006065 w = PyUnicode_FromEncodedObject(v,
6066 Py_FileSystemDefaultEncoding,
6067 "strict");
6068 if (w != NULL) {
6069 Py_DECREF(v);
6070 v = w;
6071 }
6072 else {
6073 /* fall back to the original byte string, as
6074 discussed in patch #683592 */
6075 PyErr_Clear();
6076 }
6077 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006078#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006079 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006080}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006081#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006082
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006083
Guido van Rossumb6775db1994-08-01 11:34:53 +00006084#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006085PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006086"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006087Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006088
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006090posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006091{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006092 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006093}
6094#endif /* HAVE_SYMLINK */
6095
6096
6097#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006098#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6099static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006100system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006101{
6102 ULONG value = 0;
6103
6104 Py_BEGIN_ALLOW_THREADS
6105 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6106 Py_END_ALLOW_THREADS
6107
6108 return value;
6109}
6110
6111static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006112posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006113{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006114 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006115 return Py_BuildValue("ddddd",
6116 (double)0 /* t.tms_utime / HZ */,
6117 (double)0 /* t.tms_stime / HZ */,
6118 (double)0 /* t.tms_cutime / HZ */,
6119 (double)0 /* t.tms_cstime / HZ */,
6120 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006121}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006122#else /* not OS2 */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00006123#define NEED_TICKS_PER_SECOND
6124static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006125static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006126posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006127{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006128 struct tms t;
6129 clock_t c;
6130 errno = 0;
6131 c = times(&t);
6132 if (c == (clock_t) -1)
6133 return posix_error();
6134 return Py_BuildValue("ddddd",
6135 (double)t.tms_utime / ticks_per_second,
6136 (double)t.tms_stime / ticks_per_second,
6137 (double)t.tms_cutime / ticks_per_second,
6138 (double)t.tms_cstime / ticks_per_second,
6139 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006140}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006141#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006142#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006143
6144
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006145#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006146#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006147static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006148posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006149{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006150 FILETIME create, exit, kernel, user;
6151 HANDLE hProc;
6152 hProc = GetCurrentProcess();
6153 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6154 /* The fields of a FILETIME structure are the hi and lo part
6155 of a 64-bit value expressed in 100 nanosecond units.
6156 1e7 is one second in such units; 1e-7 the inverse.
6157 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6158 */
6159 return Py_BuildValue(
6160 "ddddd",
6161 (double)(user.dwHighDateTime*429.4967296 +
6162 user.dwLowDateTime*1e-7),
6163 (double)(kernel.dwHighDateTime*429.4967296 +
6164 kernel.dwLowDateTime*1e-7),
6165 (double)0,
6166 (double)0,
6167 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006168}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006169#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006170
6171#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006172PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006173"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006174Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006175#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006176
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006177
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006178#ifdef HAVE_GETSID
6179PyDoc_STRVAR(posix_getsid__doc__,
6180"getsid(pid) -> sid\n\n\
6181Call the system call getsid().");
6182
6183static PyObject *
6184posix_getsid(PyObject *self, PyObject *args)
6185{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006186 pid_t pid;
6187 int sid;
6188 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6189 return NULL;
6190 sid = getsid(pid);
6191 if (sid < 0)
6192 return posix_error();
6193 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006194}
6195#endif /* HAVE_GETSID */
6196
6197
Guido van Rossumb6775db1994-08-01 11:34:53 +00006198#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006199PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006200"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006201Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006202
Barry Warsaw53699e91996-12-10 23:23:01 +00006203static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006204posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006205{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006206 if (setsid() < 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_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006212
Guido van Rossumb6775db1994-08-01 11:34:53 +00006213#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006214PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006215"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006217
Barry Warsaw53699e91996-12-10 23:23:01 +00006218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006219posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006220{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006221 pid_t pid;
6222 int pgrp;
6223 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6224 return NULL;
6225 if (setpgid(pid, pgrp) < 0)
6226 return posix_error();
6227 Py_INCREF(Py_None);
6228 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006229}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006230#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006232
Guido van Rossumb6775db1994-08-01 11:34:53 +00006233#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006235"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Barry Warsaw53699e91996-12-10 23:23:01 +00006238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006239posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006240{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006241 int fd;
6242 pid_t pgid;
6243 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6244 return NULL;
6245 pgid = tcgetpgrp(fd);
6246 if (pgid < 0)
6247 return posix_error();
6248 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006249}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006250#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006251
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006252
Guido van Rossumb6775db1994-08-01 11:34:53 +00006253#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006254PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006255"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006256Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006257
Barry Warsaw53699e91996-12-10 23:23:01 +00006258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006259posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006260{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006261 int fd;
6262 pid_t pgid;
6263 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6264 return NULL;
6265 if (tcsetpgrp(fd, pgid) < 0)
6266 return posix_error();
6267 Py_INCREF(Py_None);
6268 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006269}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006270#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006271
Guido van Rossum687dd131993-05-17 08:34:16 +00006272/* Functions acting on file descriptors */
6273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006275"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006276Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006277
Barry Warsaw53699e91996-12-10 23:23:01 +00006278static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006279posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006280{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006281 char *file = NULL;
6282 int flag;
6283 int mode = 0777;
6284 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006285
6286#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006287 if (unicode_file_names()) {
6288 PyUnicodeObject *po;
6289 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6290 Py_BEGIN_ALLOW_THREADS
6291 /* PyUnicode_AS_UNICODE OK without thread
6292 lock as it is a simple dereference. */
6293 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6294 Py_END_ALLOW_THREADS
6295 if (fd < 0)
6296 return posix_error();
6297 return PyInt_FromLong((long)fd);
6298 }
6299 /* Drop the argument parsing error as narrow strings
6300 are also valid. */
6301 PyErr_Clear();
6302 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006303#endif
6304
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006305 if (!PyArg_ParseTuple(args, "eti|i",
6306 Py_FileSystemDefaultEncoding, &file,
6307 &flag, &mode))
6308 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006309
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006310 Py_BEGIN_ALLOW_THREADS
6311 fd = open(file, flag, mode);
6312 Py_END_ALLOW_THREADS
6313 if (fd < 0)
6314 return posix_error_with_allocated_filename(file);
6315 PyMem_Free(file);
6316 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006317}
6318
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006320PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006321"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006322Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006323
Barry Warsaw53699e91996-12-10 23:23:01 +00006324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006325posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006326{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006327 int fd, res;
6328 if (!PyArg_ParseTuple(args, "i:close", &fd))
6329 return NULL;
6330 Py_BEGIN_ALLOW_THREADS
6331 res = close(fd);
6332 Py_END_ALLOW_THREADS
6333 if (res < 0)
6334 return posix_error();
6335 Py_INCREF(Py_None);
6336 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006337}
6338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006339
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006340PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006341"closerange(fd_low, fd_high)\n\n\
6342Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6343
6344static PyObject *
6345posix_closerange(PyObject *self, PyObject *args)
6346{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006347 int fd_from, fd_to, i;
6348 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6349 return NULL;
6350 Py_BEGIN_ALLOW_THREADS
6351 for (i = fd_from; i < fd_to; i++)
6352 close(i);
6353 Py_END_ALLOW_THREADS
6354 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006355}
6356
6357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006359"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006360Return a duplicate of a 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_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006364{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006365 int fd;
6366 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6367 return NULL;
6368 Py_BEGIN_ALLOW_THREADS
6369 fd = dup(fd);
6370 Py_END_ALLOW_THREADS
6371 if (fd < 0)
6372 return posix_error();
6373 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006374}
6375
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006377PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006378"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006379Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006380
Barry Warsaw53699e91996-12-10 23:23:01 +00006381static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006382posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006383{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006384 int fd, fd2, res;
6385 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6386 return NULL;
6387 Py_BEGIN_ALLOW_THREADS
6388 res = dup2(fd, fd2);
6389 Py_END_ALLOW_THREADS
6390 if (res < 0)
6391 return posix_error();
6392 Py_INCREF(Py_None);
6393 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006394}
6395
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006397PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006398"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006399Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006400
Barry Warsaw53699e91996-12-10 23:23:01 +00006401static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006402posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006403{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006404 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006405#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006406 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006407#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006408 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006409#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006410 PyObject *posobj;
6411 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6412 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006413#ifdef SEEK_SET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006414 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6415 switch (how) {
6416 case 0: how = SEEK_SET; break;
6417 case 1: how = SEEK_CUR; break;
6418 case 2: how = SEEK_END; break;
6419 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006420#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006421
6422#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006423 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006424#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006425 pos = PyLong_Check(posobj) ?
6426 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006427#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006428 if (PyErr_Occurred())
6429 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006430
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006431 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006432#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006433 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006434#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006435 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006436#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006437 Py_END_ALLOW_THREADS
6438 if (res < 0)
6439 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006440
6441#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006442 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006443#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006444 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006445#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006446}
6447
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006449PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006450"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006451Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006452
Barry Warsaw53699e91996-12-10 23:23:01 +00006453static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006454posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006455{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006456 int fd, size, n;
6457 PyObject *buffer;
6458 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6459 return NULL;
6460 if (size < 0) {
6461 errno = EINVAL;
6462 return posix_error();
6463 }
6464 buffer = PyString_FromStringAndSize((char *)NULL, size);
6465 if (buffer == NULL)
6466 return NULL;
6467 Py_BEGIN_ALLOW_THREADS
6468 n = read(fd, PyString_AsString(buffer), size);
6469 Py_END_ALLOW_THREADS
6470 if (n < 0) {
6471 Py_DECREF(buffer);
6472 return posix_error();
6473 }
6474 if (n != size)
6475 _PyString_Resize(&buffer, n);
6476 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006477}
6478
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006480PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006481"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006482Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006483
Barry Warsaw53699e91996-12-10 23:23:01 +00006484static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006485posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006486{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006487 Py_buffer pbuf;
6488 int fd;
6489 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006490
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006491 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6492 return NULL;
6493 Py_BEGIN_ALLOW_THREADS
6494 size = write(fd, pbuf.buf, (size_t)pbuf.len);
6495 Py_END_ALLOW_THREADS
6496 PyBuffer_Release(&pbuf);
6497 if (size < 0)
6498 return posix_error();
6499 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006500}
6501
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006503PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006504"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006505Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006506
Barry Warsaw53699e91996-12-10 23:23:01 +00006507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006508posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006509{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006510 int fd;
6511 STRUCT_STAT st;
6512 int res;
6513 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6514 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006515#ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006516 /* on OpenVMS we must ensure that all bytes are written to the file */
6517 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006518#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006519 Py_BEGIN_ALLOW_THREADS
6520 res = FSTAT(fd, &st);
6521 Py_END_ALLOW_THREADS
6522 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006523#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006524 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006525#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006526 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006527#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006528 }
Tim Peters5aa91602002-01-30 05:46:57 +00006529
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006530 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006531}
6532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006534PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006535"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006536Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006537
Barry Warsaw53699e91996-12-10 23:23:01 +00006538static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006539posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006540{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006541 int fd;
6542 char *orgmode = "r";
6543 int bufsize = -1;
6544 FILE *fp;
6545 PyObject *f;
6546 char *mode;
6547 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6548 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006549
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006550 /* Sanitize mode. See fileobject.c */
6551 mode = PyMem_MALLOC(strlen(orgmode)+3);
6552 if (!mode) {
6553 PyErr_NoMemory();
6554 return NULL;
6555 }
6556 strcpy(mode, orgmode);
6557 if (_PyFile_SanitizeMode(mode)) {
6558 PyMem_FREE(mode);
6559 return NULL;
6560 }
6561 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006562#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006563 if (mode[0] == 'a') {
6564 /* try to make sure the O_APPEND flag is set */
6565 int flags;
6566 flags = fcntl(fd, F_GETFL);
6567 if (flags != -1)
6568 fcntl(fd, F_SETFL, flags | O_APPEND);
6569 fp = fdopen(fd, mode);
6570 if (fp == NULL && flags != -1)
6571 /* restore old mode if fdopen failed */
6572 fcntl(fd, F_SETFL, flags);
6573 } else {
6574 fp = fdopen(fd, mode);
6575 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006576#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006577 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006578#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006579 Py_END_ALLOW_THREADS
6580 PyMem_FREE(mode);
6581 if (fp == NULL)
6582 return posix_error();
6583 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6584 if (f != NULL)
6585 PyFile_SetBufSize(f, bufsize);
6586 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006587}
6588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006590"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006591Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006592connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006593
6594static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006595posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006596{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006597 int fd;
6598 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6599 return NULL;
6600 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006601}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006602
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006603#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006604PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006605"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006606Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006607
Barry Warsaw53699e91996-12-10 23:23:01 +00006608static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006609posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006610{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006611#if defined(PYOS_OS2)
6612 HFILE read, write;
6613 APIRET rc;
6614
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006615 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006616 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006617 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006618 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006619 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006620
6621 return Py_BuildValue("(ii)", read, write);
6622#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006623#if !defined(MS_WINDOWS)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006624 int fds[2];
6625 int res;
6626 Py_BEGIN_ALLOW_THREADS
6627 res = pipe(fds);
6628 Py_END_ALLOW_THREADS
6629 if (res != 0)
6630 return posix_error();
6631 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006632#else /* MS_WINDOWS */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006633 HANDLE read, write;
6634 int read_fd, write_fd;
6635 BOOL ok;
6636 Py_BEGIN_ALLOW_THREADS
6637 ok = CreatePipe(&read, &write, NULL, 0);
6638 Py_END_ALLOW_THREADS
6639 if (!ok)
6640 return win32_error("CreatePipe", NULL);
6641 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6642 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6643 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006644#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006645#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006646}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006647#endif /* HAVE_PIPE */
6648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006649
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006650#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006651PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006652"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006653Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006654
Barry Warsaw53699e91996-12-10 23:23:01 +00006655static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006656posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006657{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006658 char *filename;
6659 int mode = 0666;
6660 int res;
6661 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6662 return NULL;
6663 Py_BEGIN_ALLOW_THREADS
6664 res = mkfifo(filename, mode);
6665 Py_END_ALLOW_THREADS
6666 if (res < 0)
6667 return posix_error();
6668 Py_INCREF(Py_None);
6669 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006670}
6671#endif
6672
6673
Neal Norwitz11690112002-07-30 01:08:28 +00006674#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006675PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006676"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006677Create a filesystem node (file, device special file or named pipe)\n\
6678named filename. mode specifies both the permissions to use and the\n\
6679type of node to be created, being combined (bitwise OR) with one of\n\
6680S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006681device defines the newly created device special file (probably using\n\
6682os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006683
6684
6685static PyObject *
6686posix_mknod(PyObject *self, PyObject *args)
6687{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006688 char *filename;
6689 int mode = 0600;
6690 int device = 0;
6691 int res;
6692 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6693 return NULL;
6694 Py_BEGIN_ALLOW_THREADS
6695 res = mknod(filename, mode, device);
6696 Py_END_ALLOW_THREADS
6697 if (res < 0)
6698 return posix_error();
6699 Py_INCREF(Py_None);
6700 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006701}
6702#endif
6703
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006704#ifdef HAVE_DEVICE_MACROS
6705PyDoc_STRVAR(posix_major__doc__,
6706"major(device) -> major number\n\
6707Extracts a device major number from a raw device number.");
6708
6709static PyObject *
6710posix_major(PyObject *self, PyObject *args)
6711{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006712 int device;
6713 if (!PyArg_ParseTuple(args, "i:major", &device))
6714 return NULL;
6715 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006716}
6717
6718PyDoc_STRVAR(posix_minor__doc__,
6719"minor(device) -> minor number\n\
6720Extracts a device minor number from a raw device number.");
6721
6722static PyObject *
6723posix_minor(PyObject *self, PyObject *args)
6724{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006725 int device;
6726 if (!PyArg_ParseTuple(args, "i:minor", &device))
6727 return NULL;
6728 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006729}
6730
6731PyDoc_STRVAR(posix_makedev__doc__,
6732"makedev(major, minor) -> device number\n\
6733Composes a raw device number from the major and minor device numbers.");
6734
6735static PyObject *
6736posix_makedev(PyObject *self, PyObject *args)
6737{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006738 int major, minor;
6739 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6740 return NULL;
6741 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006742}
6743#endif /* device macros */
6744
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006745
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006746#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006747PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006748"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006749Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006750
Barry Warsaw53699e91996-12-10 23:23:01 +00006751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006752posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006753{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006754 int fd;
6755 off_t length;
6756 int res;
6757 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006758
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006759 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6760 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006761
6762#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006763 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006764#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006765 length = PyLong_Check(lenobj) ?
6766 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006767#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006768 if (PyErr_Occurred())
6769 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006770
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006771 Py_BEGIN_ALLOW_THREADS
6772 res = ftruncate(fd, length);
6773 Py_END_ALLOW_THREADS
6774 if (res < 0) {
6775 PyErr_SetFromErrno(PyExc_IOError);
6776 return NULL;
6777 }
6778 Py_INCREF(Py_None);
6779 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006780}
6781#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006782
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006783#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006784PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006785"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006786Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006787
Fred Drake762e2061999-08-26 17:23:54 +00006788/* Save putenv() parameters as values here, so we can collect them when they
6789 * get re-set with another call for the same key. */
6790static PyObject *posix_putenv_garbage;
6791
Tim Peters5aa91602002-01-30 05:46:57 +00006792static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006793posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006794{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006795 char *s1, *s2;
6796 char *newenv;
6797 PyObject *newstr;
6798 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006799
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006800 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6801 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006802
6803#if defined(PYOS_OS2)
6804 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6805 APIRET rc;
6806
Guido van Rossumd48f2521997-12-05 22:19:34 +00006807 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6808 if (rc != NO_ERROR)
6809 return os2_error(rc);
6810
6811 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6812 APIRET rc;
6813
Guido van Rossumd48f2521997-12-05 22:19:34 +00006814 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6815 if (rc != NO_ERROR)
6816 return os2_error(rc);
6817 } else {
6818#endif
6819
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006820 /* XXX This can leak memory -- not easy to fix :-( */
6821 len = strlen(s1) + strlen(s2) + 2;
6822 /* len includes space for a trailing \0; the size arg to
6823 PyString_FromStringAndSize does not count that */
6824 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6825 if (newstr == NULL)
6826 return PyErr_NoMemory();
6827 newenv = PyString_AS_STRING(newstr);
6828 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6829 if (putenv(newenv)) {
6830 Py_DECREF(newstr);
6831 posix_error();
6832 return NULL;
6833 }
6834 /* Install the first arg and newstr in posix_putenv_garbage;
6835 * this will cause previous value to be collected. This has to
6836 * happen after the real putenv() call because the old value
6837 * was still accessible until then. */
6838 if (PyDict_SetItem(posix_putenv_garbage,
6839 PyTuple_GET_ITEM(args, 0), newstr)) {
6840 /* really not much we can do; just leak */
6841 PyErr_Clear();
6842 }
6843 else {
6844 Py_DECREF(newstr);
6845 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006846
6847#if defined(PYOS_OS2)
6848 }
6849#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006850 Py_INCREF(Py_None);
6851 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006852}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006853#endif /* putenv */
6854
Guido van Rossumc524d952001-10-19 01:31:59 +00006855#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006856PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006857"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006858Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006859
6860static PyObject *
6861posix_unsetenv(PyObject *self, PyObject *args)
6862{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006863 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006864
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006865 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6866 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00006867
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006868 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006869
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006870 /* Remove the key from posix_putenv_garbage;
6871 * this will cause it to be collected. This has to
6872 * happen after the real unsetenv() call because the
6873 * old value was still accessible until then.
6874 */
6875 if (PyDict_DelItem(posix_putenv_garbage,
6876 PyTuple_GET_ITEM(args, 0))) {
6877 /* really not much we can do; just leak */
6878 PyErr_Clear();
6879 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006880
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006881 Py_INCREF(Py_None);
6882 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00006883}
6884#endif /* unsetenv */
6885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006886PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006887"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006888Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006889
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006890static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006891posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006892{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006893 int code;
6894 char *message;
6895 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6896 return NULL;
6897 message = strerror(code);
6898 if (message == NULL) {
6899 PyErr_SetString(PyExc_ValueError,
6900 "strerror() argument out of range");
6901 return NULL;
6902 }
6903 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006904}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006905
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006906
Guido van Rossumc9641791998-08-04 15:26:23 +00006907#ifdef HAVE_SYS_WAIT_H
6908
Fred Drake106c1a02002-04-23 15:58:02 +00006909#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006910PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006911"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006912Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006913
6914static PyObject *
6915posix_WCOREDUMP(PyObject *self, PyObject *args)
6916{
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:WCOREDUMP", &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(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006924}
6925#endif /* WCOREDUMP */
6926
6927#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006928PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006929"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006930Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006931job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006932
6933static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006934posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006935{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006936 WAIT_TYPE status;
6937 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006938
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006939 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6940 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006941
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006942 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006943}
6944#endif /* WIFCONTINUED */
6945
Guido van Rossumc9641791998-08-04 15:26:23 +00006946#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006947PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006948"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006949Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006950
6951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006952posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006953{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006954 WAIT_TYPE status;
6955 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006956
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006957 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6958 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006959
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006960 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006961}
6962#endif /* WIFSTOPPED */
6963
6964#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006965PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006966"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006967Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006968
6969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006970posix_WIFSIGNALED(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:WIFSIGNALED", &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(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006979}
6980#endif /* WIFSIGNALED */
6981
6982#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006983PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006984"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006985Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006986system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006987
6988static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006989posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006990{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006991 WAIT_TYPE status;
6992 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006993
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006994 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6995 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006996
Victor Stinner43cdd0a2010-05-06 00:20:44 +00006997 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006998}
6999#endif /* WIFEXITED */
7000
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007001#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007002PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007003"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007004Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007005
7006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007007posix_WEXITSTATUS(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:WEXITSTATUS", &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", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007016}
7017#endif /* WEXITSTATUS */
7018
7019#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007020PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007021"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007022Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007023value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007024
7025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007026posix_WTERMSIG(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:WTERMSIG", &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", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007035}
7036#endif /* WTERMSIG */
7037
7038#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007039PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007040"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007041Return the signal that stopped the process that provided\n\
7042the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007043
7044static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007045posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007046{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007047 WAIT_TYPE status;
7048 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007049
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007050 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7051 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007052
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007053 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007054}
7055#endif /* WSTOPSIG */
7056
7057#endif /* HAVE_SYS_WAIT_H */
7058
7059
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007060#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007061#ifdef _SCO_DS
7062/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7063 needed definitions in sys/statvfs.h */
7064#define _SVID3
7065#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007066#include <sys/statvfs.h>
7067
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007068static PyObject*
7069_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007070 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7071 if (v == NULL)
7072 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007073
7074#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007075 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7076 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7077 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7078 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7079 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7080 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7081 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7082 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7083 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7084 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007085#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007086 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7087 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7088 PyStructSequence_SET_ITEM(v, 2,
7089 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7090 PyStructSequence_SET_ITEM(v, 3,
7091 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7092 PyStructSequence_SET_ITEM(v, 4,
7093 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7094 PyStructSequence_SET_ITEM(v, 5,
7095 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7096 PyStructSequence_SET_ITEM(v, 6,
7097 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7098 PyStructSequence_SET_ITEM(v, 7,
7099 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7100 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7101 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007102#endif
7103
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007104 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007105}
7106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007107PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007108"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007109Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007110
7111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007112posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007113{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007114 int fd, res;
7115 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007116
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007117 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7118 return NULL;
7119 Py_BEGIN_ALLOW_THREADS
7120 res = fstatvfs(fd, &st);
7121 Py_END_ALLOW_THREADS
7122 if (res != 0)
7123 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007124
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007125 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007126}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007127#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007128
7129
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007130#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007131#include <sys/statvfs.h>
7132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007133PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007134"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007135Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007136
7137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007138posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007139{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007140 char *path;
7141 int res;
7142 struct statvfs st;
7143 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7144 return NULL;
7145 Py_BEGIN_ALLOW_THREADS
7146 res = statvfs(path, &st);
7147 Py_END_ALLOW_THREADS
7148 if (res != 0)
7149 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007150
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007151 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007152}
7153#endif /* HAVE_STATVFS */
7154
7155
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007156#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007157PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007158"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007159Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007160The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007161or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007162
7163static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007164posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007165{
7166 PyObject *result = NULL;
7167 char *dir = NULL;
7168 char *pfx = NULL;
7169 char *name;
7170
7171 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007172 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007173
7174 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007175 "tempnam is a potential security risk to your program") < 0)
7176 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007177
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007178#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007179 name = _tempnam(dir, pfx);
7180#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007181 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007182#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007183 if (name == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007184 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007185 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007186 free(name);
7187 return result;
7188}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007189#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007190
7191
7192#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007193PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007194"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007195Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007196
7197static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007198posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007199{
7200 FILE *fp;
7201
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007202 fp = tmpfile();
7203 if (fp == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007204 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007205 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007206}
7207#endif
7208
7209
7210#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007211PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007212"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007213Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007214
7215static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007216posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007217{
7218 char buffer[L_tmpnam];
7219 char *name;
7220
Skip Montanaro95618b52001-08-18 18:52:10 +00007221 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007222 "tmpnam is a potential security risk to your program") < 0)
7223 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007224
Greg Wardb48bc172000-03-01 21:51:56 +00007225#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007226 name = tmpnam_r(buffer);
7227#else
7228 name = tmpnam(buffer);
7229#endif
7230 if (name == NULL) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007231 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007232#ifdef USE_TMPNAM_R
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007233 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007234#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007235 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007236#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007237 );
7238 PyErr_SetObject(PyExc_OSError, err);
7239 Py_XDECREF(err);
7240 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007241 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007242 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007243}
7244#endif
7245
7246
Fred Drakec9680921999-12-13 16:37:25 +00007247/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7248 * It maps strings representing configuration variable names to
7249 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007250 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007251 * rarely-used constants. There are three separate tables that use
7252 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007253 *
7254 * This code is always included, even if none of the interfaces that
7255 * need it are included. The #if hackery needed to avoid it would be
7256 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007257 */
7258struct constdef {
7259 char *name;
7260 long value;
7261};
7262
Fred Drake12c6e2d1999-12-14 21:25:03 +00007263static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007264conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007265 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007266{
7267 if (PyInt_Check(arg)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007268 *valuep = PyInt_AS_LONG(arg);
7269 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007270 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007271 if (PyString_Check(arg)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007272 /* look up the value in the table using a binary search */
7273 size_t lo = 0;
7274 size_t mid;
7275 size_t hi = tablesize;
7276 int cmp;
7277 char *confname = PyString_AS_STRING(arg);
7278 while (lo < hi) {
7279 mid = (lo + hi) / 2;
7280 cmp = strcmp(confname, table[mid].name);
7281 if (cmp < 0)
7282 hi = mid;
7283 else if (cmp > 0)
7284 lo = mid + 1;
7285 else {
7286 *valuep = table[mid].value;
7287 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007288 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007289 }
7290 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007291 }
7292 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007293 PyErr_SetString(PyExc_TypeError,
7294 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007295 return 0;
7296}
7297
7298
7299#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7300static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007301#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007302 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007303#endif
7304#ifdef _PC_ABI_ASYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007305 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007306#endif
Fred Drakec9680921999-12-13 16:37:25 +00007307#ifdef _PC_ASYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007308 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007309#endif
7310#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007311 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007312#endif
7313#ifdef _PC_FILESIZEBITS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007314 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007315#endif
7316#ifdef _PC_LAST
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007317 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007318#endif
7319#ifdef _PC_LINK_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007320 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007321#endif
7322#ifdef _PC_MAX_CANON
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007323 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007324#endif
7325#ifdef _PC_MAX_INPUT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007326 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007327#endif
7328#ifdef _PC_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007329 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007330#endif
7331#ifdef _PC_NO_TRUNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007332 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007333#endif
7334#ifdef _PC_PATH_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007335 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007336#endif
7337#ifdef _PC_PIPE_BUF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007338 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007339#endif
7340#ifdef _PC_PRIO_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007341 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007342#endif
7343#ifdef _PC_SOCK_MAXBUF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007344 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007345#endif
7346#ifdef _PC_SYNC_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007347 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007348#endif
7349#ifdef _PC_VDISABLE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007350 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007351#endif
7352};
7353
Fred Drakec9680921999-12-13 16:37:25 +00007354static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007355conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007356{
7357 return conv_confname(arg, valuep, posix_constants_pathconf,
7358 sizeof(posix_constants_pathconf)
7359 / sizeof(struct constdef));
7360}
7361#endif
7362
7363#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007364PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007365"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007366Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007367If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007368
7369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007370posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007371{
7372 PyObject *result = NULL;
7373 int name, fd;
7374
Fred Drake12c6e2d1999-12-14 21:25:03 +00007375 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7376 conv_path_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007377 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007378
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007379 errno = 0;
7380 limit = fpathconf(fd, name);
7381 if (limit == -1 && errno != 0)
7382 posix_error();
7383 else
7384 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007385 }
7386 return result;
7387}
7388#endif
7389
7390
7391#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007392PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007393"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007394Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007395If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007396
7397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007398posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007399{
7400 PyObject *result = NULL;
7401 int name;
7402 char *path;
7403
7404 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7405 conv_path_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007406 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007407
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007408 errno = 0;
7409 limit = pathconf(path, name);
7410 if (limit == -1 && errno != 0) {
7411 if (errno == EINVAL)
7412 /* could be a path or name problem */
7413 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007414 else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007415 posix_error_with_filename(path);
7416 }
7417 else
7418 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007419 }
7420 return result;
7421}
7422#endif
7423
7424#ifdef HAVE_CONFSTR
7425static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007426#ifdef _CS_ARCHITECTURE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007427 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007428#endif
7429#ifdef _CS_HOSTNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007430 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007431#endif
7432#ifdef _CS_HW_PROVIDER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007433 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007434#endif
7435#ifdef _CS_HW_SERIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007436 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007437#endif
7438#ifdef _CS_INITTAB_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007439 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007440#endif
Fred Drakec9680921999-12-13 16:37:25 +00007441#ifdef _CS_LFS64_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007442 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007443#endif
7444#ifdef _CS_LFS64_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007445 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007446#endif
7447#ifdef _CS_LFS64_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007448 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007449#endif
7450#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007451 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007452#endif
7453#ifdef _CS_LFS_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007454 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007455#endif
7456#ifdef _CS_LFS_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007457 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007458#endif
7459#ifdef _CS_LFS_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007460 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007461#endif
7462#ifdef _CS_LFS_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007463 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007464#endif
Fred Draked86ed291999-12-15 15:34:33 +00007465#ifdef _CS_MACHINE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007466 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007467#endif
Fred Drakec9680921999-12-13 16:37:25 +00007468#ifdef _CS_PATH
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007469 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007470#endif
Fred Draked86ed291999-12-15 15:34:33 +00007471#ifdef _CS_RELEASE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007472 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007473#endif
7474#ifdef _CS_SRPC_DOMAIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007475 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007476#endif
7477#ifdef _CS_SYSNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007478 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007479#endif
7480#ifdef _CS_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007481 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007482#endif
Fred Drakec9680921999-12-13 16:37:25 +00007483#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007484 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007485#endif
7486#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007487 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007488#endif
7489#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007490 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007491#endif
7492#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007493 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007494#endif
7495#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007496 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007497#endif
7498#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007499 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007500#endif
7501#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007502 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007503#endif
7504#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007505 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007506#endif
7507#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007508 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007509#endif
7510#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007511 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007512#endif
7513#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007514 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007515#endif
7516#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007517 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007518#endif
7519#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007520 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007521#endif
7522#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007523 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007524#endif
7525#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007526 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007527#endif
7528#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007529 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007530#endif
Fred Draked86ed291999-12-15 15:34:33 +00007531#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007532 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007533#endif
7534#ifdef _MIPS_CS_BASE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007535 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007536#endif
7537#ifdef _MIPS_CS_HOSTID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007538 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007539#endif
7540#ifdef _MIPS_CS_HW_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007541 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007542#endif
7543#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007544 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007545#endif
7546#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007547 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007548#endif
7549#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007550 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007551#endif
7552#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007553 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007554#endif
7555#ifdef _MIPS_CS_OS_NAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007556 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007557#endif
7558#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007559 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007560#endif
7561#ifdef _MIPS_CS_PROCESSORS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007562 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007563#endif
7564#ifdef _MIPS_CS_SERIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007565 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007566#endif
7567#ifdef _MIPS_CS_VENDOR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007568 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007569#endif
Fred Drakec9680921999-12-13 16:37:25 +00007570};
7571
7572static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007573conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007574{
7575 return conv_confname(arg, valuep, posix_constants_confstr,
7576 sizeof(posix_constants_confstr)
7577 / sizeof(struct constdef));
7578}
7579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007580PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007581"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007582Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007583
7584static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007585posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007586{
7587 PyObject *result = NULL;
7588 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007589 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007590
7591 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007592 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007593
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007594 errno = 0;
7595 len = confstr(name, buffer, sizeof(buffer));
7596 if (len == 0) {
7597 if (errno) {
7598 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007599 }
7600 else {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007601 result = Py_None;
7602 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007603 }
7604 }
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007605 else {
7606 if ((unsigned int)len >= sizeof(buffer)) {
7607 result = PyString_FromStringAndSize(NULL, len-1);
7608 if (result != NULL)
7609 confstr(name, PyString_AS_STRING(result), len);
7610 }
7611 else
7612 result = PyString_FromStringAndSize(buffer, len-1);
7613 }
7614 }
Fred Drakec9680921999-12-13 16:37:25 +00007615 return result;
7616}
7617#endif
7618
7619
7620#ifdef HAVE_SYSCONF
7621static struct constdef posix_constants_sysconf[] = {
7622#ifdef _SC_2_CHAR_TERM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007623 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007624#endif
7625#ifdef _SC_2_C_BIND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007626 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007627#endif
7628#ifdef _SC_2_C_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007629 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007630#endif
7631#ifdef _SC_2_C_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007632 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007633#endif
7634#ifdef _SC_2_FORT_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007635 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007636#endif
7637#ifdef _SC_2_FORT_RUN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007638 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007639#endif
7640#ifdef _SC_2_LOCALEDEF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007641 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007642#endif
7643#ifdef _SC_2_SW_DEV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007644 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007645#endif
7646#ifdef _SC_2_UPE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007647 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007648#endif
7649#ifdef _SC_2_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007650 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007651#endif
Fred Draked86ed291999-12-15 15:34:33 +00007652#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007653 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007654#endif
7655#ifdef _SC_ACL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007656 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007657#endif
Fred Drakec9680921999-12-13 16:37:25 +00007658#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007659 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007660#endif
Fred Drakec9680921999-12-13 16:37:25 +00007661#ifdef _SC_AIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007662 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007663#endif
7664#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007665 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007666#endif
7667#ifdef _SC_ARG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007668 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007669#endif
7670#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007671 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007672#endif
7673#ifdef _SC_ATEXIT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007674 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007675#endif
Fred Draked86ed291999-12-15 15:34:33 +00007676#ifdef _SC_AUDIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007677 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007678#endif
Fred Drakec9680921999-12-13 16:37:25 +00007679#ifdef _SC_AVPHYS_PAGES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007680 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007681#endif
7682#ifdef _SC_BC_BASE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007683 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007684#endif
7685#ifdef _SC_BC_DIM_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007686 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007687#endif
7688#ifdef _SC_BC_SCALE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007689 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007690#endif
7691#ifdef _SC_BC_STRING_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007692 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007693#endif
Fred Draked86ed291999-12-15 15:34:33 +00007694#ifdef _SC_CAP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007695 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007696#endif
Fred Drakec9680921999-12-13 16:37:25 +00007697#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007698 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007699#endif
7700#ifdef _SC_CHAR_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007701 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007702#endif
7703#ifdef _SC_CHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007704 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007705#endif
7706#ifdef _SC_CHAR_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007707 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007708#endif
7709#ifdef _SC_CHILD_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007710 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007711#endif
7712#ifdef _SC_CLK_TCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007713 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007714#endif
7715#ifdef _SC_COHER_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007716 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007717#endif
7718#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007719 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007720#endif
7721#ifdef _SC_DCACHE_ASSOC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007722 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007723#endif
7724#ifdef _SC_DCACHE_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007725 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007726#endif
7727#ifdef _SC_DCACHE_LINESZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007728 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007729#endif
7730#ifdef _SC_DCACHE_SZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007731 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007732#endif
7733#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007734 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007735#endif
7736#ifdef _SC_DELAYTIMER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007737 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007738#endif
7739#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007740 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007741#endif
7742#ifdef _SC_EXPR_NEST_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007743 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007744#endif
7745#ifdef _SC_FSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007746 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007747#endif
7748#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007749 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007750#endif
7751#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007752 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007753#endif
7754#ifdef _SC_ICACHE_ASSOC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007755 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007756#endif
7757#ifdef _SC_ICACHE_BLKSZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007758 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007759#endif
7760#ifdef _SC_ICACHE_LINESZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007761 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007762#endif
7763#ifdef _SC_ICACHE_SZ
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007764 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007765#endif
Fred Draked86ed291999-12-15 15:34:33 +00007766#ifdef _SC_INF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007767 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007768#endif
Fred Drakec9680921999-12-13 16:37:25 +00007769#ifdef _SC_INT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007770 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007771#endif
7772#ifdef _SC_INT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007773 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007774#endif
7775#ifdef _SC_IOV_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007776 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007777#endif
Fred Draked86ed291999-12-15 15:34:33 +00007778#ifdef _SC_IP_SECOPTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007779 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007780#endif
Fred Drakec9680921999-12-13 16:37:25 +00007781#ifdef _SC_JOB_CONTROL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007782 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007783#endif
Fred Draked86ed291999-12-15 15:34:33 +00007784#ifdef _SC_KERN_POINTERS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007785 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007786#endif
7787#ifdef _SC_KERN_SIM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007788 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007789#endif
Fred Drakec9680921999-12-13 16:37:25 +00007790#ifdef _SC_LINE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007791 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007792#endif
7793#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007794 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007795#endif
7796#ifdef _SC_LOGNAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007797 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007798#endif
7799#ifdef _SC_LONG_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007800 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007801#endif
Fred Draked86ed291999-12-15 15:34:33 +00007802#ifdef _SC_MAC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007803 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007804#endif
Fred Drakec9680921999-12-13 16:37:25 +00007805#ifdef _SC_MAPPED_FILES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007806 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007807#endif
7808#ifdef _SC_MAXPID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007809 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007810#endif
7811#ifdef _SC_MB_LEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007812 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007813#endif
7814#ifdef _SC_MEMLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007815 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007816#endif
7817#ifdef _SC_MEMLOCK_RANGE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007818 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007819#endif
7820#ifdef _SC_MEMORY_PROTECTION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007821 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007822#endif
7823#ifdef _SC_MESSAGE_PASSING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007824 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007825#endif
Fred Draked86ed291999-12-15 15:34:33 +00007826#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007827 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007828#endif
Fred Drakec9680921999-12-13 16:37:25 +00007829#ifdef _SC_MQ_OPEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007830 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007831#endif
7832#ifdef _SC_MQ_PRIO_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007833 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007834#endif
Fred Draked86ed291999-12-15 15:34:33 +00007835#ifdef _SC_NACLS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007836 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007837#endif
Fred Drakec9680921999-12-13 16:37:25 +00007838#ifdef _SC_NGROUPS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007839 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007840#endif
7841#ifdef _SC_NL_ARGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007842 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007843#endif
7844#ifdef _SC_NL_LANGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007845 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007846#endif
7847#ifdef _SC_NL_MSGMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007848 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007849#endif
7850#ifdef _SC_NL_NMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007851 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007852#endif
7853#ifdef _SC_NL_SETMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007854 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007855#endif
7856#ifdef _SC_NL_TEXTMAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007857 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007858#endif
7859#ifdef _SC_NPROCESSORS_CONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007860 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007861#endif
7862#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007863 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007864#endif
Fred Draked86ed291999-12-15 15:34:33 +00007865#ifdef _SC_NPROC_CONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007866 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007867#endif
7868#ifdef _SC_NPROC_ONLN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007869 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007870#endif
Fred Drakec9680921999-12-13 16:37:25 +00007871#ifdef _SC_NZERO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007872 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007873#endif
7874#ifdef _SC_OPEN_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007875 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007876#endif
7877#ifdef _SC_PAGESIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007878 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007879#endif
7880#ifdef _SC_PAGE_SIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007881 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007882#endif
7883#ifdef _SC_PASS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007884 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007885#endif
7886#ifdef _SC_PHYS_PAGES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007887 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007888#endif
7889#ifdef _SC_PII
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007890 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007891#endif
7892#ifdef _SC_PII_INTERNET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007893 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007894#endif
7895#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007896 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007897#endif
7898#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007899 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007900#endif
7901#ifdef _SC_PII_OSI
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007902 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007903#endif
7904#ifdef _SC_PII_OSI_CLTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007905 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007906#endif
7907#ifdef _SC_PII_OSI_COTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007908 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007909#endif
7910#ifdef _SC_PII_OSI_M
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007911 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007912#endif
7913#ifdef _SC_PII_SOCKET
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007914 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007915#endif
7916#ifdef _SC_PII_XTI
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007917 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007918#endif
7919#ifdef _SC_POLL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007920 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007921#endif
7922#ifdef _SC_PRIORITIZED_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007923 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007924#endif
7925#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007926 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007927#endif
7928#ifdef _SC_REALTIME_SIGNALS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007929 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007930#endif
7931#ifdef _SC_RE_DUP_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007932 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007933#endif
7934#ifdef _SC_RTSIG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007935 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007936#endif
7937#ifdef _SC_SAVED_IDS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007938 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007939#endif
7940#ifdef _SC_SCHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007941 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007942#endif
7943#ifdef _SC_SCHAR_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007944 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007945#endif
7946#ifdef _SC_SELECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007947 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007948#endif
7949#ifdef _SC_SEMAPHORES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007950 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007951#endif
7952#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007953 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007954#endif
7955#ifdef _SC_SEM_VALUE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007956 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007957#endif
7958#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007959 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007960#endif
7961#ifdef _SC_SHRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007962 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007963#endif
7964#ifdef _SC_SHRT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007965 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007966#endif
7967#ifdef _SC_SIGQUEUE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007968 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007969#endif
7970#ifdef _SC_SIGRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007971 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007972#endif
7973#ifdef _SC_SIGRT_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007974 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007975#endif
Fred Draked86ed291999-12-15 15:34:33 +00007976#ifdef _SC_SOFTPOWER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007977 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007978#endif
Fred Drakec9680921999-12-13 16:37:25 +00007979#ifdef _SC_SPLIT_CACHE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007980 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007981#endif
7982#ifdef _SC_SSIZE_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007983 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007984#endif
7985#ifdef _SC_STACK_PROT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007986 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007987#endif
7988#ifdef _SC_STREAM_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007989 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007990#endif
7991#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007992 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007993#endif
7994#ifdef _SC_THREADS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007995 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007996#endif
7997#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00007998 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007999#endif
8000#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008001 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008002#endif
8003#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008004 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008005#endif
8006#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008007 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008008#endif
8009#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008010 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008011#endif
8012#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008013 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008014#endif
8015#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008016 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008017#endif
8018#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008019 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008020#endif
8021#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008022 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008023#endif
8024#ifdef _SC_THREAD_STACK_MIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008025 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008026#endif
8027#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008028 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008029#endif
8030#ifdef _SC_TIMERS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008031 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008032#endif
8033#ifdef _SC_TIMER_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008034 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008035#endif
8036#ifdef _SC_TTY_NAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008037 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008038#endif
8039#ifdef _SC_TZNAME_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008040 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008041#endif
8042#ifdef _SC_T_IOV_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008043 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008044#endif
8045#ifdef _SC_UCHAR_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008046 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008047#endif
8048#ifdef _SC_UINT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008049 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008050#endif
8051#ifdef _SC_UIO_MAXIOV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008052 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008053#endif
8054#ifdef _SC_ULONG_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008055 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008056#endif
8057#ifdef _SC_USHRT_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008058 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008059#endif
8060#ifdef _SC_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008061 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008062#endif
8063#ifdef _SC_WORD_BIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008064 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008065#endif
8066#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008067 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008068#endif
8069#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008070 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008071#endif
8072#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008073 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008074#endif
8075#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008076 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008077#endif
8078#ifdef _SC_XOPEN_CRYPT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008079 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008080#endif
8081#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008082 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008083#endif
8084#ifdef _SC_XOPEN_LEGACY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008085 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008086#endif
8087#ifdef _SC_XOPEN_REALTIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008088 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008089#endif
8090#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008091 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008092#endif
8093#ifdef _SC_XOPEN_SHM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008094 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008095#endif
8096#ifdef _SC_XOPEN_UNIX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008097 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008098#endif
8099#ifdef _SC_XOPEN_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008100 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008101#endif
8102#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008103 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008104#endif
8105#ifdef _SC_XOPEN_XPG2
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008106 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008107#endif
8108#ifdef _SC_XOPEN_XPG3
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008109 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008110#endif
8111#ifdef _SC_XOPEN_XPG4
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008112 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008113#endif
8114};
8115
8116static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008117conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008118{
8119 return conv_confname(arg, valuep, posix_constants_sysconf,
8120 sizeof(posix_constants_sysconf)
8121 / sizeof(struct constdef));
8122}
8123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008124PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008125"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008126Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008127
8128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008129posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008130{
8131 PyObject *result = NULL;
8132 int name;
8133
8134 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8135 int value;
8136
8137 errno = 0;
8138 value = sysconf(name);
8139 if (value == -1 && errno != 0)
8140 posix_error();
8141 else
8142 result = PyInt_FromLong(value);
8143 }
8144 return result;
8145}
8146#endif
8147
8148
Fred Drakebec628d1999-12-15 18:31:10 +00008149/* This code is used to ensure that the tables of configuration value names
8150 * are in sorted order as required by conv_confname(), and also to build the
8151 * the exported dictionaries that are used to publish information about the
8152 * names available on the host platform.
8153 *
8154 * Sorting the table at runtime ensures that the table is properly ordered
8155 * when used, even for platforms we're not able to test on. It also makes
8156 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008157 */
Fred Drakebec628d1999-12-15 18:31:10 +00008158
8159static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008160cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008161{
8162 const struct constdef *c1 =
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008163 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008164 const struct constdef *c2 =
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008165 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008166
8167 return strcmp(c1->name, c2->name);
8168}
8169
8170static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008171setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008172 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008173{
Fred Drakebec628d1999-12-15 18:31:10 +00008174 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008175 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008176
8177 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8178 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008179 if (d == NULL)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008180 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008181
Barry Warsaw3155db32000-04-13 15:20:40 +00008182 for (i=0; i < tablesize; ++i) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008183 PyObject *o = PyInt_FromLong(table[i].value);
8184 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8185 Py_XDECREF(o);
8186 Py_DECREF(d);
8187 return -1;
8188 }
8189 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008190 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008191 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008192}
8193
Fred Drakebec628d1999-12-15 18:31:10 +00008194/* Return -1 on failure, 0 on success. */
8195static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008196setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008197{
8198#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008199 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008200 sizeof(posix_constants_pathconf)
8201 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008202 "pathconf_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008203 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008204#endif
8205#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008206 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008207 sizeof(posix_constants_confstr)
8208 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008209 "confstr_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008210 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008211#endif
8212#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008213 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008214 sizeof(posix_constants_sysconf)
8215 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008216 "sysconf_names", module))
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008217 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008218#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008219 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008220}
Fred Draked86ed291999-12-15 15:34:33 +00008221
8222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008223PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008224"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008225Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008226in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008227
8228static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008229posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008230{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008231 abort();
8232 /*NOTREACHED*/
8233 Py_FatalError("abort() called from Python code didn't abort!");
8234 return NULL;
8235}
Fred Drakebec628d1999-12-15 18:31:10 +00008236
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008237#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008238PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008239"startfile(filepath [, operation]) - Start a file with its associated\n\
8240application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008241\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008242When \"operation\" is not specified or \"open\", this acts like\n\
8243double-clicking the file in Explorer, or giving the file name as an\n\
8244argument to the DOS \"start\" command: the file is opened with whatever\n\
8245application (if any) its extension is associated.\n\
8246When another \"operation\" is given, it specifies what should be done with\n\
8247the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008248\n\
8249startfile returns as soon as the associated application is launched.\n\
8250There is no option to wait for the application to close, and no way\n\
8251to retrieve the application's exit status.\n\
8252\n\
8253The filepath is relative to the current directory. If you want to use\n\
8254an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008255the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008256
8257static PyObject *
8258win32_startfile(PyObject *self, PyObject *args)
8259{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008260 char *filepath;
8261 char *operation = NULL;
8262 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00008263#ifdef Py_WIN_WIDE_FILENAMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008264 if (unicode_file_names()) {
8265 PyObject *unipath, *woperation = NULL;
8266 if (!PyArg_ParseTuple(args, "U|s:startfile",
8267 &unipath, &operation)) {
8268 PyErr_Clear();
8269 goto normal;
8270 }
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008271
Georg Brandlad89dc82006-04-03 12:26:26 +00008272
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008273 if (operation) {
8274 woperation = PyUnicode_DecodeASCII(operation,
8275 strlen(operation), NULL);
8276 if (!woperation) {
8277 PyErr_Clear();
8278 operation = NULL;
8279 goto normal;
8280 }
8281 }
8282
8283 Py_BEGIN_ALLOW_THREADS
8284 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8285 PyUnicode_AS_UNICODE(unipath),
8286 NULL, NULL, SW_SHOWNORMAL);
8287 Py_END_ALLOW_THREADS
8288
8289 Py_XDECREF(woperation);
8290 if (rc <= (HINSTANCE)32) {
8291 PyObject *errval = win32_error_unicode("startfile",
8292 PyUnicode_AS_UNICODE(unipath));
8293 return errval;
8294 }
8295 Py_INCREF(Py_None);
8296 return Py_None;
8297 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008298#endif
8299
8300normal:
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008301 if (!PyArg_ParseTuple(args, "et|s:startfile",
8302 Py_FileSystemDefaultEncoding, &filepath,
8303 &operation))
8304 return NULL;
8305 Py_BEGIN_ALLOW_THREADS
8306 rc = ShellExecute((HWND)0, operation, filepath,
8307 NULL, NULL, SW_SHOWNORMAL);
8308 Py_END_ALLOW_THREADS
8309 if (rc <= (HINSTANCE)32) {
8310 PyObject *errval = win32_error("startfile", filepath);
8311 PyMem_Free(filepath);
8312 return errval;
8313 }
8314 PyMem_Free(filepath);
8315 Py_INCREF(Py_None);
8316 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008317}
8318#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008319
Martin v. Löwis438b5342002-12-27 10:16:42 +00008320#ifdef HAVE_GETLOADAVG
8321PyDoc_STRVAR(posix_getloadavg__doc__,
8322"getloadavg() -> (float, float, float)\n\n\
8323Return the number of processes in the system run queue averaged over\n\
8324the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8325was unobtainable");
8326
8327static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008328posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008329{
8330 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008331 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008332 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8333 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008334 } else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008335 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008336}
8337#endif
8338
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008339#ifdef MS_WINDOWS
8340
8341PyDoc_STRVAR(win32_urandom__doc__,
8342"urandom(n) -> str\n\n\
8343Return a string of n random bytes suitable for cryptographic use.");
8344
8345typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8346 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8347 DWORD dwFlags );
8348typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8349 BYTE *pbBuffer );
8350
8351static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008352/* This handle is never explicitly released. Instead, the operating
8353 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008354static HCRYPTPROV hCryptProv = 0;
8355
Tim Peters4ad82172004-08-30 17:02:04 +00008356static PyObject*
8357win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008358{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008359 int howMany;
8360 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008361
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008362 /* Read arguments */
8363 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8364 return NULL;
8365 if (howMany < 0)
8366 return PyErr_Format(PyExc_ValueError,
8367 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008368
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008369 if (hCryptProv == 0) {
8370 HINSTANCE hAdvAPI32 = NULL;
8371 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008372
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008373 /* Obtain handle to the DLL containing CryptoAPI
8374 This should not fail */
8375 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8376 if(hAdvAPI32 == NULL)
8377 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008378
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008379 /* Obtain pointers to the CryptoAPI functions
8380 This will fail on some early versions of Win95 */
8381 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8382 hAdvAPI32,
8383 "CryptAcquireContextA");
8384 if (pCryptAcquireContext == NULL)
8385 return PyErr_Format(PyExc_NotImplementedError,
8386 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008387
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008388 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8389 hAdvAPI32, "CryptGenRandom");
8390 if (pCryptGenRandom == NULL)
8391 return PyErr_Format(PyExc_NotImplementedError,
8392 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008393
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008394 /* Acquire context */
8395 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8396 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8397 return win32_error("CryptAcquireContext", NULL);
8398 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008399
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008400 /* Allocate bytes */
8401 result = PyString_FromStringAndSize(NULL, howMany);
8402 if (result != NULL) {
8403 /* Get random data */
8404 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
8405 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8406 PyString_AS_STRING(result))) {
8407 Py_DECREF(result);
8408 return win32_error("CryptGenRandom", NULL);
8409 }
8410 }
8411 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008412}
8413#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008414
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008415#ifdef __VMS
8416/* Use openssl random routine */
8417#include <openssl/rand.h>
8418PyDoc_STRVAR(vms_urandom__doc__,
8419"urandom(n) -> str\n\n\
8420Return a string of n random bytes suitable for cryptographic use.");
8421
8422static PyObject*
8423vms_urandom(PyObject *self, PyObject *args)
8424{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008425 int howMany;
8426 PyObject* result;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008427
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008428 /* Read arguments */
8429 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8430 return NULL;
8431 if (howMany < 0)
8432 return PyErr_Format(PyExc_ValueError,
8433 "negative argument not allowed");
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008434
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008435 /* Allocate bytes */
8436 result = PyString_FromStringAndSize(NULL, howMany);
8437 if (result != NULL) {
8438 /* Get random data */
8439 if (RAND_pseudo_bytes((unsigned char*)
8440 PyString_AS_STRING(result),
8441 howMany) < 0) {
8442 Py_DECREF(result);
8443 return PyErr_Format(PyExc_ValueError,
8444 "RAND_pseudo_bytes");
8445 }
8446 }
8447 return result;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008448}
8449#endif
8450
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008451static PyMethodDef posix_methods[] = {
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008452 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008453#ifdef HAVE_TTYNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008454 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008455#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008456 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008457#ifdef HAVE_CHFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008458 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008459#endif /* HAVE_CHFLAGS */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008460 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008461#ifdef HAVE_FCHMOD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008462 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008463#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008464#ifdef HAVE_CHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008465 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008466#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008467#ifdef HAVE_LCHMOD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008468 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008469#endif /* HAVE_LCHMOD */
8470#ifdef HAVE_FCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008471 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008472#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008473#ifdef HAVE_LCHFLAGS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008474 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008475#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008476#ifdef HAVE_LCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008477 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008478#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008479#ifdef HAVE_CHROOT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008480 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008481#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008482#ifdef HAVE_CTERMID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008483 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008484#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008485#ifdef HAVE_GETCWD
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008486 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008487#ifdef Py_USING_UNICODE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008488 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008489#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008490#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008491#ifdef HAVE_LINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008492 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008493#endif /* HAVE_LINK */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008494 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8495 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8496 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008497#ifdef HAVE_NICE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008498 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008499#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008500#ifdef HAVE_READLINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008501 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008502#endif /* HAVE_READLINK */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008503 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8504 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8505 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8506 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008507#ifdef HAVE_SYMLINK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008508 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008509#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008510#ifdef HAVE_SYSTEM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008511 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008512#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008513 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008514#ifdef HAVE_UNAME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008515 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008516#endif /* HAVE_UNAME */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008517 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8518 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8519 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008520#ifdef HAVE_TIMES
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008521 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008522#endif /* HAVE_TIMES */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008523 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008524#ifdef HAVE_EXECV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008525 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8526 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008527#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008528#ifdef HAVE_SPAWNV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008529 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8530 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008531#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008532 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8533 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008534#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008535#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008536#ifdef HAVE_FORK1
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008537 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008538#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008539#ifdef HAVE_FORK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008540 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008541#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008542#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008543 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008544#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008545#ifdef HAVE_FORKPTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008546 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008547#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008548#ifdef HAVE_GETEGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008549 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008550#endif /* HAVE_GETEGID */
8551#ifdef HAVE_GETEUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008552 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008553#endif /* HAVE_GETEUID */
8554#ifdef HAVE_GETGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008555 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008556#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008557#ifdef HAVE_GETGROUPS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008558 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008559#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008560 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008561#ifdef HAVE_GETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008562 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008563#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008564#ifdef HAVE_GETPPID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008565 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008566#endif /* HAVE_GETPPID */
8567#ifdef HAVE_GETUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008568 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008569#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008570#ifdef HAVE_GETLOGIN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008571 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008572#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008573#ifdef HAVE_KILL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008574 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008575#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008576#ifdef HAVE_KILLPG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008577 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008578#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008579#ifdef HAVE_PLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008580 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008581#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008582#ifdef HAVE_POPEN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008583 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008584#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008585 {"popen2", win32_popen2, METH_VARARGS},
8586 {"popen3", win32_popen3, METH_VARARGS},
8587 {"popen4", win32_popen4, METH_VARARGS},
8588 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008589#else
8590#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008591 {"popen2", os2emx_popen2, METH_VARARGS},
8592 {"popen3", os2emx_popen3, METH_VARARGS},
8593 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008594#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008595#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008596#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008597#ifdef HAVE_SETUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008598 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008599#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008600#ifdef HAVE_SETEUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008601 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008602#endif /* HAVE_SETEUID */
8603#ifdef HAVE_SETEGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008604 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008605#endif /* HAVE_SETEGID */
8606#ifdef HAVE_SETREUID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008607 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008608#endif /* HAVE_SETREUID */
8609#ifdef HAVE_SETREGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008610 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008611#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008612#ifdef HAVE_SETGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008613 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008614#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008615#ifdef HAVE_SETGROUPS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008616 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008617#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008618#ifdef HAVE_GETPGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008619 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008620#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008621#ifdef HAVE_SETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008622 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008623#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008624#ifdef HAVE_WAIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008625 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008626#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008627#ifdef HAVE_WAIT3
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008628 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008629#endif /* HAVE_WAIT3 */
8630#ifdef HAVE_WAIT4
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008631 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008632#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008633#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008634 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008635#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008636#ifdef HAVE_GETSID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008637 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008638#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008639#ifdef HAVE_SETSID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008640 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008641#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008642#ifdef HAVE_SETPGID
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008643 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008644#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008645#ifdef HAVE_TCGETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008646 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008647#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008648#ifdef HAVE_TCSETPGRP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008649 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008650#endif /* HAVE_TCSETPGRP */
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008651 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8652 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8653 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8654 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8655 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8656 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8657 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8658 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8659 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8660 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8661 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008662#ifdef HAVE_PIPE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008663 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008664#endif
8665#ifdef HAVE_MKFIFO
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008666 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008667#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008668#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008669 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008670#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008671#ifdef HAVE_DEVICE_MACROS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008672 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8673 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8674 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008675#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008676#ifdef HAVE_FTRUNCATE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008677 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008678#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008679#ifdef HAVE_PUTENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008680 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008681#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008682#ifdef HAVE_UNSETENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008683 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008684#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008685 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008686#ifdef HAVE_FCHDIR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008687 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008688#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008689#ifdef HAVE_FSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008690 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008691#endif
8692#ifdef HAVE_FDATASYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008693 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008694#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008695#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008696#ifdef WCOREDUMP
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008697 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008698#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008699#ifdef WIFCONTINUED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008700 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008701#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008702#ifdef WIFSTOPPED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008703 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008704#endif /* WIFSTOPPED */
8705#ifdef WIFSIGNALED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008706 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008707#endif /* WIFSIGNALED */
8708#ifdef WIFEXITED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008709 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008710#endif /* WIFEXITED */
8711#ifdef WEXITSTATUS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008712 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008713#endif /* WEXITSTATUS */
8714#ifdef WTERMSIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008715 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008716#endif /* WTERMSIG */
8717#ifdef WSTOPSIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008718 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008719#endif /* WSTOPSIG */
8720#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008721#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008722 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008723#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008724#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008725 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008726#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008727#ifdef HAVE_TMPFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008728 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008729#endif
8730#ifdef HAVE_TEMPNAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008731 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008732#endif
8733#ifdef HAVE_TMPNAM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008734 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008735#endif
Fred Drakec9680921999-12-13 16:37:25 +00008736#ifdef HAVE_CONFSTR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008737 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
8739#ifdef HAVE_SYSCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008740 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
8742#ifdef HAVE_FPATHCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008743 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008744#endif
8745#ifdef HAVE_PATHCONF
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008746 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008747#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008748 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008749#ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008750 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008751#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008752#ifdef HAVE_GETLOADAVG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008753 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008754#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008755 #ifdef MS_WINDOWS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008756 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008757 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008758 #ifdef __VMS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008759 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008760 #endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008761 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008762};
8763
8764
Barry Warsaw4a342091996-12-19 23:50:02 +00008765static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008766ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008767{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008768 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008769}
8770
Guido van Rossumd48f2521997-12-05 22:19:34 +00008771#if defined(PYOS_OS2)
8772/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008773static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008774{
8775 APIRET rc;
8776 ULONG values[QSV_MAX+1];
8777 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008778 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008779
8780 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008781 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008782 Py_END_ALLOW_THREADS
8783
8784 if (rc != NO_ERROR) {
8785 os2_error(rc);
8786 return -1;
8787 }
8788
Fred Drake4d1e64b2002-04-15 19:40:07 +00008789 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8790 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8791 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8792 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8793 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8794 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8795 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008796
8797 switch (values[QSV_VERSION_MINOR]) {
8798 case 0: ver = "2.00"; break;
8799 case 10: ver = "2.10"; break;
8800 case 11: ver = "2.11"; break;
8801 case 30: ver = "3.00"; break;
8802 case 40: ver = "4.00"; break;
8803 case 50: ver = "5.00"; break;
8804 default:
Tim Peters885d4572001-11-28 20:27:42 +00008805 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008806 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008807 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008808 ver = &tmp[0];
8809 }
8810
8811 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008812 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008813 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008814
8815 /* Add Indicator of Which Drive was Used to Boot the System */
8816 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8817 tmp[1] = ':';
8818 tmp[2] = '\0';
8819
Fred Drake4d1e64b2002-04-15 19:40:07 +00008820 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008821}
8822#endif
8823
Barry Warsaw4a342091996-12-19 23:50:02 +00008824static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008825all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008826{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008827#ifdef F_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008828 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008829#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008830#ifdef R_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008831 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008832#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008833#ifdef W_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008834 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008835#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008836#ifdef X_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008837 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008838#endif
Fred Drakec9680921999-12-13 16:37:25 +00008839#ifdef NGROUPS_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008840 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008842#ifdef TMP_MAX
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008843 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008844#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008845#ifdef WCONTINUED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008846 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008847#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008848#ifdef WNOHANG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008849 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008850#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008851#ifdef WUNTRACED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008852 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008853#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008854#ifdef O_RDONLY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008855 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008856#endif
8857#ifdef O_WRONLY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008858 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008859#endif
8860#ifdef O_RDWR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008861 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008862#endif
8863#ifdef O_NDELAY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008864 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008865#endif
8866#ifdef O_NONBLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008867 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008868#endif
8869#ifdef O_APPEND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008870 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008871#endif
8872#ifdef O_DSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008873 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008874#endif
8875#ifdef O_RSYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008876 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008877#endif
8878#ifdef O_SYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008879 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008880#endif
8881#ifdef O_NOCTTY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008882 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008883#endif
8884#ifdef O_CREAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008885 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008886#endif
8887#ifdef O_EXCL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008888 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008889#endif
8890#ifdef O_TRUNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008891 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008892#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008893#ifdef O_BINARY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008894 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008895#endif
8896#ifdef O_TEXT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008897 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008898#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008899#ifdef O_LARGEFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008900 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008901#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008902#ifdef O_SHLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008903 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008904#endif
8905#ifdef O_EXLOCK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008906 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008907#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008908
Tim Peters5aa91602002-01-30 05:46:57 +00008909/* MS Windows */
8910#ifdef O_NOINHERIT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008911 /* Don't inherit in child processes. */
8912 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008913#endif
8914#ifdef _O_SHORT_LIVED
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008915 /* Optimize for short life (keep in memory). */
8916 /* MS forgot to define this one with a non-underscore form too. */
8917 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008918#endif
8919#ifdef O_TEMPORARY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008920 /* Automatically delete when last handle is closed. */
8921 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008922#endif
8923#ifdef O_RANDOM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008924 /* Optimize for random access. */
8925 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008926#endif
8927#ifdef O_SEQUENTIAL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008928 /* Optimize for sequential access. */
8929 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008930#endif
8931
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008932/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00008933#ifdef O_ASYNC
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008934 /* Send a SIGIO signal whenever input or output
8935 becomes available on file descriptor */
8936 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00008937#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008938#ifdef O_DIRECT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008939 /* Direct disk access. */
8940 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008941#endif
8942#ifdef O_DIRECTORY
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008943 /* Must be a directory. */
8944 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008945#endif
8946#ifdef O_NOFOLLOW
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008947 /* Do not follow links. */
8948 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008949#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008950#ifdef O_NOATIME
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008951 /* Do not update the access time. */
8952 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00008953#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008954
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008955 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008956#ifdef EX_OK
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008957 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008958#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008959#ifdef EX_USAGE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008960 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008961#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008962#ifdef EX_DATAERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008963 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008964#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008965#ifdef EX_NOINPUT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008966 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008967#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008968#ifdef EX_NOUSER
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008969 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008970#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008971#ifdef EX_NOHOST
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008972 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008973#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008974#ifdef EX_UNAVAILABLE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008975 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008976#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008977#ifdef EX_SOFTWARE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008978 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008979#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008980#ifdef EX_OSERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008981 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008982#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008983#ifdef EX_OSFILE
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008984 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008985#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008986#ifdef EX_CANTCREAT
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008987 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008988#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008989#ifdef EX_IOERR
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008990 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008991#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008992#ifdef EX_TEMPFAIL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008993 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008994#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008995#ifdef EX_PROTOCOL
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008996 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008997#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008998#ifdef EX_NOPERM
Victor Stinner43cdd0a2010-05-06 00:20:44 +00008999 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009000#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009001#ifdef EX_CONFIG
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009002 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009003#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009004#ifdef EX_NOTFOUND
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009005 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009006#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009007
Guido van Rossum246bc171999-02-01 23:54:31 +00009008#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009009#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009010 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9011 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9012 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9013 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9014 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9015 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9016 if (ins(d, "P_PM", (long)P_PM)) return -1;
9017 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9018 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9019 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9020 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9021 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9022 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9023 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9024 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9025 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9026 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9027 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9028 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9029 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009030#else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009031 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9032 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9033 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9034 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9035 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009036#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009037#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009038
Guido van Rossumd48f2521997-12-05 22:19:34 +00009039#if defined(PYOS_OS2)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009040 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009041#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009042 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009043}
9044
9045
Tim Peters5aa91602002-01-30 05:46:57 +00009046#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009047#define INITFUNC initnt
9048#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009049
9050#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009051#define INITFUNC initos2
9052#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009053
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009054#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009055#define INITFUNC initposix
9056#define MODNAME "posix"
9057#endif
9058
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009059PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009060INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009061{
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009062 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009063
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009064 m = Py_InitModule3(MODNAME,
9065 posix_methods,
9066 posix__doc__);
9067 if (m == NULL)
9068 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009069
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009070 /* Initialize environ dictionary */
9071 v = convertenviron();
9072 Py_XINCREF(v);
9073 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9074 return;
9075 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009076
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009077 if (all_ins(m))
9078 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009079
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009080 if (setup_confname_tables(m))
9081 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009082
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009083 Py_INCREF(PyExc_OSError);
9084 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009085
Guido van Rossumb3d39562000-01-31 18:41:26 +00009086#ifdef HAVE_PUTENV
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009087 if (posix_putenv_garbage == NULL)
9088 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009089#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009090
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009091 if (!initialized) {
9092 stat_result_desc.name = MODNAME ".stat_result";
9093 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9094 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9095 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9096 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9097 structseq_new = StatResultType.tp_new;
9098 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009099
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009100 statvfs_result_desc.name = MODNAME ".statvfs_result";
9101 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009102#ifdef NEED_TICKS_PER_SECOND
9103# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009104 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009105# elif defined(HZ)
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009106 ticks_per_second = HZ;
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009107# else
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009108 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis3f15ae32008-12-29 18:20:48 +00009109# endif
9110#endif
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009111 }
9112 Py_INCREF((PyObject*) &StatResultType);
9113 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9114 Py_INCREF((PyObject*) &StatVFSResultType);
9115 PyModule_AddObject(m, "statvfs_result",
9116 (PyObject*) &StatVFSResultType);
9117 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009118
9119#ifdef __APPLE__
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009120 /*
9121 * Step 2 of weak-linking support on Mac OS X.
9122 *
9123 * The code below removes functions that are not available on the
9124 * currently active platform.
9125 *
9126 * This block allow one to use a python binary that was build on
9127 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9128 * OSX 10.4.
9129 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009130#ifdef HAVE_FSTATVFS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009131 if (fstatvfs == NULL) {
9132 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9133 return;
9134 }
9135 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009136#endif /* HAVE_FSTATVFS */
9137
9138#ifdef HAVE_STATVFS
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009139 if (statvfs == NULL) {
9140 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9141 return;
9142 }
9143 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009144#endif /* HAVE_STATVFS */
9145
9146# ifdef HAVE_LCHOWN
Victor Stinner43cdd0a2010-05-06 00:20:44 +00009147 if (lchown == NULL) {
9148 if (PyObject_DelAttrString(m, "lchown") == -1) {
9149 return;
9150 }
9151 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009152#endif /* HAVE_LCHOWN */
9153
9154
9155#endif /* __APPLE__ */
9156
Guido van Rossumb6775db1994-08-01 11:34:53 +00009157}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009158
9159#ifdef __cplusplus
9160}
9161#endif
9162
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009163