blob: b6f49b81e6cc623b312b18a3345f7000c0491a5b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000099/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#include <process.h>
102#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#define HAVE_GETCWD 1
105#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000106#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#if defined(__OS2__)
108#define HAVE_EXECV 1
109#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000110#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#include <process.h>
112#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000113#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_EXECV 1
115#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000118#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_WAIT 1
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000123#define HAVE_GETPPID 1
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000124#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000132#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV 1
138#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#define HAVE_FORK1 1
141#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_GETCWD 1
143#define HAVE_GETEGID 1
144#define HAVE_GETEUID 1
145#define HAVE_GETGID 1
146#define HAVE_GETPPID 1
147#define HAVE_GETUID 1
148#define HAVE_KILL 1
149#define HAVE_OPENDIR 1
150#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000205#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_UTIME_H
214#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_SYS_TIMES_H
223#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
226#ifdef HAVE_SYS_PARAM_H
227#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
230#ifdef HAVE_SYS_UTSNAME_H
231#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000232#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) strlen((dirent)->d_name)
237#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000238#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#include <direct.h>
240#define NAMLEN(dirent) strlen((dirent)->d_name)
241#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
260#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262#endif
263#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000267#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000268#endif
269#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000270#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000274#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000276#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000278#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000279#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000282static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000283#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285
Guido van Rossumd48f2521997-12-05 22:19:34 +0000286#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000288#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000291#if defined(PATH_MAX) && PATH_MAX > 1024
292#define MAXPATHLEN PATH_MAX
293#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000294#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000295#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000296#endif /* MAXPATHLEN */
297
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000298#ifdef UNION_WAIT
299/* Emulate some macros on systems that have a union instead of macros */
300
301#ifndef WIFEXITED
302#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
303#endif
304
305#ifndef WEXITSTATUS
306#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
307#endif
308
309#ifndef WTERMSIG
310#define WTERMSIG(u_wait) ((u_wait).w_termsig)
311#endif
312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313#define WAIT_TYPE union wait
314#define WAIT_STATUS_INT(s) (s.w_status)
315
316#else /* !UNION_WAIT */
317#define WAIT_TYPE int
318#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000319#endif /* UNION_WAIT */
320
Greg Wardb48bc172000-03-01 21:51:56 +0000321/* Don't use the "_r" form if we don't need it (also, won't have a
322 prototype for it, at least on Solaris -- maybe others as well?). */
323#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
324#define USE_CTERMID_R
325#endif
326
Fred Drake699f3522000-06-29 21:12:41 +0000327/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000328#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000329#undef FSTAT
330#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000331#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000332# define STAT win32_stat
333# define FSTAT win32_fstat
334# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000335#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000336# define STAT stat
337# define FSTAT fstat
338# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000339#endif
340
Tim Peters11b23062003-04-23 02:39:17 +0000341#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#include <sys/mkdev.h>
343#else
344#if defined(MAJOR_IN_SYSMACROS)
345#include <sys/sysmacros.h>
346#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000347#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
348#include <sys/mkdev.h>
349#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000350#endif
Fred Drake699f3522000-06-29 21:12:41 +0000351
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000352#if defined _MSC_VER && _MSC_VER >= 1400
353/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
354 * valid and throw an assertion if it isn't.
355 * Normally, an invalid fd is likely to be a C program error and therefore
356 * an assertion can be useful, but it does contradict the POSIX standard
357 * which for write(2) states:
358 * "Otherwise, -1 shall be returned and errno set to indicate the error."
359 * "[EBADF] The fildes argument is not a valid file descriptor open for
360 * writing."
361 * Furthermore, python allows the user to enter any old integer
362 * as a fd and should merely raise a python exception on error.
363 * The Microsoft CRT doesn't provide an official way to check for the
364 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000365 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000366 * internal structures involved.
367 * The structures below must be updated for each version of visual studio
368 * according to the file internal.h in the CRT source, until MS comes
369 * up with a less hacky way to do this.
370 * (all of this is to avoid globally modifying the CRT behaviour using
371 * _set_invalid_parameter_handler() and _CrtSetReportMode())
372 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373/* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
375 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000376typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000377 intptr_t osfhnd;
378 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382#define IOINFO_L2E 5
383#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384#define IOINFO_ARRAYS 64
385#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
386#define FOPEN 0x01
387#define _NO_CONSOLE_FILENO (intptr_t)-2
388
389/* This function emulates what the windows CRT does to validate file handles */
390int
391_PyVerify_fd(int fd)
392{
Victor Stinner8c62be82010-05-06 00:08:46 +0000393 const int i1 = fd >> IOINFO_L2E;
394 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000395
Antoine Pitrou22e41552010-08-15 18:07:50 +0000396 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000397
Victor Stinner8c62be82010-05-06 00:08:46 +0000398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
400 */
401 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
402 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
403 }
404 if (sizeof_ioinfo == 0) {
405 /* This should not happen... */
406 goto fail;
407 }
408
409 /* See that it isn't a special CLEAR fileno */
410 if (fd != _NO_CONSOLE_FILENO) {
411 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
412 * we check pointer validity and other info
413 */
414 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
415 /* finally, check that the file is open */
416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
418 return 1;
419 }
420 }
421 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000423 errno = EBADF;
424 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000425}
426
427/* the special case of checking dup2. The target fd must be in a sensible range */
428static int
429_PyVerify_fd_dup2(int fd1, int fd2)
430{
Victor Stinner8c62be82010-05-06 00:08:46 +0000431 if (!_PyVerify_fd(fd1))
432 return 0;
433 if (fd2 == _NO_CONSOLE_FILENO)
434 return 0;
435 if ((unsigned)fd2 < _NHANDLE_)
436 return 1;
437 else
438 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000439}
440#else
441/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442#define _PyVerify_fd_dup2(A, B) (1)
443#endif
444
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000445#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000446/* The following structure was copied from
447 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
448 include doesn't seem to be present in the Windows SDK (at least as included
449 with Visual Studio Express). */
450typedef struct _REPARSE_DATA_BUFFER {
451 ULONG ReparseTag;
452 USHORT ReparseDataLength;
453 USHORT Reserved;
454 union {
455 struct {
456 USHORT SubstituteNameOffset;
457 USHORT SubstituteNameLength;
458 USHORT PrintNameOffset;
459 USHORT PrintNameLength;
460 ULONG Flags;
461 WCHAR PathBuffer[1];
462 } SymbolicLinkReparseBuffer;
463
464 struct {
465 USHORT SubstituteNameOffset;
466 USHORT SubstituteNameLength;
467 USHORT PrintNameOffset;
468 USHORT PrintNameLength;
469 WCHAR PathBuffer[1];
470 } MountPointReparseBuffer;
471
472 struct {
473 UCHAR DataBuffer[1];
474 } GenericReparseBuffer;
475 };
476} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
477
478#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
479 GenericReparseBuffer)
480#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
481
482static int
Brian Curtind25aef52011-06-13 15:16:04 -0500483win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000484{
485 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
486 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
487 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000488
489 if (0 == DeviceIoControl(
490 reparse_point_handle,
491 FSCTL_GET_REPARSE_POINT,
492 NULL, 0, /* in buffer */
493 target_buffer, sizeof(target_buffer),
494 &n_bytes_returned,
495 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500496 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000497
498 if (reparse_tag)
499 *reparse_tag = rdb->ReparseTag;
500
Brian Curtind25aef52011-06-13 15:16:04 -0500501 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000502}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000503#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000504
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000506#ifdef WITH_NEXT_FRAMEWORK
507/* On Darwin/MacOSX a shared library or framework has no access to
508** environ directly, we must obtain it with _NSGetEnviron().
509*/
510#include <crt_externs.h>
511static char **environ;
512#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000514#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515
Barry Warsaw53699e91996-12-10 23:23:01 +0000516static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000517convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518{
Victor Stinner8c62be82010-05-06 00:08:46 +0000519 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000520#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000521 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000522#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000523 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000524#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000525#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000526 APIRET rc;
527 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
528#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000529
Victor Stinner8c62be82010-05-06 00:08:46 +0000530 d = PyDict_New();
531 if (d == NULL)
532 return NULL;
533#ifdef WITH_NEXT_FRAMEWORK
534 if (environ == NULL)
535 environ = *_NSGetEnviron();
536#endif
537#ifdef MS_WINDOWS
538 /* _wenviron must be initialized in this way if the program is started
539 through main() instead of wmain(). */
540 _wgetenv(L"");
541 if (_wenviron == NULL)
542 return d;
543 /* This part ignores errors */
544 for (e = _wenviron; *e != NULL; e++) {
545 PyObject *k;
546 PyObject *v;
547 wchar_t *p = wcschr(*e, L'=');
548 if (p == NULL)
549 continue;
550 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
551 if (k == NULL) {
552 PyErr_Clear();
553 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000554 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000555 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
556 if (v == NULL) {
557 PyErr_Clear();
558 Py_DECREF(k);
559 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000560 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000561 if (PyDict_GetItem(d, k) == NULL) {
562 if (PyDict_SetItem(d, k, v) != 0)
563 PyErr_Clear();
564 }
565 Py_DECREF(k);
566 Py_DECREF(v);
567 }
568#else
569 if (environ == NULL)
570 return d;
571 /* This part ignores errors */
572 for (e = environ; *e != NULL; e++) {
573 PyObject *k;
574 PyObject *v;
575 char *p = strchr(*e, '=');
576 if (p == NULL)
577 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000578 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000579 if (k == NULL) {
580 PyErr_Clear();
581 continue;
582 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000583 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000584 if (v == NULL) {
585 PyErr_Clear();
586 Py_DECREF(k);
587 continue;
588 }
589 if (PyDict_GetItem(d, k) == NULL) {
590 if (PyDict_SetItem(d, k, v) != 0)
591 PyErr_Clear();
592 }
593 Py_DECREF(k);
594 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000595 }
596#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000597#if defined(PYOS_OS2)
598 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
599 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
600 PyObject *v = PyBytes_FromString(buffer);
601 PyDict_SetItemString(d, "BEGINLIBPATH", v);
602 Py_DECREF(v);
603 }
604 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
605 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
606 PyObject *v = PyBytes_FromString(buffer);
607 PyDict_SetItemString(d, "ENDLIBPATH", v);
608 Py_DECREF(v);
609 }
610#endif
611 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612}
613
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614/* Set a POSIX-specific error from errno, and return NULL */
615
Barry Warsawd58d7641998-07-23 16:14:40 +0000616static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000617posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000618{
Victor Stinner8c62be82010-05-06 00:08:46 +0000619 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620}
Barry Warsawd58d7641998-07-23 16:14:40 +0000621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000622posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000623{
Victor Stinner8c62be82010-05-06 00:08:46 +0000624 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000625}
626
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000627
Mark Hammondef8b6542001-05-13 08:04:26 +0000628static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000629posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000630{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000631 PyObject *name_str, *rc;
632 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
633 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000634 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000635 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
636 name_str);
637 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000638 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000639}
640
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000641#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000642static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000643win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000644{
Victor Stinner8c62be82010-05-06 00:08:46 +0000645 /* XXX We should pass the function name along in the future.
646 (winreg.c also wants to pass the function name.)
647 This would however require an additional param to the
648 Windows error object, which is non-trivial.
649 */
650 errno = GetLastError();
651 if (filename)
652 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
653 else
654 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000655}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000656
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000657static PyObject *
658win32_error_unicode(char* function, Py_UNICODE* filename)
659{
Victor Stinner8c62be82010-05-06 00:08:46 +0000660 /* XXX - see win32_error for comments on 'function' */
661 errno = GetLastError();
662 if (filename)
663 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
664 else
665 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000666}
667
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000669convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670{
Victor Stinner8c62be82010-05-06 00:08:46 +0000671 if (PyUnicode_CheckExact(*param))
672 Py_INCREF(*param);
673 else if (PyUnicode_Check(*param))
674 /* For a Unicode subtype that's not a Unicode object,
675 return a true Unicode object with the same data. */
676 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
677 PyUnicode_GET_SIZE(*param));
678 else
679 *param = PyUnicode_FromEncodedObject(*param,
680 Py_FileSystemDefaultEncoding,
681 "strict");
682 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000683}
684
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000685#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686
Guido van Rossumd48f2521997-12-05 22:19:34 +0000687#if defined(PYOS_OS2)
688/**********************************************************************
689 * Helper Function to Trim and Format OS/2 Messages
690 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000691static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000692os2_formatmsg(char *msgbuf, int msglen, char *reason)
693{
694 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
695
696 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
697 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
698
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000699 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000700 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
701 }
702
703 /* Add Optional Reason Text */
704 if (reason) {
705 strcat(msgbuf, " : ");
706 strcat(msgbuf, reason);
707 }
708}
709
710/**********************************************************************
711 * Decode an OS/2 Operating System Error Code
712 *
713 * A convenience function to lookup an OS/2 error code and return a
714 * text message we can use to raise a Python exception.
715 *
716 * Notes:
717 * The messages for errors returned from the OS/2 kernel reside in
718 * the file OSO001.MSG in the \OS2 directory hierarchy.
719 *
720 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000721static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000722os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
723{
724 APIRET rc;
725 ULONG msglen;
726
727 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
728 Py_BEGIN_ALLOW_THREADS
729 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
730 errorcode, "oso001.msg", &msglen);
731 Py_END_ALLOW_THREADS
732
733 if (rc == NO_ERROR)
734 os2_formatmsg(msgbuf, msglen, reason);
735 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000736 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000737 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000738
739 return msgbuf;
740}
741
742/* Set an OS/2-specific error and return NULL. OS/2 kernel
743 errors are not in a global variable e.g. 'errno' nor are
744 they congruent with posix error numbers. */
745
Victor Stinner8c62be82010-05-06 00:08:46 +0000746static PyObject *
747os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000748{
749 char text[1024];
750 PyObject *v;
751
752 os2_strerror(text, sizeof(text), code, "");
753
754 v = Py_BuildValue("(is)", code, text);
755 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000756 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000757 Py_DECREF(v);
758 }
759 return NULL; /* Signal to Python that an Exception is Pending */
760}
761
762#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763
764/* POSIX generic methods */
765
Barry Warsaw53699e91996-12-10 23:23:01 +0000766static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000767posix_fildes(PyObject *fdobj, int (*func)(int))
768{
Victor Stinner8c62be82010-05-06 00:08:46 +0000769 int fd;
770 int res;
771 fd = PyObject_AsFileDescriptor(fdobj);
772 if (fd < 0)
773 return NULL;
774 if (!_PyVerify_fd(fd))
775 return posix_error();
776 Py_BEGIN_ALLOW_THREADS
777 res = (*func)(fd);
778 Py_END_ALLOW_THREADS
779 if (res < 0)
780 return posix_error();
781 Py_INCREF(Py_None);
782 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000783}
Guido van Rossum21142a01999-01-08 21:05:37 +0000784
785static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787{
Victor Stinner8c62be82010-05-06 00:08:46 +0000788 PyObject *opath1 = NULL;
789 char *path1;
790 int res;
791 if (!PyArg_ParseTuple(args, format,
792 PyUnicode_FSConverter, &opath1))
793 return NULL;
794 path1 = PyBytes_AsString(opath1);
795 Py_BEGIN_ALLOW_THREADS
796 res = (*func)(path1);
797 Py_END_ALLOW_THREADS
798 if (res < 0)
799 return posix_error_with_allocated_filename(opath1);
800 Py_DECREF(opath1);
801 Py_INCREF(Py_None);
802 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000803}
804
Barry Warsaw53699e91996-12-10 23:23:01 +0000805static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000806posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000807 char *format,
808 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809{
Victor Stinner8c62be82010-05-06 00:08:46 +0000810 PyObject *opath1 = NULL, *opath2 = NULL;
811 char *path1, *path2;
812 int res;
813 if (!PyArg_ParseTuple(args, format,
814 PyUnicode_FSConverter, &opath1,
815 PyUnicode_FSConverter, &opath2)) {
816 return NULL;
817 }
818 path1 = PyBytes_AsString(opath1);
819 path2 = PyBytes_AsString(opath2);
820 Py_BEGIN_ALLOW_THREADS
821 res = (*func)(path1, path2);
822 Py_END_ALLOW_THREADS
823 Py_DECREF(opath1);
824 Py_DECREF(opath2);
825 if (res != 0)
826 /* XXX how to report both path1 and path2??? */
827 return posix_error();
828 Py_INCREF(Py_None);
829 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830}
831
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000832#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000833static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000834win32_1str(PyObject* args, char* func,
835 char* format, BOOL (__stdcall *funcA)(LPCSTR),
836 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837{
Victor Stinner8c62be82010-05-06 00:08:46 +0000838 PyObject *uni;
839 char *ansi;
840 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000841
Victor Stinner8c62be82010-05-06 00:08:46 +0000842 if (!PyArg_ParseTuple(args, wformat, &uni))
843 PyErr_Clear();
844 else {
845 Py_BEGIN_ALLOW_THREADS
846 result = funcW(PyUnicode_AsUnicode(uni));
847 Py_END_ALLOW_THREADS
848 if (!result)
849 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
850 Py_INCREF(Py_None);
851 return Py_None;
852 }
853 if (!PyArg_ParseTuple(args, format, &ansi))
854 return NULL;
855 Py_BEGIN_ALLOW_THREADS
856 result = funcA(ansi);
857 Py_END_ALLOW_THREADS
858 if (!result)
859 return win32_error(func, ansi);
860 Py_INCREF(Py_None);
861 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862
863}
864
865/* This is a reimplementation of the C library's chdir function,
866 but one that produces Win32 errors instead of DOS error codes.
867 chdir is essentially a wrapper around SetCurrentDirectory; however,
868 it also needs to set "magic" environment variables indicating
869 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000870static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871win32_chdir(LPCSTR path)
872{
Victor Stinner8c62be82010-05-06 00:08:46 +0000873 char new_path[MAX_PATH+1];
874 int result;
875 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000876
Victor Stinner8c62be82010-05-06 00:08:46 +0000877 if(!SetCurrentDirectoryA(path))
878 return FALSE;
879 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
880 if (!result)
881 return FALSE;
882 /* In the ANSI API, there should not be any paths longer
883 than MAX_PATH. */
884 assert(result <= MAX_PATH+1);
885 if (strncmp(new_path, "\\\\", 2) == 0 ||
886 strncmp(new_path, "//", 2) == 0)
887 /* UNC path, nothing to do. */
888 return TRUE;
889 env[1] = new_path[0];
890 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891}
892
893/* The Unicode version differs from the ANSI version
894 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000895static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896win32_wchdir(LPCWSTR path)
897{
Victor Stinner8c62be82010-05-06 00:08:46 +0000898 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
899 int result;
900 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 if(!SetCurrentDirectoryW(path))
903 return FALSE;
904 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
905 if (!result)
906 return FALSE;
907 if (result > MAX_PATH+1) {
908 new_path = malloc(result * sizeof(wchar_t));
909 if (!new_path) {
910 SetLastError(ERROR_OUTOFMEMORY);
911 return FALSE;
912 }
913 result = GetCurrentDirectoryW(result, new_path);
914 if (!result) {
915 free(new_path);
916 return FALSE;
917 }
918 }
919 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
920 wcsncmp(new_path, L"//", 2) == 0)
921 /* UNC path, nothing to do. */
922 return TRUE;
923 env[1] = new_path[0];
924 result = SetEnvironmentVariableW(env, new_path);
925 if (new_path != _new_path)
926 free(new_path);
927 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928}
929#endif
930
Martin v. Löwis14694662006-02-03 12:54:16 +0000931#ifdef MS_WINDOWS
932/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
933 - time stamps are restricted to second resolution
934 - file modification times suffer from forth-and-back conversions between
935 UTC and local time
936 Therefore, we implement our own stat, based on the Win32 API directly.
937*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000938#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000939
940struct win32_stat{
941 int st_dev;
942 __int64 st_ino;
943 unsigned short st_mode;
944 int st_nlink;
945 int st_uid;
946 int st_gid;
947 int st_rdev;
948 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000949 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000950 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000951 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000952 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000953 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000954 int st_ctime_nsec;
955};
956
957static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
958
959static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000960FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000961{
Victor Stinner8c62be82010-05-06 00:08:46 +0000962 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
963 /* Cannot simply cast and dereference in_ptr,
964 since it might not be aligned properly */
965 __int64 in;
966 memcpy(&in, in_ptr, sizeof(in));
967 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000968 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +0000969}
970
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000972time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973{
Victor Stinner8c62be82010-05-06 00:08:46 +0000974 /* XXX endianness */
975 __int64 out;
976 out = time_in + secs_between_epochs;
977 out = out * 10000000 + nsec_in / 100;
978 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979}
980
Martin v. Löwis14694662006-02-03 12:54:16 +0000981/* Below, we *know* that ugo+r is 0444 */
982#if _S_IREAD != 0400
983#error Unsupported C library
984#endif
985static int
986attributes_to_mode(DWORD attr)
987{
Victor Stinner8c62be82010-05-06 00:08:46 +0000988 int m = 0;
989 if (attr & FILE_ATTRIBUTE_DIRECTORY)
990 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
991 else
992 m |= _S_IFREG;
993 if (attr & FILE_ATTRIBUTE_READONLY)
994 m |= 0444;
995 else
996 m |= 0666;
997 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000998}
999
1000static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001001attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001002{
Victor Stinner8c62be82010-05-06 00:08:46 +00001003 memset(result, 0, sizeof(*result));
1004 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1005 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1006 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1007 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1008 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001009 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001010 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001011 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1012 /* first clear the S_IFMT bits */
1013 result->st_mode ^= (result->st_mode & 0170000);
1014 /* now set the bits that make this a symlink */
1015 result->st_mode |= 0120000;
1016 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001017
Victor Stinner8c62be82010-05-06 00:08:46 +00001018 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001019}
1020
Guido van Rossumd8faa362007-04-27 19:54:29 +00001021static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001022attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001023{
Victor Stinner8c62be82010-05-06 00:08:46 +00001024 HANDLE hFindFile;
1025 WIN32_FIND_DATAA FileData;
1026 hFindFile = FindFirstFileA(pszFile, &FileData);
1027 if (hFindFile == INVALID_HANDLE_VALUE)
1028 return FALSE;
1029 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001030 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001031 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001032 info->dwFileAttributes = FileData.dwFileAttributes;
1033 info->ftCreationTime = FileData.ftCreationTime;
1034 info->ftLastAccessTime = FileData.ftLastAccessTime;
1035 info->ftLastWriteTime = FileData.ftLastWriteTime;
1036 info->nFileSizeHigh = FileData.nFileSizeHigh;
1037 info->nFileSizeLow = FileData.nFileSizeLow;
1038/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001039 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1040 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001041 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001042}
1043
1044static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001045attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001046{
Victor Stinner8c62be82010-05-06 00:08:46 +00001047 HANDLE hFindFile;
1048 WIN32_FIND_DATAW FileData;
1049 hFindFile = FindFirstFileW(pszFile, &FileData);
1050 if (hFindFile == INVALID_HANDLE_VALUE)
1051 return FALSE;
1052 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001053 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001054 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001055 info->dwFileAttributes = FileData.dwFileAttributes;
1056 info->ftCreationTime = FileData.ftCreationTime;
1057 info->ftLastAccessTime = FileData.ftLastAccessTime;
1058 info->ftLastWriteTime = FileData.ftLastWriteTime;
1059 info->nFileSizeHigh = FileData.nFileSizeHigh;
1060 info->nFileSizeLow = FileData.nFileSizeLow;
1061/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001062 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1063 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001064 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001065}
1066
Brian Curtind25aef52011-06-13 15:16:04 -05001067/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1068static int has_GetFinalPathNameByHandle = 0;
1069static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1070 DWORD);
1071static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1072 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001073static int
Brian Curtind25aef52011-06-13 15:16:04 -05001074check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001075{
Brian Curtind25aef52011-06-13 15:16:04 -05001076 HINSTANCE hKernel32;
1077 /* only recheck */
1078 if (!has_GetFinalPathNameByHandle)
1079 {
1080 hKernel32 = GetModuleHandle("KERNEL32");
1081 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1082 "GetFinalPathNameByHandleA");
1083 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1084 "GetFinalPathNameByHandleW");
1085 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1086 Py_GetFinalPathNameByHandleW;
1087 }
1088 return has_GetFinalPathNameByHandle;
1089}
1090
1091static BOOL
1092get_target_path(HANDLE hdl, wchar_t **target_path)
1093{
1094 int buf_size, result_length;
1095 wchar_t *buf;
1096
1097 /* We have a good handle to the target, use it to determine
1098 the target path name (then we'll call lstat on it). */
1099 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1100 VOLUME_NAME_DOS);
1101 if(!buf_size)
1102 return FALSE;
1103
1104 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
1105 result_length = Py_GetFinalPathNameByHandleW(hdl,
1106 buf, buf_size, VOLUME_NAME_DOS);
1107
1108 if(!result_length) {
1109 free(buf);
1110 return FALSE;
1111 }
1112
1113 if(!CloseHandle(hdl)) {
1114 free(buf);
1115 return FALSE;
1116 }
1117
1118 buf[result_length] = 0;
1119
1120 *target_path = buf;
1121 return TRUE;
1122}
1123
1124static int
1125win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1126 BOOL traverse);
1127static int
1128win32_xstat_impl(const char *path, struct win32_stat *result,
1129 BOOL traverse)
1130{
1131 int code;
1132 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001133 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001134 ULONG reparse_tag = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001135 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001136 const char *dot;
1137
Brian Curtind25aef52011-06-13 15:16:04 -05001138 if(!check_GetFinalPathNameByHandle()) {
1139 /* If the OS doesn't have GetFinalPathNameByHandle, return a
1140 NotImplementedError. */
1141 PyErr_SetString(PyExc_NotImplementedError,
1142 "GetFinalPathNameByHandle not available on this platform");
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001143 return -1;
1144 }
1145
Brian Curtinf5e76d02010-11-24 13:14:05 +00001146 hFile = CreateFileA(
1147 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001148 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001149 0, /* share mode */
1150 NULL, /* security attributes */
1151 OPEN_EXISTING,
1152 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001153 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1154 Because of this, calls like GetFinalPathNameByHandle will return
1155 the symlink path agin and not the actual final path. */
1156 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1157 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001158 NULL);
1159
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001160 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001161 /* Either the target doesn't exist, or we don't have access to
1162 get a handle to it. If the former, we need to return an error.
1163 If the latter, we can use attributes_from_dir. */
1164 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001165 return -1;
1166 /* Could not get attributes on open file. Fall back to
1167 reading the directory. */
1168 if (!attributes_from_dir(path, &info, &reparse_tag))
1169 /* Very strange. This should not fail now */
1170 return -1;
1171 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1172 if (traverse) {
1173 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001175 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001177 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001178 } else {
1179 if (!GetFileInformationByHandle(hFile, &info)) {
1180 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001181 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001182 }
1183 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001184 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1185 return -1;
1186
1187 /* Close the outer open file handle now that we're about to
1188 reopen it with different flags. */
1189 if (!CloseHandle(hFile))
1190 return -1;
1191
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001192 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001193 /* In order to call GetFinalPathNameByHandle we need to open
1194 the file without the reparse handling flag set. */
1195 hFile2 = CreateFileA(
1196 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1197 NULL, OPEN_EXISTING,
1198 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1199 NULL);
1200 if (hFile2 == INVALID_HANDLE_VALUE)
1201 return -1;
1202
1203 if (!get_target_path(hFile2, &target_path))
1204 return -1;
1205
1206 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207 free(target_path);
1208 return code;
1209 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001210 } else
1211 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001212 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001213 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001214
1215 /* Set S_IEXEC if it is an .exe, .bat, ... */
1216 dot = strrchr(path, '.');
1217 if (dot) {
1218 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1219 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1220 result->st_mode |= 0111;
1221 }
1222 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001223}
1224
1225static int
Brian Curtind25aef52011-06-13 15:16:04 -05001226win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1227 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001228{
1229 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001230 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001231 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001232 ULONG reparse_tag = 0;
1233 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001234 const wchar_t *dot;
1235
Brian Curtind25aef52011-06-13 15:16:04 -05001236 if(!check_GetFinalPathNameByHandle()) {
1237 /* If the OS doesn't have GetFinalPathNameByHandle, return a
1238 NotImplementedError. */
1239 PyErr_SetString(PyExc_NotImplementedError,
1240 "GetFinalPathNameByHandle not available on this platform");
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001241 return -1;
1242 }
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. */
1254 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1255 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
1373static int
1374win32_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;
2206 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2207 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;
2411
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;
2791
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);
2812
2813 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;
3161
3162 /* 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{
Victor Stinner8c62be82010-05-06 00:08:46 +00004005 PyObject *opath
4006 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
4351 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4352 * 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;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004567#ifdef MS_WINDOWS
4568 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);
4574 }
4575 else
4576 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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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
5259
5260 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5261 {
5262 return win32_error_unicode("readlink", path);
5263 }
5264
5265 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;
5334 WIN32_FILE_ATTRIBUTE_DATA src_info;
5335
5336 if (!check_CreateSymbolicLinkW())
5337 {
5338 /* raise NotImplementedError */
5339 return PyErr_Format(PyExc_NotImplementedError,
5340 "CreateSymbolicLinkW not found");
5341 }
5342 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5343 kwlist, &src, &dest, &target_is_directory))
5344 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005345
5346 if (win32_can_symlink == 0)
5347 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5348
Brian Curtind40e6f72010-07-08 21:39:08 +00005349 if (!convert_to_unicode(&src)) { return NULL; }
5350 if (!convert_to_unicode(&dest)) {
5351 Py_DECREF(src);
5352 return NULL;
5353 }
5354
5355 /* if src is a directory, ensure target_is_directory==1 */
5356 if(
5357 GetFileAttributesExW(
5358 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5359 ))
5360 {
5361 target_is_directory = target_is_directory ||
5362 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5363 }
5364
5365 Py_BEGIN_ALLOW_THREADS
5366 res = Py_CreateSymbolicLinkW(
5367 PyUnicode_AsUnicode(dest),
5368 PyUnicode_AsUnicode(src),
5369 target_is_directory);
5370 Py_END_ALLOW_THREADS
5371 Py_DECREF(src);
5372 Py_DECREF(dest);
5373 if (!res)
5374 {
5375 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5376 }
5377
5378 Py_INCREF(Py_None);
5379 return Py_None;
5380}
Brian Curtin52173d42010-12-02 18:29:18 +00005381#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005382
5383#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005384#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5385static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005386system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005387{
5388 ULONG value = 0;
5389
5390 Py_BEGIN_ALLOW_THREADS
5391 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5392 Py_END_ALLOW_THREADS
5393
5394 return value;
5395}
5396
5397static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005398posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005399{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005400 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005401 return Py_BuildValue("ddddd",
5402 (double)0 /* t.tms_utime / HZ */,
5403 (double)0 /* t.tms_stime / HZ */,
5404 (double)0 /* t.tms_cutime / HZ */,
5405 (double)0 /* t.tms_cstime / HZ */,
5406 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005407}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005408#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005409#define NEED_TICKS_PER_SECOND
5410static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005411static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005412posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005413{
Victor Stinner8c62be82010-05-06 00:08:46 +00005414 struct tms t;
5415 clock_t c;
5416 errno = 0;
5417 c = times(&t);
5418 if (c == (clock_t) -1)
5419 return posix_error();
5420 return Py_BuildValue("ddddd",
5421 (double)t.tms_utime / ticks_per_second,
5422 (double)t.tms_stime / ticks_per_second,
5423 (double)t.tms_cutime / ticks_per_second,
5424 (double)t.tms_cstime / ticks_per_second,
5425 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005426}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005427#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005428#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005429
5430
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005431#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005432#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005433static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005434posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005435{
Victor Stinner8c62be82010-05-06 00:08:46 +00005436 FILETIME create, exit, kernel, user;
5437 HANDLE hProc;
5438 hProc = GetCurrentProcess();
5439 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5440 /* The fields of a FILETIME structure are the hi and lo part
5441 of a 64-bit value expressed in 100 nanosecond units.
5442 1e7 is one second in such units; 1e-7 the inverse.
5443 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5444 */
5445 return Py_BuildValue(
5446 "ddddd",
5447 (double)(user.dwHighDateTime*429.4967296 +
5448 user.dwLowDateTime*1e-7),
5449 (double)(kernel.dwHighDateTime*429.4967296 +
5450 kernel.dwLowDateTime*1e-7),
5451 (double)0,
5452 (double)0,
5453 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005454}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005455#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005456
5457#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005459"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005460Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005461#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005462
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005463
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005464#ifdef HAVE_GETSID
5465PyDoc_STRVAR(posix_getsid__doc__,
5466"getsid(pid) -> sid\n\n\
5467Call the system call getsid().");
5468
5469static PyObject *
5470posix_getsid(PyObject *self, PyObject *args)
5471{
Victor Stinner8c62be82010-05-06 00:08:46 +00005472 pid_t pid;
5473 int sid;
5474 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5475 return NULL;
5476 sid = getsid(pid);
5477 if (sid < 0)
5478 return posix_error();
5479 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005480}
5481#endif /* HAVE_GETSID */
5482
5483
Guido van Rossumb6775db1994-08-01 11:34:53 +00005484#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005486"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005487Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005488
Barry Warsaw53699e91996-12-10 23:23:01 +00005489static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005490posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005491{
Victor Stinner8c62be82010-05-06 00:08:46 +00005492 if (setsid() < 0)
5493 return posix_error();
5494 Py_INCREF(Py_None);
5495 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005496}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005497#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005498
Guido van Rossumb6775db1994-08-01 11:34:53 +00005499#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005501"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005503
Barry Warsaw53699e91996-12-10 23:23:01 +00005504static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005505posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005506{
Victor Stinner8c62be82010-05-06 00:08:46 +00005507 pid_t pid;
5508 int pgrp;
5509 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5510 return NULL;
5511 if (setpgid(pid, pgrp) < 0)
5512 return posix_error();
5513 Py_INCREF(Py_None);
5514 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005515}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005516#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005517
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005518
Guido van Rossumb6775db1994-08-01 11:34:53 +00005519#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005520PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005521"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005523
Barry Warsaw53699e91996-12-10 23:23:01 +00005524static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005525posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005526{
Victor Stinner8c62be82010-05-06 00:08:46 +00005527 int fd;
5528 pid_t pgid;
5529 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5530 return NULL;
5531 pgid = tcgetpgrp(fd);
5532 if (pgid < 0)
5533 return posix_error();
5534 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005535}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005536#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005538
Guido van Rossumb6775db1994-08-01 11:34:53 +00005539#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005541"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005542Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005543
Barry Warsaw53699e91996-12-10 23:23:01 +00005544static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005545posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005546{
Victor Stinner8c62be82010-05-06 00:08:46 +00005547 int fd;
5548 pid_t pgid;
5549 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5550 return NULL;
5551 if (tcsetpgrp(fd, pgid) < 0)
5552 return posix_error();
5553 Py_INCREF(Py_None);
5554 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005555}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005556#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005557
Guido van Rossum687dd131993-05-17 08:34:16 +00005558/* Functions acting on file descriptors */
5559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005561"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005562Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005563
Barry Warsaw53699e91996-12-10 23:23:01 +00005564static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005565posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005566{
Victor Stinner8c62be82010-05-06 00:08:46 +00005567 PyObject *ofile;
5568 char *file;
5569 int flag;
5570 int mode = 0777;
5571 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005572
5573#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005574 PyUnicodeObject *po;
5575 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5576 Py_BEGIN_ALLOW_THREADS
5577 /* PyUnicode_AS_UNICODE OK without thread
5578 lock as it is a simple dereference. */
5579 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5580 Py_END_ALLOW_THREADS
5581 if (fd < 0)
5582 return posix_error();
5583 return PyLong_FromLong((long)fd);
5584 }
5585 /* Drop the argument parsing error as narrow strings
5586 are also valid. */
5587 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005588#endif
5589
Victor Stinner8c62be82010-05-06 00:08:46 +00005590 if (!PyArg_ParseTuple(args, "O&i|i",
5591 PyUnicode_FSConverter, &ofile,
5592 &flag, &mode))
5593 return NULL;
5594 file = PyBytes_AsString(ofile);
5595 Py_BEGIN_ALLOW_THREADS
5596 fd = open(file, flag, mode);
5597 Py_END_ALLOW_THREADS
5598 if (fd < 0)
5599 return posix_error_with_allocated_filename(ofile);
5600 Py_DECREF(ofile);
5601 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005602}
5603
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005606"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005607Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005608
Barry Warsaw53699e91996-12-10 23:23:01 +00005609static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005610posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005611{
Victor Stinner8c62be82010-05-06 00:08:46 +00005612 int fd, res;
5613 if (!PyArg_ParseTuple(args, "i:close", &fd))
5614 return NULL;
5615 if (!_PyVerify_fd(fd))
5616 return posix_error();
5617 Py_BEGIN_ALLOW_THREADS
5618 res = close(fd);
5619 Py_END_ALLOW_THREADS
5620 if (res < 0)
5621 return posix_error();
5622 Py_INCREF(Py_None);
5623 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005624}
5625
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005626
Victor Stinner8c62be82010-05-06 00:08:46 +00005627PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005628"closerange(fd_low, fd_high)\n\n\
5629Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5630
5631static PyObject *
5632posix_closerange(PyObject *self, PyObject *args)
5633{
Victor Stinner8c62be82010-05-06 00:08:46 +00005634 int fd_from, fd_to, i;
5635 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5636 return NULL;
5637 Py_BEGIN_ALLOW_THREADS
5638 for (i = fd_from; i < fd_to; i++)
5639 if (_PyVerify_fd(i))
5640 close(i);
5641 Py_END_ALLOW_THREADS
5642 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005643}
5644
5645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005646PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005647"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005648Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005649
Barry Warsaw53699e91996-12-10 23:23:01 +00005650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005651posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005652{
Victor Stinner8c62be82010-05-06 00:08:46 +00005653 int fd;
5654 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5655 return NULL;
5656 if (!_PyVerify_fd(fd))
5657 return posix_error();
5658 Py_BEGIN_ALLOW_THREADS
5659 fd = dup(fd);
5660 Py_END_ALLOW_THREADS
5661 if (fd < 0)
5662 return posix_error();
5663 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005664}
5665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005667PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005668"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005669Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005670
Barry Warsaw53699e91996-12-10 23:23:01 +00005671static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005672posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005673{
Victor Stinner8c62be82010-05-06 00:08:46 +00005674 int fd, fd2, res;
5675 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5676 return NULL;
5677 if (!_PyVerify_fd_dup2(fd, fd2))
5678 return posix_error();
5679 Py_BEGIN_ALLOW_THREADS
5680 res = dup2(fd, fd2);
5681 Py_END_ALLOW_THREADS
5682 if (res < 0)
5683 return posix_error();
5684 Py_INCREF(Py_None);
5685 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005686}
5687
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005689PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005690"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005691Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005692
Barry Warsaw53699e91996-12-10 23:23:01 +00005693static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005694posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005695{
Victor Stinner8c62be82010-05-06 00:08:46 +00005696 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005697#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005698 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005699#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005700 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005701#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005702 PyObject *posobj;
5703 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5704 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005705#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005706 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5707 switch (how) {
5708 case 0: how = SEEK_SET; break;
5709 case 1: how = SEEK_CUR; break;
5710 case 2: how = SEEK_END; break;
5711 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005712#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005713
5714#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005715 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005716#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005717 pos = PyLong_Check(posobj) ?
5718 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005719#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005720 if (PyErr_Occurred())
5721 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005722
Victor Stinner8c62be82010-05-06 00:08:46 +00005723 if (!_PyVerify_fd(fd))
5724 return posix_error();
5725 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005726#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005727 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005728#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005729 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005730#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 Py_END_ALLOW_THREADS
5732 if (res < 0)
5733 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005734
5735#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005736 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005737#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005738 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005739#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005740}
5741
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005743PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005744"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005745Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005746
Barry Warsaw53699e91996-12-10 23:23:01 +00005747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005748posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005749{
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 int fd, size;
5751 Py_ssize_t n;
5752 PyObject *buffer;
5753 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5754 return NULL;
5755 if (size < 0) {
5756 errno = EINVAL;
5757 return posix_error();
5758 }
5759 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5760 if (buffer == NULL)
5761 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005762 if (!_PyVerify_fd(fd)) {
5763 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005765 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 Py_BEGIN_ALLOW_THREADS
5767 n = read(fd, PyBytes_AS_STRING(buffer), size);
5768 Py_END_ALLOW_THREADS
5769 if (n < 0) {
5770 Py_DECREF(buffer);
5771 return posix_error();
5772 }
5773 if (n != size)
5774 _PyBytes_Resize(&buffer, n);
5775 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005776}
5777
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005779PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005780"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005781Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005782
Barry Warsaw53699e91996-12-10 23:23:01 +00005783static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005784posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005785{
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 Py_buffer pbuf;
5787 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005788 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005789
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5791 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005792 if (!_PyVerify_fd(fd)) {
5793 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005795 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005796 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005798#if defined(MS_WIN64) || defined(MS_WINDOWS)
5799 if (len > INT_MAX)
5800 len = INT_MAX;
5801 size = write(fd, pbuf.buf, (int)len);
5802#else
Victor Stinner72344792011-01-11 00:04:12 +00005803 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005804#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005805 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005806 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005807 if (size < 0)
5808 return posix_error();
5809 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005810}
5811
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005813PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005814"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005816
Barry Warsaw53699e91996-12-10 23:23:01 +00005817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005818posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005819{
Victor Stinner8c62be82010-05-06 00:08:46 +00005820 int fd;
5821 STRUCT_STAT st;
5822 int res;
5823 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5824 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005825#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005826 /* on OpenVMS we must ensure that all bytes are written to the file */
5827 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005828#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005829 if (!_PyVerify_fd(fd))
5830 return posix_error();
5831 Py_BEGIN_ALLOW_THREADS
5832 res = FSTAT(fd, &st);
5833 Py_END_ALLOW_THREADS
5834 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005835#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005836 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005837#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005838 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005839#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005840 }
Tim Peters5aa91602002-01-30 05:46:57 +00005841
Victor Stinner8c62be82010-05-06 00:08:46 +00005842 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005843}
5844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005845PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005846"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005847Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005848connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005849
5850static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005851posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005852{
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 int fd;
5854 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5855 return NULL;
5856 if (!_PyVerify_fd(fd))
5857 return PyBool_FromLong(0);
5858 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005859}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005860
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005861#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005862PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005863"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005864Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005865
Barry Warsaw53699e91996-12-10 23:23:01 +00005866static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005867posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005868{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005869#if defined(PYOS_OS2)
5870 HFILE read, write;
5871 APIRET rc;
5872
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005874 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005875 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005876 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005877 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005878
5879 return Py_BuildValue("(ii)", read, write);
5880#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005881#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 int fds[2];
5883 int res;
5884 Py_BEGIN_ALLOW_THREADS
5885 res = pipe(fds);
5886 Py_END_ALLOW_THREADS
5887 if (res != 0)
5888 return posix_error();
5889 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005890#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 HANDLE read, write;
5892 int read_fd, write_fd;
5893 BOOL ok;
5894 Py_BEGIN_ALLOW_THREADS
5895 ok = CreatePipe(&read, &write, NULL, 0);
5896 Py_END_ALLOW_THREADS
5897 if (!ok)
5898 return win32_error("CreatePipe", NULL);
5899 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5900 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5901 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005902#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005903#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005904}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005905#endif /* HAVE_PIPE */
5906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005907
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005908#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005909PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005910"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005911Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005912
Barry Warsaw53699e91996-12-10 23:23:01 +00005913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005914posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005915{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005916 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 char *filename;
5918 int mode = 0666;
5919 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005920 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5921 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005922 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005923 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005924 Py_BEGIN_ALLOW_THREADS
5925 res = mkfifo(filename, mode);
5926 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005927 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005928 if (res < 0)
5929 return posix_error();
5930 Py_INCREF(Py_None);
5931 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005932}
5933#endif
5934
5935
Neal Norwitz11690112002-07-30 01:08:28 +00005936#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005937PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005938"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005939Create a filesystem node (file, device special file or named pipe)\n\
5940named filename. mode specifies both the permissions to use and the\n\
5941type of node to be created, being combined (bitwise OR) with one of\n\
5942S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005943device defines the newly created device special file (probably using\n\
5944os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005945
5946
5947static PyObject *
5948posix_mknod(PyObject *self, PyObject *args)
5949{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005950 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 char *filename;
5952 int mode = 0600;
5953 int device = 0;
5954 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005955 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5956 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005957 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005958 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005959 Py_BEGIN_ALLOW_THREADS
5960 res = mknod(filename, mode, device);
5961 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005962 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 if (res < 0)
5964 return posix_error();
5965 Py_INCREF(Py_None);
5966 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005967}
5968#endif
5969
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005970#ifdef HAVE_DEVICE_MACROS
5971PyDoc_STRVAR(posix_major__doc__,
5972"major(device) -> major number\n\
5973Extracts a device major number from a raw device number.");
5974
5975static PyObject *
5976posix_major(PyObject *self, PyObject *args)
5977{
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 int device;
5979 if (!PyArg_ParseTuple(args, "i:major", &device))
5980 return NULL;
5981 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005982}
5983
5984PyDoc_STRVAR(posix_minor__doc__,
5985"minor(device) -> minor number\n\
5986Extracts a device minor number from a raw device number.");
5987
5988static PyObject *
5989posix_minor(PyObject *self, PyObject *args)
5990{
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 int device;
5992 if (!PyArg_ParseTuple(args, "i:minor", &device))
5993 return NULL;
5994 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005995}
5996
5997PyDoc_STRVAR(posix_makedev__doc__,
5998"makedev(major, minor) -> device number\n\
5999Composes a raw device number from the major and minor device numbers.");
6000
6001static PyObject *
6002posix_makedev(PyObject *self, PyObject *args)
6003{
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 int major, minor;
6005 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6006 return NULL;
6007 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006008}
6009#endif /* device macros */
6010
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006011
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006012#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006013PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006014"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006016
Barry Warsaw53699e91996-12-10 23:23:01 +00006017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006018posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006019{
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 int fd;
6021 off_t length;
6022 int res;
6023 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006024
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6026 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006027
6028#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006029 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006030#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006031 length = PyLong_Check(lenobj) ?
6032 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006033#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 if (PyErr_Occurred())
6035 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006036
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 Py_BEGIN_ALLOW_THREADS
6038 res = ftruncate(fd, length);
6039 Py_END_ALLOW_THREADS
6040 if (res < 0)
6041 return posix_error();
6042 Py_INCREF(Py_None);
6043 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006044}
6045#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006046
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006047#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006048PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006049"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006051
Fred Drake762e2061999-08-26 17:23:54 +00006052/* Save putenv() parameters as values here, so we can collect them when they
6053 * get re-set with another call for the same key. */
6054static PyObject *posix_putenv_garbage;
6055
Tim Peters5aa91602002-01-30 05:46:57 +00006056static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006057posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006058{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006059#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 wchar_t *s1, *s2;
6061 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006062#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 PyObject *os1, *os2;
6064 char *s1, *s2;
6065 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006066#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006067 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006068 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006069
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006070#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 if (!PyArg_ParseTuple(args,
6072 "uu:putenv",
6073 &s1, &s2))
6074 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006075#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006076 if (!PyArg_ParseTuple(args,
6077 "O&O&:putenv",
6078 PyUnicode_FSConverter, &os1,
6079 PyUnicode_FSConverter, &os2))
6080 return NULL;
6081 s1 = PyBytes_AsString(os1);
6082 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006083#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006084
6085#if defined(PYOS_OS2)
6086 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6087 APIRET rc;
6088
Guido van Rossumd48f2521997-12-05 22:19:34 +00006089 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006090 if (rc != NO_ERROR) {
6091 os2_error(rc);
6092 goto error;
6093 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006094
6095 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6096 APIRET rc;
6097
Guido van Rossumd48f2521997-12-05 22:19:34 +00006098 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006099 if (rc != NO_ERROR) {
6100 os2_error(rc);
6101 goto error;
6102 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006103 } else {
6104#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006105 /* XXX This can leak memory -- not easy to fix :-( */
6106 /* len includes space for a trailing \0; the size arg to
6107 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006108#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 len = wcslen(s1) + wcslen(s2) + 2;
6110 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006111#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006112 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006113 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006114#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006115 if (newstr == NULL) {
6116 PyErr_NoMemory();
6117 goto error;
6118 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006119#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 newenv = PyUnicode_AsUnicode(newstr);
6121 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6122 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006123 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006124 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006125 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006126#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 newenv = PyBytes_AS_STRING(newstr);
6128 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6129 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006131 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006133#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006134
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 /* Install the first arg and newstr in posix_putenv_garbage;
6136 * this will cause previous value to be collected. This has to
6137 * happen after the real putenv() call because the old value
6138 * was still accessible until then. */
6139 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006140#ifdef MS_WINDOWS
6141 PyTuple_GET_ITEM(args, 0),
6142#else
6143 os1,
6144#endif
6145 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 /* really not much we can do; just leak */
6147 PyErr_Clear();
6148 }
6149 else {
6150 Py_DECREF(newstr);
6151 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006152
6153#if defined(PYOS_OS2)
6154 }
6155#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006156
Martin v. Löwis011e8422009-05-05 04:43:17 +00006157#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006158 Py_DECREF(os1);
6159 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006160#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006161 Py_RETURN_NONE;
6162
6163error:
6164#ifndef MS_WINDOWS
6165 Py_DECREF(os1);
6166 Py_DECREF(os2);
6167#endif
6168 Py_XDECREF(newstr);
6169 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006170}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006171#endif /* putenv */
6172
Guido van Rossumc524d952001-10-19 01:31:59 +00006173#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006174PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006175"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006176Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006177
6178static PyObject *
6179posix_unsetenv(PyObject *self, PyObject *args)
6180{
Victor Stinner84ae1182010-05-06 22:05:07 +00006181#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006183
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6185 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006186#else
6187 PyObject *os1;
6188 char *s1;
6189
6190 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6191 PyUnicode_FSConverter, &os1))
6192 return NULL;
6193 s1 = PyBytes_AsString(os1);
6194#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006195
Victor Stinner8c62be82010-05-06 00:08:46 +00006196 unsetenv(s1);
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 */
6203 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006204#ifdef MS_WINDOWS
6205 PyTuple_GET_ITEM(args, 0)
6206#else
6207 os1
6208#endif
6209 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006210 /* really not much we can do; just leak */
6211 PyErr_Clear();
6212 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006213
Victor Stinner84ae1182010-05-06 22:05:07 +00006214#ifndef MS_WINDOWS
6215 Py_DECREF(os1);
6216#endif
6217 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006218}
6219#endif /* unsetenv */
6220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006221PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006222"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006223Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006224
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006225static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006226posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006227{
Victor Stinner8c62be82010-05-06 00:08:46 +00006228 int code;
6229 char *message;
6230 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6231 return NULL;
6232 message = strerror(code);
6233 if (message == NULL) {
6234 PyErr_SetString(PyExc_ValueError,
6235 "strerror() argument out of range");
6236 return NULL;
6237 }
6238 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006239}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006240
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006241
Guido van Rossumc9641791998-08-04 15:26:23 +00006242#ifdef HAVE_SYS_WAIT_H
6243
Fred Drake106c1a02002-04-23 15:58:02 +00006244#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006245PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006246"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006247Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006248
6249static PyObject *
6250posix_WCOREDUMP(PyObject *self, PyObject *args)
6251{
Victor Stinner8c62be82010-05-06 00:08:46 +00006252 WAIT_TYPE status;
6253 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006254
Victor Stinner8c62be82010-05-06 00:08:46 +00006255 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6256 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006257
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006259}
6260#endif /* WCOREDUMP */
6261
6262#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006263PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006264"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006265Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006266job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006267
6268static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006269posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006270{
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 WAIT_TYPE status;
6272 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006273
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6275 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006276
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006278}
6279#endif /* WIFCONTINUED */
6280
Guido van Rossumc9641791998-08-04 15:26:23 +00006281#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006282PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006283"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006284Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006285
6286static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006287posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006288{
Victor Stinner8c62be82010-05-06 00:08:46 +00006289 WAIT_TYPE status;
6290 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006291
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6293 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006294
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006296}
6297#endif /* WIFSTOPPED */
6298
6299#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006300PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006301"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006302Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006303
6304static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006305posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006306{
Victor Stinner8c62be82010-05-06 00:08:46 +00006307 WAIT_TYPE status;
6308 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006309
Victor Stinner8c62be82010-05-06 00:08:46 +00006310 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6311 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006312
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006314}
6315#endif /* WIFSIGNALED */
6316
6317#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006318PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006319"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006320Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006321system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006322
6323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006324posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006325{
Victor Stinner8c62be82010-05-06 00:08:46 +00006326 WAIT_TYPE status;
6327 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006328
Victor Stinner8c62be82010-05-06 00:08:46 +00006329 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6330 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006331
Victor Stinner8c62be82010-05-06 00:08:46 +00006332 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006333}
6334#endif /* WIFEXITED */
6335
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006336#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006337PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006338"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006339Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006340
6341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006342posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006343{
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 WAIT_TYPE status;
6345 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006346
Victor Stinner8c62be82010-05-06 00:08:46 +00006347 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6348 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006349
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006351}
6352#endif /* WEXITSTATUS */
6353
6354#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006355PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006356"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006357Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006359
6360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006361posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006362{
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 WAIT_TYPE status;
6364 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006365
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6367 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006368
Victor Stinner8c62be82010-05-06 00:08:46 +00006369 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006370}
6371#endif /* WTERMSIG */
6372
6373#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006374PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006375"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006376Return the signal that stopped the process that provided\n\
6377the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006378
6379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006380posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006381{
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 WAIT_TYPE status;
6383 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006384
Victor Stinner8c62be82010-05-06 00:08:46 +00006385 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6386 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006387
Victor Stinner8c62be82010-05-06 00:08:46 +00006388 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006389}
6390#endif /* WSTOPSIG */
6391
6392#endif /* HAVE_SYS_WAIT_H */
6393
6394
Thomas Wouters477c8d52006-05-27 19:21:47 +00006395#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006396#ifdef _SCO_DS
6397/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6398 needed definitions in sys/statvfs.h */
6399#define _SVID3
6400#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006401#include <sys/statvfs.h>
6402
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006403static PyObject*
6404_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006405 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6406 if (v == NULL)
6407 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006408
6409#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006410 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6411 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6412 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6413 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6414 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6415 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6416 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6417 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6418 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6419 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006420#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006421 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6422 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6423 PyStructSequence_SET_ITEM(v, 2,
6424 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6425 PyStructSequence_SET_ITEM(v, 3,
6426 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6427 PyStructSequence_SET_ITEM(v, 4,
6428 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6429 PyStructSequence_SET_ITEM(v, 5,
6430 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6431 PyStructSequence_SET_ITEM(v, 6,
6432 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6433 PyStructSequence_SET_ITEM(v, 7,
6434 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6435 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6436 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006437#endif
6438
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006440}
6441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006442PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006443"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006444Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006445
6446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006447posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006448{
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 int fd, res;
6450 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006451
Victor Stinner8c62be82010-05-06 00:08:46 +00006452 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6453 return NULL;
6454 Py_BEGIN_ALLOW_THREADS
6455 res = fstatvfs(fd, &st);
6456 Py_END_ALLOW_THREADS
6457 if (res != 0)
6458 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006459
Victor Stinner8c62be82010-05-06 00:08:46 +00006460 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006461}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006462#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006463
6464
Thomas Wouters477c8d52006-05-27 19:21:47 +00006465#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006466#include <sys/statvfs.h>
6467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006468PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006469"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006470Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006471
6472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006473posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006474{
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 char *path;
6476 int res;
6477 struct statvfs st;
6478 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6479 return NULL;
6480 Py_BEGIN_ALLOW_THREADS
6481 res = statvfs(path, &st);
6482 Py_END_ALLOW_THREADS
6483 if (res != 0)
6484 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006485
Victor Stinner8c62be82010-05-06 00:08:46 +00006486 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006487}
6488#endif /* HAVE_STATVFS */
6489
Fred Drakec9680921999-12-13 16:37:25 +00006490/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6491 * It maps strings representing configuration variable names to
6492 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006493 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006494 * rarely-used constants. There are three separate tables that use
6495 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006496 *
6497 * This code is always included, even if none of the interfaces that
6498 * need it are included. The #if hackery needed to avoid it would be
6499 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006500 */
6501struct constdef {
6502 char *name;
6503 long value;
6504};
6505
Fred Drake12c6e2d1999-12-14 21:25:03 +00006506static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006507conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006508 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006509{
Christian Heimes217cfd12007-12-02 14:31:20 +00006510 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006511 *valuep = PyLong_AS_LONG(arg);
6512 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006513 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006514 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006515 /* look up the value in the table using a binary search */
6516 size_t lo = 0;
6517 size_t mid;
6518 size_t hi = tablesize;
6519 int cmp;
6520 const char *confname;
6521 if (!PyUnicode_Check(arg)) {
6522 PyErr_SetString(PyExc_TypeError,
6523 "configuration names must be strings or integers");
6524 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006525 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006526 confname = _PyUnicode_AsString(arg);
6527 if (confname == NULL)
6528 return 0;
6529 while (lo < hi) {
6530 mid = (lo + hi) / 2;
6531 cmp = strcmp(confname, table[mid].name);
6532 if (cmp < 0)
6533 hi = mid;
6534 else if (cmp > 0)
6535 lo = mid + 1;
6536 else {
6537 *valuep = table[mid].value;
6538 return 1;
6539 }
6540 }
6541 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6542 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006543 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006544}
6545
6546
6547#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6548static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006549#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006550 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006551#endif
6552#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006553 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006554#endif
Fred Drakec9680921999-12-13 16:37:25 +00006555#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006557#endif
6558#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006560#endif
6561#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006562 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006563#endif
6564#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006565 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006566#endif
6567#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006569#endif
6570#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006571 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006572#endif
6573#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006575#endif
6576#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006578#endif
6579#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006580 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006581#endif
6582#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006584#endif
6585#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006586 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006587#endif
6588#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006590#endif
6591#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006593#endif
6594#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006596#endif
6597#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006599#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006600#ifdef _PC_ACL_ENABLED
6601 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6602#endif
6603#ifdef _PC_MIN_HOLE_SIZE
6604 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6605#endif
6606#ifdef _PC_ALLOC_SIZE_MIN
6607 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6608#endif
6609#ifdef _PC_REC_INCR_XFER_SIZE
6610 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6611#endif
6612#ifdef _PC_REC_MAX_XFER_SIZE
6613 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6614#endif
6615#ifdef _PC_REC_MIN_XFER_SIZE
6616 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6617#endif
6618#ifdef _PC_REC_XFER_ALIGN
6619 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6620#endif
6621#ifdef _PC_SYMLINK_MAX
6622 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6623#endif
6624#ifdef _PC_XATTR_ENABLED
6625 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6626#endif
6627#ifdef _PC_XATTR_EXISTS
6628 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6629#endif
6630#ifdef _PC_TIMESTAMP_RESOLUTION
6631 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6632#endif
Fred Drakec9680921999-12-13 16:37:25 +00006633};
6634
Fred Drakec9680921999-12-13 16:37:25 +00006635static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006636conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006637{
6638 return conv_confname(arg, valuep, posix_constants_pathconf,
6639 sizeof(posix_constants_pathconf)
6640 / sizeof(struct constdef));
6641}
6642#endif
6643
6644#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006645PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006646"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006647Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006648If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006649
6650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006651posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006652{
6653 PyObject *result = NULL;
6654 int name, fd;
6655
Fred Drake12c6e2d1999-12-14 21:25:03 +00006656 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6657 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006658 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006659
Stefan Krah0e803b32010-11-26 16:16:47 +00006660 errno = 0;
6661 limit = fpathconf(fd, name);
6662 if (limit == -1 && errno != 0)
6663 posix_error();
6664 else
6665 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006666 }
6667 return result;
6668}
6669#endif
6670
6671
6672#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006673PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006674"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006675Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006676If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006677
6678static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006679posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006680{
6681 PyObject *result = NULL;
6682 int name;
6683 char *path;
6684
6685 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6686 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006688
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 errno = 0;
6690 limit = pathconf(path, name);
6691 if (limit == -1 && errno != 0) {
6692 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006693 /* could be a path or name problem */
6694 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006695 else
Stefan Krah99439262010-11-26 12:58:05 +00006696 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 }
6698 else
6699 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006700 }
6701 return result;
6702}
6703#endif
6704
6705#ifdef HAVE_CONFSTR
6706static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006707#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006709#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006710#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006712#endif
6713#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006715#endif
Fred Draked86ed291999-12-15 15:34:33 +00006716#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006718#endif
6719#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006721#endif
6722#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006724#endif
6725#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006727#endif
Fred Drakec9680921999-12-13 16:37:25 +00006728#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006730#endif
6731#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006732 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006733#endif
6734#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006735 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006736#endif
6737#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006738 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006739#endif
6740#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006742#endif
6743#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006745#endif
6746#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006748#endif
6749#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006750 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006751#endif
Fred Draked86ed291999-12-15 15:34:33 +00006752#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006753 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006754#endif
Fred Drakec9680921999-12-13 16:37:25 +00006755#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006757#endif
Fred Draked86ed291999-12-15 15:34:33 +00006758#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006760#endif
6761#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006762 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006763#endif
6764#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006765 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006766#endif
6767#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006769#endif
Fred Drakec9680921999-12-13 16:37:25 +00006770#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006771 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006772#endif
6773#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006775#endif
6776#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006777 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006778#endif
6779#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006780 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006781#endif
6782#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006783 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006784#endif
6785#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006786 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006787#endif
6788#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006790#endif
6791#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006793#endif
6794#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006795 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006796#endif
6797#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006799#endif
6800#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006801 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006802#endif
6803#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006805#endif
6806#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006808#endif
6809#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006811#endif
6812#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006814#endif
6815#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006817#endif
Fred Draked86ed291999-12-15 15:34:33 +00006818#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006820#endif
6821#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006823#endif
6824#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006826#endif
6827#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006829#endif
6830#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006832#endif
6833#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006834 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006835#endif
6836#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006838#endif
6839#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006840 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006841#endif
6842#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006844#endif
6845#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006847#endif
6848#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006850#endif
6851#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006852 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006853#endif
6854#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006856#endif
Fred Drakec9680921999-12-13 16:37:25 +00006857};
6858
6859static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006860conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006861{
6862 return conv_confname(arg, valuep, posix_constants_confstr,
6863 sizeof(posix_constants_confstr)
6864 / sizeof(struct constdef));
6865}
6866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006867PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006868"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006869Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006870
6871static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006872posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006873{
6874 PyObject *result = NULL;
6875 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006876 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006877 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006878
Victor Stinnercb043522010-09-10 23:49:04 +00006879 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6880 return NULL;
6881
6882 errno = 0;
6883 len = confstr(name, buffer, sizeof(buffer));
6884 if (len == 0) {
6885 if (errno) {
6886 posix_error();
6887 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006888 }
6889 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006890 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006891 }
6892 }
Victor Stinnercb043522010-09-10 23:49:04 +00006893
6894 if ((unsigned int)len >= sizeof(buffer)) {
6895 char *buf = PyMem_Malloc(len);
6896 if (buf == NULL)
6897 return PyErr_NoMemory();
6898 confstr(name, buf, len);
6899 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6900 PyMem_Free(buf);
6901 }
6902 else
6903 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006904 return result;
6905}
6906#endif
6907
6908
6909#ifdef HAVE_SYSCONF
6910static struct constdef posix_constants_sysconf[] = {
6911#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006913#endif
6914#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006916#endif
6917#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006919#endif
6920#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006922#endif
6923#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006924 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006925#endif
6926#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006928#endif
6929#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006931#endif
6932#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006934#endif
6935#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006937#endif
6938#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006940#endif
Fred Draked86ed291999-12-15 15:34:33 +00006941#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006943#endif
6944#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006946#endif
Fred Drakec9680921999-12-13 16:37:25 +00006947#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006949#endif
Fred Drakec9680921999-12-13 16:37:25 +00006950#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006952#endif
6953#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006955#endif
6956#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006958#endif
6959#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006961#endif
6962#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006964#endif
Fred Draked86ed291999-12-15 15:34:33 +00006965#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006967#endif
Fred Drakec9680921999-12-13 16:37:25 +00006968#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006970#endif
6971#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006973#endif
6974#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006976#endif
6977#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006979#endif
6980#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006982#endif
Fred Draked86ed291999-12-15 15:34:33 +00006983#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006985#endif
Fred Drakec9680921999-12-13 16:37:25 +00006986#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006988#endif
6989#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006991#endif
6992#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006994#endif
6995#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006997#endif
6998#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007000#endif
7001#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007003#endif
7004#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007006#endif
7007#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007009#endif
7010#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007011 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007012#endif
7013#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007015#endif
7016#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007018#endif
7019#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007021#endif
7022#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007024#endif
7025#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
7028#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007030#endif
7031#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007033#endif
7034#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007036#endif
7037#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007039#endif
7040#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007042#endif
7043#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007045#endif
7046#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007048#endif
7049#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007051#endif
7052#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007054#endif
Fred Draked86ed291999-12-15 15:34:33 +00007055#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007057#endif
Fred Drakec9680921999-12-13 16:37:25 +00007058#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007060#endif
7061#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007063#endif
7064#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007066#endif
Fred Draked86ed291999-12-15 15:34:33 +00007067#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007069#endif
Fred Drakec9680921999-12-13 16:37:25 +00007070#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007072#endif
Fred Draked86ed291999-12-15 15:34:33 +00007073#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007075#endif
7076#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007078#endif
Fred Drakec9680921999-12-13 16:37:25 +00007079#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007081#endif
7082#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007084#endif
7085#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007087#endif
7088#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007090#endif
Fred Draked86ed291999-12-15 15:34:33 +00007091#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007093#endif
Fred Drakec9680921999-12-13 16:37:25 +00007094#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007096#endif
7097#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007099#endif
7100#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007102#endif
7103#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007104 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007105#endif
7106#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007108#endif
7109#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007111#endif
7112#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007114#endif
Fred Draked86ed291999-12-15 15:34:33 +00007115#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007116 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007117#endif
Fred Drakec9680921999-12-13 16:37:25 +00007118#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007120#endif
7121#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007123#endif
Fred Draked86ed291999-12-15 15:34:33 +00007124#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007126#endif
Fred Drakec9680921999-12-13 16:37:25 +00007127#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007129#endif
7130#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007132#endif
7133#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007135#endif
7136#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007138#endif
7139#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007141#endif
7142#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007144#endif
7145#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007147#endif
7148#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007150#endif
7151#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007153#endif
Fred Draked86ed291999-12-15 15:34:33 +00007154#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007156#endif
7157#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007159#endif
Fred Drakec9680921999-12-13 16:37:25 +00007160#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007162#endif
7163#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007165#endif
7166#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007168#endif
7169#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007171#endif
7172#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007173 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007174#endif
7175#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007177#endif
7178#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007180#endif
7181#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007183#endif
7184#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007186#endif
7187#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007188 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007189#endif
7190#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007192#endif
7193#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007195#endif
7196#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007198#endif
7199#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007201#endif
7202#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007204#endif
7205#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007207#endif
7208#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007210#endif
7211#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007213#endif
7214#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007215 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007216#endif
7217#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007219#endif
7220#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007222#endif
7223#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007225#endif
7226#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007228#endif
7229#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007231#endif
7232#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007234#endif
7235#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007237#endif
7238#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007240#endif
7241#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007243#endif
7244#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007246#endif
7247#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007249#endif
7250#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007252#endif
7253#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007255#endif
7256#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007258#endif
7259#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007261#endif
7262#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007263 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007264#endif
Fred Draked86ed291999-12-15 15:34:33 +00007265#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007267#endif
Fred Drakec9680921999-12-13 16:37:25 +00007268#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007270#endif
7271#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007272 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007273#endif
7274#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007276#endif
7277#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007278 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007279#endif
7280#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007281 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007282#endif
7283#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007284 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007285#endif
7286#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007287 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007288#endif
7289#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007291#endif
7292#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007294#endif
7295#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007297#endif
7298#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007299 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007300#endif
7301#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007303#endif
7304#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007305 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007306#endif
7307#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007309#endif
7310#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007311 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007312#endif
7313#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007315#endif
7316#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007318#endif
7319#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007321#endif
7322#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007324#endif
7325#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007327#endif
7328#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007329 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007330#endif
7331#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007332 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007333#endif
7334#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007335 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007336#endif
7337#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007338 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007339#endif
7340#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007341 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007342#endif
7343#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007345#endif
7346#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007348#endif
7349#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007351#endif
7352#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007353 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007354#endif
7355#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007357#endif
7358#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007360#endif
7361#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007363#endif
7364#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007366#endif
7367#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007369#endif
7370#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007372#endif
7373#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007375#endif
7376#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007378#endif
7379#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007381#endif
7382#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007384#endif
7385#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007386 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007387#endif
7388#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007390#endif
7391#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007392 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007393#endif
7394#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007395 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007396#endif
7397#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007398 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007399#endif
7400#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007401 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007402#endif
7403};
7404
7405static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007406conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007407{
7408 return conv_confname(arg, valuep, posix_constants_sysconf,
7409 sizeof(posix_constants_sysconf)
7410 / sizeof(struct constdef));
7411}
7412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007413PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007414"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007415Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007416
7417static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007418posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007419{
7420 PyObject *result = NULL;
7421 int name;
7422
7423 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7424 int value;
7425
7426 errno = 0;
7427 value = sysconf(name);
7428 if (value == -1 && errno != 0)
7429 posix_error();
7430 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007431 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007432 }
7433 return result;
7434}
7435#endif
7436
7437
Fred Drakebec628d1999-12-15 18:31:10 +00007438/* This code is used to ensure that the tables of configuration value names
7439 * are in sorted order as required by conv_confname(), and also to build the
7440 * the exported dictionaries that are used to publish information about the
7441 * names available on the host platform.
7442 *
7443 * Sorting the table at runtime ensures that the table is properly ordered
7444 * when used, even for platforms we're not able to test on. It also makes
7445 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007446 */
Fred Drakebec628d1999-12-15 18:31:10 +00007447
7448static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007449cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007450{
7451 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007453 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007454 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007455
7456 return strcmp(c1->name, c2->name);
7457}
7458
7459static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007460setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007462{
Fred Drakebec628d1999-12-15 18:31:10 +00007463 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007464 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007465
7466 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7467 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007468 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007469 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007470
Barry Warsaw3155db32000-04-13 15:20:40 +00007471 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 PyObject *o = PyLong_FromLong(table[i].value);
7473 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7474 Py_XDECREF(o);
7475 Py_DECREF(d);
7476 return -1;
7477 }
7478 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007479 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007480 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007481}
7482
Fred Drakebec628d1999-12-15 18:31:10 +00007483/* Return -1 on failure, 0 on success. */
7484static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007485setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007486{
7487#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007488 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007489 sizeof(posix_constants_pathconf)
7490 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007491 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007492 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007493#endif
7494#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007495 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007496 sizeof(posix_constants_confstr)
7497 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007498 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007499 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007500#endif
7501#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007502 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007503 sizeof(posix_constants_sysconf)
7504 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007505 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007506 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007507#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007508 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007509}
Fred Draked86ed291999-12-15 15:34:33 +00007510
7511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007512PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007513"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007514Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007515in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007516
7517static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007518posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007519{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007520 abort();
7521 /*NOTREACHED*/
7522 Py_FatalError("abort() called from Python code didn't abort!");
7523 return NULL;
7524}
Fred Drakebec628d1999-12-15 18:31:10 +00007525
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007526#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007527PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007528"startfile(filepath [, operation]) - Start a file with its associated\n\
7529application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007530\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007531When \"operation\" is not specified or \"open\", this acts like\n\
7532double-clicking the file in Explorer, or giving the file name as an\n\
7533argument to the DOS \"start\" command: the file is opened with whatever\n\
7534application (if any) its extension is associated.\n\
7535When another \"operation\" is given, it specifies what should be done with\n\
7536the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007537\n\
7538startfile returns as soon as the associated application is launched.\n\
7539There is no option to wait for the application to close, and no way\n\
7540to retrieve the application's exit status.\n\
7541\n\
7542The filepath is relative to the current directory. If you want to use\n\
7543an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007544the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007545
7546static PyObject *
7547win32_startfile(PyObject *self, PyObject *args)
7548{
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 PyObject *ofilepath;
7550 char *filepath;
7551 char *operation = NULL;
7552 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007553
Victor Stinner8c62be82010-05-06 00:08:46 +00007554 PyObject *unipath, *woperation = NULL;
7555 if (!PyArg_ParseTuple(args, "U|s:startfile",
7556 &unipath, &operation)) {
7557 PyErr_Clear();
7558 goto normal;
7559 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007560
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 if (operation) {
7562 woperation = PyUnicode_DecodeASCII(operation,
7563 strlen(operation), NULL);
7564 if (!woperation) {
7565 PyErr_Clear();
7566 operation = NULL;
7567 goto normal;
7568 }
7569 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007570
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 Py_BEGIN_ALLOW_THREADS
7572 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7573 PyUnicode_AS_UNICODE(unipath),
7574 NULL, NULL, SW_SHOWNORMAL);
7575 Py_END_ALLOW_THREADS
7576
7577 Py_XDECREF(woperation);
7578 if (rc <= (HINSTANCE)32) {
7579 PyObject *errval = win32_error_unicode("startfile",
7580 PyUnicode_AS_UNICODE(unipath));
7581 return errval;
7582 }
7583 Py_INCREF(Py_None);
7584 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007585
7586normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7588 PyUnicode_FSConverter, &ofilepath,
7589 &operation))
7590 return NULL;
7591 filepath = PyBytes_AsString(ofilepath);
7592 Py_BEGIN_ALLOW_THREADS
7593 rc = ShellExecute((HWND)0, operation, filepath,
7594 NULL, NULL, SW_SHOWNORMAL);
7595 Py_END_ALLOW_THREADS
7596 if (rc <= (HINSTANCE)32) {
7597 PyObject *errval = win32_error("startfile", filepath);
7598 Py_DECREF(ofilepath);
7599 return errval;
7600 }
7601 Py_DECREF(ofilepath);
7602 Py_INCREF(Py_None);
7603 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007604}
7605#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007606
Martin v. Löwis438b5342002-12-27 10:16:42 +00007607#ifdef HAVE_GETLOADAVG
7608PyDoc_STRVAR(posix_getloadavg__doc__,
7609"getloadavg() -> (float, float, float)\n\n\
7610Return the number of processes in the system run queue averaged over\n\
7611the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7612was unobtainable");
7613
7614static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007615posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007616{
7617 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007618 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007619 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7620 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007621 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007622 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007623}
7624#endif
7625
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007626#ifdef MS_WINDOWS
7627
7628PyDoc_STRVAR(win32_urandom__doc__,
7629"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007630Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007631
7632typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7633 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7634 DWORD dwFlags );
7635typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7636 BYTE *pbBuffer );
7637
7638static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007639/* This handle is never explicitly released. Instead, the operating
7640 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007641static HCRYPTPROV hCryptProv = 0;
7642
Tim Peters4ad82172004-08-30 17:02:04 +00007643static PyObject*
7644win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007645{
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 int howMany;
7647 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007648
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 /* Read arguments */
7650 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7651 return NULL;
7652 if (howMany < 0)
7653 return PyErr_Format(PyExc_ValueError,
7654 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007655
Victor Stinner8c62be82010-05-06 00:08:46 +00007656 if (hCryptProv == 0) {
7657 HINSTANCE hAdvAPI32 = NULL;
7658 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007659
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 /* Obtain handle to the DLL containing CryptoAPI
7661 This should not fail */
7662 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7663 if(hAdvAPI32 == NULL)
7664 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007665
Victor Stinner8c62be82010-05-06 00:08:46 +00007666 /* Obtain pointers to the CryptoAPI functions
7667 This will fail on some early versions of Win95 */
7668 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7669 hAdvAPI32,
7670 "CryptAcquireContextA");
7671 if (pCryptAcquireContext == NULL)
7672 return PyErr_Format(PyExc_NotImplementedError,
7673 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007674
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7676 hAdvAPI32, "CryptGenRandom");
7677 if (pCryptGenRandom == NULL)
7678 return PyErr_Format(PyExc_NotImplementedError,
7679 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007680
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 /* Acquire context */
7682 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7683 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7684 return win32_error("CryptAcquireContext", NULL);
7685 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007686
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 /* Allocate bytes */
7688 result = PyBytes_FromStringAndSize(NULL, howMany);
7689 if (result != NULL) {
7690 /* Get random data */
7691 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7692 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7693 PyBytes_AS_STRING(result))) {
7694 Py_DECREF(result);
7695 return win32_error("CryptGenRandom", NULL);
7696 }
7697 }
7698 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007699}
7700#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007701
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007702PyDoc_STRVAR(device_encoding__doc__,
7703"device_encoding(fd) -> str\n\n\
7704Return a string describing the encoding of the device\n\
7705if the output is a terminal; else return None.");
7706
7707static PyObject *
7708device_encoding(PyObject *self, PyObject *args)
7709{
Victor Stinner8c62be82010-05-06 00:08:46 +00007710 int fd;
7711 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7712 return NULL;
7713 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7714 Py_INCREF(Py_None);
7715 return Py_None;
7716 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007717#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 if (fd == 0) {
7719 char buf[100];
7720 sprintf(buf, "cp%d", GetConsoleCP());
7721 return PyUnicode_FromString(buf);
7722 }
7723 if (fd == 1 || fd == 2) {
7724 char buf[100];
7725 sprintf(buf, "cp%d", GetConsoleOutputCP());
7726 return PyUnicode_FromString(buf);
7727 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007728#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 {
7730 char *codeset = nl_langinfo(CODESET);
7731 if (codeset != NULL && codeset[0] != 0)
7732 return PyUnicode_FromString(codeset);
7733 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007734#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007735 Py_INCREF(Py_None);
7736 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007737}
7738
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007739#ifdef __VMS
7740/* Use openssl random routine */
7741#include <openssl/rand.h>
7742PyDoc_STRVAR(vms_urandom__doc__,
7743"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007744Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007745
7746static PyObject*
7747vms_urandom(PyObject *self, PyObject *args)
7748{
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 int howMany;
7750 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007751
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 /* Read arguments */
7753 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7754 return NULL;
7755 if (howMany < 0)
7756 return PyErr_Format(PyExc_ValueError,
7757 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007758
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 /* Allocate bytes */
7760 result = PyBytes_FromStringAndSize(NULL, howMany);
7761 if (result != NULL) {
7762 /* Get random data */
7763 if (RAND_pseudo_bytes((unsigned char*)
7764 PyBytes_AS_STRING(result),
7765 howMany) < 0) {
7766 Py_DECREF(result);
7767 return PyErr_Format(PyExc_ValueError,
7768 "RAND_pseudo_bytes");
7769 }
7770 }
7771 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007772}
7773#endif
7774
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007775#ifdef HAVE_SETRESUID
7776PyDoc_STRVAR(posix_setresuid__doc__,
7777"setresuid(ruid, euid, suid)\n\n\
7778Set the current process's real, effective, and saved user ids.");
7779
7780static PyObject*
7781posix_setresuid (PyObject *self, PyObject *args)
7782{
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 /* We assume uid_t is no larger than a long. */
7784 long ruid, euid, suid;
7785 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7786 return NULL;
7787 if (setresuid(ruid, euid, suid) < 0)
7788 return posix_error();
7789 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007790}
7791#endif
7792
7793#ifdef HAVE_SETRESGID
7794PyDoc_STRVAR(posix_setresgid__doc__,
7795"setresgid(rgid, egid, sgid)\n\n\
7796Set the current process's real, effective, and saved group ids.");
7797
7798static PyObject*
7799posix_setresgid (PyObject *self, PyObject *args)
7800{
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 /* We assume uid_t is no larger than a long. */
7802 long rgid, egid, sgid;
7803 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7804 return NULL;
7805 if (setresgid(rgid, egid, sgid) < 0)
7806 return posix_error();
7807 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007808}
7809#endif
7810
7811#ifdef HAVE_GETRESUID
7812PyDoc_STRVAR(posix_getresuid__doc__,
7813"getresuid() -> (ruid, euid, suid)\n\n\
7814Get tuple of the current process's real, effective, and saved user ids.");
7815
7816static PyObject*
7817posix_getresuid (PyObject *self, PyObject *noargs)
7818{
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 uid_t ruid, euid, suid;
7820 long l_ruid, l_euid, l_suid;
7821 if (getresuid(&ruid, &euid, &suid) < 0)
7822 return posix_error();
7823 /* Force the values into long's as we don't know the size of uid_t. */
7824 l_ruid = ruid;
7825 l_euid = euid;
7826 l_suid = suid;
7827 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007828}
7829#endif
7830
7831#ifdef HAVE_GETRESGID
7832PyDoc_STRVAR(posix_getresgid__doc__,
7833"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007834Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007835
7836static PyObject*
7837posix_getresgid (PyObject *self, PyObject *noargs)
7838{
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 uid_t rgid, egid, sgid;
7840 long l_rgid, l_egid, l_sgid;
7841 if (getresgid(&rgid, &egid, &sgid) < 0)
7842 return posix_error();
7843 /* Force the values into long's as we don't know the size of uid_t. */
7844 l_rgid = rgid;
7845 l_egid = egid;
7846 l_sgid = sgid;
7847 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007848}
7849#endif
7850
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007851static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007852 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007853#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007855#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007857#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007859#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007860 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007861#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007863#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007864#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007866#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007867#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007869#endif /* HAVE_LCHMOD */
7870#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007872#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007873#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007875#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007876#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007878#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007879#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007881#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007882#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007884#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007885#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7887 METH_NOARGS, posix_getcwd__doc__},
7888 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7889 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007890#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007891#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007893#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7895 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7896 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007897#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007899#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007900#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007902#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007903#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007904 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007905#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007906 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7907 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7908 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007910#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007911 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007912#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007913#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007914 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007915 win_symlink__doc__},
7916#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007917#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007919#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007921#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007923#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7925 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7926 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007927#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007929#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007931#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7933 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007934#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007935#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7937 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007938#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7940 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007941#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007942#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007943#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007944 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007945#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007946#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007947 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007948#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007949#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007950 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007951#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007952#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007953 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007954#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007955#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007956 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007957#endif /* HAVE_GETEGID */
7958#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007959 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007960#endif /* HAVE_GETEUID */
7961#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007962 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007963#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007964#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007966#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007968#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007970#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007971#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007973#endif /* HAVE_GETPPID */
7974#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007976#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007977#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007979#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007980#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007982#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007983#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007985#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007986#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007988#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007989#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7991 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007992 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007993#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007994#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007996#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007997#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007999#endif /* HAVE_SETEUID */
8000#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008002#endif /* HAVE_SETEGID */
8003#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008005#endif /* HAVE_SETREUID */
8006#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008008#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008009#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008011#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008012#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008014#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00008015#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00008017#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008018#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008020#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008021#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008023#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008024#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008025 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008026#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008027#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00008028 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008029#endif /* HAVE_WAIT3 */
8030#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008032#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008033#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008035#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008036#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008037 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008038#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008039#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008041#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008042#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008044#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008045#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008047#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008048#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008049 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008050#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8052 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8053 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8054 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
8055 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8056 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8057 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8058 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8059 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8060 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8061 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008062#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008064#endif
8065#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008067#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008068#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008070#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008071#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8073 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8074 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008075#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008076#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008078#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008079#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008080 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008081#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008082#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008083 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008084#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008085 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008086#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008088#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008089#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008091#endif
8092#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008093 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008094#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008095#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008096#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008098#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008099#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008101#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008102#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008104#endif /* WIFSTOPPED */
8105#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008107#endif /* WIFSIGNALED */
8108#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008110#endif /* WIFEXITED */
8111#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008113#endif /* WEXITSTATUS */
8114#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008116#endif /* WTERMSIG */
8117#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008119#endif /* WSTOPSIG */
8120#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008121#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008122 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008123#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008124#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008125 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008126#endif
Fred Drakec9680921999-12-13 16:37:25 +00008127#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008128 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008129#endif
8130#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008131 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008132#endif
8133#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008134 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008135#endif
8136#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008137 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008138#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008140#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008142 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008143 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -05008144 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00008145#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008146#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008148#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008149 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008151 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008152 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008154 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008155#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008157#endif
8158#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008160#endif
8161#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008163#endif
8164#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008165 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008166#endif
8167
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008169};
8170
8171
Barry Warsaw4a342091996-12-19 23:50:02 +00008172static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008173ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008174{
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008176}
8177
Guido van Rossumd48f2521997-12-05 22:19:34 +00008178#if defined(PYOS_OS2)
8179/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008180static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008181{
8182 APIRET rc;
8183 ULONG values[QSV_MAX+1];
8184 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008185 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008186
8187 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008188 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008189 Py_END_ALLOW_THREADS
8190
8191 if (rc != NO_ERROR) {
8192 os2_error(rc);
8193 return -1;
8194 }
8195
Fred Drake4d1e64b2002-04-15 19:40:07 +00008196 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8197 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8198 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8199 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8200 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8201 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8202 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008203
8204 switch (values[QSV_VERSION_MINOR]) {
8205 case 0: ver = "2.00"; break;
8206 case 10: ver = "2.10"; break;
8207 case 11: ver = "2.11"; break;
8208 case 30: ver = "3.00"; break;
8209 case 40: ver = "4.00"; break;
8210 case 50: ver = "5.00"; break;
8211 default:
Tim Peters885d4572001-11-28 20:27:42 +00008212 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008213 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008214 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008215 ver = &tmp[0];
8216 }
8217
8218 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008219 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008220 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008221
8222 /* Add Indicator of Which Drive was Used to Boot the System */
8223 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8224 tmp[1] = ':';
8225 tmp[2] = '\0';
8226
Fred Drake4d1e64b2002-04-15 19:40:07 +00008227 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008228}
8229#endif
8230
Brian Curtin52173d42010-12-02 18:29:18 +00008231#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008232static int
Brian Curtin52173d42010-12-02 18:29:18 +00008233enable_symlink()
8234{
8235 HANDLE tok;
8236 TOKEN_PRIVILEGES tok_priv;
8237 LUID luid;
8238 int meth_idx = 0;
8239
8240 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008241 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008242
8243 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008244 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008245
8246 tok_priv.PrivilegeCount = 1;
8247 tok_priv.Privileges[0].Luid = luid;
8248 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8249
8250 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8251 sizeof(TOKEN_PRIVILEGES),
8252 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008253 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008254
Brian Curtin3b4499c2010-12-28 14:31:47 +00008255 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8256 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008257}
8258#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8259
Barry Warsaw4a342091996-12-19 23:50:02 +00008260static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008261all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008262{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008263#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008265#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008266#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008267 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008268#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008269#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008271#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008272#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008274#endif
Fred Drakec9680921999-12-13 16:37:25 +00008275#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008277#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008278#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008280#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008281#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008282 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008283#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008284#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008286#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008287#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008289#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008290#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008291 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008292#endif
8293#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008294 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008295#endif
8296#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008297 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008298#endif
8299#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008300 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008301#endif
8302#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008303 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008304#endif
8305#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008306 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008307#endif
8308#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008310#endif
8311#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008313#endif
8314#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008315 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008316#endif
8317#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008318 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008319#endif
8320#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008322#endif
8323#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008324 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008325#endif
8326#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008327 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008328#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008329#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008331#endif
8332#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008334#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008335#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008337#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008338#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008340#endif
8341#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008343#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008344
Tim Peters5aa91602002-01-30 05:46:57 +00008345/* MS Windows */
8346#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008347 /* Don't inherit in child processes. */
8348 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008349#endif
8350#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008351 /* Optimize for short life (keep in memory). */
8352 /* MS forgot to define this one with a non-underscore form too. */
8353 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008354#endif
8355#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 /* Automatically delete when last handle is closed. */
8357 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008358#endif
8359#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008360 /* Optimize for random access. */
8361 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008362#endif
8363#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008364 /* Optimize for sequential access. */
8365 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008366#endif
8367
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008368/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008369#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 /* Send a SIGIO signal whenever input or output
8371 becomes available on file descriptor */
8372 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008373#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008374#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 /* Direct disk access. */
8376 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008377#endif
8378#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 /* Must be a directory. */
8380 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008381#endif
8382#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 /* Do not follow links. */
8384 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008385#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008386#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 /* Do not update the access time. */
8388 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008389#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008390
Victor Stinner8c62be82010-05-06 00:08:46 +00008391 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008392#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008393 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008394#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008395#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008397#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008398#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008399 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008400#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008401#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008403#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008404#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008405 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008406#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008407#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008409#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008410#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008412#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008413#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008415#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008416#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008418#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008419#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008420 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008421#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008422#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008423 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008424#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008425#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008426 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008427#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008428#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008430#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008431#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008432 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008433#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008434#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008435 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008436#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008437#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008439#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008440#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008442#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008443
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008444 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008445#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008446 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008447#endif /* ST_RDONLY */
8448#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008449 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008450#endif /* ST_NOSUID */
8451
Guido van Rossum246bc171999-02-01 23:54:31 +00008452#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008453#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008454 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8455 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8456 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8457 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8458 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8459 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8460 if (ins(d, "P_PM", (long)P_PM)) return -1;
8461 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8462 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8463 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8464 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8465 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8466 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8467 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8468 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8469 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8470 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8471 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8472 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8473 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008474#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8476 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8477 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8478 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8479 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008480#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008481#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008482
Guido van Rossumd48f2521997-12-05 22:19:34 +00008483#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008485#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008486 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008487}
8488
8489
Tim Peters5aa91602002-01-30 05:46:57 +00008490#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008491#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008492#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008493
8494#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008495#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008496#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008497
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008498#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008499#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008500#define MODNAME "posix"
8501#endif
8502
Martin v. Löwis1a214512008-06-11 05:26:20 +00008503static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008504 PyModuleDef_HEAD_INIT,
8505 MODNAME,
8506 posix__doc__,
8507 -1,
8508 posix_methods,
8509 NULL,
8510 NULL,
8511 NULL,
8512 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008513};
8514
8515
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008516PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008517INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008518{
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008520
Brian Curtin52173d42010-12-02 18:29:18 +00008521#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008522 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008523#endif
8524
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 m = PyModule_Create(&posixmodule);
8526 if (m == NULL)
8527 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008528
Victor Stinner8c62be82010-05-06 00:08:46 +00008529 /* Initialize environ dictionary */
8530 v = convertenviron();
8531 Py_XINCREF(v);
8532 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8533 return NULL;
8534 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008535
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 if (all_ins(m))
8537 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008538
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 if (setup_confname_tables(m))
8540 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008541
Victor Stinner8c62be82010-05-06 00:08:46 +00008542 Py_INCREF(PyExc_OSError);
8543 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008544
Guido van Rossumb3d39562000-01-31 18:41:26 +00008545#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008546 if (posix_putenv_garbage == NULL)
8547 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008548#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008549
Victor Stinner8c62be82010-05-06 00:08:46 +00008550 if (!initialized) {
8551 stat_result_desc.name = MODNAME ".stat_result";
8552 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8553 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8554 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8555 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8556 structseq_new = StatResultType.tp_new;
8557 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008558
Victor Stinner8c62be82010-05-06 00:08:46 +00008559 statvfs_result_desc.name = MODNAME ".statvfs_result";
8560 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008561#ifdef NEED_TICKS_PER_SECOND
8562# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008563 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008564# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008565 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008566# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008567 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008568# endif
8569#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008570 }
8571 Py_INCREF((PyObject*) &StatResultType);
8572 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8573 Py_INCREF((PyObject*) &StatVFSResultType);
8574 PyModule_AddObject(m, "statvfs_result",
8575 (PyObject*) &StatVFSResultType);
8576 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008577
8578#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008579 /*
8580 * Step 2 of weak-linking support on Mac OS X.
8581 *
8582 * The code below removes functions that are not available on the
8583 * currently active platform.
8584 *
8585 * This block allow one to use a python binary that was build on
8586 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8587 * OSX 10.4.
8588 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008589#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008590 if (fstatvfs == NULL) {
8591 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8592 return NULL;
8593 }
8594 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008595#endif /* HAVE_FSTATVFS */
8596
8597#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008598 if (statvfs == NULL) {
8599 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8600 return NULL;
8601 }
8602 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008603#endif /* HAVE_STATVFS */
8604
8605# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008606 if (lchown == NULL) {
8607 if (PyObject_DelAttrString(m, "lchown") == -1) {
8608 return NULL;
8609 }
8610 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008611#endif /* HAVE_LCHOWN */
8612
8613
8614#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008615 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008616
Guido van Rossumb6775db1994-08-01 11:34:53 +00008617}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008618
8619#ifdef __cplusplus
8620}
8621#endif