blob: 4179c0ef0746500f04f11c94a84c7d635b442c62 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000031# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#endif /* defined(__VMS) */
33
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef __cplusplus
35extern "C" {
36#endif
37
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000038PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000039"This module provides access to operating system functionality that is\n\
40standardized by the C Standard and the POSIX standard (a thinly\n\
41disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000042corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000044
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000045#if defined(PYOS_OS2)
46#define INCL_DOS
47#define INCL_DOSERRORS
48#define INCL_DOSPROCESS
49#define INCL_NOPMAPI
50#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000051#if defined(PYCC_GCC)
52#include <ctype.h>
53#include <io.h>
54#include <stdio.h>
55#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000056#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000057#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000058#endif
59
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000061#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#endif /* HAVE_SYS_TYPES_H */
63
64#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000065#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000067
Guido van Rossum36bc6801995-06-14 22:54:23 +000068#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000069#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000071
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000073#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000075
Guido van Rossumb6775db1994-08-01 11:34:53 +000076#ifdef HAVE_FCNTL_H
77#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000078#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000079
Guido van Rossuma6535fd2001-10-18 19:44:10 +000080#ifdef HAVE_GRP_H
81#include <grp.h>
82#endif
83
Barry Warsaw5676bd12003-01-07 20:57:09 +000084#ifdef HAVE_SYSEXITS_H
85#include <sysexits.h>
86#endif /* HAVE_SYSEXITS_H */
87
Anthony Baxter8a560de2004-10-13 15:30:56 +000088#ifdef HAVE_SYS_LOADAVG_H
89#include <sys/loadavg.h>
90#endif
91
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000092#ifdef HAVE_LANGINFO_H
93#include <langinfo.h>
94#endif
95
Guido van Rossuma4916fa1996-05-23 22:58:55 +000096/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000097/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000098#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000099#include <process.h>
100#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000101#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000102#define HAVE_GETCWD 1
103#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000104#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#if defined(__OS2__)
106#define HAVE_EXECV 1
107#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000108#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#include <process.h>
110#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000111#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#define HAVE_EXECV 1
113#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_OPENDIR 1
115#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000116#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_WAIT 1
118#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000120#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000121#define HAVE_GETPPID 1
Victor Stinner26de69d2011-06-17 15:15:38 +0200122#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000123#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_EXECV 1
125#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000126#define HAVE_SYSTEM 1
127#define HAVE_CWAIT 1
128#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000130#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000131#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
132/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000133#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134/* Unix functions that the configure script doesn't check for */
135#define HAVE_EXECV 1
136#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000137#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#define HAVE_FORK1 1
139#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#define HAVE_GETCWD 1
141#define HAVE_GETEGID 1
142#define HAVE_GETEUID 1
143#define HAVE_GETGID 1
144#define HAVE_GETPPID 1
145#define HAVE_GETUID 1
146#define HAVE_KILL 1
147#define HAVE_OPENDIR 1
148#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000149#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000152#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#endif /* _MSC_VER */
154#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000155#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000157
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000159
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000160#if defined(__sgi)&&_COMPILER_VERSION>=700
161/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
162 (default) */
163extern char *ctermid_r(char *);
164#endif
165
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000166#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000167#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000168extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000170#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000171extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#endif
176#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int chdir(char *);
178extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000179#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000180extern int chdir(const char *);
181extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000182#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000183#ifdef __BORLANDC__
184extern int chmod(const char *, int);
185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000187#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000188/*#ifdef HAVE_FCHMOD
189extern int fchmod(int, mode_t);
190#endif*/
191/*#ifdef HAVE_LCHMOD
192extern int lchmod(const char *, mode_t);
193#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000194extern int chown(const char *, uid_t, gid_t);
195extern char *getcwd(char *, int);
196extern char *strerror(int);
197extern int link(const char *, const char *);
198extern int rename(const char *, const char *);
199extern int stat(const char *, struct stat *);
200extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000203#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000205extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000208
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#ifdef HAVE_UTIME_H
212#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000215#ifdef HAVE_SYS_UTIME_H
216#include <sys/utime.h>
217#define HAVE_UTIME_H /* pretend we do for the rest of this file */
218#endif /* HAVE_SYS_UTIME_H */
219
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#ifdef HAVE_SYS_TIMES_H
221#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000222#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223
224#ifdef HAVE_SYS_PARAM_H
225#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000226#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227
228#ifdef HAVE_SYS_UTSNAME_H
229#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000230#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#define NAMLEN(dirent) strlen((dirent)->d_name)
235#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000236#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#include <direct.h>
238#define NAMLEN(dirent) strlen((dirent)->d_name)
239#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000241#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000242#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#endif
246#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#endif
249#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000251#endif
252#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000254#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#endif
258#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260#endif
261#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000264#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000265#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#endif
267#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000268#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000269#endif
270#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000271#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000272#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000273#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000274#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000276#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000277#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000278#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
279#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000280static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000281#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000282#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283
Guido van Rossumd48f2521997-12-05 22:19:34 +0000284#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000286#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000289#if defined(PATH_MAX) && PATH_MAX > 1024
290#define MAXPATHLEN PATH_MAX
291#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000292#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000293#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000294#endif /* MAXPATHLEN */
295
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000296#ifdef UNION_WAIT
297/* Emulate some macros on systems that have a union instead of macros */
298
299#ifndef WIFEXITED
300#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
301#endif
302
303#ifndef WEXITSTATUS
304#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
305#endif
306
307#ifndef WTERMSIG
308#define WTERMSIG(u_wait) ((u_wait).w_termsig)
309#endif
310
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311#define WAIT_TYPE union wait
312#define WAIT_STATUS_INT(s) (s.w_status)
313
314#else /* !UNION_WAIT */
315#define WAIT_TYPE int
316#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000317#endif /* UNION_WAIT */
318
Greg Wardb48bc172000-03-01 21:51:56 +0000319/* Don't use the "_r" form if we don't need it (also, won't have a
320 prototype for it, at least on Solaris -- maybe others as well?). */
321#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
322#define USE_CTERMID_R
323#endif
324
Fred Drake699f3522000-06-29 21:12:41 +0000325/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000326#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000327#undef FSTAT
328#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000329#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000330# define STAT win32_stat
331# define FSTAT win32_fstat
332# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000333#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000334# define STAT stat
335# define FSTAT fstat
336# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000337#endif
338
Tim Peters11b23062003-04-23 02:39:17 +0000339#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000340#include <sys/mkdev.h>
341#else
342#if defined(MAJOR_IN_SYSMACROS)
343#include <sys/sysmacros.h>
344#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000345#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
346#include <sys/mkdev.h>
347#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000348#endif
Fred Drake699f3522000-06-29 21:12:41 +0000349
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000350#if defined _MSC_VER && _MSC_VER >= 1400
351/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov737fb892012-12-18 21:14:22 +0200352 * valid and raise an assertion if it isn't.
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000353 * Normally, an invalid fd is likely to be a C program error and therefore
354 * an assertion can be useful, but it does contradict the POSIX standard
355 * which for write(2) states:
356 * "Otherwise, -1 shall be returned and errno set to indicate the error."
357 * "[EBADF] The fildes argument is not a valid file descriptor open for
358 * writing."
359 * Furthermore, python allows the user to enter any old integer
360 * as a fd and should merely raise a python exception on error.
361 * The Microsoft CRT doesn't provide an official way to check for the
362 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000363 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000364 * internal structures involved.
365 * The structures below must be updated for each version of visual studio
366 * according to the file internal.h in the CRT source, until MS comes
367 * up with a less hacky way to do this.
368 * (all of this is to avoid globally modifying the CRT behaviour using
369 * _set_invalid_parameter_handler() and _CrtSetReportMode())
370 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000371/* The actual size of the structure is determined at runtime.
372 * Only the first items must be present.
373 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000374typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000375 intptr_t osfhnd;
376 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000377} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000378
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380#define IOINFO_L2E 5
381#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
382#define IOINFO_ARRAYS 64
383#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
384#define FOPEN 0x01
385#define _NO_CONSOLE_FILENO (intptr_t)-2
386
387/* This function emulates what the windows CRT does to validate file handles */
388int
389_PyVerify_fd(int fd)
390{
Victor Stinner8c62be82010-05-06 00:08:46 +0000391 const int i1 = fd >> IOINFO_L2E;
392 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000393
Antoine Pitrou22e41552010-08-15 18:07:50 +0000394 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000395
Victor Stinner8c62be82010-05-06 00:08:46 +0000396 /* Determine the actual size of the ioinfo structure,
397 * as used by the CRT loaded in memory
398 */
399 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
400 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
401 }
402 if (sizeof_ioinfo == 0) {
403 /* This should not happen... */
404 goto fail;
405 }
406
407 /* See that it isn't a special CLEAR fileno */
408 if (fd != _NO_CONSOLE_FILENO) {
409 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
410 * we check pointer validity and other info
411 */
412 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
413 /* finally, check that the file is open */
414 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
415 if (info->osfile & FOPEN) {
416 return 1;
417 }
418 }
419 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000420 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000421 errno = EBADF;
422 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423}
424
425/* the special case of checking dup2. The target fd must be in a sensible range */
426static int
427_PyVerify_fd_dup2(int fd1, int fd2)
428{
Victor Stinner8c62be82010-05-06 00:08:46 +0000429 if (!_PyVerify_fd(fd1))
430 return 0;
431 if (fd2 == _NO_CONSOLE_FILENO)
432 return 0;
433 if ((unsigned)fd2 < _NHANDLE_)
434 return 1;
435 else
436 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000437}
438#else
439/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
440#define _PyVerify_fd_dup2(A, B) (1)
441#endif
442
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000443#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000444/* The following structure was copied from
445 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
446 include doesn't seem to be present in the Windows SDK (at least as included
447 with Visual Studio Express). */
448typedef struct _REPARSE_DATA_BUFFER {
449 ULONG ReparseTag;
450 USHORT ReparseDataLength;
451 USHORT Reserved;
452 union {
453 struct {
454 USHORT SubstituteNameOffset;
455 USHORT SubstituteNameLength;
456 USHORT PrintNameOffset;
457 USHORT PrintNameLength;
458 ULONG Flags;
459 WCHAR PathBuffer[1];
460 } SymbolicLinkReparseBuffer;
461
462 struct {
463 USHORT SubstituteNameOffset;
464 USHORT SubstituteNameLength;
465 USHORT PrintNameOffset;
466 USHORT PrintNameLength;
467 WCHAR PathBuffer[1];
468 } MountPointReparseBuffer;
469
470 struct {
471 UCHAR DataBuffer[1];
472 } GenericReparseBuffer;
473 };
474} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
475
476#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
477 GenericReparseBuffer)
478#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
479
480static int
Brian Curtind25aef52011-06-13 15:16:04 -0500481win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000482{
483 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
484 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
485 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000486
487 if (0 == DeviceIoControl(
488 reparse_point_handle,
489 FSCTL_GET_REPARSE_POINT,
490 NULL, 0, /* in buffer */
491 target_buffer, sizeof(target_buffer),
492 &n_bytes_returned,
493 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500494 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000495
496 if (reparse_tag)
497 *reparse_tag = rdb->ReparseTag;
498
Brian Curtind25aef52011-06-13 15:16:04 -0500499 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000500}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000501#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000502
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +0100504#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +0000505/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +0100506** environ directly, we must obtain it with _NSGetEnviron(). See also
507** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +0000508*/
509#include <crt_externs.h>
510static char **environ;
511#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000513#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000514
Barry Warsaw53699e91996-12-10 23:23:01 +0000515static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000516convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517{
Victor Stinner8c62be82010-05-06 00:08:46 +0000518 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000519#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000520 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000521#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000522 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000523#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000524#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000525 APIRET rc;
526 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
527#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000528
Victor Stinner8c62be82010-05-06 00:08:46 +0000529 d = PyDict_New();
530 if (d == NULL)
531 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +0100532#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +0000533 if (environ == NULL)
534 environ = *_NSGetEnviron();
535#endif
536#ifdef MS_WINDOWS
537 /* _wenviron must be initialized in this way if the program is started
538 through main() instead of wmain(). */
539 _wgetenv(L"");
540 if (_wenviron == NULL)
541 return d;
542 /* This part ignores errors */
543 for (e = _wenviron; *e != NULL; e++) {
544 PyObject *k;
545 PyObject *v;
546 wchar_t *p = wcschr(*e, L'=');
547 if (p == NULL)
548 continue;
549 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
550 if (k == NULL) {
551 PyErr_Clear();
552 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000553 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
555 if (v == NULL) {
556 PyErr_Clear();
557 Py_DECREF(k);
558 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000559 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000560 if (PyDict_GetItem(d, k) == NULL) {
561 if (PyDict_SetItem(d, k, v) != 0)
562 PyErr_Clear();
563 }
564 Py_DECREF(k);
565 Py_DECREF(v);
566 }
567#else
568 if (environ == NULL)
569 return d;
570 /* This part ignores errors */
571 for (e = environ; *e != NULL; e++) {
572 PyObject *k;
573 PyObject *v;
574 char *p = strchr(*e, '=');
575 if (p == NULL)
576 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000577 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000578 if (k == NULL) {
579 PyErr_Clear();
580 continue;
581 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000582 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000583 if (v == NULL) {
584 PyErr_Clear();
585 Py_DECREF(k);
586 continue;
587 }
588 if (PyDict_GetItem(d, k) == NULL) {
589 if (PyDict_SetItem(d, k, v) != 0)
590 PyErr_Clear();
591 }
592 Py_DECREF(k);
593 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000594 }
595#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000596#if defined(PYOS_OS2)
597 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
598 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
599 PyObject *v = PyBytes_FromString(buffer);
600 PyDict_SetItemString(d, "BEGINLIBPATH", v);
601 Py_DECREF(v);
602 }
603 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
604 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
605 PyObject *v = PyBytes_FromString(buffer);
606 PyDict_SetItemString(d, "ENDLIBPATH", v);
607 Py_DECREF(v);
608 }
609#endif
610 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611}
612
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613/* Set a POSIX-specific error from errno, and return NULL */
614
Barry Warsawd58d7641998-07-23 16:14:40 +0000615static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000616posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000617{
Victor Stinner8c62be82010-05-06 00:08:46 +0000618 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
Barry Warsawd58d7641998-07-23 16:14:40 +0000620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000621posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000622{
Victor Stinner8c62be82010-05-06 00:08:46 +0000623 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000624}
625
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000626
Mark Hammondef8b6542001-05-13 08:04:26 +0000627static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000628posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000629{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000630 PyObject *name_str, *rc;
631 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
632 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000633 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000634 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
635 name_str);
636 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000637 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000638}
639
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000640#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000641static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000642win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000643{
Victor Stinner8c62be82010-05-06 00:08:46 +0000644 /* XXX We should pass the function name along in the future.
645 (winreg.c also wants to pass the function name.)
646 This would however require an additional param to the
647 Windows error object, which is non-trivial.
648 */
649 errno = GetLastError();
650 if (filename)
651 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
652 else
653 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000654}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000655
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000656static PyObject *
657win32_error_unicode(char* function, Py_UNICODE* filename)
658{
Victor Stinner8c62be82010-05-06 00:08:46 +0000659 /* XXX - see win32_error for comments on 'function' */
660 errno = GetLastError();
661 if (filename)
662 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
663 else
664 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000665}
666
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000668convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669{
Victor Stinner8c62be82010-05-06 00:08:46 +0000670 if (PyUnicode_CheckExact(*param))
671 Py_INCREF(*param);
672 else if (PyUnicode_Check(*param))
673 /* For a Unicode subtype that's not a Unicode object,
674 return a true Unicode object with the same data. */
675 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
676 PyUnicode_GET_SIZE(*param));
677 else
678 *param = PyUnicode_FromEncodedObject(*param,
679 Py_FileSystemDefaultEncoding,
680 "strict");
681 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000682}
683
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000684#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685
Guido van Rossumd48f2521997-12-05 22:19:34 +0000686#if defined(PYOS_OS2)
687/**********************************************************************
688 * Helper Function to Trim and Format OS/2 Messages
689 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000690static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000691os2_formatmsg(char *msgbuf, int msglen, char *reason)
692{
693 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
694
695 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
696 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
697
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000698 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000699 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
700 }
701
702 /* Add Optional Reason Text */
703 if (reason) {
704 strcat(msgbuf, " : ");
705 strcat(msgbuf, reason);
706 }
707}
708
709/**********************************************************************
710 * Decode an OS/2 Operating System Error Code
711 *
712 * A convenience function to lookup an OS/2 error code and return a
713 * text message we can use to raise a Python exception.
714 *
715 * Notes:
716 * The messages for errors returned from the OS/2 kernel reside in
717 * the file OSO001.MSG in the \OS2 directory hierarchy.
718 *
719 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000720static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000721os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
722{
723 APIRET rc;
724 ULONG msglen;
725
726 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
727 Py_BEGIN_ALLOW_THREADS
728 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
729 errorcode, "oso001.msg", &msglen);
730 Py_END_ALLOW_THREADS
731
732 if (rc == NO_ERROR)
733 os2_formatmsg(msgbuf, msglen, reason);
734 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000735 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000736 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000737
738 return msgbuf;
739}
740
741/* Set an OS/2-specific error and return NULL. OS/2 kernel
742 errors are not in a global variable e.g. 'errno' nor are
743 they congruent with posix error numbers. */
744
Victor Stinner8c62be82010-05-06 00:08:46 +0000745static PyObject *
746os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000747{
748 char text[1024];
749 PyObject *v;
750
751 os2_strerror(text, sizeof(text), code, "");
752
753 v = Py_BuildValue("(is)", code, text);
754 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000755 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000756 Py_DECREF(v);
757 }
758 return NULL; /* Signal to Python that an Exception is Pending */
759}
760
761#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762
763/* POSIX generic methods */
764
Barry Warsaw53699e91996-12-10 23:23:01 +0000765static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000766posix_fildes(PyObject *fdobj, int (*func)(int))
767{
Victor Stinner8c62be82010-05-06 00:08:46 +0000768 int fd;
769 int res;
770 fd = PyObject_AsFileDescriptor(fdobj);
771 if (fd < 0)
772 return NULL;
773 if (!_PyVerify_fd(fd))
774 return posix_error();
775 Py_BEGIN_ALLOW_THREADS
776 res = (*func)(fd);
777 Py_END_ALLOW_THREADS
778 if (res < 0)
779 return posix_error();
780 Py_INCREF(Py_None);
781 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000782}
Guido van Rossum21142a01999-01-08 21:05:37 +0000783
784static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786{
Victor Stinner8c62be82010-05-06 00:08:46 +0000787 PyObject *opath1 = NULL;
788 char *path1;
789 int res;
790 if (!PyArg_ParseTuple(args, format,
791 PyUnicode_FSConverter, &opath1))
792 return NULL;
793 path1 = PyBytes_AsString(opath1);
794 Py_BEGIN_ALLOW_THREADS
795 res = (*func)(path1);
796 Py_END_ALLOW_THREADS
797 if (res < 0)
798 return posix_error_with_allocated_filename(opath1);
799 Py_DECREF(opath1);
800 Py_INCREF(Py_None);
801 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802}
803
Barry Warsaw53699e91996-12-10 23:23:01 +0000804static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000805posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000806 char *format,
807 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808{
Victor Stinner8c62be82010-05-06 00:08:46 +0000809 PyObject *opath1 = NULL, *opath2 = NULL;
810 char *path1, *path2;
811 int res;
812 if (!PyArg_ParseTuple(args, format,
813 PyUnicode_FSConverter, &opath1,
814 PyUnicode_FSConverter, &opath2)) {
815 return NULL;
816 }
817 path1 = PyBytes_AsString(opath1);
818 path2 = PyBytes_AsString(opath2);
819 Py_BEGIN_ALLOW_THREADS
820 res = (*func)(path1, path2);
821 Py_END_ALLOW_THREADS
822 Py_DECREF(opath1);
823 Py_DECREF(opath2);
824 if (res != 0)
825 /* XXX how to report both path1 and path2??? */
826 return posix_error();
827 Py_INCREF(Py_None);
828 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829}
830
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000831#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000833win32_1str(PyObject* args, char* func,
834 char* format, BOOL (__stdcall *funcA)(LPCSTR),
835 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000836{
Victor Stinner8c62be82010-05-06 00:08:46 +0000837 PyObject *uni;
838 char *ansi;
839 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000840
Victor Stinner8c62be82010-05-06 00:08:46 +0000841 if (!PyArg_ParseTuple(args, wformat, &uni))
842 PyErr_Clear();
843 else {
844 Py_BEGIN_ALLOW_THREADS
845 result = funcW(PyUnicode_AsUnicode(uni));
846 Py_END_ALLOW_THREADS
847 if (!result)
848 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
849 Py_INCREF(Py_None);
850 return Py_None;
851 }
852 if (!PyArg_ParseTuple(args, format, &ansi))
853 return NULL;
854 Py_BEGIN_ALLOW_THREADS
855 result = funcA(ansi);
856 Py_END_ALLOW_THREADS
857 if (!result)
858 return win32_error(func, ansi);
859 Py_INCREF(Py_None);
860 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000861
862}
863
864/* This is a reimplementation of the C library's chdir function,
865 but one that produces Win32 errors instead of DOS error codes.
866 chdir is essentially a wrapper around SetCurrentDirectory; however,
867 it also needs to set "magic" environment variables indicating
868 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000869static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870win32_chdir(LPCSTR path)
871{
Victor Stinner8c62be82010-05-06 00:08:46 +0000872 char new_path[MAX_PATH+1];
873 int result;
874 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875
Victor Stinner8c62be82010-05-06 00:08:46 +0000876 if(!SetCurrentDirectoryA(path))
877 return FALSE;
878 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
879 if (!result)
880 return FALSE;
881 /* In the ANSI API, there should not be any paths longer
882 than MAX_PATH. */
883 assert(result <= MAX_PATH+1);
884 if (strncmp(new_path, "\\\\", 2) == 0 ||
885 strncmp(new_path, "//", 2) == 0)
886 /* UNC path, nothing to do. */
887 return TRUE;
888 env[1] = new_path[0];
889 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890}
891
892/* The Unicode version differs from the ANSI version
893 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000894static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000895win32_wchdir(LPCWSTR path)
896{
Victor Stinner8c62be82010-05-06 00:08:46 +0000897 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
898 int result;
899 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900
Victor Stinner8c62be82010-05-06 00:08:46 +0000901 if(!SetCurrentDirectoryW(path))
902 return FALSE;
903 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
904 if (!result)
905 return FALSE;
906 if (result > MAX_PATH+1) {
907 new_path = malloc(result * sizeof(wchar_t));
908 if (!new_path) {
909 SetLastError(ERROR_OUTOFMEMORY);
910 return FALSE;
911 }
912 result = GetCurrentDirectoryW(result, new_path);
913 if (!result) {
914 free(new_path);
915 return FALSE;
916 }
917 }
918 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
919 wcsncmp(new_path, L"//", 2) == 0)
920 /* UNC path, nothing to do. */
921 return TRUE;
922 env[1] = new_path[0];
923 result = SetEnvironmentVariableW(env, new_path);
924 if (new_path != _new_path)
925 free(new_path);
926 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000927}
928#endif
929
Martin v. Löwis14694662006-02-03 12:54:16 +0000930#ifdef MS_WINDOWS
931/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
932 - time stamps are restricted to second resolution
933 - file modification times suffer from forth-and-back conversions between
934 UTC and local time
935 Therefore, we implement our own stat, based on the Win32 API directly.
936*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000937#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000938
939struct win32_stat{
940 int st_dev;
941 __int64 st_ino;
942 unsigned short st_mode;
943 int st_nlink;
944 int st_uid;
945 int st_gid;
946 int st_rdev;
947 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000948 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000949 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000950 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000951 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000952 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000953 int st_ctime_nsec;
954};
955
956static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
957
958static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000959FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000960{
Victor Stinner8c62be82010-05-06 00:08:46 +0000961 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
962 /* Cannot simply cast and dereference in_ptr,
963 since it might not be aligned properly */
964 __int64 in;
965 memcpy(&in, in_ptr, sizeof(in));
966 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000967 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +0000968}
969
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000971time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972{
Victor Stinner8c62be82010-05-06 00:08:46 +0000973 /* XXX endianness */
974 __int64 out;
975 out = time_in + secs_between_epochs;
976 out = out * 10000000 + nsec_in / 100;
977 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978}
979
Martin v. Löwis14694662006-02-03 12:54:16 +0000980/* Below, we *know* that ugo+r is 0444 */
981#if _S_IREAD != 0400
982#error Unsupported C library
983#endif
984static int
985attributes_to_mode(DWORD attr)
986{
Victor Stinner8c62be82010-05-06 00:08:46 +0000987 int m = 0;
988 if (attr & FILE_ATTRIBUTE_DIRECTORY)
989 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
990 else
991 m |= _S_IFREG;
992 if (attr & FILE_ATTRIBUTE_READONLY)
993 m |= 0444;
994 else
995 m |= 0666;
996 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000997}
998
999static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001000attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001001{
Victor Stinner8c62be82010-05-06 00:08:46 +00001002 memset(result, 0, sizeof(*result));
1003 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1004 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1005 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1006 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1007 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001008 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001009 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001010 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1011 /* first clear the S_IFMT bits */
1012 result->st_mode ^= (result->st_mode & 0170000);
1013 /* now set the bits that make this a symlink */
1014 result->st_mode |= 0120000;
1015 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001016
Victor Stinner8c62be82010-05-06 00:08:46 +00001017 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001018}
1019
Guido van Rossumd8faa362007-04-27 19:54:29 +00001020static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001021attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022{
Victor Stinner8c62be82010-05-06 00:08:46 +00001023 HANDLE hFindFile;
1024 WIN32_FIND_DATAA FileData;
1025 hFindFile = FindFirstFileA(pszFile, &FileData);
1026 if (hFindFile == INVALID_HANDLE_VALUE)
1027 return FALSE;
1028 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001029 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001030 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001031 info->dwFileAttributes = FileData.dwFileAttributes;
1032 info->ftCreationTime = FileData.ftCreationTime;
1033 info->ftLastAccessTime = FileData.ftLastAccessTime;
1034 info->ftLastWriteTime = FileData.ftLastWriteTime;
1035 info->nFileSizeHigh = FileData.nFileSizeHigh;
1036 info->nFileSizeLow = FileData.nFileSizeLow;
1037/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001038 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1039 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001040 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001041}
1042
1043static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001044attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001045{
Victor Stinner8c62be82010-05-06 00:08:46 +00001046 HANDLE hFindFile;
1047 WIN32_FIND_DATAW FileData;
1048 hFindFile = FindFirstFileW(pszFile, &FileData);
1049 if (hFindFile == INVALID_HANDLE_VALUE)
1050 return FALSE;
1051 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001052 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001053 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001054 info->dwFileAttributes = FileData.dwFileAttributes;
1055 info->ftCreationTime = FileData.ftCreationTime;
1056 info->ftLastAccessTime = FileData.ftLastAccessTime;
1057 info->ftLastWriteTime = FileData.ftLastWriteTime;
1058 info->nFileSizeHigh = FileData.nFileSizeHigh;
1059 info->nFileSizeLow = FileData.nFileSizeLow;
1060/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001061 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1062 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001063 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001064}
1065
Brian Curtind25aef52011-06-13 15:16:04 -05001066/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1067static int has_GetFinalPathNameByHandle = 0;
1068static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1069 DWORD);
1070static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1071 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001072static int
Brian Curtind25aef52011-06-13 15:16:04 -05001073check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001074{
Brian Curtind25aef52011-06-13 15:16:04 -05001075 HINSTANCE hKernel32;
1076 /* only recheck */
1077 if (!has_GetFinalPathNameByHandle)
1078 {
1079 hKernel32 = GetModuleHandle("KERNEL32");
1080 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1081 "GetFinalPathNameByHandleA");
1082 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1083 "GetFinalPathNameByHandleW");
1084 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1085 Py_GetFinalPathNameByHandleW;
1086 }
1087 return has_GetFinalPathNameByHandle;
1088}
1089
1090static BOOL
1091get_target_path(HANDLE hdl, wchar_t **target_path)
1092{
1093 int buf_size, result_length;
1094 wchar_t *buf;
1095
1096 /* We have a good handle to the target, use it to determine
1097 the target path name (then we'll call lstat on it). */
1098 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1099 VOLUME_NAME_DOS);
1100 if(!buf_size)
1101 return FALSE;
1102
1103 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001104 if (!buf) {
1105 SetLastError(ERROR_OUTOFMEMORY);
1106 return FALSE;
1107 }
1108
Brian Curtind25aef52011-06-13 15:16:04 -05001109 result_length = Py_GetFinalPathNameByHandleW(hdl,
1110 buf, buf_size, VOLUME_NAME_DOS);
1111
1112 if(!result_length) {
1113 free(buf);
1114 return FALSE;
1115 }
1116
1117 if(!CloseHandle(hdl)) {
1118 free(buf);
1119 return FALSE;
1120 }
1121
1122 buf[result_length] = 0;
1123
1124 *target_path = buf;
1125 return TRUE;
1126}
1127
1128static int
1129win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1130 BOOL traverse);
1131static int
1132win32_xstat_impl(const char *path, struct win32_stat *result,
1133 BOOL traverse)
1134{
Victor Stinner26de69d2011-06-17 15:15:38 +02001135 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001136 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001137 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001138 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001139 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001140 const char *dot;
1141
Brian Curtind25aef52011-06-13 15:16:04 -05001142 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001143 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1144 traverse reparse point. */
1145 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001146 }
1147
Brian Curtinf5e76d02010-11-24 13:14:05 +00001148 hFile = CreateFileA(
1149 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001150 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001151 0, /* share mode */
1152 NULL, /* security attributes */
1153 OPEN_EXISTING,
1154 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001155 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1156 Because of this, calls like GetFinalPathNameByHandle will return
1157 the symlink path agin and not the actual final path. */
1158 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1159 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001160 NULL);
1161
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001162 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001163 /* Either the target doesn't exist, or we don't have access to
1164 get a handle to it. If the former, we need to return an error.
1165 If the latter, we can use attributes_from_dir. */
1166 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001167 return -1;
1168 /* Could not get attributes on open file. Fall back to
1169 reading the directory. */
1170 if (!attributes_from_dir(path, &info, &reparse_tag))
1171 /* Very strange. This should not fail now */
1172 return -1;
1173 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1174 if (traverse) {
1175 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001177 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001178 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001179 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001180 } else {
1181 if (!GetFileInformationByHandle(hFile, &info)) {
1182 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001183 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001184 }
1185 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001186 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1187 return -1;
1188
1189 /* Close the outer open file handle now that we're about to
1190 reopen it with different flags. */
1191 if (!CloseHandle(hFile))
1192 return -1;
1193
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001194 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001195 /* In order to call GetFinalPathNameByHandle we need to open
1196 the file without the reparse handling flag set. */
1197 hFile2 = CreateFileA(
1198 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1199 NULL, OPEN_EXISTING,
1200 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1201 NULL);
1202 if (hFile2 == INVALID_HANDLE_VALUE)
1203 return -1;
1204
1205 if (!get_target_path(hFile2, &target_path))
1206 return -1;
1207
1208 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001209 free(target_path);
1210 return code;
1211 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001212 } else
1213 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001214 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001215 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216
1217 /* Set S_IEXEC if it is an .exe, .bat, ... */
1218 dot = strrchr(path, '.');
1219 if (dot) {
1220 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1221 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1222 result->st_mode |= 0111;
1223 }
1224 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001225}
1226
1227static int
Brian Curtind25aef52011-06-13 15:16:04 -05001228win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1229 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001230{
1231 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001232 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001233 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001234 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001235 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001236 const wchar_t *dot;
1237
Brian Curtind25aef52011-06-13 15:16:04 -05001238 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001239 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1240 traverse reparse point. */
1241 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001242 }
1243
Brian Curtinf5e76d02010-11-24 13:14:05 +00001244 hFile = CreateFileW(
1245 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001246 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001247 0, /* share mode */
1248 NULL, /* security attributes */
1249 OPEN_EXISTING,
1250 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001251 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1252 Because of this, calls like GetFinalPathNameByHandle will return
1253 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001254 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001255 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001256 NULL);
1257
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001258 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259 /* Either the target doesn't exist, or we don't have access to
1260 get a handle to it. If the former, we need to return an error.
1261 If the latter, we can use attributes_from_dir. */
1262 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001263 return -1;
1264 /* Could not get attributes on open file. Fall back to
1265 reading the directory. */
1266 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1267 /* Very strange. This should not fail now */
1268 return -1;
1269 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1270 if (traverse) {
1271 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001272 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001273 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001274 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001275 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001276 } else {
1277 if (!GetFileInformationByHandle(hFile, &info)) {
1278 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001279 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001280 }
1281 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001282 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1283 return -1;
1284
1285 /* Close the outer open file handle now that we're about to
1286 reopen it with different flags. */
1287 if (!CloseHandle(hFile))
1288 return -1;
1289
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001290 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001291 /* In order to call GetFinalPathNameByHandle we need to open
1292 the file without the reparse handling flag set. */
1293 hFile2 = CreateFileW(
1294 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1295 NULL, OPEN_EXISTING,
1296 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1297 NULL);
1298 if (hFile2 == INVALID_HANDLE_VALUE)
1299 return -1;
1300
1301 if (!get_target_path(hFile2, &target_path))
1302 return -1;
1303
1304 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001305 free(target_path);
1306 return code;
1307 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001308 } else
1309 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001310 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001311 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001312
1313 /* Set S_IEXEC if it is an .exe, .bat, ... */
1314 dot = wcsrchr(path, '.');
1315 if (dot) {
1316 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1317 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1318 result->st_mode |= 0111;
1319 }
1320 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001321}
1322
1323static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001324win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001325{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001326 /* Protocol violation: we explicitly clear errno, instead of
1327 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001328 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001329 errno = 0;
1330 return code;
1331}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001332
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001333static int
1334win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1335{
1336 /* Protocol violation: we explicitly clear errno, instead of
1337 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001338 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001339 errno = 0;
1340 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001341}
Brian Curtind25aef52011-06-13 15:16:04 -05001342/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001343
1344 In Posix, stat automatically traverses symlinks and returns the stat
1345 structure for the target. In Windows, the equivalent GetFileAttributes by
1346 default does not traverse symlinks and instead returns attributes for
1347 the symlink.
1348
1349 Therefore, win32_lstat will get the attributes traditionally, and
1350 win32_stat will first explicitly resolve the symlink target and then will
1351 call win32_lstat on that result.
1352
Ezio Melotti4969f702011-03-15 05:59:46 +02001353 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001354
Brian Curtind25aef52011-06-13 15:16:04 -05001355static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001356win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001357{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001358 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001359}
1360
Victor Stinner8c62be82010-05-06 00:08:46 +00001361static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001362win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001363{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001364 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001365}
1366
1367static int
1368win32_stat(const char* path, struct win32_stat *result)
1369{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001370 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001371}
1372
Victor Stinner26de69d2011-06-17 15:15:38 +02001373static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001374win32_stat_w(const wchar_t* path, struct win32_stat *result)
1375{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001376 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001377}
1378
1379static int
1380win32_fstat(int file_number, struct win32_stat *result)
1381{
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 BY_HANDLE_FILE_INFORMATION info;
1383 HANDLE h;
1384 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001385
Victor Stinner8c62be82010-05-06 00:08:46 +00001386 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001387
Victor Stinner8c62be82010-05-06 00:08:46 +00001388 /* Protocol violation: we explicitly clear errno, instead of
1389 setting it to a POSIX error. Callers should use GetLastError. */
1390 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001391
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 if (h == INVALID_HANDLE_VALUE) {
1393 /* This is really a C library error (invalid file handle).
1394 We set the Win32 error to the closes one matching. */
1395 SetLastError(ERROR_INVALID_HANDLE);
1396 return -1;
1397 }
1398 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001399
Victor Stinner8c62be82010-05-06 00:08:46 +00001400 type = GetFileType(h);
1401 if (type == FILE_TYPE_UNKNOWN) {
1402 DWORD error = GetLastError();
1403 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001405 }
1406 /* else: valid but unknown file */
1407 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001408
Victor Stinner8c62be82010-05-06 00:08:46 +00001409 if (type != FILE_TYPE_DISK) {
1410 if (type == FILE_TYPE_CHAR)
1411 result->st_mode = _S_IFCHR;
1412 else if (type == FILE_TYPE_PIPE)
1413 result->st_mode = _S_IFIFO;
1414 return 0;
1415 }
1416
1417 if (!GetFileInformationByHandle(h, &info)) {
1418 return -1;
1419 }
1420
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001421 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001423 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1424 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001425}
1426
1427#endif /* MS_WINDOWS */
1428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001429PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001430"stat_result: Result from stat or lstat.\n\n\
1431This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001432 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001433or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1434\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001435Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1436or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001437\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001438See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001439
1440static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 {"st_mode", "protection bits"},
1442 {"st_ino", "inode"},
1443 {"st_dev", "device"},
1444 {"st_nlink", "number of hard links"},
1445 {"st_uid", "user ID of owner"},
1446 {"st_gid", "group ID of owner"},
1447 {"st_size", "total size, in bytes"},
1448 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1449 {NULL, "integer time of last access"},
1450 {NULL, "integer time of last modification"},
1451 {NULL, "integer time of last change"},
1452 {"st_atime", "time of last access"},
1453 {"st_mtime", "time of last modification"},
1454 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001455#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001456 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001457#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001458#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001460#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001461#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001463#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001464#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001465 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001466#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001467#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001469#endif
1470#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001472#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001474};
1475
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001476#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001477#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001478#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001479#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001480#endif
1481
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001482#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001483#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1484#else
1485#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1486#endif
1487
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001488#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001489#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1490#else
1491#define ST_RDEV_IDX ST_BLOCKS_IDX
1492#endif
1493
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001494#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1495#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1496#else
1497#define ST_FLAGS_IDX ST_RDEV_IDX
1498#endif
1499
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001500#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001501#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001502#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001503#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001504#endif
1505
1506#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1507#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1508#else
1509#define ST_BIRTHTIME_IDX ST_GEN_IDX
1510#endif
1511
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001513 "stat_result", /* name */
1514 stat_result__doc__, /* doc */
1515 stat_result_fields,
1516 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001517};
1518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001520"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1521This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001522 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001523or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001524\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001526
1527static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001528 {"f_bsize", },
1529 {"f_frsize", },
1530 {"f_blocks", },
1531 {"f_bfree", },
1532 {"f_bavail", },
1533 {"f_files", },
1534 {"f_ffree", },
1535 {"f_favail", },
1536 {"f_flag", },
1537 {"f_namemax",},
1538 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001539};
1540
1541static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001542 "statvfs_result", /* name */
1543 statvfs_result__doc__, /* doc */
1544 statvfs_result_fields,
1545 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001546};
1547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001548static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001549static PyTypeObject StatResultType;
1550static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001551static newfunc structseq_new;
1552
1553static PyObject *
1554statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1555{
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 PyStructSequence *result;
1557 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001558
Victor Stinner8c62be82010-05-06 00:08:46 +00001559 result = (PyStructSequence*)structseq_new(type, args, kwds);
1560 if (!result)
1561 return NULL;
1562 /* If we have been initialized from a tuple,
1563 st_?time might be set to None. Initialize it
1564 from the int slots. */
1565 for (i = 7; i <= 9; i++) {
1566 if (result->ob_item[i+3] == Py_None) {
1567 Py_DECREF(Py_None);
1568 Py_INCREF(result->ob_item[i]);
1569 result->ob_item[i+3] = result->ob_item[i];
1570 }
1571 }
1572 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001573}
1574
1575
1576
1577/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001578static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001579
1580PyDoc_STRVAR(stat_float_times__doc__,
1581"stat_float_times([newval]) -> oldval\n\n\
1582Determine whether os.[lf]stat represents time stamps as float objects.\n\
1583If newval is True, future calls to stat() return floats, if it is False,\n\
1584future calls return ints. \n\
1585If newval is omitted, return the current setting.\n");
1586
1587static PyObject*
1588stat_float_times(PyObject* self, PyObject *args)
1589{
Victor Stinner8c62be82010-05-06 00:08:46 +00001590 int newval = -1;
1591 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1592 return NULL;
1593 if (newval == -1)
1594 /* Return old value */
1595 return PyBool_FromLong(_stat_float_times);
1596 _stat_float_times = newval;
1597 Py_INCREF(Py_None);
1598 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001599}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001600
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001601static void
1602fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1603{
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001605#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001606 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001607#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001609#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 if (!ival)
1611 return;
1612 if (_stat_float_times) {
1613 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1614 } else {
1615 fval = ival;
1616 Py_INCREF(fval);
1617 }
1618 PyStructSequence_SET_ITEM(v, index, ival);
1619 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001620}
1621
Tim Peters5aa91602002-01-30 05:46:57 +00001622/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001623 (used by posix_stat() and posix_fstat()) */
1624static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001625_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001626{
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 unsigned long ansec, mnsec, cnsec;
1628 PyObject *v = PyStructSequence_New(&StatResultType);
1629 if (v == NULL)
1630 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001631
Victor Stinner8c62be82010-05-06 00:08:46 +00001632 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001633#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001634 PyStructSequence_SET_ITEM(v, 1,
1635 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001636#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001637 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001638#endif
1639#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001640 PyStructSequence_SET_ITEM(v, 2,
1641 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001642#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001643 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001644#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1646 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1647 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001648#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001649 PyStructSequence_SET_ITEM(v, 6,
1650 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001651#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001652 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001653#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001654
Martin v. Löwis14694662006-02-03 12:54:16 +00001655#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 ansec = st->st_atim.tv_nsec;
1657 mnsec = st->st_mtim.tv_nsec;
1658 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001659#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 ansec = st->st_atimespec.tv_nsec;
1661 mnsec = st->st_mtimespec.tv_nsec;
1662 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001663#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 ansec = st->st_atime_nsec;
1665 mnsec = st->st_mtime_nsec;
1666 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001667#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001669#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 fill_time(v, 7, st->st_atime, ansec);
1671 fill_time(v, 8, st->st_mtime, mnsec);
1672 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001673
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001674#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001675 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1676 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001677#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001678#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1680 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001681#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001682#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1684 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001685#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001686#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1688 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001689#endif
1690#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 {
1692 PyObject *val;
1693 unsigned long bsec,bnsec;
1694 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001695#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001697#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001699#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 if (_stat_float_times) {
1701 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1702 } else {
1703 val = PyLong_FromLong((long)bsec);
1704 }
1705 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1706 val);
1707 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001708#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001709#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001710 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1711 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001712#endif
Fred Drake699f3522000-06-29 21:12:41 +00001713
Victor Stinner8c62be82010-05-06 00:08:46 +00001714 if (PyErr_Occurred()) {
1715 Py_DECREF(v);
1716 return NULL;
1717 }
Fred Drake699f3522000-06-29 21:12:41 +00001718
Victor Stinner8c62be82010-05-06 00:08:46 +00001719 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001720}
1721
Barry Warsaw53699e91996-12-10 23:23:01 +00001722static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001723posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001724 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001725#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001727#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001729#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 char *wformat,
1731 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001732{
Victor Stinner8c62be82010-05-06 00:08:46 +00001733 STRUCT_STAT st;
1734 PyObject *opath;
1735 char *path;
1736 int res;
1737 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001738
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001739#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001740 PyUnicodeObject *po;
1741 if (PyArg_ParseTuple(args, wformat, &po)) {
1742 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001743
Victor Stinner8c62be82010-05-06 00:08:46 +00001744 Py_BEGIN_ALLOW_THREADS
1745 /* PyUnicode_AS_UNICODE result OK without
1746 thread lock as it is a simple dereference. */
1747 res = wstatfunc(wpath, &st);
1748 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001749
Victor Stinner8c62be82010-05-06 00:08:46 +00001750 if (res != 0)
1751 return win32_error_unicode("stat", wpath);
1752 return _pystat_fromstructstat(&st);
1753 }
1754 /* Drop the argument parsing error as narrow strings
1755 are also valid. */
1756 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001757#endif
1758
Victor Stinner8c62be82010-05-06 00:08:46 +00001759 if (!PyArg_ParseTuple(args, format,
1760 PyUnicode_FSConverter, &opath))
1761 return NULL;
1762 path = PyBytes_AsString(opath);
1763 Py_BEGIN_ALLOW_THREADS
1764 res = (*statfunc)(path, &st);
1765 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001766
Victor Stinner8c62be82010-05-06 00:08:46 +00001767 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001768#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001770#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001771 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001772#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001773 }
1774 else
1775 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001776
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 Py_DECREF(opath);
1778 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001779}
1780
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001781/* POSIX methods */
1782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001783PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001784"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001785Use the real uid/gid to test for access to a path. Note that most\n\
1786operations will use the effective uid/gid, therefore this routine can\n\
1787be used in a suid/sgid environment to test if the invoking user has the\n\
1788specified access to the path. The mode argument can be F_OK to test\n\
1789existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001790
1791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001792posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001793{
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 PyObject *opath;
1795 char *path;
1796 int mode;
1797
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001798#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 DWORD attr;
1800 PyUnicodeObject *po;
1801 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1802 Py_BEGIN_ALLOW_THREADS
1803 /* PyUnicode_AS_UNICODE OK without thread lock as
1804 it is a simple dereference. */
1805 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1806 Py_END_ALLOW_THREADS
1807 goto finish;
1808 }
1809 /* Drop the argument parsing error as narrow strings
1810 are also valid. */
1811 PyErr_Clear();
1812 if (!PyArg_ParseTuple(args, "O&i:access",
1813 PyUnicode_FSConverter, &opath, &mode))
1814 return NULL;
1815 path = PyBytes_AsString(opath);
1816 Py_BEGIN_ALLOW_THREADS
1817 attr = GetFileAttributesA(path);
1818 Py_END_ALLOW_THREADS
1819 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001820finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001821 if (attr == 0xFFFFFFFF)
1822 /* File does not exist, or cannot read attributes */
1823 return PyBool_FromLong(0);
1824 /* Access is possible if either write access wasn't requested, or
1825 the file isn't read-only, or if it's a directory, as there are
1826 no read-only directories on Windows. */
1827 return PyBool_FromLong(!(mode & 2)
1828 || !(attr & FILE_ATTRIBUTE_READONLY)
1829 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001830#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 int res;
1832 if (!PyArg_ParseTuple(args, "O&i:access",
1833 PyUnicode_FSConverter, &opath, &mode))
1834 return NULL;
1835 path = PyBytes_AsString(opath);
1836 Py_BEGIN_ALLOW_THREADS
1837 res = access(path, mode);
1838 Py_END_ALLOW_THREADS
1839 Py_DECREF(opath);
1840 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001841#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001842}
1843
Guido van Rossumd371ff11999-01-25 16:12:23 +00001844#ifndef F_OK
1845#define F_OK 0
1846#endif
1847#ifndef R_OK
1848#define R_OK 4
1849#endif
1850#ifndef W_OK
1851#define W_OK 2
1852#endif
1853#ifndef X_OK
1854#define X_OK 1
1855#endif
1856
1857#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001861
1862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001863posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001864{
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 int id;
1866 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001867
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1869 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001870
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001871#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001872 /* file descriptor 0 only, the default input device (stdin) */
1873 if (id == 0) {
1874 ret = ttyname();
1875 }
1876 else {
1877 ret = NULL;
1878 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001879#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001880 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001881#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001882 if (ret == NULL)
1883 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001884 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001885}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001886#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001887
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001888#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001889PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001890"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001892
1893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001894posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001895{
Victor Stinner8c62be82010-05-06 00:08:46 +00001896 char *ret;
1897 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001898
Greg Wardb48bc172000-03-01 21:51:56 +00001899#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001900 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001901#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001902 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001903#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001904 if (ret == NULL)
1905 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001906 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001907}
1908#endif
1909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001911"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001913
Barry Warsaw53699e91996-12-10 23:23:01 +00001914static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001915posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001917#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001918 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001919#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001920 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001921#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001923#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001924 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001925#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001926}
1927
Fred Drake4d1e64b2002-04-15 19:40:07 +00001928#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001929PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001930"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001931Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001932opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001933
1934static PyObject *
1935posix_fchdir(PyObject *self, PyObject *fdobj)
1936{
Victor Stinner8c62be82010-05-06 00:08:46 +00001937 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001938}
1939#endif /* HAVE_FCHDIR */
1940
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001942PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001943"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001944Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001945
Barry Warsaw53699e91996-12-10 23:23:01 +00001946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001947posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001948{
Victor Stinner8c62be82010-05-06 00:08:46 +00001949 PyObject *opath = NULL;
1950 char *path = NULL;
1951 int i;
1952 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001953#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001954 DWORD attr;
1955 PyUnicodeObject *po;
1956 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1957 Py_BEGIN_ALLOW_THREADS
1958 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1959 if (attr != 0xFFFFFFFF) {
1960 if (i & _S_IWRITE)
1961 attr &= ~FILE_ATTRIBUTE_READONLY;
1962 else
1963 attr |= FILE_ATTRIBUTE_READONLY;
1964 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1965 }
1966 else
1967 res = 0;
1968 Py_END_ALLOW_THREADS
1969 if (!res)
1970 return win32_error_unicode("chmod",
1971 PyUnicode_AS_UNICODE(po));
1972 Py_INCREF(Py_None);
1973 return Py_None;
1974 }
1975 /* Drop the argument parsing error as narrow strings
1976 are also valid. */
1977 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001978
Victor Stinner8c62be82010-05-06 00:08:46 +00001979 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1980 &opath, &i))
1981 return NULL;
1982 path = PyBytes_AsString(opath);
1983 Py_BEGIN_ALLOW_THREADS
1984 attr = GetFileAttributesA(path);
1985 if (attr != 0xFFFFFFFF) {
1986 if (i & _S_IWRITE)
1987 attr &= ~FILE_ATTRIBUTE_READONLY;
1988 else
1989 attr |= FILE_ATTRIBUTE_READONLY;
1990 res = SetFileAttributesA(path, attr);
1991 }
1992 else
1993 res = 0;
1994 Py_END_ALLOW_THREADS
1995 if (!res) {
1996 win32_error("chmod", path);
1997 Py_DECREF(opath);
1998 return NULL;
1999 }
2000 Py_DECREF(opath);
2001 Py_INCREF(Py_None);
2002 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002003#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002004 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2005 &opath, &i))
2006 return NULL;
2007 path = PyBytes_AsString(opath);
2008 Py_BEGIN_ALLOW_THREADS
2009 res = chmod(path, i);
2010 Py_END_ALLOW_THREADS
2011 if (res < 0)
2012 return posix_error_with_allocated_filename(opath);
2013 Py_DECREF(opath);
2014 Py_INCREF(Py_None);
2015 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002016#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002017}
2018
Christian Heimes4e30a842007-11-30 22:12:06 +00002019#ifdef HAVE_FCHMOD
2020PyDoc_STRVAR(posix_fchmod__doc__,
2021"fchmod(fd, mode)\n\n\
2022Change the access permissions of the file given by file\n\
2023descriptor fd.");
2024
2025static PyObject *
2026posix_fchmod(PyObject *self, PyObject *args)
2027{
Victor Stinner8c62be82010-05-06 00:08:46 +00002028 int fd, mode, res;
2029 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2030 return NULL;
2031 Py_BEGIN_ALLOW_THREADS
2032 res = fchmod(fd, mode);
2033 Py_END_ALLOW_THREADS
2034 if (res < 0)
2035 return posix_error();
2036 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002037}
2038#endif /* HAVE_FCHMOD */
2039
2040#ifdef HAVE_LCHMOD
2041PyDoc_STRVAR(posix_lchmod__doc__,
2042"lchmod(path, mode)\n\n\
2043Change the access permissions of a file. If path is a symlink, this\n\
2044affects the link itself rather than the target.");
2045
2046static PyObject *
2047posix_lchmod(PyObject *self, PyObject *args)
2048{
Victor Stinner8c62be82010-05-06 00:08:46 +00002049 PyObject *opath;
2050 char *path;
2051 int i;
2052 int res;
2053 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2054 &opath, &i))
2055 return NULL;
2056 path = PyBytes_AsString(opath);
2057 Py_BEGIN_ALLOW_THREADS
2058 res = lchmod(path, i);
2059 Py_END_ALLOW_THREADS
2060 if (res < 0)
2061 return posix_error_with_allocated_filename(opath);
2062 Py_DECREF(opath);
2063 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002064}
2065#endif /* HAVE_LCHMOD */
2066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067
Thomas Wouterscf297e42007-02-23 15:07:44 +00002068#ifdef HAVE_CHFLAGS
2069PyDoc_STRVAR(posix_chflags__doc__,
2070"chflags(path, flags)\n\n\
2071Set file flags.");
2072
2073static PyObject *
2074posix_chflags(PyObject *self, PyObject *args)
2075{
Victor Stinner8c62be82010-05-06 00:08:46 +00002076 PyObject *opath;
2077 char *path;
2078 unsigned long flags;
2079 int res;
2080 if (!PyArg_ParseTuple(args, "O&k:chflags",
2081 PyUnicode_FSConverter, &opath, &flags))
2082 return NULL;
2083 path = PyBytes_AsString(opath);
2084 Py_BEGIN_ALLOW_THREADS
2085 res = chflags(path, flags);
2086 Py_END_ALLOW_THREADS
2087 if (res < 0)
2088 return posix_error_with_allocated_filename(opath);
2089 Py_DECREF(opath);
2090 Py_INCREF(Py_None);
2091 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002092}
2093#endif /* HAVE_CHFLAGS */
2094
2095#ifdef HAVE_LCHFLAGS
2096PyDoc_STRVAR(posix_lchflags__doc__,
2097"lchflags(path, flags)\n\n\
2098Set file flags.\n\
2099This function will not follow symbolic links.");
2100
2101static PyObject *
2102posix_lchflags(PyObject *self, PyObject *args)
2103{
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 PyObject *opath;
2105 char *path;
2106 unsigned long flags;
2107 int res;
2108 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2109 PyUnicode_FSConverter, &opath, &flags))
2110 return NULL;
2111 path = PyBytes_AsString(opath);
2112 Py_BEGIN_ALLOW_THREADS
2113 res = lchflags(path, flags);
2114 Py_END_ALLOW_THREADS
2115 if (res < 0)
2116 return posix_error_with_allocated_filename(opath);
2117 Py_DECREF(opath);
2118 Py_INCREF(Py_None);
2119 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002120}
2121#endif /* HAVE_LCHFLAGS */
2122
Martin v. Löwis244edc82001-10-04 22:44:26 +00002123#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002124PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002125"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002126Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002127
2128static PyObject *
2129posix_chroot(PyObject *self, PyObject *args)
2130{
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002132}
2133#endif
2134
Guido van Rossum21142a01999-01-08 21:05:37 +00002135#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002136PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002137"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002138force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002139
2140static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002141posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002142{
Stefan Krah0e803b32010-11-26 16:16:47 +00002143 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002144}
2145#endif /* HAVE_FSYNC */
2146
2147#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002148
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002149#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002150extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2151#endif
2152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002153PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002154"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002155force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002157
2158static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002159posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002160{
Stefan Krah0e803b32010-11-26 16:16:47 +00002161 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002162}
2163#endif /* HAVE_FDATASYNC */
2164
2165
Fredrik Lundh10723342000-07-10 16:38:09 +00002166#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002167PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002168"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002169Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002170
Barry Warsaw53699e91996-12-10 23:23:01 +00002171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002172posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002173{
Victor Stinner8c62be82010-05-06 00:08:46 +00002174 PyObject *opath;
2175 char *path;
2176 long uid, gid;
2177 int res;
2178 if (!PyArg_ParseTuple(args, "O&ll:chown",
2179 PyUnicode_FSConverter, &opath,
2180 &uid, &gid))
2181 return NULL;
2182 path = PyBytes_AsString(opath);
2183 Py_BEGIN_ALLOW_THREADS
2184 res = chown(path, (uid_t) uid, (gid_t) gid);
2185 Py_END_ALLOW_THREADS
2186 if (res < 0)
2187 return posix_error_with_allocated_filename(opath);
2188 Py_DECREF(opath);
2189 Py_INCREF(Py_None);
2190 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002191}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002192#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193
Christian Heimes4e30a842007-11-30 22:12:06 +00002194#ifdef HAVE_FCHOWN
2195PyDoc_STRVAR(posix_fchown__doc__,
2196"fchown(fd, uid, gid)\n\n\
2197Change the owner and group id of the file given by file descriptor\n\
2198fd to the numeric uid and gid.");
2199
2200static PyObject *
2201posix_fchown(PyObject *self, PyObject *args)
2202{
Victor Stinner8c62be82010-05-06 00:08:46 +00002203 int fd;
2204 long uid, gid;
2205 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002206 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002207 return NULL;
2208 Py_BEGIN_ALLOW_THREADS
2209 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2210 Py_END_ALLOW_THREADS
2211 if (res < 0)
2212 return posix_error();
2213 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002214}
2215#endif /* HAVE_FCHOWN */
2216
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002217#ifdef HAVE_LCHOWN
2218PyDoc_STRVAR(posix_lchown__doc__,
2219"lchown(path, uid, gid)\n\n\
2220Change the owner and group id of path to the numeric uid and gid.\n\
2221This function will not follow symbolic links.");
2222
2223static PyObject *
2224posix_lchown(PyObject *self, PyObject *args)
2225{
Victor Stinner8c62be82010-05-06 00:08:46 +00002226 PyObject *opath;
2227 char *path;
2228 long uid, gid;
2229 int res;
2230 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2231 PyUnicode_FSConverter, &opath,
2232 &uid, &gid))
2233 return NULL;
2234 path = PyBytes_AsString(opath);
2235 Py_BEGIN_ALLOW_THREADS
2236 res = lchown(path, (uid_t) uid, (gid_t) gid);
2237 Py_END_ALLOW_THREADS
2238 if (res < 0)
2239 return posix_error_with_allocated_filename(opath);
2240 Py_DECREF(opath);
2241 Py_INCREF(Py_None);
2242 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002243}
2244#endif /* HAVE_LCHOWN */
2245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002246
Guido van Rossum36bc6801995-06-14 22:54:23 +00002247#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002248static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002249posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002250{
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 char buf[1026];
2252 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002253
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002254#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 if (!use_bytes) {
2256 wchar_t wbuf[1026];
2257 wchar_t *wbuf2 = wbuf;
2258 PyObject *resobj;
2259 DWORD len;
2260 Py_BEGIN_ALLOW_THREADS
2261 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2262 /* If the buffer is large enough, len does not include the
2263 terminating \0. If the buffer is too small, len includes
2264 the space needed for the terminator. */
2265 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2266 wbuf2 = malloc(len * sizeof(wchar_t));
2267 if (wbuf2)
2268 len = GetCurrentDirectoryW(len, wbuf2);
2269 }
2270 Py_END_ALLOW_THREADS
2271 if (!wbuf2) {
2272 PyErr_NoMemory();
2273 return NULL;
2274 }
2275 if (!len) {
2276 if (wbuf2 != wbuf) free(wbuf2);
2277 return win32_error("getcwdu", NULL);
2278 }
2279 resobj = PyUnicode_FromWideChar(wbuf2, len);
2280 if (wbuf2 != wbuf) free(wbuf2);
2281 return resobj;
2282 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002283#endif
2284
Victor Stinner8c62be82010-05-06 00:08:46 +00002285 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002286#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002287 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002289 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002291 Py_END_ALLOW_THREADS
2292 if (res == NULL)
2293 return posix_error();
2294 if (use_bytes)
2295 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002296 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002297}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002298
2299PyDoc_STRVAR(posix_getcwd__doc__,
2300"getcwd() -> path\n\n\
2301Return a unicode string representing the current working directory.");
2302
2303static PyObject *
2304posix_getcwd_unicode(PyObject *self)
2305{
2306 return posix_getcwd(0);
2307}
2308
2309PyDoc_STRVAR(posix_getcwdb__doc__,
2310"getcwdb() -> path\n\n\
2311Return a bytes string representing the current working directory.");
2312
2313static PyObject *
2314posix_getcwd_bytes(PyObject *self)
2315{
2316 return posix_getcwd(1);
2317}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002319
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002320
Guido van Rossumb6775db1994-08-01 11:34:53 +00002321#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002323"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002324Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002325
Barry Warsaw53699e91996-12-10 23:23:01 +00002326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002327posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002328{
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002330}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002331#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002332
Brian Curtin1b9df392010-11-24 20:24:31 +00002333#ifdef MS_WINDOWS
2334PyDoc_STRVAR(win32_link__doc__,
2335"link(src, dst)\n\n\
2336Create a hard link to a file.");
2337
2338static PyObject *
2339win32_link(PyObject *self, PyObject *args)
2340{
2341 PyObject *osrc, *odst;
2342 char *src, *dst;
2343 BOOL rslt;
2344
Brian Curtinfc889c42010-11-28 23:59:46 +00002345 PyUnicodeObject *usrc, *udst;
2346 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2347 Py_BEGIN_ALLOW_THREADS
2348 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2349 PyUnicode_AS_UNICODE(usrc), NULL);
2350 Py_END_ALLOW_THREADS
2351
2352 if (rslt == 0)
2353 return win32_error("link", NULL);
2354
2355 Py_RETURN_NONE;
2356 }
2357
2358 /* Narrow strings also valid. */
2359 PyErr_Clear();
2360
Brian Curtin1b9df392010-11-24 20:24:31 +00002361 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2362 PyUnicode_FSConverter, &odst))
2363 return NULL;
2364
2365 src = PyBytes_AsString(osrc);
2366 dst = PyBytes_AsString(odst);
2367
2368 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002369 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002370 Py_END_ALLOW_THREADS
2371
Stefan Krah30b341f2010-11-27 11:44:18 +00002372 Py_DECREF(osrc);
2373 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002374 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002375 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002376
2377 Py_RETURN_NONE;
2378}
2379#endif /* MS_WINDOWS */
2380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002382PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002383"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002384Return a list containing the names of the entries in the directory.\n\
2385\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002386 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002387\n\
2388The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002389entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002390
Barry Warsaw53699e91996-12-10 23:23:01 +00002391static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002392posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002393{
Victor Stinner8c62be82010-05-06 00:08:46 +00002394 /* XXX Should redo this putting the (now four) versions of opendir
2395 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002396#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002397
Victor Stinner8c62be82010-05-06 00:08:46 +00002398 PyObject *d, *v;
2399 HANDLE hFindFile;
2400 BOOL result;
2401 WIN32_FIND_DATA FileData;
2402 PyObject *opath;
2403 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2404 char *bufptr = namebuf;
2405 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002406
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002407 PyObject *po = NULL;
2408 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002409 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002410 Py_UNICODE *wnamebuf, *po_wchars;
Victor Stinner26de69d2011-06-17 15:15:38 +02002411
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002412 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002413 po_wchars = L".";
2414 len = 1;
2415 } else {
2416 po_wchars = PyUnicode_AS_UNICODE(po);
2417 len = PyUnicode_GET_SIZE(po);
2418 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002419 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002420 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2421 if (!wnamebuf) {
2422 PyErr_NoMemory();
2423 return NULL;
2424 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002425 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002426 if (len > 0) {
2427 Py_UNICODE wch = wnamebuf[len-1];
2428 if (wch != L'/' && wch != L'\\' && wch != L':')
2429 wnamebuf[len++] = L'\\';
2430 wcscpy(wnamebuf + len, L"*.*");
2431 }
2432 if ((d = PyList_New(0)) == NULL) {
2433 free(wnamebuf);
2434 return NULL;
2435 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002436 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002437 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002438 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002439 if (hFindFile == INVALID_HANDLE_VALUE) {
2440 int error = GetLastError();
2441 if (error == ERROR_FILE_NOT_FOUND) {
2442 free(wnamebuf);
2443 return d;
2444 }
2445 Py_DECREF(d);
2446 win32_error_unicode("FindFirstFileW", wnamebuf);
2447 free(wnamebuf);
2448 return NULL;
2449 }
2450 do {
2451 /* Skip over . and .. */
2452 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2453 wcscmp(wFileData.cFileName, L"..") != 0) {
2454 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2455 if (v == NULL) {
2456 Py_DECREF(d);
2457 d = NULL;
2458 break;
2459 }
2460 if (PyList_Append(d, v) != 0) {
2461 Py_DECREF(v);
2462 Py_DECREF(d);
2463 d = NULL;
2464 break;
2465 }
2466 Py_DECREF(v);
2467 }
2468 Py_BEGIN_ALLOW_THREADS
2469 result = FindNextFileW(hFindFile, &wFileData);
2470 Py_END_ALLOW_THREADS
2471 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2472 it got to the end of the directory. */
2473 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2474 Py_DECREF(d);
2475 win32_error_unicode("FindNextFileW", wnamebuf);
2476 FindClose(hFindFile);
2477 free(wnamebuf);
2478 return NULL;
2479 }
2480 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002481
Victor Stinner8c62be82010-05-06 00:08:46 +00002482 if (FindClose(hFindFile) == FALSE) {
2483 Py_DECREF(d);
2484 win32_error_unicode("FindClose", wnamebuf);
2485 free(wnamebuf);
2486 return NULL;
2487 }
2488 free(wnamebuf);
2489 return d;
2490 }
2491 /* Drop the argument parsing error as narrow strings
2492 are also valid. */
2493 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002494
Victor Stinner8c62be82010-05-06 00:08:46 +00002495 if (!PyArg_ParseTuple(args, "O&:listdir",
2496 PyUnicode_FSConverter, &opath))
2497 return NULL;
2498 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2499 PyErr_SetString(PyExc_ValueError, "path too long");
2500 Py_DECREF(opath);
2501 return NULL;
2502 }
2503 strcpy(namebuf, PyBytes_AsString(opath));
2504 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002505 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002506 if (len > 0) {
2507 char ch = namebuf[len-1];
2508 if (ch != SEP && ch != ALTSEP && ch != ':')
2509 namebuf[len++] = '/';
2510 strcpy(namebuf + len, "*.*");
2511 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002512
Victor Stinner8c62be82010-05-06 00:08:46 +00002513 if ((d = PyList_New(0)) == NULL)
2514 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002515
Antoine Pitroub73caab2010-08-09 23:39:31 +00002516 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002517 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002518 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 if (hFindFile == INVALID_HANDLE_VALUE) {
2520 int error = GetLastError();
2521 if (error == ERROR_FILE_NOT_FOUND)
2522 return d;
2523 Py_DECREF(d);
2524 return win32_error("FindFirstFile", namebuf);
2525 }
2526 do {
2527 /* Skip over . and .. */
2528 if (strcmp(FileData.cFileName, ".") != 0 &&
2529 strcmp(FileData.cFileName, "..") != 0) {
2530 v = PyBytes_FromString(FileData.cFileName);
2531 if (v == NULL) {
2532 Py_DECREF(d);
2533 d = NULL;
2534 break;
2535 }
2536 if (PyList_Append(d, v) != 0) {
2537 Py_DECREF(v);
2538 Py_DECREF(d);
2539 d = NULL;
2540 break;
2541 }
2542 Py_DECREF(v);
2543 }
2544 Py_BEGIN_ALLOW_THREADS
2545 result = FindNextFile(hFindFile, &FileData);
2546 Py_END_ALLOW_THREADS
2547 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2548 it got to the end of the directory. */
2549 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2550 Py_DECREF(d);
2551 win32_error("FindNextFile", namebuf);
2552 FindClose(hFindFile);
2553 return NULL;
2554 }
2555 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002556
Victor Stinner8c62be82010-05-06 00:08:46 +00002557 if (FindClose(hFindFile) == FALSE) {
2558 Py_DECREF(d);
2559 return win32_error("FindClose", namebuf);
2560 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561
Victor Stinner8c62be82010-05-06 00:08:46 +00002562 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002563
Tim Peters0bb44a42000-09-15 07:44:49 +00002564#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002565
2566#ifndef MAX_PATH
2567#define MAX_PATH CCHMAXPATH
2568#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002569 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002570 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002571 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002572 PyObject *d, *v;
2573 char namebuf[MAX_PATH+5];
2574 HDIR hdir = 1;
2575 ULONG srchcnt = 1;
2576 FILEFINDBUF3 ep;
2577 APIRET rc;
2578
Victor Stinner8c62be82010-05-06 00:08:46 +00002579 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002580 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002581 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002582 name = PyBytes_AsString(oname);
2583 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002584 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002585 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002586 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002587 return NULL;
2588 }
2589 strcpy(namebuf, name);
2590 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002591 if (*pt == ALTSEP)
2592 *pt = SEP;
2593 if (namebuf[len-1] != SEP)
2594 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002595 strcpy(namebuf + len, "*.*");
2596
Neal Norwitz6c913782007-10-14 03:23:09 +00002597 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002598 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002599 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002600 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002601
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002602 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2603 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002604 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002605 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2606 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2607 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002608
2609 if (rc != NO_ERROR) {
2610 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002611 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002612 }
2613
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002614 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002615 do {
2616 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002617 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002618 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002619
2620 strcpy(namebuf, ep.achName);
2621
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002622 /* Leave Case of Name Alone -- In Native Form */
2623 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002624
Christian Heimes72b710a2008-05-26 13:28:38 +00002625 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002626 if (v == NULL) {
2627 Py_DECREF(d);
2628 d = NULL;
2629 break;
2630 }
2631 if (PyList_Append(d, v) != 0) {
2632 Py_DECREF(v);
2633 Py_DECREF(d);
2634 d = NULL;
2635 break;
2636 }
2637 Py_DECREF(v);
2638 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2639 }
2640
Victor Stinnerdcb24032010-04-22 12:08:36 +00002641 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642 return d;
2643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 PyObject *oname;
2645 char *name;
2646 PyObject *d, *v;
2647 DIR *dirp;
2648 struct dirent *ep;
2649 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002650
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002652 /* v is never read, so it does not need to be initialized yet. */
2653 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002654 arg_is_unicode = 0;
2655 PyErr_Clear();
2656 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002657 oname = NULL;
2658 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002660 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002661 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002662 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002664 Py_BEGIN_ALLOW_THREADS
2665 dirp = opendir(name);
2666 Py_END_ALLOW_THREADS
2667 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002668 return posix_error_with_allocated_filename(oname);
2669 }
2670 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002671 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002672 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002673 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002674 Py_DECREF(oname);
2675 return NULL;
2676 }
2677 for (;;) {
2678 errno = 0;
2679 Py_BEGIN_ALLOW_THREADS
2680 ep = readdir(dirp);
2681 Py_END_ALLOW_THREADS
2682 if (ep == NULL) {
2683 if (errno == 0) {
2684 break;
2685 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002686 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002687 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002688 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 Py_DECREF(d);
2690 return posix_error_with_allocated_filename(oname);
2691 }
2692 }
2693 if (ep->d_name[0] == '.' &&
2694 (NAMLEN(ep) == 1 ||
2695 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2696 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002697 if (arg_is_unicode)
2698 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2699 else
2700 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002701 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002702 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002703 break;
2704 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002705 if (PyList_Append(d, v) != 0) {
2706 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002707 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002708 break;
2709 }
2710 Py_DECREF(v);
2711 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002712 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002713 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002714 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002715 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002716
Victor Stinner8c62be82010-05-06 00:08:46 +00002717 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002718
Tim Peters0bb44a42000-09-15 07:44:49 +00002719#endif /* which OS */
2720} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002721
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002722#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002723/* A helper function for abspath on win32 */
2724static PyObject *
2725posix__getfullpathname(PyObject *self, PyObject *args)
2726{
Victor Stinner8c62be82010-05-06 00:08:46 +00002727 PyObject *opath;
2728 char *path;
2729 char outbuf[MAX_PATH*2];
2730 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002731#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 PyUnicodeObject *po;
2733 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2734 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2735 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2736 Py_UNICODE *wtemp;
2737 DWORD result;
2738 PyObject *v;
2739 result = GetFullPathNameW(wpath,
2740 sizeof(woutbuf)/sizeof(woutbuf[0]),
2741 woutbuf, &wtemp);
2742 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2743 woutbufp = malloc(result * sizeof(Py_UNICODE));
2744 if (!woutbufp)
2745 return PyErr_NoMemory();
2746 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2747 }
2748 if (result)
2749 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2750 else
2751 v = win32_error_unicode("GetFullPathNameW", wpath);
2752 if (woutbufp != woutbuf)
2753 free(woutbufp);
2754 return v;
2755 }
2756 /* Drop the argument parsing error as narrow strings
2757 are also valid. */
2758 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002759
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002760#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002761 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2762 PyUnicode_FSConverter, &opath))
2763 return NULL;
2764 path = PyBytes_AsString(opath);
2765 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2766 outbuf, &temp)) {
2767 win32_error("GetFullPathName", path);
2768 Py_DECREF(opath);
2769 return NULL;
2770 }
2771 Py_DECREF(opath);
2772 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2773 return PyUnicode_Decode(outbuf, strlen(outbuf),
2774 Py_FileSystemDefaultEncoding, NULL);
2775 }
2776 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002777} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002778
Brian Curtind25aef52011-06-13 15:16:04 -05002779
Brian Curtinf5e76d02010-11-24 13:14:05 +00002780
Brian Curtind40e6f72010-07-08 21:39:08 +00002781/* A helper function for samepath on windows */
2782static PyObject *
2783posix__getfinalpathname(PyObject *self, PyObject *args)
2784{
2785 HANDLE hFile;
2786 int buf_size;
2787 wchar_t *target_path;
2788 int result_length;
2789 PyObject *result;
2790 wchar_t *path;
Victor Stinner26de69d2011-06-17 15:15:38 +02002791
Brian Curtin94622b02010-09-24 00:03:39 +00002792 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002793 return NULL;
2794 }
2795
2796 if(!check_GetFinalPathNameByHandle()) {
2797 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2798 NotImplementedError. */
2799 return PyErr_Format(PyExc_NotImplementedError,
2800 "GetFinalPathNameByHandle not available on this platform");
2801 }
2802
2803 hFile = CreateFileW(
2804 path,
2805 0, /* desired access */
2806 0, /* share mode */
2807 NULL, /* security attributes */
2808 OPEN_EXISTING,
2809 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2810 FILE_FLAG_BACKUP_SEMANTICS,
2811 NULL);
Victor Stinner26de69d2011-06-17 15:15:38 +02002812
Brian Curtind40e6f72010-07-08 21:39:08 +00002813 if(hFile == INVALID_HANDLE_VALUE) {
2814 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002815 return PyErr_Format(PyExc_RuntimeError,
2816 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002817 }
2818
2819 /* We have a good handle to the target, use it to determine the
2820 target path name. */
2821 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2822
2823 if(!buf_size)
2824 return win32_error_unicode("GetFinalPathNameByHandle", path);
2825
2826 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2827 if(!target_path)
2828 return PyErr_NoMemory();
2829
2830 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2831 buf_size, VOLUME_NAME_DOS);
2832 if(!result_length)
2833 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2834
2835 if(!CloseHandle(hFile))
2836 return win32_error_unicode("GetFinalPathNameByHandle", path);
2837
2838 target_path[result_length] = 0;
2839 result = PyUnicode_FromUnicode(target_path, result_length);
2840 free(target_path);
2841 return result;
2842
2843} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002844
2845static PyObject *
2846posix__getfileinformation(PyObject *self, PyObject *args)
2847{
2848 HANDLE hFile;
2849 BY_HANDLE_FILE_INFORMATION info;
2850 int fd;
2851
2852 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2853 return NULL;
2854
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002855 if (!_PyVerify_fd(fd))
2856 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002857
2858 hFile = (HANDLE)_get_osfhandle(fd);
2859 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002860 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002861
2862 if (!GetFileInformationByHandle(hFile, &info))
2863 return win32_error("_getfileinformation", NULL);
2864
2865 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2866 info.nFileIndexHigh,
2867 info.nFileIndexLow);
2868}
Brian Curtin9c669cc2011-06-08 18:17:18 -05002869
Brian Curtin95d028f2011-06-09 09:10:38 -05002870PyDoc_STRVAR(posix__isdir__doc__,
2871"Return true if the pathname refers to an existing directory.");
2872
Brian Curtin9c669cc2011-06-08 18:17:18 -05002873static PyObject *
2874posix__isdir(PyObject *self, PyObject *args)
2875{
2876 PyObject *opath;
2877 char *path;
2878 PyUnicodeObject *po;
2879 DWORD attributes;
2880
2881 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
2882 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2883
2884 attributes = GetFileAttributesW(wpath);
2885 if (attributes == INVALID_FILE_ATTRIBUTES)
2886 Py_RETURN_FALSE;
2887 goto check;
2888 }
2889 /* Drop the argument parsing error as narrow strings
2890 are also valid. */
2891 PyErr_Clear();
2892
2893 if (!PyArg_ParseTuple(args, "O&:_isdir",
2894 PyUnicode_FSConverter, &opath))
2895 return NULL;
2896
2897 path = PyBytes_AsString(opath);
2898 attributes = GetFileAttributesA(path);
2899 if (attributes == INVALID_FILE_ATTRIBUTES)
2900 Py_RETURN_FALSE;
2901
2902check:
2903 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
2904 Py_RETURN_TRUE;
2905 else
2906 Py_RETURN_FALSE;
2907}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002908#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002910PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002911"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002912Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002913
Barry Warsaw53699e91996-12-10 23:23:01 +00002914static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002915posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002916{
Victor Stinner8c62be82010-05-06 00:08:46 +00002917 int res;
2918 PyObject *opath;
2919 char *path;
2920 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002921
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002922#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002923 PyUnicodeObject *po;
2924 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2925 Py_BEGIN_ALLOW_THREADS
2926 /* PyUnicode_AS_UNICODE OK without thread lock as
2927 it is a simple dereference. */
2928 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2929 Py_END_ALLOW_THREADS
2930 if (!res)
2931 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2932 Py_INCREF(Py_None);
2933 return Py_None;
2934 }
2935 /* Drop the argument parsing error as narrow strings
2936 are also valid. */
2937 PyErr_Clear();
2938 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2939 PyUnicode_FSConverter, &opath, &mode))
2940 return NULL;
2941 path = PyBytes_AsString(opath);
2942 Py_BEGIN_ALLOW_THREADS
2943 /* PyUnicode_AS_UNICODE OK without thread lock as
2944 it is a simple dereference. */
2945 res = CreateDirectoryA(path, NULL);
2946 Py_END_ALLOW_THREADS
2947 if (!res) {
2948 win32_error("mkdir", path);
2949 Py_DECREF(opath);
2950 return NULL;
2951 }
2952 Py_DECREF(opath);
2953 Py_INCREF(Py_None);
2954 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002955#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002956
Victor Stinner8c62be82010-05-06 00:08:46 +00002957 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2958 PyUnicode_FSConverter, &opath, &mode))
2959 return NULL;
2960 path = PyBytes_AsString(opath);
2961 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002962#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002963 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002964#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002965 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002966#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002967 Py_END_ALLOW_THREADS
2968 if (res < 0)
2969 return posix_error_with_allocated_filename(opath);
2970 Py_DECREF(opath);
2971 Py_INCREF(Py_None);
2972 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002973#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002974}
2975
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002976
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002977/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2978#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002979#include <sys/resource.h>
2980#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002981
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002982
2983#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002985"nice(inc) -> new_priority\n\n\
2986Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002987
Barry Warsaw53699e91996-12-10 23:23:01 +00002988static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002989posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002990{
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002992
Victor Stinner8c62be82010-05-06 00:08:46 +00002993 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2994 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002995
Victor Stinner8c62be82010-05-06 00:08:46 +00002996 /* There are two flavours of 'nice': one that returns the new
2997 priority (as required by almost all standards out there) and the
2998 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2999 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003000
Victor Stinner8c62be82010-05-06 00:08:46 +00003001 If we are of the nice family that returns the new priority, we
3002 need to clear errno before the call, and check if errno is filled
3003 before calling posix_error() on a returnvalue of -1, because the
3004 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003005
Victor Stinner8c62be82010-05-06 00:08:46 +00003006 errno = 0;
3007 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003008#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003009 if (value == 0)
3010 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003011#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003012 if (value == -1 && errno != 0)
3013 /* either nice() or getpriority() returned an error */
3014 return posix_error();
3015 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003016}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003017#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003019PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003020"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003021Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003022
Barry Warsaw53699e91996-12-10 23:23:01 +00003023static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003024posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003025{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003026#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003027 PyObject *o1, *o2;
3028 char *p1, *p2;
3029 BOOL result;
3030 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3031 goto error;
3032 if (!convert_to_unicode(&o1))
3033 goto error;
3034 if (!convert_to_unicode(&o2)) {
3035 Py_DECREF(o1);
3036 goto error;
3037 }
3038 Py_BEGIN_ALLOW_THREADS
3039 result = MoveFileW(PyUnicode_AsUnicode(o1),
3040 PyUnicode_AsUnicode(o2));
3041 Py_END_ALLOW_THREADS
3042 Py_DECREF(o1);
3043 Py_DECREF(o2);
3044 if (!result)
3045 return win32_error("rename", NULL);
3046 Py_INCREF(Py_None);
3047 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003048error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 PyErr_Clear();
3050 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3051 return NULL;
3052 Py_BEGIN_ALLOW_THREADS
3053 result = MoveFileA(p1, p2);
3054 Py_END_ALLOW_THREADS
3055 if (!result)
3056 return win32_error("rename", NULL);
3057 Py_INCREF(Py_None);
3058 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003059#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003060 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003061#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003062}
3063
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003065PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003066"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003067Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003068
Barry Warsaw53699e91996-12-10 23:23:01 +00003069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003070posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003071{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003072#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003073 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003074#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003075 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003076#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003077}
3078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003080PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003081"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003082Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003083
Barry Warsaw53699e91996-12-10 23:23:01 +00003084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003085posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003086{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003087#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003088 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003089#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003090 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003091#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003092}
3093
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003094
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003095#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003097"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Barry Warsaw53699e91996-12-10 23:23:01 +00003100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003101posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003102{
Victor Stinner8c62be82010-05-06 00:08:46 +00003103 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003104#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003105 wchar_t *command;
3106 if (!PyArg_ParseTuple(args, "u:system", &command))
3107 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003108
Victor Stinner8c62be82010-05-06 00:08:46 +00003109 Py_BEGIN_ALLOW_THREADS
3110 sts = _wsystem(command);
3111 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003112#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003113 PyObject *command_obj;
3114 char *command;
3115 if (!PyArg_ParseTuple(args, "O&:system",
3116 PyUnicode_FSConverter, &command_obj))
3117 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003118
Victor Stinner8c62be82010-05-06 00:08:46 +00003119 command = PyBytes_AsString(command_obj);
3120 Py_BEGIN_ALLOW_THREADS
3121 sts = system(command);
3122 Py_END_ALLOW_THREADS
3123 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003124#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003125 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003126}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003127#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003128
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003129
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003130PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003131"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003132Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003133
Barry Warsaw53699e91996-12-10 23:23:01 +00003134static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003135posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003136{
Victor Stinner8c62be82010-05-06 00:08:46 +00003137 int i;
3138 if (!PyArg_ParseTuple(args, "i:umask", &i))
3139 return NULL;
3140 i = (int)umask(i);
3141 if (i < 0)
3142 return posix_error();
3143 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003144}
3145
Brian Curtind40e6f72010-07-08 21:39:08 +00003146#ifdef MS_WINDOWS
3147
3148/* override the default DeleteFileW behavior so that directory
3149symlinks can be removed with this function, the same as with
3150Unix symlinks */
3151BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3152{
3153 WIN32_FILE_ATTRIBUTE_DATA info;
3154 WIN32_FIND_DATAW find_data;
3155 HANDLE find_data_handle;
3156 int is_directory = 0;
3157 int is_link = 0;
3158
3159 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3160 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Victor Stinner26de69d2011-06-17 15:15:38 +02003161
Brian Curtind40e6f72010-07-08 21:39:08 +00003162 /* Get WIN32_FIND_DATA structure for the path to determine if
3163 it is a symlink */
3164 if(is_directory &&
3165 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3166 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3167
3168 if(find_data_handle != INVALID_HANDLE_VALUE) {
3169 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3170 FindClose(find_data_handle);
3171 }
3172 }
3173 }
3174
3175 if (is_directory && is_link)
3176 return RemoveDirectoryW(lpFileName);
3177
3178 return DeleteFileW(lpFileName);
3179}
3180#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003182PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003183"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003184Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003186PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003187"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003188Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003189
Barry Warsaw53699e91996-12-10 23:23:01 +00003190static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003191posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003192{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003193#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003194 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3195 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003196#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003197 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003198#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003199}
3200
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003201
Guido van Rossumb6775db1994-08-01 11:34:53 +00003202#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003203PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003204"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003205Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003206
Barry Warsaw53699e91996-12-10 23:23:01 +00003207static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003208posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003209{
Victor Stinner8c62be82010-05-06 00:08:46 +00003210 struct utsname u;
3211 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003212
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 Py_BEGIN_ALLOW_THREADS
3214 res = uname(&u);
3215 Py_END_ALLOW_THREADS
3216 if (res < 0)
3217 return posix_error();
3218 return Py_BuildValue("(sssss)",
3219 u.sysname,
3220 u.nodename,
3221 u.release,
3222 u.version,
3223 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003224}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003225#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003226
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003227static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003228extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003229{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003230 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003231 if (PyFloat_Check(t)) {
3232 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003233 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003234 if (!intobj)
3235 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003236#if SIZEOF_TIME_T > SIZEOF_LONG
3237 intval = PyLong_AsUnsignedLongLongMask(intobj);
3238#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003239 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003240#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003241 Py_DECREF(intobj);
3242 if (intval == -1 && PyErr_Occurred())
3243 return -1;
3244 *sec = intval;
3245 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3246 if (*usec < 0)
3247 /* If rounding gave us a negative number,
3248 truncate. */
3249 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003250 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003251 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003252#if SIZEOF_TIME_T > SIZEOF_LONG
3253 intval = PyLong_AsUnsignedLongLongMask(t);
3254#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003256#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003257 if (intval == -1 && PyErr_Occurred())
3258 return -1;
3259 *sec = intval;
3260 *usec = 0;
3261 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003262}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003264PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003265"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003266utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003267Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003268second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003269
Barry Warsaw53699e91996-12-10 23:23:01 +00003270static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003271posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003272{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003273#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003274 PyObject *arg;
3275 PyUnicodeObject *obwpath;
3276 wchar_t *wpath = NULL;
3277 PyObject *oapath;
3278 char *apath;
3279 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003280 time_t atimesec, mtimesec;
3281 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003282 FILETIME atime, mtime;
3283 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003284
Victor Stinner8c62be82010-05-06 00:08:46 +00003285 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3286 wpath = PyUnicode_AS_UNICODE(obwpath);
3287 Py_BEGIN_ALLOW_THREADS
3288 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3289 NULL, OPEN_EXISTING,
3290 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3291 Py_END_ALLOW_THREADS
3292 if (hFile == INVALID_HANDLE_VALUE)
3293 return win32_error_unicode("utime", wpath);
3294 } else
3295 /* Drop the argument parsing error as narrow strings
3296 are also valid. */
3297 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003298
Victor Stinner8c62be82010-05-06 00:08:46 +00003299 if (!wpath) {
3300 if (!PyArg_ParseTuple(args, "O&O:utime",
3301 PyUnicode_FSConverter, &oapath, &arg))
3302 return NULL;
3303 apath = PyBytes_AsString(oapath);
3304 Py_BEGIN_ALLOW_THREADS
3305 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3306 NULL, OPEN_EXISTING,
3307 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3308 Py_END_ALLOW_THREADS
3309 if (hFile == INVALID_HANDLE_VALUE) {
3310 win32_error("utime", apath);
3311 Py_DECREF(oapath);
3312 return NULL;
3313 }
3314 Py_DECREF(oapath);
3315 }
3316
3317 if (arg == Py_None) {
3318 SYSTEMTIME now;
3319 GetSystemTime(&now);
3320 if (!SystemTimeToFileTime(&now, &mtime) ||
3321 !SystemTimeToFileTime(&now, &atime)) {
3322 win32_error("utime", NULL);
3323 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003324 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003325 }
3326 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3327 PyErr_SetString(PyExc_TypeError,
3328 "utime() arg 2 must be a tuple (atime, mtime)");
3329 goto done;
3330 }
3331 else {
3332 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3333 &atimesec, &ausec) == -1)
3334 goto done;
3335 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3336 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3337 &mtimesec, &musec) == -1)
3338 goto done;
3339 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3340 }
3341 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3342 /* Avoid putting the file name into the error here,
3343 as that may confuse the user into believing that
3344 something is wrong with the file, when it also
3345 could be the time stamp that gives a problem. */
3346 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003347 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003348 }
3349 Py_INCREF(Py_None);
3350 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003351done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003352 CloseHandle(hFile);
3353 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003354#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003355
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 PyObject *opath;
3357 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003358 time_t atime, mtime;
3359 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003360 int res;
3361 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003362
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003363#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003365#define ATIME buf[0].tv_sec
3366#define MTIME buf[1].tv_sec
3367#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003368/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003369 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003370#define ATIME buf.actime
3371#define MTIME buf.modtime
3372#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003373#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003374 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003375#define ATIME buf[0]
3376#define MTIME buf[1]
3377#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003378#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003379
Mark Hammond817c9292003-12-03 01:22:38 +00003380
Victor Stinner8c62be82010-05-06 00:08:46 +00003381 if (!PyArg_ParseTuple(args, "O&O:utime",
3382 PyUnicode_FSConverter, &opath, &arg))
3383 return NULL;
3384 path = PyBytes_AsString(opath);
3385 if (arg == Py_None) {
3386 /* optional time values not given */
3387 Py_BEGIN_ALLOW_THREADS
3388 res = utime(path, NULL);
3389 Py_END_ALLOW_THREADS
3390 }
3391 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3392 PyErr_SetString(PyExc_TypeError,
3393 "utime() arg 2 must be a tuple (atime, mtime)");
3394 Py_DECREF(opath);
3395 return NULL;
3396 }
3397 else {
3398 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3399 &atime, &ausec) == -1) {
3400 Py_DECREF(opath);
3401 return NULL;
3402 }
3403 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3404 &mtime, &musec) == -1) {
3405 Py_DECREF(opath);
3406 return NULL;
3407 }
3408 ATIME = atime;
3409 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003410#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003411 buf[0].tv_usec = ausec;
3412 buf[1].tv_usec = musec;
3413 Py_BEGIN_ALLOW_THREADS
3414 res = utimes(path, buf);
3415 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003416#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003417 Py_BEGIN_ALLOW_THREADS
3418 res = utime(path, UTIME_ARG);
3419 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003420#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003421 }
3422 if (res < 0) {
3423 return posix_error_with_allocated_filename(opath);
3424 }
3425 Py_DECREF(opath);
3426 Py_INCREF(Py_None);
3427 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003428#undef UTIME_ARG
3429#undef ATIME
3430#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003431#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003432}
3433
Guido van Rossum85e3b011991-06-03 12:42:10 +00003434
Guido van Rossum3b066191991-06-04 19:40:25 +00003435/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003437PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003438"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003439Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003440
Barry Warsaw53699e91996-12-10 23:23:01 +00003441static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003442posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003443{
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 int sts;
3445 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3446 return NULL;
3447 _exit(sts);
3448 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003449}
3450
Martin v. Löwis114619e2002-10-07 06:44:21 +00003451#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3452static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003453free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003454{
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 Py_ssize_t i;
3456 for (i = 0; i < count; i++)
3457 PyMem_Free(array[i]);
3458 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003459}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003460
Antoine Pitrou69f71142009-05-24 21:25:49 +00003461static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003462int fsconvert_strdup(PyObject *o, char**out)
3463{
Victor Stinner8c62be82010-05-06 00:08:46 +00003464 PyObject *bytes;
3465 Py_ssize_t size;
3466 if (!PyUnicode_FSConverter(o, &bytes))
3467 return 0;
3468 size = PyBytes_GET_SIZE(bytes);
3469 *out = PyMem_Malloc(size+1);
3470 if (!*out)
3471 return 0;
3472 memcpy(*out, PyBytes_AsString(bytes), size+1);
3473 Py_DECREF(bytes);
3474 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003475}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003476#endif
3477
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003478
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003479#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003480PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003481"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003482Execute an executable path with arguments, replacing current process.\n\
3483\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003484 path: path of executable file\n\
3485 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003486
Barry Warsaw53699e91996-12-10 23:23:01 +00003487static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003488posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003489{
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 PyObject *opath;
3491 char *path;
3492 PyObject *argv;
3493 char **argvlist;
3494 Py_ssize_t i, argc;
3495 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003496
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 /* execv has two arguments: (path, argv), where
3498 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003499
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 if (!PyArg_ParseTuple(args, "O&O:execv",
3501 PyUnicode_FSConverter,
3502 &opath, &argv))
3503 return NULL;
3504 path = PyBytes_AsString(opath);
3505 if (PyList_Check(argv)) {
3506 argc = PyList_Size(argv);
3507 getitem = PyList_GetItem;
3508 }
3509 else if (PyTuple_Check(argv)) {
3510 argc = PyTuple_Size(argv);
3511 getitem = PyTuple_GetItem;
3512 }
3513 else {
3514 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3515 Py_DECREF(opath);
3516 return NULL;
3517 }
3518 if (argc < 1) {
3519 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3520 Py_DECREF(opath);
3521 return NULL;
3522 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003523
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 argvlist = PyMem_NEW(char *, argc+1);
3525 if (argvlist == NULL) {
3526 Py_DECREF(opath);
3527 return PyErr_NoMemory();
3528 }
3529 for (i = 0; i < argc; i++) {
3530 if (!fsconvert_strdup((*getitem)(argv, i),
3531 &argvlist[i])) {
3532 free_string_array(argvlist, i);
3533 PyErr_SetString(PyExc_TypeError,
3534 "execv() arg 2 must contain only strings");
3535 Py_DECREF(opath);
3536 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003537
Victor Stinner8c62be82010-05-06 00:08:46 +00003538 }
3539 }
3540 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003541
Victor Stinner8c62be82010-05-06 00:08:46 +00003542 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003543
Victor Stinner8c62be82010-05-06 00:08:46 +00003544 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003545
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 free_string_array(argvlist, argc);
3547 Py_DECREF(opath);
3548 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003549}
3550
Victor Stinner13bb71c2010-04-23 21:41:56 +00003551static char**
3552parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3553{
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 char **envlist;
3555 Py_ssize_t i, pos, envc;
3556 PyObject *keys=NULL, *vals=NULL;
3557 PyObject *key, *val, *key2, *val2;
3558 char *p, *k, *v;
3559 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003560
Victor Stinner8c62be82010-05-06 00:08:46 +00003561 i = PyMapping_Size(env);
3562 if (i < 0)
3563 return NULL;
3564 envlist = PyMem_NEW(char *, i + 1);
3565 if (envlist == NULL) {
3566 PyErr_NoMemory();
3567 return NULL;
3568 }
3569 envc = 0;
3570 keys = PyMapping_Keys(env);
3571 vals = PyMapping_Values(env);
3572 if (!keys || !vals)
3573 goto error;
3574 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3575 PyErr_Format(PyExc_TypeError,
3576 "env.keys() or env.values() is not a list");
3577 goto error;
3578 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003579
Victor Stinner8c62be82010-05-06 00:08:46 +00003580 for (pos = 0; pos < i; pos++) {
3581 key = PyList_GetItem(keys, pos);
3582 val = PyList_GetItem(vals, pos);
3583 if (!key || !val)
3584 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003585
Victor Stinner8c62be82010-05-06 00:08:46 +00003586 if (PyUnicode_FSConverter(key, &key2) == 0)
3587 goto error;
3588 if (PyUnicode_FSConverter(val, &val2) == 0) {
3589 Py_DECREF(key2);
3590 goto error;
3591 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003592
3593#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003594 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3595 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003596#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 k = PyBytes_AsString(key2);
3598 v = PyBytes_AsString(val2);
3599 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003600
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 p = PyMem_NEW(char, len);
3602 if (p == NULL) {
3603 PyErr_NoMemory();
3604 Py_DECREF(key2);
3605 Py_DECREF(val2);
3606 goto error;
3607 }
3608 PyOS_snprintf(p, len, "%s=%s", k, v);
3609 envlist[envc++] = p;
3610 Py_DECREF(key2);
3611 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003612#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003614#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003615 }
3616 Py_DECREF(vals);
3617 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003618
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 envlist[envc] = 0;
3620 *envc_ptr = envc;
3621 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003622
3623error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 Py_XDECREF(keys);
3625 Py_XDECREF(vals);
3626 while (--envc >= 0)
3627 PyMem_DEL(envlist[envc]);
3628 PyMem_DEL(envlist);
3629 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003630}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003632PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003633"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003634Execute a path with arguments and environment, replacing current process.\n\
3635\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003636 path: path of executable file\n\
3637 args: tuple or list of arguments\n\
3638 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003639
Barry Warsaw53699e91996-12-10 23:23:01 +00003640static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003641posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003642{
Victor Stinner8c62be82010-05-06 00:08:46 +00003643 PyObject *opath;
3644 char *path;
3645 PyObject *argv, *env;
3646 char **argvlist;
3647 char **envlist;
3648 Py_ssize_t i, argc, envc;
3649 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3650 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003651
Victor Stinner8c62be82010-05-06 00:08:46 +00003652 /* execve has three arguments: (path, argv, env), where
3653 argv is a list or tuple of strings and env is a dictionary
3654 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003655
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 if (!PyArg_ParseTuple(args, "O&OO:execve",
3657 PyUnicode_FSConverter,
3658 &opath, &argv, &env))
3659 return NULL;
3660 path = PyBytes_AsString(opath);
3661 if (PyList_Check(argv)) {
3662 argc = PyList_Size(argv);
3663 getitem = PyList_GetItem;
3664 }
3665 else if (PyTuple_Check(argv)) {
3666 argc = PyTuple_Size(argv);
3667 getitem = PyTuple_GetItem;
3668 }
3669 else {
3670 PyErr_SetString(PyExc_TypeError,
3671 "execve() arg 2 must be a tuple or list");
3672 goto fail_0;
3673 }
3674 if (!PyMapping_Check(env)) {
3675 PyErr_SetString(PyExc_TypeError,
3676 "execve() arg 3 must be a mapping object");
3677 goto fail_0;
3678 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003679
Victor Stinner8c62be82010-05-06 00:08:46 +00003680 argvlist = PyMem_NEW(char *, argc+1);
3681 if (argvlist == NULL) {
3682 PyErr_NoMemory();
3683 goto fail_0;
3684 }
3685 for (i = 0; i < argc; i++) {
3686 if (!fsconvert_strdup((*getitem)(argv, i),
3687 &argvlist[i]))
3688 {
3689 lastarg = i;
3690 goto fail_1;
3691 }
3692 }
3693 lastarg = argc;
3694 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003695
Victor Stinner8c62be82010-05-06 00:08:46 +00003696 envlist = parse_envlist(env, &envc);
3697 if (envlist == NULL)
3698 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003699
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003701
Victor Stinner8c62be82010-05-06 00:08:46 +00003702 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003703
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003705
Victor Stinner8c62be82010-05-06 00:08:46 +00003706 while (--envc >= 0)
3707 PyMem_DEL(envlist[envc]);
3708 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003709 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003711 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 Py_DECREF(opath);
3713 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003714}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003715#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003717
Guido van Rossuma1065681999-01-25 23:20:23 +00003718#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003719PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003720"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003721Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003722\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003723 mode: mode of process creation\n\
3724 path: path of executable file\n\
3725 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003726
3727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003728posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003729{
Victor Stinner8c62be82010-05-06 00:08:46 +00003730 PyObject *opath;
3731 char *path;
3732 PyObject *argv;
3733 char **argvlist;
3734 int mode, i;
3735 Py_ssize_t argc;
3736 Py_intptr_t spawnval;
3737 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003738
Victor Stinner8c62be82010-05-06 00:08:46 +00003739 /* spawnv has three arguments: (mode, path, argv), where
3740 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003741
Victor Stinner8c62be82010-05-06 00:08:46 +00003742 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3743 PyUnicode_FSConverter,
3744 &opath, &argv))
3745 return NULL;
3746 path = PyBytes_AsString(opath);
3747 if (PyList_Check(argv)) {
3748 argc = PyList_Size(argv);
3749 getitem = PyList_GetItem;
3750 }
3751 else if (PyTuple_Check(argv)) {
3752 argc = PyTuple_Size(argv);
3753 getitem = PyTuple_GetItem;
3754 }
3755 else {
3756 PyErr_SetString(PyExc_TypeError,
3757 "spawnv() arg 2 must be a tuple or list");
3758 Py_DECREF(opath);
3759 return NULL;
3760 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003761
Victor Stinner8c62be82010-05-06 00:08:46 +00003762 argvlist = PyMem_NEW(char *, argc+1);
3763 if (argvlist == NULL) {
3764 Py_DECREF(opath);
3765 return PyErr_NoMemory();
3766 }
3767 for (i = 0; i < argc; i++) {
3768 if (!fsconvert_strdup((*getitem)(argv, i),
3769 &argvlist[i])) {
3770 free_string_array(argvlist, i);
3771 PyErr_SetString(
3772 PyExc_TypeError,
3773 "spawnv() arg 2 must contain only strings");
3774 Py_DECREF(opath);
3775 return NULL;
3776 }
3777 }
3778 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003779
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003780#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003781 Py_BEGIN_ALLOW_THREADS
3782 spawnval = spawnv(mode, path, argvlist);
3783 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003784#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003785 if (mode == _OLD_P_OVERLAY)
3786 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003787
Victor Stinner8c62be82010-05-06 00:08:46 +00003788 Py_BEGIN_ALLOW_THREADS
3789 spawnval = _spawnv(mode, path, argvlist);
3790 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003791#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003792
Victor Stinner8c62be82010-05-06 00:08:46 +00003793 free_string_array(argvlist, argc);
3794 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003795
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 if (spawnval == -1)
3797 return posix_error();
3798 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003799#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003800 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003801#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003802 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003803#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003804}
3805
3806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003807PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003808"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003809Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003810\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003811 mode: mode of process creation\n\
3812 path: path of executable file\n\
3813 args: tuple or list of arguments\n\
3814 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003815
3816static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003817posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003818{
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 PyObject *opath;
3820 char *path;
3821 PyObject *argv, *env;
3822 char **argvlist;
3823 char **envlist;
3824 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003825 int mode;
3826 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003827 Py_intptr_t spawnval;
3828 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3829 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003830
Victor Stinner8c62be82010-05-06 00:08:46 +00003831 /* spawnve has four arguments: (mode, path, argv, env), where
3832 argv is a list or tuple of strings and env is a dictionary
3833 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003834
Victor Stinner8c62be82010-05-06 00:08:46 +00003835 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3836 PyUnicode_FSConverter,
3837 &opath, &argv, &env))
3838 return NULL;
3839 path = PyBytes_AsString(opath);
3840 if (PyList_Check(argv)) {
3841 argc = PyList_Size(argv);
3842 getitem = PyList_GetItem;
3843 }
3844 else if (PyTuple_Check(argv)) {
3845 argc = PyTuple_Size(argv);
3846 getitem = PyTuple_GetItem;
3847 }
3848 else {
3849 PyErr_SetString(PyExc_TypeError,
3850 "spawnve() arg 2 must be a tuple or list");
3851 goto fail_0;
3852 }
3853 if (!PyMapping_Check(env)) {
3854 PyErr_SetString(PyExc_TypeError,
3855 "spawnve() arg 3 must be a mapping object");
3856 goto fail_0;
3857 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003858
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 argvlist = PyMem_NEW(char *, argc+1);
3860 if (argvlist == NULL) {
3861 PyErr_NoMemory();
3862 goto fail_0;
3863 }
3864 for (i = 0; i < argc; i++) {
3865 if (!fsconvert_strdup((*getitem)(argv, i),
3866 &argvlist[i]))
3867 {
3868 lastarg = i;
3869 goto fail_1;
3870 }
3871 }
3872 lastarg = argc;
3873 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003874
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 envlist = parse_envlist(env, &envc);
3876 if (envlist == NULL)
3877 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003878
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003879#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003880 Py_BEGIN_ALLOW_THREADS
3881 spawnval = spawnve(mode, path, argvlist, envlist);
3882 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003883#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003884 if (mode == _OLD_P_OVERLAY)
3885 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003886
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 Py_BEGIN_ALLOW_THREADS
3888 spawnval = _spawnve(mode, path, argvlist, envlist);
3889 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003890#endif
Tim Peters25059d32001-12-07 20:35:43 +00003891
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 if (spawnval == -1)
3893 (void) posix_error();
3894 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003895#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003896 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003897#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003898 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003899#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003900
Victor Stinner8c62be82010-05-06 00:08:46 +00003901 while (--envc >= 0)
3902 PyMem_DEL(envlist[envc]);
3903 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003904 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003905 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003906 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003907 Py_DECREF(opath);
3908 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003909}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003910
3911/* OS/2 supports spawnvp & spawnvpe natively */
3912#if defined(PYOS_OS2)
3913PyDoc_STRVAR(posix_spawnvp__doc__,
3914"spawnvp(mode, file, args)\n\n\
3915Execute the program 'file' in a new process, using the environment\n\
3916search path to find the file.\n\
3917\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003918 mode: mode of process creation\n\
3919 file: executable file name\n\
3920 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003921
3922static PyObject *
3923posix_spawnvp(PyObject *self, PyObject *args)
3924{
Victor Stinner8c62be82010-05-06 00:08:46 +00003925 PyObject *opath;
3926 char *path;
3927 PyObject *argv;
3928 char **argvlist;
3929 int mode, i, argc;
3930 Py_intptr_t spawnval;
3931 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003932
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 /* spawnvp has three arguments: (mode, path, argv), where
3934 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003935
Victor Stinner8c62be82010-05-06 00:08:46 +00003936 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3937 PyUnicode_FSConverter,
3938 &opath, &argv))
3939 return NULL;
3940 path = PyBytes_AsString(opath);
3941 if (PyList_Check(argv)) {
3942 argc = PyList_Size(argv);
3943 getitem = PyList_GetItem;
3944 }
3945 else if (PyTuple_Check(argv)) {
3946 argc = PyTuple_Size(argv);
3947 getitem = PyTuple_GetItem;
3948 }
3949 else {
3950 PyErr_SetString(PyExc_TypeError,
3951 "spawnvp() arg 2 must be a tuple or list");
3952 Py_DECREF(opath);
3953 return NULL;
3954 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003955
Victor Stinner8c62be82010-05-06 00:08:46 +00003956 argvlist = PyMem_NEW(char *, argc+1);
3957 if (argvlist == NULL) {
3958 Py_DECREF(opath);
3959 return PyErr_NoMemory();
3960 }
3961 for (i = 0; i < argc; i++) {
3962 if (!fsconvert_strdup((*getitem)(argv, i),
3963 &argvlist[i])) {
3964 free_string_array(argvlist, i);
3965 PyErr_SetString(
3966 PyExc_TypeError,
3967 "spawnvp() arg 2 must contain only strings");
3968 Py_DECREF(opath);
3969 return NULL;
3970 }
3971 }
3972 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003973
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003975#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003977#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003978 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003979#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003980 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003981
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 free_string_array(argvlist, argc);
3983 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003984
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 if (spawnval == -1)
3986 return posix_error();
3987 else
3988 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003989}
3990
3991
3992PyDoc_STRVAR(posix_spawnvpe__doc__,
3993"spawnvpe(mode, file, args, env)\n\n\
3994Execute the program 'file' in a new process, using the environment\n\
3995search path to find the file.\n\
3996\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003997 mode: mode of process creation\n\
3998 file: executable file name\n\
3999 args: tuple or list of arguments\n\
4000 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004001
4002static PyObject *
4003posix_spawnvpe(PyObject *self, PyObject *args)
4004{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004005 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004006 char *path;
4007 PyObject *argv, *env;
4008 char **argvlist;
4009 char **envlist;
4010 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004011 int mode;
4012 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 Py_intptr_t spawnval;
4014 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4015 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004016
Victor Stinner8c62be82010-05-06 00:08:46 +00004017 /* spawnvpe has four arguments: (mode, path, argv, env), where
4018 argv is a list or tuple of strings and env is a dictionary
4019 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004020
Victor Stinner8c62be82010-05-06 00:08:46 +00004021 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4022 PyUnicode_FSConverter,
4023 &opath, &argv, &env))
4024 return NULL;
4025 path = PyBytes_AsString(opath);
4026 if (PyList_Check(argv)) {
4027 argc = PyList_Size(argv);
4028 getitem = PyList_GetItem;
4029 }
4030 else if (PyTuple_Check(argv)) {
4031 argc = PyTuple_Size(argv);
4032 getitem = PyTuple_GetItem;
4033 }
4034 else {
4035 PyErr_SetString(PyExc_TypeError,
4036 "spawnvpe() arg 2 must be a tuple or list");
4037 goto fail_0;
4038 }
4039 if (!PyMapping_Check(env)) {
4040 PyErr_SetString(PyExc_TypeError,
4041 "spawnvpe() arg 3 must be a mapping object");
4042 goto fail_0;
4043 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004044
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 argvlist = PyMem_NEW(char *, argc+1);
4046 if (argvlist == NULL) {
4047 PyErr_NoMemory();
4048 goto fail_0;
4049 }
4050 for (i = 0; i < argc; i++) {
4051 if (!fsconvert_strdup((*getitem)(argv, i),
4052 &argvlist[i]))
4053 {
4054 lastarg = i;
4055 goto fail_1;
4056 }
4057 }
4058 lastarg = argc;
4059 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004060
Victor Stinner8c62be82010-05-06 00:08:46 +00004061 envlist = parse_envlist(env, &envc);
4062 if (envlist == NULL)
4063 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004064
Victor Stinner8c62be82010-05-06 00:08:46 +00004065 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004066#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004067 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004068#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004069 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004070#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004071 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004072
Victor Stinner8c62be82010-05-06 00:08:46 +00004073 if (spawnval == -1)
4074 (void) posix_error();
4075 else
4076 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004077
Victor Stinner8c62be82010-05-06 00:08:46 +00004078 while (--envc >= 0)
4079 PyMem_DEL(envlist[envc]);
4080 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004081 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004082 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004083 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004084 Py_DECREF(opath);
4085 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004086}
4087#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004088#endif /* HAVE_SPAWNV */
4089
4090
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004091#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004092PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004093"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004094Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4095\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004096Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004097
4098static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004099posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004100{
Victor Stinner8c62be82010-05-06 00:08:46 +00004101 pid_t pid;
4102 int result = 0;
4103 _PyImport_AcquireLock();
4104 pid = fork1();
4105 if (pid == 0) {
4106 /* child: this clobbers and resets the import lock. */
4107 PyOS_AfterFork();
4108 } else {
4109 /* parent: release the import lock. */
4110 result = _PyImport_ReleaseLock();
4111 }
4112 if (pid == -1)
4113 return posix_error();
4114 if (result < 0) {
4115 /* Don't clobber the OSError if the fork failed. */
4116 PyErr_SetString(PyExc_RuntimeError,
4117 "not holding the import lock");
4118 return NULL;
4119 }
4120 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004121}
4122#endif
4123
4124
Guido van Rossumad0ee831995-03-01 10:34:45 +00004125#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004127"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004128Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004129Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004130
Barry Warsaw53699e91996-12-10 23:23:01 +00004131static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004132posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004133{
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 pid_t pid;
4135 int result = 0;
4136 _PyImport_AcquireLock();
4137 pid = fork();
4138 if (pid == 0) {
4139 /* child: this clobbers and resets the import lock. */
4140 PyOS_AfterFork();
4141 } else {
4142 /* parent: release the import lock. */
4143 result = _PyImport_ReleaseLock();
4144 }
4145 if (pid == -1)
4146 return posix_error();
4147 if (result < 0) {
4148 /* Don't clobber the OSError if the fork failed. */
4149 PyErr_SetString(PyExc_RuntimeError,
4150 "not holding the import lock");
4151 return NULL;
4152 }
4153 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004154}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004155#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004156
Neal Norwitzb59798b2003-03-21 01:43:31 +00004157/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004158/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4159#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004160#define DEV_PTY_FILE "/dev/ptc"
4161#define HAVE_DEV_PTMX
4162#else
4163#define DEV_PTY_FILE "/dev/ptmx"
4164#endif
4165
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004166#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004167#ifdef HAVE_PTY_H
4168#include <pty.h>
4169#else
4170#ifdef HAVE_LIBUTIL_H
4171#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004172#else
4173#ifdef HAVE_UTIL_H
4174#include <util.h>
4175#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004176#endif /* HAVE_LIBUTIL_H */
4177#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004178#ifdef HAVE_STROPTS_H
4179#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004180#endif
4181#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004182
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004183#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004184PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004185"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004186Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004187
4188static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004189posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004190{
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004192#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004193 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004194#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004195#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004196 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004197#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004198 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004199#endif
4200#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004201
Thomas Wouters70c21a12000-07-14 14:28:33 +00004202#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004203 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4204 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004205#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004206 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4207 if (slave_name == NULL)
4208 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004209
Victor Stinner8c62be82010-05-06 00:08:46 +00004210 slave_fd = open(slave_name, O_RDWR);
4211 if (slave_fd < 0)
4212 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004213#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004214 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4215 if (master_fd < 0)
4216 return posix_error();
4217 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4218 /* change permission of slave */
4219 if (grantpt(master_fd) < 0) {
4220 PyOS_setsig(SIGCHLD, sig_saved);
4221 return posix_error();
4222 }
4223 /* unlock slave */
4224 if (unlockpt(master_fd) < 0) {
4225 PyOS_setsig(SIGCHLD, sig_saved);
4226 return posix_error();
4227 }
4228 PyOS_setsig(SIGCHLD, sig_saved);
4229 slave_name = ptsname(master_fd); /* get name of slave */
4230 if (slave_name == NULL)
4231 return posix_error();
4232 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4233 if (slave_fd < 0)
4234 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004235#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004236 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4237 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004238#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004239 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004240#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004241#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004242#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004243
Victor Stinner8c62be82010-05-06 00:08:46 +00004244 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004245
Fred Drake8cef4cf2000-06-28 16:40:38 +00004246}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004247#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004248
4249#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004250PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004251"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004252Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4253Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004254To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004255
4256static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004257posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004258{
Victor Stinner8c62be82010-05-06 00:08:46 +00004259 int master_fd = -1, result = 0;
4260 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004261
Victor Stinner8c62be82010-05-06 00:08:46 +00004262 _PyImport_AcquireLock();
4263 pid = forkpty(&master_fd, NULL, NULL, NULL);
4264 if (pid == 0) {
4265 /* child: this clobbers and resets the import lock. */
4266 PyOS_AfterFork();
4267 } else {
4268 /* parent: release the import lock. */
4269 result = _PyImport_ReleaseLock();
4270 }
4271 if (pid == -1)
4272 return posix_error();
4273 if (result < 0) {
4274 /* Don't clobber the OSError if the fork failed. */
4275 PyErr_SetString(PyExc_RuntimeError,
4276 "not holding the import lock");
4277 return NULL;
4278 }
4279 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004280}
4281#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004282
Guido van Rossumad0ee831995-03-01 10:34:45 +00004283#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004284PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004285"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004286Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004287
Barry Warsaw53699e91996-12-10 23:23:01 +00004288static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004289posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004290{
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004292}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004293#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004294
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004295
Guido van Rossumad0ee831995-03-01 10:34:45 +00004296#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004297PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004298"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004299Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004300
Barry Warsaw53699e91996-12-10 23:23:01 +00004301static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004302posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004303{
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004305}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004306#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004307
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004308
Guido van Rossumad0ee831995-03-01 10:34:45 +00004309#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004310PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004311"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004313
Barry Warsaw53699e91996-12-10 23:23:01 +00004314static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004315posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004316{
Victor Stinner8c62be82010-05-06 00:08:46 +00004317 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004318}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004319#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004322PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004323"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004324Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004325
Barry Warsaw53699e91996-12-10 23:23:01 +00004326static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004327posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004328{
Victor Stinner8c62be82010-05-06 00:08:46 +00004329 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004330}
4331
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004332
Fred Drakec9680921999-12-13 16:37:25 +00004333#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004334PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004335"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004336Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004337
4338static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004339posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004340{
4341 PyObject *result = NULL;
4342
Fred Drakec9680921999-12-13 16:37:25 +00004343#ifdef NGROUPS_MAX
4344#define MAX_GROUPS NGROUPS_MAX
4345#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004347#define MAX_GROUPS 64
4348#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004350
Victor Stinner26de69d2011-06-17 15:15:38 +02004351 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004352 * This is a helper variable to store the intermediate result when
4353 * that happens.
4354 *
4355 * To keep the code readable the OSX behaviour is unconditional,
4356 * according to the POSIX spec this should be safe on all unix-y
4357 * systems.
4358 */
4359 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004360 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004361
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004363 if (n < 0) {
4364 if (errno == EINVAL) {
4365 n = getgroups(0, NULL);
4366 if (n == -1) {
4367 return posix_error();
4368 }
4369 if (n == 0) {
4370 /* Avoid malloc(0) */
4371 alt_grouplist = grouplist;
4372 } else {
4373 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4374 if (alt_grouplist == NULL) {
4375 errno = EINVAL;
4376 return posix_error();
4377 }
4378 n = getgroups(n, alt_grouplist);
4379 if (n == -1) {
4380 PyMem_Free(alt_grouplist);
4381 return posix_error();
4382 }
4383 }
4384 } else {
4385 return posix_error();
4386 }
4387 }
4388 result = PyList_New(n);
4389 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 int i;
4391 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004392 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004393 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004394 Py_DECREF(result);
4395 result = NULL;
4396 break;
Fred Drakec9680921999-12-13 16:37:25 +00004397 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004398 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004399 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004400 }
4401
4402 if (alt_grouplist != grouplist) {
4403 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004404 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004405
Fred Drakec9680921999-12-13 16:37:25 +00004406 return result;
4407}
4408#endif
4409
Antoine Pitroub7572f02009-12-02 20:46:48 +00004410#ifdef HAVE_INITGROUPS
4411PyDoc_STRVAR(posix_initgroups__doc__,
4412"initgroups(username, gid) -> None\n\n\
4413Call the system initgroups() to initialize the group access list with all of\n\
4414the groups of which the specified username is a member, plus the specified\n\
4415group id.");
4416
4417static PyObject *
4418posix_initgroups(PyObject *self, PyObject *args)
4419{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004420 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004421 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004422 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004423 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004424
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004425 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4426 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004427 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004428 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004429
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004430 res = initgroups(username, (gid_t) gid);
4431 Py_DECREF(oname);
4432 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004434
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 Py_INCREF(Py_None);
4436 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004437}
4438#endif
4439
Martin v. Löwis606edc12002-06-13 21:09:11 +00004440#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004441PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004442"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004443Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004444
4445static PyObject *
4446posix_getpgid(PyObject *self, PyObject *args)
4447{
Victor Stinner8c62be82010-05-06 00:08:46 +00004448 pid_t pid, pgid;
4449 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4450 return NULL;
4451 pgid = getpgid(pid);
4452 if (pgid < 0)
4453 return posix_error();
4454 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004455}
4456#endif /* HAVE_GETPGID */
4457
4458
Guido van Rossumb6775db1994-08-01 11:34:53 +00004459#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004460PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004461"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004462Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004463
Barry Warsaw53699e91996-12-10 23:23:01 +00004464static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004465posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004466{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004467#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004469#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004470 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004471#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004472}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004473#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004474
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004475
Guido van Rossumb6775db1994-08-01 11:34:53 +00004476#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004477PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004478"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004479Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004480
Barry Warsaw53699e91996-12-10 23:23:01 +00004481static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004482posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004483{
Guido van Rossum64933891994-10-20 21:56:42 +00004484#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004485 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004486#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004487 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004488#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004489 return posix_error();
4490 Py_INCREF(Py_None);
4491 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004492}
4493
Guido van Rossumb6775db1994-08-01 11:34:53 +00004494#endif /* HAVE_SETPGRP */
4495
Guido van Rossumad0ee831995-03-01 10:34:45 +00004496#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004497
4498#ifdef MS_WINDOWS
4499#include <tlhelp32.h>
4500
4501static PyObject*
4502win32_getppid()
4503{
4504 HANDLE snapshot;
4505 pid_t mypid;
4506 PyObject* result = NULL;
4507 BOOL have_record;
4508 PROCESSENTRY32 pe;
4509
4510 mypid = getpid(); /* This function never fails */
4511
4512 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4513 if (snapshot == INVALID_HANDLE_VALUE)
4514 return PyErr_SetFromWindowsErr(GetLastError());
4515
4516 pe.dwSize = sizeof(pe);
4517 have_record = Process32First(snapshot, &pe);
4518 while (have_record) {
4519 if (mypid == (pid_t)pe.th32ProcessID) {
4520 /* We could cache the ulong value in a static variable. */
4521 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4522 break;
4523 }
4524
4525 have_record = Process32Next(snapshot, &pe);
4526 }
4527
4528 /* If our loop exits and our pid was not found (result will be NULL)
4529 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4530 * error anyway, so let's raise it. */
4531 if (!result)
4532 result = PyErr_SetFromWindowsErr(GetLastError());
4533
4534 CloseHandle(snapshot);
4535
4536 return result;
4537}
4538#endif /*MS_WINDOWS*/
4539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004540PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004541"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004542Return the parent's process id. If the parent process has already exited,\n\
4543Windows machines will still return its id; others systems will return the id\n\
4544of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004545
Barry Warsaw53699e91996-12-10 23:23:01 +00004546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004547posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004548{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004549#ifdef MS_WINDOWS
4550 return win32_getppid();
4551#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004552 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004553#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004554}
4555#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004557
Fred Drake12c6e2d1999-12-14 21:25:03 +00004558#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004559PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004560"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004561Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004562
4563static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004564posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004565{
Victor Stinner8c62be82010-05-06 00:08:46 +00004566 PyObject *result = NULL;
Victor Stinner26de69d2011-06-17 15:15:38 +02004567#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004568 wchar_t user_name[UNLEN + 1];
4569 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4570
4571 if (GetUserNameW(user_name, &num_chars)) {
4572 /* num_chars is the number of unicode chars plus null terminator */
4573 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Victor Stinner26de69d2011-06-17 15:15:38 +02004574 }
4575 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004576 result = PyErr_SetFromWindowsErr(GetLastError());
4577#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004578 char *name;
4579 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004580
Victor Stinner8c62be82010-05-06 00:08:46 +00004581 errno = 0;
4582 name = getlogin();
4583 if (name == NULL) {
4584 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004585 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004586 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004587 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004588 }
4589 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004590 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004591 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004592#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004593 return result;
4594}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004595#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004596
Guido van Rossumad0ee831995-03-01 10:34:45 +00004597#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004598PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004599"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004601
Barry Warsaw53699e91996-12-10 23:23:01 +00004602static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004603posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004604{
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004606}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004607#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004608
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004609
Guido van Rossumad0ee831995-03-01 10:34:45 +00004610#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004611PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004612"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004613Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004614
Barry Warsaw53699e91996-12-10 23:23:01 +00004615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004616posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004617{
Victor Stinner8c62be82010-05-06 00:08:46 +00004618 pid_t pid;
4619 int sig;
4620 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4621 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004622#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004623 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4624 APIRET rc;
4625 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004626 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004627
4628 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4629 APIRET rc;
4630 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004631 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004632
4633 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004634 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004635#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004636 if (kill(pid, sig) == -1)
4637 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004638#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004639 Py_INCREF(Py_None);
4640 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004641}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004642#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004643
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004644#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004645PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004646"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004647Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004648
4649static PyObject *
4650posix_killpg(PyObject *self, PyObject *args)
4651{
Victor Stinner8c62be82010-05-06 00:08:46 +00004652 int sig;
4653 pid_t pgid;
4654 /* XXX some man pages make the `pgid` parameter an int, others
4655 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4656 take the same type. Moreover, pid_t is always at least as wide as
4657 int (else compilation of this module fails), which is safe. */
4658 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4659 return NULL;
4660 if (killpg(pgid, sig) == -1)
4661 return posix_error();
4662 Py_INCREF(Py_None);
4663 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004664}
4665#endif
4666
Brian Curtineb24d742010-04-12 17:16:38 +00004667#ifdef MS_WINDOWS
4668PyDoc_STRVAR(win32_kill__doc__,
4669"kill(pid, sig)\n\n\
4670Kill a process with a signal.");
4671
4672static PyObject *
4673win32_kill(PyObject *self, PyObject *args)
4674{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004675 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004676 DWORD pid, sig, err;
4677 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004678
Victor Stinner8c62be82010-05-06 00:08:46 +00004679 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4680 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004681
Victor Stinner8c62be82010-05-06 00:08:46 +00004682 /* Console processes which share a common console can be sent CTRL+C or
4683 CTRL+BREAK events, provided they handle said events. */
4684 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4685 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4686 err = GetLastError();
4687 PyErr_SetFromWindowsErr(err);
4688 }
4689 else
4690 Py_RETURN_NONE;
4691 }
Brian Curtineb24d742010-04-12 17:16:38 +00004692
Victor Stinner8c62be82010-05-06 00:08:46 +00004693 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4694 attempt to open and terminate the process. */
4695 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4696 if (handle == NULL) {
4697 err = GetLastError();
4698 return PyErr_SetFromWindowsErr(err);
4699 }
Brian Curtineb24d742010-04-12 17:16:38 +00004700
Victor Stinner8c62be82010-05-06 00:08:46 +00004701 if (TerminateProcess(handle, sig) == 0) {
4702 err = GetLastError();
4703 result = PyErr_SetFromWindowsErr(err);
4704 } else {
4705 Py_INCREF(Py_None);
4706 result = Py_None;
4707 }
Brian Curtineb24d742010-04-12 17:16:38 +00004708
Victor Stinner8c62be82010-05-06 00:08:46 +00004709 CloseHandle(handle);
4710 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004711}
4712#endif /* MS_WINDOWS */
4713
Guido van Rossumc0125471996-06-28 18:55:32 +00004714#ifdef HAVE_PLOCK
4715
4716#ifdef HAVE_SYS_LOCK_H
4717#include <sys/lock.h>
4718#endif
4719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004720PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004721"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004722Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004723
Barry Warsaw53699e91996-12-10 23:23:01 +00004724static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004725posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004726{
Victor Stinner8c62be82010-05-06 00:08:46 +00004727 int op;
4728 if (!PyArg_ParseTuple(args, "i:plock", &op))
4729 return NULL;
4730 if (plock(op) == -1)
4731 return posix_error();
4732 Py_INCREF(Py_None);
4733 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004734}
4735#endif
4736
Guido van Rossumb6775db1994-08-01 11:34:53 +00004737#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004738PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004739"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004740Set the current process's user id.");
4741
Barry Warsaw53699e91996-12-10 23:23:01 +00004742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004743posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004744{
Victor Stinner8c62be82010-05-06 00:08:46 +00004745 long uid_arg;
4746 uid_t uid;
4747 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4748 return NULL;
4749 uid = uid_arg;
4750 if (uid != uid_arg) {
4751 PyErr_SetString(PyExc_OverflowError, "user id too big");
4752 return NULL;
4753 }
4754 if (setuid(uid) < 0)
4755 return posix_error();
4756 Py_INCREF(Py_None);
4757 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004758}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004759#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004760
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004761
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004762#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004764"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004765Set the current process's effective user id.");
4766
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004767static PyObject *
4768posix_seteuid (PyObject *self, PyObject *args)
4769{
Victor Stinner8c62be82010-05-06 00:08:46 +00004770 long euid_arg;
4771 uid_t euid;
4772 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4773 return NULL;
4774 euid = euid_arg;
4775 if (euid != euid_arg) {
4776 PyErr_SetString(PyExc_OverflowError, "user id too big");
4777 return NULL;
4778 }
4779 if (seteuid(euid) < 0) {
4780 return posix_error();
4781 } else {
4782 Py_INCREF(Py_None);
4783 return Py_None;
4784 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004785}
4786#endif /* HAVE_SETEUID */
4787
4788#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004789PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004790"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004791Set the current process's effective group id.");
4792
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004793static PyObject *
4794posix_setegid (PyObject *self, PyObject *args)
4795{
Victor Stinner8c62be82010-05-06 00:08:46 +00004796 long egid_arg;
4797 gid_t egid;
4798 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4799 return NULL;
4800 egid = egid_arg;
4801 if (egid != egid_arg) {
4802 PyErr_SetString(PyExc_OverflowError, "group id too big");
4803 return NULL;
4804 }
4805 if (setegid(egid) < 0) {
4806 return posix_error();
4807 } else {
4808 Py_INCREF(Py_None);
4809 return Py_None;
4810 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004811}
4812#endif /* HAVE_SETEGID */
4813
4814#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004815PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004816"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004817Set the current process's real and effective user ids.");
4818
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004819static PyObject *
4820posix_setreuid (PyObject *self, PyObject *args)
4821{
Victor Stinner8c62be82010-05-06 00:08:46 +00004822 long ruid_arg, euid_arg;
4823 uid_t ruid, euid;
4824 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4825 return NULL;
4826 if (ruid_arg == -1)
4827 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4828 else
4829 ruid = ruid_arg; /* otherwise, assign from our long */
4830 if (euid_arg == -1)
4831 euid = (uid_t)-1;
4832 else
4833 euid = euid_arg;
4834 if ((euid_arg != -1 && euid != euid_arg) ||
4835 (ruid_arg != -1 && ruid != ruid_arg)) {
4836 PyErr_SetString(PyExc_OverflowError, "user id too big");
4837 return NULL;
4838 }
4839 if (setreuid(ruid, euid) < 0) {
4840 return posix_error();
4841 } else {
4842 Py_INCREF(Py_None);
4843 return Py_None;
4844 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004845}
4846#endif /* HAVE_SETREUID */
4847
4848#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004849PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004850"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004851Set the current process's real and effective group ids.");
4852
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004853static PyObject *
4854posix_setregid (PyObject *self, PyObject *args)
4855{
Victor Stinner8c62be82010-05-06 00:08:46 +00004856 long rgid_arg, egid_arg;
4857 gid_t rgid, egid;
4858 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4859 return NULL;
4860 if (rgid_arg == -1)
4861 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4862 else
4863 rgid = rgid_arg; /* otherwise, assign from our long */
4864 if (egid_arg == -1)
4865 egid = (gid_t)-1;
4866 else
4867 egid = egid_arg;
4868 if ((egid_arg != -1 && egid != egid_arg) ||
4869 (rgid_arg != -1 && rgid != rgid_arg)) {
4870 PyErr_SetString(PyExc_OverflowError, "group id too big");
4871 return NULL;
4872 }
4873 if (setregid(rgid, egid) < 0) {
4874 return posix_error();
4875 } else {
4876 Py_INCREF(Py_None);
4877 return Py_None;
4878 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00004879}
4880#endif /* HAVE_SETREGID */
4881
Guido van Rossumb6775db1994-08-01 11:34:53 +00004882#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004883PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004884"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004885Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004886
Barry Warsaw53699e91996-12-10 23:23:01 +00004887static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004888posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004889{
Victor Stinner8c62be82010-05-06 00:08:46 +00004890 long gid_arg;
4891 gid_t gid;
4892 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4893 return NULL;
4894 gid = gid_arg;
4895 if (gid != gid_arg) {
4896 PyErr_SetString(PyExc_OverflowError, "group id too big");
4897 return NULL;
4898 }
4899 if (setgid(gid) < 0)
4900 return posix_error();
4901 Py_INCREF(Py_None);
4902 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004903}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004904#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004905
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004906#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004907PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004908"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004909Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004910
4911static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004912posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004913{
Victor Stinner8c62be82010-05-06 00:08:46 +00004914 int i, len;
4915 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004916
Victor Stinner8c62be82010-05-06 00:08:46 +00004917 if (!PySequence_Check(groups)) {
4918 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4919 return NULL;
4920 }
4921 len = PySequence_Size(groups);
4922 if (len > MAX_GROUPS) {
4923 PyErr_SetString(PyExc_ValueError, "too many groups");
4924 return NULL;
4925 }
4926 for(i = 0; i < len; i++) {
4927 PyObject *elem;
4928 elem = PySequence_GetItem(groups, i);
4929 if (!elem)
4930 return NULL;
4931 if (!PyLong_Check(elem)) {
4932 PyErr_SetString(PyExc_TypeError,
4933 "groups must be integers");
4934 Py_DECREF(elem);
4935 return NULL;
4936 } else {
4937 unsigned long x = PyLong_AsUnsignedLong(elem);
4938 if (PyErr_Occurred()) {
4939 PyErr_SetString(PyExc_TypeError,
4940 "group id too big");
4941 Py_DECREF(elem);
4942 return NULL;
4943 }
4944 grouplist[i] = x;
4945 /* read back the value to see if it fitted in gid_t */
4946 if (grouplist[i] != x) {
4947 PyErr_SetString(PyExc_TypeError,
4948 "group id too big");
4949 Py_DECREF(elem);
4950 return NULL;
4951 }
4952 }
4953 Py_DECREF(elem);
4954 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004955
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 if (setgroups(len, grouplist) < 0)
4957 return posix_error();
4958 Py_INCREF(Py_None);
4959 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004960}
4961#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004962
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004963#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4964static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004965wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004966{
Victor Stinner8c62be82010-05-06 00:08:46 +00004967 PyObject *result;
4968 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004969
Victor Stinner8c62be82010-05-06 00:08:46 +00004970 if (pid == -1)
4971 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004972
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 if (struct_rusage == NULL) {
4974 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4975 if (m == NULL)
4976 return NULL;
4977 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4978 Py_DECREF(m);
4979 if (struct_rusage == NULL)
4980 return NULL;
4981 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004982
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4984 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4985 if (!result)
4986 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004987
4988#ifndef doubletime
4989#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4990#endif
4991
Victor Stinner8c62be82010-05-06 00:08:46 +00004992 PyStructSequence_SET_ITEM(result, 0,
4993 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4994 PyStructSequence_SET_ITEM(result, 1,
4995 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004996#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004997 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4998 SET_INT(result, 2, ru->ru_maxrss);
4999 SET_INT(result, 3, ru->ru_ixrss);
5000 SET_INT(result, 4, ru->ru_idrss);
5001 SET_INT(result, 5, ru->ru_isrss);
5002 SET_INT(result, 6, ru->ru_minflt);
5003 SET_INT(result, 7, ru->ru_majflt);
5004 SET_INT(result, 8, ru->ru_nswap);
5005 SET_INT(result, 9, ru->ru_inblock);
5006 SET_INT(result, 10, ru->ru_oublock);
5007 SET_INT(result, 11, ru->ru_msgsnd);
5008 SET_INT(result, 12, ru->ru_msgrcv);
5009 SET_INT(result, 13, ru->ru_nsignals);
5010 SET_INT(result, 14, ru->ru_nvcsw);
5011 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005012#undef SET_INT
5013
Victor Stinner8c62be82010-05-06 00:08:46 +00005014 if (PyErr_Occurred()) {
5015 Py_DECREF(result);
5016 return NULL;
5017 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005018
Victor Stinner8c62be82010-05-06 00:08:46 +00005019 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005020}
5021#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5022
5023#ifdef HAVE_WAIT3
5024PyDoc_STRVAR(posix_wait3__doc__,
5025"wait3(options) -> (pid, status, rusage)\n\n\
5026Wait for completion of a child process.");
5027
5028static PyObject *
5029posix_wait3(PyObject *self, PyObject *args)
5030{
Victor Stinner8c62be82010-05-06 00:08:46 +00005031 pid_t pid;
5032 int options;
5033 struct rusage ru;
5034 WAIT_TYPE status;
5035 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005036
Victor Stinner8c62be82010-05-06 00:08:46 +00005037 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5038 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005039
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 Py_BEGIN_ALLOW_THREADS
5041 pid = wait3(&status, options, &ru);
5042 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005043
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005045}
5046#endif /* HAVE_WAIT3 */
5047
5048#ifdef HAVE_WAIT4
5049PyDoc_STRVAR(posix_wait4__doc__,
5050"wait4(pid, options) -> (pid, status, rusage)\n\n\
5051Wait for completion of a given child process.");
5052
5053static PyObject *
5054posix_wait4(PyObject *self, PyObject *args)
5055{
Victor Stinner8c62be82010-05-06 00:08:46 +00005056 pid_t pid;
5057 int options;
5058 struct rusage ru;
5059 WAIT_TYPE status;
5060 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005061
Victor Stinner8c62be82010-05-06 00:08:46 +00005062 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5063 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005064
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 Py_BEGIN_ALLOW_THREADS
5066 pid = wait4(pid, &status, options, &ru);
5067 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005068
Victor Stinner8c62be82010-05-06 00:08:46 +00005069 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005070}
5071#endif /* HAVE_WAIT4 */
5072
Guido van Rossumb6775db1994-08-01 11:34:53 +00005073#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005074PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005075"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005076Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005077
Barry Warsaw53699e91996-12-10 23:23:01 +00005078static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005079posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005080{
Victor Stinner8c62be82010-05-06 00:08:46 +00005081 pid_t pid;
5082 int options;
5083 WAIT_TYPE status;
5084 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005085
Victor Stinner8c62be82010-05-06 00:08:46 +00005086 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5087 return NULL;
5088 Py_BEGIN_ALLOW_THREADS
5089 pid = waitpid(pid, &status, options);
5090 Py_END_ALLOW_THREADS
5091 if (pid == -1)
5092 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005093
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005095}
5096
Tim Petersab034fa2002-02-01 11:27:43 +00005097#elif defined(HAVE_CWAIT)
5098
5099/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005100PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005101"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005102"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005103
5104static PyObject *
5105posix_waitpid(PyObject *self, PyObject *args)
5106{
Victor Stinner8c62be82010-05-06 00:08:46 +00005107 Py_intptr_t pid;
5108 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005109
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5111 return NULL;
5112 Py_BEGIN_ALLOW_THREADS
5113 pid = _cwait(&status, pid, options);
5114 Py_END_ALLOW_THREADS
5115 if (pid == -1)
5116 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005117
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 /* shift the status left a byte so this is more like the POSIX waitpid */
5119 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005120}
5121#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005122
Guido van Rossumad0ee831995-03-01 10:34:45 +00005123#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005124PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005125"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005126Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005127
Barry Warsaw53699e91996-12-10 23:23:01 +00005128static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005129posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005130{
Victor Stinner8c62be82010-05-06 00:08:46 +00005131 pid_t pid;
5132 WAIT_TYPE status;
5133 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005134
Victor Stinner8c62be82010-05-06 00:08:46 +00005135 Py_BEGIN_ALLOW_THREADS
5136 pid = wait(&status);
5137 Py_END_ALLOW_THREADS
5138 if (pid == -1)
5139 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005140
Victor Stinner8c62be82010-05-06 00:08:46 +00005141 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005142}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005143#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005144
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005146PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005147"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005148Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005149
Barry Warsaw53699e91996-12-10 23:23:01 +00005150static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005151posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005152{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005153#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005154 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005155#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005156#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05005157 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00005158 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005159#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005160 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005161#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005162#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005163}
5164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005165
Guido van Rossumb6775db1994-08-01 11:34:53 +00005166#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005167PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005168"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005169Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005170
Barry Warsaw53699e91996-12-10 23:23:01 +00005171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005172posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005173{
Victor Stinner8c62be82010-05-06 00:08:46 +00005174 PyObject* v;
5175 char buf[MAXPATHLEN];
5176 PyObject *opath;
5177 char *path;
5178 int n;
5179 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005180
Victor Stinner8c62be82010-05-06 00:08:46 +00005181 if (!PyArg_ParseTuple(args, "O&:readlink",
5182 PyUnicode_FSConverter, &opath))
5183 return NULL;
5184 path = PyBytes_AsString(opath);
5185 v = PySequence_GetItem(args, 0);
5186 if (v == NULL) {
5187 Py_DECREF(opath);
5188 return NULL;
5189 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005190
Victor Stinner8c62be82010-05-06 00:08:46 +00005191 if (PyUnicode_Check(v)) {
5192 arg_is_unicode = 1;
5193 }
5194 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005195
Victor Stinner8c62be82010-05-06 00:08:46 +00005196 Py_BEGIN_ALLOW_THREADS
5197 n = readlink(path, buf, (int) sizeof buf);
5198 Py_END_ALLOW_THREADS
5199 if (n < 0)
5200 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005201
Victor Stinner8c62be82010-05-06 00:08:46 +00005202 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005203 if (arg_is_unicode)
5204 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5205 else
5206 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005207}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005208#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005209
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005210
Brian Curtin52173d42010-12-02 18:29:18 +00005211#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005213"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005214Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005215
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005217posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005218{
Victor Stinner8c62be82010-05-06 00:08:46 +00005219 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005220}
5221#endif /* HAVE_SYMLINK */
5222
Brian Curtind40e6f72010-07-08 21:39:08 +00005223#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5224
5225PyDoc_STRVAR(win_readlink__doc__,
5226"readlink(path) -> path\n\n\
5227Return a string representing the path to which the symbolic link points.");
5228
Brian Curtind40e6f72010-07-08 21:39:08 +00005229/* Windows readlink implementation */
5230static PyObject *
5231win_readlink(PyObject *self, PyObject *args)
5232{
5233 wchar_t *path;
5234 DWORD n_bytes_returned;
5235 DWORD io_result;
5236 PyObject *result;
5237 HANDLE reparse_point_handle;
5238
5239 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5240 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5241 wchar_t *print_name;
5242
5243 if (!PyArg_ParseTuple(args,
5244 "u:readlink",
5245 &path))
5246 return NULL;
5247
5248 /* First get a handle to the reparse point */
5249 Py_BEGIN_ALLOW_THREADS
5250 reparse_point_handle = CreateFileW(
5251 path,
5252 0,
5253 0,
5254 0,
5255 OPEN_EXISTING,
5256 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5257 0);
5258 Py_END_ALLOW_THREADS
Victor Stinner26de69d2011-06-17 15:15:38 +02005259
Brian Curtind40e6f72010-07-08 21:39:08 +00005260 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5261 {
5262 return win32_error_unicode("readlink", path);
5263 }
Victor Stinner26de69d2011-06-17 15:15:38 +02005264
Brian Curtind40e6f72010-07-08 21:39:08 +00005265 Py_BEGIN_ALLOW_THREADS
5266 /* New call DeviceIoControl to read the reparse point */
5267 io_result = DeviceIoControl(
5268 reparse_point_handle,
5269 FSCTL_GET_REPARSE_POINT,
5270 0, 0, /* in buffer */
5271 target_buffer, sizeof(target_buffer),
5272 &n_bytes_returned,
5273 0 /* we're not using OVERLAPPED_IO */
5274 );
5275 CloseHandle(reparse_point_handle);
5276 Py_END_ALLOW_THREADS
5277
5278 if (io_result==0)
5279 {
5280 return win32_error_unicode("readlink", path);
5281 }
5282
5283 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5284 {
5285 PyErr_SetString(PyExc_ValueError,
5286 "not a symbolic link");
5287 return NULL;
5288 }
Brian Curtin74e45612010-07-09 15:58:59 +00005289 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5290 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5291
5292 result = PyUnicode_FromWideChar(print_name,
5293 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005294 return result;
5295}
5296
5297#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5298
Brian Curtin52173d42010-12-02 18:29:18 +00005299#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005300
5301/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5302static int has_CreateSymbolicLinkW = 0;
5303static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5304static int
5305check_CreateSymbolicLinkW()
5306{
5307 HINSTANCE hKernel32;
5308 /* only recheck */
5309 if (has_CreateSymbolicLinkW)
5310 return has_CreateSymbolicLinkW;
5311 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005312 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5313 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005314 if (Py_CreateSymbolicLinkW)
5315 has_CreateSymbolicLinkW = 1;
5316 return has_CreateSymbolicLinkW;
5317}
5318
5319PyDoc_STRVAR(win_symlink__doc__,
5320"symlink(src, dst, target_is_directory=False)\n\n\
5321Create a symbolic link pointing to src named dst.\n\
5322target_is_directory is required if the target is to be interpreted as\n\
5323a directory.\n\
5324This function requires Windows 6.0 or greater, and raises a\n\
5325NotImplementedError otherwise.");
5326
5327static PyObject *
5328win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5329{
5330 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5331 PyObject *src, *dest;
5332 int target_is_directory = 0;
5333 DWORD res;
Victor Stinner26de69d2011-06-17 15:15:38 +02005334
Brian Curtind40e6f72010-07-08 21:39:08 +00005335 if (!check_CreateSymbolicLinkW())
5336 {
5337 /* raise NotImplementedError */
5338 return PyErr_Format(PyExc_NotImplementedError,
5339 "CreateSymbolicLinkW not found");
5340 }
5341 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5342 kwlist, &src, &dest, &target_is_directory))
5343 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005344
5345 if (win32_can_symlink == 0)
5346 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5347
Brian Curtind40e6f72010-07-08 21:39:08 +00005348 if (!convert_to_unicode(&src)) { return NULL; }
5349 if (!convert_to_unicode(&dest)) {
5350 Py_DECREF(src);
5351 return NULL;
5352 }
Victor Stinner26de69d2011-06-17 15:15:38 +02005353
Brian Curtind40e6f72010-07-08 21:39:08 +00005354 Py_BEGIN_ALLOW_THREADS
5355 res = Py_CreateSymbolicLinkW(
5356 PyUnicode_AsUnicode(dest),
5357 PyUnicode_AsUnicode(src),
5358 target_is_directory);
5359 Py_END_ALLOW_THREADS
5360 Py_DECREF(src);
5361 Py_DECREF(dest);
5362 if (!res)
5363 {
5364 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5365 }
Victor Stinner26de69d2011-06-17 15:15:38 +02005366
Brian Curtind40e6f72010-07-08 21:39:08 +00005367 Py_INCREF(Py_None);
5368 return Py_None;
5369}
Brian Curtin52173d42010-12-02 18:29:18 +00005370#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005371
5372#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005373#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5374static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005375system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005376{
5377 ULONG value = 0;
5378
5379 Py_BEGIN_ALLOW_THREADS
5380 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5381 Py_END_ALLOW_THREADS
5382
5383 return value;
5384}
5385
5386static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005387posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005388{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005389 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005390 return Py_BuildValue("ddddd",
5391 (double)0 /* t.tms_utime / HZ */,
5392 (double)0 /* t.tms_stime / HZ */,
5393 (double)0 /* t.tms_cutime / HZ */,
5394 (double)0 /* t.tms_cstime / HZ */,
5395 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005396}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005397#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005398#define NEED_TICKS_PER_SECOND
5399static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005401posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005402{
Victor Stinner8c62be82010-05-06 00:08:46 +00005403 struct tms t;
5404 clock_t c;
5405 errno = 0;
5406 c = times(&t);
5407 if (c == (clock_t) -1)
5408 return posix_error();
5409 return Py_BuildValue("ddddd",
5410 (double)t.tms_utime / ticks_per_second,
5411 (double)t.tms_stime / ticks_per_second,
5412 (double)t.tms_cutime / ticks_per_second,
5413 (double)t.tms_cstime / ticks_per_second,
5414 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005415}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005416#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005417#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005418
5419
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005420#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005421#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005422static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005423posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005424{
Victor Stinner8c62be82010-05-06 00:08:46 +00005425 FILETIME create, exit, kernel, user;
5426 HANDLE hProc;
5427 hProc = GetCurrentProcess();
5428 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5429 /* The fields of a FILETIME structure are the hi and lo part
5430 of a 64-bit value expressed in 100 nanosecond units.
5431 1e7 is one second in such units; 1e-7 the inverse.
5432 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5433 */
5434 return Py_BuildValue(
5435 "ddddd",
5436 (double)(user.dwHighDateTime*429.4967296 +
5437 user.dwLowDateTime*1e-7),
5438 (double)(kernel.dwHighDateTime*429.4967296 +
5439 kernel.dwLowDateTime*1e-7),
5440 (double)0,
5441 (double)0,
5442 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005443}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005444#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005445
5446#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005447PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005448"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005449Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005450#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005451
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005452
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005453#ifdef HAVE_GETSID
5454PyDoc_STRVAR(posix_getsid__doc__,
5455"getsid(pid) -> sid\n\n\
5456Call the system call getsid().");
5457
5458static PyObject *
5459posix_getsid(PyObject *self, PyObject *args)
5460{
Victor Stinner8c62be82010-05-06 00:08:46 +00005461 pid_t pid;
5462 int sid;
5463 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5464 return NULL;
5465 sid = getsid(pid);
5466 if (sid < 0)
5467 return posix_error();
5468 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005469}
5470#endif /* HAVE_GETSID */
5471
5472
Guido van Rossumb6775db1994-08-01 11:34:53 +00005473#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005475"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005476Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005477
Barry Warsaw53699e91996-12-10 23:23:01 +00005478static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005479posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005480{
Victor Stinner8c62be82010-05-06 00:08:46 +00005481 if (setsid() < 0)
5482 return posix_error();
5483 Py_INCREF(Py_None);
5484 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005485}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005486#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005487
Guido van Rossumb6775db1994-08-01 11:34:53 +00005488#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005489PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005490"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005491Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005492
Barry Warsaw53699e91996-12-10 23:23:01 +00005493static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005494posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005495{
Victor Stinner8c62be82010-05-06 00:08:46 +00005496 pid_t pid;
5497 int pgrp;
5498 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5499 return NULL;
5500 if (setpgid(pid, pgrp) < 0)
5501 return posix_error();
5502 Py_INCREF(Py_None);
5503 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005504}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005505#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005506
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005507
Guido van Rossumb6775db1994-08-01 11:34:53 +00005508#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005509PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005510"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005511Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005512
Barry Warsaw53699e91996-12-10 23:23:01 +00005513static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005514posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005515{
Victor Stinner8c62be82010-05-06 00:08:46 +00005516 int fd;
5517 pid_t pgid;
5518 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5519 return NULL;
5520 pgid = tcgetpgrp(fd);
5521 if (pgid < 0)
5522 return posix_error();
5523 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005524}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005525#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005527
Guido van Rossumb6775db1994-08-01 11:34:53 +00005528#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005530"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005531Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005532
Barry Warsaw53699e91996-12-10 23:23:01 +00005533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005534posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005535{
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 int fd;
5537 pid_t pgid;
5538 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5539 return NULL;
5540 if (tcsetpgrp(fd, pgid) < 0)
5541 return posix_error();
5542 Py_INCREF(Py_None);
5543 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005544}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005545#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005546
Guido van Rossum687dd131993-05-17 08:34:16 +00005547/* Functions acting on file descriptors */
5548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005549PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005550"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005551Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005552
Barry Warsaw53699e91996-12-10 23:23:01 +00005553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005554posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005555{
Victor Stinner8c62be82010-05-06 00:08:46 +00005556 PyObject *ofile;
5557 char *file;
5558 int flag;
5559 int mode = 0777;
5560 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005561
5562#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005563 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02005564 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 Py_BEGIN_ALLOW_THREADS
5566 /* PyUnicode_AS_UNICODE OK without thread
5567 lock as it is a simple dereference. */
5568 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5569 Py_END_ALLOW_THREADS
5570 if (fd < 0)
5571 return posix_error();
5572 return PyLong_FromLong((long)fd);
5573 }
5574 /* Drop the argument parsing error as narrow strings
5575 are also valid. */
5576 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005577#endif
5578
Victor Stinner26de69d2011-06-17 15:15:38 +02005579 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00005580 PyUnicode_FSConverter, &ofile,
5581 &flag, &mode))
5582 return NULL;
5583 file = PyBytes_AsString(ofile);
5584 Py_BEGIN_ALLOW_THREADS
5585 fd = open(file, flag, mode);
5586 Py_END_ALLOW_THREADS
5587 if (fd < 0)
5588 return posix_error_with_allocated_filename(ofile);
5589 Py_DECREF(ofile);
5590 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005591}
5592
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005594PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005595"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005597
Barry Warsaw53699e91996-12-10 23:23:01 +00005598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005599posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005600{
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 int fd, res;
5602 if (!PyArg_ParseTuple(args, "i:close", &fd))
5603 return NULL;
5604 if (!_PyVerify_fd(fd))
5605 return posix_error();
5606 Py_BEGIN_ALLOW_THREADS
5607 res = close(fd);
5608 Py_END_ALLOW_THREADS
5609 if (res < 0)
5610 return posix_error();
5611 Py_INCREF(Py_None);
5612 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005613}
5614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005615
Victor Stinner8c62be82010-05-06 00:08:46 +00005616PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005617"closerange(fd_low, fd_high)\n\n\
5618Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5619
5620static PyObject *
5621posix_closerange(PyObject *self, PyObject *args)
5622{
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 int fd_from, fd_to, i;
5624 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5625 return NULL;
5626 Py_BEGIN_ALLOW_THREADS
5627 for (i = fd_from; i < fd_to; i++)
5628 if (_PyVerify_fd(i))
5629 close(i);
5630 Py_END_ALLOW_THREADS
5631 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005632}
5633
5634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005635PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005636"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005637Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005638
Barry Warsaw53699e91996-12-10 23:23:01 +00005639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005640posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005641{
Victor Stinner8c62be82010-05-06 00:08:46 +00005642 int fd;
5643 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5644 return NULL;
5645 if (!_PyVerify_fd(fd))
5646 return posix_error();
5647 Py_BEGIN_ALLOW_THREADS
5648 fd = dup(fd);
5649 Py_END_ALLOW_THREADS
5650 if (fd < 0)
5651 return posix_error();
5652 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005653}
5654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005656PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005657"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005658Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005659
Barry Warsaw53699e91996-12-10 23:23:01 +00005660static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005661posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005662{
Victor Stinner8c62be82010-05-06 00:08:46 +00005663 int fd, fd2, res;
5664 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5665 return NULL;
5666 if (!_PyVerify_fd_dup2(fd, fd2))
5667 return posix_error();
5668 Py_BEGIN_ALLOW_THREADS
5669 res = dup2(fd, fd2);
5670 Py_END_ALLOW_THREADS
5671 if (res < 0)
5672 return posix_error();
5673 Py_INCREF(Py_None);
5674 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005675}
5676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005679"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01005680Set the current position of a file descriptor.\n\
5681Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005682
Barry Warsaw53699e91996-12-10 23:23:01 +00005683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005684posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005685{
Victor Stinner8c62be82010-05-06 00:08:46 +00005686 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005687#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005688 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005689#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005690 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005691#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005692 PyObject *posobj;
5693 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5694 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005695#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005696 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5697 switch (how) {
5698 case 0: how = SEEK_SET; break;
5699 case 1: how = SEEK_CUR; break;
5700 case 2: how = SEEK_END; break;
5701 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005702#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005703
5704#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005705 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005706#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005707 pos = PyLong_Check(posobj) ?
5708 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005709#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005710 if (PyErr_Occurred())
5711 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005712
Victor Stinner8c62be82010-05-06 00:08:46 +00005713 if (!_PyVerify_fd(fd))
5714 return posix_error();
5715 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005716#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005717 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005718#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005720#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 Py_END_ALLOW_THREADS
5722 if (res < 0)
5723 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005724
5725#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005726 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005727#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005728 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005729#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005730}
5731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005732
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005733PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005734"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005735Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005736
Barry Warsaw53699e91996-12-10 23:23:01 +00005737static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005738posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005739{
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 int fd, size;
5741 Py_ssize_t n;
5742 PyObject *buffer;
5743 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5744 return NULL;
5745 if (size < 0) {
5746 errno = EINVAL;
5747 return posix_error();
5748 }
5749 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5750 if (buffer == NULL)
5751 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005752 if (!_PyVerify_fd(fd)) {
5753 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005755 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 Py_BEGIN_ALLOW_THREADS
5757 n = read(fd, PyBytes_AS_STRING(buffer), size);
5758 Py_END_ALLOW_THREADS
5759 if (n < 0) {
5760 Py_DECREF(buffer);
5761 return posix_error();
5762 }
5763 if (n != size)
5764 _PyBytes_Resize(&buffer, n);
5765 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005766}
5767
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005768
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005769PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005770"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005771Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005772
Barry Warsaw53699e91996-12-10 23:23:01 +00005773static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005774posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005775{
Victor Stinner8c62be82010-05-06 00:08:46 +00005776 Py_buffer pbuf;
5777 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005778 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005779
Victor Stinner8c62be82010-05-06 00:08:46 +00005780 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5781 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005782 if (!_PyVerify_fd(fd)) {
5783 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005784 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005785 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005786 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005788#if defined(MS_WIN64) || defined(MS_WINDOWS)
5789 if (len > INT_MAX)
5790 len = INT_MAX;
5791 size = write(fd, pbuf.buf, (int)len);
5792#else
Victor Stinner72344792011-01-11 00:04:12 +00005793 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005794#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005795 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005796 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 if (size < 0)
5798 return posix_error();
5799 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005800}
5801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005804"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005806
Barry Warsaw53699e91996-12-10 23:23:01 +00005807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005808posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005809{
Victor Stinner8c62be82010-05-06 00:08:46 +00005810 int fd;
5811 STRUCT_STAT st;
5812 int res;
5813 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5814 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005815#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 /* on OpenVMS we must ensure that all bytes are written to the file */
5817 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005818#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 if (!_PyVerify_fd(fd))
5820 return posix_error();
5821 Py_BEGIN_ALLOW_THREADS
5822 res = FSTAT(fd, &st);
5823 Py_END_ALLOW_THREADS
5824 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005825#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005826 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005827#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005829#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005830 }
Tim Peters5aa91602002-01-30 05:46:57 +00005831
Victor Stinner8c62be82010-05-06 00:08:46 +00005832 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005833}
5834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005835PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005836"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005837Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005838connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005839
5840static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005841posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005842{
Victor Stinner8c62be82010-05-06 00:08:46 +00005843 int fd;
5844 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5845 return NULL;
5846 if (!_PyVerify_fd(fd))
5847 return PyBool_FromLong(0);
5848 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005849}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005850
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005851#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005852PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005853"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005855
Barry Warsaw53699e91996-12-10 23:23:01 +00005856static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005857posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005858{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005859#if defined(PYOS_OS2)
5860 HFILE read, write;
5861 APIRET rc;
5862
Victor Stinner8c62be82010-05-06 00:08:46 +00005863 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005864 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005866 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005867 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005868
5869 return Py_BuildValue("(ii)", read, write);
5870#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005871#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005872 int fds[2];
5873 int res;
5874 Py_BEGIN_ALLOW_THREADS
5875 res = pipe(fds);
5876 Py_END_ALLOW_THREADS
5877 if (res != 0)
5878 return posix_error();
5879 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005880#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005881 HANDLE read, write;
5882 int read_fd, write_fd;
5883 BOOL ok;
5884 Py_BEGIN_ALLOW_THREADS
5885 ok = CreatePipe(&read, &write, NULL, 0);
5886 Py_END_ALLOW_THREADS
5887 if (!ok)
5888 return win32_error("CreatePipe", NULL);
5889 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5890 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5891 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005892#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005893#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005894}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005895#endif /* HAVE_PIPE */
5896
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005897
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005898#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005899PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005900"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005901Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005902
Barry Warsaw53699e91996-12-10 23:23:01 +00005903static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005904posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005905{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005906 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005907 char *filename;
5908 int mode = 0666;
5909 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005910 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5911 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005913 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 Py_BEGIN_ALLOW_THREADS
5915 res = mkfifo(filename, mode);
5916 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005917 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005918 if (res < 0)
5919 return posix_error();
5920 Py_INCREF(Py_None);
5921 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005922}
5923#endif
5924
5925
Neal Norwitz11690112002-07-30 01:08:28 +00005926#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005928"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005929Create a filesystem node (file, device special file or named pipe)\n\
5930named filename. mode specifies both the permissions to use and the\n\
5931type of node to be created, being combined (bitwise OR) with one of\n\
5932S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005933device defines the newly created device special file (probably using\n\
5934os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005935
5936
5937static PyObject *
5938posix_mknod(PyObject *self, PyObject *args)
5939{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005940 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005941 char *filename;
5942 int mode = 0600;
5943 int device = 0;
5944 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005945 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5946 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005947 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005948 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 Py_BEGIN_ALLOW_THREADS
5950 res = mknod(filename, mode, device);
5951 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005952 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 if (res < 0)
5954 return posix_error();
5955 Py_INCREF(Py_None);
5956 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005957}
5958#endif
5959
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005960#ifdef HAVE_DEVICE_MACROS
5961PyDoc_STRVAR(posix_major__doc__,
5962"major(device) -> major number\n\
5963Extracts a device major number from a raw device number.");
5964
5965static PyObject *
5966posix_major(PyObject *self, PyObject *args)
5967{
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 int device;
5969 if (!PyArg_ParseTuple(args, "i:major", &device))
5970 return NULL;
5971 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005972}
5973
5974PyDoc_STRVAR(posix_minor__doc__,
5975"minor(device) -> minor number\n\
5976Extracts a device minor number from a raw device number.");
5977
5978static PyObject *
5979posix_minor(PyObject *self, PyObject *args)
5980{
Victor Stinner8c62be82010-05-06 00:08:46 +00005981 int device;
5982 if (!PyArg_ParseTuple(args, "i:minor", &device))
5983 return NULL;
5984 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005985}
5986
5987PyDoc_STRVAR(posix_makedev__doc__,
5988"makedev(major, minor) -> device number\n\
5989Composes a raw device number from the major and minor device numbers.");
5990
5991static PyObject *
5992posix_makedev(PyObject *self, PyObject *args)
5993{
Victor Stinner8c62be82010-05-06 00:08:46 +00005994 int major, minor;
5995 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5996 return NULL;
5997 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005998}
5999#endif /* device macros */
6000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006002#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006003PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006004"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006005Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006006
Barry Warsaw53699e91996-12-10 23:23:01 +00006007static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006008posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006009{
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 int fd;
6011 off_t length;
6012 int res;
6013 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006014
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6016 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006017
6018#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006020#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 length = PyLong_Check(lenobj) ?
6022 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006023#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 if (PyErr_Occurred())
6025 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006026
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 Py_BEGIN_ALLOW_THREADS
6028 res = ftruncate(fd, length);
6029 Py_END_ALLOW_THREADS
6030 if (res < 0)
6031 return posix_error();
6032 Py_INCREF(Py_None);
6033 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006034}
6035#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006036
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006037#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006038PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006039"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006040Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006041
Fred Drake762e2061999-08-26 17:23:54 +00006042/* Save putenv() parameters as values here, so we can collect them when they
6043 * get re-set with another call for the same key. */
6044static PyObject *posix_putenv_garbage;
6045
Tim Peters5aa91602002-01-30 05:46:57 +00006046static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006047posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006048{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006049#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006050 wchar_t *s1, *s2;
6051 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006052#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006053 PyObject *os1, *os2;
6054 char *s1, *s2;
6055 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006056#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006057 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006059
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006060#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006061 if (!PyArg_ParseTuple(args,
6062 "uu:putenv",
6063 &s1, &s2))
6064 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006065#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006066 if (!PyArg_ParseTuple(args,
6067 "O&O&:putenv",
6068 PyUnicode_FSConverter, &os1,
6069 PyUnicode_FSConverter, &os2))
6070 return NULL;
6071 s1 = PyBytes_AsString(os1);
6072 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006073#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006074
6075#if defined(PYOS_OS2)
6076 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6077 APIRET rc;
6078
Guido van Rossumd48f2521997-12-05 22:19:34 +00006079 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006080 if (rc != NO_ERROR) {
6081 os2_error(rc);
6082 goto error;
6083 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006084
6085 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6086 APIRET rc;
6087
Guido van Rossumd48f2521997-12-05 22:19:34 +00006088 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006089 if (rc != NO_ERROR) {
6090 os2_error(rc);
6091 goto error;
6092 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006093 } else {
6094#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006095 /* XXX This can leak memory -- not easy to fix :-( */
6096 /* len includes space for a trailing \0; the size arg to
6097 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006098#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 len = wcslen(s1) + wcslen(s2) + 2;
Victor Stinner60b385e2011-11-22 22:01:28 +01006100 if (_MAX_ENV < (len - 1)) {
6101 PyErr_Format(PyExc_ValueError,
6102 "the environment variable is longer than %u characters",
6103 _MAX_ENV);
6104 goto error;
6105 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006106 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006107#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006108 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006110#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006111 if (newstr == NULL) {
6112 PyErr_NoMemory();
6113 goto error;
6114 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006115#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006116 newenv = PyUnicode_AsUnicode(newstr);
6117 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6118 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006120 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006122#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006123 newenv = PyBytes_AS_STRING(newstr);
6124 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6125 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006126 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006127 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006128 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006129#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006130
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 /* Install the first arg and newstr in posix_putenv_garbage;
6132 * this will cause previous value to be collected. This has to
6133 * happen after the real putenv() call because the old value
6134 * was still accessible until then. */
6135 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006136#ifdef MS_WINDOWS
6137 PyTuple_GET_ITEM(args, 0),
6138#else
6139 os1,
6140#endif
6141 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 /* really not much we can do; just leak */
6143 PyErr_Clear();
6144 }
6145 else {
6146 Py_DECREF(newstr);
6147 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006148
6149#if defined(PYOS_OS2)
6150 }
6151#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006152
Martin v. Löwis011e8422009-05-05 04:43:17 +00006153#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 Py_DECREF(os1);
6155 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006156#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006157 Py_RETURN_NONE;
6158
6159error:
6160#ifndef MS_WINDOWS
6161 Py_DECREF(os1);
6162 Py_DECREF(os2);
6163#endif
6164 Py_XDECREF(newstr);
6165 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006166}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006167#endif /* putenv */
6168
Guido van Rossumc524d952001-10-19 01:31:59 +00006169#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006170PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006171"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006172Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006173
6174static PyObject *
6175posix_unsetenv(PyObject *self, PyObject *args)
6176{
Victor Stinner84ae1182010-05-06 22:05:07 +00006177 PyObject *os1;
6178 char *s1;
Charles-François Natali6613c182011-11-27 12:41:06 +01006179#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01006180 int err;
Charles-François Natali6613c182011-11-27 12:41:06 +01006181#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006182
6183 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6184 PyUnicode_FSConverter, &os1))
6185 return NULL;
6186 s1 = PyBytes_AsString(os1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006187
Charles-François Natali6613c182011-11-27 12:41:06 +01006188#ifdef HAVE_BROKEN_UNSETENV
6189 unsetenv(s1);
6190#else
Victor Stinner60b385e2011-11-22 22:01:28 +01006191 err = unsetenv(s1);
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06006192 if (err) {
Benjamin Peterson06403cf2011-11-22 23:57:23 -06006193 Py_DECREF(os1);
Victor Stinner60b385e2011-11-22 22:01:28 +01006194 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06006195 }
Charles-François Natali6613c182011-11-27 12:41:06 +01006196#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006197
Victor Stinner8c62be82010-05-06 00:08:46 +00006198 /* Remove the key from posix_putenv_garbage;
6199 * this will cause it to be collected. This has to
6200 * happen after the real unsetenv() call because the
6201 * old value was still accessible until then.
6202 */
Victor Stinner60b385e2011-11-22 22:01:28 +01006203 if (PyDict_DelItem(posix_putenv_garbage, os1)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 /* really not much we can do; just leak */
6205 PyErr_Clear();
6206 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006207
Victor Stinner84ae1182010-05-06 22:05:07 +00006208 Py_DECREF(os1);
Victor Stinner84ae1182010-05-06 22:05:07 +00006209 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006210}
6211#endif /* unsetenv */
6212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006214"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006215Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006216
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006218posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006219{
Victor Stinner8c62be82010-05-06 00:08:46 +00006220 int code;
6221 char *message;
6222 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6223 return NULL;
6224 message = strerror(code);
6225 if (message == NULL) {
6226 PyErr_SetString(PyExc_ValueError,
6227 "strerror() argument out of range");
6228 return NULL;
6229 }
6230 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006231}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006232
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006233
Guido van Rossumc9641791998-08-04 15:26:23 +00006234#ifdef HAVE_SYS_WAIT_H
6235
Fred Drake106c1a02002-04-23 15:58:02 +00006236#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006237PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006238"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006239Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006240
6241static PyObject *
6242posix_WCOREDUMP(PyObject *self, PyObject *args)
6243{
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 WAIT_TYPE status;
6245 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006246
Victor Stinner8c62be82010-05-06 00:08:46 +00006247 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6248 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006249
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006251}
6252#endif /* WCOREDUMP */
6253
6254#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006255PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006256"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006257Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006258job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006259
6260static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006261posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006262{
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 WAIT_TYPE status;
6264 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006265
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6267 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006268
Victor Stinner8c62be82010-05-06 00:08:46 +00006269 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006270}
6271#endif /* WIFCONTINUED */
6272
Guido van Rossumc9641791998-08-04 15:26:23 +00006273#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006275"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006276Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006277
6278static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006279posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006280{
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 WAIT_TYPE status;
6282 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006283
Victor Stinner8c62be82010-05-06 00:08:46 +00006284 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6285 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006286
Victor Stinner8c62be82010-05-06 00:08:46 +00006287 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006288}
6289#endif /* WIFSTOPPED */
6290
6291#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006292PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006293"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006294Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006295
6296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006297posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006298{
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 WAIT_TYPE status;
6300 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006301
Victor Stinner8c62be82010-05-06 00:08:46 +00006302 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6303 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006304
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006306}
6307#endif /* WIFSIGNALED */
6308
6309#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006310PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006311"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006312Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006313system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006314
6315static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006316posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006317{
Victor Stinner8c62be82010-05-06 00:08:46 +00006318 WAIT_TYPE status;
6319 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006320
Victor Stinner8c62be82010-05-06 00:08:46 +00006321 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6322 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006323
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006325}
6326#endif /* WIFEXITED */
6327
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006328#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006329PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006330"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006331Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006332
6333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006334posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006335{
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 WAIT_TYPE status;
6337 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006338
Victor Stinner8c62be82010-05-06 00:08:46 +00006339 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6340 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006341
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006343}
6344#endif /* WEXITSTATUS */
6345
6346#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006347PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006348"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006349Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006350value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006351
6352static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006353posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006354{
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 WAIT_TYPE status;
6356 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006357
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6359 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006360
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006362}
6363#endif /* WTERMSIG */
6364
6365#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006366PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006367"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006368Return the signal that stopped the process that provided\n\
6369the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006370
6371static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006372posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006373{
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 WAIT_TYPE status;
6375 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006376
Victor Stinner8c62be82010-05-06 00:08:46 +00006377 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6378 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006379
Victor Stinner8c62be82010-05-06 00:08:46 +00006380 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006381}
6382#endif /* WSTOPSIG */
6383
6384#endif /* HAVE_SYS_WAIT_H */
6385
6386
Thomas Wouters477c8d52006-05-27 19:21:47 +00006387#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006388#ifdef _SCO_DS
6389/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6390 needed definitions in sys/statvfs.h */
6391#define _SVID3
6392#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006393#include <sys/statvfs.h>
6394
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006395static PyObject*
6396_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006397 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6398 if (v == NULL)
6399 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006400
6401#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6403 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6404 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6405 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6406 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6407 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6408 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6409 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6410 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6411 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006412#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6414 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6415 PyStructSequence_SET_ITEM(v, 2,
6416 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6417 PyStructSequence_SET_ITEM(v, 3,
6418 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6419 PyStructSequence_SET_ITEM(v, 4,
6420 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6421 PyStructSequence_SET_ITEM(v, 5,
6422 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6423 PyStructSequence_SET_ITEM(v, 6,
6424 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6425 PyStructSequence_SET_ITEM(v, 7,
6426 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6427 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6428 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006429#endif
6430
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006432}
6433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006435"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006436Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006437
6438static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006439posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006440{
Victor Stinner8c62be82010-05-06 00:08:46 +00006441 int fd, res;
6442 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006443
Victor Stinner8c62be82010-05-06 00:08:46 +00006444 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6445 return NULL;
6446 Py_BEGIN_ALLOW_THREADS
6447 res = fstatvfs(fd, &st);
6448 Py_END_ALLOW_THREADS
6449 if (res != 0)
6450 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006451
Victor Stinner8c62be82010-05-06 00:08:46 +00006452 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006453}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006454#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006455
6456
Thomas Wouters477c8d52006-05-27 19:21:47 +00006457#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006458#include <sys/statvfs.h>
6459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006460PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006461"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006462Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006463
6464static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006465posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006466{
Victor Stinnere4110dc2013-01-01 23:05:55 +01006467 PyObject *opath, *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006468 char *path;
6469 int res;
6470 struct statvfs st;
Victor Stinnere4110dc2013-01-01 23:05:55 +01006471 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &opath))
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 return NULL;
Victor Stinnere4110dc2013-01-01 23:05:55 +01006473 path = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006474 Py_BEGIN_ALLOW_THREADS
6475 res = statvfs(path, &st);
6476 Py_END_ALLOW_THREADS
6477 if (res != 0)
Victor Stinnere4110dc2013-01-01 23:05:55 +01006478 return posix_error_with_allocated_filename(opath);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006479
Victor Stinnere4110dc2013-01-01 23:05:55 +01006480 result = _pystatvfs_fromstructstatvfs(st);
6481 Py_DECREF(opath);
6482 return result;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006483}
6484#endif /* HAVE_STATVFS */
6485
Fred Drakec9680921999-12-13 16:37:25 +00006486/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6487 * It maps strings representing configuration variable names to
6488 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006489 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006490 * rarely-used constants. There are three separate tables that use
6491 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006492 *
6493 * This code is always included, even if none of the interfaces that
6494 * need it are included. The #if hackery needed to avoid it would be
6495 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006496 */
6497struct constdef {
6498 char *name;
6499 long value;
6500};
6501
Fred Drake12c6e2d1999-12-14 21:25:03 +00006502static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006503conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006504 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006505{
Christian Heimes217cfd12007-12-02 14:31:20 +00006506 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006507 *valuep = PyLong_AS_LONG(arg);
6508 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006509 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006510 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006511 /* look up the value in the table using a binary search */
6512 size_t lo = 0;
6513 size_t mid;
6514 size_t hi = tablesize;
6515 int cmp;
6516 const char *confname;
6517 if (!PyUnicode_Check(arg)) {
6518 PyErr_SetString(PyExc_TypeError,
6519 "configuration names must be strings or integers");
6520 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006522 confname = _PyUnicode_AsString(arg);
6523 if (confname == NULL)
6524 return 0;
6525 while (lo < hi) {
6526 mid = (lo + hi) / 2;
6527 cmp = strcmp(confname, table[mid].name);
6528 if (cmp < 0)
6529 hi = mid;
6530 else if (cmp > 0)
6531 lo = mid + 1;
6532 else {
6533 *valuep = table[mid].value;
6534 return 1;
6535 }
6536 }
6537 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6538 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006540}
6541
6542
6543#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6544static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006545#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006546 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006547#endif
6548#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006549 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006550#endif
Fred Drakec9680921999-12-13 16:37:25 +00006551#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006552 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006553#endif
6554#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006555 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006556#endif
6557#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006558 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006559#endif
6560#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006561 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006562#endif
6563#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006564 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006565#endif
6566#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006567 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006568#endif
6569#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006570 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006571#endif
6572#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006573 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006574#endif
6575#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006577#endif
6578#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006580#endif
6581#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006583#endif
6584#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006585 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006586#endif
6587#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006588 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006589#endif
6590#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006591 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006592#endif
6593#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006595#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006596#ifdef _PC_ACL_ENABLED
6597 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6598#endif
6599#ifdef _PC_MIN_HOLE_SIZE
6600 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6601#endif
6602#ifdef _PC_ALLOC_SIZE_MIN
6603 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6604#endif
6605#ifdef _PC_REC_INCR_XFER_SIZE
6606 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6607#endif
6608#ifdef _PC_REC_MAX_XFER_SIZE
6609 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6610#endif
6611#ifdef _PC_REC_MIN_XFER_SIZE
6612 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6613#endif
6614#ifdef _PC_REC_XFER_ALIGN
6615 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6616#endif
6617#ifdef _PC_SYMLINK_MAX
6618 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6619#endif
6620#ifdef _PC_XATTR_ENABLED
6621 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6622#endif
6623#ifdef _PC_XATTR_EXISTS
6624 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6625#endif
6626#ifdef _PC_TIMESTAMP_RESOLUTION
6627 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6628#endif
Fred Drakec9680921999-12-13 16:37:25 +00006629};
6630
Fred Drakec9680921999-12-13 16:37:25 +00006631static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006632conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006633{
6634 return conv_confname(arg, valuep, posix_constants_pathconf,
6635 sizeof(posix_constants_pathconf)
6636 / sizeof(struct constdef));
6637}
6638#endif
6639
6640#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006641PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006642"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006643Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006644If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006645
6646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006647posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006648{
6649 PyObject *result = NULL;
6650 int name, fd;
6651
Fred Drake12c6e2d1999-12-14 21:25:03 +00006652 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6653 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006654 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006655
Stefan Krah0e803b32010-11-26 16:16:47 +00006656 errno = 0;
6657 limit = fpathconf(fd, name);
6658 if (limit == -1 && errno != 0)
6659 posix_error();
6660 else
6661 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006662 }
6663 return result;
6664}
6665#endif
6666
6667
6668#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006669PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006670"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006671Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006672If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006673
6674static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006675posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006676{
6677 PyObject *result = NULL;
6678 int name;
6679 char *path;
6680
6681 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6682 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006684
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 errno = 0;
6686 limit = pathconf(path, name);
6687 if (limit == -1 && errno != 0) {
6688 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006689 /* could be a path or name problem */
6690 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006691 else
Stefan Krah99439262010-11-26 12:58:05 +00006692 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 }
6694 else
6695 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006696 }
6697 return result;
6698}
6699#endif
6700
6701#ifdef HAVE_CONFSTR
6702static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006703#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006705#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006706#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006707 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006708#endif
6709#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006710 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006711#endif
Fred Draked86ed291999-12-15 15:34:33 +00006712#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006713 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006714#endif
6715#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006716 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006717#endif
6718#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006719 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006720#endif
6721#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006723#endif
Fred Drakec9680921999-12-13 16:37:25 +00006724#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006726#endif
6727#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006729#endif
6730#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006731 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006732#endif
6733#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006734 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006735#endif
6736#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006737 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006738#endif
6739#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006740 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006741#endif
6742#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006743 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006744#endif
6745#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006746 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006747#endif
Fred Draked86ed291999-12-15 15:34:33 +00006748#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006749 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006750#endif
Fred Drakec9680921999-12-13 16:37:25 +00006751#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006752 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006753#endif
Fred Draked86ed291999-12-15 15:34:33 +00006754#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006755 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006756#endif
6757#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006758 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006759#endif
6760#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006762#endif
6763#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006764 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006765#endif
Fred Drakec9680921999-12-13 16:37:25 +00006766#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006767 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006768#endif
6769#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006771#endif
6772#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006774#endif
6775#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006776 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006777#endif
6778#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006779 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006780#endif
6781#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006782 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006783#endif
6784#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006785 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006786#endif
6787#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006789#endif
6790#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006792#endif
6793#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006795#endif
6796#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006797 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006798#endif
6799#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006801#endif
6802#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006803 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006804#endif
6805#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006807#endif
6808#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006810#endif
6811#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006812 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006813#endif
Fred Draked86ed291999-12-15 15:34:33 +00006814#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006815 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006816#endif
6817#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006819#endif
6820#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006821 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006822#endif
6823#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006825#endif
6826#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006827 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006828#endif
6829#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006830 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006831#endif
6832#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006833 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006834#endif
6835#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006836 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006837#endif
6838#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006839 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006840#endif
6841#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006842 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006843#endif
6844#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006845 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006846#endif
6847#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006848 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006849#endif
6850#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006852#endif
Fred Drakec9680921999-12-13 16:37:25 +00006853};
6854
6855static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006856conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006857{
6858 return conv_confname(arg, valuep, posix_constants_confstr,
6859 sizeof(posix_constants_confstr)
6860 / sizeof(struct constdef));
6861}
6862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006863PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006864"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006865Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006866
6867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006868posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006869{
6870 PyObject *result = NULL;
6871 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006872 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006873 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006874
Victor Stinnercb043522010-09-10 23:49:04 +00006875 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6876 return NULL;
6877
6878 errno = 0;
6879 len = confstr(name, buffer, sizeof(buffer));
6880 if (len == 0) {
6881 if (errno) {
6882 posix_error();
6883 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006884 }
6885 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006886 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006887 }
6888 }
Victor Stinnercb043522010-09-10 23:49:04 +00006889
6890 if ((unsigned int)len >= sizeof(buffer)) {
6891 char *buf = PyMem_Malloc(len);
6892 if (buf == NULL)
6893 return PyErr_NoMemory();
6894 confstr(name, buf, len);
6895 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6896 PyMem_Free(buf);
6897 }
6898 else
6899 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006900 return result;
6901}
6902#endif
6903
6904
6905#ifdef HAVE_SYSCONF
6906static struct constdef posix_constants_sysconf[] = {
6907#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006909#endif
6910#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006912#endif
6913#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006915#endif
6916#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006917 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006918#endif
6919#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006921#endif
6922#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006924#endif
6925#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006926 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006927#endif
6928#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006930#endif
6931#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006932 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006933#endif
6934#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006935 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006936#endif
Fred Draked86ed291999-12-15 15:34:33 +00006937#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006938 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006939#endif
6940#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006941 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006942#endif
Fred Drakec9680921999-12-13 16:37:25 +00006943#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006944 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006945#endif
Fred Drakec9680921999-12-13 16:37:25 +00006946#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006947 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006948#endif
6949#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006950 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006951#endif
6952#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006953 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006954#endif
6955#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006957#endif
6958#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006959 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006960#endif
Fred Draked86ed291999-12-15 15:34:33 +00006961#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006962 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006963#endif
Fred Drakec9680921999-12-13 16:37:25 +00006964#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006965 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006966#endif
6967#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006968 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006969#endif
6970#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006971 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006972#endif
6973#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006974 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006975#endif
6976#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006977 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006978#endif
Fred Draked86ed291999-12-15 15:34:33 +00006979#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006980 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006981#endif
Fred Drakec9680921999-12-13 16:37:25 +00006982#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006983 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006984#endif
6985#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006987#endif
6988#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006989 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006990#endif
6991#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006992 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006993#endif
6994#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006996#endif
6997#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006998 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006999#endif
7000#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007001 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007002#endif
7003#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007004 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007005#endif
7006#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007007 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007008#endif
7009#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007010 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007011#endif
7012#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007013 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007014#endif
7015#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007016 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007017#endif
7018#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007019 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007020#endif
7021#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007022 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007023#endif
7024#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007026#endif
7027#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007029#endif
7030#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007031 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007032#endif
7033#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007034 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007035#endif
7036#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007037 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007038#endif
7039#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007040 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007041#endif
7042#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007044#endif
7045#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007046 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007047#endif
7048#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007049 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007050#endif
Fred Draked86ed291999-12-15 15:34:33 +00007051#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007053#endif
Fred Drakec9680921999-12-13 16:37:25 +00007054#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007055 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007056#endif
7057#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007059#endif
7060#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007061 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007062#endif
Fred Draked86ed291999-12-15 15:34:33 +00007063#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007064 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007065#endif
Fred Drakec9680921999-12-13 16:37:25 +00007066#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007068#endif
Fred Draked86ed291999-12-15 15:34:33 +00007069#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007070 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007071#endif
7072#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007074#endif
Fred Drakec9680921999-12-13 16:37:25 +00007075#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007076 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007077#endif
7078#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007079 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007080#endif
7081#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007082 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007083#endif
7084#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007085 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007086#endif
Fred Draked86ed291999-12-15 15:34:33 +00007087#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007088 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007089#endif
Fred Drakec9680921999-12-13 16:37:25 +00007090#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007091 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007092#endif
7093#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007095#endif
7096#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007097 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007098#endif
7099#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007100 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007101#endif
7102#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007103 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007104#endif
7105#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007106 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007107#endif
7108#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007109 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007110#endif
Fred Draked86ed291999-12-15 15:34:33 +00007111#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007113#endif
Fred Drakec9680921999-12-13 16:37:25 +00007114#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007116#endif
7117#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007118 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007119#endif
Fred Draked86ed291999-12-15 15:34:33 +00007120#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007121 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007122#endif
Fred Drakec9680921999-12-13 16:37:25 +00007123#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007124 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007125#endif
7126#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007127 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007128#endif
7129#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007130 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007131#endif
7132#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007133 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007134#endif
7135#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007136 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007137#endif
7138#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007139 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007140#endif
7141#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007142 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007143#endif
7144#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007145 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007146#endif
7147#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007148 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007149#endif
Fred Draked86ed291999-12-15 15:34:33 +00007150#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007151 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007152#endif
7153#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007155#endif
Fred Drakec9680921999-12-13 16:37:25 +00007156#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007157 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007158#endif
7159#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007161#endif
7162#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007163 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007164#endif
7165#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007166 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007167#endif
7168#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007169 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007170#endif
7171#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007172 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007173#endif
7174#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007175 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007176#endif
7177#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007178 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007179#endif
7180#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007182#endif
7183#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007184 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007185#endif
7186#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007188#endif
7189#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007191#endif
7192#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007194#endif
7195#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007197#endif
7198#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007200#endif
7201#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007202 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007203#endif
7204#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007206#endif
7207#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007209#endif
7210#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007212#endif
7213#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007215#endif
7216#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007217 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007218#endif
7219#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007221#endif
7222#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007224#endif
7225#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007226 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007227#endif
7228#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007229 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007230#endif
7231#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007233#endif
7234#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007236#endif
7237#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007239#endif
7240#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007242#endif
7243#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007244 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007245#endif
7246#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007248#endif
7249#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007250 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007251#endif
7252#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007254#endif
7255#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007256 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007257#endif
7258#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007260#endif
Fred Draked86ed291999-12-15 15:34:33 +00007261#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007262 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007263#endif
Fred Drakec9680921999-12-13 16:37:25 +00007264#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007266#endif
7267#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007269#endif
7270#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007272#endif
7273#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007274 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007275#endif
7276#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007278#endif
7279#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007280 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007281#endif
7282#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007283 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007284#endif
7285#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007286 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007287#endif
7288#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007289 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007290#endif
7291#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007292 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007293#endif
7294#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007295 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007296#endif
7297#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007298 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007299#endif
7300#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007301 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007302#endif
7303#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007304 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007305#endif
7306#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007308#endif
7309#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007311#endif
7312#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007313 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007314#endif
7315#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007316 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007317#endif
7318#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007319 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007320#endif
7321#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007322 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007323#endif
7324#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007325 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007326#endif
7327#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007328 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007329#endif
7330#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007331 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007332#endif
7333#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007334 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007335#endif
7336#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007337 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007338#endif
7339#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007340 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007341#endif
7342#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007344#endif
7345#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007347#endif
7348#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007350#endif
7351#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007353#endif
7354#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007356#endif
7357#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007359#endif
7360#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007361 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007362#endif
7363#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007365#endif
7366#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007368#endif
7369#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007370 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007371#endif
7372#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007373 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007374#endif
7375#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007377#endif
7378#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007379 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007380#endif
7381#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007383#endif
7384#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007386#endif
7387#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007388 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007389#endif
7390#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007391 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007392#endif
7393#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007394 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007395#endif
7396#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007397 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007398#endif
7399};
7400
7401static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007402conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007403{
7404 return conv_confname(arg, valuep, posix_constants_sysconf,
7405 sizeof(posix_constants_sysconf)
7406 / sizeof(struct constdef));
7407}
7408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007409PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007410"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007411Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007412
7413static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007414posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007415{
7416 PyObject *result = NULL;
7417 int name;
7418
7419 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7420 int value;
7421
7422 errno = 0;
7423 value = sysconf(name);
7424 if (value == -1 && errno != 0)
7425 posix_error();
7426 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007427 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007428 }
7429 return result;
7430}
7431#endif
7432
7433
Fred Drakebec628d1999-12-15 18:31:10 +00007434/* This code is used to ensure that the tables of configuration value names
7435 * are in sorted order as required by conv_confname(), and also to build the
7436 * the exported dictionaries that are used to publish information about the
7437 * names available on the host platform.
7438 *
7439 * Sorting the table at runtime ensures that the table is properly ordered
7440 * when used, even for platforms we're not able to test on. It also makes
7441 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007442 */
Fred Drakebec628d1999-12-15 18:31:10 +00007443
7444static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007445cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007446{
7447 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007448 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007449 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007451
7452 return strcmp(c1->name, c2->name);
7453}
7454
7455static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007456setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007457 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007458{
Fred Drakebec628d1999-12-15 18:31:10 +00007459 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007460 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007461
7462 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7463 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007464 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007466
Barry Warsaw3155db32000-04-13 15:20:40 +00007467 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 PyObject *o = PyLong_FromLong(table[i].value);
7469 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7470 Py_XDECREF(o);
7471 Py_DECREF(d);
7472 return -1;
7473 }
7474 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007475 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007476 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007477}
7478
Fred Drakebec628d1999-12-15 18:31:10 +00007479/* Return -1 on failure, 0 on success. */
7480static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007481setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007482{
7483#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007484 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007485 sizeof(posix_constants_pathconf)
7486 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007487 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007488 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007489#endif
7490#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007491 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007492 sizeof(posix_constants_confstr)
7493 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007494 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007495 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007496#endif
7497#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007498 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007499 sizeof(posix_constants_sysconf)
7500 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007501 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007502 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007503#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007504 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007505}
Fred Draked86ed291999-12-15 15:34:33 +00007506
7507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007508PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007509"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007510Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007511in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007512
7513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007514posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007515{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007516 abort();
7517 /*NOTREACHED*/
7518 Py_FatalError("abort() called from Python code didn't abort!");
7519 return NULL;
7520}
Fred Drakebec628d1999-12-15 18:31:10 +00007521
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007522#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007523PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007524"startfile(filepath [, operation]) - Start a file with its associated\n\
7525application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007526\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007527When \"operation\" is not specified or \"open\", this acts like\n\
7528double-clicking the file in Explorer, or giving the file name as an\n\
7529argument to the DOS \"start\" command: the file is opened with whatever\n\
7530application (if any) its extension is associated.\n\
7531When another \"operation\" is given, it specifies what should be done with\n\
7532the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007533\n\
7534startfile returns as soon as the associated application is launched.\n\
7535There is no option to wait for the application to close, and no way\n\
7536to retrieve the application's exit status.\n\
7537\n\
7538The filepath is relative to the current directory. If you want to use\n\
7539an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007540the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007541
7542static PyObject *
7543win32_startfile(PyObject *self, PyObject *args)
7544{
Victor Stinner8c62be82010-05-06 00:08:46 +00007545 PyObject *ofilepath;
7546 char *filepath;
7547 char *operation = NULL;
7548 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007549
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 PyObject *unipath, *woperation = NULL;
7551 if (!PyArg_ParseTuple(args, "U|s:startfile",
7552 &unipath, &operation)) {
7553 PyErr_Clear();
7554 goto normal;
7555 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007556
Victor Stinner8c62be82010-05-06 00:08:46 +00007557 if (operation) {
7558 woperation = PyUnicode_DecodeASCII(operation,
7559 strlen(operation), NULL);
7560 if (!woperation) {
7561 PyErr_Clear();
7562 operation = NULL;
7563 goto normal;
7564 }
7565 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007566
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 Py_BEGIN_ALLOW_THREADS
7568 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7569 PyUnicode_AS_UNICODE(unipath),
7570 NULL, NULL, SW_SHOWNORMAL);
7571 Py_END_ALLOW_THREADS
7572
7573 Py_XDECREF(woperation);
7574 if (rc <= (HINSTANCE)32) {
7575 PyObject *errval = win32_error_unicode("startfile",
7576 PyUnicode_AS_UNICODE(unipath));
7577 return errval;
7578 }
7579 Py_INCREF(Py_None);
7580 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007581
7582normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007583 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7584 PyUnicode_FSConverter, &ofilepath,
7585 &operation))
7586 return NULL;
7587 filepath = PyBytes_AsString(ofilepath);
7588 Py_BEGIN_ALLOW_THREADS
7589 rc = ShellExecute((HWND)0, operation, filepath,
7590 NULL, NULL, SW_SHOWNORMAL);
7591 Py_END_ALLOW_THREADS
7592 if (rc <= (HINSTANCE)32) {
7593 PyObject *errval = win32_error("startfile", filepath);
7594 Py_DECREF(ofilepath);
7595 return errval;
7596 }
7597 Py_DECREF(ofilepath);
7598 Py_INCREF(Py_None);
7599 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007600}
7601#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007602
Martin v. Löwis438b5342002-12-27 10:16:42 +00007603#ifdef HAVE_GETLOADAVG
7604PyDoc_STRVAR(posix_getloadavg__doc__,
7605"getloadavg() -> (float, float, float)\n\n\
7606Return the number of processes in the system run queue averaged over\n\
7607the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7608was unobtainable");
7609
7610static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007611posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007612{
7613 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007614 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007615 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7616 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007617 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007618 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007619}
7620#endif
7621
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007622PyDoc_STRVAR(device_encoding__doc__,
7623"device_encoding(fd) -> str\n\n\
7624Return a string describing the encoding of the device\n\
7625if the output is a terminal; else return None.");
7626
7627static PyObject *
7628device_encoding(PyObject *self, PyObject *args)
7629{
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 int fd;
7631 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7632 return NULL;
7633 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7634 Py_INCREF(Py_None);
7635 return Py_None;
7636 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007637#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007638 if (fd == 0) {
7639 char buf[100];
7640 sprintf(buf, "cp%d", GetConsoleCP());
7641 return PyUnicode_FromString(buf);
7642 }
7643 if (fd == 1 || fd == 2) {
7644 char buf[100];
7645 sprintf(buf, "cp%d", GetConsoleOutputCP());
7646 return PyUnicode_FromString(buf);
7647 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007648#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 {
7650 char *codeset = nl_langinfo(CODESET);
7651 if (codeset != NULL && codeset[0] != 0)
7652 return PyUnicode_FromString(codeset);
7653 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007654#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 Py_INCREF(Py_None);
7656 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007657}
7658
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007659PyDoc_STRVAR(posix_urandom__doc__,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007660"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007661Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007662
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007663static PyObject *
7664posix_urandom(PyObject *self, PyObject *args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007665{
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007666 Py_ssize_t size;
7667 PyObject *result;
7668 int ret;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007669
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007670 /* Read arguments */
7671 if (!PyArg_ParseTuple(args, "n:urandom", &size))
Victor Stinner8c62be82010-05-06 00:08:46 +00007672 return NULL;
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007673 if (size < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007674 return PyErr_Format(PyExc_ValueError,
7675 "negative argument not allowed");
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007676 result = PyBytes_FromStringAndSize(NULL, size);
7677 if (result == NULL)
7678 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007679
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007680 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
7681 PyBytes_GET_SIZE(result));
7682 if (ret == -1) {
7683 Py_DECREF(result);
7684 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007685 }
7686 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007687}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007688
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007689#ifdef HAVE_SETRESUID
7690PyDoc_STRVAR(posix_setresuid__doc__,
7691"setresuid(ruid, euid, suid)\n\n\
7692Set the current process's real, effective, and saved user ids.");
7693
7694static PyObject*
7695posix_setresuid (PyObject *self, PyObject *args)
7696{
Victor Stinner8c62be82010-05-06 00:08:46 +00007697 /* We assume uid_t is no larger than a long. */
7698 long ruid, euid, suid;
7699 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7700 return NULL;
7701 if (setresuid(ruid, euid, suid) < 0)
7702 return posix_error();
7703 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007704}
7705#endif
7706
7707#ifdef HAVE_SETRESGID
7708PyDoc_STRVAR(posix_setresgid__doc__,
7709"setresgid(rgid, egid, sgid)\n\n\
7710Set the current process's real, effective, and saved group ids.");
7711
7712static PyObject*
7713posix_setresgid (PyObject *self, PyObject *args)
7714{
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 /* We assume uid_t is no larger than a long. */
7716 long rgid, egid, sgid;
7717 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7718 return NULL;
7719 if (setresgid(rgid, egid, sgid) < 0)
7720 return posix_error();
7721 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007722}
7723#endif
7724
7725#ifdef HAVE_GETRESUID
7726PyDoc_STRVAR(posix_getresuid__doc__,
7727"getresuid() -> (ruid, euid, suid)\n\n\
7728Get tuple of the current process's real, effective, and saved user ids.");
7729
7730static PyObject*
7731posix_getresuid (PyObject *self, PyObject *noargs)
7732{
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 uid_t ruid, euid, suid;
7734 long l_ruid, l_euid, l_suid;
7735 if (getresuid(&ruid, &euid, &suid) < 0)
7736 return posix_error();
7737 /* Force the values into long's as we don't know the size of uid_t. */
7738 l_ruid = ruid;
7739 l_euid = euid;
7740 l_suid = suid;
7741 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007742}
7743#endif
7744
7745#ifdef HAVE_GETRESGID
7746PyDoc_STRVAR(posix_getresgid__doc__,
7747"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007748Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007749
7750static PyObject*
7751posix_getresgid (PyObject *self, PyObject *noargs)
7752{
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 uid_t rgid, egid, sgid;
7754 long l_rgid, l_egid, l_sgid;
7755 if (getresgid(&rgid, &egid, &sgid) < 0)
7756 return posix_error();
7757 /* Force the values into long's as we don't know the size of uid_t. */
7758 l_rgid = rgid;
7759 l_egid = egid;
7760 l_sgid = sgid;
7761 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007762}
7763#endif
7764
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007765static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007767#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007768 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007769#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007771#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007773#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007775#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007777#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007778#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007780#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007781#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007783#endif /* HAVE_LCHMOD */
7784#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007786#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007787#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007789#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007790#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007791 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007792#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007793#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007795#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007796#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007798#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007799#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7801 METH_NOARGS, posix_getcwd__doc__},
7802 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7803 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007804#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007805#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007807#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7809 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7810 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007811#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007813#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007814#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007816#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007817#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007818 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007819#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007820 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7821 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7822 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007824#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007826#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007827#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007828 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007829 win_symlink__doc__},
7830#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007831#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007833#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007835#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007837#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7839 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7840 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007841#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007843#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007845#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007846 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7847 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007848#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007849#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7851 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007852#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7854 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007855#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007856#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007857#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007859#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007860#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007862#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007863#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007865#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007866#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007868#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007869#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007871#endif /* HAVE_GETEGID */
7872#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007874#endif /* HAVE_GETEUID */
7875#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007877#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007878#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007882#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007884#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007885#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007887#endif /* HAVE_GETPPID */
7888#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007890#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007891#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007893#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007894#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007896#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007897#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007899#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007900#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007902#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007903#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7905 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007906 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007907#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007908#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007910#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007911#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007913#endif /* HAVE_SETEUID */
7914#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007916#endif /* HAVE_SETEGID */
7917#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007919#endif /* HAVE_SETREUID */
7920#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007922#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007923#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007925#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007926#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007928#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007929#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007931#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007932#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007933 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007934#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007935#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007937#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007938#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007940#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007941#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007943#endif /* HAVE_WAIT3 */
7944#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007946#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007947#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007949#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007950#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007952#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007953#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007955#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007956#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007958#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007959#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007961#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007962#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007964#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7966 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7967 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7968 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7969 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7970 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7971 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7972 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7973 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7974 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7975 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007976#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007978#endif
7979#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007981#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007982#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007984#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007985#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007986 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7987 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7988 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007989#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007990#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007992#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007993#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007995#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007996#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007998#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008000#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008002#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008003#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008005#endif
8006#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008008#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008009#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008010#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008011 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008012#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008013#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008015#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008016#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008018#endif /* WIFSTOPPED */
8019#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008021#endif /* WIFSIGNALED */
8022#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008024#endif /* WIFEXITED */
8025#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008027#endif /* WEXITSTATUS */
8028#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008030#endif /* WTERMSIG */
8031#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008033#endif /* WSTOPSIG */
8034#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008035#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008037#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008038#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008040#endif
Fred Drakec9680921999-12-13 16:37:25 +00008041#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008054#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008055 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008056 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008057 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -05008058 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00008059#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008060#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008062#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +01008063 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008064#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008066#endif
8067#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008069#endif
8070#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008072#endif
8073#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008075#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008077};
8078
8079
Barry Warsaw4a342091996-12-19 23:50:02 +00008080static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008081ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008082{
Victor Stinner8c62be82010-05-06 00:08:46 +00008083 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008084}
8085
Guido van Rossumd48f2521997-12-05 22:19:34 +00008086#if defined(PYOS_OS2)
8087/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008088static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008089{
8090 APIRET rc;
8091 ULONG values[QSV_MAX+1];
8092 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008093 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008094
8095 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008096 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008097 Py_END_ALLOW_THREADS
8098
8099 if (rc != NO_ERROR) {
8100 os2_error(rc);
8101 return -1;
8102 }
8103
Fred Drake4d1e64b2002-04-15 19:40:07 +00008104 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8105 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8106 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8107 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8108 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8109 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8110 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008111
8112 switch (values[QSV_VERSION_MINOR]) {
8113 case 0: ver = "2.00"; break;
8114 case 10: ver = "2.10"; break;
8115 case 11: ver = "2.11"; break;
8116 case 30: ver = "3.00"; break;
8117 case 40: ver = "4.00"; break;
8118 case 50: ver = "5.00"; break;
8119 default:
Tim Peters885d4572001-11-28 20:27:42 +00008120 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008121 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008122 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008123 ver = &tmp[0];
8124 }
8125
8126 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008127 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008128 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008129
8130 /* Add Indicator of Which Drive was Used to Boot the System */
8131 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8132 tmp[1] = ':';
8133 tmp[2] = '\0';
8134
Fred Drake4d1e64b2002-04-15 19:40:07 +00008135 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008136}
8137#endif
8138
Brian Curtin52173d42010-12-02 18:29:18 +00008139#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008140static int
Brian Curtin52173d42010-12-02 18:29:18 +00008141enable_symlink()
8142{
8143 HANDLE tok;
8144 TOKEN_PRIVILEGES tok_priv;
8145 LUID luid;
8146 int meth_idx = 0;
8147
8148 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008149 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008150
8151 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008152 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008153
8154 tok_priv.PrivilegeCount = 1;
8155 tok_priv.Privileges[0].Luid = luid;
8156 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8157
8158 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8159 sizeof(TOKEN_PRIVILEGES),
8160 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008161 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008162
Brian Curtin3b4499c2010-12-28 14:31:47 +00008163 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8164 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008165}
8166#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8167
Barry Warsaw4a342091996-12-19 23:50:02 +00008168static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008169all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008170{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008171#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008172 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008173#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008174#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008176#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008177#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008178 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008179#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008180#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008181 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008182#endif
Fred Drakec9680921999-12-13 16:37:25 +00008183#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008184 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008185#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008186#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008187 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008188#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008189#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008191#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008192#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008193 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008194#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008195#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008197#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008198#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008199 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008200#endif
8201#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008202 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008203#endif
8204#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008206#endif
8207#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008209#endif
8210#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008211 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008212#endif
8213#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008215#endif
8216#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008218#endif
8219#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008221#endif
8222#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008224#endif
8225#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008227#endif
8228#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008230#endif
8231#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008233#endif
8234#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008236#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008237#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008239#endif
8240#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008242#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008243#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008245#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008246#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008248#endif
8249#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008251#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008252
Tim Peters5aa91602002-01-30 05:46:57 +00008253/* MS Windows */
8254#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008255 /* Don't inherit in child processes. */
8256 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008257#endif
8258#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 /* Optimize for short life (keep in memory). */
8260 /* MS forgot to define this one with a non-underscore form too. */
8261 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008262#endif
8263#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 /* Automatically delete when last handle is closed. */
8265 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008266#endif
8267#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 /* Optimize for random access. */
8269 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008270#endif
8271#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 /* Optimize for sequential access. */
8273 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008274#endif
8275
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008276/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008277#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 /* Send a SIGIO signal whenever input or output
8279 becomes available on file descriptor */
8280 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008281#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008282#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 /* Direct disk access. */
8284 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008285#endif
8286#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 /* Must be a directory. */
8288 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008289#endif
8290#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008291 /* Do not follow links. */
8292 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008293#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008294#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 /* Do not update the access time. */
8296 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008297#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008298
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008300#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008302#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008303#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008305#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008306#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008308#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008309#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008311#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008312#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008314#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008315#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008317#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008318#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008320#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008321#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008323#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008324#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008326#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008327#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008329#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008330#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008332#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008333#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008335#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008336#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008338#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008339#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008341#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008342#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008344#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008345#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008347#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008348#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008350#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008351
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008352 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008353#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008354 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008355#endif /* ST_RDONLY */
8356#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008357 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008358#endif /* ST_NOSUID */
8359
Guido van Rossum246bc171999-02-01 23:54:31 +00008360#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008361#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8363 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8364 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8365 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8366 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8367 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8368 if (ins(d, "P_PM", (long)P_PM)) return -1;
8369 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8370 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8371 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8372 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8373 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8374 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8375 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8376 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8377 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8378 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8379 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8380 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8381 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008382#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8384 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8385 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8386 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8387 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008388#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008389#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008390
Guido van Rossumd48f2521997-12-05 22:19:34 +00008391#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008392 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008393#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008395}
8396
8397
Tim Peters5aa91602002-01-30 05:46:57 +00008398#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008399#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008400#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008401
8402#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008403#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008404#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008405
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008406#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008407#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008408#define MODNAME "posix"
8409#endif
8410
Martin v. Löwis1a214512008-06-11 05:26:20 +00008411static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 PyModuleDef_HEAD_INIT,
8413 MODNAME,
8414 posix__doc__,
8415 -1,
8416 posix_methods,
8417 NULL,
8418 NULL,
8419 NULL,
8420 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008421};
8422
8423
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008424PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008425INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008426{
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008428
Brian Curtin52173d42010-12-02 18:29:18 +00008429#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008430 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008431#endif
8432
Victor Stinner8c62be82010-05-06 00:08:46 +00008433 m = PyModule_Create(&posixmodule);
8434 if (m == NULL)
8435 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008436
Victor Stinner8c62be82010-05-06 00:08:46 +00008437 /* Initialize environ dictionary */
8438 v = convertenviron();
8439 Py_XINCREF(v);
8440 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8441 return NULL;
8442 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008443
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 if (all_ins(m))
8445 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008446
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 if (setup_confname_tables(m))
8448 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008449
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 Py_INCREF(PyExc_OSError);
8451 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008452
Guido van Rossumb3d39562000-01-31 18:41:26 +00008453#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008454 if (posix_putenv_garbage == NULL)
8455 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008456#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008457
Victor Stinner8c62be82010-05-06 00:08:46 +00008458 if (!initialized) {
8459 stat_result_desc.name = MODNAME ".stat_result";
8460 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8461 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8462 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8463 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8464 structseq_new = StatResultType.tp_new;
8465 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008466
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 statvfs_result_desc.name = MODNAME ".statvfs_result";
8468 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008469#ifdef NEED_TICKS_PER_SECOND
8470# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008472# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008474# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008476# endif
8477#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 }
8479 Py_INCREF((PyObject*) &StatResultType);
8480 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8481 Py_INCREF((PyObject*) &StatVFSResultType);
8482 PyModule_AddObject(m, "statvfs_result",
8483 (PyObject*) &StatVFSResultType);
8484 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008485
8486#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008487 /*
8488 * Step 2 of weak-linking support on Mac OS X.
8489 *
8490 * The code below removes functions that are not available on the
8491 * currently active platform.
8492 *
8493 * This block allow one to use a python binary that was build on
8494 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8495 * OSX 10.4.
8496 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008497#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008498 if (fstatvfs == NULL) {
8499 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8500 return NULL;
8501 }
8502 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008503#endif /* HAVE_FSTATVFS */
8504
8505#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 if (statvfs == NULL) {
8507 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8508 return NULL;
8509 }
8510 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008511#endif /* HAVE_STATVFS */
8512
8513# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008514 if (lchown == NULL) {
8515 if (PyObject_DelAttrString(m, "lchown") == -1) {
8516 return NULL;
8517 }
8518 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008519#endif /* HAVE_LCHOWN */
8520
8521
8522#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008523 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008524
Guido van Rossumb6775db1994-08-01 11:34:53 +00008525}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008526
8527#ifdef __cplusplus
8528}
8529#endif