blob: 34dff11257efc57705ccf59b7b523c84b77030aa [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +020029#ifndef MS_WINDOWS
30#include "posixmodule.h"
31#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000032
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000072#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000073#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_WAIT 1
121#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000124#define HAVE_GETPPID 1
Victor Stinner26de69d2011-06-17 15:15:38 +0200125#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000126#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_EXECV 1
128#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000129#define HAVE_SYSTEM 1
130#define HAVE_CWAIT 1
131#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000136#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000154#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000155#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#endif /* _MSC_VER */
157#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000158#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000159#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000160
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000162
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000163#if defined(__sgi)&&_COMPILER_VERSION>=700
164/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
165 (default) */
166extern char *ctermid_r(char *);
167#endif
168
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000169#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000171extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000172#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000173#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000176extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000178#endif
179#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000180extern int chdir(char *);
181extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000182#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(const char *);
184extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000186#ifdef __BORLANDC__
187extern int chmod(const char *, int);
188#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000190#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000191/*#ifdef HAVE_FCHMOD
192extern int fchmod(int, mode_t);
193#endif*/
194/*#ifdef HAVE_LCHMOD
195extern int lchmod(const char *, mode_t);
196#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int chown(const char *, uid_t, gid_t);
198extern char *getcwd(char *, int);
199extern char *strerror(int);
200extern int link(const char *, const char *);
201extern int rename(const char *, const char *);
202extern int stat(const char *, struct stat *);
203extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000205extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000206#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000208extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000209#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000211
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000212#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#ifdef HAVE_UTIME_H
215#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000216#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000218#ifdef HAVE_SYS_UTIME_H
219#include <sys/utime.h>
220#define HAVE_UTIME_H /* pretend we do for the rest of this file */
221#endif /* HAVE_SYS_UTIME_H */
222
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#ifdef HAVE_SYS_TIMES_H
224#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
227#ifdef HAVE_SYS_PARAM_H
228#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
231#ifdef HAVE_SYS_UTSNAME_H
232#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000237#define NAMLEN(dirent) strlen((dirent)->d_name)
238#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000239#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000240#include <direct.h>
241#define NAMLEN(dirent) strlen((dirent)->d_name)
242#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000245#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#endif
249#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000251#endif
252#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000254#endif
255#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000257#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260#endif
261#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263#endif
264#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000266#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000267#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000268#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000269#endif
270#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000271#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000272#endif
273#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000274#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000275#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000276#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000277#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000279#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000280#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000281#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
282#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000283static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000284#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000285#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286
Guido van Rossumd48f2521997-12-05 22:19:34 +0000287#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000289#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290
Tim Petersbc2e10e2002-03-03 23:17:02 +0000291#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000292#if defined(PATH_MAX) && PATH_MAX > 1024
293#define MAXPATHLEN PATH_MAX
294#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000295#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000296#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000297#endif /* MAXPATHLEN */
298
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000299#ifdef UNION_WAIT
300/* Emulate some macros on systems that have a union instead of macros */
301
302#ifndef WIFEXITED
303#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
304#endif
305
306#ifndef WEXITSTATUS
307#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
308#endif
309
310#ifndef WTERMSIG
311#define WTERMSIG(u_wait) ((u_wait).w_termsig)
312#endif
313
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314#define WAIT_TYPE union wait
315#define WAIT_STATUS_INT(s) (s.w_status)
316
317#else /* !UNION_WAIT */
318#define WAIT_TYPE int
319#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000320#endif /* UNION_WAIT */
321
Greg Wardb48bc172000-03-01 21:51:56 +0000322/* Don't use the "_r" form if we don't need it (also, won't have a
323 prototype for it, at least on Solaris -- maybe others as well?). */
324#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
325#define USE_CTERMID_R
326#endif
327
Fred Drake699f3522000-06-29 21:12:41 +0000328/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000329#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000330#undef FSTAT
331#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000332#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000333# define STAT win32_stat
334# define FSTAT win32_fstat
335# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000336#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000337# define STAT stat
338# define FSTAT fstat
339# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000340#endif
341
Tim Peters11b23062003-04-23 02:39:17 +0000342#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000343#include <sys/mkdev.h>
344#else
345#if defined(MAJOR_IN_SYSMACROS)
346#include <sys/sysmacros.h>
347#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000348#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
349#include <sys/mkdev.h>
350#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000351#endif
Fred Drake699f3522000-06-29 21:12:41 +0000352
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +0200353
354#ifndef MS_WINDOWS
355PyObject *
356_PyLong_FromUid(uid_t uid)
357{
358 if (uid == (uid_t)-1)
359 return PyLong_FromLong(-1);
360 return PyLong_FromUnsignedLong(uid);
361}
362
363PyObject *
364_PyLong_FromGid(gid_t gid)
365{
366 if (gid == (gid_t)-1)
367 return PyLong_FromLong(-1);
368 return PyLong_FromUnsignedLong(gid);
369}
370
371int
372_Py_Uid_Converter(PyObject *obj, void *p)
373{
374 int overflow;
375 long result;
376 if (PyFloat_Check(obj)) {
377 PyErr_SetString(PyExc_TypeError,
378 "integer argument expected, got float");
379 return 0;
380 }
381 result = PyLong_AsLongAndOverflow(obj, &overflow);
382 if (overflow < 0)
383 goto OverflowDown;
384 if (!overflow && result == -1) {
385 /* error or -1 */
386 if (PyErr_Occurred())
387 return 0;
388 *(uid_t *)p = (uid_t)-1;
389 }
390 else {
391 /* unsigned uid_t */
392 unsigned long uresult;
393 if (overflow > 0) {
394 uresult = PyLong_AsUnsignedLong(obj);
395 if (PyErr_Occurred()) {
396 if (PyErr_ExceptionMatches(PyExc_OverflowError))
397 goto OverflowUp;
398 return 0;
399 }
400 if ((uid_t)uresult == (uid_t)-1)
401 goto OverflowUp;
402 } else {
403 if (result < 0)
404 goto OverflowDown;
405 uresult = result;
406 }
407 if (sizeof(uid_t) < sizeof(long) &&
408 (unsigned long)(uid_t)uresult != uresult)
409 goto OverflowUp;
410 *(uid_t *)p = (uid_t)uresult;
411 }
412 return 1;
413
414OverflowDown:
415 PyErr_SetString(PyExc_OverflowError,
416 "user id is less than minimum");
417 return 0;
418
419OverflowUp:
420 PyErr_SetString(PyExc_OverflowError,
421 "user id is greater than maximum");
422 return 0;
423}
424
425int
426_Py_Gid_Converter(PyObject *obj, void *p)
427{
428 int overflow;
429 long result;
430 if (PyFloat_Check(obj)) {
431 PyErr_SetString(PyExc_TypeError,
432 "integer argument expected, got float");
433 return 0;
434 }
435 result = PyLong_AsLongAndOverflow(obj, &overflow);
436 if (overflow < 0)
437 goto OverflowDown;
438 if (!overflow && result == -1) {
439 /* error or -1 */
440 if (PyErr_Occurred())
441 return 0;
442 *(gid_t *)p = (gid_t)-1;
443 }
444 else {
445 /* unsigned gid_t */
446 unsigned long uresult;
447 if (overflow > 0) {
448 uresult = PyLong_AsUnsignedLong(obj);
449 if (PyErr_Occurred()) {
450 if (PyErr_ExceptionMatches(PyExc_OverflowError))
451 goto OverflowUp;
452 return 0;
453 }
454 if ((gid_t)uresult == (gid_t)-1)
455 goto OverflowUp;
456 } else {
457 if (result < 0)
458 goto OverflowDown;
459 uresult = result;
460 }
461 if (sizeof(gid_t) < sizeof(long) &&
462 (unsigned long)(gid_t)uresult != uresult)
463 goto OverflowUp;
464 *(gid_t *)p = (gid_t)uresult;
465 }
466 return 1;
467
468OverflowDown:
469 PyErr_SetString(PyExc_OverflowError,
470 "group id is less than minimum");
471 return 0;
472
473OverflowUp:
474 PyErr_SetString(PyExc_OverflowError,
475 "group id is greater than maximum");
476 return 0;
477}
478#endif /* MS_WINDOWS */
479
480
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000481#if defined _MSC_VER && _MSC_VER >= 1400
482/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov737fb892012-12-18 21:14:22 +0200483 * valid and raise an assertion if it isn't.
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000484 * Normally, an invalid fd is likely to be a C program error and therefore
485 * an assertion can be useful, but it does contradict the POSIX standard
486 * which for write(2) states:
487 * "Otherwise, -1 shall be returned and errno set to indicate the error."
488 * "[EBADF] The fildes argument is not a valid file descriptor open for
489 * writing."
490 * Furthermore, python allows the user to enter any old integer
491 * as a fd and should merely raise a python exception on error.
492 * The Microsoft CRT doesn't provide an official way to check for the
493 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000494 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000495 * internal structures involved.
496 * The structures below must be updated for each version of visual studio
497 * according to the file internal.h in the CRT source, until MS comes
498 * up with a less hacky way to do this.
499 * (all of this is to avoid globally modifying the CRT behaviour using
500 * _set_invalid_parameter_handler() and _CrtSetReportMode())
501 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000502/* The actual size of the structure is determined at runtime.
503 * Only the first items must be present.
504 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000505typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000506 intptr_t osfhnd;
507 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000508} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000509
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000510extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000511#define IOINFO_L2E 5
512#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
513#define IOINFO_ARRAYS 64
514#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
515#define FOPEN 0x01
516#define _NO_CONSOLE_FILENO (intptr_t)-2
517
518/* This function emulates what the windows CRT does to validate file handles */
519int
520_PyVerify_fd(int fd)
521{
Victor Stinner8c62be82010-05-06 00:08:46 +0000522 const int i1 = fd >> IOINFO_L2E;
523 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000524
Antoine Pitrou22e41552010-08-15 18:07:50 +0000525 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000526
Victor Stinner8c62be82010-05-06 00:08:46 +0000527 /* Determine the actual size of the ioinfo structure,
528 * as used by the CRT loaded in memory
529 */
530 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
531 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
532 }
533 if (sizeof_ioinfo == 0) {
534 /* This should not happen... */
535 goto fail;
536 }
537
538 /* See that it isn't a special CLEAR fileno */
539 if (fd != _NO_CONSOLE_FILENO) {
540 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
541 * we check pointer validity and other info
542 */
543 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
544 /* finally, check that the file is open */
545 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
546 if (info->osfile & FOPEN) {
547 return 1;
548 }
549 }
550 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000551 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 errno = EBADF;
553 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000554}
555
556/* the special case of checking dup2. The target fd must be in a sensible range */
557static int
558_PyVerify_fd_dup2(int fd1, int fd2)
559{
Victor Stinner8c62be82010-05-06 00:08:46 +0000560 if (!_PyVerify_fd(fd1))
561 return 0;
562 if (fd2 == _NO_CONSOLE_FILENO)
563 return 0;
564 if ((unsigned)fd2 < _NHANDLE_)
565 return 1;
566 else
567 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000568}
569#else
570/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
571#define _PyVerify_fd_dup2(A, B) (1)
572#endif
573
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000574#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000575/* The following structure was copied from
576 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
577 include doesn't seem to be present in the Windows SDK (at least as included
578 with Visual Studio Express). */
579typedef struct _REPARSE_DATA_BUFFER {
580 ULONG ReparseTag;
581 USHORT ReparseDataLength;
582 USHORT Reserved;
583 union {
584 struct {
585 USHORT SubstituteNameOffset;
586 USHORT SubstituteNameLength;
587 USHORT PrintNameOffset;
588 USHORT PrintNameLength;
589 ULONG Flags;
590 WCHAR PathBuffer[1];
591 } SymbolicLinkReparseBuffer;
592
593 struct {
594 USHORT SubstituteNameOffset;
595 USHORT SubstituteNameLength;
596 USHORT PrintNameOffset;
597 USHORT PrintNameLength;
598 WCHAR PathBuffer[1];
599 } MountPointReparseBuffer;
600
601 struct {
602 UCHAR DataBuffer[1];
603 } GenericReparseBuffer;
604 };
605} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
606
607#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
608 GenericReparseBuffer)
609#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
610
611static int
Brian Curtind25aef52011-06-13 15:16:04 -0500612win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000613{
614 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
615 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
616 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000617
618 if (0 == DeviceIoControl(
619 reparse_point_handle,
620 FSCTL_GET_REPARSE_POINT,
621 NULL, 0, /* in buffer */
622 target_buffer, sizeof(target_buffer),
623 &n_bytes_returned,
624 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500625 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000626
627 if (reparse_tag)
628 *reparse_tag = rdb->ReparseTag;
629
Brian Curtind25aef52011-06-13 15:16:04 -0500630 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000631}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000632#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000633
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +0100635#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +0000636/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +0100637** environ directly, we must obtain it with _NSGetEnviron(). See also
638** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +0000639*/
640#include <crt_externs.h>
641static char **environ;
642#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000644#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645
Barry Warsaw53699e91996-12-10 23:23:01 +0000646static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648{
Victor Stinner8c62be82010-05-06 00:08:46 +0000649 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000650#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000651 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000652#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000654#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000655#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000656 APIRET rc;
657 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
658#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000659
Victor Stinner8c62be82010-05-06 00:08:46 +0000660 d = PyDict_New();
661 if (d == NULL)
662 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +0100663#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +0000664 if (environ == NULL)
665 environ = *_NSGetEnviron();
666#endif
667#ifdef MS_WINDOWS
668 /* _wenviron must be initialized in this way if the program is started
669 through main() instead of wmain(). */
670 _wgetenv(L"");
671 if (_wenviron == NULL)
672 return d;
673 /* This part ignores errors */
674 for (e = _wenviron; *e != NULL; e++) {
675 PyObject *k;
676 PyObject *v;
677 wchar_t *p = wcschr(*e, L'=');
678 if (p == NULL)
679 continue;
680 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
681 if (k == NULL) {
682 PyErr_Clear();
683 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000684 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000685 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
686 if (v == NULL) {
687 PyErr_Clear();
688 Py_DECREF(k);
689 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000690 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000691 if (PyDict_GetItem(d, k) == NULL) {
692 if (PyDict_SetItem(d, k, v) != 0)
693 PyErr_Clear();
694 }
695 Py_DECREF(k);
696 Py_DECREF(v);
697 }
698#else
699 if (environ == NULL)
700 return d;
701 /* This part ignores errors */
702 for (e = environ; *e != NULL; e++) {
703 PyObject *k;
704 PyObject *v;
705 char *p = strchr(*e, '=');
706 if (p == NULL)
707 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000708 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000709 if (k == NULL) {
710 PyErr_Clear();
711 continue;
712 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000713 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000714 if (v == NULL) {
715 PyErr_Clear();
716 Py_DECREF(k);
717 continue;
718 }
719 if (PyDict_GetItem(d, k) == NULL) {
720 if (PyDict_SetItem(d, k, v) != 0)
721 PyErr_Clear();
722 }
723 Py_DECREF(k);
724 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000725 }
726#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000727#if defined(PYOS_OS2)
728 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
729 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
730 PyObject *v = PyBytes_FromString(buffer);
731 PyDict_SetItemString(d, "BEGINLIBPATH", v);
732 Py_DECREF(v);
733 }
734 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
735 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
736 PyObject *v = PyBytes_FromString(buffer);
737 PyDict_SetItemString(d, "ENDLIBPATH", v);
738 Py_DECREF(v);
739 }
740#endif
741 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742}
743
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744/* Set a POSIX-specific error from errno, and return NULL */
745
Barry Warsawd58d7641998-07-23 16:14:40 +0000746static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000747posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000748{
Victor Stinner8c62be82010-05-06 00:08:46 +0000749 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000750}
Barry Warsawd58d7641998-07-23 16:14:40 +0000751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000752posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000753{
Victor Stinner8c62be82010-05-06 00:08:46 +0000754 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000755}
756
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000757
Mark Hammondef8b6542001-05-13 08:04:26 +0000758static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000759posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000760{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000761 PyObject *name_str, *rc;
762 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
763 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000764 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000765 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
766 name_str);
767 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000768 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000769}
770
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000771#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000772static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000773win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000774{
Victor Stinner8c62be82010-05-06 00:08:46 +0000775 /* XXX We should pass the function name along in the future.
776 (winreg.c also wants to pass the function name.)
777 This would however require an additional param to the
778 Windows error object, which is non-trivial.
779 */
780 errno = GetLastError();
781 if (filename)
782 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
783 else
784 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000785}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000786
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000787static PyObject *
788win32_error_unicode(char* function, Py_UNICODE* filename)
789{
Victor Stinner8c62be82010-05-06 00:08:46 +0000790 /* XXX - see win32_error for comments on 'function' */
791 errno = GetLastError();
792 if (filename)
793 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
794 else
795 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000796}
797
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000799convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000800{
Victor Stinner8c62be82010-05-06 00:08:46 +0000801 if (PyUnicode_CheckExact(*param))
802 Py_INCREF(*param);
803 else if (PyUnicode_Check(*param))
804 /* For a Unicode subtype that's not a Unicode object,
805 return a true Unicode object with the same data. */
806 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
807 PyUnicode_GET_SIZE(*param));
808 else
809 *param = PyUnicode_FromEncodedObject(*param,
810 Py_FileSystemDefaultEncoding,
811 "strict");
812 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000813}
814
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000815#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816
Guido van Rossumd48f2521997-12-05 22:19:34 +0000817#if defined(PYOS_OS2)
818/**********************************************************************
819 * Helper Function to Trim and Format OS/2 Messages
820 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000821static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000822os2_formatmsg(char *msgbuf, int msglen, char *reason)
823{
824 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
825
826 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
827 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
828
Antoine Pitrou4de74572013-02-09 23:11:27 +0100829 while (lastc > msgbuf && Py_ISSPACE(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000830 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
831 }
832
833 /* Add Optional Reason Text */
834 if (reason) {
835 strcat(msgbuf, " : ");
836 strcat(msgbuf, reason);
837 }
838}
839
840/**********************************************************************
841 * Decode an OS/2 Operating System Error Code
842 *
843 * A convenience function to lookup an OS/2 error code and return a
844 * text message we can use to raise a Python exception.
845 *
846 * Notes:
847 * The messages for errors returned from the OS/2 kernel reside in
848 * the file OSO001.MSG in the \OS2 directory hierarchy.
849 *
850 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000851static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000852os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
853{
854 APIRET rc;
855 ULONG msglen;
856
857 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
858 Py_BEGIN_ALLOW_THREADS
859 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
860 errorcode, "oso001.msg", &msglen);
861 Py_END_ALLOW_THREADS
862
863 if (rc == NO_ERROR)
864 os2_formatmsg(msgbuf, msglen, reason);
865 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000866 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000867 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000868
869 return msgbuf;
870}
871
872/* Set an OS/2-specific error and return NULL. OS/2 kernel
873 errors are not in a global variable e.g. 'errno' nor are
874 they congruent with posix error numbers. */
875
Victor Stinner8c62be82010-05-06 00:08:46 +0000876static PyObject *
877os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000878{
879 char text[1024];
880 PyObject *v;
881
882 os2_strerror(text, sizeof(text), code, "");
883
884 v = Py_BuildValue("(is)", code, text);
885 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000886 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000887 Py_DECREF(v);
888 }
889 return NULL; /* Signal to Python that an Exception is Pending */
890}
891
892#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893
894/* POSIX generic methods */
895
Barry Warsaw53699e91996-12-10 23:23:01 +0000896static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000897posix_fildes(PyObject *fdobj, int (*func)(int))
898{
Victor Stinner8c62be82010-05-06 00:08:46 +0000899 int fd;
900 int res;
901 fd = PyObject_AsFileDescriptor(fdobj);
902 if (fd < 0)
903 return NULL;
904 if (!_PyVerify_fd(fd))
905 return posix_error();
906 Py_BEGIN_ALLOW_THREADS
907 res = (*func)(fd);
908 Py_END_ALLOW_THREADS
909 if (res < 0)
910 return posix_error();
911 Py_INCREF(Py_None);
912 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000913}
Guido van Rossum21142a01999-01-08 21:05:37 +0000914
915static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000916posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917{
Victor Stinner8c62be82010-05-06 00:08:46 +0000918 PyObject *opath1 = NULL;
919 char *path1;
920 int res;
921 if (!PyArg_ParseTuple(args, format,
922 PyUnicode_FSConverter, &opath1))
923 return NULL;
924 path1 = PyBytes_AsString(opath1);
925 Py_BEGIN_ALLOW_THREADS
926 res = (*func)(path1);
927 Py_END_ALLOW_THREADS
928 if (res < 0)
929 return posix_error_with_allocated_filename(opath1);
930 Py_DECREF(opath1);
931 Py_INCREF(Py_None);
932 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000933}
934
Barry Warsaw53699e91996-12-10 23:23:01 +0000935static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000936posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000937 char *format,
938 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000939{
Victor Stinner8c62be82010-05-06 00:08:46 +0000940 PyObject *opath1 = NULL, *opath2 = NULL;
941 char *path1, *path2;
942 int res;
943 if (!PyArg_ParseTuple(args, format,
944 PyUnicode_FSConverter, &opath1,
945 PyUnicode_FSConverter, &opath2)) {
946 return NULL;
947 }
948 path1 = PyBytes_AsString(opath1);
949 path2 = PyBytes_AsString(opath2);
950 Py_BEGIN_ALLOW_THREADS
951 res = (*func)(path1, path2);
952 Py_END_ALLOW_THREADS
953 Py_DECREF(opath1);
954 Py_DECREF(opath2);
955 if (res != 0)
956 /* XXX how to report both path1 and path2??? */
957 return posix_error();
958 Py_INCREF(Py_None);
959 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960}
961
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000962#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000964win32_1str(PyObject* args, char* func,
965 char* format, BOOL (__stdcall *funcA)(LPCSTR),
966 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000967{
Victor Stinner8c62be82010-05-06 00:08:46 +0000968 PyObject *uni;
969 char *ansi;
970 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000971
Victor Stinner8c62be82010-05-06 00:08:46 +0000972 if (!PyArg_ParseTuple(args, wformat, &uni))
973 PyErr_Clear();
974 else {
975 Py_BEGIN_ALLOW_THREADS
976 result = funcW(PyUnicode_AsUnicode(uni));
977 Py_END_ALLOW_THREADS
978 if (!result)
979 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
980 Py_INCREF(Py_None);
981 return Py_None;
982 }
983 if (!PyArg_ParseTuple(args, format, &ansi))
984 return NULL;
985 Py_BEGIN_ALLOW_THREADS
986 result = funcA(ansi);
987 Py_END_ALLOW_THREADS
988 if (!result)
989 return win32_error(func, ansi);
990 Py_INCREF(Py_None);
991 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000992
993}
994
995/* This is a reimplementation of the C library's chdir function,
996 but one that produces Win32 errors instead of DOS error codes.
997 chdir is essentially a wrapper around SetCurrentDirectory; however,
998 it also needs to set "magic" environment variables indicating
999 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001000static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001win32_chdir(LPCSTR path)
1002{
Victor Stinner8c62be82010-05-06 00:08:46 +00001003 char new_path[MAX_PATH+1];
1004 int result;
1005 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006
Victor Stinner8c62be82010-05-06 00:08:46 +00001007 if(!SetCurrentDirectoryA(path))
1008 return FALSE;
1009 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
1010 if (!result)
1011 return FALSE;
1012 /* In the ANSI API, there should not be any paths longer
1013 than MAX_PATH. */
1014 assert(result <= MAX_PATH+1);
1015 if (strncmp(new_path, "\\\\", 2) == 0 ||
1016 strncmp(new_path, "//", 2) == 0)
1017 /* UNC path, nothing to do. */
1018 return TRUE;
1019 env[1] = new_path[0];
1020 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021}
1022
1023/* The Unicode version differs from the ANSI version
1024 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001025static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026win32_wchdir(LPCWSTR path)
1027{
Victor Stinner8c62be82010-05-06 00:08:46 +00001028 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
1029 int result;
1030 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001031
Victor Stinner8c62be82010-05-06 00:08:46 +00001032 if(!SetCurrentDirectoryW(path))
1033 return FALSE;
1034 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
1035 if (!result)
1036 return FALSE;
1037 if (result > MAX_PATH+1) {
1038 new_path = malloc(result * sizeof(wchar_t));
1039 if (!new_path) {
1040 SetLastError(ERROR_OUTOFMEMORY);
1041 return FALSE;
1042 }
1043 result = GetCurrentDirectoryW(result, new_path);
1044 if (!result) {
1045 free(new_path);
1046 return FALSE;
1047 }
1048 }
1049 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1050 wcsncmp(new_path, L"//", 2) == 0)
1051 /* UNC path, nothing to do. */
1052 return TRUE;
1053 env[1] = new_path[0];
1054 result = SetEnvironmentVariableW(env, new_path);
1055 if (new_path != _new_path)
1056 free(new_path);
1057 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058}
1059#endif
1060
Martin v. Löwis14694662006-02-03 12:54:16 +00001061#ifdef MS_WINDOWS
1062/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1063 - time stamps are restricted to second resolution
1064 - file modification times suffer from forth-and-back conversions between
1065 UTC and local time
1066 Therefore, we implement our own stat, based on the Win32 API directly.
1067*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001068#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001069
1070struct win32_stat{
1071 int st_dev;
1072 __int64 st_ino;
1073 unsigned short st_mode;
1074 int st_nlink;
1075 int st_uid;
1076 int st_gid;
1077 int st_rdev;
1078 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001079 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001080 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001081 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001082 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001083 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001084 int st_ctime_nsec;
1085};
1086
1087static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1088
1089static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001090FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001091{
Victor Stinner8c62be82010-05-06 00:08:46 +00001092 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1093 /* Cannot simply cast and dereference in_ptr,
1094 since it might not be aligned properly */
1095 __int64 in;
1096 memcpy(&in, in_ptr, sizeof(in));
1097 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001098 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001099}
1100
Thomas Wouters477c8d52006-05-27 19:21:47 +00001101static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001102time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001103{
Victor Stinner8c62be82010-05-06 00:08:46 +00001104 /* XXX endianness */
1105 __int64 out;
1106 out = time_in + secs_between_epochs;
1107 out = out * 10000000 + nsec_in / 100;
1108 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001109}
1110
Martin v. Löwis14694662006-02-03 12:54:16 +00001111/* Below, we *know* that ugo+r is 0444 */
1112#if _S_IREAD != 0400
1113#error Unsupported C library
1114#endif
1115static int
1116attributes_to_mode(DWORD attr)
1117{
Victor Stinner8c62be82010-05-06 00:08:46 +00001118 int m = 0;
1119 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1120 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1121 else
1122 m |= _S_IFREG;
1123 if (attr & FILE_ATTRIBUTE_READONLY)
1124 m |= 0444;
1125 else
1126 m |= 0666;
1127 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001128}
1129
1130static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001132{
Victor Stinner8c62be82010-05-06 00:08:46 +00001133 memset(result, 0, sizeof(*result));
1134 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1135 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1136 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1137 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1138 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001139 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001140 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001141 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1142 /* first clear the S_IFMT bits */
1143 result->st_mode ^= (result->st_mode & 0170000);
1144 /* now set the bits that make this a symlink */
1145 result->st_mode |= 0120000;
1146 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001147
Victor Stinner8c62be82010-05-06 00:08:46 +00001148 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001149}
1150
Guido van Rossumd8faa362007-04-27 19:54:29 +00001151static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001152attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001153{
Victor Stinner8c62be82010-05-06 00:08:46 +00001154 HANDLE hFindFile;
1155 WIN32_FIND_DATAA FileData;
1156 hFindFile = FindFirstFileA(pszFile, &FileData);
1157 if (hFindFile == INVALID_HANDLE_VALUE)
1158 return FALSE;
1159 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001160 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001161 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001162 info->dwFileAttributes = FileData.dwFileAttributes;
1163 info->ftCreationTime = FileData.ftCreationTime;
1164 info->ftLastAccessTime = FileData.ftLastAccessTime;
1165 info->ftLastWriteTime = FileData.ftLastWriteTime;
1166 info->nFileSizeHigh = FileData.nFileSizeHigh;
1167 info->nFileSizeLow = FileData.nFileSizeLow;
1168/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001169 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1170 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001171 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001172}
1173
1174static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001175attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001176{
Victor Stinner8c62be82010-05-06 00:08:46 +00001177 HANDLE hFindFile;
1178 WIN32_FIND_DATAW FileData;
1179 hFindFile = FindFirstFileW(pszFile, &FileData);
1180 if (hFindFile == INVALID_HANDLE_VALUE)
1181 return FALSE;
1182 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001183 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001184 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185 info->dwFileAttributes = FileData.dwFileAttributes;
1186 info->ftCreationTime = FileData.ftCreationTime;
1187 info->ftLastAccessTime = FileData.ftLastAccessTime;
1188 info->ftLastWriteTime = FileData.ftLastWriteTime;
1189 info->nFileSizeHigh = FileData.nFileSizeHigh;
1190 info->nFileSizeLow = FileData.nFileSizeLow;
1191/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001192 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1193 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001194 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001195}
1196
Brian Curtind25aef52011-06-13 15:16:04 -05001197/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1198static int has_GetFinalPathNameByHandle = 0;
1199static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1200 DWORD);
1201static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1202 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001203static int
Brian Curtind25aef52011-06-13 15:16:04 -05001204check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001205{
Brian Curtind25aef52011-06-13 15:16:04 -05001206 HINSTANCE hKernel32;
1207 /* only recheck */
1208 if (!has_GetFinalPathNameByHandle)
1209 {
1210 hKernel32 = GetModuleHandle("KERNEL32");
1211 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1212 "GetFinalPathNameByHandleA");
1213 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1214 "GetFinalPathNameByHandleW");
1215 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1216 Py_GetFinalPathNameByHandleW;
1217 }
1218 return has_GetFinalPathNameByHandle;
1219}
1220
1221static BOOL
1222get_target_path(HANDLE hdl, wchar_t **target_path)
1223{
1224 int buf_size, result_length;
1225 wchar_t *buf;
1226
1227 /* We have a good handle to the target, use it to determine
1228 the target path name (then we'll call lstat on it). */
1229 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1230 VOLUME_NAME_DOS);
1231 if(!buf_size)
1232 return FALSE;
1233
1234 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001235 if (!buf) {
1236 SetLastError(ERROR_OUTOFMEMORY);
1237 return FALSE;
1238 }
1239
Brian Curtind25aef52011-06-13 15:16:04 -05001240 result_length = Py_GetFinalPathNameByHandleW(hdl,
1241 buf, buf_size, VOLUME_NAME_DOS);
1242
1243 if(!result_length) {
1244 free(buf);
1245 return FALSE;
1246 }
1247
1248 if(!CloseHandle(hdl)) {
1249 free(buf);
1250 return FALSE;
1251 }
1252
1253 buf[result_length] = 0;
1254
1255 *target_path = buf;
1256 return TRUE;
1257}
1258
1259static int
1260win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1261 BOOL traverse);
1262static int
1263win32_xstat_impl(const char *path, struct win32_stat *result,
1264 BOOL traverse)
1265{
Victor Stinner26de69d2011-06-17 15:15:38 +02001266 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001267 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001269 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001270 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001271 const char *dot;
1272
Brian Curtind25aef52011-06-13 15:16:04 -05001273 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001274 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1275 traverse reparse point. */
1276 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001277 }
1278
Brian Curtinf5e76d02010-11-24 13:14:05 +00001279 hFile = CreateFileA(
1280 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001281 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001282 0, /* share mode */
1283 NULL, /* security attributes */
1284 OPEN_EXISTING,
1285 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001286 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1287 Because of this, calls like GetFinalPathNameByHandle will return
1288 the symlink path agin and not the actual final path. */
1289 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1290 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001291 NULL);
1292
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001293 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001294 /* Either the target doesn't exist, or we don't have access to
1295 get a handle to it. If the former, we need to return an error.
1296 If the latter, we can use attributes_from_dir. */
1297 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001298 return -1;
1299 /* Could not get attributes on open file. Fall back to
1300 reading the directory. */
1301 if (!attributes_from_dir(path, &info, &reparse_tag))
1302 /* Very strange. This should not fail now */
1303 return -1;
1304 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1305 if (traverse) {
1306 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001307 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001308 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001309 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001310 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001311 } else {
1312 if (!GetFileInformationByHandle(hFile, &info)) {
1313 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001314 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001315 }
1316 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001317 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1318 return -1;
1319
1320 /* Close the outer open file handle now that we're about to
1321 reopen it with different flags. */
1322 if (!CloseHandle(hFile))
1323 return -1;
1324
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001325 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001326 /* In order to call GetFinalPathNameByHandle we need to open
1327 the file without the reparse handling flag set. */
1328 hFile2 = CreateFileA(
1329 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1330 NULL, OPEN_EXISTING,
1331 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1332 NULL);
1333 if (hFile2 == INVALID_HANDLE_VALUE)
1334 return -1;
1335
1336 if (!get_target_path(hFile2, &target_path))
1337 return -1;
1338
1339 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001340 free(target_path);
1341 return code;
1342 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001343 } else
1344 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001345 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001346 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001347
1348 /* Set S_IEXEC if it is an .exe, .bat, ... */
1349 dot = strrchr(path, '.');
1350 if (dot) {
1351 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1352 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1353 result->st_mode |= 0111;
1354 }
1355 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001356}
1357
1358static int
Brian Curtind25aef52011-06-13 15:16:04 -05001359win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1360 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001361{
1362 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001363 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001364 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001365 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001366 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001367 const wchar_t *dot;
1368
Brian Curtind25aef52011-06-13 15:16:04 -05001369 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001370 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1371 traverse reparse point. */
1372 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001373 }
1374
Brian Curtinf5e76d02010-11-24 13:14:05 +00001375 hFile = CreateFileW(
1376 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001377 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001378 0, /* share mode */
1379 NULL, /* security attributes */
1380 OPEN_EXISTING,
1381 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001382 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1383 Because of this, calls like GetFinalPathNameByHandle will return
1384 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001385 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001386 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001387 NULL);
1388
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001389 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001390 /* Either the target doesn't exist, or we don't have access to
1391 get a handle to it. If the former, we need to return an error.
1392 If the latter, we can use attributes_from_dir. */
1393 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394 return -1;
1395 /* Could not get attributes on open file. Fall back to
1396 reading the directory. */
1397 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1398 /* Very strange. This should not fail now */
1399 return -1;
1400 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1401 if (traverse) {
1402 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001403 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001404 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001405 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001406 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001407 } else {
1408 if (!GetFileInformationByHandle(hFile, &info)) {
1409 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001410 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001411 }
1412 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001413 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1414 return -1;
1415
1416 /* Close the outer open file handle now that we're about to
1417 reopen it with different flags. */
1418 if (!CloseHandle(hFile))
1419 return -1;
1420
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001421 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001422 /* In order to call GetFinalPathNameByHandle we need to open
1423 the file without the reparse handling flag set. */
1424 hFile2 = CreateFileW(
1425 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1426 NULL, OPEN_EXISTING,
1427 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1428 NULL);
1429 if (hFile2 == INVALID_HANDLE_VALUE)
1430 return -1;
1431
1432 if (!get_target_path(hFile2, &target_path))
1433 return -1;
1434
1435 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001436 free(target_path);
1437 return code;
1438 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001439 } else
1440 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001441 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001442 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001443
1444 /* Set S_IEXEC if it is an .exe, .bat, ... */
1445 dot = wcsrchr(path, '.');
1446 if (dot) {
1447 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1448 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1449 result->st_mode |= 0111;
1450 }
1451 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001452}
1453
1454static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001455win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001456{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001457 /* Protocol violation: we explicitly clear errno, instead of
1458 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001459 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001460 errno = 0;
1461 return code;
1462}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001463
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001464static int
1465win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1466{
1467 /* Protocol violation: we explicitly clear errno, instead of
1468 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001469 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001470 errno = 0;
1471 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001472}
Brian Curtind25aef52011-06-13 15:16:04 -05001473/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001474
1475 In Posix, stat automatically traverses symlinks and returns the stat
1476 structure for the target. In Windows, the equivalent GetFileAttributes by
1477 default does not traverse symlinks and instead returns attributes for
1478 the symlink.
1479
1480 Therefore, win32_lstat will get the attributes traditionally, and
1481 win32_stat will first explicitly resolve the symlink target and then will
1482 call win32_lstat on that result.
1483
Ezio Melotti4969f702011-03-15 05:59:46 +02001484 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001485
Brian Curtind25aef52011-06-13 15:16:04 -05001486static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001487win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001488{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001489 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001490}
1491
Victor Stinner8c62be82010-05-06 00:08:46 +00001492static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001493win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001494{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001495 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001496}
1497
1498static int
1499win32_stat(const char* path, struct win32_stat *result)
1500{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001501 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001502}
1503
Victor Stinner26de69d2011-06-17 15:15:38 +02001504static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001505win32_stat_w(const wchar_t* path, struct win32_stat *result)
1506{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001507 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001508}
1509
1510static int
1511win32_fstat(int file_number, struct win32_stat *result)
1512{
Victor Stinner8c62be82010-05-06 00:08:46 +00001513 BY_HANDLE_FILE_INFORMATION info;
1514 HANDLE h;
1515 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001516
Victor Stinner8c62be82010-05-06 00:08:46 +00001517 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001518
Victor Stinner8c62be82010-05-06 00:08:46 +00001519 /* Protocol violation: we explicitly clear errno, instead of
1520 setting it to a POSIX error. Callers should use GetLastError. */
1521 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001522
Victor Stinner8c62be82010-05-06 00:08:46 +00001523 if (h == INVALID_HANDLE_VALUE) {
1524 /* This is really a C library error (invalid file handle).
1525 We set the Win32 error to the closes one matching. */
1526 SetLastError(ERROR_INVALID_HANDLE);
1527 return -1;
1528 }
1529 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001530
Victor Stinner8c62be82010-05-06 00:08:46 +00001531 type = GetFileType(h);
1532 if (type == FILE_TYPE_UNKNOWN) {
1533 DWORD error = GetLastError();
1534 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001535 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001536 }
1537 /* else: valid but unknown file */
1538 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001539
Victor Stinner8c62be82010-05-06 00:08:46 +00001540 if (type != FILE_TYPE_DISK) {
1541 if (type == FILE_TYPE_CHAR)
1542 result->st_mode = _S_IFCHR;
1543 else if (type == FILE_TYPE_PIPE)
1544 result->st_mode = _S_IFIFO;
1545 return 0;
1546 }
1547
1548 if (!GetFileInformationByHandle(h, &info)) {
1549 return -1;
1550 }
1551
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001552 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001553 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001554 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1555 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001556}
1557
1558#endif /* MS_WINDOWS */
1559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001561"stat_result: Result from stat or lstat.\n\n\
1562This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001563 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001564or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1565\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001566Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1567or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001568\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001569See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001570
1571static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 {"st_mode", "protection bits"},
1573 {"st_ino", "inode"},
1574 {"st_dev", "device"},
1575 {"st_nlink", "number of hard links"},
1576 {"st_uid", "user ID of owner"},
1577 {"st_gid", "group ID of owner"},
1578 {"st_size", "total size, in bytes"},
1579 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1580 {NULL, "integer time of last access"},
1581 {NULL, "integer time of last modification"},
1582 {NULL, "integer time of last change"},
1583 {"st_atime", "time of last access"},
1584 {"st_mtime", "time of last modification"},
1585 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001586#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001588#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001589#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001590 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001591#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001592#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001594#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001595#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001596 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001597#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001598#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001600#endif
1601#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001602 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001603#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001605};
1606
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001607#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001608#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001609#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001610#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001611#endif
1612
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001613#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001614#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1615#else
1616#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1617#endif
1618
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001619#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001620#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1621#else
1622#define ST_RDEV_IDX ST_BLOCKS_IDX
1623#endif
1624
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001625#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1626#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1627#else
1628#define ST_FLAGS_IDX ST_RDEV_IDX
1629#endif
1630
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001631#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001632#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001633#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001634#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001635#endif
1636
1637#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1638#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1639#else
1640#define ST_BIRTHTIME_IDX ST_GEN_IDX
1641#endif
1642
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001643static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001644 "stat_result", /* name */
1645 stat_result__doc__, /* doc */
1646 stat_result_fields,
1647 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001648};
1649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001651"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1652This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001653 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001654or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001655\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001657
1658static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 {"f_bsize", },
1660 {"f_frsize", },
1661 {"f_blocks", },
1662 {"f_bfree", },
1663 {"f_bavail", },
1664 {"f_files", },
1665 {"f_ffree", },
1666 {"f_favail", },
1667 {"f_flag", },
1668 {"f_namemax",},
1669 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001670};
1671
1672static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 "statvfs_result", /* name */
1674 statvfs_result__doc__, /* doc */
1675 statvfs_result_fields,
1676 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001677};
1678
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001679static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001680static PyTypeObject StatResultType;
1681static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001682static newfunc structseq_new;
1683
1684static PyObject *
1685statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1686{
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 PyStructSequence *result;
1688 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001689
Victor Stinner8c62be82010-05-06 00:08:46 +00001690 result = (PyStructSequence*)structseq_new(type, args, kwds);
1691 if (!result)
1692 return NULL;
1693 /* If we have been initialized from a tuple,
1694 st_?time might be set to None. Initialize it
1695 from the int slots. */
1696 for (i = 7; i <= 9; i++) {
1697 if (result->ob_item[i+3] == Py_None) {
1698 Py_DECREF(Py_None);
1699 Py_INCREF(result->ob_item[i]);
1700 result->ob_item[i+3] = result->ob_item[i];
1701 }
1702 }
1703 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001704}
1705
1706
1707
1708/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001709static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001710
1711PyDoc_STRVAR(stat_float_times__doc__,
1712"stat_float_times([newval]) -> oldval\n\n\
1713Determine whether os.[lf]stat represents time stamps as float objects.\n\
1714If newval is True, future calls to stat() return floats, if it is False,\n\
1715future calls return ints. \n\
1716If newval is omitted, return the current setting.\n");
1717
1718static PyObject*
1719stat_float_times(PyObject* self, PyObject *args)
1720{
Victor Stinner8c62be82010-05-06 00:08:46 +00001721 int newval = -1;
1722 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1723 return NULL;
1724 if (newval == -1)
1725 /* Return old value */
1726 return PyBool_FromLong(_stat_float_times);
1727 _stat_float_times = newval;
1728 Py_INCREF(Py_None);
1729 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001730}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001731
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001732static void
1733fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1734{
Victor Stinner8c62be82010-05-06 00:08:46 +00001735 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001736#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001738#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001740#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 if (!ival)
1742 return;
1743 if (_stat_float_times) {
1744 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1745 } else {
1746 fval = ival;
1747 Py_INCREF(fval);
1748 }
1749 PyStructSequence_SET_ITEM(v, index, ival);
1750 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001751}
1752
Tim Peters5aa91602002-01-30 05:46:57 +00001753/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001754 (used by posix_stat() and posix_fstat()) */
1755static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001756_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001757{
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 unsigned long ansec, mnsec, cnsec;
1759 PyObject *v = PyStructSequence_New(&StatResultType);
1760 if (v == NULL)
1761 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001762
Victor Stinner8c62be82010-05-06 00:08:46 +00001763 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001764#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001765 PyStructSequence_SET_ITEM(v, 1,
1766 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001767#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001768 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001769#endif
1770#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001771 PyStructSequence_SET_ITEM(v, 2,
1772 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001773#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001774 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001775#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02001777#if defined(MS_WINDOWS)
1778 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
1779 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
1780#else
1781 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
1782 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
1783#endif
Fred Drake699f3522000-06-29 21:12:41 +00001784#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001785 PyStructSequence_SET_ITEM(v, 6,
1786 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001787#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001789#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001790
Martin v. Löwis14694662006-02-03 12:54:16 +00001791#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 ansec = st->st_atim.tv_nsec;
1793 mnsec = st->st_mtim.tv_nsec;
1794 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001795#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 ansec = st->st_atimespec.tv_nsec;
1797 mnsec = st->st_mtimespec.tv_nsec;
1798 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001799#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001800 ansec = st->st_atime_nsec;
1801 mnsec = st->st_mtime_nsec;
1802 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001803#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001804 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001805#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 fill_time(v, 7, st->st_atime, ansec);
1807 fill_time(v, 8, st->st_mtime, mnsec);
1808 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001809
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001810#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1812 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001813#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001814#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001815 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1816 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001817#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001818#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001819 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1820 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001821#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001822#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1824 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001825#endif
1826#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001827 {
1828 PyObject *val;
1829 unsigned long bsec,bnsec;
1830 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001831#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001832 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001833#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001835#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001836 if (_stat_float_times) {
1837 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1838 } else {
1839 val = PyLong_FromLong((long)bsec);
1840 }
1841 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1842 val);
1843 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001844#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001845#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001846 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1847 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001848#endif
Fred Drake699f3522000-06-29 21:12:41 +00001849
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 if (PyErr_Occurred()) {
1851 Py_DECREF(v);
1852 return NULL;
1853 }
Fred Drake699f3522000-06-29 21:12:41 +00001854
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001856}
1857
Barry Warsaw53699e91996-12-10 23:23:01 +00001858static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001859posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001860 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001861#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001862 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001863#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001864 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001865#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001866 char *wformat,
1867 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001868{
Victor Stinner8c62be82010-05-06 00:08:46 +00001869 STRUCT_STAT st;
1870 PyObject *opath;
1871 char *path;
1872 int res;
1873 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001874
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001875#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001876 PyUnicodeObject *po;
1877 if (PyArg_ParseTuple(args, wformat, &po)) {
1878 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001879
Victor Stinner8c62be82010-05-06 00:08:46 +00001880 Py_BEGIN_ALLOW_THREADS
1881 /* PyUnicode_AS_UNICODE result OK without
1882 thread lock as it is a simple dereference. */
1883 res = wstatfunc(wpath, &st);
1884 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001885
Victor Stinner8c62be82010-05-06 00:08:46 +00001886 if (res != 0)
1887 return win32_error_unicode("stat", wpath);
1888 return _pystat_fromstructstat(&st);
1889 }
1890 /* Drop the argument parsing error as narrow strings
1891 are also valid. */
1892 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001893#endif
1894
Victor Stinner8c62be82010-05-06 00:08:46 +00001895 if (!PyArg_ParseTuple(args, format,
1896 PyUnicode_FSConverter, &opath))
1897 return NULL;
1898 path = PyBytes_AsString(opath);
1899 Py_BEGIN_ALLOW_THREADS
1900 res = (*statfunc)(path, &st);
1901 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001902
Victor Stinner8c62be82010-05-06 00:08:46 +00001903 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001904#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001906#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001908#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001909 }
1910 else
1911 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001912
Victor Stinner8c62be82010-05-06 00:08:46 +00001913 Py_DECREF(opath);
1914 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001915}
1916
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001917/* POSIX methods */
1918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001919PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001920"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001921Use the real uid/gid to test for access to a path. Note that most\n\
1922operations will use the effective uid/gid, therefore this routine can\n\
1923be used in a suid/sgid environment to test if the invoking user has the\n\
1924specified access to the path. The mode argument can be F_OK to test\n\
1925existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001926
1927static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001928posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001929{
Victor Stinner8c62be82010-05-06 00:08:46 +00001930 PyObject *opath;
1931 char *path;
1932 int mode;
1933
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001934#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 DWORD attr;
1936 PyUnicodeObject *po;
1937 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1938 Py_BEGIN_ALLOW_THREADS
1939 /* PyUnicode_AS_UNICODE OK without thread lock as
1940 it is a simple dereference. */
1941 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1942 Py_END_ALLOW_THREADS
1943 goto finish;
1944 }
1945 /* Drop the argument parsing error as narrow strings
1946 are also valid. */
1947 PyErr_Clear();
1948 if (!PyArg_ParseTuple(args, "O&i:access",
1949 PyUnicode_FSConverter, &opath, &mode))
1950 return NULL;
1951 path = PyBytes_AsString(opath);
1952 Py_BEGIN_ALLOW_THREADS
1953 attr = GetFileAttributesA(path);
1954 Py_END_ALLOW_THREADS
1955 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001956finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 if (attr == 0xFFFFFFFF)
1958 /* File does not exist, or cannot read attributes */
1959 return PyBool_FromLong(0);
1960 /* Access is possible if either write access wasn't requested, or
1961 the file isn't read-only, or if it's a directory, as there are
1962 no read-only directories on Windows. */
1963 return PyBool_FromLong(!(mode & 2)
1964 || !(attr & FILE_ATTRIBUTE_READONLY)
1965 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001966#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001967 int res;
1968 if (!PyArg_ParseTuple(args, "O&i:access",
1969 PyUnicode_FSConverter, &opath, &mode))
1970 return NULL;
1971 path = PyBytes_AsString(opath);
1972 Py_BEGIN_ALLOW_THREADS
1973 res = access(path, mode);
1974 Py_END_ALLOW_THREADS
1975 Py_DECREF(opath);
1976 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001978}
1979
Guido van Rossumd371ff11999-01-25 16:12:23 +00001980#ifndef F_OK
1981#define F_OK 0
1982#endif
1983#ifndef R_OK
1984#define R_OK 4
1985#endif
1986#ifndef W_OK
1987#define W_OK 2
1988#endif
1989#ifndef X_OK
1990#define X_OK 1
1991#endif
1992
1993#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001995"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001997
1998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001999posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002000{
Victor Stinner8c62be82010-05-06 00:08:46 +00002001 int id;
2002 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002003
Victor Stinner8c62be82010-05-06 00:08:46 +00002004 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
2005 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002006
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002007#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002008 /* file descriptor 0 only, the default input device (stdin) */
2009 if (id == 0) {
2010 ret = ttyname();
2011 }
2012 else {
2013 ret = NULL;
2014 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002015#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002017#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002018 if (ret == NULL)
2019 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002020 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002021}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002022#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002023
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002024#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002026"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002028
2029static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002030posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002031{
Victor Stinner8c62be82010-05-06 00:08:46 +00002032 char *ret;
2033 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002034
Greg Wardb48bc172000-03-01 21:51:56 +00002035#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002036 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002037#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002038 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002039#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002040 if (ret == NULL)
2041 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002042 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002043}
2044#endif
2045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002047"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Barry Warsaw53699e91996-12-10 23:23:01 +00002050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002051posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002053#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002056 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002057#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002058 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002059#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002060 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002061#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002062}
2063
Fred Drake4d1e64b2002-04-15 19:40:07 +00002064#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002066"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002067Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002069
2070static PyObject *
2071posix_fchdir(PyObject *self, PyObject *fdobj)
2072{
Victor Stinner8c62be82010-05-06 00:08:46 +00002073 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002074}
2075#endif /* HAVE_FCHDIR */
2076
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002078PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002079"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002080Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081
Barry Warsaw53699e91996-12-10 23:23:01 +00002082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002083posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002084{
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 PyObject *opath = NULL;
2086 char *path = NULL;
2087 int i;
2088 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002089#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 DWORD attr;
2091 PyUnicodeObject *po;
2092 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2093 Py_BEGIN_ALLOW_THREADS
2094 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2095 if (attr != 0xFFFFFFFF) {
2096 if (i & _S_IWRITE)
2097 attr &= ~FILE_ATTRIBUTE_READONLY;
2098 else
2099 attr |= FILE_ATTRIBUTE_READONLY;
2100 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2101 }
2102 else
2103 res = 0;
2104 Py_END_ALLOW_THREADS
2105 if (!res)
2106 return win32_error_unicode("chmod",
2107 PyUnicode_AS_UNICODE(po));
2108 Py_INCREF(Py_None);
2109 return Py_None;
2110 }
2111 /* Drop the argument parsing error as narrow strings
2112 are also valid. */
2113 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002114
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2116 &opath, &i))
2117 return NULL;
2118 path = PyBytes_AsString(opath);
2119 Py_BEGIN_ALLOW_THREADS
2120 attr = GetFileAttributesA(path);
2121 if (attr != 0xFFFFFFFF) {
2122 if (i & _S_IWRITE)
2123 attr &= ~FILE_ATTRIBUTE_READONLY;
2124 else
2125 attr |= FILE_ATTRIBUTE_READONLY;
2126 res = SetFileAttributesA(path, attr);
2127 }
2128 else
2129 res = 0;
2130 Py_END_ALLOW_THREADS
2131 if (!res) {
2132 win32_error("chmod", path);
2133 Py_DECREF(opath);
2134 return NULL;
2135 }
2136 Py_DECREF(opath);
2137 Py_INCREF(Py_None);
2138 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002139#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002140 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2141 &opath, &i))
2142 return NULL;
2143 path = PyBytes_AsString(opath);
2144 Py_BEGIN_ALLOW_THREADS
2145 res = chmod(path, i);
2146 Py_END_ALLOW_THREADS
2147 if (res < 0)
2148 return posix_error_with_allocated_filename(opath);
2149 Py_DECREF(opath);
2150 Py_INCREF(Py_None);
2151 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002152#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002153}
2154
Christian Heimes4e30a842007-11-30 22:12:06 +00002155#ifdef HAVE_FCHMOD
2156PyDoc_STRVAR(posix_fchmod__doc__,
2157"fchmod(fd, mode)\n\n\
2158Change the access permissions of the file given by file\n\
2159descriptor fd.");
2160
2161static PyObject *
2162posix_fchmod(PyObject *self, PyObject *args)
2163{
Victor Stinner8c62be82010-05-06 00:08:46 +00002164 int fd, mode, res;
2165 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2166 return NULL;
2167 Py_BEGIN_ALLOW_THREADS
2168 res = fchmod(fd, mode);
2169 Py_END_ALLOW_THREADS
2170 if (res < 0)
2171 return posix_error();
2172 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002173}
2174#endif /* HAVE_FCHMOD */
2175
2176#ifdef HAVE_LCHMOD
2177PyDoc_STRVAR(posix_lchmod__doc__,
2178"lchmod(path, mode)\n\n\
2179Change the access permissions of a file. If path is a symlink, this\n\
2180affects the link itself rather than the target.");
2181
2182static PyObject *
2183posix_lchmod(PyObject *self, PyObject *args)
2184{
Victor Stinner8c62be82010-05-06 00:08:46 +00002185 PyObject *opath;
2186 char *path;
2187 int i;
2188 int res;
2189 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2190 &opath, &i))
2191 return NULL;
2192 path = PyBytes_AsString(opath);
2193 Py_BEGIN_ALLOW_THREADS
2194 res = lchmod(path, i);
2195 Py_END_ALLOW_THREADS
2196 if (res < 0)
2197 return posix_error_with_allocated_filename(opath);
2198 Py_DECREF(opath);
2199 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002200}
2201#endif /* HAVE_LCHMOD */
2202
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002203
Thomas Wouterscf297e42007-02-23 15:07:44 +00002204#ifdef HAVE_CHFLAGS
2205PyDoc_STRVAR(posix_chflags__doc__,
2206"chflags(path, flags)\n\n\
2207Set file flags.");
2208
2209static PyObject *
2210posix_chflags(PyObject *self, PyObject *args)
2211{
Victor Stinner8c62be82010-05-06 00:08:46 +00002212 PyObject *opath;
2213 char *path;
2214 unsigned long flags;
2215 int res;
2216 if (!PyArg_ParseTuple(args, "O&k:chflags",
2217 PyUnicode_FSConverter, &opath, &flags))
2218 return NULL;
2219 path = PyBytes_AsString(opath);
2220 Py_BEGIN_ALLOW_THREADS
2221 res = chflags(path, flags);
2222 Py_END_ALLOW_THREADS
2223 if (res < 0)
2224 return posix_error_with_allocated_filename(opath);
2225 Py_DECREF(opath);
2226 Py_INCREF(Py_None);
2227 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002228}
2229#endif /* HAVE_CHFLAGS */
2230
2231#ifdef HAVE_LCHFLAGS
2232PyDoc_STRVAR(posix_lchflags__doc__,
2233"lchflags(path, flags)\n\n\
2234Set file flags.\n\
2235This function will not follow symbolic links.");
2236
2237static PyObject *
2238posix_lchflags(PyObject *self, PyObject *args)
2239{
Victor Stinner8c62be82010-05-06 00:08:46 +00002240 PyObject *opath;
2241 char *path;
2242 unsigned long flags;
2243 int res;
2244 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2245 PyUnicode_FSConverter, &opath, &flags))
2246 return NULL;
2247 path = PyBytes_AsString(opath);
2248 Py_BEGIN_ALLOW_THREADS
2249 res = lchflags(path, flags);
2250 Py_END_ALLOW_THREADS
2251 if (res < 0)
2252 return posix_error_with_allocated_filename(opath);
2253 Py_DECREF(opath);
2254 Py_INCREF(Py_None);
2255 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002256}
2257#endif /* HAVE_LCHFLAGS */
2258
Martin v. Löwis244edc82001-10-04 22:44:26 +00002259#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002260PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002261"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002262Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002263
2264static PyObject *
2265posix_chroot(PyObject *self, PyObject *args)
2266{
Victor Stinner8c62be82010-05-06 00:08:46 +00002267 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002268}
2269#endif
2270
Guido van Rossum21142a01999-01-08 21:05:37 +00002271#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002272PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002273"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002274force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002275
2276static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002277posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002278{
Stefan Krah0e803b32010-11-26 16:16:47 +00002279 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002280}
2281#endif /* HAVE_FSYNC */
2282
2283#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002284
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002285#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002286extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2287#endif
2288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002289PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002290"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002291force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002292 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002293
2294static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002295posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002296{
Stefan Krah0e803b32010-11-26 16:16:47 +00002297 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002298}
2299#endif /* HAVE_FDATASYNC */
2300
2301
Fredrik Lundh10723342000-07-10 16:38:09 +00002302#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002303PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002304"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002305Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002306
Barry Warsaw53699e91996-12-10 23:23:01 +00002307static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002308posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002309{
Victor Stinner8c62be82010-05-06 00:08:46 +00002310 PyObject *opath;
2311 char *path;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002312 uid_t uid;
2313 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00002314 int res;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002315 if (!PyArg_ParseTuple(args, "O&O&O&:chown",
Victor Stinner8c62be82010-05-06 00:08:46 +00002316 PyUnicode_FSConverter, &opath,
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002317 _Py_Uid_Converter, &uid,
2318 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002319 return NULL;
2320 path = PyBytes_AsString(opath);
2321 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002322 res = chown(path, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00002323 Py_END_ALLOW_THREADS
2324 if (res < 0)
2325 return posix_error_with_allocated_filename(opath);
2326 Py_DECREF(opath);
2327 Py_INCREF(Py_None);
2328 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002329}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002330#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002331
Christian Heimes4e30a842007-11-30 22:12:06 +00002332#ifdef HAVE_FCHOWN
2333PyDoc_STRVAR(posix_fchown__doc__,
2334"fchown(fd, uid, gid)\n\n\
2335Change the owner and group id of the file given by file descriptor\n\
2336fd to the numeric uid and gid.");
2337
2338static PyObject *
2339posix_fchown(PyObject *self, PyObject *args)
2340{
Victor Stinner8c62be82010-05-06 00:08:46 +00002341 int fd;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002342 uid_t uid;
2343 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00002344 int res;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002345 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd,
2346 _Py_Uid_Converter, &uid,
2347 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002348 return NULL;
2349 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002350 res = fchown(fd, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00002351 Py_END_ALLOW_THREADS
2352 if (res < 0)
2353 return posix_error();
2354 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002355}
2356#endif /* HAVE_FCHOWN */
2357
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002358#ifdef HAVE_LCHOWN
2359PyDoc_STRVAR(posix_lchown__doc__,
2360"lchown(path, uid, gid)\n\n\
2361Change the owner and group id of path to the numeric uid and gid.\n\
2362This function will not follow symbolic links.");
2363
2364static PyObject *
2365posix_lchown(PyObject *self, PyObject *args)
2366{
Victor Stinner8c62be82010-05-06 00:08:46 +00002367 PyObject *opath;
2368 char *path;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002369 uid_t uid;
2370 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00002371 int res;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002372 if (!PyArg_ParseTuple(args, "O&O&O&:lchown",
Victor Stinner8c62be82010-05-06 00:08:46 +00002373 PyUnicode_FSConverter, &opath,
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002374 _Py_Uid_Converter, &uid,
2375 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002376 return NULL;
2377 path = PyBytes_AsString(opath);
2378 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02002379 res = lchown(path, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00002380 Py_END_ALLOW_THREADS
2381 if (res < 0)
2382 return posix_error_with_allocated_filename(opath);
2383 Py_DECREF(opath);
2384 Py_INCREF(Py_None);
2385 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002386}
2387#endif /* HAVE_LCHOWN */
2388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002389
Guido van Rossum36bc6801995-06-14 22:54:23 +00002390#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002391static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002392posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002393{
Victor Stinner8c62be82010-05-06 00:08:46 +00002394 char buf[1026];
2395 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002396
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002397#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002398 if (!use_bytes) {
2399 wchar_t wbuf[1026];
2400 wchar_t *wbuf2 = wbuf;
2401 PyObject *resobj;
2402 DWORD len;
2403 Py_BEGIN_ALLOW_THREADS
2404 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2405 /* If the buffer is large enough, len does not include the
2406 terminating \0. If the buffer is too small, len includes
2407 the space needed for the terminator. */
2408 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2409 wbuf2 = malloc(len * sizeof(wchar_t));
2410 if (wbuf2)
2411 len = GetCurrentDirectoryW(len, wbuf2);
2412 }
2413 Py_END_ALLOW_THREADS
2414 if (!wbuf2) {
2415 PyErr_NoMemory();
2416 return NULL;
2417 }
2418 if (!len) {
2419 if (wbuf2 != wbuf) free(wbuf2);
2420 return win32_error("getcwdu", NULL);
2421 }
2422 resobj = PyUnicode_FromWideChar(wbuf2, len);
2423 if (wbuf2 != wbuf) free(wbuf2);
2424 return resobj;
2425 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002426#endif
2427
Victor Stinner8c62be82010-05-06 00:08:46 +00002428 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002429#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002430 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002431#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002432 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002433#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002434 Py_END_ALLOW_THREADS
2435 if (res == NULL)
2436 return posix_error();
2437 if (use_bytes)
2438 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002439 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002440}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002441
2442PyDoc_STRVAR(posix_getcwd__doc__,
2443"getcwd() -> path\n\n\
2444Return a unicode string representing the current working directory.");
2445
2446static PyObject *
2447posix_getcwd_unicode(PyObject *self)
2448{
2449 return posix_getcwd(0);
2450}
2451
2452PyDoc_STRVAR(posix_getcwdb__doc__,
2453"getcwdb() -> path\n\n\
2454Return a bytes string representing the current working directory.");
2455
2456static PyObject *
2457posix_getcwd_bytes(PyObject *self)
2458{
2459 return posix_getcwd(1);
2460}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002461#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002462
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002463
Guido van Rossumb6775db1994-08-01 11:34:53 +00002464#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002465PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002466"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002467Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468
Barry Warsaw53699e91996-12-10 23:23:01 +00002469static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002470posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002471{
Victor Stinner8c62be82010-05-06 00:08:46 +00002472 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002473}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002474#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002475
Brian Curtin1b9df392010-11-24 20:24:31 +00002476#ifdef MS_WINDOWS
2477PyDoc_STRVAR(win32_link__doc__,
2478"link(src, dst)\n\n\
2479Create a hard link to a file.");
2480
2481static PyObject *
2482win32_link(PyObject *self, PyObject *args)
2483{
2484 PyObject *osrc, *odst;
2485 char *src, *dst;
2486 BOOL rslt;
2487
Brian Curtinfc889c42010-11-28 23:59:46 +00002488 PyUnicodeObject *usrc, *udst;
2489 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2490 Py_BEGIN_ALLOW_THREADS
2491 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2492 PyUnicode_AS_UNICODE(usrc), NULL);
2493 Py_END_ALLOW_THREADS
2494
2495 if (rslt == 0)
2496 return win32_error("link", NULL);
2497
2498 Py_RETURN_NONE;
2499 }
2500
2501 /* Narrow strings also valid. */
2502 PyErr_Clear();
2503
Brian Curtin1b9df392010-11-24 20:24:31 +00002504 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2505 PyUnicode_FSConverter, &odst))
2506 return NULL;
2507
2508 src = PyBytes_AsString(osrc);
2509 dst = PyBytes_AsString(odst);
2510
2511 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002512 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002513 Py_END_ALLOW_THREADS
2514
Stefan Krah30b341f2010-11-27 11:44:18 +00002515 Py_DECREF(osrc);
2516 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002517 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002518 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002519
2520 Py_RETURN_NONE;
2521}
2522#endif /* MS_WINDOWS */
2523
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002525PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002526"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002527Return a list containing the names of the entries in the directory.\n\
2528\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002529 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002530\n\
2531The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002532entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533
Barry Warsaw53699e91996-12-10 23:23:01 +00002534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002535posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002536{
Victor Stinner8c62be82010-05-06 00:08:46 +00002537 /* XXX Should redo this putting the (now four) versions of opendir
2538 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002539#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002540
Victor Stinner8c62be82010-05-06 00:08:46 +00002541 PyObject *d, *v;
2542 HANDLE hFindFile;
2543 BOOL result;
2544 WIN32_FIND_DATA FileData;
2545 PyObject *opath;
2546 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2547 char *bufptr = namebuf;
2548 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002549
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002550 PyObject *po = NULL;
2551 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002552 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002553 Py_UNICODE *wnamebuf, *po_wchars;
Victor Stinner26de69d2011-06-17 15:15:38 +02002554
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002555 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002556 po_wchars = L".";
2557 len = 1;
2558 } else {
2559 po_wchars = PyUnicode_AS_UNICODE(po);
2560 len = PyUnicode_GET_SIZE(po);
2561 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002562 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2564 if (!wnamebuf) {
2565 PyErr_NoMemory();
2566 return NULL;
2567 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002568 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002569 if (len > 0) {
2570 Py_UNICODE wch = wnamebuf[len-1];
2571 if (wch != L'/' && wch != L'\\' && wch != L':')
2572 wnamebuf[len++] = L'\\';
2573 wcscpy(wnamebuf + len, L"*.*");
2574 }
2575 if ((d = PyList_New(0)) == NULL) {
2576 free(wnamebuf);
2577 return NULL;
2578 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002579 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002580 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002581 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 if (hFindFile == INVALID_HANDLE_VALUE) {
2583 int error = GetLastError();
2584 if (error == ERROR_FILE_NOT_FOUND) {
2585 free(wnamebuf);
2586 return d;
2587 }
2588 Py_DECREF(d);
2589 win32_error_unicode("FindFirstFileW", wnamebuf);
2590 free(wnamebuf);
2591 return NULL;
2592 }
2593 do {
2594 /* Skip over . and .. */
2595 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2596 wcscmp(wFileData.cFileName, L"..") != 0) {
2597 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2598 if (v == NULL) {
2599 Py_DECREF(d);
2600 d = NULL;
2601 break;
2602 }
2603 if (PyList_Append(d, v) != 0) {
2604 Py_DECREF(v);
2605 Py_DECREF(d);
2606 d = NULL;
2607 break;
2608 }
2609 Py_DECREF(v);
2610 }
2611 Py_BEGIN_ALLOW_THREADS
2612 result = FindNextFileW(hFindFile, &wFileData);
2613 Py_END_ALLOW_THREADS
2614 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2615 it got to the end of the directory. */
2616 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2617 Py_DECREF(d);
2618 win32_error_unicode("FindNextFileW", wnamebuf);
2619 FindClose(hFindFile);
2620 free(wnamebuf);
2621 return NULL;
2622 }
2623 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002624
Victor Stinner8c62be82010-05-06 00:08:46 +00002625 if (FindClose(hFindFile) == FALSE) {
2626 Py_DECREF(d);
2627 win32_error_unicode("FindClose", wnamebuf);
2628 free(wnamebuf);
2629 return NULL;
2630 }
2631 free(wnamebuf);
2632 return d;
2633 }
2634 /* Drop the argument parsing error as narrow strings
2635 are also valid. */
2636 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002637
Victor Stinner8c62be82010-05-06 00:08:46 +00002638 if (!PyArg_ParseTuple(args, "O&:listdir",
2639 PyUnicode_FSConverter, &opath))
2640 return NULL;
2641 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2642 PyErr_SetString(PyExc_ValueError, "path too long");
2643 Py_DECREF(opath);
2644 return NULL;
2645 }
2646 strcpy(namebuf, PyBytes_AsString(opath));
2647 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002648 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002649 if (len > 0) {
2650 char ch = namebuf[len-1];
2651 if (ch != SEP && ch != ALTSEP && ch != ':')
2652 namebuf[len++] = '/';
2653 strcpy(namebuf + len, "*.*");
2654 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002655
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 if ((d = PyList_New(0)) == NULL)
2657 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002658
Antoine Pitroub73caab2010-08-09 23:39:31 +00002659 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002660 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002661 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002662 if (hFindFile == INVALID_HANDLE_VALUE) {
2663 int error = GetLastError();
2664 if (error == ERROR_FILE_NOT_FOUND)
2665 return d;
2666 Py_DECREF(d);
2667 return win32_error("FindFirstFile", namebuf);
2668 }
2669 do {
2670 /* Skip over . and .. */
2671 if (strcmp(FileData.cFileName, ".") != 0 &&
2672 strcmp(FileData.cFileName, "..") != 0) {
2673 v = PyBytes_FromString(FileData.cFileName);
2674 if (v == NULL) {
2675 Py_DECREF(d);
2676 d = NULL;
2677 break;
2678 }
2679 if (PyList_Append(d, v) != 0) {
2680 Py_DECREF(v);
2681 Py_DECREF(d);
2682 d = NULL;
2683 break;
2684 }
2685 Py_DECREF(v);
2686 }
2687 Py_BEGIN_ALLOW_THREADS
2688 result = FindNextFile(hFindFile, &FileData);
2689 Py_END_ALLOW_THREADS
2690 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2691 it got to the end of the directory. */
2692 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2693 Py_DECREF(d);
2694 win32_error("FindNextFile", namebuf);
2695 FindClose(hFindFile);
2696 return NULL;
2697 }
2698 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002699
Victor Stinner8c62be82010-05-06 00:08:46 +00002700 if (FindClose(hFindFile) == FALSE) {
2701 Py_DECREF(d);
2702 return win32_error("FindClose", namebuf);
2703 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002704
Victor Stinner8c62be82010-05-06 00:08:46 +00002705 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002706
Tim Peters0bb44a42000-09-15 07:44:49 +00002707#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002708
2709#ifndef MAX_PATH
2710#define MAX_PATH CCHMAXPATH
2711#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002712 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002713 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002714 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002715 PyObject *d, *v;
2716 char namebuf[MAX_PATH+5];
2717 HDIR hdir = 1;
2718 ULONG srchcnt = 1;
2719 FILEFINDBUF3 ep;
2720 APIRET rc;
2721
Victor Stinner8c62be82010-05-06 00:08:46 +00002722 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002723 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002724 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002725 name = PyBytes_AsString(oname);
2726 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002727 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002728 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002729 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002730 return NULL;
2731 }
2732 strcpy(namebuf, name);
2733 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002734 if (*pt == ALTSEP)
2735 *pt = SEP;
2736 if (namebuf[len-1] != SEP)
2737 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002738 strcpy(namebuf + len, "*.*");
2739
Neal Norwitz6c913782007-10-14 03:23:09 +00002740 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002741 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002742 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002743 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002744
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002745 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2746 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002747 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002748 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2749 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2750 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002751
2752 if (rc != NO_ERROR) {
2753 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002754 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002755 }
2756
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002757 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002758 do {
2759 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002760 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002761 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002762
2763 strcpy(namebuf, ep.achName);
2764
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002765 /* Leave Case of Name Alone -- In Native Form */
2766 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002767
Christian Heimes72b710a2008-05-26 13:28:38 +00002768 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002769 if (v == NULL) {
2770 Py_DECREF(d);
2771 d = NULL;
2772 break;
2773 }
2774 if (PyList_Append(d, v) != 0) {
2775 Py_DECREF(v);
2776 Py_DECREF(d);
2777 d = NULL;
2778 break;
2779 }
2780 Py_DECREF(v);
2781 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2782 }
2783
Victor Stinnerdcb24032010-04-22 12:08:36 +00002784 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002785 return d;
2786#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002787 PyObject *oname;
2788 char *name;
2789 PyObject *d, *v;
2790 DIR *dirp;
2791 struct dirent *ep;
2792 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002793
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002795 /* v is never read, so it does not need to be initialized yet. */
2796 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002797 arg_is_unicode = 0;
2798 PyErr_Clear();
2799 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002800 oname = NULL;
2801 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002802 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002803 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002804 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002805 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002807 Py_BEGIN_ALLOW_THREADS
2808 dirp = opendir(name);
2809 Py_END_ALLOW_THREADS
2810 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002811 return posix_error_with_allocated_filename(oname);
2812 }
2813 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002814 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002815 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002816 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002817 Py_DECREF(oname);
2818 return NULL;
2819 }
2820 for (;;) {
2821 errno = 0;
2822 Py_BEGIN_ALLOW_THREADS
2823 ep = readdir(dirp);
2824 Py_END_ALLOW_THREADS
2825 if (ep == NULL) {
2826 if (errno == 0) {
2827 break;
2828 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002829 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002830 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002831 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002832 Py_DECREF(d);
2833 return posix_error_with_allocated_filename(oname);
2834 }
2835 }
2836 if (ep->d_name[0] == '.' &&
2837 (NAMLEN(ep) == 1 ||
2838 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2839 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002840 if (arg_is_unicode)
2841 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2842 else
2843 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002844 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002845 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002846 break;
2847 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002848 if (PyList_Append(d, v) != 0) {
2849 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002850 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002851 break;
2852 }
2853 Py_DECREF(v);
2854 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002855 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002856 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002857 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002858 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002859
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002861
Tim Peters0bb44a42000-09-15 07:44:49 +00002862#endif /* which OS */
2863} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002864
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002865#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002866/* A helper function for abspath on win32 */
2867static PyObject *
2868posix__getfullpathname(PyObject *self, PyObject *args)
2869{
Victor Stinner8c62be82010-05-06 00:08:46 +00002870 PyObject *opath;
2871 char *path;
2872 char outbuf[MAX_PATH*2];
2873 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002874#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002875 PyUnicodeObject *po;
2876 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2877 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2878 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2879 Py_UNICODE *wtemp;
2880 DWORD result;
2881 PyObject *v;
2882 result = GetFullPathNameW(wpath,
2883 sizeof(woutbuf)/sizeof(woutbuf[0]),
2884 woutbuf, &wtemp);
2885 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2886 woutbufp = malloc(result * sizeof(Py_UNICODE));
2887 if (!woutbufp)
2888 return PyErr_NoMemory();
2889 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2890 }
2891 if (result)
2892 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2893 else
2894 v = win32_error_unicode("GetFullPathNameW", wpath);
2895 if (woutbufp != woutbuf)
2896 free(woutbufp);
2897 return v;
2898 }
2899 /* Drop the argument parsing error as narrow strings
2900 are also valid. */
2901 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002902
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002903#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002904 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2905 PyUnicode_FSConverter, &opath))
2906 return NULL;
2907 path = PyBytes_AsString(opath);
2908 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2909 outbuf, &temp)) {
2910 win32_error("GetFullPathName", path);
2911 Py_DECREF(opath);
2912 return NULL;
2913 }
2914 Py_DECREF(opath);
2915 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2916 return PyUnicode_Decode(outbuf, strlen(outbuf),
2917 Py_FileSystemDefaultEncoding, NULL);
2918 }
2919 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002920} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002921
Brian Curtind25aef52011-06-13 15:16:04 -05002922
Brian Curtinf5e76d02010-11-24 13:14:05 +00002923
Brian Curtind40e6f72010-07-08 21:39:08 +00002924/* A helper function for samepath on windows */
2925static PyObject *
2926posix__getfinalpathname(PyObject *self, PyObject *args)
2927{
2928 HANDLE hFile;
2929 int buf_size;
2930 wchar_t *target_path;
2931 int result_length;
2932 PyObject *result;
2933 wchar_t *path;
Victor Stinner26de69d2011-06-17 15:15:38 +02002934
Brian Curtin94622b02010-09-24 00:03:39 +00002935 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002936 return NULL;
2937 }
2938
2939 if(!check_GetFinalPathNameByHandle()) {
2940 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2941 NotImplementedError. */
2942 return PyErr_Format(PyExc_NotImplementedError,
2943 "GetFinalPathNameByHandle not available on this platform");
2944 }
2945
2946 hFile = CreateFileW(
2947 path,
2948 0, /* desired access */
2949 0, /* share mode */
2950 NULL, /* security attributes */
2951 OPEN_EXISTING,
2952 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2953 FILE_FLAG_BACKUP_SEMANTICS,
2954 NULL);
Victor Stinner26de69d2011-06-17 15:15:38 +02002955
Brian Curtind40e6f72010-07-08 21:39:08 +00002956 if(hFile == INVALID_HANDLE_VALUE) {
2957 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002958 return PyErr_Format(PyExc_RuntimeError,
2959 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002960 }
2961
2962 /* We have a good handle to the target, use it to determine the
2963 target path name. */
2964 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2965
2966 if(!buf_size)
2967 return win32_error_unicode("GetFinalPathNameByHandle", path);
2968
2969 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2970 if(!target_path)
2971 return PyErr_NoMemory();
2972
2973 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2974 buf_size, VOLUME_NAME_DOS);
2975 if(!result_length)
2976 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2977
2978 if(!CloseHandle(hFile))
2979 return win32_error_unicode("GetFinalPathNameByHandle", path);
2980
2981 target_path[result_length] = 0;
2982 result = PyUnicode_FromUnicode(target_path, result_length);
2983 free(target_path);
2984 return result;
2985
2986} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002987
2988static PyObject *
2989posix__getfileinformation(PyObject *self, PyObject *args)
2990{
2991 HANDLE hFile;
2992 BY_HANDLE_FILE_INFORMATION info;
2993 int fd;
2994
2995 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2996 return NULL;
2997
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002998 if (!_PyVerify_fd(fd))
2999 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003000
3001 hFile = (HANDLE)_get_osfhandle(fd);
3002 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003003 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003004
3005 if (!GetFileInformationByHandle(hFile, &info))
3006 return win32_error("_getfileinformation", NULL);
3007
3008 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3009 info.nFileIndexHigh,
3010 info.nFileIndexLow);
3011}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003012
Brian Curtin95d028f2011-06-09 09:10:38 -05003013PyDoc_STRVAR(posix__isdir__doc__,
3014"Return true if the pathname refers to an existing directory.");
3015
Brian Curtin9c669cc2011-06-08 18:17:18 -05003016static PyObject *
3017posix__isdir(PyObject *self, PyObject *args)
3018{
3019 PyObject *opath;
3020 char *path;
3021 PyUnicodeObject *po;
3022 DWORD attributes;
3023
3024 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3025 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3026
3027 attributes = GetFileAttributesW(wpath);
3028 if (attributes == INVALID_FILE_ATTRIBUTES)
3029 Py_RETURN_FALSE;
3030 goto check;
3031 }
3032 /* Drop the argument parsing error as narrow strings
3033 are also valid. */
3034 PyErr_Clear();
3035
3036 if (!PyArg_ParseTuple(args, "O&:_isdir",
3037 PyUnicode_FSConverter, &opath))
3038 return NULL;
3039
3040 path = PyBytes_AsString(opath);
3041 attributes = GetFileAttributesA(path);
3042 if (attributes == INVALID_FILE_ATTRIBUTES)
3043 Py_RETURN_FALSE;
3044
3045check:
3046 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3047 Py_RETURN_TRUE;
3048 else
3049 Py_RETURN_FALSE;
3050}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003051#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003053PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003054"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003055Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003056
Barry Warsaw53699e91996-12-10 23:23:01 +00003057static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003058posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003059{
Victor Stinner8c62be82010-05-06 00:08:46 +00003060 int res;
3061 PyObject *opath;
3062 char *path;
3063 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003064
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003065#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003066 PyUnicodeObject *po;
3067 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3068 Py_BEGIN_ALLOW_THREADS
3069 /* PyUnicode_AS_UNICODE OK without thread lock as
3070 it is a simple dereference. */
3071 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3072 Py_END_ALLOW_THREADS
3073 if (!res)
3074 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3075 Py_INCREF(Py_None);
3076 return Py_None;
3077 }
3078 /* Drop the argument parsing error as narrow strings
3079 are also valid. */
3080 PyErr_Clear();
3081 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3082 PyUnicode_FSConverter, &opath, &mode))
3083 return NULL;
3084 path = PyBytes_AsString(opath);
3085 Py_BEGIN_ALLOW_THREADS
3086 /* PyUnicode_AS_UNICODE OK without thread lock as
3087 it is a simple dereference. */
3088 res = CreateDirectoryA(path, NULL);
3089 Py_END_ALLOW_THREADS
3090 if (!res) {
3091 win32_error("mkdir", path);
3092 Py_DECREF(opath);
3093 return NULL;
3094 }
3095 Py_DECREF(opath);
3096 Py_INCREF(Py_None);
3097 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003098#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003099
Victor Stinner8c62be82010-05-06 00:08:46 +00003100 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3101 PyUnicode_FSConverter, &opath, &mode))
3102 return NULL;
3103 path = PyBytes_AsString(opath);
3104 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003105#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003106 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003107#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003108 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003109#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 Py_END_ALLOW_THREADS
3111 if (res < 0)
3112 return posix_error_with_allocated_filename(opath);
3113 Py_DECREF(opath);
3114 Py_INCREF(Py_None);
3115 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003116#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003117}
3118
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003119
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003120/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3121#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003122#include <sys/resource.h>
3123#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003124
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003125
3126#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003127PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003128"nice(inc) -> new_priority\n\n\
3129Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003130
Barry Warsaw53699e91996-12-10 23:23:01 +00003131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003132posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003133{
Victor Stinner8c62be82010-05-06 00:08:46 +00003134 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003135
Victor Stinner8c62be82010-05-06 00:08:46 +00003136 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3137 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003138
Victor Stinner8c62be82010-05-06 00:08:46 +00003139 /* There are two flavours of 'nice': one that returns the new
3140 priority (as required by almost all standards out there) and the
3141 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3142 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003143
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 If we are of the nice family that returns the new priority, we
3145 need to clear errno before the call, and check if errno is filled
3146 before calling posix_error() on a returnvalue of -1, because the
3147 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003148
Victor Stinner8c62be82010-05-06 00:08:46 +00003149 errno = 0;
3150 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003151#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003152 if (value == 0)
3153 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003154#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003155 if (value == -1 && errno != 0)
3156 /* either nice() or getpriority() returned an error */
3157 return posix_error();
3158 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003159}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003160#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003161
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003162PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003163"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165
Barry Warsaw53699e91996-12-10 23:23:01 +00003166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003167posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003168{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003169#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003170 PyObject *o1, *o2;
3171 char *p1, *p2;
3172 BOOL result;
3173 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3174 goto error;
3175 if (!convert_to_unicode(&o1))
3176 goto error;
3177 if (!convert_to_unicode(&o2)) {
3178 Py_DECREF(o1);
3179 goto error;
3180 }
3181 Py_BEGIN_ALLOW_THREADS
3182 result = MoveFileW(PyUnicode_AsUnicode(o1),
3183 PyUnicode_AsUnicode(o2));
3184 Py_END_ALLOW_THREADS
3185 Py_DECREF(o1);
3186 Py_DECREF(o2);
3187 if (!result)
3188 return win32_error("rename", NULL);
3189 Py_INCREF(Py_None);
3190 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003191error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003192 PyErr_Clear();
3193 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3194 return NULL;
3195 Py_BEGIN_ALLOW_THREADS
3196 result = MoveFileA(p1, p2);
3197 Py_END_ALLOW_THREADS
3198 if (!result)
3199 return win32_error("rename", NULL);
3200 Py_INCREF(Py_None);
3201 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003202#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003204#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003205}
3206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003209"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211
Barry Warsaw53699e91996-12-10 23:23:01 +00003212static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003213posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003214{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003215#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003218 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003219#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003220}
3221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003223PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003224"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003225Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003226
Barry Warsaw53699e91996-12-10 23:23:01 +00003227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003228posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003229{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003230#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003231 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003232#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003234#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003235}
3236
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003237
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003238#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003239PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003240"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003241Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003242
Barry Warsaw53699e91996-12-10 23:23:01 +00003243static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003244posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003245{
Victor Stinner8c62be82010-05-06 00:08:46 +00003246 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003247#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003248 wchar_t *command;
3249 if (!PyArg_ParseTuple(args, "u:system", &command))
3250 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003251
Victor Stinner8c62be82010-05-06 00:08:46 +00003252 Py_BEGIN_ALLOW_THREADS
3253 sts = _wsystem(command);
3254 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003255#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003256 PyObject *command_obj;
3257 char *command;
3258 if (!PyArg_ParseTuple(args, "O&:system",
3259 PyUnicode_FSConverter, &command_obj))
3260 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003261
Victor Stinner8c62be82010-05-06 00:08:46 +00003262 command = PyBytes_AsString(command_obj);
3263 Py_BEGIN_ALLOW_THREADS
3264 sts = system(command);
3265 Py_END_ALLOW_THREADS
3266 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003267#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003268 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003269}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003270#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003271
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003273PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003274"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003275Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003276
Barry Warsaw53699e91996-12-10 23:23:01 +00003277static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003278posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003279{
Victor Stinner8c62be82010-05-06 00:08:46 +00003280 int i;
3281 if (!PyArg_ParseTuple(args, "i:umask", &i))
3282 return NULL;
3283 i = (int)umask(i);
3284 if (i < 0)
3285 return posix_error();
3286 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003287}
3288
Brian Curtind40e6f72010-07-08 21:39:08 +00003289#ifdef MS_WINDOWS
3290
3291/* override the default DeleteFileW behavior so that directory
3292symlinks can be removed with this function, the same as with
3293Unix symlinks */
3294BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3295{
3296 WIN32_FILE_ATTRIBUTE_DATA info;
3297 WIN32_FIND_DATAW find_data;
3298 HANDLE find_data_handle;
3299 int is_directory = 0;
3300 int is_link = 0;
3301
3302 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3303 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Victor Stinner26de69d2011-06-17 15:15:38 +02003304
Brian Curtind40e6f72010-07-08 21:39:08 +00003305 /* Get WIN32_FIND_DATA structure for the path to determine if
3306 it is a symlink */
3307 if(is_directory &&
3308 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3309 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3310
3311 if(find_data_handle != INVALID_HANDLE_VALUE) {
3312 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3313 FindClose(find_data_handle);
3314 }
3315 }
3316 }
3317
3318 if (is_directory && is_link)
3319 return RemoveDirectoryW(lpFileName);
3320
3321 return DeleteFileW(lpFileName);
3322}
3323#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003325PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003326"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003327Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003329PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003330"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003331Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003332
Barry Warsaw53699e91996-12-10 23:23:01 +00003333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003334posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003335{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003336#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003337 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3338 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003339#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003341#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003342}
3343
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003344
Guido van Rossumb6775db1994-08-01 11:34:53 +00003345#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003346PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003347"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003348Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003349
Barry Warsaw53699e91996-12-10 23:23:01 +00003350static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003351posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003352{
Victor Stinner8c62be82010-05-06 00:08:46 +00003353 struct utsname u;
3354 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003355
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 Py_BEGIN_ALLOW_THREADS
3357 res = uname(&u);
3358 Py_END_ALLOW_THREADS
3359 if (res < 0)
3360 return posix_error();
3361 return Py_BuildValue("(sssss)",
3362 u.sysname,
3363 u.nodename,
3364 u.release,
3365 u.version,
3366 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003367}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003368#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003369
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003370static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003371extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003372{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003373 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003374 if (PyFloat_Check(t)) {
3375 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003376 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003377 if (!intobj)
3378 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003379#if SIZEOF_TIME_T > SIZEOF_LONG
3380 intval = PyLong_AsUnsignedLongLongMask(intobj);
3381#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003382 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003383#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 Py_DECREF(intobj);
3385 if (intval == -1 && PyErr_Occurred())
3386 return -1;
3387 *sec = intval;
3388 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3389 if (*usec < 0)
3390 /* If rounding gave us a negative number,
3391 truncate. */
3392 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003393 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003395#if SIZEOF_TIME_T > SIZEOF_LONG
3396 intval = PyLong_AsUnsignedLongLongMask(t);
3397#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003398 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003399#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003400 if (intval == -1 && PyErr_Occurred())
3401 return -1;
3402 *sec = intval;
3403 *usec = 0;
3404 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003405}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003407PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003408"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003409utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003410Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003411second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003412
Barry Warsaw53699e91996-12-10 23:23:01 +00003413static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003414posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003415{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003416#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003417 PyObject *arg;
3418 PyUnicodeObject *obwpath;
3419 wchar_t *wpath = NULL;
3420 PyObject *oapath;
3421 char *apath;
3422 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003423 time_t atimesec, mtimesec;
3424 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003425 FILETIME atime, mtime;
3426 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003427
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3429 wpath = PyUnicode_AS_UNICODE(obwpath);
3430 Py_BEGIN_ALLOW_THREADS
3431 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3432 NULL, OPEN_EXISTING,
3433 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3434 Py_END_ALLOW_THREADS
3435 if (hFile == INVALID_HANDLE_VALUE)
3436 return win32_error_unicode("utime", wpath);
3437 } else
3438 /* Drop the argument parsing error as narrow strings
3439 are also valid. */
3440 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003441
Victor Stinner8c62be82010-05-06 00:08:46 +00003442 if (!wpath) {
3443 if (!PyArg_ParseTuple(args, "O&O:utime",
3444 PyUnicode_FSConverter, &oapath, &arg))
3445 return NULL;
3446 apath = PyBytes_AsString(oapath);
3447 Py_BEGIN_ALLOW_THREADS
3448 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3449 NULL, OPEN_EXISTING,
3450 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3451 Py_END_ALLOW_THREADS
3452 if (hFile == INVALID_HANDLE_VALUE) {
3453 win32_error("utime", apath);
3454 Py_DECREF(oapath);
3455 return NULL;
3456 }
3457 Py_DECREF(oapath);
3458 }
3459
3460 if (arg == Py_None) {
3461 SYSTEMTIME now;
3462 GetSystemTime(&now);
3463 if (!SystemTimeToFileTime(&now, &mtime) ||
3464 !SystemTimeToFileTime(&now, &atime)) {
3465 win32_error("utime", NULL);
3466 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003467 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003468 }
3469 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3470 PyErr_SetString(PyExc_TypeError,
3471 "utime() arg 2 must be a tuple (atime, mtime)");
3472 goto done;
3473 }
3474 else {
3475 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3476 &atimesec, &ausec) == -1)
3477 goto done;
3478 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3479 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3480 &mtimesec, &musec) == -1)
3481 goto done;
3482 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3483 }
3484 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3485 /* Avoid putting the file name into the error here,
3486 as that may confuse the user into believing that
3487 something is wrong with the file, when it also
3488 could be the time stamp that gives a problem. */
3489 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003490 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003491 }
3492 Py_INCREF(Py_None);
3493 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003494done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003495 CloseHandle(hFile);
3496 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003497#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003498
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 PyObject *opath;
3500 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003501 time_t atime, mtime;
3502 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003503 int res;
3504 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003505
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003506#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003507 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003508#define ATIME buf[0].tv_sec
3509#define MTIME buf[1].tv_sec
3510#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003511/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003512 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003513#define ATIME buf.actime
3514#define MTIME buf.modtime
3515#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003516#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003517 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003518#define ATIME buf[0]
3519#define MTIME buf[1]
3520#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003521#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003522
Mark Hammond817c9292003-12-03 01:22:38 +00003523
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 if (!PyArg_ParseTuple(args, "O&O:utime",
3525 PyUnicode_FSConverter, &opath, &arg))
3526 return NULL;
3527 path = PyBytes_AsString(opath);
3528 if (arg == Py_None) {
3529 /* optional time values not given */
3530 Py_BEGIN_ALLOW_THREADS
3531 res = utime(path, NULL);
3532 Py_END_ALLOW_THREADS
3533 }
3534 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3535 PyErr_SetString(PyExc_TypeError,
3536 "utime() arg 2 must be a tuple (atime, mtime)");
3537 Py_DECREF(opath);
3538 return NULL;
3539 }
3540 else {
3541 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3542 &atime, &ausec) == -1) {
3543 Py_DECREF(opath);
3544 return NULL;
3545 }
3546 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3547 &mtime, &musec) == -1) {
3548 Py_DECREF(opath);
3549 return NULL;
3550 }
3551 ATIME = atime;
3552 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003553#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 buf[0].tv_usec = ausec;
3555 buf[1].tv_usec = musec;
3556 Py_BEGIN_ALLOW_THREADS
3557 res = utimes(path, buf);
3558 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003559#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003560 Py_BEGIN_ALLOW_THREADS
3561 res = utime(path, UTIME_ARG);
3562 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003563#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003564 }
3565 if (res < 0) {
3566 return posix_error_with_allocated_filename(opath);
3567 }
3568 Py_DECREF(opath);
3569 Py_INCREF(Py_None);
3570 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003571#undef UTIME_ARG
3572#undef ATIME
3573#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003574#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003575}
3576
Guido van Rossum85e3b011991-06-03 12:42:10 +00003577
Guido van Rossum3b066191991-06-04 19:40:25 +00003578/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003580PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003581"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003582Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003583
Barry Warsaw53699e91996-12-10 23:23:01 +00003584static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003585posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003586{
Victor Stinner8c62be82010-05-06 00:08:46 +00003587 int sts;
3588 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3589 return NULL;
3590 _exit(sts);
3591 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003592}
3593
Martin v. Löwis114619e2002-10-07 06:44:21 +00003594#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3595static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003596free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003597{
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 Py_ssize_t i;
3599 for (i = 0; i < count; i++)
3600 PyMem_Free(array[i]);
3601 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003602}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003603
Antoine Pitrou69f71142009-05-24 21:25:49 +00003604static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003605int fsconvert_strdup(PyObject *o, char**out)
3606{
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 PyObject *bytes;
3608 Py_ssize_t size;
3609 if (!PyUnicode_FSConverter(o, &bytes))
3610 return 0;
3611 size = PyBytes_GET_SIZE(bytes);
3612 *out = PyMem_Malloc(size+1);
3613 if (!*out)
3614 return 0;
3615 memcpy(*out, PyBytes_AsString(bytes), size+1);
3616 Py_DECREF(bytes);
3617 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003618}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003619#endif
3620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003621
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003622#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003623PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003624"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003625Execute an executable path with arguments, replacing current process.\n\
3626\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003627 path: path of executable file\n\
3628 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003629
Barry Warsaw53699e91996-12-10 23:23:01 +00003630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003631posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003632{
Victor Stinner8c62be82010-05-06 00:08:46 +00003633 PyObject *opath;
3634 char *path;
3635 PyObject *argv;
3636 char **argvlist;
3637 Py_ssize_t i, argc;
3638 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003639
Victor Stinner8c62be82010-05-06 00:08:46 +00003640 /* execv has two arguments: (path, argv), where
3641 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003642
Victor Stinner8c62be82010-05-06 00:08:46 +00003643 if (!PyArg_ParseTuple(args, "O&O:execv",
3644 PyUnicode_FSConverter,
3645 &opath, &argv))
3646 return NULL;
3647 path = PyBytes_AsString(opath);
3648 if (PyList_Check(argv)) {
3649 argc = PyList_Size(argv);
3650 getitem = PyList_GetItem;
3651 }
3652 else if (PyTuple_Check(argv)) {
3653 argc = PyTuple_Size(argv);
3654 getitem = PyTuple_GetItem;
3655 }
3656 else {
3657 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3658 Py_DECREF(opath);
3659 return NULL;
3660 }
3661 if (argc < 1) {
3662 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3663 Py_DECREF(opath);
3664 return NULL;
3665 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003666
Victor Stinner8c62be82010-05-06 00:08:46 +00003667 argvlist = PyMem_NEW(char *, argc+1);
3668 if (argvlist == NULL) {
3669 Py_DECREF(opath);
3670 return PyErr_NoMemory();
3671 }
3672 for (i = 0; i < argc; i++) {
3673 if (!fsconvert_strdup((*getitem)(argv, i),
3674 &argvlist[i])) {
3675 free_string_array(argvlist, i);
3676 PyErr_SetString(PyExc_TypeError,
3677 "execv() arg 2 must contain only strings");
3678 Py_DECREF(opath);
3679 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003680
Victor Stinner8c62be82010-05-06 00:08:46 +00003681 }
3682 }
3683 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003684
Victor Stinner8c62be82010-05-06 00:08:46 +00003685 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003686
Victor Stinner8c62be82010-05-06 00:08:46 +00003687 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003688
Victor Stinner8c62be82010-05-06 00:08:46 +00003689 free_string_array(argvlist, argc);
3690 Py_DECREF(opath);
3691 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003692}
3693
Victor Stinner13bb71c2010-04-23 21:41:56 +00003694static char**
3695parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3696{
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 char **envlist;
3698 Py_ssize_t i, pos, envc;
3699 PyObject *keys=NULL, *vals=NULL;
3700 PyObject *key, *val, *key2, *val2;
3701 char *p, *k, *v;
3702 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003703
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 i = PyMapping_Size(env);
3705 if (i < 0)
3706 return NULL;
3707 envlist = PyMem_NEW(char *, i + 1);
3708 if (envlist == NULL) {
3709 PyErr_NoMemory();
3710 return NULL;
3711 }
3712 envc = 0;
3713 keys = PyMapping_Keys(env);
3714 vals = PyMapping_Values(env);
3715 if (!keys || !vals)
3716 goto error;
3717 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3718 PyErr_Format(PyExc_TypeError,
3719 "env.keys() or env.values() is not a list");
3720 goto error;
3721 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003722
Victor Stinner8c62be82010-05-06 00:08:46 +00003723 for (pos = 0; pos < i; pos++) {
3724 key = PyList_GetItem(keys, pos);
3725 val = PyList_GetItem(vals, pos);
3726 if (!key || !val)
3727 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003728
Victor Stinner8c62be82010-05-06 00:08:46 +00003729 if (PyUnicode_FSConverter(key, &key2) == 0)
3730 goto error;
3731 if (PyUnicode_FSConverter(val, &val2) == 0) {
3732 Py_DECREF(key2);
3733 goto error;
3734 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003735
3736#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003737 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3738 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003739#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003740 k = PyBytes_AsString(key2);
3741 v = PyBytes_AsString(val2);
3742 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003743
Victor Stinner8c62be82010-05-06 00:08:46 +00003744 p = PyMem_NEW(char, len);
3745 if (p == NULL) {
3746 PyErr_NoMemory();
3747 Py_DECREF(key2);
3748 Py_DECREF(val2);
3749 goto error;
3750 }
3751 PyOS_snprintf(p, len, "%s=%s", k, v);
3752 envlist[envc++] = p;
3753 Py_DECREF(key2);
3754 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003755#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003756 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003757#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003758 }
3759 Py_DECREF(vals);
3760 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003761
Victor Stinner8c62be82010-05-06 00:08:46 +00003762 envlist[envc] = 0;
3763 *envc_ptr = envc;
3764 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003765
3766error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003767 Py_XDECREF(keys);
3768 Py_XDECREF(vals);
3769 while (--envc >= 0)
3770 PyMem_DEL(envlist[envc]);
3771 PyMem_DEL(envlist);
3772 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003773}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003775PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003776"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003777Execute a path with arguments and environment, replacing current process.\n\
3778\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003779 path: path of executable file\n\
3780 args: tuple or list of arguments\n\
3781 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003782
Barry Warsaw53699e91996-12-10 23:23:01 +00003783static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003784posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003785{
Victor Stinner8c62be82010-05-06 00:08:46 +00003786 PyObject *opath;
3787 char *path;
3788 PyObject *argv, *env;
3789 char **argvlist;
3790 char **envlist;
3791 Py_ssize_t i, argc, envc;
3792 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3793 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003794
Victor Stinner8c62be82010-05-06 00:08:46 +00003795 /* execve has three arguments: (path, argv, env), where
3796 argv is a list or tuple of strings and env is a dictionary
3797 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003798
Victor Stinner8c62be82010-05-06 00:08:46 +00003799 if (!PyArg_ParseTuple(args, "O&OO:execve",
3800 PyUnicode_FSConverter,
3801 &opath, &argv, &env))
3802 return NULL;
3803 path = PyBytes_AsString(opath);
3804 if (PyList_Check(argv)) {
3805 argc = PyList_Size(argv);
3806 getitem = PyList_GetItem;
3807 }
3808 else if (PyTuple_Check(argv)) {
3809 argc = PyTuple_Size(argv);
3810 getitem = PyTuple_GetItem;
3811 }
3812 else {
3813 PyErr_SetString(PyExc_TypeError,
3814 "execve() arg 2 must be a tuple or list");
3815 goto fail_0;
3816 }
3817 if (!PyMapping_Check(env)) {
3818 PyErr_SetString(PyExc_TypeError,
3819 "execve() arg 3 must be a mapping object");
3820 goto fail_0;
3821 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003822
Victor Stinner8c62be82010-05-06 00:08:46 +00003823 argvlist = PyMem_NEW(char *, argc+1);
3824 if (argvlist == NULL) {
3825 PyErr_NoMemory();
3826 goto fail_0;
3827 }
3828 for (i = 0; i < argc; i++) {
3829 if (!fsconvert_strdup((*getitem)(argv, i),
3830 &argvlist[i]))
3831 {
3832 lastarg = i;
3833 goto fail_1;
3834 }
3835 }
3836 lastarg = argc;
3837 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003838
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 envlist = parse_envlist(env, &envc);
3840 if (envlist == NULL)
3841 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003842
Victor Stinner8c62be82010-05-06 00:08:46 +00003843 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003844
Victor Stinner8c62be82010-05-06 00:08:46 +00003845 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003846
Victor Stinner8c62be82010-05-06 00:08:46 +00003847 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003848
Victor Stinner8c62be82010-05-06 00:08:46 +00003849 while (--envc >= 0)
3850 PyMem_DEL(envlist[envc]);
3851 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003852 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003853 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003854 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003855 Py_DECREF(opath);
3856 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003857}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003858#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003860
Guido van Rossuma1065681999-01-25 23:20:23 +00003861#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003862PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003863"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003864Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003865\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003866 mode: mode of process creation\n\
3867 path: path of executable file\n\
3868 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003869
3870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003871posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003872{
Victor Stinner8c62be82010-05-06 00:08:46 +00003873 PyObject *opath;
3874 char *path;
3875 PyObject *argv;
3876 char **argvlist;
3877 int mode, i;
3878 Py_ssize_t argc;
3879 Py_intptr_t spawnval;
3880 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003881
Victor Stinner8c62be82010-05-06 00:08:46 +00003882 /* spawnv has three arguments: (mode, path, argv), where
3883 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003884
Victor Stinner8c62be82010-05-06 00:08:46 +00003885 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3886 PyUnicode_FSConverter,
3887 &opath, &argv))
3888 return NULL;
3889 path = PyBytes_AsString(opath);
3890 if (PyList_Check(argv)) {
3891 argc = PyList_Size(argv);
3892 getitem = PyList_GetItem;
3893 }
3894 else if (PyTuple_Check(argv)) {
3895 argc = PyTuple_Size(argv);
3896 getitem = PyTuple_GetItem;
3897 }
3898 else {
3899 PyErr_SetString(PyExc_TypeError,
3900 "spawnv() arg 2 must be a tuple or list");
3901 Py_DECREF(opath);
3902 return NULL;
3903 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003904
Victor Stinner8c62be82010-05-06 00:08:46 +00003905 argvlist = PyMem_NEW(char *, argc+1);
3906 if (argvlist == NULL) {
3907 Py_DECREF(opath);
3908 return PyErr_NoMemory();
3909 }
3910 for (i = 0; i < argc; i++) {
3911 if (!fsconvert_strdup((*getitem)(argv, i),
3912 &argvlist[i])) {
3913 free_string_array(argvlist, i);
3914 PyErr_SetString(
3915 PyExc_TypeError,
3916 "spawnv() arg 2 must contain only strings");
3917 Py_DECREF(opath);
3918 return NULL;
3919 }
3920 }
3921 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003922
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003923#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 Py_BEGIN_ALLOW_THREADS
3925 spawnval = spawnv(mode, path, argvlist);
3926 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003927#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003928 if (mode == _OLD_P_OVERLAY)
3929 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003930
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 Py_BEGIN_ALLOW_THREADS
3932 spawnval = _spawnv(mode, path, argvlist);
3933 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003934#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003935
Victor Stinner8c62be82010-05-06 00:08:46 +00003936 free_string_array(argvlist, argc);
3937 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003938
Victor Stinner8c62be82010-05-06 00:08:46 +00003939 if (spawnval == -1)
3940 return posix_error();
3941 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003942#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003943 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003944#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003945 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003946#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003947}
3948
3949
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003950PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003951"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003952Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003953\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003954 mode: mode of process creation\n\
3955 path: path of executable file\n\
3956 args: tuple or list of arguments\n\
3957 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003958
3959static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003960posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003961{
Victor Stinner8c62be82010-05-06 00:08:46 +00003962 PyObject *opath;
3963 char *path;
3964 PyObject *argv, *env;
3965 char **argvlist;
3966 char **envlist;
3967 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003968 int mode;
3969 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 Py_intptr_t spawnval;
3971 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3972 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003973
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 /* spawnve has four arguments: (mode, path, argv, env), where
3975 argv is a list or tuple of strings and env is a dictionary
3976 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003977
Victor Stinner8c62be82010-05-06 00:08:46 +00003978 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3979 PyUnicode_FSConverter,
3980 &opath, &argv, &env))
3981 return NULL;
3982 path = PyBytes_AsString(opath);
3983 if (PyList_Check(argv)) {
3984 argc = PyList_Size(argv);
3985 getitem = PyList_GetItem;
3986 }
3987 else if (PyTuple_Check(argv)) {
3988 argc = PyTuple_Size(argv);
3989 getitem = PyTuple_GetItem;
3990 }
3991 else {
3992 PyErr_SetString(PyExc_TypeError,
3993 "spawnve() arg 2 must be a tuple or list");
3994 goto fail_0;
3995 }
3996 if (!PyMapping_Check(env)) {
3997 PyErr_SetString(PyExc_TypeError,
3998 "spawnve() arg 3 must be a mapping object");
3999 goto fail_0;
4000 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004001
Victor Stinner8c62be82010-05-06 00:08:46 +00004002 argvlist = PyMem_NEW(char *, argc+1);
4003 if (argvlist == NULL) {
4004 PyErr_NoMemory();
4005 goto fail_0;
4006 }
4007 for (i = 0; i < argc; i++) {
4008 if (!fsconvert_strdup((*getitem)(argv, i),
4009 &argvlist[i]))
4010 {
4011 lastarg = i;
4012 goto fail_1;
4013 }
4014 }
4015 lastarg = argc;
4016 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004017
Victor Stinner8c62be82010-05-06 00:08:46 +00004018 envlist = parse_envlist(env, &envc);
4019 if (envlist == NULL)
4020 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004021
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004022#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004023 Py_BEGIN_ALLOW_THREADS
4024 spawnval = spawnve(mode, path, argvlist, envlist);
4025 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004026#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004027 if (mode == _OLD_P_OVERLAY)
4028 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004029
Victor Stinner8c62be82010-05-06 00:08:46 +00004030 Py_BEGIN_ALLOW_THREADS
4031 spawnval = _spawnve(mode, path, argvlist, envlist);
4032 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004033#endif
Tim Peters25059d32001-12-07 20:35:43 +00004034
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 if (spawnval == -1)
4036 (void) posix_error();
4037 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004038#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004040#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004042#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004043
Victor Stinner8c62be82010-05-06 00:08:46 +00004044 while (--envc >= 0)
4045 PyMem_DEL(envlist[envc]);
4046 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004047 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004048 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004049 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004050 Py_DECREF(opath);
4051 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004052}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004053
4054/* OS/2 supports spawnvp & spawnvpe natively */
4055#if defined(PYOS_OS2)
4056PyDoc_STRVAR(posix_spawnvp__doc__,
4057"spawnvp(mode, file, args)\n\n\
4058Execute the program 'file' in a new process, using the environment\n\
4059search path to find the file.\n\
4060\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004061 mode: mode of process creation\n\
4062 file: executable file name\n\
4063 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004064
4065static PyObject *
4066posix_spawnvp(PyObject *self, PyObject *args)
4067{
Victor Stinner8c62be82010-05-06 00:08:46 +00004068 PyObject *opath;
4069 char *path;
4070 PyObject *argv;
4071 char **argvlist;
4072 int mode, i, argc;
4073 Py_intptr_t spawnval;
4074 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004075
Victor Stinner8c62be82010-05-06 00:08:46 +00004076 /* spawnvp has three arguments: (mode, path, argv), where
4077 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004078
Victor Stinner8c62be82010-05-06 00:08:46 +00004079 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4080 PyUnicode_FSConverter,
4081 &opath, &argv))
4082 return NULL;
4083 path = PyBytes_AsString(opath);
4084 if (PyList_Check(argv)) {
4085 argc = PyList_Size(argv);
4086 getitem = PyList_GetItem;
4087 }
4088 else if (PyTuple_Check(argv)) {
4089 argc = PyTuple_Size(argv);
4090 getitem = PyTuple_GetItem;
4091 }
4092 else {
4093 PyErr_SetString(PyExc_TypeError,
4094 "spawnvp() arg 2 must be a tuple or list");
4095 Py_DECREF(opath);
4096 return NULL;
4097 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004098
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 argvlist = PyMem_NEW(char *, argc+1);
4100 if (argvlist == NULL) {
4101 Py_DECREF(opath);
4102 return PyErr_NoMemory();
4103 }
4104 for (i = 0; i < argc; i++) {
4105 if (!fsconvert_strdup((*getitem)(argv, i),
4106 &argvlist[i])) {
4107 free_string_array(argvlist, i);
4108 PyErr_SetString(
4109 PyExc_TypeError,
4110 "spawnvp() arg 2 must contain only strings");
4111 Py_DECREF(opath);
4112 return NULL;
4113 }
4114 }
4115 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004116
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004118#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004119 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004120#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004121 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004122#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004123 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004124
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 free_string_array(argvlist, argc);
4126 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004127
Victor Stinner8c62be82010-05-06 00:08:46 +00004128 if (spawnval == -1)
4129 return posix_error();
4130 else
4131 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004132}
4133
4134
4135PyDoc_STRVAR(posix_spawnvpe__doc__,
4136"spawnvpe(mode, file, args, env)\n\n\
4137Execute the program 'file' in a new process, using the environment\n\
4138search path to find the file.\n\
4139\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004140 mode: mode of process creation\n\
4141 file: executable file name\n\
4142 args: tuple or list of arguments\n\
4143 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004144
4145static PyObject *
4146posix_spawnvpe(PyObject *self, PyObject *args)
4147{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004148 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 char *path;
4150 PyObject *argv, *env;
4151 char **argvlist;
4152 char **envlist;
4153 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004154 int mode;
4155 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004156 Py_intptr_t spawnval;
4157 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4158 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004159
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 /* spawnvpe has four arguments: (mode, path, argv, env), where
4161 argv is a list or tuple of strings and env is a dictionary
4162 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004163
Victor Stinner8c62be82010-05-06 00:08:46 +00004164 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4165 PyUnicode_FSConverter,
4166 &opath, &argv, &env))
4167 return NULL;
4168 path = PyBytes_AsString(opath);
4169 if (PyList_Check(argv)) {
4170 argc = PyList_Size(argv);
4171 getitem = PyList_GetItem;
4172 }
4173 else if (PyTuple_Check(argv)) {
4174 argc = PyTuple_Size(argv);
4175 getitem = PyTuple_GetItem;
4176 }
4177 else {
4178 PyErr_SetString(PyExc_TypeError,
4179 "spawnvpe() arg 2 must be a tuple or list");
4180 goto fail_0;
4181 }
4182 if (!PyMapping_Check(env)) {
4183 PyErr_SetString(PyExc_TypeError,
4184 "spawnvpe() arg 3 must be a mapping object");
4185 goto fail_0;
4186 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004187
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 argvlist = PyMem_NEW(char *, argc+1);
4189 if (argvlist == NULL) {
4190 PyErr_NoMemory();
4191 goto fail_0;
4192 }
4193 for (i = 0; i < argc; i++) {
4194 if (!fsconvert_strdup((*getitem)(argv, i),
4195 &argvlist[i]))
4196 {
4197 lastarg = i;
4198 goto fail_1;
4199 }
4200 }
4201 lastarg = argc;
4202 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004203
Victor Stinner8c62be82010-05-06 00:08:46 +00004204 envlist = parse_envlist(env, &envc);
4205 if (envlist == NULL)
4206 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004207
Victor Stinner8c62be82010-05-06 00:08:46 +00004208 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004209#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004210 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004211#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004213#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004214 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004215
Victor Stinner8c62be82010-05-06 00:08:46 +00004216 if (spawnval == -1)
4217 (void) posix_error();
4218 else
4219 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004220
Victor Stinner8c62be82010-05-06 00:08:46 +00004221 while (--envc >= 0)
4222 PyMem_DEL(envlist[envc]);
4223 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004224 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004225 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004226 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004227 Py_DECREF(opath);
4228 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004229}
4230#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004231#endif /* HAVE_SPAWNV */
4232
4233
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004234#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004235PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004236"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004237Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4238\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004240
4241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004242posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004243{
Victor Stinner8c62be82010-05-06 00:08:46 +00004244 pid_t pid;
4245 int result = 0;
4246 _PyImport_AcquireLock();
4247 pid = fork1();
4248 if (pid == 0) {
4249 /* child: this clobbers and resets the import lock. */
4250 PyOS_AfterFork();
4251 } else {
4252 /* parent: release the import lock. */
4253 result = _PyImport_ReleaseLock();
4254 }
4255 if (pid == -1)
4256 return posix_error();
4257 if (result < 0) {
4258 /* Don't clobber the OSError if the fork failed. */
4259 PyErr_SetString(PyExc_RuntimeError,
4260 "not holding the import lock");
4261 return NULL;
4262 }
4263 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004264}
4265#endif
4266
4267
Guido van Rossumad0ee831995-03-01 10:34:45 +00004268#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004269PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004270"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004271Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004272Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004273
Barry Warsaw53699e91996-12-10 23:23:01 +00004274static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004275posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004276{
Victor Stinner8c62be82010-05-06 00:08:46 +00004277 pid_t pid;
4278 int result = 0;
4279 _PyImport_AcquireLock();
4280 pid = fork();
4281 if (pid == 0) {
4282 /* child: this clobbers and resets the import lock. */
4283 PyOS_AfterFork();
4284 } else {
4285 /* parent: release the import lock. */
4286 result = _PyImport_ReleaseLock();
4287 }
4288 if (pid == -1)
4289 return posix_error();
4290 if (result < 0) {
4291 /* Don't clobber the OSError if the fork failed. */
4292 PyErr_SetString(PyExc_RuntimeError,
4293 "not holding the import lock");
4294 return NULL;
4295 }
4296 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004297}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004298#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004299
Neal Norwitzb59798b2003-03-21 01:43:31 +00004300/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004301/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4302#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004303#define DEV_PTY_FILE "/dev/ptc"
4304#define HAVE_DEV_PTMX
4305#else
4306#define DEV_PTY_FILE "/dev/ptmx"
4307#endif
4308
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004309#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004310#ifdef HAVE_PTY_H
4311#include <pty.h>
4312#else
4313#ifdef HAVE_LIBUTIL_H
4314#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004315#else
4316#ifdef HAVE_UTIL_H
4317#include <util.h>
4318#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004319#endif /* HAVE_LIBUTIL_H */
4320#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004321#ifdef HAVE_STROPTS_H
4322#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004323#endif
4324#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004325
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004326#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004327PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004328"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004329Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004330
4331static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004332posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004333{
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004335#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004336 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004337#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004338#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004339 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004340#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004341 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004342#endif
4343#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004344
Thomas Wouters70c21a12000-07-14 14:28:33 +00004345#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4347 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004348#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4350 if (slave_name == NULL)
4351 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004352
Victor Stinner8c62be82010-05-06 00:08:46 +00004353 slave_fd = open(slave_name, O_RDWR);
4354 if (slave_fd < 0)
4355 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004356#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004357 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4358 if (master_fd < 0)
4359 return posix_error();
4360 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4361 /* change permission of slave */
4362 if (grantpt(master_fd) < 0) {
4363 PyOS_setsig(SIGCHLD, sig_saved);
4364 return posix_error();
4365 }
4366 /* unlock slave */
4367 if (unlockpt(master_fd) < 0) {
4368 PyOS_setsig(SIGCHLD, sig_saved);
4369 return posix_error();
4370 }
4371 PyOS_setsig(SIGCHLD, sig_saved);
4372 slave_name = ptsname(master_fd); /* get name of slave */
4373 if (slave_name == NULL)
4374 return posix_error();
4375 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4376 if (slave_fd < 0)
4377 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004378#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004379 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4380 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004381#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004382 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004383#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004384#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004385#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004386
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004388
Fred Drake8cef4cf2000-06-28 16:40:38 +00004389}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004390#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004391
4392#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004393PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004394"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004395Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4396Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004397To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004398
4399static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004400posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004401{
Victor Stinner8c62be82010-05-06 00:08:46 +00004402 int master_fd = -1, result = 0;
4403 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004404
Victor Stinner8c62be82010-05-06 00:08:46 +00004405 _PyImport_AcquireLock();
4406 pid = forkpty(&master_fd, NULL, NULL, NULL);
4407 if (pid == 0) {
4408 /* child: this clobbers and resets the import lock. */
4409 PyOS_AfterFork();
4410 } else {
4411 /* parent: release the import lock. */
4412 result = _PyImport_ReleaseLock();
4413 }
4414 if (pid == -1)
4415 return posix_error();
4416 if (result < 0) {
4417 /* Don't clobber the OSError if the fork failed. */
4418 PyErr_SetString(PyExc_RuntimeError,
4419 "not holding the import lock");
4420 return NULL;
4421 }
4422 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004423}
4424#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004425
Guido van Rossumad0ee831995-03-01 10:34:45 +00004426#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004427PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004428"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004429Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004430
Barry Warsaw53699e91996-12-10 23:23:01 +00004431static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004432posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004433{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004434 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004435}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004436#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004437
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004438
Guido van Rossumad0ee831995-03-01 10:34:45 +00004439#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004440PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004441"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004442Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004443
Barry Warsaw53699e91996-12-10 23:23:01 +00004444static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004445posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004446{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004447 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004448}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004449#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004450
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004451
Guido van Rossumad0ee831995-03-01 10:34:45 +00004452#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004453PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004454"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004455Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004456
Barry Warsaw53699e91996-12-10 23:23:01 +00004457static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004458posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004459{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004460 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004461}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004462#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004463
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004465PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004466"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004467Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004468
Barry Warsaw53699e91996-12-10 23:23:01 +00004469static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004470posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004471{
Victor Stinner8c62be82010-05-06 00:08:46 +00004472 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004473}
4474
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004475
Fred Drakec9680921999-12-13 16:37:25 +00004476#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004477PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004478"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004479Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004480
4481static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004482posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004483{
4484 PyObject *result = NULL;
4485
Fred Drakec9680921999-12-13 16:37:25 +00004486#ifdef NGROUPS_MAX
4487#define MAX_GROUPS NGROUPS_MAX
4488#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004489 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004490#define MAX_GROUPS 64
4491#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004493
Victor Stinner26de69d2011-06-17 15:15:38 +02004494 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004495 * This is a helper variable to store the intermediate result when
4496 * that happens.
4497 *
4498 * To keep the code readable the OSX behaviour is unconditional,
4499 * according to the POSIX spec this should be safe on all unix-y
4500 * systems.
4501 */
4502 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004503 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004504
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004506 if (n < 0) {
4507 if (errno == EINVAL) {
4508 n = getgroups(0, NULL);
4509 if (n == -1) {
4510 return posix_error();
4511 }
4512 if (n == 0) {
4513 /* Avoid malloc(0) */
4514 alt_grouplist = grouplist;
4515 } else {
4516 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4517 if (alt_grouplist == NULL) {
4518 errno = EINVAL;
4519 return posix_error();
4520 }
4521 n = getgroups(n, alt_grouplist);
4522 if (n == -1) {
4523 PyMem_Free(alt_grouplist);
4524 return posix_error();
4525 }
4526 }
4527 } else {
4528 return posix_error();
4529 }
4530 }
4531 result = PyList_New(n);
4532 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004533 int i;
4534 for (i = 0; i < n; ++i) {
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004535 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004536 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004537 Py_DECREF(result);
4538 result = NULL;
4539 break;
Fred Drakec9680921999-12-13 16:37:25 +00004540 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004542 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004543 }
4544
4545 if (alt_grouplist != grouplist) {
4546 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004548
Fred Drakec9680921999-12-13 16:37:25 +00004549 return result;
4550}
4551#endif
4552
Antoine Pitroub7572f02009-12-02 20:46:48 +00004553#ifdef HAVE_INITGROUPS
4554PyDoc_STRVAR(posix_initgroups__doc__,
4555"initgroups(username, gid) -> None\n\n\
4556Call the system initgroups() to initialize the group access list with all of\n\
4557the groups of which the specified username is a member, plus the specified\n\
4558group id.");
4559
4560static PyObject *
4561posix_initgroups(PyObject *self, PyObject *args)
4562{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004563 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004564 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004565 int res;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004566#ifdef __APPLE__
4567 int gid;
4568#else
4569 gid_t gid;
4570#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00004571
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004572#ifdef __APPLE__
4573 if (!PyArg_ParseTuple(args, "O&i:initgroups",
4574 PyUnicode_FSConverter, &oname,
4575 &gid))
4576#else
4577 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
4578 PyUnicode_FSConverter, &oname,
4579 _Py_Gid_Converter, &gid))
4580#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004581 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004582 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004583
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004584 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004585 Py_DECREF(oname);
4586 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004587 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004588
Victor Stinner8c62be82010-05-06 00:08:46 +00004589 Py_INCREF(Py_None);
4590 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004591}
4592#endif
4593
Martin v. Löwis606edc12002-06-13 21:09:11 +00004594#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004595PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004596"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004597Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004598
4599static PyObject *
4600posix_getpgid(PyObject *self, PyObject *args)
4601{
Victor Stinner8c62be82010-05-06 00:08:46 +00004602 pid_t pid, pgid;
4603 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4604 return NULL;
4605 pgid = getpgid(pid);
4606 if (pgid < 0)
4607 return posix_error();
4608 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004609}
4610#endif /* HAVE_GETPGID */
4611
4612
Guido van Rossumb6775db1994-08-01 11:34:53 +00004613#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004614PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004615"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004616Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004617
Barry Warsaw53699e91996-12-10 23:23:01 +00004618static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004619posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004620{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004621#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004622 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004623#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004624 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004625#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004626}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004627#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004628
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004629
Guido van Rossumb6775db1994-08-01 11:34:53 +00004630#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004632"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004633Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004634
Barry Warsaw53699e91996-12-10 23:23:01 +00004635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004636posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004637{
Guido van Rossum64933891994-10-20 21:56:42 +00004638#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004639 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004640#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004641 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004642#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004643 return posix_error();
4644 Py_INCREF(Py_None);
4645 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004646}
4647
Guido van Rossumb6775db1994-08-01 11:34:53 +00004648#endif /* HAVE_SETPGRP */
4649
Guido van Rossumad0ee831995-03-01 10:34:45 +00004650#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004651
4652#ifdef MS_WINDOWS
4653#include <tlhelp32.h>
4654
4655static PyObject*
4656win32_getppid()
4657{
4658 HANDLE snapshot;
4659 pid_t mypid;
4660 PyObject* result = NULL;
4661 BOOL have_record;
4662 PROCESSENTRY32 pe;
4663
4664 mypid = getpid(); /* This function never fails */
4665
4666 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4667 if (snapshot == INVALID_HANDLE_VALUE)
4668 return PyErr_SetFromWindowsErr(GetLastError());
4669
4670 pe.dwSize = sizeof(pe);
4671 have_record = Process32First(snapshot, &pe);
4672 while (have_record) {
4673 if (mypid == (pid_t)pe.th32ProcessID) {
4674 /* We could cache the ulong value in a static variable. */
4675 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4676 break;
4677 }
4678
4679 have_record = Process32Next(snapshot, &pe);
4680 }
4681
4682 /* If our loop exits and our pid was not found (result will be NULL)
4683 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4684 * error anyway, so let's raise it. */
4685 if (!result)
4686 result = PyErr_SetFromWindowsErr(GetLastError());
4687
4688 CloseHandle(snapshot);
4689
4690 return result;
4691}
4692#endif /*MS_WINDOWS*/
4693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004694PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004695"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004696Return the parent's process id. If the parent process has already exited,\n\
4697Windows machines will still return its id; others systems will return the id\n\
4698of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004699
Barry Warsaw53699e91996-12-10 23:23:01 +00004700static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004701posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004702{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004703#ifdef MS_WINDOWS
4704 return win32_getppid();
4705#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004706 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004707#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004708}
4709#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004711
Fred Drake12c6e2d1999-12-14 21:25:03 +00004712#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004713PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004714"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004715Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004716
4717static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004718posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004719{
Victor Stinner8c62be82010-05-06 00:08:46 +00004720 PyObject *result = NULL;
Victor Stinner26de69d2011-06-17 15:15:38 +02004721#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004722 wchar_t user_name[UNLEN + 1];
4723 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4724
4725 if (GetUserNameW(user_name, &num_chars)) {
4726 /* num_chars is the number of unicode chars plus null terminator */
4727 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Victor Stinner26de69d2011-06-17 15:15:38 +02004728 }
4729 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004730 result = PyErr_SetFromWindowsErr(GetLastError());
4731#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004732 char *name;
4733 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004734
Victor Stinner8c62be82010-05-06 00:08:46 +00004735 errno = 0;
4736 name = getlogin();
4737 if (name == NULL) {
4738 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004739 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004740 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004741 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004742 }
4743 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004744 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004745 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004746#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004747 return result;
4748}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004749#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004750
Guido van Rossumad0ee831995-03-01 10:34:45 +00004751#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004752PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004753"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004754Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004755
Barry Warsaw53699e91996-12-10 23:23:01 +00004756static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004757posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004758{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004759 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004760}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004761#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004763
Guido van Rossumad0ee831995-03-01 10:34:45 +00004764#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004765PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004766"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004767Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004768
Barry Warsaw53699e91996-12-10 23:23:01 +00004769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004770posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004771{
Victor Stinner8c62be82010-05-06 00:08:46 +00004772 pid_t pid;
4773 int sig;
4774 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4775 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004776#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004777 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4778 APIRET rc;
4779 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004780 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004781
4782 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4783 APIRET rc;
4784 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004785 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004786
4787 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004788 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004789#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004790 if (kill(pid, sig) == -1)
4791 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004792#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004793 Py_INCREF(Py_None);
4794 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004795}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004796#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004797
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004798#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004799PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004800"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004801Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004802
4803static PyObject *
4804posix_killpg(PyObject *self, PyObject *args)
4805{
Victor Stinner8c62be82010-05-06 00:08:46 +00004806 int sig;
4807 pid_t pgid;
4808 /* XXX some man pages make the `pgid` parameter an int, others
4809 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4810 take the same type. Moreover, pid_t is always at least as wide as
4811 int (else compilation of this module fails), which is safe. */
4812 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4813 return NULL;
4814 if (killpg(pgid, sig) == -1)
4815 return posix_error();
4816 Py_INCREF(Py_None);
4817 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004818}
4819#endif
4820
Brian Curtineb24d742010-04-12 17:16:38 +00004821#ifdef MS_WINDOWS
4822PyDoc_STRVAR(win32_kill__doc__,
4823"kill(pid, sig)\n\n\
4824Kill a process with a signal.");
4825
4826static PyObject *
4827win32_kill(PyObject *self, PyObject *args)
4828{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004829 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004830 DWORD pid, sig, err;
4831 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004832
Victor Stinner8c62be82010-05-06 00:08:46 +00004833 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4834 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004835
Victor Stinner8c62be82010-05-06 00:08:46 +00004836 /* Console processes which share a common console can be sent CTRL+C or
4837 CTRL+BREAK events, provided they handle said events. */
4838 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4839 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4840 err = GetLastError();
4841 PyErr_SetFromWindowsErr(err);
4842 }
4843 else
4844 Py_RETURN_NONE;
4845 }
Brian Curtineb24d742010-04-12 17:16:38 +00004846
Victor Stinner8c62be82010-05-06 00:08:46 +00004847 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4848 attempt to open and terminate the process. */
4849 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4850 if (handle == NULL) {
4851 err = GetLastError();
4852 return PyErr_SetFromWindowsErr(err);
4853 }
Brian Curtineb24d742010-04-12 17:16:38 +00004854
Victor Stinner8c62be82010-05-06 00:08:46 +00004855 if (TerminateProcess(handle, sig) == 0) {
4856 err = GetLastError();
4857 result = PyErr_SetFromWindowsErr(err);
4858 } else {
4859 Py_INCREF(Py_None);
4860 result = Py_None;
4861 }
Brian Curtineb24d742010-04-12 17:16:38 +00004862
Victor Stinner8c62be82010-05-06 00:08:46 +00004863 CloseHandle(handle);
4864 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004865}
4866#endif /* MS_WINDOWS */
4867
Guido van Rossumc0125471996-06-28 18:55:32 +00004868#ifdef HAVE_PLOCK
4869
4870#ifdef HAVE_SYS_LOCK_H
4871#include <sys/lock.h>
4872#endif
4873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004874PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004875"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004876Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004877
Barry Warsaw53699e91996-12-10 23:23:01 +00004878static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004879posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004880{
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 int op;
4882 if (!PyArg_ParseTuple(args, "i:plock", &op))
4883 return NULL;
4884 if (plock(op) == -1)
4885 return posix_error();
4886 Py_INCREF(Py_None);
4887 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004888}
4889#endif
4890
Guido van Rossumb6775db1994-08-01 11:34:53 +00004891#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004892PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004893"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004894Set the current process's user id.");
4895
Barry Warsaw53699e91996-12-10 23:23:01 +00004896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004897posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004898{
Victor Stinner8c62be82010-05-06 00:08:46 +00004899 uid_t uid;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004900 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004901 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004902 if (setuid(uid) < 0)
4903 return posix_error();
4904 Py_INCREF(Py_None);
4905 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004906}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004907#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004909
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004910#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004911PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004912"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004913Set the current process's effective user id.");
4914
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004915static PyObject *
4916posix_seteuid (PyObject *self, PyObject *args)
4917{
Victor Stinner8c62be82010-05-06 00:08:46 +00004918 uid_t euid;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004919 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004920 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004921 if (seteuid(euid) < 0) {
4922 return posix_error();
4923 } else {
4924 Py_INCREF(Py_None);
4925 return Py_None;
4926 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004927}
4928#endif /* HAVE_SETEUID */
4929
4930#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004931PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004932"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004933Set the current process's effective group id.");
4934
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004935static PyObject *
4936posix_setegid (PyObject *self, PyObject *args)
4937{
Victor Stinner8c62be82010-05-06 00:08:46 +00004938 gid_t egid;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004939 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004940 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004941 if (setegid(egid) < 0) {
4942 return posix_error();
4943 } else {
4944 Py_INCREF(Py_None);
4945 return Py_None;
4946 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004947}
4948#endif /* HAVE_SETEGID */
4949
4950#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004951PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004952"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004953Set the current process's real and effective user ids.");
4954
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004955static PyObject *
4956posix_setreuid (PyObject *self, PyObject *args)
4957{
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 uid_t ruid, euid;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004959 if (!PyArg_ParseTuple(args, "O&O&:setreuid",
4960 _Py_Uid_Converter, &ruid,
4961 _Py_Uid_Converter, &euid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004962 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 if (setreuid(ruid, euid) < 0) {
4964 return posix_error();
4965 } else {
4966 Py_INCREF(Py_None);
4967 return Py_None;
4968 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004969}
4970#endif /* HAVE_SETREUID */
4971
4972#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004973PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004974"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004975Set the current process's real and effective group ids.");
4976
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004977static PyObject *
4978posix_setregid (PyObject *self, PyObject *args)
4979{
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 gid_t rgid, egid;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02004981 if (!PyArg_ParseTuple(args, "O&O&:setregid",
4982 _Py_Gid_Converter, &rgid,
4983 _Py_Gid_Converter, &egid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 if (setregid(rgid, egid) < 0) {
4986 return posix_error();
4987 } else {
4988 Py_INCREF(Py_None);
4989 return Py_None;
4990 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004991}
4992#endif /* HAVE_SETREGID */
4993
Guido van Rossumb6775db1994-08-01 11:34:53 +00004994#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004995PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004996"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004997Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004998
Barry Warsaw53699e91996-12-10 23:23:01 +00004999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005000posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005001{
Victor Stinner8c62be82010-05-06 00:08:46 +00005002 gid_t gid;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02005003 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005005 if (setgid(gid) < 0)
5006 return posix_error();
5007 Py_INCREF(Py_None);
5008 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005009}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005010#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005011
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005012#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005013PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005014"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005015Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005016
5017static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005018posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005019{
Victor Stinner8c62be82010-05-06 00:08:46 +00005020 int i, len;
5021 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005022
Victor Stinner8c62be82010-05-06 00:08:46 +00005023 if (!PySequence_Check(groups)) {
5024 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5025 return NULL;
5026 }
5027 len = PySequence_Size(groups);
5028 if (len > MAX_GROUPS) {
5029 PyErr_SetString(PyExc_ValueError, "too many groups");
5030 return NULL;
5031 }
5032 for(i = 0; i < len; i++) {
5033 PyObject *elem;
5034 elem = PySequence_GetItem(groups, i);
5035 if (!elem)
5036 return NULL;
5037 if (!PyLong_Check(elem)) {
5038 PyErr_SetString(PyExc_TypeError,
5039 "groups must be integers");
5040 Py_DECREF(elem);
5041 return NULL;
5042 } else {
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02005043 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 Py_DECREF(elem);
5045 return NULL;
5046 }
5047 }
5048 Py_DECREF(elem);
5049 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005050
Victor Stinner8c62be82010-05-06 00:08:46 +00005051 if (setgroups(len, grouplist) < 0)
5052 return posix_error();
5053 Py_INCREF(Py_None);
5054 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005055}
5056#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005057
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005058#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5059static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005060wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005061{
Victor Stinner8c62be82010-05-06 00:08:46 +00005062 PyObject *result;
5063 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005064
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 if (pid == -1)
5066 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005067
Victor Stinner8c62be82010-05-06 00:08:46 +00005068 if (struct_rusage == NULL) {
5069 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5070 if (m == NULL)
5071 return NULL;
5072 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5073 Py_DECREF(m);
5074 if (struct_rusage == NULL)
5075 return NULL;
5076 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005077
Victor Stinner8c62be82010-05-06 00:08:46 +00005078 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5079 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5080 if (!result)
5081 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005082
5083#ifndef doubletime
5084#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5085#endif
5086
Victor Stinner8c62be82010-05-06 00:08:46 +00005087 PyStructSequence_SET_ITEM(result, 0,
5088 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5089 PyStructSequence_SET_ITEM(result, 1,
5090 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005091#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5093 SET_INT(result, 2, ru->ru_maxrss);
5094 SET_INT(result, 3, ru->ru_ixrss);
5095 SET_INT(result, 4, ru->ru_idrss);
5096 SET_INT(result, 5, ru->ru_isrss);
5097 SET_INT(result, 6, ru->ru_minflt);
5098 SET_INT(result, 7, ru->ru_majflt);
5099 SET_INT(result, 8, ru->ru_nswap);
5100 SET_INT(result, 9, ru->ru_inblock);
5101 SET_INT(result, 10, ru->ru_oublock);
5102 SET_INT(result, 11, ru->ru_msgsnd);
5103 SET_INT(result, 12, ru->ru_msgrcv);
5104 SET_INT(result, 13, ru->ru_nsignals);
5105 SET_INT(result, 14, ru->ru_nvcsw);
5106 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005107#undef SET_INT
5108
Victor Stinner8c62be82010-05-06 00:08:46 +00005109 if (PyErr_Occurred()) {
5110 Py_DECREF(result);
5111 return NULL;
5112 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005113
Victor Stinner8c62be82010-05-06 00:08:46 +00005114 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005115}
5116#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5117
5118#ifdef HAVE_WAIT3
5119PyDoc_STRVAR(posix_wait3__doc__,
5120"wait3(options) -> (pid, status, rusage)\n\n\
5121Wait for completion of a child process.");
5122
5123static PyObject *
5124posix_wait3(PyObject *self, PyObject *args)
5125{
Victor Stinner8c62be82010-05-06 00:08:46 +00005126 pid_t pid;
5127 int options;
5128 struct rusage ru;
5129 WAIT_TYPE status;
5130 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005131
Victor Stinner8c62be82010-05-06 00:08:46 +00005132 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5133 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005134
Victor Stinner8c62be82010-05-06 00:08:46 +00005135 Py_BEGIN_ALLOW_THREADS
5136 pid = wait3(&status, options, &ru);
5137 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005138
Victor Stinner8c62be82010-05-06 00:08:46 +00005139 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005140}
5141#endif /* HAVE_WAIT3 */
5142
5143#ifdef HAVE_WAIT4
5144PyDoc_STRVAR(posix_wait4__doc__,
5145"wait4(pid, options) -> (pid, status, rusage)\n\n\
5146Wait for completion of a given child process.");
5147
5148static PyObject *
5149posix_wait4(PyObject *self, PyObject *args)
5150{
Victor Stinner8c62be82010-05-06 00:08:46 +00005151 pid_t pid;
5152 int options;
5153 struct rusage ru;
5154 WAIT_TYPE status;
5155 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005156
Victor Stinner8c62be82010-05-06 00:08:46 +00005157 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5158 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005159
Victor Stinner8c62be82010-05-06 00:08:46 +00005160 Py_BEGIN_ALLOW_THREADS
5161 pid = wait4(pid, &status, options, &ru);
5162 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005163
Victor Stinner8c62be82010-05-06 00:08:46 +00005164 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005165}
5166#endif /* HAVE_WAIT4 */
5167
Guido van Rossumb6775db1994-08-01 11:34:53 +00005168#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005169PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005170"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005171Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005172
Barry Warsaw53699e91996-12-10 23:23:01 +00005173static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005174posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005175{
Victor Stinner8c62be82010-05-06 00:08:46 +00005176 pid_t pid;
5177 int options;
5178 WAIT_TYPE status;
5179 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005180
Victor Stinner8c62be82010-05-06 00:08:46 +00005181 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5182 return NULL;
5183 Py_BEGIN_ALLOW_THREADS
5184 pid = waitpid(pid, &status, options);
5185 Py_END_ALLOW_THREADS
5186 if (pid == -1)
5187 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005188
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005190}
5191
Tim Petersab034fa2002-02-01 11:27:43 +00005192#elif defined(HAVE_CWAIT)
5193
5194/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005195PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005196"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005197"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005198
5199static PyObject *
5200posix_waitpid(PyObject *self, PyObject *args)
5201{
Victor Stinner8c62be82010-05-06 00:08:46 +00005202 Py_intptr_t pid;
5203 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005204
Victor Stinner8c62be82010-05-06 00:08:46 +00005205 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5206 return NULL;
5207 Py_BEGIN_ALLOW_THREADS
5208 pid = _cwait(&status, pid, options);
5209 Py_END_ALLOW_THREADS
5210 if (pid == -1)
5211 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005212
Victor Stinner8c62be82010-05-06 00:08:46 +00005213 /* shift the status left a byte so this is more like the POSIX waitpid */
5214 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005215}
5216#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005217
Guido van Rossumad0ee831995-03-01 10:34:45 +00005218#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005219PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005220"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005221Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005222
Barry Warsaw53699e91996-12-10 23:23:01 +00005223static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005224posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005225{
Victor Stinner8c62be82010-05-06 00:08:46 +00005226 pid_t pid;
5227 WAIT_TYPE status;
5228 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005229
Victor Stinner8c62be82010-05-06 00:08:46 +00005230 Py_BEGIN_ALLOW_THREADS
5231 pid = wait(&status);
5232 Py_END_ALLOW_THREADS
5233 if (pid == -1)
5234 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005235
Victor Stinner8c62be82010-05-06 00:08:46 +00005236 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005237}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005238#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005241PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005242"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005243Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005244
Barry Warsaw53699e91996-12-10 23:23:01 +00005245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005246posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005247{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005248#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005249 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005250#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005251#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05005252 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00005253 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005254#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005255 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005256#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005257#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005258}
5259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005260
Guido van Rossumb6775db1994-08-01 11:34:53 +00005261#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005262PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005263"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005264Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005265
Barry Warsaw53699e91996-12-10 23:23:01 +00005266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005267posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005268{
Victor Stinner8c62be82010-05-06 00:08:46 +00005269 PyObject* v;
5270 char buf[MAXPATHLEN];
5271 PyObject *opath;
5272 char *path;
5273 int n;
5274 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005275
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 if (!PyArg_ParseTuple(args, "O&:readlink",
5277 PyUnicode_FSConverter, &opath))
5278 return NULL;
5279 path = PyBytes_AsString(opath);
5280 v = PySequence_GetItem(args, 0);
5281 if (v == NULL) {
5282 Py_DECREF(opath);
5283 return NULL;
5284 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005285
Victor Stinner8c62be82010-05-06 00:08:46 +00005286 if (PyUnicode_Check(v)) {
5287 arg_is_unicode = 1;
5288 }
5289 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005290
Victor Stinner8c62be82010-05-06 00:08:46 +00005291 Py_BEGIN_ALLOW_THREADS
5292 n = readlink(path, buf, (int) sizeof buf);
5293 Py_END_ALLOW_THREADS
5294 if (n < 0)
5295 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005296
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005298 if (arg_is_unicode)
5299 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5300 else
5301 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005302}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005303#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005304
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005305
Brian Curtin52173d42010-12-02 18:29:18 +00005306#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005307PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005308"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005309Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005310
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005312posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005313{
Victor Stinner8c62be82010-05-06 00:08:46 +00005314 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005315}
5316#endif /* HAVE_SYMLINK */
5317
Brian Curtind40e6f72010-07-08 21:39:08 +00005318#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5319
5320PyDoc_STRVAR(win_readlink__doc__,
5321"readlink(path) -> path\n\n\
5322Return a string representing the path to which the symbolic link points.");
5323
Brian Curtind40e6f72010-07-08 21:39:08 +00005324/* Windows readlink implementation */
5325static PyObject *
5326win_readlink(PyObject *self, PyObject *args)
5327{
5328 wchar_t *path;
5329 DWORD n_bytes_returned;
5330 DWORD io_result;
5331 PyObject *result;
5332 HANDLE reparse_point_handle;
5333
5334 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5335 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5336 wchar_t *print_name;
5337
5338 if (!PyArg_ParseTuple(args,
5339 "u:readlink",
5340 &path))
5341 return NULL;
5342
5343 /* First get a handle to the reparse point */
5344 Py_BEGIN_ALLOW_THREADS
5345 reparse_point_handle = CreateFileW(
5346 path,
5347 0,
5348 0,
5349 0,
5350 OPEN_EXISTING,
5351 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5352 0);
5353 Py_END_ALLOW_THREADS
Victor Stinner26de69d2011-06-17 15:15:38 +02005354
Brian Curtind40e6f72010-07-08 21:39:08 +00005355 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5356 {
5357 return win32_error_unicode("readlink", path);
5358 }
Victor Stinner26de69d2011-06-17 15:15:38 +02005359
Brian Curtind40e6f72010-07-08 21:39:08 +00005360 Py_BEGIN_ALLOW_THREADS
5361 /* New call DeviceIoControl to read the reparse point */
5362 io_result = DeviceIoControl(
5363 reparse_point_handle,
5364 FSCTL_GET_REPARSE_POINT,
5365 0, 0, /* in buffer */
5366 target_buffer, sizeof(target_buffer),
5367 &n_bytes_returned,
5368 0 /* we're not using OVERLAPPED_IO */
5369 );
5370 CloseHandle(reparse_point_handle);
5371 Py_END_ALLOW_THREADS
5372
5373 if (io_result==0)
5374 {
5375 return win32_error_unicode("readlink", path);
5376 }
5377
5378 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5379 {
5380 PyErr_SetString(PyExc_ValueError,
5381 "not a symbolic link");
5382 return NULL;
5383 }
Brian Curtin74e45612010-07-09 15:58:59 +00005384 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5385 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5386
5387 result = PyUnicode_FromWideChar(print_name,
5388 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005389 return result;
5390}
5391
5392#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5393
Brian Curtin52173d42010-12-02 18:29:18 +00005394#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005395
5396/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5397static int has_CreateSymbolicLinkW = 0;
5398static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5399static int
5400check_CreateSymbolicLinkW()
5401{
5402 HINSTANCE hKernel32;
5403 /* only recheck */
5404 if (has_CreateSymbolicLinkW)
5405 return has_CreateSymbolicLinkW;
5406 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005407 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5408 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005409 if (Py_CreateSymbolicLinkW)
5410 has_CreateSymbolicLinkW = 1;
5411 return has_CreateSymbolicLinkW;
5412}
5413
5414PyDoc_STRVAR(win_symlink__doc__,
5415"symlink(src, dst, target_is_directory=False)\n\n\
5416Create a symbolic link pointing to src named dst.\n\
5417target_is_directory is required if the target is to be interpreted as\n\
5418a directory.\n\
5419This function requires Windows 6.0 or greater, and raises a\n\
5420NotImplementedError otherwise.");
5421
5422static PyObject *
5423win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5424{
5425 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5426 PyObject *src, *dest;
5427 int target_is_directory = 0;
5428 DWORD res;
Victor Stinner26de69d2011-06-17 15:15:38 +02005429
Brian Curtind40e6f72010-07-08 21:39:08 +00005430 if (!check_CreateSymbolicLinkW())
5431 {
5432 /* raise NotImplementedError */
5433 return PyErr_Format(PyExc_NotImplementedError,
5434 "CreateSymbolicLinkW not found");
5435 }
5436 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5437 kwlist, &src, &dest, &target_is_directory))
5438 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005439
5440 if (win32_can_symlink == 0)
5441 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5442
Brian Curtind40e6f72010-07-08 21:39:08 +00005443 if (!convert_to_unicode(&src)) { return NULL; }
5444 if (!convert_to_unicode(&dest)) {
5445 Py_DECREF(src);
5446 return NULL;
5447 }
Victor Stinner26de69d2011-06-17 15:15:38 +02005448
Brian Curtind40e6f72010-07-08 21:39:08 +00005449 Py_BEGIN_ALLOW_THREADS
5450 res = Py_CreateSymbolicLinkW(
5451 PyUnicode_AsUnicode(dest),
5452 PyUnicode_AsUnicode(src),
5453 target_is_directory);
5454 Py_END_ALLOW_THREADS
5455 Py_DECREF(src);
5456 Py_DECREF(dest);
5457 if (!res)
5458 {
5459 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5460 }
Victor Stinner26de69d2011-06-17 15:15:38 +02005461
Brian Curtind40e6f72010-07-08 21:39:08 +00005462 Py_INCREF(Py_None);
5463 return Py_None;
5464}
Brian Curtin52173d42010-12-02 18:29:18 +00005465#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005466
5467#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005468#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5469static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005470system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005471{
5472 ULONG value = 0;
5473
5474 Py_BEGIN_ALLOW_THREADS
5475 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5476 Py_END_ALLOW_THREADS
5477
5478 return value;
5479}
5480
5481static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005482posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005483{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005484 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005485 return Py_BuildValue("ddddd",
5486 (double)0 /* t.tms_utime / HZ */,
5487 (double)0 /* t.tms_stime / HZ */,
5488 (double)0 /* t.tms_cutime / HZ */,
5489 (double)0 /* t.tms_cstime / HZ */,
5490 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005491}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005492#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005493#define NEED_TICKS_PER_SECOND
5494static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005495static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005496posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005497{
Victor Stinner8c62be82010-05-06 00:08:46 +00005498 struct tms t;
5499 clock_t c;
5500 errno = 0;
5501 c = times(&t);
5502 if (c == (clock_t) -1)
5503 return posix_error();
5504 return Py_BuildValue("ddddd",
5505 (double)t.tms_utime / ticks_per_second,
5506 (double)t.tms_stime / ticks_per_second,
5507 (double)t.tms_cutime / ticks_per_second,
5508 (double)t.tms_cstime / ticks_per_second,
5509 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005510}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005511#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005512#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005513
5514
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005515#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005516#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005517static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005518posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005519{
Victor Stinner8c62be82010-05-06 00:08:46 +00005520 FILETIME create, exit, kernel, user;
5521 HANDLE hProc;
5522 hProc = GetCurrentProcess();
5523 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5524 /* The fields of a FILETIME structure are the hi and lo part
5525 of a 64-bit value expressed in 100 nanosecond units.
5526 1e7 is one second in such units; 1e-7 the inverse.
5527 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5528 */
5529 return Py_BuildValue(
5530 "ddddd",
5531 (double)(user.dwHighDateTime*429.4967296 +
5532 user.dwLowDateTime*1e-7),
5533 (double)(kernel.dwHighDateTime*429.4967296 +
5534 kernel.dwLowDateTime*1e-7),
5535 (double)0,
5536 (double)0,
5537 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005538}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005539#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005540
5541#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005542PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005543"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005544Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005545#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005546
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005547
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005548#ifdef HAVE_GETSID
5549PyDoc_STRVAR(posix_getsid__doc__,
5550"getsid(pid) -> sid\n\n\
5551Call the system call getsid().");
5552
5553static PyObject *
5554posix_getsid(PyObject *self, PyObject *args)
5555{
Victor Stinner8c62be82010-05-06 00:08:46 +00005556 pid_t pid;
5557 int sid;
5558 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5559 return NULL;
5560 sid = getsid(pid);
5561 if (sid < 0)
5562 return posix_error();
5563 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005564}
5565#endif /* HAVE_GETSID */
5566
5567
Guido van Rossumb6775db1994-08-01 11:34:53 +00005568#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005569PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005570"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005571Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005572
Barry Warsaw53699e91996-12-10 23:23:01 +00005573static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005574posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005575{
Victor Stinner8c62be82010-05-06 00:08:46 +00005576 if (setsid() < 0)
5577 return posix_error();
5578 Py_INCREF(Py_None);
5579 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005580}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005581#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005582
Guido van Rossumb6775db1994-08-01 11:34:53 +00005583#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005584PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005585"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005586Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005587
Barry Warsaw53699e91996-12-10 23:23:01 +00005588static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005589posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005590{
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 pid_t pid;
5592 int pgrp;
5593 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5594 return NULL;
5595 if (setpgid(pid, pgrp) < 0)
5596 return posix_error();
5597 Py_INCREF(Py_None);
5598 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005599}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005600#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005602
Guido van Rossumb6775db1994-08-01 11:34:53 +00005603#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005604PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005605"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005606Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005607
Barry Warsaw53699e91996-12-10 23:23:01 +00005608static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005609posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005610{
Victor Stinner8c62be82010-05-06 00:08:46 +00005611 int fd;
5612 pid_t pgid;
5613 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5614 return NULL;
5615 pgid = tcgetpgrp(fd);
5616 if (pgid < 0)
5617 return posix_error();
5618 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005619}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005620#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005621
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005622
Guido van Rossumb6775db1994-08-01 11:34:53 +00005623#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005625"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005627
Barry Warsaw53699e91996-12-10 23:23:01 +00005628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005629posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005630{
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 int fd;
5632 pid_t pgid;
5633 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5634 return NULL;
5635 if (tcsetpgrp(fd, pgid) < 0)
5636 return posix_error();
5637 Py_INCREF(Py_None);
5638 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005639}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005640#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005641
Guido van Rossum687dd131993-05-17 08:34:16 +00005642/* Functions acting on file descriptors */
5643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005644PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005645"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005646Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005647
Barry Warsaw53699e91996-12-10 23:23:01 +00005648static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005649posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005650{
Victor Stinner8c62be82010-05-06 00:08:46 +00005651 PyObject *ofile;
5652 char *file;
5653 int flag;
5654 int mode = 0777;
5655 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005656
5657#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02005659 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005660 Py_BEGIN_ALLOW_THREADS
5661 /* PyUnicode_AS_UNICODE OK without thread
5662 lock as it is a simple dereference. */
5663 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5664 Py_END_ALLOW_THREADS
5665 if (fd < 0)
5666 return posix_error();
5667 return PyLong_FromLong((long)fd);
5668 }
5669 /* Drop the argument parsing error as narrow strings
5670 are also valid. */
5671 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005672#endif
5673
Victor Stinner26de69d2011-06-17 15:15:38 +02005674 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00005675 PyUnicode_FSConverter, &ofile,
5676 &flag, &mode))
5677 return NULL;
5678 file = PyBytes_AsString(ofile);
5679 Py_BEGIN_ALLOW_THREADS
5680 fd = open(file, flag, mode);
5681 Py_END_ALLOW_THREADS
5682 if (fd < 0)
5683 return posix_error_with_allocated_filename(ofile);
5684 Py_DECREF(ofile);
5685 return PyLong_FromLong((long)fd);
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_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005690"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005691Close a file descriptor (for low level IO).");
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_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005695{
Victor Stinner8c62be82010-05-06 00:08:46 +00005696 int fd, res;
5697 if (!PyArg_ParseTuple(args, "i:close", &fd))
5698 return NULL;
5699 if (!_PyVerify_fd(fd))
5700 return posix_error();
5701 Py_BEGIN_ALLOW_THREADS
5702 res = close(fd);
5703 Py_END_ALLOW_THREADS
5704 if (res < 0)
5705 return posix_error();
5706 Py_INCREF(Py_None);
5707 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005708}
5709
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005710
Victor Stinner8c62be82010-05-06 00:08:46 +00005711PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005712"closerange(fd_low, fd_high)\n\n\
5713Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5714
5715static PyObject *
5716posix_closerange(PyObject *self, PyObject *args)
5717{
Victor Stinner8c62be82010-05-06 00:08:46 +00005718 int fd_from, fd_to, i;
5719 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5720 return NULL;
5721 Py_BEGIN_ALLOW_THREADS
5722 for (i = fd_from; i < fd_to; i++)
5723 if (_PyVerify_fd(i))
5724 close(i);
5725 Py_END_ALLOW_THREADS
5726 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005727}
5728
5729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005730PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005731"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005732Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005733
Barry Warsaw53699e91996-12-10 23:23:01 +00005734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005735posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005736{
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 int fd;
5738 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5739 return NULL;
5740 if (!_PyVerify_fd(fd))
5741 return posix_error();
5742 Py_BEGIN_ALLOW_THREADS
5743 fd = dup(fd);
5744 Py_END_ALLOW_THREADS
5745 if (fd < 0)
5746 return posix_error();
5747 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005748}
5749
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005751PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005752"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005753Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005754
Barry Warsaw53699e91996-12-10 23:23:01 +00005755static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005756posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005757{
Victor Stinner8c62be82010-05-06 00:08:46 +00005758 int fd, fd2, res;
5759 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5760 return NULL;
5761 if (!_PyVerify_fd_dup2(fd, fd2))
5762 return posix_error();
5763 Py_BEGIN_ALLOW_THREADS
5764 res = dup2(fd, fd2);
5765 Py_END_ALLOW_THREADS
5766 if (res < 0)
5767 return posix_error();
5768 Py_INCREF(Py_None);
5769 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005770}
5771
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005773PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005774"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01005775Set the current position of a file descriptor.\n\
5776Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005777
Barry Warsaw53699e91996-12-10 23:23:01 +00005778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005779posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005780{
Victor Stinner8c62be82010-05-06 00:08:46 +00005781 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005782#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005784#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005786#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 PyObject *posobj;
5788 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5789 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005790#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5792 switch (how) {
5793 case 0: how = SEEK_SET; break;
5794 case 1: how = SEEK_CUR; break;
5795 case 2: how = SEEK_END; break;
5796 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005797#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005798
5799#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005800 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005801#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005802 pos = PyLong_Check(posobj) ?
5803 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005804#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005805 if (PyErr_Occurred())
5806 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005807
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 if (!_PyVerify_fd(fd))
5809 return posix_error();
5810 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005811#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005812 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005813#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005814 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005815#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 Py_END_ALLOW_THREADS
5817 if (res < 0)
5818 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005819
5820#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005821 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005822#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005823 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005824#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005825}
5826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005828PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005829"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005830Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005831
Barry Warsaw53699e91996-12-10 23:23:01 +00005832static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005833posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005834{
Victor Stinner8c62be82010-05-06 00:08:46 +00005835 int fd, size;
5836 Py_ssize_t n;
5837 PyObject *buffer;
5838 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5839 return NULL;
5840 if (size < 0) {
5841 errno = EINVAL;
5842 return posix_error();
5843 }
5844 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5845 if (buffer == NULL)
5846 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005847 if (!_PyVerify_fd(fd)) {
5848 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005849 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005850 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005851 Py_BEGIN_ALLOW_THREADS
5852 n = read(fd, PyBytes_AS_STRING(buffer), size);
5853 Py_END_ALLOW_THREADS
5854 if (n < 0) {
5855 Py_DECREF(buffer);
5856 return posix_error();
5857 }
5858 if (n != size)
5859 _PyBytes_Resize(&buffer, n);
5860 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005861}
5862
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005863
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005864PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005865"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005866Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005867
Barry Warsaw53699e91996-12-10 23:23:01 +00005868static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005869posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005870{
Victor Stinner8c62be82010-05-06 00:08:46 +00005871 Py_buffer pbuf;
5872 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005873 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005874
Victor Stinner8c62be82010-05-06 00:08:46 +00005875 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5876 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005877 if (!_PyVerify_fd(fd)) {
5878 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005880 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005881 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005883#if defined(MS_WIN64) || defined(MS_WINDOWS)
5884 if (len > INT_MAX)
5885 len = INT_MAX;
5886 size = write(fd, pbuf.buf, (int)len);
5887#else
Victor Stinner72344792011-01-11 00:04:12 +00005888 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005889#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005890 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005891 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005892 if (size < 0)
5893 return posix_error();
5894 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005895}
5896
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005898PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005899"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005900Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005901
Barry Warsaw53699e91996-12-10 23:23:01 +00005902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005903posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005904{
Victor Stinner8c62be82010-05-06 00:08:46 +00005905 int fd;
5906 STRUCT_STAT st;
5907 int res;
5908 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5909 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005910#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005911 /* on OpenVMS we must ensure that all bytes are written to the file */
5912 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005913#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 if (!_PyVerify_fd(fd))
5915 return posix_error();
5916 Py_BEGIN_ALLOW_THREADS
5917 res = FSTAT(fd, &st);
5918 Py_END_ALLOW_THREADS
5919 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005920#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005922#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005923 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005924#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005925 }
Tim Peters5aa91602002-01-30 05:46:57 +00005926
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005928}
5929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005930PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005931"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005932Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005933connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005934
5935static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005936posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005937{
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 int fd;
5939 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5940 return NULL;
5941 if (!_PyVerify_fd(fd))
5942 return PyBool_FromLong(0);
5943 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005944}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005945
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005946#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005947PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005948"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005949Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005950
Barry Warsaw53699e91996-12-10 23:23:01 +00005951static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005952posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005953{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005954#if defined(PYOS_OS2)
5955 HFILE read, write;
5956 APIRET rc;
5957
Victor Stinner8c62be82010-05-06 00:08:46 +00005958 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005959 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005961 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005962 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005963
5964 return Py_BuildValue("(ii)", read, write);
5965#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005966#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 int fds[2];
5968 int res;
5969 Py_BEGIN_ALLOW_THREADS
5970 res = pipe(fds);
5971 Py_END_ALLOW_THREADS
5972 if (res != 0)
5973 return posix_error();
5974 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005975#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005976 HANDLE read, write;
5977 int read_fd, write_fd;
5978 BOOL ok;
5979 Py_BEGIN_ALLOW_THREADS
5980 ok = CreatePipe(&read, &write, NULL, 0);
5981 Py_END_ALLOW_THREADS
5982 if (!ok)
5983 return win32_error("CreatePipe", NULL);
5984 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5985 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5986 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005987#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005988#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005989}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005990#endif /* HAVE_PIPE */
5991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005992
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005993#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005994PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005995"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005996Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005997
Barry Warsaw53699e91996-12-10 23:23:01 +00005998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005999posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006000{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006001 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006002 char *filename;
6003 int mode = 0666;
6004 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006005 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6006 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006008 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006009 Py_BEGIN_ALLOW_THREADS
6010 res = mkfifo(filename, mode);
6011 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006012 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006013 if (res < 0)
6014 return posix_error();
6015 Py_INCREF(Py_None);
6016 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006017}
6018#endif
6019
6020
Neal Norwitz11690112002-07-30 01:08:28 +00006021#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006022PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006023"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006024Create a filesystem node (file, device special file or named pipe)\n\
6025named filename. mode specifies both the permissions to use and the\n\
6026type of node to be created, being combined (bitwise OR) with one of\n\
6027S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006028device defines the newly created device special file (probably using\n\
6029os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006030
6031
6032static PyObject *
6033posix_mknod(PyObject *self, PyObject *args)
6034{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006035 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 char *filename;
6037 int mode = 0600;
6038 int device = 0;
6039 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006040 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6041 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006042 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006043 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006044 Py_BEGIN_ALLOW_THREADS
6045 res = mknod(filename, mode, device);
6046 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006047 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006048 if (res < 0)
6049 return posix_error();
6050 Py_INCREF(Py_None);
6051 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006052}
6053#endif
6054
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006055#ifdef HAVE_DEVICE_MACROS
6056PyDoc_STRVAR(posix_major__doc__,
6057"major(device) -> major number\n\
6058Extracts a device major number from a raw device number.");
6059
6060static PyObject *
6061posix_major(PyObject *self, PyObject *args)
6062{
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 int device;
6064 if (!PyArg_ParseTuple(args, "i:major", &device))
6065 return NULL;
6066 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006067}
6068
6069PyDoc_STRVAR(posix_minor__doc__,
6070"minor(device) -> minor number\n\
6071Extracts a device minor number from a raw device number.");
6072
6073static PyObject *
6074posix_minor(PyObject *self, PyObject *args)
6075{
Victor Stinner8c62be82010-05-06 00:08:46 +00006076 int device;
6077 if (!PyArg_ParseTuple(args, "i:minor", &device))
6078 return NULL;
6079 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006080}
6081
6082PyDoc_STRVAR(posix_makedev__doc__,
6083"makedev(major, minor) -> device number\n\
6084Composes a raw device number from the major and minor device numbers.");
6085
6086static PyObject *
6087posix_makedev(PyObject *self, PyObject *args)
6088{
Victor Stinner8c62be82010-05-06 00:08:46 +00006089 int major, minor;
6090 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6091 return NULL;
6092 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006093}
6094#endif /* device macros */
6095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006096
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006097#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006098PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006099"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006100Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006101
Barry Warsaw53699e91996-12-10 23:23:01 +00006102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006103posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006104{
Victor Stinner8c62be82010-05-06 00:08:46 +00006105 int fd;
6106 off_t length;
6107 int res;
6108 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006109
Victor Stinner8c62be82010-05-06 00:08:46 +00006110 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6111 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006112
6113#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006115#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006116 length = PyLong_Check(lenobj) ?
6117 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006118#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 if (PyErr_Occurred())
6120 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006121
Victor Stinner8c62be82010-05-06 00:08:46 +00006122 Py_BEGIN_ALLOW_THREADS
6123 res = ftruncate(fd, length);
6124 Py_END_ALLOW_THREADS
6125 if (res < 0)
6126 return posix_error();
6127 Py_INCREF(Py_None);
6128 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006129}
6130#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006131
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006132#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006134"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006136
Fred Drake762e2061999-08-26 17:23:54 +00006137/* Save putenv() parameters as values here, so we can collect them when they
6138 * get re-set with another call for the same key. */
6139static PyObject *posix_putenv_garbage;
6140
Tim Peters5aa91602002-01-30 05:46:57 +00006141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006142posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006143{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006144#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006145 wchar_t *s1, *s2;
6146 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006147#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 PyObject *os1, *os2;
6149 char *s1, *s2;
6150 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006151#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006152 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006154
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006155#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006156 if (!PyArg_ParseTuple(args,
6157 "uu:putenv",
6158 &s1, &s2))
6159 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006160#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 if (!PyArg_ParseTuple(args,
6162 "O&O&:putenv",
6163 PyUnicode_FSConverter, &os1,
6164 PyUnicode_FSConverter, &os2))
6165 return NULL;
6166 s1 = PyBytes_AsString(os1);
6167 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006168#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006169
6170#if defined(PYOS_OS2)
6171 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6172 APIRET rc;
6173
Guido van Rossumd48f2521997-12-05 22:19:34 +00006174 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006175 if (rc != NO_ERROR) {
6176 os2_error(rc);
6177 goto error;
6178 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006179
6180 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6181 APIRET rc;
6182
Guido van Rossumd48f2521997-12-05 22:19:34 +00006183 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006184 if (rc != NO_ERROR) {
6185 os2_error(rc);
6186 goto error;
6187 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006188 } else {
6189#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 /* XXX This can leak memory -- not easy to fix :-( */
6191 /* len includes space for a trailing \0; the size arg to
6192 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006193#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006194 len = wcslen(s1) + wcslen(s2) + 2;
Victor Stinner60b385e2011-11-22 22:01:28 +01006195 if (_MAX_ENV < (len - 1)) {
6196 PyErr_Format(PyExc_ValueError,
6197 "the environment variable is longer than %u characters",
6198 _MAX_ENV);
6199 goto error;
6200 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006201 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006202#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006203 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006205#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006206 if (newstr == NULL) {
6207 PyErr_NoMemory();
6208 goto error;
6209 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006210#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006211 newenv = PyUnicode_AsUnicode(newstr);
6212 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6213 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006214 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006215 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 newenv = PyBytes_AS_STRING(newstr);
6219 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6220 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006222 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006224#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006225
Victor Stinner8c62be82010-05-06 00:08:46 +00006226 /* Install the first arg and newstr in posix_putenv_garbage;
6227 * this will cause previous value to be collected. This has to
6228 * happen after the real putenv() call because the old value
6229 * was still accessible until then. */
6230 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006231#ifdef MS_WINDOWS
6232 PyTuple_GET_ITEM(args, 0),
6233#else
6234 os1,
6235#endif
6236 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006237 /* really not much we can do; just leak */
6238 PyErr_Clear();
6239 }
6240 else {
6241 Py_DECREF(newstr);
6242 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006243
6244#if defined(PYOS_OS2)
6245 }
6246#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006247
Martin v. Löwis011e8422009-05-05 04:43:17 +00006248#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006249 Py_DECREF(os1);
6250 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006251#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006252 Py_RETURN_NONE;
6253
6254error:
6255#ifndef MS_WINDOWS
6256 Py_DECREF(os1);
6257 Py_DECREF(os2);
6258#endif
6259 Py_XDECREF(newstr);
6260 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006261}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006262#endif /* putenv */
6263
Guido van Rossumc524d952001-10-19 01:31:59 +00006264#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006265PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006266"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006267Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006268
6269static PyObject *
6270posix_unsetenv(PyObject *self, PyObject *args)
6271{
Victor Stinner84ae1182010-05-06 22:05:07 +00006272 PyObject *os1;
6273 char *s1;
Charles-François Natali6613c182011-11-27 12:41:06 +01006274#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01006275 int err;
Charles-François Natali6613c182011-11-27 12:41:06 +01006276#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006277
6278 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6279 PyUnicode_FSConverter, &os1))
6280 return NULL;
6281 s1 = PyBytes_AsString(os1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006282
Charles-François Natali6613c182011-11-27 12:41:06 +01006283#ifdef HAVE_BROKEN_UNSETENV
6284 unsetenv(s1);
6285#else
Victor Stinner60b385e2011-11-22 22:01:28 +01006286 err = unsetenv(s1);
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06006287 if (err) {
Benjamin Peterson06403cf2011-11-22 23:57:23 -06006288 Py_DECREF(os1);
Victor Stinner60b385e2011-11-22 22:01:28 +01006289 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06006290 }
Charles-François Natali6613c182011-11-27 12:41:06 +01006291#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006292
Victor Stinner8c62be82010-05-06 00:08:46 +00006293 /* Remove the key from posix_putenv_garbage;
6294 * this will cause it to be collected. This has to
6295 * happen after the real unsetenv() call because the
6296 * old value was still accessible until then.
6297 */
Victor Stinner60b385e2011-11-22 22:01:28 +01006298 if (PyDict_DelItem(posix_putenv_garbage, os1)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 /* really not much we can do; just leak */
6300 PyErr_Clear();
6301 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006302
Victor Stinner84ae1182010-05-06 22:05:07 +00006303 Py_DECREF(os1);
Victor Stinner84ae1182010-05-06 22:05:07 +00006304 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006305}
6306#endif /* unsetenv */
6307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006308PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006309"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006310Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006311
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006312static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006313posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006314{
Victor Stinner8c62be82010-05-06 00:08:46 +00006315 int code;
6316 char *message;
6317 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6318 return NULL;
6319 message = strerror(code);
6320 if (message == NULL) {
6321 PyErr_SetString(PyExc_ValueError,
6322 "strerror() argument out of range");
6323 return NULL;
6324 }
6325 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006326}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006327
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006328
Guido van Rossumc9641791998-08-04 15:26:23 +00006329#ifdef HAVE_SYS_WAIT_H
6330
Fred Drake106c1a02002-04-23 15:58:02 +00006331#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006332PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006333"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006334Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006335
6336static PyObject *
6337posix_WCOREDUMP(PyObject *self, PyObject *args)
6338{
Victor Stinner8c62be82010-05-06 00:08:46 +00006339 WAIT_TYPE status;
6340 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006341
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6343 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006344
Victor Stinner8c62be82010-05-06 00:08:46 +00006345 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006346}
6347#endif /* WCOREDUMP */
6348
6349#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006350PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006351"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006352Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006353job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006354
6355static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006356posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006357{
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 WAIT_TYPE status;
6359 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006360
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6362 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006363
Victor Stinner8c62be82010-05-06 00:08:46 +00006364 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006365}
6366#endif /* WIFCONTINUED */
6367
Guido van Rossumc9641791998-08-04 15:26:23 +00006368#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006369PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006370"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006371Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006372
6373static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006374posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006375{
Victor Stinner8c62be82010-05-06 00:08:46 +00006376 WAIT_TYPE status;
6377 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006378
Victor Stinner8c62be82010-05-06 00:08:46 +00006379 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6380 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006381
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006383}
6384#endif /* WIFSTOPPED */
6385
6386#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006387PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006388"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006389Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006390
6391static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006392posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006393{
Victor Stinner8c62be82010-05-06 00:08:46 +00006394 WAIT_TYPE status;
6395 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006396
Victor Stinner8c62be82010-05-06 00:08:46 +00006397 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6398 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006399
Victor Stinner8c62be82010-05-06 00:08:46 +00006400 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006401}
6402#endif /* WIFSIGNALED */
6403
6404#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006406"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006407Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006408system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006409
6410static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006411posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006412{
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 WAIT_TYPE status;
6414 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006415
Victor Stinner8c62be82010-05-06 00:08:46 +00006416 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6417 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006418
Victor Stinner8c62be82010-05-06 00:08:46 +00006419 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006420}
6421#endif /* WIFEXITED */
6422
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006423#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006424PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006425"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006426Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006427
6428static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006429posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006430{
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 WAIT_TYPE status;
6432 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006433
Victor Stinner8c62be82010-05-06 00:08:46 +00006434 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6435 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006436
Victor Stinner8c62be82010-05-06 00:08:46 +00006437 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006438}
6439#endif /* WEXITSTATUS */
6440
6441#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006442PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006443"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006444Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006445value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006446
6447static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006448posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006449{
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 WAIT_TYPE status;
6451 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006452
Victor Stinner8c62be82010-05-06 00:08:46 +00006453 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6454 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006455
Victor Stinner8c62be82010-05-06 00:08:46 +00006456 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006457}
6458#endif /* WTERMSIG */
6459
6460#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006461PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006462"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006463Return the signal that stopped the process that provided\n\
6464the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006465
6466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006467posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006468{
Victor Stinner8c62be82010-05-06 00:08:46 +00006469 WAIT_TYPE status;
6470 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006471
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6473 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006474
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006476}
6477#endif /* WSTOPSIG */
6478
6479#endif /* HAVE_SYS_WAIT_H */
6480
6481
Thomas Wouters477c8d52006-05-27 19:21:47 +00006482#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006483#ifdef _SCO_DS
6484/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6485 needed definitions in sys/statvfs.h */
6486#define _SVID3
6487#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006488#include <sys/statvfs.h>
6489
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006490static PyObject*
6491_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6493 if (v == NULL)
6494 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006495
6496#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6498 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6499 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6500 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6501 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6502 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6503 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6504 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6505 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6506 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006507#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006508 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6509 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6510 PyStructSequence_SET_ITEM(v, 2,
6511 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6512 PyStructSequence_SET_ITEM(v, 3,
6513 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6514 PyStructSequence_SET_ITEM(v, 4,
6515 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6516 PyStructSequence_SET_ITEM(v, 5,
6517 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6518 PyStructSequence_SET_ITEM(v, 6,
6519 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6520 PyStructSequence_SET_ITEM(v, 7,
6521 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6522 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6523 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006524#endif
6525
Victor Stinner8c62be82010-05-06 00:08:46 +00006526 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006527}
6528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006529PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006530"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006531Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006532
6533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006534posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006535{
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 int fd, res;
6537 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006538
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6540 return NULL;
6541 Py_BEGIN_ALLOW_THREADS
6542 res = fstatvfs(fd, &st);
6543 Py_END_ALLOW_THREADS
6544 if (res != 0)
6545 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006546
Victor Stinner8c62be82010-05-06 00:08:46 +00006547 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006548}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006549#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006550
6551
Thomas Wouters477c8d52006-05-27 19:21:47 +00006552#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006553#include <sys/statvfs.h>
6554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006555PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006556"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006557Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006558
6559static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006560posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006561{
Victor Stinnere4110dc2013-01-01 23:05:55 +01006562 PyObject *opath, *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006563 char *path;
6564 int res;
6565 struct statvfs st;
Victor Stinnere4110dc2013-01-01 23:05:55 +01006566 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &opath))
Victor Stinner8c62be82010-05-06 00:08:46 +00006567 return NULL;
Victor Stinnere4110dc2013-01-01 23:05:55 +01006568 path = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006569 Py_BEGIN_ALLOW_THREADS
6570 res = statvfs(path, &st);
6571 Py_END_ALLOW_THREADS
6572 if (res != 0)
Victor Stinnere4110dc2013-01-01 23:05:55 +01006573 return posix_error_with_allocated_filename(opath);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006574
Victor Stinnere4110dc2013-01-01 23:05:55 +01006575 result = _pystatvfs_fromstructstatvfs(st);
6576 Py_DECREF(opath);
6577 return result;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006578}
6579#endif /* HAVE_STATVFS */
6580
Fred Drakec9680921999-12-13 16:37:25 +00006581/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6582 * It maps strings representing configuration variable names to
6583 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006584 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006585 * rarely-used constants. There are three separate tables that use
6586 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006587 *
6588 * This code is always included, even if none of the interfaces that
6589 * need it are included. The #if hackery needed to avoid it would be
6590 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006591 */
6592struct constdef {
6593 char *name;
6594 long value;
6595};
6596
Fred Drake12c6e2d1999-12-14 21:25:03 +00006597static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006598conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006599 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006600{
Christian Heimes217cfd12007-12-02 14:31:20 +00006601 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006602 *valuep = PyLong_AS_LONG(arg);
6603 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006604 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006605 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006606 /* look up the value in the table using a binary search */
6607 size_t lo = 0;
6608 size_t mid;
6609 size_t hi = tablesize;
6610 int cmp;
6611 const char *confname;
6612 if (!PyUnicode_Check(arg)) {
6613 PyErr_SetString(PyExc_TypeError,
6614 "configuration names must be strings or integers");
6615 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006617 confname = _PyUnicode_AsString(arg);
6618 if (confname == NULL)
6619 return 0;
6620 while (lo < hi) {
6621 mid = (lo + hi) / 2;
6622 cmp = strcmp(confname, table[mid].name);
6623 if (cmp < 0)
6624 hi = mid;
6625 else if (cmp > 0)
6626 lo = mid + 1;
6627 else {
6628 *valuep = table[mid].value;
6629 return 1;
6630 }
6631 }
6632 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6633 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006635}
6636
6637
6638#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6639static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006640#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006642#endif
6643#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006644 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006645#endif
Fred Drakec9680921999-12-13 16:37:25 +00006646#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006647 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006648#endif
6649#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006651#endif
6652#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006654#endif
6655#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006656 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006657#endif
6658#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006659 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006660#endif
6661#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006662 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006663#endif
6664#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006666#endif
6667#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006669#endif
6670#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006671 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006672#endif
6673#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006675#endif
6676#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006678#endif
6679#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006681#endif
6682#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006684#endif
6685#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006687#endif
6688#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006690#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006691#ifdef _PC_ACL_ENABLED
6692 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6693#endif
6694#ifdef _PC_MIN_HOLE_SIZE
6695 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6696#endif
6697#ifdef _PC_ALLOC_SIZE_MIN
6698 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6699#endif
6700#ifdef _PC_REC_INCR_XFER_SIZE
6701 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6702#endif
6703#ifdef _PC_REC_MAX_XFER_SIZE
6704 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6705#endif
6706#ifdef _PC_REC_MIN_XFER_SIZE
6707 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6708#endif
6709#ifdef _PC_REC_XFER_ALIGN
6710 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6711#endif
6712#ifdef _PC_SYMLINK_MAX
6713 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6714#endif
6715#ifdef _PC_XATTR_ENABLED
6716 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6717#endif
6718#ifdef _PC_XATTR_EXISTS
6719 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6720#endif
6721#ifdef _PC_TIMESTAMP_RESOLUTION
6722 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6723#endif
Fred Drakec9680921999-12-13 16:37:25 +00006724};
6725
Fred Drakec9680921999-12-13 16:37:25 +00006726static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006727conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006728{
6729 return conv_confname(arg, valuep, posix_constants_pathconf,
6730 sizeof(posix_constants_pathconf)
6731 / sizeof(struct constdef));
6732}
6733#endif
6734
6735#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006736PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006737"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006738Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006739If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006740
6741static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006742posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006743{
6744 PyObject *result = NULL;
6745 int name, fd;
6746
Fred Drake12c6e2d1999-12-14 21:25:03 +00006747 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6748 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006749 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006750
Stefan Krah0e803b32010-11-26 16:16:47 +00006751 errno = 0;
6752 limit = fpathconf(fd, name);
6753 if (limit == -1 && errno != 0)
6754 posix_error();
6755 else
6756 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006757 }
6758 return result;
6759}
6760#endif
6761
6762
6763#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006764PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006765"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006766Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006767If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006768
6769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006770posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006771{
6772 PyObject *result = NULL;
6773 int name;
6774 char *path;
6775
6776 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6777 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006779
Victor Stinner8c62be82010-05-06 00:08:46 +00006780 errno = 0;
6781 limit = pathconf(path, name);
6782 if (limit == -1 && errno != 0) {
6783 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006784 /* could be a path or name problem */
6785 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006786 else
Stefan Krah99439262010-11-26 12:58:05 +00006787 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 }
6789 else
6790 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006791 }
6792 return result;
6793}
6794#endif
6795
6796#ifdef HAVE_CONFSTR
6797static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006798#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006800#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006801#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006803#endif
6804#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006806#endif
Fred Draked86ed291999-12-15 15:34:33 +00006807#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006809#endif
6810#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006812#endif
6813#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006815#endif
6816#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006818#endif
Fred Drakec9680921999-12-13 16:37:25 +00006819#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006821#endif
6822#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006824#endif
6825#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
6831#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006833#endif
6834#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006836#endif
6837#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006839#endif
6840#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006842#endif
Fred Draked86ed291999-12-15 15:34:33 +00006843#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006845#endif
Fred Drakec9680921999-12-13 16:37:25 +00006846#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006848#endif
Fred Draked86ed291999-12-15 15:34:33 +00006849#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006851#endif
6852#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006854#endif
6855#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006857#endif
6858#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006860#endif
Fred Drakec9680921999-12-13 16:37:25 +00006861#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
6864#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
6876#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
6879#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
6894#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006896#endif
6897#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006899#endif
6900#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
Fred Draked86ed291999-12-15 15:34:33 +00006909#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006911#endif
6912#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006914#endif
6915#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006917#endif
6918#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006920#endif
6921#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006923#endif
6924#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006926#endif
6927#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006929#endif
6930#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006932#endif
6933#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006935#endif
6936#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006938#endif
6939#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006941#endif
6942#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006944#endif
6945#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006947#endif
Fred Drakec9680921999-12-13 16:37:25 +00006948};
6949
6950static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006951conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006952{
6953 return conv_confname(arg, valuep, posix_constants_confstr,
6954 sizeof(posix_constants_confstr)
6955 / sizeof(struct constdef));
6956}
6957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006958PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006959"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006960Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006961
6962static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006963posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006964{
6965 PyObject *result = NULL;
6966 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006967 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006968 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006969
Victor Stinnercb043522010-09-10 23:49:04 +00006970 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6971 return NULL;
6972
6973 errno = 0;
6974 len = confstr(name, buffer, sizeof(buffer));
6975 if (len == 0) {
6976 if (errno) {
6977 posix_error();
6978 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006979 }
6980 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006981 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006982 }
6983 }
Victor Stinnercb043522010-09-10 23:49:04 +00006984
6985 if ((unsigned int)len >= sizeof(buffer)) {
6986 char *buf = PyMem_Malloc(len);
6987 if (buf == NULL)
6988 return PyErr_NoMemory();
6989 confstr(name, buf, len);
6990 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6991 PyMem_Free(buf);
6992 }
6993 else
6994 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006995 return result;
6996}
6997#endif
6998
6999
7000#ifdef HAVE_SYSCONF
7001static struct constdef posix_constants_sysconf[] = {
7002#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
7005#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007007#endif
7008#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
7026#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007028#endif
7029#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007031#endif
Fred Draked86ed291999-12-15 15:34:33 +00007032#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007034#endif
7035#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007037#endif
Fred Drakec9680921999-12-13 16:37:25 +00007038#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
Fred Drakec9680921999-12-13 16:37:25 +00007041#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
7044#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
7047#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
Fred Draked86ed291999-12-15 15:34:33 +00007056#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007058#endif
Fred Drakec9680921999-12-13 16:37:25 +00007059#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
7065#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007067#endif
7068#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007070#endif
7071#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
Fred Draked86ed291999-12-15 15:34:33 +00007074#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007076#endif
Fred Drakec9680921999-12-13 16:37:25 +00007077#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
7104#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007106#endif
7107#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007109#endif
7110#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007112#endif
7113#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
7140#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
Fred Draked86ed291999-12-15 15:34:33 +00007146#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007148#endif
Fred Drakec9680921999-12-13 16:37:25 +00007149#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
Fred Draked86ed291999-12-15 15:34:33 +00007158#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007160#endif
Fred Drakec9680921999-12-13 16:37:25 +00007161#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
Fred Draked86ed291999-12-15 15:34:33 +00007164#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007166#endif
7167#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007169#endif
Fred Drakec9680921999-12-13 16:37:25 +00007170#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
7176#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
7179#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
Fred Draked86ed291999-12-15 15:34:33 +00007182#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007184#endif
Fred Drakec9680921999-12-13 16:37:25 +00007185#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
Fred Draked86ed291999-12-15 15:34:33 +00007206#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007208#endif
Fred Drakec9680921999-12-13 16:37:25 +00007209#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
Fred Draked86ed291999-12-15 15:34:33 +00007215#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007217#endif
Fred Drakec9680921999-12-13 16:37:25 +00007218#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
Fred Draked86ed291999-12-15 15:34:33 +00007245#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007247#endif
7248#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007250#endif
Fred Drakec9680921999-12-13 16:37:25 +00007251#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
7284#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007286#endif
7287#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007295#endif
7296#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007298#endif
7299#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007301#endif
7302#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007304#endif
7305#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007307#endif
7308#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007310#endif
7311#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007313#endif
7314#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007316#endif
7317#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007319#endif
7320#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007322#endif
7323#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007325#endif
7326#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007328#endif
7329#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007331#endif
7332#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007334#endif
7335#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007337#endif
7338#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007340#endif
7341#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007343#endif
7344#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007346#endif
7347#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007348 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007349#endif
7350#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007352#endif
7353#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007355#endif
Fred Draked86ed291999-12-15 15:34:33 +00007356#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007358#endif
Fred Drakec9680921999-12-13 16:37:25 +00007359#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007360 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007361#endif
7362#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007363 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007364#endif
7365#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007367#endif
7368#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007370#endif
7371#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007372 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007373#endif
7374#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007375 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007376#endif
7377#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007379#endif
7380#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007382#endif
7383#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007384 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007385#endif
7386#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007387 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007388#endif
7389#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007390 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007391#endif
7392#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007393 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007394#endif
7395#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007396 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007397#endif
7398#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007399 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007400#endif
7401#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007402 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007403#endif
7404#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007406#endif
7407#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007409#endif
7410#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007412#endif
7413#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007415#endif
7416#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007417 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007418#endif
7419#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007421#endif
7422#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007423 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007424#endif
7425#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007426 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007427#endif
7428#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007430#endif
7431#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007432 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007433#endif
7434#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007435 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007436#endif
7437#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007438 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007439#endif
7440#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007441 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007442#endif
7443#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007445#endif
7446#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007447 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007448#endif
7449#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007451#endif
7452#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007454#endif
7455#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007457#endif
7458#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007460#endif
7461#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007463#endif
7464#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007466#endif
7467#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007469#endif
7470#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007471 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007472#endif
7473#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007474 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007475#endif
7476#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007478#endif
7479#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007481#endif
7482#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007484#endif
7485#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007486 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007487#endif
7488#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007489 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007490#endif
7491#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007492 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007493#endif
7494};
7495
7496static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007497conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007498{
7499 return conv_confname(arg, valuep, posix_constants_sysconf,
7500 sizeof(posix_constants_sysconf)
7501 / sizeof(struct constdef));
7502}
7503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007504PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007505"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007506Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007507
7508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007509posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007510{
7511 PyObject *result = NULL;
7512 int name;
7513
7514 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7515 int value;
7516
7517 errno = 0;
7518 value = sysconf(name);
7519 if (value == -1 && errno != 0)
7520 posix_error();
7521 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007522 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007523 }
7524 return result;
7525}
7526#endif
7527
7528
Fred Drakebec628d1999-12-15 18:31:10 +00007529/* This code is used to ensure that the tables of configuration value names
7530 * are in sorted order as required by conv_confname(), and also to build the
7531 * the exported dictionaries that are used to publish information about the
7532 * names available on the host platform.
7533 *
7534 * Sorting the table at runtime ensures that the table is properly ordered
7535 * when used, even for platforms we're not able to test on. It also makes
7536 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007537 */
Fred Drakebec628d1999-12-15 18:31:10 +00007538
7539static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007540cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007541{
7542 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007544 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007545 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007546
7547 return strcmp(c1->name, c2->name);
7548}
7549
7550static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007551setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007553{
Fred Drakebec628d1999-12-15 18:31:10 +00007554 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007555 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007556
7557 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7558 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007559 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007561
Barry Warsaw3155db32000-04-13 15:20:40 +00007562 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 PyObject *o = PyLong_FromLong(table[i].value);
7564 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7565 Py_XDECREF(o);
7566 Py_DECREF(d);
7567 return -1;
7568 }
7569 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007570 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007571 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007572}
7573
Fred Drakebec628d1999-12-15 18:31:10 +00007574/* Return -1 on failure, 0 on success. */
7575static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007576setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007577{
7578#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007579 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007580 sizeof(posix_constants_pathconf)
7581 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007582 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007583 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007584#endif
7585#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007586 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007587 sizeof(posix_constants_confstr)
7588 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007589 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007590 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007591#endif
7592#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007593 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007594 sizeof(posix_constants_sysconf)
7595 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007596 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007597 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007598#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007599 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007600}
Fred Draked86ed291999-12-15 15:34:33 +00007601
7602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007603PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007604"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007605Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007606in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007607
7608static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007609posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007610{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007611 abort();
7612 /*NOTREACHED*/
7613 Py_FatalError("abort() called from Python code didn't abort!");
7614 return NULL;
7615}
Fred Drakebec628d1999-12-15 18:31:10 +00007616
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007617#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007618PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007619"startfile(filepath [, operation]) - Start a file with its associated\n\
7620application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007621\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007622When \"operation\" is not specified or \"open\", this acts like\n\
7623double-clicking the file in Explorer, or giving the file name as an\n\
7624argument to the DOS \"start\" command: the file is opened with whatever\n\
7625application (if any) its extension is associated.\n\
7626When another \"operation\" is given, it specifies what should be done with\n\
7627the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007628\n\
7629startfile returns as soon as the associated application is launched.\n\
7630There is no option to wait for the application to close, and no way\n\
7631to retrieve the application's exit status.\n\
7632\n\
7633The filepath is relative to the current directory. If you want to use\n\
7634an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007635the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007636
7637static PyObject *
7638win32_startfile(PyObject *self, PyObject *args)
7639{
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 PyObject *ofilepath;
7641 char *filepath;
7642 char *operation = NULL;
7643 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007644
Victor Stinner8c62be82010-05-06 00:08:46 +00007645 PyObject *unipath, *woperation = NULL;
7646 if (!PyArg_ParseTuple(args, "U|s:startfile",
7647 &unipath, &operation)) {
7648 PyErr_Clear();
7649 goto normal;
7650 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007651
Victor Stinner8c62be82010-05-06 00:08:46 +00007652 if (operation) {
7653 woperation = PyUnicode_DecodeASCII(operation,
7654 strlen(operation), NULL);
7655 if (!woperation) {
7656 PyErr_Clear();
7657 operation = NULL;
7658 goto normal;
7659 }
7660 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007661
Victor Stinner8c62be82010-05-06 00:08:46 +00007662 Py_BEGIN_ALLOW_THREADS
7663 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7664 PyUnicode_AS_UNICODE(unipath),
7665 NULL, NULL, SW_SHOWNORMAL);
7666 Py_END_ALLOW_THREADS
7667
7668 Py_XDECREF(woperation);
7669 if (rc <= (HINSTANCE)32) {
7670 PyObject *errval = win32_error_unicode("startfile",
7671 PyUnicode_AS_UNICODE(unipath));
7672 return errval;
7673 }
7674 Py_INCREF(Py_None);
7675 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007676
7677normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007678 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7679 PyUnicode_FSConverter, &ofilepath,
7680 &operation))
7681 return NULL;
7682 filepath = PyBytes_AsString(ofilepath);
7683 Py_BEGIN_ALLOW_THREADS
7684 rc = ShellExecute((HWND)0, operation, filepath,
7685 NULL, NULL, SW_SHOWNORMAL);
7686 Py_END_ALLOW_THREADS
7687 if (rc <= (HINSTANCE)32) {
7688 PyObject *errval = win32_error("startfile", filepath);
7689 Py_DECREF(ofilepath);
7690 return errval;
7691 }
7692 Py_DECREF(ofilepath);
7693 Py_INCREF(Py_None);
7694 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007695}
7696#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007697
Martin v. Löwis438b5342002-12-27 10:16:42 +00007698#ifdef HAVE_GETLOADAVG
7699PyDoc_STRVAR(posix_getloadavg__doc__,
7700"getloadavg() -> (float, float, float)\n\n\
7701Return the number of processes in the system run queue averaged over\n\
7702the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7703was unobtainable");
7704
7705static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007706posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007707{
7708 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007709 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007710 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7711 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007712 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007713 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007714}
7715#endif
7716
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007717PyDoc_STRVAR(device_encoding__doc__,
7718"device_encoding(fd) -> str\n\n\
7719Return a string describing the encoding of the device\n\
7720if the output is a terminal; else return None.");
7721
7722static PyObject *
7723device_encoding(PyObject *self, PyObject *args)
7724{
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 int fd;
7726 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7727 return NULL;
7728 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7729 Py_INCREF(Py_None);
7730 return Py_None;
7731 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007732#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 if (fd == 0) {
7734 char buf[100];
7735 sprintf(buf, "cp%d", GetConsoleCP());
7736 return PyUnicode_FromString(buf);
7737 }
7738 if (fd == 1 || fd == 2) {
7739 char buf[100];
7740 sprintf(buf, "cp%d", GetConsoleOutputCP());
7741 return PyUnicode_FromString(buf);
7742 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007743#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 {
7745 char *codeset = nl_langinfo(CODESET);
7746 if (codeset != NULL && codeset[0] != 0)
7747 return PyUnicode_FromString(codeset);
7748 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007749#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 Py_INCREF(Py_None);
7751 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007752}
7753
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007754PyDoc_STRVAR(posix_urandom__doc__,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007755"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007756Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007757
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007758static PyObject *
7759posix_urandom(PyObject *self, PyObject *args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007760{
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007761 Py_ssize_t size;
7762 PyObject *result;
7763 int ret;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007764
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007765 /* Read arguments */
7766 if (!PyArg_ParseTuple(args, "n:urandom", &size))
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 return NULL;
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007768 if (size < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 return PyErr_Format(PyExc_ValueError,
7770 "negative argument not allowed");
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007771 result = PyBytes_FromStringAndSize(NULL, size);
7772 if (result == NULL)
7773 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007774
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007775 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
7776 PyBytes_GET_SIZE(result));
7777 if (ret == -1) {
7778 Py_DECREF(result);
7779 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 }
7781 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007782}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007783
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007784#ifdef HAVE_SETRESUID
7785PyDoc_STRVAR(posix_setresuid__doc__,
7786"setresuid(ruid, euid, suid)\n\n\
7787Set the current process's real, effective, and saved user ids.");
7788
7789static PyObject*
7790posix_setresuid (PyObject *self, PyObject *args)
7791{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02007792 uid_t ruid, euid, suid;
7793 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
7794 _Py_Uid_Converter, &ruid,
7795 _Py_Uid_Converter, &euid,
7796 _Py_Uid_Converter, &suid))
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 return NULL;
7798 if (setresuid(ruid, euid, suid) < 0)
7799 return posix_error();
7800 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007801}
7802#endif
7803
7804#ifdef HAVE_SETRESGID
7805PyDoc_STRVAR(posix_setresgid__doc__,
7806"setresgid(rgid, egid, sgid)\n\n\
7807Set the current process's real, effective, and saved group ids.");
7808
7809static PyObject*
7810posix_setresgid (PyObject *self, PyObject *args)
7811{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02007812 gid_t rgid, egid, sgid;
7813 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
7814 _Py_Gid_Converter, &rgid,
7815 _Py_Gid_Converter, &egid,
7816 _Py_Gid_Converter, &sgid))
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 return NULL;
7818 if (setresgid(rgid, egid, sgid) < 0)
7819 return posix_error();
7820 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007821}
7822#endif
7823
7824#ifdef HAVE_GETRESUID
7825PyDoc_STRVAR(posix_getresuid__doc__,
7826"getresuid() -> (ruid, euid, suid)\n\n\
7827Get tuple of the current process's real, effective, and saved user ids.");
7828
7829static PyObject*
7830posix_getresuid (PyObject *self, PyObject *noargs)
7831{
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007833 if (getresuid(&ruid, &euid, &suid) < 0)
7834 return posix_error();
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02007835 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
7836 _PyLong_FromUid(euid),
7837 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007838}
7839#endif
7840
7841#ifdef HAVE_GETRESGID
7842PyDoc_STRVAR(posix_getresgid__doc__,
7843"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007844Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007845
7846static PyObject*
7847posix_getresgid (PyObject *self, PyObject *noargs)
7848{
Victor Stinner8c62be82010-05-06 00:08:46 +00007849 uid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 if (getresgid(&rgid, &egid, &sgid) < 0)
7851 return posix_error();
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +02007852 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
7853 _PyLong_FromGid(egid),
7854 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007855}
7856#endif
7857
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007858static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007860#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007862#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007863 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007864#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007866#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007868#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007869 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007870#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007871#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007872 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007873#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007874#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007875 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007876#endif /* HAVE_LCHMOD */
7877#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007879#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007880#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007882#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007883#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007884 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007885#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007886#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007887 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007888#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007889#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007891#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007892#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7894 METH_NOARGS, posix_getcwd__doc__},
7895 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7896 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007897#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007898#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007900#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7902 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7903 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007904#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007906#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007907#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007909#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007910#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007911 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007912#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007913 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7914 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7915 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007917#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007919#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007920#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007921 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007922 win_symlink__doc__},
7923#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007924#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007926#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007928#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007929 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007930#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7932 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7933 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007934#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007936#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007938#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7940 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007941#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007942#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7944 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007945#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7947 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007948#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007949#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007950#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007952#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007953#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007955#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007956#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007958#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007959#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007961#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007962#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007964#endif /* HAVE_GETEGID */
7965#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007967#endif /* HAVE_GETEUID */
7968#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007970#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007971#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007973#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007975#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007977#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007978#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007980#endif /* HAVE_GETPPID */
7981#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007983#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007984#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007986#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007987#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007989#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007990#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007992#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007993#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007995#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007996#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7998 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007999 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00008000#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008001#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008003#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008004#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008006#endif /* HAVE_SETEUID */
8007#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008009#endif /* HAVE_SETEGID */
8010#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008011 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008012#endif /* HAVE_SETREUID */
8013#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008015#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008016#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008018#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008019#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008021#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00008022#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00008024#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008025#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008027#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008028#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008030#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008031#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008033#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008034#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00008035 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008036#endif /* HAVE_WAIT3 */
8037#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008039#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008040#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008042#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008043#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008044 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008045#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008046#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008048#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008049#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008051#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008052#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008054#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008055#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008057#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00008058 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8059 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8060 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8061 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
8062 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8063 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8064 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8065 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8066 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8067 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8068 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008069#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008071#endif
8072#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00008073 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008074#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008075#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008077#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008078#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8080 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8081 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008082#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008083#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008085#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008086#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008088#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008089#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008091#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008092 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008093#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008095#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008096#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008098#endif
8099#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008101#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008102#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008103#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008104 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008105#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008106#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008108#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008109#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008111#endif /* WIFSTOPPED */
8112#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008113 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008114#endif /* WIFSIGNALED */
8115#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008116 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008117#endif /* WIFEXITED */
8118#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008119 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008120#endif /* WEXITSTATUS */
8121#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008122 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008123#endif /* WTERMSIG */
8124#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008125 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008126#endif /* WSTOPSIG */
8127#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008128#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008130#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008131#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008133#endif
Fred Drakec9680921999-12-13 16:37:25 +00008134#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008146 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008147#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008148 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008149 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008150 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -05008151 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00008152#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008153#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008154 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008155#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +01008156 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008157#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008158 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008159#endif
8160#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008161 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008162#endif
8163#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008165#endif
8166#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008167 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008168#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008169 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008170};
8171
8172
Barry Warsaw4a342091996-12-19 23:50:02 +00008173static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008174ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008175{
Victor Stinner8c62be82010-05-06 00:08:46 +00008176 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008177}
8178
Guido van Rossumd48f2521997-12-05 22:19:34 +00008179#if defined(PYOS_OS2)
8180/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008181static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008182{
8183 APIRET rc;
8184 ULONG values[QSV_MAX+1];
8185 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008186 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008187
8188 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008189 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008190 Py_END_ALLOW_THREADS
8191
8192 if (rc != NO_ERROR) {
8193 os2_error(rc);
8194 return -1;
8195 }
8196
Fred Drake4d1e64b2002-04-15 19:40:07 +00008197 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8198 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8199 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8200 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8201 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8202 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8203 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008204
8205 switch (values[QSV_VERSION_MINOR]) {
8206 case 0: ver = "2.00"; break;
8207 case 10: ver = "2.10"; break;
8208 case 11: ver = "2.11"; break;
8209 case 30: ver = "3.00"; break;
8210 case 40: ver = "4.00"; break;
8211 case 50: ver = "5.00"; break;
8212 default:
Tim Peters885d4572001-11-28 20:27:42 +00008213 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008215 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008216 ver = &tmp[0];
8217 }
8218
8219 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008220 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008221 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008222
8223 /* Add Indicator of Which Drive was Used to Boot the System */
8224 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8225 tmp[1] = ':';
8226 tmp[2] = '\0';
8227
Fred Drake4d1e64b2002-04-15 19:40:07 +00008228 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008229}
8230#endif
8231
Brian Curtin52173d42010-12-02 18:29:18 +00008232#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008233static int
Brian Curtin52173d42010-12-02 18:29:18 +00008234enable_symlink()
8235{
8236 HANDLE tok;
8237 TOKEN_PRIVILEGES tok_priv;
8238 LUID luid;
8239 int meth_idx = 0;
8240
8241 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008242 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008243
8244 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008245 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008246
8247 tok_priv.PrivilegeCount = 1;
8248 tok_priv.Privileges[0].Luid = luid;
8249 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8250
8251 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8252 sizeof(TOKEN_PRIVILEGES),
8253 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008254 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008255
Brian Curtin3b4499c2010-12-28 14:31:47 +00008256 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8257 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008258}
8259#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8260
Barry Warsaw4a342091996-12-19 23:50:02 +00008261static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008262all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008263{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008264#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008266#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008267#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008269#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008270#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008272#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008273#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008275#endif
Fred Drakec9680921999-12-13 16:37:25 +00008276#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008278#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008279#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008281#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008282#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008284#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008285#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008287#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008288#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008290#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008291#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008293#endif
8294#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008296#endif
8297#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008299#endif
8300#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008302#endif
8303#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008305#endif
8306#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008308#endif
8309#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008311#endif
8312#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008314#endif
8315#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008317#endif
8318#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008320#endif
8321#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008323#endif
8324#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008326#endif
8327#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008329#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008330#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008332#endif
8333#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008335#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008336#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008338#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008339#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008341#endif
8342#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008344#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008345
Tim Peters5aa91602002-01-30 05:46:57 +00008346/* MS Windows */
8347#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 /* Don't inherit in child processes. */
8349 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008350#endif
8351#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 /* Optimize for short life (keep in memory). */
8353 /* MS forgot to define this one with a non-underscore form too. */
8354 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008355#endif
8356#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008357 /* Automatically delete when last handle is closed. */
8358 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008359#endif
8360#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008361 /* Optimize for random access. */
8362 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008363#endif
8364#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 /* Optimize for sequential access. */
8366 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008367#endif
8368
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008369/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008370#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 /* Send a SIGIO signal whenever input or output
8372 becomes available on file descriptor */
8373 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008374#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008375#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008376 /* Direct disk access. */
8377 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008378#endif
8379#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 /* Must be a directory. */
8381 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008382#endif
8383#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 /* Do not follow links. */
8385 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008386#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008387#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 /* Do not update the access time. */
8389 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008390#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008391
Victor Stinner8c62be82010-05-06 00:08:46 +00008392 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008393#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008395#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008396#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008397 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008398#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008399#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008400 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008401#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008402#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008403 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008404#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008405#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008406 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008407#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008408#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008409 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008410#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008411#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008413#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008414#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008415 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008416#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008417#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008418 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008419#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008420#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008421 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008422#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008423#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008424 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008425#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008426#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008428#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008429#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008430 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008431#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008432#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008433 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008434#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008435#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008436 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008437#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008438#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008439 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008440#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008441#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008442 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008443#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008444
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008445 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008446#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008447 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008448#endif /* ST_RDONLY */
8449#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008450 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008451#endif /* ST_NOSUID */
8452
Guido van Rossum246bc171999-02-01 23:54:31 +00008453#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008454#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8456 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8457 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8458 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8459 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8460 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8461 if (ins(d, "P_PM", (long)P_PM)) return -1;
8462 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8463 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8464 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8465 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8466 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8467 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8468 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8469 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8470 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8471 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8472 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8473 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8474 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008475#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8477 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8478 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8479 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8480 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008481#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008482#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008483
Guido van Rossumd48f2521997-12-05 22:19:34 +00008484#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008486#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008487 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008488}
8489
8490
Tim Peters5aa91602002-01-30 05:46:57 +00008491#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008492#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008493#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008494
8495#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008496#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008497#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008498
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008499#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008500#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008501#define MODNAME "posix"
8502#endif
8503
Martin v. Löwis1a214512008-06-11 05:26:20 +00008504static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008505 PyModuleDef_HEAD_INIT,
8506 MODNAME,
8507 posix__doc__,
8508 -1,
8509 posix_methods,
8510 NULL,
8511 NULL,
8512 NULL,
8513 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008514};
8515
8516
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008517PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008518INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008519{
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008521
Brian Curtin52173d42010-12-02 18:29:18 +00008522#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008523 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008524#endif
8525
Victor Stinner8c62be82010-05-06 00:08:46 +00008526 m = PyModule_Create(&posixmodule);
8527 if (m == NULL)
8528 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008529
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 /* Initialize environ dictionary */
8531 v = convertenviron();
8532 Py_XINCREF(v);
8533 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8534 return NULL;
8535 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008536
Victor Stinner8c62be82010-05-06 00:08:46 +00008537 if (all_ins(m))
8538 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008539
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 if (setup_confname_tables(m))
8541 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008542
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 Py_INCREF(PyExc_OSError);
8544 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008545
Guido van Rossumb3d39562000-01-31 18:41:26 +00008546#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008547 if (posix_putenv_garbage == NULL)
8548 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008549#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008550
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 if (!initialized) {
8552 stat_result_desc.name = MODNAME ".stat_result";
8553 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8554 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8555 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8556 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8557 structseq_new = StatResultType.tp_new;
8558 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008559
Victor Stinner8c62be82010-05-06 00:08:46 +00008560 statvfs_result_desc.name = MODNAME ".statvfs_result";
8561 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008562#ifdef NEED_TICKS_PER_SECOND
8563# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008564 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008565# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008567# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008568 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008569# endif
8570#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008571 }
8572 Py_INCREF((PyObject*) &StatResultType);
8573 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8574 Py_INCREF((PyObject*) &StatVFSResultType);
8575 PyModule_AddObject(m, "statvfs_result",
8576 (PyObject*) &StatVFSResultType);
8577 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008578
8579#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008580 /*
8581 * Step 2 of weak-linking support on Mac OS X.
8582 *
8583 * The code below removes functions that are not available on the
8584 * currently active platform.
8585 *
8586 * This block allow one to use a python binary that was build on
8587 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8588 * OSX 10.4.
8589 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008590#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 if (fstatvfs == NULL) {
8592 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8593 return NULL;
8594 }
8595 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008596#endif /* HAVE_FSTATVFS */
8597
8598#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 if (statvfs == NULL) {
8600 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8601 return NULL;
8602 }
8603 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008604#endif /* HAVE_STATVFS */
8605
8606# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008607 if (lchown == NULL) {
8608 if (PyObject_DelAttrString(m, "lchown") == -1) {
8609 return NULL;
8610 }
8611 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008612#endif /* HAVE_LCHOWN */
8613
8614
8615#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008616 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008617
Guido van Rossumb6775db1994-08-01 11:34:53 +00008618}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008619
8620#ifdef __cplusplus
8621}
8622#endif