blob: aca52e5e56263fb0602065b25638e7e4aa6bc8ca [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020047#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500106#ifdef HAVE_SCHED_H
107#include <sched.h>
108#endif
109
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400110#ifdef HAVE_SYS_XATTR_H
111#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400112#endif
113
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000114#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
115#ifdef HAVE_SYS_SOCKET_H
116#include <sys/socket.h>
117#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#endif
119
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000121/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000122#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000123#include <process.h>
124#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_GETCWD 1
127#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000129#if defined(__OS2__)
130#define HAVE_EXECV 1
131#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000132#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#include <process.h>
134#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#define HAVE_EXECV 1
137#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000138#define HAVE_OPENDIR 1
139#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000140#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_WAIT 1
142#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000143#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000144#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000145#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000146#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000147#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000148#define HAVE_EXECV 1
149#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#define HAVE_SYSTEM 1
151#define HAVE_CWAIT 1
152#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000153#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000154#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000155#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
156/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000157#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158/* Unix functions that the configure script doesn't check for */
159#define HAVE_EXECV 1
160#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000161#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000162#define HAVE_FORK1 1
163#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#define HAVE_GETCWD 1
165#define HAVE_GETEGID 1
166#define HAVE_GETEUID 1
167#define HAVE_GETGID 1
168#define HAVE_GETPPID 1
169#define HAVE_GETUID 1
170#define HAVE_KILL 1
171#define HAVE_OPENDIR 1
172#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000173#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000175#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000176#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177#endif /* _MSC_VER */
178#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000179#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000181
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000183
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000184#if defined(__sgi)&&_COMPILER_VERSION>=700
185/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
186 (default) */
187extern char *ctermid_r(char *);
188#endif
189
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000190#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000193#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000194#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000196#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#endif
200#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000201extern int chdir(char *);
202extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000203#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int chdir(const char *);
205extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000206#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000207#ifdef __BORLANDC__
208extern int chmod(const char *, int);
209#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000211#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000212/*#ifdef HAVE_FCHMOD
213extern int fchmod(int, mode_t);
214#endif*/
215/*#ifdef HAVE_LCHMOD
216extern int lchmod(const char *, mode_t);
217#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int chown(const char *, uid_t, gid_t);
219extern char *getcwd(char *, int);
220extern char *strerror(int);
221extern int link(const char *, const char *);
222extern int rename(const char *, const char *);
223extern int stat(const char *, struct stat *);
224extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000226extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000227#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000229extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000230#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000232
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000233#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#ifdef HAVE_UTIME_H
236#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000239#ifdef HAVE_SYS_UTIME_H
240#include <sys/utime.h>
241#define HAVE_UTIME_H /* pretend we do for the rest of this file */
242#endif /* HAVE_SYS_UTIME_H */
243
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#ifdef HAVE_SYS_TIMES_H
245#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000246#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
248#ifdef HAVE_SYS_PARAM_H
249#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
252#ifdef HAVE_SYS_UTSNAME_H
253#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000256#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#define NAMLEN(dirent) strlen((dirent)->d_name)
259#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000260#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#include <direct.h>
262#define NAMLEN(dirent) strlen((dirent)->d_name)
263#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000265#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000266#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000267#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269#endif
270#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#endif
273#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#endif
276#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281#endif
282#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284#endif
285#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000288#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000289#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000290#endif
291#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000292#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000293#endif
294#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000295#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000296#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000297#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000298#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000300#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000301#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000302#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
303#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000304static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000305#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000306#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307
Guido van Rossumd48f2521997-12-05 22:19:34 +0000308#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000309#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000310#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313#if defined(PATH_MAX) && PATH_MAX > 1024
314#define MAXPATHLEN PATH_MAX
315#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000318#endif /* MAXPATHLEN */
319
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000320#ifdef UNION_WAIT
321/* Emulate some macros on systems that have a union instead of macros */
322
323#ifndef WIFEXITED
324#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
325#endif
326
327#ifndef WEXITSTATUS
328#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
329#endif
330
331#ifndef WTERMSIG
332#define WTERMSIG(u_wait) ((u_wait).w_termsig)
333#endif
334
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335#define WAIT_TYPE union wait
336#define WAIT_STATUS_INT(s) (s.w_status)
337
338#else /* !UNION_WAIT */
339#define WAIT_TYPE int
340#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000341#endif /* UNION_WAIT */
342
Greg Wardb48bc172000-03-01 21:51:56 +0000343/* Don't use the "_r" form if we don't need it (also, won't have a
344 prototype for it, at least on Solaris -- maybe others as well?). */
345#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
346#define USE_CTERMID_R
347#endif
348
Fred Drake699f3522000-06-29 21:12:41 +0000349/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000350#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000351#undef FSTAT
352#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000353#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000354# define STAT win32_stat
355# define FSTAT win32_fstat
356# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000357#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000358# define STAT stat
359# define FSTAT fstat
360# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000361#endif
362
Tim Peters11b23062003-04-23 02:39:17 +0000363#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000364#include <sys/mkdev.h>
365#else
366#if defined(MAJOR_IN_SYSMACROS)
367#include <sys/sysmacros.h>
368#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000369#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
370#include <sys/mkdev.h>
371#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000372#endif
Fred Drake699f3522000-06-29 21:12:41 +0000373
Georg Brandla391b112011-02-25 15:23:18 +0000374static int
375_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000376{
377#if !defined(HAVE_LARGEFILE_SUPPORT)
378 *((off_t*)addr) = PyLong_AsLong(arg);
379#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000380 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000381#endif
382 if (PyErr_Occurred())
383 return 0;
384 return 1;
385}
386
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000387#if defined _MSC_VER && _MSC_VER >= 1400
388/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
389 * valid and throw an assertion if it isn't.
390 * Normally, an invalid fd is likely to be a C program error and therefore
391 * an assertion can be useful, but it does contradict the POSIX standard
392 * which for write(2) states:
393 * "Otherwise, -1 shall be returned and errno set to indicate the error."
394 * "[EBADF] The fildes argument is not a valid file descriptor open for
395 * writing."
396 * Furthermore, python allows the user to enter any old integer
397 * as a fd and should merely raise a python exception on error.
398 * The Microsoft CRT doesn't provide an official way to check for the
399 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000400 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000401 * internal structures involved.
402 * The structures below must be updated for each version of visual studio
403 * according to the file internal.h in the CRT source, until MS comes
404 * up with a less hacky way to do this.
405 * (all of this is to avoid globally modifying the CRT behaviour using
406 * _set_invalid_parameter_handler() and _CrtSetReportMode())
407 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000408/* The actual size of the structure is determined at runtime.
409 * Only the first items must be present.
410 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000411typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000412 intptr_t osfhnd;
413 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000414} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000415
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000416extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000417#define IOINFO_L2E 5
418#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
419#define IOINFO_ARRAYS 64
420#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
421#define FOPEN 0x01
422#define _NO_CONSOLE_FILENO (intptr_t)-2
423
424/* This function emulates what the windows CRT does to validate file handles */
425int
426_PyVerify_fd(int fd)
427{
Victor Stinner8c62be82010-05-06 00:08:46 +0000428 const int i1 = fd >> IOINFO_L2E;
429 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000430
Antoine Pitrou22e41552010-08-15 18:07:50 +0000431 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000432
Victor Stinner8c62be82010-05-06 00:08:46 +0000433 /* Determine the actual size of the ioinfo structure,
434 * as used by the CRT loaded in memory
435 */
436 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
437 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
438 }
439 if (sizeof_ioinfo == 0) {
440 /* This should not happen... */
441 goto fail;
442 }
443
444 /* See that it isn't a special CLEAR fileno */
445 if (fd != _NO_CONSOLE_FILENO) {
446 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
447 * we check pointer validity and other info
448 */
449 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
450 /* finally, check that the file is open */
451 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
452 if (info->osfile & FOPEN) {
453 return 1;
454 }
455 }
456 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000457 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000458 errno = EBADF;
459 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000460}
461
462/* the special case of checking dup2. The target fd must be in a sensible range */
463static int
464_PyVerify_fd_dup2(int fd1, int fd2)
465{
Victor Stinner8c62be82010-05-06 00:08:46 +0000466 if (!_PyVerify_fd(fd1))
467 return 0;
468 if (fd2 == _NO_CONSOLE_FILENO)
469 return 0;
470 if ((unsigned)fd2 < _NHANDLE_)
471 return 1;
472 else
473 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000474}
475#else
476/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
477#define _PyVerify_fd_dup2(A, B) (1)
478#endif
479
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000480#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000481/* The following structure was copied from
482 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
483 include doesn't seem to be present in the Windows SDK (at least as included
484 with Visual Studio Express). */
485typedef struct _REPARSE_DATA_BUFFER {
486 ULONG ReparseTag;
487 USHORT ReparseDataLength;
488 USHORT Reserved;
489 union {
490 struct {
491 USHORT SubstituteNameOffset;
492 USHORT SubstituteNameLength;
493 USHORT PrintNameOffset;
494 USHORT PrintNameLength;
495 ULONG Flags;
496 WCHAR PathBuffer[1];
497 } SymbolicLinkReparseBuffer;
498
499 struct {
500 USHORT SubstituteNameOffset;
501 USHORT SubstituteNameLength;
502 USHORT PrintNameOffset;
503 USHORT PrintNameLength;
504 WCHAR PathBuffer[1];
505 } MountPointReparseBuffer;
506
507 struct {
508 UCHAR DataBuffer[1];
509 } GenericReparseBuffer;
510 };
511} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
512
513#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
514 GenericReparseBuffer)
515#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
516
517static int
Brian Curtind25aef52011-06-13 15:16:04 -0500518win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000519{
520 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
521 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
522 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000523
524 if (0 == DeviceIoControl(
525 reparse_point_handle,
526 FSCTL_GET_REPARSE_POINT,
527 NULL, 0, /* in buffer */
528 target_buffer, sizeof(target_buffer),
529 &n_bytes_returned,
530 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500531 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000532
533 if (reparse_tag)
534 *reparse_tag = rdb->ReparseTag;
535
Brian Curtind25aef52011-06-13 15:16:04 -0500536 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000537}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000538#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000539
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000541#ifdef WITH_NEXT_FRAMEWORK
542/* On Darwin/MacOSX a shared library or framework has no access to
543** environ directly, we must obtain it with _NSGetEnviron().
544*/
545#include <crt_externs.h>
546static char **environ;
547#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000549#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550
Barry Warsaw53699e91996-12-10 23:23:01 +0000551static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000552convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553{
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000555#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000557#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000558 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000559#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000560#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000561 APIRET rc;
562 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
563#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000564
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 d = PyDict_New();
566 if (d == NULL)
567 return NULL;
568#ifdef WITH_NEXT_FRAMEWORK
569 if (environ == NULL)
570 environ = *_NSGetEnviron();
571#endif
572#ifdef MS_WINDOWS
573 /* _wenviron must be initialized in this way if the program is started
574 through main() instead of wmain(). */
575 _wgetenv(L"");
576 if (_wenviron == NULL)
577 return d;
578 /* This part ignores errors */
579 for (e = _wenviron; *e != NULL; e++) {
580 PyObject *k;
581 PyObject *v;
582 wchar_t *p = wcschr(*e, L'=');
583 if (p == NULL)
584 continue;
585 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
586 if (k == NULL) {
587 PyErr_Clear();
588 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
591 if (v == NULL) {
592 PyErr_Clear();
593 Py_DECREF(k);
594 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000595 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000596 if (PyDict_GetItem(d, k) == NULL) {
597 if (PyDict_SetItem(d, k, v) != 0)
598 PyErr_Clear();
599 }
600 Py_DECREF(k);
601 Py_DECREF(v);
602 }
603#else
604 if (environ == NULL)
605 return d;
606 /* This part ignores errors */
607 for (e = environ; *e != NULL; e++) {
608 PyObject *k;
609 PyObject *v;
610 char *p = strchr(*e, '=');
611 if (p == NULL)
612 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000613 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 if (k == NULL) {
615 PyErr_Clear();
616 continue;
617 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000618 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000619 if (v == NULL) {
620 PyErr_Clear();
621 Py_DECREF(k);
622 continue;
623 }
624 if (PyDict_GetItem(d, k) == NULL) {
625 if (PyDict_SetItem(d, k, v) != 0)
626 PyErr_Clear();
627 }
628 Py_DECREF(k);
629 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000630 }
631#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000632#if defined(PYOS_OS2)
633 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
634 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
635 PyObject *v = PyBytes_FromString(buffer);
636 PyDict_SetItemString(d, "BEGINLIBPATH", v);
637 Py_DECREF(v);
638 }
639 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
640 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
641 PyObject *v = PyBytes_FromString(buffer);
642 PyDict_SetItemString(d, "ENDLIBPATH", v);
643 Py_DECREF(v);
644 }
645#endif
646 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647}
648
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649/* Set a POSIX-specific error from errno, and return NULL */
650
Barry Warsawd58d7641998-07-23 16:14:40 +0000651static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000652posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000653{
Victor Stinner8c62be82010-05-06 00:08:46 +0000654 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000655}
Barry Warsawd58d7641998-07-23 16:14:40 +0000656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000657posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000658{
Victor Stinner8c62be82010-05-06 00:08:46 +0000659 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000660}
661
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000662
Mark Hammondef8b6542001-05-13 08:04:26 +0000663static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000664posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000665{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000666 PyObject *name_str, *rc;
667 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
668 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000669 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000670 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
671 name_str);
672 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000673 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000674}
675
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000676#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000677static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000678win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000679{
Victor Stinner8c62be82010-05-06 00:08:46 +0000680 /* XXX We should pass the function name along in the future.
681 (winreg.c also wants to pass the function name.)
682 This would however require an additional param to the
683 Windows error object, which is non-trivial.
684 */
685 errno = GetLastError();
686 if (filename)
687 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
688 else
689 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000690}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000691
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000692static PyObject *
693win32_error_unicode(char* function, Py_UNICODE* filename)
694{
Victor Stinner8c62be82010-05-06 00:08:46 +0000695 /* XXX - see win32_error for comments on 'function' */
696 errno = GetLastError();
697 if (filename)
698 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
699 else
700 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000701}
702
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000704convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705{
Victor Stinner8c62be82010-05-06 00:08:46 +0000706 if (PyUnicode_CheckExact(*param))
707 Py_INCREF(*param);
708 else if (PyUnicode_Check(*param))
709 /* For a Unicode subtype that's not a Unicode object,
710 return a true Unicode object with the same data. */
711 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
712 PyUnicode_GET_SIZE(*param));
713 else
714 *param = PyUnicode_FromEncodedObject(*param,
715 Py_FileSystemDefaultEncoding,
716 "strict");
717 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000718}
719
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000720#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721
Guido van Rossumd48f2521997-12-05 22:19:34 +0000722#if defined(PYOS_OS2)
723/**********************************************************************
724 * Helper Function to Trim and Format OS/2 Messages
725 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000726static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000727os2_formatmsg(char *msgbuf, int msglen, char *reason)
728{
729 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
730
731 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
732 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
733
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000734 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000735 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
736 }
737
738 /* Add Optional Reason Text */
739 if (reason) {
740 strcat(msgbuf, " : ");
741 strcat(msgbuf, reason);
742 }
743}
744
745/**********************************************************************
746 * Decode an OS/2 Operating System Error Code
747 *
748 * A convenience function to lookup an OS/2 error code and return a
749 * text message we can use to raise a Python exception.
750 *
751 * Notes:
752 * The messages for errors returned from the OS/2 kernel reside in
753 * the file OSO001.MSG in the \OS2 directory hierarchy.
754 *
755 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000756static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000757os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
758{
759 APIRET rc;
760 ULONG msglen;
761
762 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
763 Py_BEGIN_ALLOW_THREADS
764 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
765 errorcode, "oso001.msg", &msglen);
766 Py_END_ALLOW_THREADS
767
768 if (rc == NO_ERROR)
769 os2_formatmsg(msgbuf, msglen, reason);
770 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000771 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000772 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000773
774 return msgbuf;
775}
776
777/* Set an OS/2-specific error and return NULL. OS/2 kernel
778 errors are not in a global variable e.g. 'errno' nor are
779 they congruent with posix error numbers. */
780
Victor Stinner8c62be82010-05-06 00:08:46 +0000781static PyObject *
782os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000783{
784 char text[1024];
785 PyObject *v;
786
787 os2_strerror(text, sizeof(text), code, "");
788
789 v = Py_BuildValue("(is)", code, text);
790 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000791 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000792 Py_DECREF(v);
793 }
794 return NULL; /* Signal to Python that an Exception is Pending */
795}
796
797#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798
799/* POSIX generic methods */
800
Barry Warsaw53699e91996-12-10 23:23:01 +0000801static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000802posix_fildes(PyObject *fdobj, int (*func)(int))
803{
Victor Stinner8c62be82010-05-06 00:08:46 +0000804 int fd;
805 int res;
806 fd = PyObject_AsFileDescriptor(fdobj);
807 if (fd < 0)
808 return NULL;
809 if (!_PyVerify_fd(fd))
810 return posix_error();
811 Py_BEGIN_ALLOW_THREADS
812 res = (*func)(fd);
813 Py_END_ALLOW_THREADS
814 if (res < 0)
815 return posix_error();
816 Py_INCREF(Py_None);
817 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000818}
Guido van Rossum21142a01999-01-08 21:05:37 +0000819
820static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822{
Victor Stinner8c62be82010-05-06 00:08:46 +0000823 PyObject *opath1 = NULL;
824 char *path1;
825 int res;
826 if (!PyArg_ParseTuple(args, format,
827 PyUnicode_FSConverter, &opath1))
828 return NULL;
829 path1 = PyBytes_AsString(opath1);
830 Py_BEGIN_ALLOW_THREADS
831 res = (*func)(path1);
832 Py_END_ALLOW_THREADS
833 if (res < 0)
834 return posix_error_with_allocated_filename(opath1);
835 Py_DECREF(opath1);
836 Py_INCREF(Py_None);
837 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838}
839
Barry Warsaw53699e91996-12-10 23:23:01 +0000840static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000841posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000842 char *format,
843 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844{
Victor Stinner8c62be82010-05-06 00:08:46 +0000845 PyObject *opath1 = NULL, *opath2 = NULL;
846 char *path1, *path2;
847 int res;
848 if (!PyArg_ParseTuple(args, format,
849 PyUnicode_FSConverter, &opath1,
850 PyUnicode_FSConverter, &opath2)) {
851 return NULL;
852 }
853 path1 = PyBytes_AsString(opath1);
854 path2 = PyBytes_AsString(opath2);
855 Py_BEGIN_ALLOW_THREADS
856 res = (*func)(path1, path2);
857 Py_END_ALLOW_THREADS
858 Py_DECREF(opath1);
859 Py_DECREF(opath2);
860 if (res != 0)
861 /* XXX how to report both path1 and path2??? */
862 return posix_error();
863 Py_INCREF(Py_None);
864 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000867#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000869win32_1str(PyObject* args, char* func,
870 char* format, BOOL (__stdcall *funcA)(LPCSTR),
871 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872{
Victor Stinner8c62be82010-05-06 00:08:46 +0000873 PyObject *uni;
874 char *ansi;
875 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000876
Victor Stinner8c62be82010-05-06 00:08:46 +0000877 if (!PyArg_ParseTuple(args, wformat, &uni))
878 PyErr_Clear();
879 else {
880 Py_BEGIN_ALLOW_THREADS
881 result = funcW(PyUnicode_AsUnicode(uni));
882 Py_END_ALLOW_THREADS
883 if (!result)
884 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
885 Py_INCREF(Py_None);
886 return Py_None;
887 }
888 if (!PyArg_ParseTuple(args, format, &ansi))
889 return NULL;
890 Py_BEGIN_ALLOW_THREADS
891 result = funcA(ansi);
892 Py_END_ALLOW_THREADS
893 if (!result)
894 return win32_error(func, ansi);
895 Py_INCREF(Py_None);
896 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897
898}
899
900/* This is a reimplementation of the C library's chdir function,
901 but one that produces Win32 errors instead of DOS error codes.
902 chdir is essentially a wrapper around SetCurrentDirectory; however,
903 it also needs to set "magic" environment variables indicating
904 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000905static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906win32_chdir(LPCSTR path)
907{
Victor Stinner8c62be82010-05-06 00:08:46 +0000908 char new_path[MAX_PATH+1];
909 int result;
910 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911
Victor Stinner8c62be82010-05-06 00:08:46 +0000912 if(!SetCurrentDirectoryA(path))
913 return FALSE;
914 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
915 if (!result)
916 return FALSE;
917 /* In the ANSI API, there should not be any paths longer
918 than MAX_PATH. */
919 assert(result <= MAX_PATH+1);
920 if (strncmp(new_path, "\\\\", 2) == 0 ||
921 strncmp(new_path, "//", 2) == 0)
922 /* UNC path, nothing to do. */
923 return TRUE;
924 env[1] = new_path[0];
925 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926}
927
928/* The Unicode version differs from the ANSI version
929 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000930static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931win32_wchdir(LPCWSTR path)
932{
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
934 int result;
935 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936
Victor Stinner8c62be82010-05-06 00:08:46 +0000937 if(!SetCurrentDirectoryW(path))
938 return FALSE;
939 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
940 if (!result)
941 return FALSE;
942 if (result > MAX_PATH+1) {
943 new_path = malloc(result * sizeof(wchar_t));
944 if (!new_path) {
945 SetLastError(ERROR_OUTOFMEMORY);
946 return FALSE;
947 }
948 result = GetCurrentDirectoryW(result, new_path);
949 if (!result) {
950 free(new_path);
951 return FALSE;
952 }
953 }
954 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
955 wcsncmp(new_path, L"//", 2) == 0)
956 /* UNC path, nothing to do. */
957 return TRUE;
958 env[1] = new_path[0];
959 result = SetEnvironmentVariableW(env, new_path);
960 if (new_path != _new_path)
961 free(new_path);
962 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963}
964#endif
965
Martin v. Löwis14694662006-02-03 12:54:16 +0000966#ifdef MS_WINDOWS
967/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
968 - time stamps are restricted to second resolution
969 - file modification times suffer from forth-and-back conversions between
970 UTC and local time
971 Therefore, we implement our own stat, based on the Win32 API directly.
972*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000973#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000974
975struct win32_stat{
976 int st_dev;
977 __int64 st_ino;
978 unsigned short st_mode;
979 int st_nlink;
980 int st_uid;
981 int st_gid;
982 int st_rdev;
983 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000984 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000985 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000986 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000987 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000988 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000989 int st_ctime_nsec;
990};
991
992static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
993
994static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000995FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000996{
Victor Stinner8c62be82010-05-06 00:08:46 +0000997 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
998 /* Cannot simply cast and dereference in_ptr,
999 since it might not be aligned properly */
1000 __int64 in;
1001 memcpy(&in, in_ptr, sizeof(in));
1002 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001003 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001004}
1005
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001007time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008{
Victor Stinner8c62be82010-05-06 00:08:46 +00001009 /* XXX endianness */
1010 __int64 out;
1011 out = time_in + secs_between_epochs;
1012 out = out * 10000000 + nsec_in / 100;
1013 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001014}
1015
Martin v. Löwis14694662006-02-03 12:54:16 +00001016/* Below, we *know* that ugo+r is 0444 */
1017#if _S_IREAD != 0400
1018#error Unsupported C library
1019#endif
1020static int
1021attributes_to_mode(DWORD attr)
1022{
Victor Stinner8c62be82010-05-06 00:08:46 +00001023 int m = 0;
1024 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1025 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1026 else
1027 m |= _S_IFREG;
1028 if (attr & FILE_ATTRIBUTE_READONLY)
1029 m |= 0444;
1030 else
1031 m |= 0666;
1032 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001033}
1034
1035static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001036attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001037{
Victor Stinner8c62be82010-05-06 00:08:46 +00001038 memset(result, 0, sizeof(*result));
1039 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1040 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1041 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1042 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1043 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001044 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001045 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001046 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1047 /* first clear the S_IFMT bits */
1048 result->st_mode ^= (result->st_mode & 0170000);
1049 /* now set the bits that make this a symlink */
1050 result->st_mode |= 0120000;
1051 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001052
Victor Stinner8c62be82010-05-06 00:08:46 +00001053 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001054}
1055
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001057attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001058{
Victor Stinner8c62be82010-05-06 00:08:46 +00001059 HANDLE hFindFile;
1060 WIN32_FIND_DATAA FileData;
1061 hFindFile = FindFirstFileA(pszFile, &FileData);
1062 if (hFindFile == INVALID_HANDLE_VALUE)
1063 return FALSE;
1064 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001065 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001066 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001067 info->dwFileAttributes = FileData.dwFileAttributes;
1068 info->ftCreationTime = FileData.ftCreationTime;
1069 info->ftLastAccessTime = FileData.ftLastAccessTime;
1070 info->ftLastWriteTime = FileData.ftLastWriteTime;
1071 info->nFileSizeHigh = FileData.nFileSizeHigh;
1072 info->nFileSizeLow = FileData.nFileSizeLow;
1073/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001074 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1075 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001076 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077}
1078
1079static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001080attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081{
Victor Stinner8c62be82010-05-06 00:08:46 +00001082 HANDLE hFindFile;
1083 WIN32_FIND_DATAW FileData;
1084 hFindFile = FindFirstFileW(pszFile, &FileData);
1085 if (hFindFile == INVALID_HANDLE_VALUE)
1086 return FALSE;
1087 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001088 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001089 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001090 info->dwFileAttributes = FileData.dwFileAttributes;
1091 info->ftCreationTime = FileData.ftCreationTime;
1092 info->ftLastAccessTime = FileData.ftLastAccessTime;
1093 info->ftLastWriteTime = FileData.ftLastWriteTime;
1094 info->nFileSizeHigh = FileData.nFileSizeHigh;
1095 info->nFileSizeLow = FileData.nFileSizeLow;
1096/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001097 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1098 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001099 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001100}
1101
Brian Curtind25aef52011-06-13 15:16:04 -05001102/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1103static int has_GetFinalPathNameByHandle = 0;
1104static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1105 DWORD);
1106static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1107 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001108static int
Brian Curtind25aef52011-06-13 15:16:04 -05001109check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001110{
Brian Curtind25aef52011-06-13 15:16:04 -05001111 HINSTANCE hKernel32;
1112 /* only recheck */
1113 if (!has_GetFinalPathNameByHandle)
1114 {
1115 hKernel32 = GetModuleHandle("KERNEL32");
1116 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1117 "GetFinalPathNameByHandleA");
1118 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1119 "GetFinalPathNameByHandleW");
1120 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1121 Py_GetFinalPathNameByHandleW;
1122 }
1123 return has_GetFinalPathNameByHandle;
1124}
1125
1126static BOOL
1127get_target_path(HANDLE hdl, wchar_t **target_path)
1128{
1129 int buf_size, result_length;
1130 wchar_t *buf;
1131
1132 /* We have a good handle to the target, use it to determine
1133 the target path name (then we'll call lstat on it). */
1134 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1135 VOLUME_NAME_DOS);
1136 if(!buf_size)
1137 return FALSE;
1138
1139 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001140 if (!buf) {
1141 SetLastError(ERROR_OUTOFMEMORY);
1142 return FALSE;
1143 }
1144
Brian Curtind25aef52011-06-13 15:16:04 -05001145 result_length = Py_GetFinalPathNameByHandleW(hdl,
1146 buf, buf_size, VOLUME_NAME_DOS);
1147
1148 if(!result_length) {
1149 free(buf);
1150 return FALSE;
1151 }
1152
1153 if(!CloseHandle(hdl)) {
1154 free(buf);
1155 return FALSE;
1156 }
1157
1158 buf[result_length] = 0;
1159
1160 *target_path = buf;
1161 return TRUE;
1162}
1163
1164static int
1165win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1166 BOOL traverse);
1167static int
1168win32_xstat_impl(const char *path, struct win32_stat *result,
1169 BOOL traverse)
1170{
Victor Stinner26de69d2011-06-17 15:15:38 +02001171 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001172 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001173 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001174 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001175 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176 const char *dot;
1177
Brian Curtind25aef52011-06-13 15:16:04 -05001178 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001179 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1180 traverse reparse point. */
1181 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001182 }
1183
Brian Curtinf5e76d02010-11-24 13:14:05 +00001184 hFile = CreateFileA(
1185 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001186 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001187 0, /* share mode */
1188 NULL, /* security attributes */
1189 OPEN_EXISTING,
1190 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001191 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1192 Because of this, calls like GetFinalPathNameByHandle will return
1193 the symlink path agin and not the actual final path. */
1194 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1195 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001196 NULL);
1197
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001198 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001199 /* Either the target doesn't exist, or we don't have access to
1200 get a handle to it. If the former, we need to return an error.
1201 If the latter, we can use attributes_from_dir. */
1202 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001203 return -1;
1204 /* Could not get attributes on open file. Fall back to
1205 reading the directory. */
1206 if (!attributes_from_dir(path, &info, &reparse_tag))
1207 /* Very strange. This should not fail now */
1208 return -1;
1209 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1210 if (traverse) {
1211 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001212 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001213 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001214 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001215 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001216 } else {
1217 if (!GetFileInformationByHandle(hFile, &info)) {
1218 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001219 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001220 }
1221 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001222 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1223 return -1;
1224
1225 /* Close the outer open file handle now that we're about to
1226 reopen it with different flags. */
1227 if (!CloseHandle(hFile))
1228 return -1;
1229
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001230 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001231 /* In order to call GetFinalPathNameByHandle we need to open
1232 the file without the reparse handling flag set. */
1233 hFile2 = CreateFileA(
1234 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1235 NULL, OPEN_EXISTING,
1236 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1237 NULL);
1238 if (hFile2 == INVALID_HANDLE_VALUE)
1239 return -1;
1240
1241 if (!get_target_path(hFile2, &target_path))
1242 return -1;
1243
1244 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001245 free(target_path);
1246 return code;
1247 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001248 } else
1249 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001250 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001251 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001252
1253 /* Set S_IEXEC if it is an .exe, .bat, ... */
1254 dot = strrchr(path, '.');
1255 if (dot) {
1256 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1257 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1258 result->st_mode |= 0111;
1259 }
1260 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001261}
1262
1263static int
Brian Curtind25aef52011-06-13 15:16:04 -05001264win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1265 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001266{
1267 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001268 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001269 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001270 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001271 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001272 const wchar_t *dot;
1273
Brian Curtind25aef52011-06-13 15:16:04 -05001274 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001275 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1276 traverse reparse point. */
1277 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001278 }
1279
Brian Curtinf5e76d02010-11-24 13:14:05 +00001280 hFile = CreateFileW(
1281 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001282 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001283 0, /* share mode */
1284 NULL, /* security attributes */
1285 OPEN_EXISTING,
1286 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001287 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1288 Because of this, calls like GetFinalPathNameByHandle will return
1289 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001290 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001291 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001292 NULL);
1293
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001294 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001295 /* Either the target doesn't exist, or we don't have access to
1296 get a handle to it. If the former, we need to return an error.
1297 If the latter, we can use attributes_from_dir. */
1298 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001299 return -1;
1300 /* Could not get attributes on open file. Fall back to
1301 reading the directory. */
1302 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1303 /* Very strange. This should not fail now */
1304 return -1;
1305 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1306 if (traverse) {
1307 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001309 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001310 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001311 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001312 } else {
1313 if (!GetFileInformationByHandle(hFile, &info)) {
1314 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001315 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001316 }
1317 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001318 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1319 return -1;
1320
1321 /* Close the outer open file handle now that we're about to
1322 reopen it with different flags. */
1323 if (!CloseHandle(hFile))
1324 return -1;
1325
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001326 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001327 /* In order to call GetFinalPathNameByHandle we need to open
1328 the file without the reparse handling flag set. */
1329 hFile2 = CreateFileW(
1330 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1331 NULL, OPEN_EXISTING,
1332 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1333 NULL);
1334 if (hFile2 == INVALID_HANDLE_VALUE)
1335 return -1;
1336
1337 if (!get_target_path(hFile2, &target_path))
1338 return -1;
1339
1340 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001341 free(target_path);
1342 return code;
1343 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001344 } else
1345 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001346 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001347 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001348
1349 /* Set S_IEXEC if it is an .exe, .bat, ... */
1350 dot = wcsrchr(path, '.');
1351 if (dot) {
1352 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1353 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1354 result->st_mode |= 0111;
1355 }
1356 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001357}
1358
1359static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001360win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001361{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001362 /* Protocol violation: we explicitly clear errno, instead of
1363 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001364 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001365 errno = 0;
1366 return code;
1367}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001368
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369static int
1370win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1371{
1372 /* Protocol violation: we explicitly clear errno, instead of
1373 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001374 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001375 errno = 0;
1376 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001377}
Brian Curtind25aef52011-06-13 15:16:04 -05001378/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001379
1380 In Posix, stat automatically traverses symlinks and returns the stat
1381 structure for the target. In Windows, the equivalent GetFileAttributes by
1382 default does not traverse symlinks and instead returns attributes for
1383 the symlink.
1384
1385 Therefore, win32_lstat will get the attributes traditionally, and
1386 win32_stat will first explicitly resolve the symlink target and then will
1387 call win32_lstat on that result.
1388
Ezio Melotti4969f702011-03-15 05:59:46 +02001389 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001390
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001391static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001392win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001393{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001395}
1396
Victor Stinner8c62be82010-05-06 00:08:46 +00001397static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001398win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001399{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001400 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001401}
1402
1403static int
1404win32_stat(const char* path, struct win32_stat *result)
1405{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001406 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001407}
1408
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001409static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001410win32_stat_w(const wchar_t* path, struct win32_stat *result)
1411{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001412 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001413}
1414
1415static int
1416win32_fstat(int file_number, struct win32_stat *result)
1417{
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 BY_HANDLE_FILE_INFORMATION info;
1419 HANDLE h;
1420 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001423
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 /* Protocol violation: we explicitly clear errno, instead of
1425 setting it to a POSIX error. Callers should use GetLastError. */
1426 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001427
Victor Stinner8c62be82010-05-06 00:08:46 +00001428 if (h == INVALID_HANDLE_VALUE) {
1429 /* This is really a C library error (invalid file handle).
1430 We set the Win32 error to the closes one matching. */
1431 SetLastError(ERROR_INVALID_HANDLE);
1432 return -1;
1433 }
1434 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001435
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 type = GetFileType(h);
1437 if (type == FILE_TYPE_UNKNOWN) {
1438 DWORD error = GetLastError();
1439 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001440 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 }
1442 /* else: valid but unknown file */
1443 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001444
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 if (type != FILE_TYPE_DISK) {
1446 if (type == FILE_TYPE_CHAR)
1447 result->st_mode = _S_IFCHR;
1448 else if (type == FILE_TYPE_PIPE)
1449 result->st_mode = _S_IFIFO;
1450 return 0;
1451 }
1452
1453 if (!GetFileInformationByHandle(h, &info)) {
1454 return -1;
1455 }
1456
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001457 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1460 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001461}
1462
1463#endif /* MS_WINDOWS */
1464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001465PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001466"stat_result: Result from stat or lstat.\n\n\
1467This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001468 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001469or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1470\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001471Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1472or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001473\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475
1476static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001477 {"st_mode", "protection bits"},
1478 {"st_ino", "inode"},
1479 {"st_dev", "device"},
1480 {"st_nlink", "number of hard links"},
1481 {"st_uid", "user ID of owner"},
1482 {"st_gid", "group ID of owner"},
1483 {"st_size", "total size, in bytes"},
1484 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1485 {NULL, "integer time of last access"},
1486 {NULL, "integer time of last modification"},
1487 {NULL, "integer time of last change"},
1488 {"st_atime", "time of last access"},
1489 {"st_mtime", "time of last modification"},
1490 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001491#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001493#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001494#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001495 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001496#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001497#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001498 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001499#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001500#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001502#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001503#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001504 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001505#endif
1506#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001508#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001510};
1511
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001512#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001513#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001514#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001515#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001516#endif
1517
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001518#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001519#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1520#else
1521#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1522#endif
1523
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001524#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001525#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1526#else
1527#define ST_RDEV_IDX ST_BLOCKS_IDX
1528#endif
1529
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001530#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1531#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1532#else
1533#define ST_FLAGS_IDX ST_RDEV_IDX
1534#endif
1535
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001536#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001537#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001538#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001539#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001540#endif
1541
1542#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1543#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1544#else
1545#define ST_BIRTHTIME_IDX ST_GEN_IDX
1546#endif
1547
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001548static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001549 "stat_result", /* name */
1550 stat_result__doc__, /* doc */
1551 stat_result_fields,
1552 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001553};
1554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001555PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001556"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1557This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001558 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001559or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001560\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001562
1563static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 {"f_bsize", },
1565 {"f_frsize", },
1566 {"f_blocks", },
1567 {"f_bfree", },
1568 {"f_bavail", },
1569 {"f_files", },
1570 {"f_ffree", },
1571 {"f_favail", },
1572 {"f_flag", },
1573 {"f_namemax",},
1574 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001575};
1576
1577static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 "statvfs_result", /* name */
1579 statvfs_result__doc__, /* doc */
1580 statvfs_result_fields,
1581 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001582};
1583
Ross Lagerwall7807c352011-03-17 20:20:30 +02001584#if defined(HAVE_WAITID) && !defined(__APPLE__)
1585PyDoc_STRVAR(waitid_result__doc__,
1586"waitid_result: Result from waitid.\n\n\
1587This object may be accessed either as a tuple of\n\
1588 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1589or via the attributes si_pid, si_uid, and so on.\n\
1590\n\
1591See os.waitid for more information.");
1592
1593static PyStructSequence_Field waitid_result_fields[] = {
1594 {"si_pid", },
1595 {"si_uid", },
1596 {"si_signo", },
1597 {"si_status", },
1598 {"si_code", },
1599 {0}
1600};
1601
1602static PyStructSequence_Desc waitid_result_desc = {
1603 "waitid_result", /* name */
1604 waitid_result__doc__, /* doc */
1605 waitid_result_fields,
1606 5
1607};
1608static PyTypeObject WaitidResultType;
1609#endif
1610
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001611static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001612static PyTypeObject StatResultType;
1613static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001614#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001615static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001616#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001617static newfunc structseq_new;
1618
1619static PyObject *
1620statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1621{
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 PyStructSequence *result;
1623 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001624
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 result = (PyStructSequence*)structseq_new(type, args, kwds);
1626 if (!result)
1627 return NULL;
1628 /* If we have been initialized from a tuple,
1629 st_?time might be set to None. Initialize it
1630 from the int slots. */
1631 for (i = 7; i <= 9; i++) {
1632 if (result->ob_item[i+3] == Py_None) {
1633 Py_DECREF(Py_None);
1634 Py_INCREF(result->ob_item[i]);
1635 result->ob_item[i+3] = result->ob_item[i];
1636 }
1637 }
1638 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001639}
1640
1641
1642
1643/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001644static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001645
1646PyDoc_STRVAR(stat_float_times__doc__,
1647"stat_float_times([newval]) -> oldval\n\n\
1648Determine whether os.[lf]stat represents time stamps as float objects.\n\
1649If newval is True, future calls to stat() return floats, if it is False,\n\
1650future calls return ints. \n\
1651If newval is omitted, return the current setting.\n");
1652
1653static PyObject*
1654stat_float_times(PyObject* self, PyObject *args)
1655{
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 int newval = -1;
1657 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1658 return NULL;
1659 if (newval == -1)
1660 /* Return old value */
1661 return PyBool_FromLong(_stat_float_times);
1662 _stat_float_times = newval;
1663 Py_INCREF(Py_None);
1664 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001665}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001666
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001667static void
1668fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1669{
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001671#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001673#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001674 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001675#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001676 if (!ival)
1677 return;
1678 if (_stat_float_times) {
1679 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1680 } else {
1681 fval = ival;
1682 Py_INCREF(fval);
1683 }
1684 PyStructSequence_SET_ITEM(v, index, ival);
1685 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001686}
1687
Tim Peters5aa91602002-01-30 05:46:57 +00001688/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001689 (used by posix_stat() and posix_fstat()) */
1690static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001691_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001692{
Victor Stinner8c62be82010-05-06 00:08:46 +00001693 unsigned long ansec, mnsec, cnsec;
1694 PyObject *v = PyStructSequence_New(&StatResultType);
1695 if (v == NULL)
1696 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001697
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001699#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 PyStructSequence_SET_ITEM(v, 1,
1701 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001702#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001703 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001704#endif
1705#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 PyStructSequence_SET_ITEM(v, 2,
1707 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001708#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001710#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1712 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1713 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001714#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 PyStructSequence_SET_ITEM(v, 6,
1716 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001717#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001719#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001720
Martin v. Löwis14694662006-02-03 12:54:16 +00001721#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001722 ansec = st->st_atim.tv_nsec;
1723 mnsec = st->st_mtim.tv_nsec;
1724 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001725#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 ansec = st->st_atimespec.tv_nsec;
1727 mnsec = st->st_mtimespec.tv_nsec;
1728 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001729#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ansec = st->st_atime_nsec;
1731 mnsec = st->st_mtime_nsec;
1732 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001733#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001734 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001735#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 fill_time(v, 7, st->st_atime, ansec);
1737 fill_time(v, 8, st->st_mtime, mnsec);
1738 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001739
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001740#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1742 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001743#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001744#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1746 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001747#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001748#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1750 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001751#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001752#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1754 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001755#endif
1756#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001757 {
1758 PyObject *val;
1759 unsigned long bsec,bnsec;
1760 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001761#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001763#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001765#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001766 if (_stat_float_times) {
1767 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1768 } else {
1769 val = PyLong_FromLong((long)bsec);
1770 }
1771 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1772 val);
1773 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001774#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001775#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1777 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001778#endif
Fred Drake699f3522000-06-29 21:12:41 +00001779
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 if (PyErr_Occurred()) {
1781 Py_DECREF(v);
1782 return NULL;
1783 }
Fred Drake699f3522000-06-29 21:12:41 +00001784
Victor Stinner8c62be82010-05-06 00:08:46 +00001785 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001786}
1787
Barry Warsaw53699e91996-12-10 23:23:01 +00001788static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001789posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001793#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001795#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 char *wformat,
1797 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001798{
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 STRUCT_STAT st;
1800 PyObject *opath;
1801 char *path;
1802 int res;
1803 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001804
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001805#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 PyUnicodeObject *po;
1807 if (PyArg_ParseTuple(args, wformat, &po)) {
1808 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001809
Victor Stinner8c62be82010-05-06 00:08:46 +00001810 Py_BEGIN_ALLOW_THREADS
1811 /* PyUnicode_AS_UNICODE result OK without
1812 thread lock as it is a simple dereference. */
1813 res = wstatfunc(wpath, &st);
1814 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001815
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 if (res != 0)
1817 return win32_error_unicode("stat", wpath);
1818 return _pystat_fromstructstat(&st);
1819 }
1820 /* Drop the argument parsing error as narrow strings
1821 are also valid. */
1822 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001823#endif
1824
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 if (!PyArg_ParseTuple(args, format,
1826 PyUnicode_FSConverter, &opath))
1827 return NULL;
1828 path = PyBytes_AsString(opath);
1829 Py_BEGIN_ALLOW_THREADS
1830 res = (*statfunc)(path, &st);
1831 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001832
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001834#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001836#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001838#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 }
1840 else
1841 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001842
Victor Stinner8c62be82010-05-06 00:08:46 +00001843 Py_DECREF(opath);
1844 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845}
1846
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001847/* POSIX methods */
1848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001850"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001851Use the real uid/gid to test for access to a path. Note that most\n\
1852operations will use the effective uid/gid, therefore this routine can\n\
1853be used in a suid/sgid environment to test if the invoking user has the\n\
1854specified access to the path. The mode argument can be F_OK to test\n\
1855existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001856
1857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001858posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001859{
Victor Stinner8c62be82010-05-06 00:08:46 +00001860 PyObject *opath;
1861 char *path;
1862 int mode;
1863
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001864#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 DWORD attr;
1866 PyUnicodeObject *po;
1867 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1868 Py_BEGIN_ALLOW_THREADS
1869 /* PyUnicode_AS_UNICODE OK without thread lock as
1870 it is a simple dereference. */
1871 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1872 Py_END_ALLOW_THREADS
1873 goto finish;
1874 }
1875 /* Drop the argument parsing error as narrow strings
1876 are also valid. */
1877 PyErr_Clear();
1878 if (!PyArg_ParseTuple(args, "O&i:access",
1879 PyUnicode_FSConverter, &opath, &mode))
1880 return NULL;
1881 path = PyBytes_AsString(opath);
1882 Py_BEGIN_ALLOW_THREADS
1883 attr = GetFileAttributesA(path);
1884 Py_END_ALLOW_THREADS
1885 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001886finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001887 if (attr == 0xFFFFFFFF)
1888 /* File does not exist, or cannot read attributes */
1889 return PyBool_FromLong(0);
1890 /* Access is possible if either write access wasn't requested, or
1891 the file isn't read-only, or if it's a directory, as there are
1892 no read-only directories on Windows. */
1893 return PyBool_FromLong(!(mode & 2)
1894 || !(attr & FILE_ATTRIBUTE_READONLY)
1895 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001897 int res;
1898 if (!PyArg_ParseTuple(args, "O&i:access",
1899 PyUnicode_FSConverter, &opath, &mode))
1900 return NULL;
1901 path = PyBytes_AsString(opath);
1902 Py_BEGIN_ALLOW_THREADS
1903 res = access(path, mode);
1904 Py_END_ALLOW_THREADS
1905 Py_DECREF(opath);
1906 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001907#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001908}
1909
Guido van Rossumd371ff11999-01-25 16:12:23 +00001910#ifndef F_OK
1911#define F_OK 0
1912#endif
1913#ifndef R_OK
1914#define R_OK 4
1915#endif
1916#ifndef W_OK
1917#define W_OK 2
1918#endif
1919#ifndef X_OK
1920#define X_OK 1
1921#endif
1922
1923#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001926Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001927
1928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001929posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001930{
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 int id;
1932 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001933
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1935 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001936
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001937#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 /* file descriptor 0 only, the default input device (stdin) */
1939 if (id == 0) {
1940 ret = ttyname();
1941 }
1942 else {
1943 ret = NULL;
1944 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001945#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001947#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001948 if (ret == NULL)
1949 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001950 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001951}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001952#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001953
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001954#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001956"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001958
1959static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001960posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001961{
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 char *ret;
1963 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001964
Greg Wardb48bc172000-03-01 21:51:56 +00001965#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001967#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001968 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001969#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001970 if (ret == NULL)
1971 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001972 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001973}
1974#endif
1975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001976PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001977"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001978Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979
Barry Warsaw53699e91996-12-10 23:23:01 +00001980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001981posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001982{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001983#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001984 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001985#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001987#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001988 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001989#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001990 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001991#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001992}
1993
Fred Drake4d1e64b2002-04-15 19:40:07 +00001994#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001996"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001997Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001999
2000static PyObject *
2001posix_fchdir(PyObject *self, PyObject *fdobj)
2002{
Victor Stinner8c62be82010-05-06 00:08:46 +00002003 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002004}
2005#endif /* HAVE_FCHDIR */
2006
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002008PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002009"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002010Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002011
Barry Warsaw53699e91996-12-10 23:23:01 +00002012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002013posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002014{
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 PyObject *opath = NULL;
2016 char *path = NULL;
2017 int i;
2018 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002019#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002020 DWORD attr;
2021 PyUnicodeObject *po;
2022 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2023 Py_BEGIN_ALLOW_THREADS
2024 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2025 if (attr != 0xFFFFFFFF) {
2026 if (i & _S_IWRITE)
2027 attr &= ~FILE_ATTRIBUTE_READONLY;
2028 else
2029 attr |= FILE_ATTRIBUTE_READONLY;
2030 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2031 }
2032 else
2033 res = 0;
2034 Py_END_ALLOW_THREADS
2035 if (!res)
2036 return win32_error_unicode("chmod",
2037 PyUnicode_AS_UNICODE(po));
2038 Py_INCREF(Py_None);
2039 return Py_None;
2040 }
2041 /* Drop the argument parsing error as narrow strings
2042 are also valid. */
2043 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002044
Victor Stinner8c62be82010-05-06 00:08:46 +00002045 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2046 &opath, &i))
2047 return NULL;
2048 path = PyBytes_AsString(opath);
2049 Py_BEGIN_ALLOW_THREADS
2050 attr = GetFileAttributesA(path);
2051 if (attr != 0xFFFFFFFF) {
2052 if (i & _S_IWRITE)
2053 attr &= ~FILE_ATTRIBUTE_READONLY;
2054 else
2055 attr |= FILE_ATTRIBUTE_READONLY;
2056 res = SetFileAttributesA(path, attr);
2057 }
2058 else
2059 res = 0;
2060 Py_END_ALLOW_THREADS
2061 if (!res) {
2062 win32_error("chmod", path);
2063 Py_DECREF(opath);
2064 return NULL;
2065 }
2066 Py_DECREF(opath);
2067 Py_INCREF(Py_None);
2068 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002069#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002070 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2071 &opath, &i))
2072 return NULL;
2073 path = PyBytes_AsString(opath);
2074 Py_BEGIN_ALLOW_THREADS
2075 res = chmod(path, i);
2076 Py_END_ALLOW_THREADS
2077 if (res < 0)
2078 return posix_error_with_allocated_filename(opath);
2079 Py_DECREF(opath);
2080 Py_INCREF(Py_None);
2081 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002082#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002083}
2084
Christian Heimes4e30a842007-11-30 22:12:06 +00002085#ifdef HAVE_FCHMOD
2086PyDoc_STRVAR(posix_fchmod__doc__,
2087"fchmod(fd, mode)\n\n\
2088Change the access permissions of the file given by file\n\
2089descriptor fd.");
2090
2091static PyObject *
2092posix_fchmod(PyObject *self, PyObject *args)
2093{
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 int fd, mode, res;
2095 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2096 return NULL;
2097 Py_BEGIN_ALLOW_THREADS
2098 res = fchmod(fd, mode);
2099 Py_END_ALLOW_THREADS
2100 if (res < 0)
2101 return posix_error();
2102 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002103}
2104#endif /* HAVE_FCHMOD */
2105
2106#ifdef HAVE_LCHMOD
2107PyDoc_STRVAR(posix_lchmod__doc__,
2108"lchmod(path, mode)\n\n\
2109Change the access permissions of a file. If path is a symlink, this\n\
2110affects the link itself rather than the target.");
2111
2112static PyObject *
2113posix_lchmod(PyObject *self, PyObject *args)
2114{
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 PyObject *opath;
2116 char *path;
2117 int i;
2118 int res;
2119 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2120 &opath, &i))
2121 return NULL;
2122 path = PyBytes_AsString(opath);
2123 Py_BEGIN_ALLOW_THREADS
2124 res = lchmod(path, i);
2125 Py_END_ALLOW_THREADS
2126 if (res < 0)
2127 return posix_error_with_allocated_filename(opath);
2128 Py_DECREF(opath);
2129 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002130}
2131#endif /* HAVE_LCHMOD */
2132
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002133
Thomas Wouterscf297e42007-02-23 15:07:44 +00002134#ifdef HAVE_CHFLAGS
2135PyDoc_STRVAR(posix_chflags__doc__,
2136"chflags(path, flags)\n\n\
2137Set file flags.");
2138
2139static PyObject *
2140posix_chflags(PyObject *self, PyObject *args)
2141{
Victor Stinner8c62be82010-05-06 00:08:46 +00002142 PyObject *opath;
2143 char *path;
2144 unsigned long flags;
2145 int res;
2146 if (!PyArg_ParseTuple(args, "O&k:chflags",
2147 PyUnicode_FSConverter, &opath, &flags))
2148 return NULL;
2149 path = PyBytes_AsString(opath);
2150 Py_BEGIN_ALLOW_THREADS
2151 res = chflags(path, flags);
2152 Py_END_ALLOW_THREADS
2153 if (res < 0)
2154 return posix_error_with_allocated_filename(opath);
2155 Py_DECREF(opath);
2156 Py_INCREF(Py_None);
2157 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002158}
2159#endif /* HAVE_CHFLAGS */
2160
2161#ifdef HAVE_LCHFLAGS
2162PyDoc_STRVAR(posix_lchflags__doc__,
2163"lchflags(path, flags)\n\n\
2164Set file flags.\n\
2165This function will not follow symbolic links.");
2166
2167static PyObject *
2168posix_lchflags(PyObject *self, PyObject *args)
2169{
Victor Stinner8c62be82010-05-06 00:08:46 +00002170 PyObject *opath;
2171 char *path;
2172 unsigned long flags;
2173 int res;
2174 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2175 PyUnicode_FSConverter, &opath, &flags))
2176 return NULL;
2177 path = PyBytes_AsString(opath);
2178 Py_BEGIN_ALLOW_THREADS
2179 res = lchflags(path, flags);
2180 Py_END_ALLOW_THREADS
2181 if (res < 0)
2182 return posix_error_with_allocated_filename(opath);
2183 Py_DECREF(opath);
2184 Py_INCREF(Py_None);
2185 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002186}
2187#endif /* HAVE_LCHFLAGS */
2188
Martin v. Löwis244edc82001-10-04 22:44:26 +00002189#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002190PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002191"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002192Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002193
2194static PyObject *
2195posix_chroot(PyObject *self, PyObject *args)
2196{
Victor Stinner8c62be82010-05-06 00:08:46 +00002197 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002198}
2199#endif
2200
Guido van Rossum21142a01999-01-08 21:05:37 +00002201#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002202PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002203"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002204force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002205
2206static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002207posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002208{
Stefan Krah0e803b32010-11-26 16:16:47 +00002209 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002210}
2211#endif /* HAVE_FSYNC */
2212
Ross Lagerwall7807c352011-03-17 20:20:30 +02002213#ifdef HAVE_SYNC
2214PyDoc_STRVAR(posix_sync__doc__,
2215"sync()\n\n\
2216Force write of everything to disk.");
2217
2218static PyObject *
2219posix_sync(PyObject *self, PyObject *noargs)
2220{
2221 Py_BEGIN_ALLOW_THREADS
2222 sync();
2223 Py_END_ALLOW_THREADS
2224 Py_RETURN_NONE;
2225}
2226#endif
2227
Guido van Rossum21142a01999-01-08 21:05:37 +00002228#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002229
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002230#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002231extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2232#endif
2233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002234PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002235"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002236force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002237 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002238
2239static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002240posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002241{
Stefan Krah0e803b32010-11-26 16:16:47 +00002242 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002243}
2244#endif /* HAVE_FDATASYNC */
2245
2246
Fredrik Lundh10723342000-07-10 16:38:09 +00002247#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002248PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002249"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002250Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002251
Barry Warsaw53699e91996-12-10 23:23:01 +00002252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002253posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002254{
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 PyObject *opath;
2256 char *path;
2257 long uid, gid;
2258 int res;
2259 if (!PyArg_ParseTuple(args, "O&ll:chown",
2260 PyUnicode_FSConverter, &opath,
2261 &uid, &gid))
2262 return NULL;
2263 path = PyBytes_AsString(opath);
2264 Py_BEGIN_ALLOW_THREADS
2265 res = chown(path, (uid_t) uid, (gid_t) gid);
2266 Py_END_ALLOW_THREADS
2267 if (res < 0)
2268 return posix_error_with_allocated_filename(opath);
2269 Py_DECREF(opath);
2270 Py_INCREF(Py_None);
2271 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002273#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002274
Christian Heimes4e30a842007-11-30 22:12:06 +00002275#ifdef HAVE_FCHOWN
2276PyDoc_STRVAR(posix_fchown__doc__,
2277"fchown(fd, uid, gid)\n\n\
2278Change the owner and group id of the file given by file descriptor\n\
2279fd to the numeric uid and gid.");
2280
2281static PyObject *
2282posix_fchown(PyObject *self, PyObject *args)
2283{
Victor Stinner8c62be82010-05-06 00:08:46 +00002284 int fd;
2285 long uid, gid;
2286 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002287 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002288 return NULL;
2289 Py_BEGIN_ALLOW_THREADS
2290 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2291 Py_END_ALLOW_THREADS
2292 if (res < 0)
2293 return posix_error();
2294 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002295}
2296#endif /* HAVE_FCHOWN */
2297
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002298#ifdef HAVE_LCHOWN
2299PyDoc_STRVAR(posix_lchown__doc__,
2300"lchown(path, uid, gid)\n\n\
2301Change the owner and group id of path to the numeric uid and gid.\n\
2302This function will not follow symbolic links.");
2303
2304static PyObject *
2305posix_lchown(PyObject *self, PyObject *args)
2306{
Victor Stinner8c62be82010-05-06 00:08:46 +00002307 PyObject *opath;
2308 char *path;
2309 long uid, gid;
2310 int res;
2311 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2312 PyUnicode_FSConverter, &opath,
2313 &uid, &gid))
2314 return NULL;
2315 path = PyBytes_AsString(opath);
2316 Py_BEGIN_ALLOW_THREADS
2317 res = lchown(path, (uid_t) uid, (gid_t) gid);
2318 Py_END_ALLOW_THREADS
2319 if (res < 0)
2320 return posix_error_with_allocated_filename(opath);
2321 Py_DECREF(opath);
2322 Py_INCREF(Py_None);
2323 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002324}
2325#endif /* HAVE_LCHOWN */
2326
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002327
Guido van Rossum36bc6801995-06-14 22:54:23 +00002328#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002329static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002330posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002331{
Victor Stinner8c62be82010-05-06 00:08:46 +00002332 char buf[1026];
2333 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002334
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002335#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002336 if (!use_bytes) {
2337 wchar_t wbuf[1026];
2338 wchar_t *wbuf2 = wbuf;
2339 PyObject *resobj;
2340 DWORD len;
2341 Py_BEGIN_ALLOW_THREADS
2342 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2343 /* If the buffer is large enough, len does not include the
2344 terminating \0. If the buffer is too small, len includes
2345 the space needed for the terminator. */
2346 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2347 wbuf2 = malloc(len * sizeof(wchar_t));
2348 if (wbuf2)
2349 len = GetCurrentDirectoryW(len, wbuf2);
2350 }
2351 Py_END_ALLOW_THREADS
2352 if (!wbuf2) {
2353 PyErr_NoMemory();
2354 return NULL;
2355 }
2356 if (!len) {
2357 if (wbuf2 != wbuf) free(wbuf2);
2358 return win32_error("getcwdu", NULL);
2359 }
2360 resobj = PyUnicode_FromWideChar(wbuf2, len);
2361 if (wbuf2 != wbuf) free(wbuf2);
2362 return resobj;
2363 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002364#endif
2365
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002367#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002369#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002370 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002371#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002372 Py_END_ALLOW_THREADS
2373 if (res == NULL)
2374 return posix_error();
2375 if (use_bytes)
2376 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002377 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002378}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002379
2380PyDoc_STRVAR(posix_getcwd__doc__,
2381"getcwd() -> path\n\n\
2382Return a unicode string representing the current working directory.");
2383
2384static PyObject *
2385posix_getcwd_unicode(PyObject *self)
2386{
2387 return posix_getcwd(0);
2388}
2389
2390PyDoc_STRVAR(posix_getcwdb__doc__,
2391"getcwdb() -> path\n\n\
2392Return a bytes string representing the current working directory.");
2393
2394static PyObject *
2395posix_getcwd_bytes(PyObject *self)
2396{
2397 return posix_getcwd(1);
2398}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002399#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002400
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002401
Guido van Rossumb6775db1994-08-01 11:34:53 +00002402#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002403PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002404"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002405Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002406
Barry Warsaw53699e91996-12-10 23:23:01 +00002407static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002408posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409{
Victor Stinner8c62be82010-05-06 00:08:46 +00002410 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002411}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002412#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002413
Brian Curtin1b9df392010-11-24 20:24:31 +00002414#ifdef MS_WINDOWS
2415PyDoc_STRVAR(win32_link__doc__,
2416"link(src, dst)\n\n\
2417Create a hard link to a file.");
2418
2419static PyObject *
2420win32_link(PyObject *self, PyObject *args)
2421{
2422 PyObject *osrc, *odst;
2423 char *src, *dst;
2424 BOOL rslt;
2425
Brian Curtinfc889c42010-11-28 23:59:46 +00002426 PyUnicodeObject *usrc, *udst;
2427 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2428 Py_BEGIN_ALLOW_THREADS
2429 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2430 PyUnicode_AS_UNICODE(usrc), NULL);
2431 Py_END_ALLOW_THREADS
2432
2433 if (rslt == 0)
2434 return win32_error("link", NULL);
2435
2436 Py_RETURN_NONE;
2437 }
2438
2439 /* Narrow strings also valid. */
2440 PyErr_Clear();
2441
Brian Curtin1b9df392010-11-24 20:24:31 +00002442 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2443 PyUnicode_FSConverter, &odst))
2444 return NULL;
2445
2446 src = PyBytes_AsString(osrc);
2447 dst = PyBytes_AsString(odst);
2448
2449 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002450 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002451 Py_END_ALLOW_THREADS
2452
Stefan Krah30b341f2010-11-27 11:44:18 +00002453 Py_DECREF(osrc);
2454 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002455 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002456 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002457
2458 Py_RETURN_NONE;
2459}
2460#endif /* MS_WINDOWS */
2461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002464"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002465Return a list containing the names of the entries in the directory.\n\
2466\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002467 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468\n\
2469The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471
Barry Warsaw53699e91996-12-10 23:23:01 +00002472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002473posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002474{
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 /* XXX Should redo this putting the (now four) versions of opendir
2476 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002477#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002478
Victor Stinner8c62be82010-05-06 00:08:46 +00002479 PyObject *d, *v;
2480 HANDLE hFindFile;
2481 BOOL result;
2482 WIN32_FIND_DATA FileData;
2483 PyObject *opath;
2484 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2485 char *bufptr = namebuf;
2486 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002487
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002488 PyObject *po = NULL;
2489 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002490 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002491 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002492
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002493 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002494 po_wchars = L".";
2495 len = 1;
2496 } else {
2497 po_wchars = PyUnicode_AS_UNICODE(po);
2498 len = PyUnicode_GET_SIZE(po);
2499 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002500 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002501 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2502 if (!wnamebuf) {
2503 PyErr_NoMemory();
2504 return NULL;
2505 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002506 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002507 if (len > 0) {
2508 Py_UNICODE wch = wnamebuf[len-1];
2509 if (wch != L'/' && wch != L'\\' && wch != L':')
2510 wnamebuf[len++] = L'\\';
2511 wcscpy(wnamebuf + len, L"*.*");
2512 }
2513 if ((d = PyList_New(0)) == NULL) {
2514 free(wnamebuf);
2515 return NULL;
2516 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002517 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002518 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002519 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002520 if (hFindFile == INVALID_HANDLE_VALUE) {
2521 int error = GetLastError();
2522 if (error == ERROR_FILE_NOT_FOUND) {
2523 free(wnamebuf);
2524 return d;
2525 }
2526 Py_DECREF(d);
2527 win32_error_unicode("FindFirstFileW", wnamebuf);
2528 free(wnamebuf);
2529 return NULL;
2530 }
2531 do {
2532 /* Skip over . and .. */
2533 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2534 wcscmp(wFileData.cFileName, L"..") != 0) {
2535 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2536 if (v == NULL) {
2537 Py_DECREF(d);
2538 d = NULL;
2539 break;
2540 }
2541 if (PyList_Append(d, v) != 0) {
2542 Py_DECREF(v);
2543 Py_DECREF(d);
2544 d = NULL;
2545 break;
2546 }
2547 Py_DECREF(v);
2548 }
2549 Py_BEGIN_ALLOW_THREADS
2550 result = FindNextFileW(hFindFile, &wFileData);
2551 Py_END_ALLOW_THREADS
2552 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2553 it got to the end of the directory. */
2554 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2555 Py_DECREF(d);
2556 win32_error_unicode("FindNextFileW", wnamebuf);
2557 FindClose(hFindFile);
2558 free(wnamebuf);
2559 return NULL;
2560 }
2561 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002562
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 if (FindClose(hFindFile) == FALSE) {
2564 Py_DECREF(d);
2565 win32_error_unicode("FindClose", wnamebuf);
2566 free(wnamebuf);
2567 return NULL;
2568 }
2569 free(wnamebuf);
2570 return d;
2571 }
2572 /* Drop the argument parsing error as narrow strings
2573 are also valid. */
2574 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002575
Victor Stinner8c62be82010-05-06 00:08:46 +00002576 if (!PyArg_ParseTuple(args, "O&:listdir",
2577 PyUnicode_FSConverter, &opath))
2578 return NULL;
2579 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2580 PyErr_SetString(PyExc_ValueError, "path too long");
2581 Py_DECREF(opath);
2582 return NULL;
2583 }
2584 strcpy(namebuf, PyBytes_AsString(opath));
2585 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002586 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002587 if (len > 0) {
2588 char ch = namebuf[len-1];
2589 if (ch != SEP && ch != ALTSEP && ch != ':')
2590 namebuf[len++] = '/';
2591 strcpy(namebuf + len, "*.*");
2592 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002593
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 if ((d = PyList_New(0)) == NULL)
2595 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002596
Antoine Pitroub73caab2010-08-09 23:39:31 +00002597 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002598 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002599 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002600 if (hFindFile == INVALID_HANDLE_VALUE) {
2601 int error = GetLastError();
2602 if (error == ERROR_FILE_NOT_FOUND)
2603 return d;
2604 Py_DECREF(d);
2605 return win32_error("FindFirstFile", namebuf);
2606 }
2607 do {
2608 /* Skip over . and .. */
2609 if (strcmp(FileData.cFileName, ".") != 0 &&
2610 strcmp(FileData.cFileName, "..") != 0) {
2611 v = PyBytes_FromString(FileData.cFileName);
2612 if (v == NULL) {
2613 Py_DECREF(d);
2614 d = NULL;
2615 break;
2616 }
2617 if (PyList_Append(d, v) != 0) {
2618 Py_DECREF(v);
2619 Py_DECREF(d);
2620 d = NULL;
2621 break;
2622 }
2623 Py_DECREF(v);
2624 }
2625 Py_BEGIN_ALLOW_THREADS
2626 result = FindNextFile(hFindFile, &FileData);
2627 Py_END_ALLOW_THREADS
2628 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2629 it got to the end of the directory. */
2630 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2631 Py_DECREF(d);
2632 win32_error("FindNextFile", namebuf);
2633 FindClose(hFindFile);
2634 return NULL;
2635 }
2636 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002637
Victor Stinner8c62be82010-05-06 00:08:46 +00002638 if (FindClose(hFindFile) == FALSE) {
2639 Py_DECREF(d);
2640 return win32_error("FindClose", namebuf);
2641 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002642
Victor Stinner8c62be82010-05-06 00:08:46 +00002643 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002644
Tim Peters0bb44a42000-09-15 07:44:49 +00002645#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002646
2647#ifndef MAX_PATH
2648#define MAX_PATH CCHMAXPATH
2649#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002650 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002651 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002652 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002653 PyObject *d, *v;
2654 char namebuf[MAX_PATH+5];
2655 HDIR hdir = 1;
2656 ULONG srchcnt = 1;
2657 FILEFINDBUF3 ep;
2658 APIRET rc;
2659
Victor Stinner8c62be82010-05-06 00:08:46 +00002660 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002661 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002662 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002663 name = PyBytes_AsString(oname);
2664 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002665 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002666 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002667 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002668 return NULL;
2669 }
2670 strcpy(namebuf, name);
2671 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002672 if (*pt == ALTSEP)
2673 *pt = SEP;
2674 if (namebuf[len-1] != SEP)
2675 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002676 strcpy(namebuf + len, "*.*");
2677
Neal Norwitz6c913782007-10-14 03:23:09 +00002678 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002679 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002680 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002681 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002682
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002683 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2684 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002685 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002686 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2687 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2688 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689
2690 if (rc != NO_ERROR) {
2691 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002692 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002693 }
2694
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002695 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696 do {
2697 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002698 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002699 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002700
2701 strcpy(namebuf, ep.achName);
2702
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002703 /* Leave Case of Name Alone -- In Native Form */
2704 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002705
Christian Heimes72b710a2008-05-26 13:28:38 +00002706 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002707 if (v == NULL) {
2708 Py_DECREF(d);
2709 d = NULL;
2710 break;
2711 }
2712 if (PyList_Append(d, v) != 0) {
2713 Py_DECREF(v);
2714 Py_DECREF(d);
2715 d = NULL;
2716 break;
2717 }
2718 Py_DECREF(v);
2719 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2720 }
2721
Victor Stinnerdcb24032010-04-22 12:08:36 +00002722 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002723 return d;
2724#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002725 PyObject *oname;
2726 char *name;
2727 PyObject *d, *v;
2728 DIR *dirp;
2729 struct dirent *ep;
2730 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002731
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002733 /* v is never read, so it does not need to be initialized yet. */
2734 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002735 arg_is_unicode = 0;
2736 PyErr_Clear();
2737 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002738 oname = NULL;
2739 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002740 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002741 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002742 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002743 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002744 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002745 Py_BEGIN_ALLOW_THREADS
2746 dirp = opendir(name);
2747 Py_END_ALLOW_THREADS
2748 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 return posix_error_with_allocated_filename(oname);
2750 }
2751 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002752 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002753 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002754 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002755 Py_DECREF(oname);
2756 return NULL;
2757 }
2758 for (;;) {
2759 errno = 0;
2760 Py_BEGIN_ALLOW_THREADS
2761 ep = readdir(dirp);
2762 Py_END_ALLOW_THREADS
2763 if (ep == NULL) {
2764 if (errno == 0) {
2765 break;
2766 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002767 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002768 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002769 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002770 Py_DECREF(d);
2771 return posix_error_with_allocated_filename(oname);
2772 }
2773 }
2774 if (ep->d_name[0] == '.' &&
2775 (NAMLEN(ep) == 1 ||
2776 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2777 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002778 if (arg_is_unicode)
2779 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2780 else
2781 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002783 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002784 break;
2785 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002786 if (PyList_Append(d, v) != 0) {
2787 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002788 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 break;
2790 }
2791 Py_DECREF(v);
2792 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002793 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002795 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002797
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002799
Tim Peters0bb44a42000-09-15 07:44:49 +00002800#endif /* which OS */
2801} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002802
Antoine Pitrou8250e232011-02-25 23:41:16 +00002803#ifdef HAVE_FDOPENDIR
2804PyDoc_STRVAR(posix_fdlistdir__doc__,
2805"fdlistdir(fd) -> list_of_strings\n\n\
2806Like listdir(), but uses a file descriptor instead.\n\
2807After succesful execution of this function, fd will be closed.");
2808
2809static PyObject *
2810posix_fdlistdir(PyObject *self, PyObject *args)
2811{
2812 PyObject *d, *v;
2813 DIR *dirp;
2814 struct dirent *ep;
2815 int fd;
2816
2817 errno = 0;
2818 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2819 return NULL;
2820 Py_BEGIN_ALLOW_THREADS
2821 dirp = fdopendir(fd);
2822 Py_END_ALLOW_THREADS
2823 if (dirp == NULL) {
2824 close(fd);
2825 return posix_error();
2826 }
2827 if ((d = PyList_New(0)) == NULL) {
2828 Py_BEGIN_ALLOW_THREADS
2829 closedir(dirp);
2830 Py_END_ALLOW_THREADS
2831 return NULL;
2832 }
2833 for (;;) {
2834 errno = 0;
2835 Py_BEGIN_ALLOW_THREADS
2836 ep = readdir(dirp);
2837 Py_END_ALLOW_THREADS
2838 if (ep == NULL) {
2839 if (errno == 0) {
2840 break;
2841 } else {
2842 Py_BEGIN_ALLOW_THREADS
2843 closedir(dirp);
2844 Py_END_ALLOW_THREADS
2845 Py_DECREF(d);
2846 return posix_error();
2847 }
2848 }
2849 if (ep->d_name[0] == '.' &&
2850 (NAMLEN(ep) == 1 ||
2851 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2852 continue;
2853 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2854 if (v == NULL) {
2855 Py_CLEAR(d);
2856 break;
2857 }
2858 if (PyList_Append(d, v) != 0) {
2859 Py_DECREF(v);
2860 Py_CLEAR(d);
2861 break;
2862 }
2863 Py_DECREF(v);
2864 }
2865 Py_BEGIN_ALLOW_THREADS
2866 closedir(dirp);
2867 Py_END_ALLOW_THREADS
2868
2869 return d;
2870}
2871#endif
2872
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002873#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002874/* A helper function for abspath on win32 */
2875static PyObject *
2876posix__getfullpathname(PyObject *self, PyObject *args)
2877{
Victor Stinner8c62be82010-05-06 00:08:46 +00002878 PyObject *opath;
2879 char *path;
2880 char outbuf[MAX_PATH*2];
2881 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002883 PyUnicodeObject *po;
2884 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2885 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2886 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2887 Py_UNICODE *wtemp;
2888 DWORD result;
2889 PyObject *v;
2890 result = GetFullPathNameW(wpath,
2891 sizeof(woutbuf)/sizeof(woutbuf[0]),
2892 woutbuf, &wtemp);
2893 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2894 woutbufp = malloc(result * sizeof(Py_UNICODE));
2895 if (!woutbufp)
2896 return PyErr_NoMemory();
2897 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2898 }
2899 if (result)
2900 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2901 else
2902 v = win32_error_unicode("GetFullPathNameW", wpath);
2903 if (woutbufp != woutbuf)
2904 free(woutbufp);
2905 return v;
2906 }
2907 /* Drop the argument parsing error as narrow strings
2908 are also valid. */
2909 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002910
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002911#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002912 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2913 PyUnicode_FSConverter, &opath))
2914 return NULL;
2915 path = PyBytes_AsString(opath);
2916 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2917 outbuf, &temp)) {
2918 win32_error("GetFullPathName", path);
2919 Py_DECREF(opath);
2920 return NULL;
2921 }
2922 Py_DECREF(opath);
2923 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2924 return PyUnicode_Decode(outbuf, strlen(outbuf),
2925 Py_FileSystemDefaultEncoding, NULL);
2926 }
2927 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002928} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002929
Brian Curtind25aef52011-06-13 15:16:04 -05002930
Brian Curtinf5e76d02010-11-24 13:14:05 +00002931
Brian Curtind40e6f72010-07-08 21:39:08 +00002932/* A helper function for samepath on windows */
2933static PyObject *
2934posix__getfinalpathname(PyObject *self, PyObject *args)
2935{
2936 HANDLE hFile;
2937 int buf_size;
2938 wchar_t *target_path;
2939 int result_length;
2940 PyObject *result;
2941 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002942
Brian Curtin94622b02010-09-24 00:03:39 +00002943 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002944 return NULL;
2945 }
2946
2947 if(!check_GetFinalPathNameByHandle()) {
2948 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2949 NotImplementedError. */
2950 return PyErr_Format(PyExc_NotImplementedError,
2951 "GetFinalPathNameByHandle not available on this platform");
2952 }
2953
2954 hFile = CreateFileW(
2955 path,
2956 0, /* desired access */
2957 0, /* share mode */
2958 NULL, /* security attributes */
2959 OPEN_EXISTING,
2960 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2961 FILE_FLAG_BACKUP_SEMANTICS,
2962 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002963
Brian Curtind40e6f72010-07-08 21:39:08 +00002964 if(hFile == INVALID_HANDLE_VALUE) {
2965 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002966 return PyErr_Format(PyExc_RuntimeError,
2967 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002968 }
2969
2970 /* We have a good handle to the target, use it to determine the
2971 target path name. */
2972 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2973
2974 if(!buf_size)
2975 return win32_error_unicode("GetFinalPathNameByHandle", path);
2976
2977 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2978 if(!target_path)
2979 return PyErr_NoMemory();
2980
2981 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2982 buf_size, VOLUME_NAME_DOS);
2983 if(!result_length)
2984 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2985
2986 if(!CloseHandle(hFile))
2987 return win32_error_unicode("GetFinalPathNameByHandle", path);
2988
2989 target_path[result_length] = 0;
2990 result = PyUnicode_FromUnicode(target_path, result_length);
2991 free(target_path);
2992 return result;
2993
2994} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002995
2996static PyObject *
2997posix__getfileinformation(PyObject *self, PyObject *args)
2998{
2999 HANDLE hFile;
3000 BY_HANDLE_FILE_INFORMATION info;
3001 int fd;
3002
3003 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3004 return NULL;
3005
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003006 if (!_PyVerify_fd(fd))
3007 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003008
3009 hFile = (HANDLE)_get_osfhandle(fd);
3010 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003011 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003012
3013 if (!GetFileInformationByHandle(hFile, &info))
3014 return win32_error("_getfileinformation", NULL);
3015
3016 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3017 info.nFileIndexHigh,
3018 info.nFileIndexLow);
3019}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003020
Brian Curtin95d028f2011-06-09 09:10:38 -05003021PyDoc_STRVAR(posix__isdir__doc__,
3022"Return true if the pathname refers to an existing directory.");
3023
Brian Curtin9c669cc2011-06-08 18:17:18 -05003024static PyObject *
3025posix__isdir(PyObject *self, PyObject *args)
3026{
3027 PyObject *opath;
3028 char *path;
3029 PyUnicodeObject *po;
3030 DWORD attributes;
3031
3032 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3033 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3034
3035 attributes = GetFileAttributesW(wpath);
3036 if (attributes == INVALID_FILE_ATTRIBUTES)
3037 Py_RETURN_FALSE;
3038 goto check;
3039 }
3040 /* Drop the argument parsing error as narrow strings
3041 are also valid. */
3042 PyErr_Clear();
3043
3044 if (!PyArg_ParseTuple(args, "O&:_isdir",
3045 PyUnicode_FSConverter, &opath))
3046 return NULL;
3047
3048 path = PyBytes_AsString(opath);
3049 attributes = GetFileAttributesA(path);
3050 if (attributes == INVALID_FILE_ATTRIBUTES)
3051 Py_RETURN_FALSE;
3052
3053check:
3054 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3055 Py_RETURN_TRUE;
3056 else
3057 Py_RETURN_FALSE;
3058}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003059#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003061PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003062"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003063Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003064
Barry Warsaw53699e91996-12-10 23:23:01 +00003065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003066posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003067{
Victor Stinner8c62be82010-05-06 00:08:46 +00003068 int res;
3069 PyObject *opath;
3070 char *path;
3071 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003072
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003073#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003074 PyUnicodeObject *po;
3075 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3076 Py_BEGIN_ALLOW_THREADS
3077 /* PyUnicode_AS_UNICODE OK without thread lock as
3078 it is a simple dereference. */
3079 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3080 Py_END_ALLOW_THREADS
3081 if (!res)
3082 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3083 Py_INCREF(Py_None);
3084 return Py_None;
3085 }
3086 /* Drop the argument parsing error as narrow strings
3087 are also valid. */
3088 PyErr_Clear();
3089 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3090 PyUnicode_FSConverter, &opath, &mode))
3091 return NULL;
3092 path = PyBytes_AsString(opath);
3093 Py_BEGIN_ALLOW_THREADS
3094 /* PyUnicode_AS_UNICODE OK without thread lock as
3095 it is a simple dereference. */
3096 res = CreateDirectoryA(path, NULL);
3097 Py_END_ALLOW_THREADS
3098 if (!res) {
3099 win32_error("mkdir", path);
3100 Py_DECREF(opath);
3101 return NULL;
3102 }
3103 Py_DECREF(opath);
3104 Py_INCREF(Py_None);
3105 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003106#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003107
Victor Stinner8c62be82010-05-06 00:08:46 +00003108 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3109 PyUnicode_FSConverter, &opath, &mode))
3110 return NULL;
3111 path = PyBytes_AsString(opath);
3112 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003113#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003115#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003116 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003117#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 Py_END_ALLOW_THREADS
3119 if (res < 0)
3120 return posix_error_with_allocated_filename(opath);
3121 Py_DECREF(opath);
3122 Py_INCREF(Py_None);
3123 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003124#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003125}
3126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003127
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003128/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3129#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003130#include <sys/resource.h>
3131#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003133
3134#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003136"nice(inc) -> new_priority\n\n\
3137Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138
Barry Warsaw53699e91996-12-10 23:23:01 +00003139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003140posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003141{
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003143
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3145 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003146
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 /* There are two flavours of 'nice': one that returns the new
3148 priority (as required by almost all standards out there) and the
3149 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3150 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003151
Victor Stinner8c62be82010-05-06 00:08:46 +00003152 If we are of the nice family that returns the new priority, we
3153 need to clear errno before the call, and check if errno is filled
3154 before calling posix_error() on a returnvalue of -1, because the
3155 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003156
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 errno = 0;
3158 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003159#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 if (value == 0)
3161 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003162#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 if (value == -1 && errno != 0)
3164 /* either nice() or getpriority() returned an error */
3165 return posix_error();
3166 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003167}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003168#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003169
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003170
3171#ifdef HAVE_GETPRIORITY
3172PyDoc_STRVAR(posix_getpriority__doc__,
3173"getpriority(which, who) -> current_priority\n\n\
3174Get program scheduling priority.");
3175
3176static PyObject *
3177posix_getpriority(PyObject *self, PyObject *args)
3178{
3179 int which, who, retval;
3180
3181 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3182 return NULL;
3183 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003184 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003185 if (errno != 0)
3186 return posix_error();
3187 return PyLong_FromLong((long)retval);
3188}
3189#endif /* HAVE_GETPRIORITY */
3190
3191
3192#ifdef HAVE_SETPRIORITY
3193PyDoc_STRVAR(posix_setpriority__doc__,
3194"setpriority(which, who, prio) -> None\n\n\
3195Set program scheduling priority.");
3196
3197static PyObject *
3198posix_setpriority(PyObject *self, PyObject *args)
3199{
3200 int which, who, prio, retval;
3201
3202 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3203 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003204 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003205 if (retval == -1)
3206 return posix_error();
3207 Py_RETURN_NONE;
3208}
3209#endif /* HAVE_SETPRIORITY */
3210
3211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003212PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003213"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003214Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003215
Barry Warsaw53699e91996-12-10 23:23:01 +00003216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003217posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003218{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003219#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003220 PyObject *o1, *o2;
3221 char *p1, *p2;
3222 BOOL result;
3223 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3224 goto error;
3225 if (!convert_to_unicode(&o1))
3226 goto error;
3227 if (!convert_to_unicode(&o2)) {
3228 Py_DECREF(o1);
3229 goto error;
3230 }
3231 Py_BEGIN_ALLOW_THREADS
3232 result = MoveFileW(PyUnicode_AsUnicode(o1),
3233 PyUnicode_AsUnicode(o2));
3234 Py_END_ALLOW_THREADS
3235 Py_DECREF(o1);
3236 Py_DECREF(o2);
3237 if (!result)
3238 return win32_error("rename", NULL);
3239 Py_INCREF(Py_None);
3240 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003241error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003242 PyErr_Clear();
3243 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3244 return NULL;
3245 Py_BEGIN_ALLOW_THREADS
3246 result = MoveFileA(p1, p2);
3247 Py_END_ALLOW_THREADS
3248 if (!result)
3249 return win32_error("rename", NULL);
3250 Py_INCREF(Py_None);
3251 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003252#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003253 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003254#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003255}
3256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003258PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003259"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003260Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003261
Barry Warsaw53699e91996-12-10 23:23:01 +00003262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003263posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003264{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003266 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003267#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003268 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003269#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003270}
3271
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003273PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003274"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003275Perform a stat system call on the given path.");
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_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003279{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003280#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003281 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003282#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003283 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003284#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003285}
3286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003287
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003288#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003289PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003290"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003291Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003292
Barry Warsaw53699e91996-12-10 23:23:01 +00003293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003294posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003295{
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003297#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 wchar_t *command;
3299 if (!PyArg_ParseTuple(args, "u:system", &command))
3300 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003301
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 Py_BEGIN_ALLOW_THREADS
3303 sts = _wsystem(command);
3304 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003305#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 PyObject *command_obj;
3307 char *command;
3308 if (!PyArg_ParseTuple(args, "O&:system",
3309 PyUnicode_FSConverter, &command_obj))
3310 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003311
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 command = PyBytes_AsString(command_obj);
3313 Py_BEGIN_ALLOW_THREADS
3314 sts = system(command);
3315 Py_END_ALLOW_THREADS
3316 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003317#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003318 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003319}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003320#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003321
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003323PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003324"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003325Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003326
Barry Warsaw53699e91996-12-10 23:23:01 +00003327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003328posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003329{
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 int i;
3331 if (!PyArg_ParseTuple(args, "i:umask", &i))
3332 return NULL;
3333 i = (int)umask(i);
3334 if (i < 0)
3335 return posix_error();
3336 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003337}
3338
Brian Curtind40e6f72010-07-08 21:39:08 +00003339#ifdef MS_WINDOWS
3340
3341/* override the default DeleteFileW behavior so that directory
3342symlinks can be removed with this function, the same as with
3343Unix symlinks */
3344BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3345{
3346 WIN32_FILE_ATTRIBUTE_DATA info;
3347 WIN32_FIND_DATAW find_data;
3348 HANDLE find_data_handle;
3349 int is_directory = 0;
3350 int is_link = 0;
3351
3352 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3353 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003354
Brian Curtind40e6f72010-07-08 21:39:08 +00003355 /* Get WIN32_FIND_DATA structure for the path to determine if
3356 it is a symlink */
3357 if(is_directory &&
3358 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3359 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3360
3361 if(find_data_handle != INVALID_HANDLE_VALUE) {
3362 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3363 FindClose(find_data_handle);
3364 }
3365 }
3366 }
3367
3368 if (is_directory && is_link)
3369 return RemoveDirectoryW(lpFileName);
3370
3371 return DeleteFileW(lpFileName);
3372}
3373#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003376"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003379PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003380"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003381Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003382
Barry Warsaw53699e91996-12-10 23:23:01 +00003383static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003384posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003385{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003386#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003387 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3388 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003389#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003390 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003391#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003392}
3393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003394
Guido van Rossumb6775db1994-08-01 11:34:53 +00003395#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003396PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003397"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003398Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003399
Barry Warsaw53699e91996-12-10 23:23:01 +00003400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003401posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003402{
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 struct utsname u;
3404 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003405
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 Py_BEGIN_ALLOW_THREADS
3407 res = uname(&u);
3408 Py_END_ALLOW_THREADS
3409 if (res < 0)
3410 return posix_error();
3411 return Py_BuildValue("(sssss)",
3412 u.sysname,
3413 u.nodename,
3414 u.release,
3415 u.version,
3416 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003417}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003418#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003419
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003420
3421/*
3422 * Classic POSIX utime functions supported microseconds (1m/sec).
3423 * Newer POSIX functions support nanoseconds (1 billion per sec).
3424 * posixmodule now uses the new functions where possible.
3425 * This improves accuracy in many situations, for example shutil.copy2().
3426 *
3427 * The implementation isn't currently sophisticated enough to handle
3428 * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false.
3429 * Specifically, posix_futimes() would break.
3430 *
3431 * Supporting such a platform wouldn't be impossible; you'd need two
3432 * extract_time() functions, or make its precision a parameter.
3433 * Since such a platform seems unlikely we haven't bothered.
3434 */
3435#if defined(HAVE_UTIMENSAT)
3436#define EXTRACT_TIME_PRECISION (1e9)
3437#if !defined(HAVE_FUTIMENS)
3438#error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment.
3439#endif
3440#else
3441#define EXTRACT_TIME_PRECISION (1e6)
3442#endif
3443
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003444static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003445extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003446{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003447 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003448 if (PyFloat_Check(t)) {
3449 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003450 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003451 if (!intobj)
3452 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003453#if SIZEOF_TIME_T > SIZEOF_LONG
3454 intval = PyLong_AsUnsignedLongLongMask(intobj);
3455#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003456 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003457#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 Py_DECREF(intobj);
3459 if (intval == -1 && PyErr_Occurred())
3460 return -1;
3461 *sec = intval;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003462
3463 *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION);
Victor Stinner8c62be82010-05-06 00:08:46 +00003464 if (*usec < 0)
3465 /* If rounding gave us a negative number,
3466 truncate. */
3467 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003468 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003469 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003470#if SIZEOF_TIME_T > SIZEOF_LONG
3471 intval = PyLong_AsUnsignedLongLongMask(t);
3472#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003474#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 if (intval == -1 && PyErr_Occurred())
3476 return -1;
3477 *sec = intval;
3478 *usec = 0;
3479 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003480}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003482PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003483"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003484utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003485Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003486second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003487
Barry Warsaw53699e91996-12-10 23:23:01 +00003488static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003489posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003490{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003491#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 PyObject *arg;
3493 PyUnicodeObject *obwpath;
3494 wchar_t *wpath = NULL;
3495 PyObject *oapath;
3496 char *apath;
3497 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003498 time_t atimesec, mtimesec;
3499 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 FILETIME atime, mtime;
3501 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003502
Victor Stinner8c62be82010-05-06 00:08:46 +00003503 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3504 wpath = PyUnicode_AS_UNICODE(obwpath);
3505 Py_BEGIN_ALLOW_THREADS
3506 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3507 NULL, OPEN_EXISTING,
3508 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3509 Py_END_ALLOW_THREADS
3510 if (hFile == INVALID_HANDLE_VALUE)
3511 return win32_error_unicode("utime", wpath);
3512 } else
3513 /* Drop the argument parsing error as narrow strings
3514 are also valid. */
3515 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003516
Victor Stinner8c62be82010-05-06 00:08:46 +00003517 if (!wpath) {
3518 if (!PyArg_ParseTuple(args, "O&O:utime",
3519 PyUnicode_FSConverter, &oapath, &arg))
3520 return NULL;
3521 apath = PyBytes_AsString(oapath);
3522 Py_BEGIN_ALLOW_THREADS
3523 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3524 NULL, OPEN_EXISTING,
3525 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3526 Py_END_ALLOW_THREADS
3527 if (hFile == INVALID_HANDLE_VALUE) {
3528 win32_error("utime", apath);
3529 Py_DECREF(oapath);
3530 return NULL;
3531 }
3532 Py_DECREF(oapath);
3533 }
3534
3535 if (arg == Py_None) {
3536 SYSTEMTIME now;
3537 GetSystemTime(&now);
3538 if (!SystemTimeToFileTime(&now, &mtime) ||
3539 !SystemTimeToFileTime(&now, &atime)) {
3540 win32_error("utime", NULL);
3541 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003542 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 }
3544 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3545 PyErr_SetString(PyExc_TypeError,
3546 "utime() arg 2 must be a tuple (atime, mtime)");
3547 goto done;
3548 }
3549 else {
3550 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3551 &atimesec, &ausec) == -1)
3552 goto done;
3553 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3554 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3555 &mtimesec, &musec) == -1)
3556 goto done;
3557 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3558 }
3559 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3560 /* Avoid putting the file name into the error here,
3561 as that may confuse the user into believing that
3562 something is wrong with the file, when it also
3563 could be the time stamp that gives a problem. */
3564 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003565 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003566 }
3567 Py_INCREF(Py_None);
3568 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003569done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 CloseHandle(hFile);
3571 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003572#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003573
Victor Stinner8c62be82010-05-06 00:08:46 +00003574 PyObject *opath;
3575 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003576 time_t atime, mtime;
3577 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 int res;
3579 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003580
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 if (!PyArg_ParseTuple(args, "O&O:utime",
3582 PyUnicode_FSConverter, &opath, &arg))
3583 return NULL;
3584 path = PyBytes_AsString(opath);
3585 if (arg == Py_None) {
3586 /* optional time values not given */
3587 Py_BEGIN_ALLOW_THREADS
3588 res = utime(path, NULL);
3589 Py_END_ALLOW_THREADS
3590 }
3591 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3592 PyErr_SetString(PyExc_TypeError,
3593 "utime() arg 2 must be a tuple (atime, mtime)");
3594 Py_DECREF(opath);
3595 return NULL;
3596 }
3597 else {
3598 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3599 &atime, &ausec) == -1) {
3600 Py_DECREF(opath);
3601 return NULL;
3602 }
3603 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3604 &mtime, &musec) == -1) {
3605 Py_DECREF(opath);
3606 return NULL;
3607 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003608
3609 Py_BEGIN_ALLOW_THREADS
3610 {
3611#ifdef HAVE_UTIMENSAT
3612 struct timespec buf[2];
3613 buf[0].tv_sec = atime;
3614 buf[0].tv_nsec = ausec;
3615 buf[1].tv_sec = mtime;
3616 buf[1].tv_nsec = musec;
3617 res = utimensat(AT_FDCWD, path, buf, 0);
3618#elif defined(HAVE_UTIMES)
3619 struct timeval buf[2];
3620 buf[0].tv_sec = atime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 buf[0].tv_usec = ausec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003622 buf[1].tv_sec = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003623 buf[1].tv_usec = musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003625#elif defined(HAVE_UTIME_H)
3626 /* XXX should define struct utimbuf instead, above */
3627 struct utimbuf buf;
3628 buf.actime = atime;
3629 buf.modtime = mtime;
3630 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003631#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003632 time_t buf[2];
3633 buf[0] = atime;
3634 buf[1] = mtime;
3635 res = utime(path, buf);
3636#endif
3637 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003639 }
3640 if (res < 0) {
3641 return posix_error_with_allocated_filename(opath);
3642 }
3643 Py_DECREF(opath);
3644 Py_INCREF(Py_None);
3645 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003646#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003647#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003648}
3649
Ross Lagerwall7807c352011-03-17 20:20:30 +02003650#ifdef HAVE_FUTIMES
3651PyDoc_STRVAR(posix_futimes__doc__,
3652"futimes(fd, (atime, mtime))\n\
3653futimes(fd, None)\n\n\
3654Set the access and modified time of the file specified by the file\n\
3655descriptor fd to the given values. If the second form is used, set the\n\
3656access and modified times to the current time.");
3657
3658static PyObject *
3659posix_futimes(PyObject *self, PyObject *args)
3660{
3661 int res, fd;
3662 PyObject* arg;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003663 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003664 long ausec, musec;
3665
3666 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3667 return NULL;
3668
3669 if (arg == Py_None) {
3670 /* optional time values not given */
3671 Py_BEGIN_ALLOW_THREADS
3672 res = futimes(fd, NULL);
3673 Py_END_ALLOW_THREADS
3674 }
3675 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3676 PyErr_SetString(PyExc_TypeError,
3677 "futimes() arg 2 must be a tuple (atime, mtime)");
3678 return NULL;
3679 }
3680 else {
3681 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003682 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003683 return NULL;
3684 }
3685 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003686 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003687 return NULL;
3688 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003689 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003690 {
3691#ifdef HAVE_FUTIMENS
3692 struct timespec buf[2];
3693 buf[0].tv_sec = atime;
3694 buf[0].tv_nsec = ausec;
3695 buf[1].tv_sec = mtime;
3696 buf[1].tv_nsec = musec;
3697 res = futimens(fd, buf);
3698#else
3699 struct timeval buf[2];
3700 buf[0].tv_sec = atime;
3701 buf[0].tv_usec = ausec;
3702 buf[1].tv_sec = mtime;
3703 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003704 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003705#endif
3706 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003707 Py_END_ALLOW_THREADS
3708 }
3709 if (res < 0)
3710 return posix_error();
3711 Py_RETURN_NONE;
3712}
3713#endif
3714
3715#ifdef HAVE_LUTIMES
3716PyDoc_STRVAR(posix_lutimes__doc__,
3717"lutimes(path, (atime, mtime))\n\
3718lutimes(path, None)\n\n\
3719Like utime(), but if path is a symbolic link, it is not dereferenced.");
3720
3721static PyObject *
3722posix_lutimes(PyObject *self, PyObject *args)
3723{
3724 PyObject *opath, *arg;
3725 const char *path;
3726 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003727 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003728 long ausec, musec;
3729
3730 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3731 PyUnicode_FSConverter, &opath, &arg))
3732 return NULL;
3733 path = PyBytes_AsString(opath);
3734 if (arg == Py_None) {
3735 /* optional time values not given */
3736 Py_BEGIN_ALLOW_THREADS
3737 res = lutimes(path, NULL);
3738 Py_END_ALLOW_THREADS
3739 }
3740 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3741 PyErr_SetString(PyExc_TypeError,
3742 "lutimes() arg 2 must be a tuple (atime, mtime)");
3743 Py_DECREF(opath);
3744 return NULL;
3745 }
3746 else {
3747 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003748 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003749 Py_DECREF(opath);
3750 return NULL;
3751 }
3752 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003753 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003754 Py_DECREF(opath);
3755 return NULL;
3756 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003757 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003758 {
3759#ifdef HAVE_UTIMENSAT
3760 struct timespec buf[2];
3761 buf[0].tv_sec = atime;
3762 buf[0].tv_nsec = ausec;
3763 buf[1].tv_sec = mtime;
3764 buf[1].tv_nsec = musec;
3765 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3766#else
3767 struct timeval buf[2];
3768 buf[0].tv_sec = atime;
3769 buf[0].tv_usec = ausec;
3770 buf[1].tv_sec = mtime;
3771 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003772 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003773#endif
3774 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003775 Py_END_ALLOW_THREADS
3776 }
3777 Py_DECREF(opath);
3778 if (res < 0)
3779 return posix_error();
3780 Py_RETURN_NONE;
3781}
3782#endif
3783
3784#ifdef HAVE_FUTIMENS
3785PyDoc_STRVAR(posix_futimens__doc__,
3786"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3787futimens(fd, None, None)\n\n\
3788Updates the timestamps of a file specified by the file descriptor fd, with\n\
3789nanosecond precision.\n\
3790The second form sets atime and mtime to the current time.\n\
3791If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3792current time.\n\
3793If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3794
3795static PyObject *
3796posix_futimens(PyObject *self, PyObject *args)
3797{
3798 int res, fd;
3799 PyObject *atime, *mtime;
3800 struct timespec buf[2];
3801
3802 if (!PyArg_ParseTuple(args, "iOO:futimens",
3803 &fd, &atime, &mtime))
3804 return NULL;
3805 if (atime == Py_None && mtime == Py_None) {
3806 /* optional time values not given */
3807 Py_BEGIN_ALLOW_THREADS
3808 res = futimens(fd, NULL);
3809 Py_END_ALLOW_THREADS
3810 }
3811 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3812 PyErr_SetString(PyExc_TypeError,
3813 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3814 return NULL;
3815 }
3816 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3817 PyErr_SetString(PyExc_TypeError,
3818 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3819 return NULL;
3820 }
3821 else {
3822 if (!PyArg_ParseTuple(atime, "ll:futimens",
3823 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3824 return NULL;
3825 }
3826 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3827 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3828 return NULL;
3829 }
3830 Py_BEGIN_ALLOW_THREADS
3831 res = futimens(fd, buf);
3832 Py_END_ALLOW_THREADS
3833 }
3834 if (res < 0)
3835 return posix_error();
3836 Py_RETURN_NONE;
3837}
3838#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003839
Guido van Rossum3b066191991-06-04 19:40:25 +00003840/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003842PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003843"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003844Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003845
Barry Warsaw53699e91996-12-10 23:23:01 +00003846static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003847posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003848{
Victor Stinner8c62be82010-05-06 00:08:46 +00003849 int sts;
3850 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3851 return NULL;
3852 _exit(sts);
3853 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003854}
3855
Martin v. Löwis114619e2002-10-07 06:44:21 +00003856#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3857static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003858free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003859{
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 Py_ssize_t i;
3861 for (i = 0; i < count; i++)
3862 PyMem_Free(array[i]);
3863 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003864}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003865
Antoine Pitrou69f71142009-05-24 21:25:49 +00003866static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003867int fsconvert_strdup(PyObject *o, char**out)
3868{
Victor Stinner8c62be82010-05-06 00:08:46 +00003869 PyObject *bytes;
3870 Py_ssize_t size;
3871 if (!PyUnicode_FSConverter(o, &bytes))
3872 return 0;
3873 size = PyBytes_GET_SIZE(bytes);
3874 *out = PyMem_Malloc(size+1);
3875 if (!*out)
3876 return 0;
3877 memcpy(*out, PyBytes_AsString(bytes), size+1);
3878 Py_DECREF(bytes);
3879 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003880}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003881#endif
3882
Ross Lagerwall7807c352011-03-17 20:20:30 +02003883#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003884static char**
3885parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3886{
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 char **envlist;
3888 Py_ssize_t i, pos, envc;
3889 PyObject *keys=NULL, *vals=NULL;
3890 PyObject *key, *val, *key2, *val2;
3891 char *p, *k, *v;
3892 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003893
Victor Stinner8c62be82010-05-06 00:08:46 +00003894 i = PyMapping_Size(env);
3895 if (i < 0)
3896 return NULL;
3897 envlist = PyMem_NEW(char *, i + 1);
3898 if (envlist == NULL) {
3899 PyErr_NoMemory();
3900 return NULL;
3901 }
3902 envc = 0;
3903 keys = PyMapping_Keys(env);
3904 vals = PyMapping_Values(env);
3905 if (!keys || !vals)
3906 goto error;
3907 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3908 PyErr_Format(PyExc_TypeError,
3909 "env.keys() or env.values() is not a list");
3910 goto error;
3911 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003912
Victor Stinner8c62be82010-05-06 00:08:46 +00003913 for (pos = 0; pos < i; pos++) {
3914 key = PyList_GetItem(keys, pos);
3915 val = PyList_GetItem(vals, pos);
3916 if (!key || !val)
3917 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003918
Victor Stinner8c62be82010-05-06 00:08:46 +00003919 if (PyUnicode_FSConverter(key, &key2) == 0)
3920 goto error;
3921 if (PyUnicode_FSConverter(val, &val2) == 0) {
3922 Py_DECREF(key2);
3923 goto error;
3924 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003925
3926#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003927 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3928 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003929#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003930 k = PyBytes_AsString(key2);
3931 v = PyBytes_AsString(val2);
3932 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003933
Victor Stinner8c62be82010-05-06 00:08:46 +00003934 p = PyMem_NEW(char, len);
3935 if (p == NULL) {
3936 PyErr_NoMemory();
3937 Py_DECREF(key2);
3938 Py_DECREF(val2);
3939 goto error;
3940 }
3941 PyOS_snprintf(p, len, "%s=%s", k, v);
3942 envlist[envc++] = p;
3943 Py_DECREF(key2);
3944 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003945#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003946 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003947#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003948 }
3949 Py_DECREF(vals);
3950 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003951
Victor Stinner8c62be82010-05-06 00:08:46 +00003952 envlist[envc] = 0;
3953 *envc_ptr = envc;
3954 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003955
3956error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003957 Py_XDECREF(keys);
3958 Py_XDECREF(vals);
3959 while (--envc >= 0)
3960 PyMem_DEL(envlist[envc]);
3961 PyMem_DEL(envlist);
3962 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003963}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003964
Ross Lagerwall7807c352011-03-17 20:20:30 +02003965static char**
3966parse_arglist(PyObject* argv, Py_ssize_t *argc)
3967{
3968 int i;
3969 char **argvlist = PyMem_NEW(char *, *argc+1);
3970 if (argvlist == NULL) {
3971 PyErr_NoMemory();
3972 return NULL;
3973 }
3974 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003975 PyObject* item = PySequence_ITEM(argv, i);
3976 if (item == NULL)
3977 goto fail;
3978 if (!fsconvert_strdup(item, &argvlist[i])) {
3979 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003980 goto fail;
3981 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003982 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003983 }
3984 argvlist[*argc] = NULL;
3985 return argvlist;
3986fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003987 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003988 free_string_array(argvlist, *argc);
3989 return NULL;
3990}
3991#endif
3992
3993#ifdef HAVE_EXECV
3994PyDoc_STRVAR(posix_execv__doc__,
3995"execv(path, args)\n\n\
3996Execute an executable path with arguments, replacing current process.\n\
3997\n\
3998 path: path of executable file\n\
3999 args: tuple or list of strings");
4000
4001static PyObject *
4002posix_execv(PyObject *self, PyObject *args)
4003{
4004 PyObject *opath;
4005 char *path;
4006 PyObject *argv;
4007 char **argvlist;
4008 Py_ssize_t argc;
4009
4010 /* execv has two arguments: (path, argv), where
4011 argv is a list or tuple of strings. */
4012
4013 if (!PyArg_ParseTuple(args, "O&O:execv",
4014 PyUnicode_FSConverter,
4015 &opath, &argv))
4016 return NULL;
4017 path = PyBytes_AsString(opath);
4018 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4019 PyErr_SetString(PyExc_TypeError,
4020 "execv() arg 2 must be a tuple or list");
4021 Py_DECREF(opath);
4022 return NULL;
4023 }
4024 argc = PySequence_Size(argv);
4025 if (argc < 1) {
4026 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4027 Py_DECREF(opath);
4028 return NULL;
4029 }
4030
4031 argvlist = parse_arglist(argv, &argc);
4032 if (argvlist == NULL) {
4033 Py_DECREF(opath);
4034 return NULL;
4035 }
4036
4037 execv(path, argvlist);
4038
4039 /* If we get here it's definitely an error */
4040
4041 free_string_array(argvlist, argc);
4042 Py_DECREF(opath);
4043 return posix_error();
4044}
4045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004046PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004047"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004048Execute a path with arguments and environment, replacing current process.\n\
4049\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004050 path: path of executable file\n\
4051 args: tuple or list of arguments\n\
4052 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004053
Barry Warsaw53699e91996-12-10 23:23:01 +00004054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004055posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004056{
Victor Stinner8c62be82010-05-06 00:08:46 +00004057 PyObject *opath;
4058 char *path;
4059 PyObject *argv, *env;
4060 char **argvlist;
4061 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004062 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004063
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 /* execve has three arguments: (path, argv, env), where
4065 argv is a list or tuple of strings and env is a dictionary
4066 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004067
Victor Stinner8c62be82010-05-06 00:08:46 +00004068 if (!PyArg_ParseTuple(args, "O&OO:execve",
4069 PyUnicode_FSConverter,
4070 &opath, &argv, &env))
4071 return NULL;
4072 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004073 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004074 PyErr_SetString(PyExc_TypeError,
4075 "execve() arg 2 must be a tuple or list");
4076 goto fail_0;
4077 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004078 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004079 if (!PyMapping_Check(env)) {
4080 PyErr_SetString(PyExc_TypeError,
4081 "execve() arg 3 must be a mapping object");
4082 goto fail_0;
4083 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004084
Ross Lagerwall7807c352011-03-17 20:20:30 +02004085 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004087 goto fail_0;
4088 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004089
Victor Stinner8c62be82010-05-06 00:08:46 +00004090 envlist = parse_envlist(env, &envc);
4091 if (envlist == NULL)
4092 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004093
Victor Stinner8c62be82010-05-06 00:08:46 +00004094 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004095
Victor Stinner8c62be82010-05-06 00:08:46 +00004096 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004097
Victor Stinner8c62be82010-05-06 00:08:46 +00004098 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004099
Victor Stinner8c62be82010-05-06 00:08:46 +00004100 while (--envc >= 0)
4101 PyMem_DEL(envlist[envc]);
4102 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004103 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004104 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004105 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004106 Py_DECREF(opath);
4107 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004108}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004109#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004110
Ross Lagerwall7807c352011-03-17 20:20:30 +02004111#ifdef HAVE_FEXECVE
4112PyDoc_STRVAR(posix_fexecve__doc__,
4113"fexecve(fd, args, env)\n\n\
4114Execute the program specified by a file descriptor with arguments and\n\
4115environment, replacing the current process.\n\
4116\n\
4117 fd: file descriptor of executable\n\
4118 args: tuple or list of arguments\n\
4119 env: dictionary of strings mapping to strings");
4120
4121static PyObject *
4122posix_fexecve(PyObject *self, PyObject *args)
4123{
4124 int fd;
4125 PyObject *argv, *env;
4126 char **argvlist;
4127 char **envlist;
4128 Py_ssize_t argc, envc;
4129
4130 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4131 &fd, &argv, &env))
4132 return NULL;
4133 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4134 PyErr_SetString(PyExc_TypeError,
4135 "fexecve() arg 2 must be a tuple or list");
4136 return NULL;
4137 }
4138 argc = PySequence_Size(argv);
4139 if (!PyMapping_Check(env)) {
4140 PyErr_SetString(PyExc_TypeError,
4141 "fexecve() arg 3 must be a mapping object");
4142 return NULL;
4143 }
4144
4145 argvlist = parse_arglist(argv, &argc);
4146 if (argvlist == NULL)
4147 return NULL;
4148
4149 envlist = parse_envlist(env, &envc);
4150 if (envlist == NULL)
4151 goto fail;
4152
4153 fexecve(fd, argvlist, envlist);
4154
4155 /* If we get here it's definitely an error */
4156
4157 (void) posix_error();
4158
4159 while (--envc >= 0)
4160 PyMem_DEL(envlist[envc]);
4161 PyMem_DEL(envlist);
4162 fail:
4163 free_string_array(argvlist, argc);
4164 return NULL;
4165}
4166#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004167
Guido van Rossuma1065681999-01-25 23:20:23 +00004168#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004169PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004170"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004171Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004172\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004173 mode: mode of process creation\n\
4174 path: path of executable file\n\
4175 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004176
4177static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004178posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004179{
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 PyObject *opath;
4181 char *path;
4182 PyObject *argv;
4183 char **argvlist;
4184 int mode, i;
4185 Py_ssize_t argc;
4186 Py_intptr_t spawnval;
4187 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004188
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 /* spawnv has three arguments: (mode, path, argv), where
4190 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004191
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4193 PyUnicode_FSConverter,
4194 &opath, &argv))
4195 return NULL;
4196 path = PyBytes_AsString(opath);
4197 if (PyList_Check(argv)) {
4198 argc = PyList_Size(argv);
4199 getitem = PyList_GetItem;
4200 }
4201 else if (PyTuple_Check(argv)) {
4202 argc = PyTuple_Size(argv);
4203 getitem = PyTuple_GetItem;
4204 }
4205 else {
4206 PyErr_SetString(PyExc_TypeError,
4207 "spawnv() arg 2 must be a tuple or list");
4208 Py_DECREF(opath);
4209 return NULL;
4210 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004211
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 argvlist = PyMem_NEW(char *, argc+1);
4213 if (argvlist == NULL) {
4214 Py_DECREF(opath);
4215 return PyErr_NoMemory();
4216 }
4217 for (i = 0; i < argc; i++) {
4218 if (!fsconvert_strdup((*getitem)(argv, i),
4219 &argvlist[i])) {
4220 free_string_array(argvlist, i);
4221 PyErr_SetString(
4222 PyExc_TypeError,
4223 "spawnv() arg 2 must contain only strings");
4224 Py_DECREF(opath);
4225 return NULL;
4226 }
4227 }
4228 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004229
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004230#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004231 Py_BEGIN_ALLOW_THREADS
4232 spawnval = spawnv(mode, path, argvlist);
4233 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004234#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 if (mode == _OLD_P_OVERLAY)
4236 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004237
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 Py_BEGIN_ALLOW_THREADS
4239 spawnval = _spawnv(mode, path, argvlist);
4240 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004241#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004242
Victor Stinner8c62be82010-05-06 00:08:46 +00004243 free_string_array(argvlist, argc);
4244 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004245
Victor Stinner8c62be82010-05-06 00:08:46 +00004246 if (spawnval == -1)
4247 return posix_error();
4248 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004249#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004250 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004251#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004252 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004253#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004254}
4255
4256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004257PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004258"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004259Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004260\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 mode: mode of process creation\n\
4262 path: path of executable file\n\
4263 args: tuple or list of arguments\n\
4264 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004265
4266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004267posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004268{
Victor Stinner8c62be82010-05-06 00:08:46 +00004269 PyObject *opath;
4270 char *path;
4271 PyObject *argv, *env;
4272 char **argvlist;
4273 char **envlist;
4274 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004275 int mode;
4276 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004277 Py_intptr_t spawnval;
4278 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4279 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004280
Victor Stinner8c62be82010-05-06 00:08:46 +00004281 /* spawnve has four arguments: (mode, path, argv, env), where
4282 argv is a list or tuple of strings and env is a dictionary
4283 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004284
Victor Stinner8c62be82010-05-06 00:08:46 +00004285 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4286 PyUnicode_FSConverter,
4287 &opath, &argv, &env))
4288 return NULL;
4289 path = PyBytes_AsString(opath);
4290 if (PyList_Check(argv)) {
4291 argc = PyList_Size(argv);
4292 getitem = PyList_GetItem;
4293 }
4294 else if (PyTuple_Check(argv)) {
4295 argc = PyTuple_Size(argv);
4296 getitem = PyTuple_GetItem;
4297 }
4298 else {
4299 PyErr_SetString(PyExc_TypeError,
4300 "spawnve() arg 2 must be a tuple or list");
4301 goto fail_0;
4302 }
4303 if (!PyMapping_Check(env)) {
4304 PyErr_SetString(PyExc_TypeError,
4305 "spawnve() arg 3 must be a mapping object");
4306 goto fail_0;
4307 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004308
Victor Stinner8c62be82010-05-06 00:08:46 +00004309 argvlist = PyMem_NEW(char *, argc+1);
4310 if (argvlist == NULL) {
4311 PyErr_NoMemory();
4312 goto fail_0;
4313 }
4314 for (i = 0; i < argc; i++) {
4315 if (!fsconvert_strdup((*getitem)(argv, i),
4316 &argvlist[i]))
4317 {
4318 lastarg = i;
4319 goto fail_1;
4320 }
4321 }
4322 lastarg = argc;
4323 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004324
Victor Stinner8c62be82010-05-06 00:08:46 +00004325 envlist = parse_envlist(env, &envc);
4326 if (envlist == NULL)
4327 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004328
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004329#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004330 Py_BEGIN_ALLOW_THREADS
4331 spawnval = spawnve(mode, path, argvlist, envlist);
4332 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004333#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 if (mode == _OLD_P_OVERLAY)
4335 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004336
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 Py_BEGIN_ALLOW_THREADS
4338 spawnval = _spawnve(mode, path, argvlist, envlist);
4339 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004340#endif
Tim Peters25059d32001-12-07 20:35:43 +00004341
Victor Stinner8c62be82010-05-06 00:08:46 +00004342 if (spawnval == -1)
4343 (void) posix_error();
4344 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004345#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004347#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004348 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004349#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004350
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 while (--envc >= 0)
4352 PyMem_DEL(envlist[envc]);
4353 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004354 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004355 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004356 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004357 Py_DECREF(opath);
4358 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004359}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004360
4361/* OS/2 supports spawnvp & spawnvpe natively */
4362#if defined(PYOS_OS2)
4363PyDoc_STRVAR(posix_spawnvp__doc__,
4364"spawnvp(mode, file, args)\n\n\
4365Execute the program 'file' in a new process, using the environment\n\
4366search path to find the file.\n\
4367\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004368 mode: mode of process creation\n\
4369 file: executable file name\n\
4370 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004371
4372static PyObject *
4373posix_spawnvp(PyObject *self, PyObject *args)
4374{
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 PyObject *opath;
4376 char *path;
4377 PyObject *argv;
4378 char **argvlist;
4379 int mode, i, argc;
4380 Py_intptr_t spawnval;
4381 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004382
Victor Stinner8c62be82010-05-06 00:08:46 +00004383 /* spawnvp has three arguments: (mode, path, argv), where
4384 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004385
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4387 PyUnicode_FSConverter,
4388 &opath, &argv))
4389 return NULL;
4390 path = PyBytes_AsString(opath);
4391 if (PyList_Check(argv)) {
4392 argc = PyList_Size(argv);
4393 getitem = PyList_GetItem;
4394 }
4395 else if (PyTuple_Check(argv)) {
4396 argc = PyTuple_Size(argv);
4397 getitem = PyTuple_GetItem;
4398 }
4399 else {
4400 PyErr_SetString(PyExc_TypeError,
4401 "spawnvp() arg 2 must be a tuple or list");
4402 Py_DECREF(opath);
4403 return NULL;
4404 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004405
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 argvlist = PyMem_NEW(char *, argc+1);
4407 if (argvlist == NULL) {
4408 Py_DECREF(opath);
4409 return PyErr_NoMemory();
4410 }
4411 for (i = 0; i < argc; i++) {
4412 if (!fsconvert_strdup((*getitem)(argv, i),
4413 &argvlist[i])) {
4414 free_string_array(argvlist, i);
4415 PyErr_SetString(
4416 PyExc_TypeError,
4417 "spawnvp() arg 2 must contain only strings");
4418 Py_DECREF(opath);
4419 return NULL;
4420 }
4421 }
4422 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004423
Victor Stinner8c62be82010-05-06 00:08:46 +00004424 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004425#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004426 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004428 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004430 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004431
Victor Stinner8c62be82010-05-06 00:08:46 +00004432 free_string_array(argvlist, argc);
4433 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004434
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 if (spawnval == -1)
4436 return posix_error();
4437 else
4438 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004439}
4440
4441
4442PyDoc_STRVAR(posix_spawnvpe__doc__,
4443"spawnvpe(mode, file, args, env)\n\n\
4444Execute the program 'file' in a new process, using the environment\n\
4445search path to find the file.\n\
4446\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004447 mode: mode of process creation\n\
4448 file: executable file name\n\
4449 args: tuple or list of arguments\n\
4450 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004451
4452static PyObject *
4453posix_spawnvpe(PyObject *self, PyObject *args)
4454{
Victor Stinner8c62be82010-05-06 00:08:46 +00004455 PyObject *opath
4456 char *path;
4457 PyObject *argv, *env;
4458 char **argvlist;
4459 char **envlist;
4460 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004461 int mode;
4462 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004463 Py_intptr_t spawnval;
4464 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4465 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004466
Victor Stinner8c62be82010-05-06 00:08:46 +00004467 /* spawnvpe has four arguments: (mode, path, argv, env), where
4468 argv is a list or tuple of strings and env is a dictionary
4469 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004470
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4472 PyUnicode_FSConverter,
4473 &opath, &argv, &env))
4474 return NULL;
4475 path = PyBytes_AsString(opath);
4476 if (PyList_Check(argv)) {
4477 argc = PyList_Size(argv);
4478 getitem = PyList_GetItem;
4479 }
4480 else if (PyTuple_Check(argv)) {
4481 argc = PyTuple_Size(argv);
4482 getitem = PyTuple_GetItem;
4483 }
4484 else {
4485 PyErr_SetString(PyExc_TypeError,
4486 "spawnvpe() arg 2 must be a tuple or list");
4487 goto fail_0;
4488 }
4489 if (!PyMapping_Check(env)) {
4490 PyErr_SetString(PyExc_TypeError,
4491 "spawnvpe() arg 3 must be a mapping object");
4492 goto fail_0;
4493 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004494
Victor Stinner8c62be82010-05-06 00:08:46 +00004495 argvlist = PyMem_NEW(char *, argc+1);
4496 if (argvlist == NULL) {
4497 PyErr_NoMemory();
4498 goto fail_0;
4499 }
4500 for (i = 0; i < argc; i++) {
4501 if (!fsconvert_strdup((*getitem)(argv, i),
4502 &argvlist[i]))
4503 {
4504 lastarg = i;
4505 goto fail_1;
4506 }
4507 }
4508 lastarg = argc;
4509 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004510
Victor Stinner8c62be82010-05-06 00:08:46 +00004511 envlist = parse_envlist(env, &envc);
4512 if (envlist == NULL)
4513 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004514
Victor Stinner8c62be82010-05-06 00:08:46 +00004515 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004516#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004517 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004518#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004519 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004520#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004521 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004522
Victor Stinner8c62be82010-05-06 00:08:46 +00004523 if (spawnval == -1)
4524 (void) posix_error();
4525 else
4526 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004527
Victor Stinner8c62be82010-05-06 00:08:46 +00004528 while (--envc >= 0)
4529 PyMem_DEL(envlist[envc]);
4530 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004531 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004532 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004533 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004534 Py_DECREF(opath);
4535 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004536}
4537#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004538#endif /* HAVE_SPAWNV */
4539
4540
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004541#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004542PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004543"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004544Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4545\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004546Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004547
4548static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004549posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004550{
Victor Stinner8c62be82010-05-06 00:08:46 +00004551 pid_t pid;
4552 int result = 0;
4553 _PyImport_AcquireLock();
4554 pid = fork1();
4555 if (pid == 0) {
4556 /* child: this clobbers and resets the import lock. */
4557 PyOS_AfterFork();
4558 } else {
4559 /* parent: release the import lock. */
4560 result = _PyImport_ReleaseLock();
4561 }
4562 if (pid == -1)
4563 return posix_error();
4564 if (result < 0) {
4565 /* Don't clobber the OSError if the fork failed. */
4566 PyErr_SetString(PyExc_RuntimeError,
4567 "not holding the import lock");
4568 return NULL;
4569 }
4570 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004571}
4572#endif
4573
4574
Guido van Rossumad0ee831995-03-01 10:34:45 +00004575#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004576PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004577"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004578Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004579Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004580
Barry Warsaw53699e91996-12-10 23:23:01 +00004581static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004582posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004583{
Victor Stinner8c62be82010-05-06 00:08:46 +00004584 pid_t pid;
4585 int result = 0;
4586 _PyImport_AcquireLock();
4587 pid = fork();
4588 if (pid == 0) {
4589 /* child: this clobbers and resets the import lock. */
4590 PyOS_AfterFork();
4591 } else {
4592 /* parent: release the import lock. */
4593 result = _PyImport_ReleaseLock();
4594 }
4595 if (pid == -1)
4596 return posix_error();
4597 if (result < 0) {
4598 /* Don't clobber the OSError if the fork failed. */
4599 PyErr_SetString(PyExc_RuntimeError,
4600 "not holding the import lock");
4601 return NULL;
4602 }
4603 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004604}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004605#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004606
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004607#ifdef HAVE_SCHED_H
4608
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004609#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4610
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004611PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4612"sched_get_priority_max(policy)\n\n\
4613Get the maximum scheduling priority for *policy*.");
4614
4615static PyObject *
4616posix_sched_get_priority_max(PyObject *self, PyObject *args)
4617{
4618 int policy, max;
4619
4620 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4621 return NULL;
4622 max = sched_get_priority_max(policy);
4623 if (max < 0)
4624 return posix_error();
4625 return PyLong_FromLong(max);
4626}
4627
4628PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4629"sched_get_priority_min(policy)\n\n\
4630Get the minimum scheduling priority for *policy*.");
4631
4632static PyObject *
4633posix_sched_get_priority_min(PyObject *self, PyObject *args)
4634{
4635 int policy, min;
4636
4637 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4638 return NULL;
4639 min = sched_get_priority_min(policy);
4640 if (min < 0)
4641 return posix_error();
4642 return PyLong_FromLong(min);
4643}
4644
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004645#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4646
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004647#ifdef HAVE_SCHED_SETSCHEDULER
4648
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004649PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4650"sched_getscheduler(pid)\n\n\
4651Get the scheduling policy for the process with a PID of *pid*.\n\
4652Passing a PID of 0 returns the scheduling policy for the calling process.");
4653
4654static PyObject *
4655posix_sched_getscheduler(PyObject *self, PyObject *args)
4656{
4657 pid_t pid;
4658 int policy;
4659
4660 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4661 return NULL;
4662 policy = sched_getscheduler(pid);
4663 if (policy < 0)
4664 return posix_error();
4665 return PyLong_FromLong(policy);
4666}
4667
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004668#endif
4669
4670#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4671
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004672static PyObject *
4673sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4674{
4675 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004676 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004677
4678 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4679 return NULL;
4680 res = PyStructSequence_New(type);
4681 if (!res)
4682 return NULL;
4683 Py_INCREF(priority);
4684 PyStructSequence_SET_ITEM(res, 0, priority);
4685 return res;
4686}
4687
4688PyDoc_STRVAR(sched_param__doc__,
4689"sched_param(sched_priority): A scheduling parameter.\n\n\
4690Current has only one field: sched_priority");
4691
4692static PyStructSequence_Field sched_param_fields[] = {
4693 {"sched_priority", "the scheduling priority"},
4694 {0}
4695};
4696
4697static PyStructSequence_Desc sched_param_desc = {
4698 "sched_param", /* name */
4699 sched_param__doc__, /* doc */
4700 sched_param_fields,
4701 1
4702};
4703
4704static int
4705convert_sched_param(PyObject *param, struct sched_param *res)
4706{
4707 long priority;
4708
4709 if (Py_TYPE(param) != &SchedParamType) {
4710 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4711 return 0;
4712 }
4713 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4714 if (priority == -1 && PyErr_Occurred())
4715 return 0;
4716 if (priority > INT_MAX || priority < INT_MIN) {
4717 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4718 return 0;
4719 }
4720 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4721 return 1;
4722}
4723
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004724#endif
4725
4726#ifdef HAVE_SCHED_SETSCHEDULER
4727
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004728PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4729"sched_setscheduler(pid, policy, param)\n\n\
4730Set the scheduling policy, *policy*, for *pid*.\n\
4731If *pid* is 0, the calling process is changed.\n\
4732*param* is an instance of sched_param.");
4733
4734static PyObject *
4735posix_sched_setscheduler(PyObject *self, PyObject *args)
4736{
4737 pid_t pid;
4738 int policy;
4739 struct sched_param param;
4740
4741 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4742 &pid, &policy, &convert_sched_param, &param))
4743 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004744
4745 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004746 ** sched_setscheduler() returns 0 in Linux, but the previous
4747 ** scheduling policy under Solaris/Illumos, and others.
4748 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004749 */
4750 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004751 return posix_error();
4752 Py_RETURN_NONE;
4753}
4754
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004755#endif
4756
4757#ifdef HAVE_SCHED_SETPARAM
4758
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004759PyDoc_STRVAR(posix_sched_getparam__doc__,
4760"sched_getparam(pid) -> sched_param\n\n\
4761Returns scheduling parameters for the process with *pid* as an instance of the\n\
4762sched_param class. A PID of 0 means the calling process.");
4763
4764static PyObject *
4765posix_sched_getparam(PyObject *self, PyObject *args)
4766{
4767 pid_t pid;
4768 struct sched_param param;
4769 PyObject *res, *priority;
4770
4771 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4772 return NULL;
4773 if (sched_getparam(pid, &param))
4774 return posix_error();
4775 res = PyStructSequence_New(&SchedParamType);
4776 if (!res)
4777 return NULL;
4778 priority = PyLong_FromLong(param.sched_priority);
4779 if (!priority) {
4780 Py_DECREF(res);
4781 return NULL;
4782 }
4783 PyStructSequence_SET_ITEM(res, 0, priority);
4784 return res;
4785}
4786
4787PyDoc_STRVAR(posix_sched_setparam__doc__,
4788"sched_setparam(pid, param)\n\n\
4789Set scheduling parameters for a process with PID *pid*.\n\
4790A PID of 0 means the calling process.");
4791
4792static PyObject *
4793posix_sched_setparam(PyObject *self, PyObject *args)
4794{
4795 pid_t pid;
4796 struct sched_param param;
4797
4798 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4799 &pid, &convert_sched_param, &param))
4800 return NULL;
4801 if (sched_setparam(pid, &param))
4802 return posix_error();
4803 Py_RETURN_NONE;
4804}
4805
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004806#endif
4807
4808#ifdef HAVE_SCHED_RR_GET_INTERVAL
4809
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004810PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4811"sched_rr_get_interval(pid) -> float\n\n\
4812Return the round-robin quantum for the process with PID *pid* in seconds.");
4813
4814static PyObject *
4815posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4816{
4817 pid_t pid;
4818 struct timespec interval;
4819
4820 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4821 return NULL;
4822 if (sched_rr_get_interval(pid, &interval))
4823 return posix_error();
4824 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4825}
4826
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004827#endif
4828
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004829PyDoc_STRVAR(posix_sched_yield__doc__,
4830"sched_yield()\n\n\
4831Voluntarily relinquish the CPU.");
4832
4833static PyObject *
4834posix_sched_yield(PyObject *self, PyObject *noargs)
4835{
4836 if (sched_yield())
4837 return posix_error();
4838 Py_RETURN_NONE;
4839}
4840
Benjamin Peterson2740af82011-08-02 17:41:34 -05004841#ifdef HAVE_SCHED_SETAFFINITY
4842
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004843typedef struct {
4844 PyObject_HEAD;
4845 Py_ssize_t size;
4846 int ncpus;
4847 cpu_set_t *set;
4848} Py_cpu_set;
4849
4850static PyTypeObject cpu_set_type;
4851
4852static void
4853cpu_set_dealloc(Py_cpu_set *set)
4854{
4855 assert(set->set);
4856 CPU_FREE(set->set);
4857 Py_TYPE(set)->tp_free(set);
4858}
4859
4860static Py_cpu_set *
4861make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4862{
4863 Py_cpu_set *set;
4864
4865 if (size < 0) {
4866 PyErr_SetString(PyExc_ValueError, "negative size");
4867 return NULL;
4868 }
4869 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4870 if (!set)
4871 return NULL;
4872 set->ncpus = size;
4873 set->size = CPU_ALLOC_SIZE(size);
4874 set->set = CPU_ALLOC(size);
4875 if (!set->set) {
4876 type->tp_free(set);
4877 PyErr_NoMemory();
4878 return NULL;
4879 }
4880 CPU_ZERO_S(set->size, set->set);
4881 return set;
4882}
4883
4884static PyObject *
4885cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4886{
4887 int size;
4888
4889 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4890 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4891 return NULL;
4892 return (PyObject *)make_new_cpu_set(type, size);
4893}
4894
4895static PyObject *
4896cpu_set_repr(Py_cpu_set *set)
4897{
4898 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4899}
4900
4901static Py_ssize_t
4902cpu_set_len(Py_cpu_set *set)
4903{
4904 return set->ncpus;
4905}
4906
4907static int
4908_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4909{
4910 int cpu;
4911 if (!PyArg_ParseTuple(args, requester, &cpu))
4912 return -1;
4913 if (cpu < 0) {
4914 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4915 return -1;
4916 }
4917 if (cpu >= set->ncpus) {
4918 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4919 return -1;
4920 }
4921 return cpu;
4922}
4923
4924PyDoc_STRVAR(cpu_set_set_doc,
4925"cpu_set.set(i)\n\n\
4926Add CPU *i* to the set.");
4927
4928static PyObject *
4929cpu_set_set(Py_cpu_set *set, PyObject *args)
4930{
4931 int cpu = _get_cpu(set, "i|set", args);
4932 if (cpu == -1)
4933 return NULL;
4934 CPU_SET_S(cpu, set->size, set->set);
4935 Py_RETURN_NONE;
4936}
4937
4938PyDoc_STRVAR(cpu_set_count_doc,
4939"cpu_set.count() -> int\n\n\
4940Return the number of CPUs active in the set.");
4941
4942static PyObject *
4943cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4944{
4945 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4946}
4947
4948PyDoc_STRVAR(cpu_set_clear_doc,
4949"cpu_set.clear(i)\n\n\
4950Remove CPU *i* from the set.");
4951
4952static PyObject *
4953cpu_set_clear(Py_cpu_set *set, PyObject *args)
4954{
4955 int cpu = _get_cpu(set, "i|clear", args);
4956 if (cpu == -1)
4957 return NULL;
4958 CPU_CLR_S(cpu, set->size, set->set);
4959 Py_RETURN_NONE;
4960}
4961
4962PyDoc_STRVAR(cpu_set_isset_doc,
4963"cpu_set.isset(i) -> bool\n\n\
4964Test if CPU *i* is in the set.");
4965
4966static PyObject *
4967cpu_set_isset(Py_cpu_set *set, PyObject *args)
4968{
4969 int cpu = _get_cpu(set, "i|isset", args);
4970 if (cpu == -1)
4971 return NULL;
4972 if (CPU_ISSET_S(cpu, set->size, set->set))
4973 Py_RETURN_TRUE;
4974 Py_RETURN_FALSE;
4975}
4976
4977PyDoc_STRVAR(cpu_set_zero_doc,
4978"cpu_set.zero()\n\n\
4979Clear the cpu_set.");
4980
4981static PyObject *
4982cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4983{
4984 CPU_ZERO_S(set->size, set->set);
4985 Py_RETURN_NONE;
4986}
4987
4988static PyObject *
4989cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4990{
4991 int eq;
4992
Brian Curtindfc80e32011-08-10 20:28:54 -05004993 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
4994 Py_RETURN_NOTIMPLEMENTED;
4995
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004996 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4997 if ((op == Py_EQ) ? eq : !eq)
4998 Py_RETURN_TRUE;
4999 else
5000 Py_RETURN_FALSE;
5001}
5002
5003#define CPU_SET_BINOP(name, op) \
5004 static PyObject * \
5005 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5006 if (res) { \
5007 Py_INCREF(res); \
5008 } \
5009 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005010 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005011 if (!res) \
5012 return NULL; \
5013 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005014 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005015 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005016 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005017 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005018 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005019 op(res->size, res->set, left->set, right->set); \
5020 return (PyObject *)res; \
5021 } \
5022 static PyObject * \
5023 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5024 return do_cpu_set_##name(left, right, NULL); \
5025 } \
5026 static PyObject * \
5027 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5028 return do_cpu_set_##name(left, right, left); \
5029 } \
5030
5031CPU_SET_BINOP(and, CPU_AND_S)
5032CPU_SET_BINOP(or, CPU_OR_S)
5033CPU_SET_BINOP(xor, CPU_XOR_S)
5034#undef CPU_SET_BINOP
5035
5036PyDoc_STRVAR(cpu_set_doc,
5037"cpu_set(size)\n\n\
5038Create an empty mask of CPUs.");
5039
5040static PyNumberMethods cpu_set_as_number = {
5041 0, /*nb_add*/
5042 0, /*nb_subtract*/
5043 0, /*nb_multiply*/
5044 0, /*nb_remainder*/
5045 0, /*nb_divmod*/
5046 0, /*nb_power*/
5047 0, /*nb_negative*/
5048 0, /*nb_positive*/
5049 0, /*nb_absolute*/
5050 0, /*nb_bool*/
5051 0, /*nb_invert*/
5052 0, /*nb_lshift*/
5053 0, /*nb_rshift*/
5054 (binaryfunc)cpu_set_and, /*nb_and*/
5055 (binaryfunc)cpu_set_xor, /*nb_xor*/
5056 (binaryfunc)cpu_set_or, /*nb_or*/
5057 0, /*nb_int*/
5058 0, /*nb_reserved*/
5059 0, /*nb_float*/
5060 0, /*nb_inplace_add*/
5061 0, /*nb_inplace_subtract*/
5062 0, /*nb_inplace_multiply*/
5063 0, /*nb_inplace_remainder*/
5064 0, /*nb_inplace_power*/
5065 0, /*nb_inplace_lshift*/
5066 0, /*nb_inplace_rshift*/
5067 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5068 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5069 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5070};
5071
5072static PySequenceMethods cpu_set_as_sequence = {
5073 (lenfunc)cpu_set_len, /* sq_length */
5074};
5075
5076static PyMethodDef cpu_set_methods[] = {
5077 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5078 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5079 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5080 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5081 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5082 {NULL, NULL} /* sentinel */
5083};
5084
5085static PyTypeObject cpu_set_type = {
5086 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5087 "posix.cpu_set", /* tp_name */
5088 sizeof(Py_cpu_set), /* tp_basicsize */
5089 0, /* tp_itemsize */
5090 /* methods */
5091 (destructor)cpu_set_dealloc, /* tp_dealloc */
5092 0, /* tp_print */
5093 0, /* tp_getattr */
5094 0, /* tp_setattr */
5095 0, /* tp_reserved */
5096 (reprfunc)cpu_set_repr, /* tp_repr */
5097 &cpu_set_as_number, /* tp_as_number */
5098 &cpu_set_as_sequence, /* tp_as_sequence */
5099 0, /* tp_as_mapping */
5100 PyObject_HashNotImplemented, /* tp_hash */
5101 0, /* tp_call */
5102 0, /* tp_str */
5103 PyObject_GenericGetAttr, /* tp_getattro */
5104 0, /* tp_setattro */
5105 0, /* tp_as_buffer */
5106 Py_TPFLAGS_DEFAULT, /* tp_flags */
5107 cpu_set_doc, /* tp_doc */
5108 0, /* tp_traverse */
5109 0, /* tp_clear */
5110 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5111 0, /* tp_weaklistoffset */
5112 0, /* tp_iter */
5113 0, /* tp_iternext */
5114 cpu_set_methods, /* tp_methods */
5115 0, /* tp_members */
5116 0, /* tp_getset */
5117 0, /* tp_base */
5118 0, /* tp_dict */
5119 0, /* tp_descr_get */
5120 0, /* tp_descr_set */
5121 0, /* tp_dictoffset */
5122 0, /* tp_init */
5123 PyType_GenericAlloc, /* tp_alloc */
5124 cpu_set_new, /* tp_new */
5125 PyObject_Del, /* tp_free */
5126};
5127
5128PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5129"sched_setaffinity(pid, cpu_set)\n\n\
5130Set the affinity of the process with PID *pid* to *cpu_set*.");
5131
5132static PyObject *
5133posix_sched_setaffinity(PyObject *self, PyObject *args)
5134{
5135 pid_t pid;
5136 Py_cpu_set *cpu_set;
5137
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005138 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005139 &pid, &cpu_set_type, &cpu_set))
5140 return NULL;
5141 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5142 return posix_error();
5143 Py_RETURN_NONE;
5144}
5145
5146PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5147"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5148Return the affinity of the process with PID *pid*.\n\
5149The returned cpu_set will be of size *ncpus*.");
5150
5151static PyObject *
5152posix_sched_getaffinity(PyObject *self, PyObject *args)
5153{
5154 pid_t pid;
5155 int ncpus;
5156 Py_cpu_set *res;
5157
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005158 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005159 &pid, &ncpus))
5160 return NULL;
5161 res = make_new_cpu_set(&cpu_set_type, ncpus);
5162 if (!res)
5163 return NULL;
5164 if (sched_getaffinity(pid, res->size, res->set)) {
5165 Py_DECREF(res);
5166 return posix_error();
5167 }
5168 return (PyObject *)res;
5169}
5170
Benjamin Peterson2740af82011-08-02 17:41:34 -05005171#endif /* HAVE_SCHED_SETAFFINITY */
5172
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005173#endif /* HAVE_SCHED_H */
5174
Neal Norwitzb59798b2003-03-21 01:43:31 +00005175/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005176/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5177#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005178#define DEV_PTY_FILE "/dev/ptc"
5179#define HAVE_DEV_PTMX
5180#else
5181#define DEV_PTY_FILE "/dev/ptmx"
5182#endif
5183
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005184#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005185#ifdef HAVE_PTY_H
5186#include <pty.h>
5187#else
5188#ifdef HAVE_LIBUTIL_H
5189#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005190#else
5191#ifdef HAVE_UTIL_H
5192#include <util.h>
5193#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005194#endif /* HAVE_LIBUTIL_H */
5195#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005196#ifdef HAVE_STROPTS_H
5197#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005198#endif
5199#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005200
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005201#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005202PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005203"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005204Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005205
5206static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005207posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005208{
Victor Stinner8c62be82010-05-06 00:08:46 +00005209 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005210#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005211 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005212#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005213#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005214 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005215#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005217#endif
5218#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005219
Thomas Wouters70c21a12000-07-14 14:28:33 +00005220#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005221 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5222 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005223#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005224 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5225 if (slave_name == NULL)
5226 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005227
Victor Stinner8c62be82010-05-06 00:08:46 +00005228 slave_fd = open(slave_name, O_RDWR);
5229 if (slave_fd < 0)
5230 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005231#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005232 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5233 if (master_fd < 0)
5234 return posix_error();
5235 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5236 /* change permission of slave */
5237 if (grantpt(master_fd) < 0) {
5238 PyOS_setsig(SIGCHLD, sig_saved);
5239 return posix_error();
5240 }
5241 /* unlock slave */
5242 if (unlockpt(master_fd) < 0) {
5243 PyOS_setsig(SIGCHLD, sig_saved);
5244 return posix_error();
5245 }
5246 PyOS_setsig(SIGCHLD, sig_saved);
5247 slave_name = ptsname(master_fd); /* get name of slave */
5248 if (slave_name == NULL)
5249 return posix_error();
5250 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5251 if (slave_fd < 0)
5252 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005253#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005254 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5255 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005256#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005257 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005258#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005259#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005260#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005261
Victor Stinner8c62be82010-05-06 00:08:46 +00005262 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005263
Fred Drake8cef4cf2000-06-28 16:40:38 +00005264}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005265#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005266
5267#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005268PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005269"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005270Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5271Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005273
5274static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005275posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005276{
Victor Stinner8c62be82010-05-06 00:08:46 +00005277 int master_fd = -1, result = 0;
5278 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005279
Victor Stinner8c62be82010-05-06 00:08:46 +00005280 _PyImport_AcquireLock();
5281 pid = forkpty(&master_fd, NULL, NULL, NULL);
5282 if (pid == 0) {
5283 /* child: this clobbers and resets the import lock. */
5284 PyOS_AfterFork();
5285 } else {
5286 /* parent: release the import lock. */
5287 result = _PyImport_ReleaseLock();
5288 }
5289 if (pid == -1)
5290 return posix_error();
5291 if (result < 0) {
5292 /* Don't clobber the OSError if the fork failed. */
5293 PyErr_SetString(PyExc_RuntimeError,
5294 "not holding the import lock");
5295 return NULL;
5296 }
5297 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005298}
5299#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005300
Ross Lagerwall7807c352011-03-17 20:20:30 +02005301
Guido van Rossumad0ee831995-03-01 10:34:45 +00005302#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005303PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005304"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005305Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005306
Barry Warsaw53699e91996-12-10 23:23:01 +00005307static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005308posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005309{
Victor Stinner8c62be82010-05-06 00:08:46 +00005310 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005311}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005312#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005313
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005314
Guido van Rossumad0ee831995-03-01 10:34:45 +00005315#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005316PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005317"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005318Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005319
Barry Warsaw53699e91996-12-10 23:23:01 +00005320static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005321posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005322{
Victor Stinner8c62be82010-05-06 00:08:46 +00005323 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005324}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005325#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005326
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005327
Guido van Rossumad0ee831995-03-01 10:34:45 +00005328#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005329PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005330"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005331Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005332
Barry Warsaw53699e91996-12-10 23:23:01 +00005333static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005334posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005335{
Victor Stinner8c62be82010-05-06 00:08:46 +00005336 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005337}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005338#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005341PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005342"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005343Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005344
Barry Warsaw53699e91996-12-10 23:23:01 +00005345static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005346posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005347{
Victor Stinner8c62be82010-05-06 00:08:46 +00005348 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005349}
5350
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005351#ifdef HAVE_GETGROUPLIST
5352PyDoc_STRVAR(posix_getgrouplist__doc__,
5353"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5354Returns a list of groups to which a user belongs.\n\n\
5355 user: username to lookup\n\
5356 group: base group id of the user");
5357
5358static PyObject *
5359posix_getgrouplist(PyObject *self, PyObject *args)
5360{
5361#ifdef NGROUPS_MAX
5362#define MAX_GROUPS NGROUPS_MAX
5363#else
5364 /* defined to be 16 on Solaris7, so this should be a small number */
5365#define MAX_GROUPS 64
5366#endif
5367
5368 const char *user;
5369 int i, ngroups;
5370 PyObject *list;
5371#ifdef __APPLE__
5372 int *groups, basegid;
5373#else
5374 gid_t *groups, basegid;
5375#endif
5376 ngroups = MAX_GROUPS;
5377
5378 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5379 return NULL;
5380
5381#ifdef __APPLE__
5382 groups = PyMem_Malloc(ngroups * sizeof(int));
5383#else
5384 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5385#endif
5386 if (groups == NULL)
5387 return PyErr_NoMemory();
5388
5389 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5390 PyMem_Del(groups);
5391 return posix_error();
5392 }
5393
5394 list = PyList_New(ngroups);
5395 if (list == NULL) {
5396 PyMem_Del(groups);
5397 return NULL;
5398 }
5399
5400 for (i = 0; i < ngroups; i++) {
5401 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5402 if (o == NULL) {
5403 Py_DECREF(list);
5404 PyMem_Del(groups);
5405 return NULL;
5406 }
5407 PyList_SET_ITEM(list, i, o);
5408 }
5409
5410 PyMem_Del(groups);
5411
5412 return list;
5413}
5414#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005415
Fred Drakec9680921999-12-13 16:37:25 +00005416#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005417PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005418"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005419Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005420
5421static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005422posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005423{
5424 PyObject *result = NULL;
5425
Fred Drakec9680921999-12-13 16:37:25 +00005426#ifdef NGROUPS_MAX
5427#define MAX_GROUPS NGROUPS_MAX
5428#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005429 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005430#define MAX_GROUPS 64
5431#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005432 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005433
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005434 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005435 * This is a helper variable to store the intermediate result when
5436 * that happens.
5437 *
5438 * To keep the code readable the OSX behaviour is unconditional,
5439 * according to the POSIX spec this should be safe on all unix-y
5440 * systems.
5441 */
5442 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005443 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005444
Victor Stinner8c62be82010-05-06 00:08:46 +00005445 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005446 if (n < 0) {
5447 if (errno == EINVAL) {
5448 n = getgroups(0, NULL);
5449 if (n == -1) {
5450 return posix_error();
5451 }
5452 if (n == 0) {
5453 /* Avoid malloc(0) */
5454 alt_grouplist = grouplist;
5455 } else {
5456 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5457 if (alt_grouplist == NULL) {
5458 errno = EINVAL;
5459 return posix_error();
5460 }
5461 n = getgroups(n, alt_grouplist);
5462 if (n == -1) {
5463 PyMem_Free(alt_grouplist);
5464 return posix_error();
5465 }
5466 }
5467 } else {
5468 return posix_error();
5469 }
5470 }
5471 result = PyList_New(n);
5472 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005473 int i;
5474 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005475 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005476 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005477 Py_DECREF(result);
5478 result = NULL;
5479 break;
Fred Drakec9680921999-12-13 16:37:25 +00005480 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005481 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005482 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005483 }
5484
5485 if (alt_grouplist != grouplist) {
5486 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005488
Fred Drakec9680921999-12-13 16:37:25 +00005489 return result;
5490}
5491#endif
5492
Antoine Pitroub7572f02009-12-02 20:46:48 +00005493#ifdef HAVE_INITGROUPS
5494PyDoc_STRVAR(posix_initgroups__doc__,
5495"initgroups(username, gid) -> None\n\n\
5496Call the system initgroups() to initialize the group access list with all of\n\
5497the groups of which the specified username is a member, plus the specified\n\
5498group id.");
5499
5500static PyObject *
5501posix_initgroups(PyObject *self, PyObject *args)
5502{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005503 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005504 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005505 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005506 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005507
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005508 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5509 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005510 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005511 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005512
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005513 res = initgroups(username, (gid_t) gid);
5514 Py_DECREF(oname);
5515 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005516 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005517
Victor Stinner8c62be82010-05-06 00:08:46 +00005518 Py_INCREF(Py_None);
5519 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005520}
5521#endif
5522
Martin v. Löwis606edc12002-06-13 21:09:11 +00005523#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005524PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005525"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005526Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005527
5528static PyObject *
5529posix_getpgid(PyObject *self, PyObject *args)
5530{
Victor Stinner8c62be82010-05-06 00:08:46 +00005531 pid_t pid, pgid;
5532 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5533 return NULL;
5534 pgid = getpgid(pid);
5535 if (pgid < 0)
5536 return posix_error();
5537 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005538}
5539#endif /* HAVE_GETPGID */
5540
5541
Guido van Rossumb6775db1994-08-01 11:34:53 +00005542#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005544"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005545Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005546
Barry Warsaw53699e91996-12-10 23:23:01 +00005547static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005548posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005549{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005550#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005551 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005552#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005553 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005554#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005555}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005556#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005557
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005558
Guido van Rossumb6775db1994-08-01 11:34:53 +00005559#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005561"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005562Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005563
Barry Warsaw53699e91996-12-10 23:23:01 +00005564static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005565posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005566{
Guido van Rossum64933891994-10-20 21:56:42 +00005567#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005568 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005569#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005570 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005571#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005572 return posix_error();
5573 Py_INCREF(Py_None);
5574 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005575}
5576
Guido van Rossumb6775db1994-08-01 11:34:53 +00005577#endif /* HAVE_SETPGRP */
5578
Guido van Rossumad0ee831995-03-01 10:34:45 +00005579#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005580
5581#ifdef MS_WINDOWS
5582#include <tlhelp32.h>
5583
5584static PyObject*
5585win32_getppid()
5586{
5587 HANDLE snapshot;
5588 pid_t mypid;
5589 PyObject* result = NULL;
5590 BOOL have_record;
5591 PROCESSENTRY32 pe;
5592
5593 mypid = getpid(); /* This function never fails */
5594
5595 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5596 if (snapshot == INVALID_HANDLE_VALUE)
5597 return PyErr_SetFromWindowsErr(GetLastError());
5598
5599 pe.dwSize = sizeof(pe);
5600 have_record = Process32First(snapshot, &pe);
5601 while (have_record) {
5602 if (mypid == (pid_t)pe.th32ProcessID) {
5603 /* We could cache the ulong value in a static variable. */
5604 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5605 break;
5606 }
5607
5608 have_record = Process32Next(snapshot, &pe);
5609 }
5610
5611 /* If our loop exits and our pid was not found (result will be NULL)
5612 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5613 * error anyway, so let's raise it. */
5614 if (!result)
5615 result = PyErr_SetFromWindowsErr(GetLastError());
5616
5617 CloseHandle(snapshot);
5618
5619 return result;
5620}
5621#endif /*MS_WINDOWS*/
5622
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005623PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005624"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005625Return the parent's process id. If the parent process has already exited,\n\
5626Windows machines will still return its id; others systems will return the id\n\
5627of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005628
Barry Warsaw53699e91996-12-10 23:23:01 +00005629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005630posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005631{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005632#ifdef MS_WINDOWS
5633 return win32_getppid();
5634#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005636#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005637}
5638#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005640
Fred Drake12c6e2d1999-12-14 21:25:03 +00005641#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005643"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005644Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005645
5646static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005647posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005648{
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005650#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005651 wchar_t user_name[UNLEN + 1];
5652 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5653
5654 if (GetUserNameW(user_name, &num_chars)) {
5655 /* num_chars is the number of unicode chars plus null terminator */
5656 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005657 }
5658 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005659 result = PyErr_SetFromWindowsErr(GetLastError());
5660#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005661 char *name;
5662 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005663
Victor Stinner8c62be82010-05-06 00:08:46 +00005664 errno = 0;
5665 name = getlogin();
5666 if (name == NULL) {
5667 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005668 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005669 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005670 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005671 }
5672 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005673 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005674 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005675#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005676 return result;
5677}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005678#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005679
Guido van Rossumad0ee831995-03-01 10:34:45 +00005680#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005682"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005684
Barry Warsaw53699e91996-12-10 23:23:01 +00005685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005686posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005687{
Victor Stinner8c62be82010-05-06 00:08:46 +00005688 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005689}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005690#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005692
Guido van Rossumad0ee831995-03-01 10:34:45 +00005693#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005694PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005695"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005696Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005697
Barry Warsaw53699e91996-12-10 23:23:01 +00005698static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005699posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005700{
Victor Stinner8c62be82010-05-06 00:08:46 +00005701 pid_t pid;
5702 int sig;
5703 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5704 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005705#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005706 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5707 APIRET rc;
5708 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005709 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005710
5711 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5712 APIRET rc;
5713 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005714 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005715
5716 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005717 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005718#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 if (kill(pid, sig) == -1)
5720 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005721#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005722 Py_INCREF(Py_None);
5723 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005724}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005725#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005726
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005727#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005728PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005729"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005730Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005731
5732static PyObject *
5733posix_killpg(PyObject *self, PyObject *args)
5734{
Victor Stinner8c62be82010-05-06 00:08:46 +00005735 int sig;
5736 pid_t pgid;
5737 /* XXX some man pages make the `pgid` parameter an int, others
5738 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5739 take the same type. Moreover, pid_t is always at least as wide as
5740 int (else compilation of this module fails), which is safe. */
5741 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5742 return NULL;
5743 if (killpg(pgid, sig) == -1)
5744 return posix_error();
5745 Py_INCREF(Py_None);
5746 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005747}
5748#endif
5749
Brian Curtineb24d742010-04-12 17:16:38 +00005750#ifdef MS_WINDOWS
5751PyDoc_STRVAR(win32_kill__doc__,
5752"kill(pid, sig)\n\n\
5753Kill a process with a signal.");
5754
5755static PyObject *
5756win32_kill(PyObject *self, PyObject *args)
5757{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005758 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005759 DWORD pid, sig, err;
5760 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005761
Victor Stinner8c62be82010-05-06 00:08:46 +00005762 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5763 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005764
Victor Stinner8c62be82010-05-06 00:08:46 +00005765 /* Console processes which share a common console can be sent CTRL+C or
5766 CTRL+BREAK events, provided they handle said events. */
5767 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5768 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5769 err = GetLastError();
5770 PyErr_SetFromWindowsErr(err);
5771 }
5772 else
5773 Py_RETURN_NONE;
5774 }
Brian Curtineb24d742010-04-12 17:16:38 +00005775
Victor Stinner8c62be82010-05-06 00:08:46 +00005776 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5777 attempt to open and terminate the process. */
5778 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5779 if (handle == NULL) {
5780 err = GetLastError();
5781 return PyErr_SetFromWindowsErr(err);
5782 }
Brian Curtineb24d742010-04-12 17:16:38 +00005783
Victor Stinner8c62be82010-05-06 00:08:46 +00005784 if (TerminateProcess(handle, sig) == 0) {
5785 err = GetLastError();
5786 result = PyErr_SetFromWindowsErr(err);
5787 } else {
5788 Py_INCREF(Py_None);
5789 result = Py_None;
5790 }
Brian Curtineb24d742010-04-12 17:16:38 +00005791
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 CloseHandle(handle);
5793 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005794}
5795#endif /* MS_WINDOWS */
5796
Guido van Rossumc0125471996-06-28 18:55:32 +00005797#ifdef HAVE_PLOCK
5798
5799#ifdef HAVE_SYS_LOCK_H
5800#include <sys/lock.h>
5801#endif
5802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005804"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005806
Barry Warsaw53699e91996-12-10 23:23:01 +00005807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005808posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005809{
Victor Stinner8c62be82010-05-06 00:08:46 +00005810 int op;
5811 if (!PyArg_ParseTuple(args, "i:plock", &op))
5812 return NULL;
5813 if (plock(op) == -1)
5814 return posix_error();
5815 Py_INCREF(Py_None);
5816 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005817}
5818#endif
5819
Guido van Rossumb6775db1994-08-01 11:34:53 +00005820#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005821PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005822"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823Set the current process's user id.");
5824
Barry Warsaw53699e91996-12-10 23:23:01 +00005825static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005826posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005827{
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 long uid_arg;
5829 uid_t uid;
5830 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5831 return NULL;
5832 uid = uid_arg;
5833 if (uid != uid_arg) {
5834 PyErr_SetString(PyExc_OverflowError, "user id too big");
5835 return NULL;
5836 }
5837 if (setuid(uid) < 0)
5838 return posix_error();
5839 Py_INCREF(Py_None);
5840 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005841}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005842#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005843
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005844
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005845#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005846PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005847"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005848Set the current process's effective user id.");
5849
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005850static PyObject *
5851posix_seteuid (PyObject *self, PyObject *args)
5852{
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 long euid_arg;
5854 uid_t euid;
5855 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5856 return NULL;
5857 euid = euid_arg;
5858 if (euid != euid_arg) {
5859 PyErr_SetString(PyExc_OverflowError, "user id too big");
5860 return NULL;
5861 }
5862 if (seteuid(euid) < 0) {
5863 return posix_error();
5864 } else {
5865 Py_INCREF(Py_None);
5866 return Py_None;
5867 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005868}
5869#endif /* HAVE_SETEUID */
5870
5871#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005873"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005874Set the current process's effective group id.");
5875
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005876static PyObject *
5877posix_setegid (PyObject *self, PyObject *args)
5878{
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 long egid_arg;
5880 gid_t egid;
5881 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5882 return NULL;
5883 egid = egid_arg;
5884 if (egid != egid_arg) {
5885 PyErr_SetString(PyExc_OverflowError, "group id too big");
5886 return NULL;
5887 }
5888 if (setegid(egid) < 0) {
5889 return posix_error();
5890 } else {
5891 Py_INCREF(Py_None);
5892 return Py_None;
5893 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005894}
5895#endif /* HAVE_SETEGID */
5896
5897#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005898PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005899"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005900Set the current process's real and effective user ids.");
5901
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005902static PyObject *
5903posix_setreuid (PyObject *self, PyObject *args)
5904{
Victor Stinner8c62be82010-05-06 00:08:46 +00005905 long ruid_arg, euid_arg;
5906 uid_t ruid, euid;
5907 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5908 return NULL;
5909 if (ruid_arg == -1)
5910 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5911 else
5912 ruid = ruid_arg; /* otherwise, assign from our long */
5913 if (euid_arg == -1)
5914 euid = (uid_t)-1;
5915 else
5916 euid = euid_arg;
5917 if ((euid_arg != -1 && euid != euid_arg) ||
5918 (ruid_arg != -1 && ruid != ruid_arg)) {
5919 PyErr_SetString(PyExc_OverflowError, "user id too big");
5920 return NULL;
5921 }
5922 if (setreuid(ruid, euid) < 0) {
5923 return posix_error();
5924 } else {
5925 Py_INCREF(Py_None);
5926 return Py_None;
5927 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005928}
5929#endif /* HAVE_SETREUID */
5930
5931#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005932PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005933"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005934Set the current process's real and effective group ids.");
5935
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005936static PyObject *
5937posix_setregid (PyObject *self, PyObject *args)
5938{
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 long rgid_arg, egid_arg;
5940 gid_t rgid, egid;
5941 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5942 return NULL;
5943 if (rgid_arg == -1)
5944 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5945 else
5946 rgid = rgid_arg; /* otherwise, assign from our long */
5947 if (egid_arg == -1)
5948 egid = (gid_t)-1;
5949 else
5950 egid = egid_arg;
5951 if ((egid_arg != -1 && egid != egid_arg) ||
5952 (rgid_arg != -1 && rgid != rgid_arg)) {
5953 PyErr_SetString(PyExc_OverflowError, "group id too big");
5954 return NULL;
5955 }
5956 if (setregid(rgid, egid) < 0) {
5957 return posix_error();
5958 } else {
5959 Py_INCREF(Py_None);
5960 return Py_None;
5961 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005962}
5963#endif /* HAVE_SETREGID */
5964
Guido van Rossumb6775db1994-08-01 11:34:53 +00005965#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005966PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005967"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005968Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005969
Barry Warsaw53699e91996-12-10 23:23:01 +00005970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005971posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005972{
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 long gid_arg;
5974 gid_t gid;
5975 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5976 return NULL;
5977 gid = gid_arg;
5978 if (gid != gid_arg) {
5979 PyErr_SetString(PyExc_OverflowError, "group id too big");
5980 return NULL;
5981 }
5982 if (setgid(gid) < 0)
5983 return posix_error();
5984 Py_INCREF(Py_None);
5985 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005986}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005987#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005988
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005989#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005990PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005991"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005992Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005993
5994static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005995posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005996{
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 int i, len;
5998 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005999
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 if (!PySequence_Check(groups)) {
6001 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6002 return NULL;
6003 }
6004 len = PySequence_Size(groups);
6005 if (len > MAX_GROUPS) {
6006 PyErr_SetString(PyExc_ValueError, "too many groups");
6007 return NULL;
6008 }
6009 for(i = 0; i < len; i++) {
6010 PyObject *elem;
6011 elem = PySequence_GetItem(groups, i);
6012 if (!elem)
6013 return NULL;
6014 if (!PyLong_Check(elem)) {
6015 PyErr_SetString(PyExc_TypeError,
6016 "groups must be integers");
6017 Py_DECREF(elem);
6018 return NULL;
6019 } else {
6020 unsigned long x = PyLong_AsUnsignedLong(elem);
6021 if (PyErr_Occurred()) {
6022 PyErr_SetString(PyExc_TypeError,
6023 "group id too big");
6024 Py_DECREF(elem);
6025 return NULL;
6026 }
6027 grouplist[i] = x;
6028 /* read back the value to see if it fitted in gid_t */
6029 if (grouplist[i] != x) {
6030 PyErr_SetString(PyExc_TypeError,
6031 "group id too big");
6032 Py_DECREF(elem);
6033 return NULL;
6034 }
6035 }
6036 Py_DECREF(elem);
6037 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006038
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 if (setgroups(len, grouplist) < 0)
6040 return posix_error();
6041 Py_INCREF(Py_None);
6042 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006043}
6044#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006045
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006046#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6047static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00006048wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006049{
Victor Stinner8c62be82010-05-06 00:08:46 +00006050 PyObject *result;
6051 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006052
Victor Stinner8c62be82010-05-06 00:08:46 +00006053 if (pid == -1)
6054 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006055
Victor Stinner8c62be82010-05-06 00:08:46 +00006056 if (struct_rusage == NULL) {
6057 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6058 if (m == NULL)
6059 return NULL;
6060 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6061 Py_DECREF(m);
6062 if (struct_rusage == NULL)
6063 return NULL;
6064 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006065
Victor Stinner8c62be82010-05-06 00:08:46 +00006066 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6067 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6068 if (!result)
6069 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006070
6071#ifndef doubletime
6072#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6073#endif
6074
Victor Stinner8c62be82010-05-06 00:08:46 +00006075 PyStructSequence_SET_ITEM(result, 0,
6076 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6077 PyStructSequence_SET_ITEM(result, 1,
6078 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006079#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6081 SET_INT(result, 2, ru->ru_maxrss);
6082 SET_INT(result, 3, ru->ru_ixrss);
6083 SET_INT(result, 4, ru->ru_idrss);
6084 SET_INT(result, 5, ru->ru_isrss);
6085 SET_INT(result, 6, ru->ru_minflt);
6086 SET_INT(result, 7, ru->ru_majflt);
6087 SET_INT(result, 8, ru->ru_nswap);
6088 SET_INT(result, 9, ru->ru_inblock);
6089 SET_INT(result, 10, ru->ru_oublock);
6090 SET_INT(result, 11, ru->ru_msgsnd);
6091 SET_INT(result, 12, ru->ru_msgrcv);
6092 SET_INT(result, 13, ru->ru_nsignals);
6093 SET_INT(result, 14, ru->ru_nvcsw);
6094 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006095#undef SET_INT
6096
Victor Stinner8c62be82010-05-06 00:08:46 +00006097 if (PyErr_Occurred()) {
6098 Py_DECREF(result);
6099 return NULL;
6100 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006101
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006103}
6104#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6105
6106#ifdef HAVE_WAIT3
6107PyDoc_STRVAR(posix_wait3__doc__,
6108"wait3(options) -> (pid, status, rusage)\n\n\
6109Wait for completion of a child process.");
6110
6111static PyObject *
6112posix_wait3(PyObject *self, PyObject *args)
6113{
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 pid_t pid;
6115 int options;
6116 struct rusage ru;
6117 WAIT_TYPE status;
6118 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006119
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6121 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006122
Victor Stinner8c62be82010-05-06 00:08:46 +00006123 Py_BEGIN_ALLOW_THREADS
6124 pid = wait3(&status, options, &ru);
6125 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006126
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006128}
6129#endif /* HAVE_WAIT3 */
6130
6131#ifdef HAVE_WAIT4
6132PyDoc_STRVAR(posix_wait4__doc__,
6133"wait4(pid, options) -> (pid, status, rusage)\n\n\
6134Wait for completion of a given child process.");
6135
6136static PyObject *
6137posix_wait4(PyObject *self, PyObject *args)
6138{
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 pid_t pid;
6140 int options;
6141 struct rusage ru;
6142 WAIT_TYPE status;
6143 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006144
Victor Stinner8c62be82010-05-06 00:08:46 +00006145 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6146 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006147
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 Py_BEGIN_ALLOW_THREADS
6149 pid = wait4(pid, &status, options, &ru);
6150 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006151
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006153}
6154#endif /* HAVE_WAIT4 */
6155
Ross Lagerwall7807c352011-03-17 20:20:30 +02006156#if defined(HAVE_WAITID) && !defined(__APPLE__)
6157PyDoc_STRVAR(posix_waitid__doc__,
6158"waitid(idtype, id, options) -> waitid_result\n\n\
6159Wait for the completion of one or more child processes.\n\n\
6160idtype can be P_PID, P_PGID or P_ALL.\n\
6161id specifies the pid to wait on.\n\
6162options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6163or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6164Returns either waitid_result or None if WNOHANG is specified and there are\n\
6165no children in a waitable state.");
6166
6167static PyObject *
6168posix_waitid(PyObject *self, PyObject *args)
6169{
6170 PyObject *result;
6171 idtype_t idtype;
6172 id_t id;
6173 int options, res;
6174 siginfo_t si;
6175 si.si_pid = 0;
6176 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6177 return NULL;
6178 Py_BEGIN_ALLOW_THREADS
6179 res = waitid(idtype, id, &si, options);
6180 Py_END_ALLOW_THREADS
6181 if (res == -1)
6182 return posix_error();
6183
6184 if (si.si_pid == 0)
6185 Py_RETURN_NONE;
6186
6187 result = PyStructSequence_New(&WaitidResultType);
6188 if (!result)
6189 return NULL;
6190
6191 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6192 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6193 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6194 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6195 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6196 if (PyErr_Occurred()) {
6197 Py_DECREF(result);
6198 return NULL;
6199 }
6200
6201 return result;
6202}
6203#endif
6204
Guido van Rossumb6775db1994-08-01 11:34:53 +00006205#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006206PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006207"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006208Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006209
Barry Warsaw53699e91996-12-10 23:23:01 +00006210static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006211posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006212{
Victor Stinner8c62be82010-05-06 00:08:46 +00006213 pid_t pid;
6214 int options;
6215 WAIT_TYPE status;
6216 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006217
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6219 return NULL;
6220 Py_BEGIN_ALLOW_THREADS
6221 pid = waitpid(pid, &status, options);
6222 Py_END_ALLOW_THREADS
6223 if (pid == -1)
6224 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006225
Victor Stinner8c62be82010-05-06 00:08:46 +00006226 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006227}
6228
Tim Petersab034fa2002-02-01 11:27:43 +00006229#elif defined(HAVE_CWAIT)
6230
6231/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006232PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006233"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006235
6236static PyObject *
6237posix_waitpid(PyObject *self, PyObject *args)
6238{
Victor Stinner8c62be82010-05-06 00:08:46 +00006239 Py_intptr_t pid;
6240 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006241
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6243 return NULL;
6244 Py_BEGIN_ALLOW_THREADS
6245 pid = _cwait(&status, pid, options);
6246 Py_END_ALLOW_THREADS
6247 if (pid == -1)
6248 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006249
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 /* shift the status left a byte so this is more like the POSIX waitpid */
6251 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006252}
6253#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006254
Guido van Rossumad0ee831995-03-01 10:34:45 +00006255#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006256PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006257"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006258Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006259
Barry Warsaw53699e91996-12-10 23:23:01 +00006260static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006261posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006262{
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 pid_t pid;
6264 WAIT_TYPE status;
6265 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006266
Victor Stinner8c62be82010-05-06 00:08:46 +00006267 Py_BEGIN_ALLOW_THREADS
6268 pid = wait(&status);
6269 Py_END_ALLOW_THREADS
6270 if (pid == -1)
6271 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006272
Victor Stinner8c62be82010-05-06 00:08:46 +00006273 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006274}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006275#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006278PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006279"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006280Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006281
Barry Warsaw53699e91996-12-10 23:23:01 +00006282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006283posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006284{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006285#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006286 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006287#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006288#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006289 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006290 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006291#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006293#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006294#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006295}
6296
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006297
Guido van Rossumb6775db1994-08-01 11:34:53 +00006298#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006299PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006300"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006301Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006302
Barry Warsaw53699e91996-12-10 23:23:01 +00006303static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006304posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006305{
Victor Stinner8c62be82010-05-06 00:08:46 +00006306 PyObject* v;
6307 char buf[MAXPATHLEN];
6308 PyObject *opath;
6309 char *path;
6310 int n;
6311 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006312
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 if (!PyArg_ParseTuple(args, "O&:readlink",
6314 PyUnicode_FSConverter, &opath))
6315 return NULL;
6316 path = PyBytes_AsString(opath);
6317 v = PySequence_GetItem(args, 0);
6318 if (v == NULL) {
6319 Py_DECREF(opath);
6320 return NULL;
6321 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006322
Victor Stinner8c62be82010-05-06 00:08:46 +00006323 if (PyUnicode_Check(v)) {
6324 arg_is_unicode = 1;
6325 }
6326 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006327
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 Py_BEGIN_ALLOW_THREADS
6329 n = readlink(path, buf, (int) sizeof buf);
6330 Py_END_ALLOW_THREADS
6331 if (n < 0)
6332 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006333
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006335 if (arg_is_unicode)
6336 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6337 else
6338 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006339}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006340#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006341
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006342
Brian Curtin52173d42010-12-02 18:29:18 +00006343#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006344PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006345"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006346Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006347
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006348static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006349posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006350{
Victor Stinner8c62be82010-05-06 00:08:46 +00006351 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006352}
6353#endif /* HAVE_SYMLINK */
6354
Brian Curtind40e6f72010-07-08 21:39:08 +00006355#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6356
6357PyDoc_STRVAR(win_readlink__doc__,
6358"readlink(path) -> path\n\n\
6359Return a string representing the path to which the symbolic link points.");
6360
Brian Curtind40e6f72010-07-08 21:39:08 +00006361/* Windows readlink implementation */
6362static PyObject *
6363win_readlink(PyObject *self, PyObject *args)
6364{
6365 wchar_t *path;
6366 DWORD n_bytes_returned;
6367 DWORD io_result;
6368 PyObject *result;
6369 HANDLE reparse_point_handle;
6370
6371 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6372 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6373 wchar_t *print_name;
6374
6375 if (!PyArg_ParseTuple(args,
6376 "u:readlink",
6377 &path))
6378 return NULL;
6379
6380 /* First get a handle to the reparse point */
6381 Py_BEGIN_ALLOW_THREADS
6382 reparse_point_handle = CreateFileW(
6383 path,
6384 0,
6385 0,
6386 0,
6387 OPEN_EXISTING,
6388 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6389 0);
6390 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006391
Brian Curtind40e6f72010-07-08 21:39:08 +00006392 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6393 {
6394 return win32_error_unicode("readlink", path);
6395 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006396
Brian Curtind40e6f72010-07-08 21:39:08 +00006397 Py_BEGIN_ALLOW_THREADS
6398 /* New call DeviceIoControl to read the reparse point */
6399 io_result = DeviceIoControl(
6400 reparse_point_handle,
6401 FSCTL_GET_REPARSE_POINT,
6402 0, 0, /* in buffer */
6403 target_buffer, sizeof(target_buffer),
6404 &n_bytes_returned,
6405 0 /* we're not using OVERLAPPED_IO */
6406 );
6407 CloseHandle(reparse_point_handle);
6408 Py_END_ALLOW_THREADS
6409
6410 if (io_result==0)
6411 {
6412 return win32_error_unicode("readlink", path);
6413 }
6414
6415 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6416 {
6417 PyErr_SetString(PyExc_ValueError,
6418 "not a symbolic link");
6419 return NULL;
6420 }
Brian Curtin74e45612010-07-09 15:58:59 +00006421 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6422 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6423
6424 result = PyUnicode_FromWideChar(print_name,
6425 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006426 return result;
6427}
6428
6429#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6430
Brian Curtin52173d42010-12-02 18:29:18 +00006431#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006432
6433/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6434static int has_CreateSymbolicLinkW = 0;
6435static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6436static int
6437check_CreateSymbolicLinkW()
6438{
6439 HINSTANCE hKernel32;
6440 /* only recheck */
6441 if (has_CreateSymbolicLinkW)
6442 return has_CreateSymbolicLinkW;
6443 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006444 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6445 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006446 if (Py_CreateSymbolicLinkW)
6447 has_CreateSymbolicLinkW = 1;
6448 return has_CreateSymbolicLinkW;
6449}
6450
6451PyDoc_STRVAR(win_symlink__doc__,
6452"symlink(src, dst, target_is_directory=False)\n\n\
6453Create a symbolic link pointing to src named dst.\n\
6454target_is_directory is required if the target is to be interpreted as\n\
6455a directory.\n\
6456This function requires Windows 6.0 or greater, and raises a\n\
6457NotImplementedError otherwise.");
6458
6459static PyObject *
6460win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6461{
6462 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6463 PyObject *src, *dest;
6464 int target_is_directory = 0;
6465 DWORD res;
6466 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006467
Brian Curtind40e6f72010-07-08 21:39:08 +00006468 if (!check_CreateSymbolicLinkW())
6469 {
6470 /* raise NotImplementedError */
6471 return PyErr_Format(PyExc_NotImplementedError,
6472 "CreateSymbolicLinkW not found");
6473 }
6474 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6475 kwlist, &src, &dest, &target_is_directory))
6476 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006477
6478 if (win32_can_symlink == 0)
6479 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6480
Brian Curtind40e6f72010-07-08 21:39:08 +00006481 if (!convert_to_unicode(&src)) { return NULL; }
6482 if (!convert_to_unicode(&dest)) {
6483 Py_DECREF(src);
6484 return NULL;
6485 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006486
Brian Curtind40e6f72010-07-08 21:39:08 +00006487 /* if src is a directory, ensure target_is_directory==1 */
6488 if(
6489 GetFileAttributesExW(
6490 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6491 ))
6492 {
6493 target_is_directory = target_is_directory ||
6494 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6495 }
6496
6497 Py_BEGIN_ALLOW_THREADS
6498 res = Py_CreateSymbolicLinkW(
6499 PyUnicode_AsUnicode(dest),
6500 PyUnicode_AsUnicode(src),
6501 target_is_directory);
6502 Py_END_ALLOW_THREADS
6503 Py_DECREF(src);
6504 Py_DECREF(dest);
6505 if (!res)
6506 {
6507 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6508 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006509
Brian Curtind40e6f72010-07-08 21:39:08 +00006510 Py_INCREF(Py_None);
6511 return Py_None;
6512}
Brian Curtin52173d42010-12-02 18:29:18 +00006513#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006514
6515#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006516#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6517static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006518system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006519{
6520 ULONG value = 0;
6521
6522 Py_BEGIN_ALLOW_THREADS
6523 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6524 Py_END_ALLOW_THREADS
6525
6526 return value;
6527}
6528
6529static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006530posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006531{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006532 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006533 return Py_BuildValue("ddddd",
6534 (double)0 /* t.tms_utime / HZ */,
6535 (double)0 /* t.tms_stime / HZ */,
6536 (double)0 /* t.tms_cutime / HZ */,
6537 (double)0 /* t.tms_cstime / HZ */,
6538 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006539}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006540#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006541#define NEED_TICKS_PER_SECOND
6542static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006543static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006544posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006545{
Victor Stinner8c62be82010-05-06 00:08:46 +00006546 struct tms t;
6547 clock_t c;
6548 errno = 0;
6549 c = times(&t);
6550 if (c == (clock_t) -1)
6551 return posix_error();
6552 return Py_BuildValue("ddddd",
6553 (double)t.tms_utime / ticks_per_second,
6554 (double)t.tms_stime / ticks_per_second,
6555 (double)t.tms_cutime / ticks_per_second,
6556 (double)t.tms_cstime / ticks_per_second,
6557 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006558}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006559#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006560#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006561
6562
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006563#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006564#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006565static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006566posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006567{
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 FILETIME create, exit, kernel, user;
6569 HANDLE hProc;
6570 hProc = GetCurrentProcess();
6571 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6572 /* The fields of a FILETIME structure are the hi and lo part
6573 of a 64-bit value expressed in 100 nanosecond units.
6574 1e7 is one second in such units; 1e-7 the inverse.
6575 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6576 */
6577 return Py_BuildValue(
6578 "ddddd",
6579 (double)(user.dwHighDateTime*429.4967296 +
6580 user.dwLowDateTime*1e-7),
6581 (double)(kernel.dwHighDateTime*429.4967296 +
6582 kernel.dwLowDateTime*1e-7),
6583 (double)0,
6584 (double)0,
6585 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006586}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006587#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006588
6589#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006590PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006591"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006592Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006593#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006594
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006595
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006596#ifdef HAVE_GETSID
6597PyDoc_STRVAR(posix_getsid__doc__,
6598"getsid(pid) -> sid\n\n\
6599Call the system call getsid().");
6600
6601static PyObject *
6602posix_getsid(PyObject *self, PyObject *args)
6603{
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 pid_t pid;
6605 int sid;
6606 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6607 return NULL;
6608 sid = getsid(pid);
6609 if (sid < 0)
6610 return posix_error();
6611 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006612}
6613#endif /* HAVE_GETSID */
6614
6615
Guido van Rossumb6775db1994-08-01 11:34:53 +00006616#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006618"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006619Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006620
Barry Warsaw53699e91996-12-10 23:23:01 +00006621static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006622posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006623{
Victor Stinner8c62be82010-05-06 00:08:46 +00006624 if (setsid() < 0)
6625 return posix_error();
6626 Py_INCREF(Py_None);
6627 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006628}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006629#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006630
Guido van Rossumb6775db1994-08-01 11:34:53 +00006631#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006632PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006633"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006634Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006635
Barry Warsaw53699e91996-12-10 23:23:01 +00006636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006637posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006638{
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 pid_t pid;
6640 int pgrp;
6641 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6642 return NULL;
6643 if (setpgid(pid, pgrp) < 0)
6644 return posix_error();
6645 Py_INCREF(Py_None);
6646 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006647}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006648#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006649
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006650
Guido van Rossumb6775db1994-08-01 11:34:53 +00006651#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006652PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006653"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006654Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006655
Barry Warsaw53699e91996-12-10 23:23:01 +00006656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006657posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006658{
Victor Stinner8c62be82010-05-06 00:08:46 +00006659 int fd;
6660 pid_t pgid;
6661 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6662 return NULL;
6663 pgid = tcgetpgrp(fd);
6664 if (pgid < 0)
6665 return posix_error();
6666 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006667}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006668#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006669
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006670
Guido van Rossumb6775db1994-08-01 11:34:53 +00006671#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006672PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006673"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006674Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006675
Barry Warsaw53699e91996-12-10 23:23:01 +00006676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006677posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006678{
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 int fd;
6680 pid_t pgid;
6681 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6682 return NULL;
6683 if (tcsetpgrp(fd, pgid) < 0)
6684 return posix_error();
6685 Py_INCREF(Py_None);
6686 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006687}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006688#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006689
Guido van Rossum687dd131993-05-17 08:34:16 +00006690/* Functions acting on file descriptors */
6691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006692PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006693"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006694Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006695
Barry Warsaw53699e91996-12-10 23:23:01 +00006696static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006697posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006698{
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 PyObject *ofile;
6700 char *file;
6701 int flag;
6702 int mode = 0777;
6703 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006704
6705#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006707 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 Py_BEGIN_ALLOW_THREADS
6709 /* PyUnicode_AS_UNICODE OK without thread
6710 lock as it is a simple dereference. */
6711 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6712 Py_END_ALLOW_THREADS
6713 if (fd < 0)
6714 return posix_error();
6715 return PyLong_FromLong((long)fd);
6716 }
6717 /* Drop the argument parsing error as narrow strings
6718 are also valid. */
6719 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006720#endif
6721
Victor Stinner26de69d2011-06-17 15:15:38 +02006722 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 PyUnicode_FSConverter, &ofile,
6724 &flag, &mode))
6725 return NULL;
6726 file = PyBytes_AsString(ofile);
6727 Py_BEGIN_ALLOW_THREADS
6728 fd = open(file, flag, mode);
6729 Py_END_ALLOW_THREADS
6730 if (fd < 0)
6731 return posix_error_with_allocated_filename(ofile);
6732 Py_DECREF(ofile);
6733 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006734}
6735
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006737PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006738"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006739Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006740
Barry Warsaw53699e91996-12-10 23:23:01 +00006741static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006742posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006743{
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 int fd, res;
6745 if (!PyArg_ParseTuple(args, "i:close", &fd))
6746 return NULL;
6747 if (!_PyVerify_fd(fd))
6748 return posix_error();
6749 Py_BEGIN_ALLOW_THREADS
6750 res = close(fd);
6751 Py_END_ALLOW_THREADS
6752 if (res < 0)
6753 return posix_error();
6754 Py_INCREF(Py_None);
6755 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006756}
6757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006758
Victor Stinner8c62be82010-05-06 00:08:46 +00006759PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006760"closerange(fd_low, fd_high)\n\n\
6761Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6762
6763static PyObject *
6764posix_closerange(PyObject *self, PyObject *args)
6765{
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 int fd_from, fd_to, i;
6767 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6768 return NULL;
6769 Py_BEGIN_ALLOW_THREADS
6770 for (i = fd_from; i < fd_to; i++)
6771 if (_PyVerify_fd(i))
6772 close(i);
6773 Py_END_ALLOW_THREADS
6774 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006775}
6776
6777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006778PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006779"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006780Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006781
Barry Warsaw53699e91996-12-10 23:23:01 +00006782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006783posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006784{
Victor Stinner8c62be82010-05-06 00:08:46 +00006785 int fd;
6786 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6787 return NULL;
6788 if (!_PyVerify_fd(fd))
6789 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 if (fd < 0)
6792 return posix_error();
6793 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006794}
6795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006797PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006798"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006799Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006800
Barry Warsaw53699e91996-12-10 23:23:01 +00006801static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006802posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006803{
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 int fd, fd2, res;
6805 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6806 return NULL;
6807 if (!_PyVerify_fd_dup2(fd, fd2))
6808 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 if (res < 0)
6811 return posix_error();
6812 Py_INCREF(Py_None);
6813 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006814}
6815
Ross Lagerwall7807c352011-03-17 20:20:30 +02006816#ifdef HAVE_LOCKF
6817PyDoc_STRVAR(posix_lockf__doc__,
6818"lockf(fd, cmd, len)\n\n\
6819Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6820fd is an open file descriptor.\n\
6821cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6822F_TEST.\n\
6823len specifies the section of the file to lock.");
6824
6825static PyObject *
6826posix_lockf(PyObject *self, PyObject *args)
6827{
6828 int fd, cmd, res;
6829 off_t len;
6830 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6831 &fd, &cmd, _parse_off_t, &len))
6832 return NULL;
6833
6834 Py_BEGIN_ALLOW_THREADS
6835 res = lockf(fd, cmd, len);
6836 Py_END_ALLOW_THREADS
6837
6838 if (res < 0)
6839 return posix_error();
6840
6841 Py_RETURN_NONE;
6842}
6843#endif
6844
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006846PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006847"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006848Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006849
Barry Warsaw53699e91996-12-10 23:23:01 +00006850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006851posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006852{
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006854#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006856#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006858#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006859 PyObject *posobj;
6860 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006862#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6864 switch (how) {
6865 case 0: how = SEEK_SET; break;
6866 case 1: how = SEEK_CUR; break;
6867 case 2: how = SEEK_END; break;
6868 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006869#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006870
Ross Lagerwall8e749672011-03-17 21:54:07 +02006871#if !defined(HAVE_LARGEFILE_SUPPORT)
6872 pos = PyLong_AsLong(posobj);
6873#else
6874 pos = PyLong_AsLongLong(posobj);
6875#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 if (PyErr_Occurred())
6877 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006878
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 if (!_PyVerify_fd(fd))
6880 return posix_error();
6881 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006882#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006884#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006886#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 Py_END_ALLOW_THREADS
6888 if (res < 0)
6889 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006890
6891#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006893#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006895#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006896}
6897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006899PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006900"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006901Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006902
Barry Warsaw53699e91996-12-10 23:23:01 +00006903static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006904posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006905{
Victor Stinner8c62be82010-05-06 00:08:46 +00006906 int fd, size;
6907 Py_ssize_t n;
6908 PyObject *buffer;
6909 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6910 return NULL;
6911 if (size < 0) {
6912 errno = EINVAL;
6913 return posix_error();
6914 }
6915 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6916 if (buffer == NULL)
6917 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006918 if (!_PyVerify_fd(fd)) {
6919 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006921 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 Py_BEGIN_ALLOW_THREADS
6923 n = read(fd, PyBytes_AS_STRING(buffer), size);
6924 Py_END_ALLOW_THREADS
6925 if (n < 0) {
6926 Py_DECREF(buffer);
6927 return posix_error();
6928 }
6929 if (n != size)
6930 _PyBytes_Resize(&buffer, n);
6931 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006932}
6933
Ross Lagerwall7807c352011-03-17 20:20:30 +02006934#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6935 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006936static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006937iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6938{
6939 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006940 Py_ssize_t blen, total = 0;
6941
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006942 *iov = PyMem_New(struct iovec, cnt);
6943 if (*iov == NULL) {
6944 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006945 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006946 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006947
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006948 *buf = PyMem_New(Py_buffer, cnt);
6949 if (*buf == NULL) {
6950 PyMem_Del(*iov);
6951 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006952 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006953 }
6954
6955 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006956 PyObject *item = PySequence_GetItem(seq, i);
6957 if (item == NULL)
6958 goto fail;
6959 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6960 Py_DECREF(item);
6961 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006962 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006963 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006964 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006965 blen = (*buf)[i].len;
6966 (*iov)[i].iov_len = blen;
6967 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006968 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006969 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006970
6971fail:
6972 PyMem_Del(*iov);
6973 for (j = 0; j < i; j++) {
6974 PyBuffer_Release(&(*buf)[j]);
6975 }
6976 PyMem_Del(*buf);
6977 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006978}
6979
6980static void
6981iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6982{
6983 int i;
6984 PyMem_Del(iov);
6985 for (i = 0; i < cnt; i++) {
6986 PyBuffer_Release(&buf[i]);
6987 }
6988 PyMem_Del(buf);
6989}
6990#endif
6991
Ross Lagerwall7807c352011-03-17 20:20:30 +02006992#ifdef HAVE_READV
6993PyDoc_STRVAR(posix_readv__doc__,
6994"readv(fd, buffers) -> bytesread\n\n\
6995Read from a file descriptor into a number of writable buffers. buffers\n\
6996is an arbitrary sequence of writable buffers.\n\
6997Returns the total number of bytes read.");
6998
6999static PyObject *
7000posix_readv(PyObject *self, PyObject *args)
7001{
7002 int fd, cnt;
7003 Py_ssize_t n;
7004 PyObject *seq;
7005 struct iovec *iov;
7006 Py_buffer *buf;
7007
7008 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7009 return NULL;
7010 if (!PySequence_Check(seq)) {
7011 PyErr_SetString(PyExc_TypeError,
7012 "readv() arg 2 must be a sequence");
7013 return NULL;
7014 }
7015 cnt = PySequence_Size(seq);
7016
7017 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7018 return NULL;
7019
7020 Py_BEGIN_ALLOW_THREADS
7021 n = readv(fd, iov, cnt);
7022 Py_END_ALLOW_THREADS
7023
7024 iov_cleanup(iov, buf, cnt);
7025 return PyLong_FromSsize_t(n);
7026}
7027#endif
7028
7029#ifdef HAVE_PREAD
7030PyDoc_STRVAR(posix_pread__doc__,
7031"pread(fd, buffersize, offset) -> string\n\n\
7032Read from a file descriptor, fd, at a position of offset. It will read up\n\
7033to buffersize number of bytes. The file offset remains unchanged.");
7034
7035static PyObject *
7036posix_pread(PyObject *self, PyObject *args)
7037{
7038 int fd, size;
7039 off_t offset;
7040 Py_ssize_t n;
7041 PyObject *buffer;
7042 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7043 return NULL;
7044
7045 if (size < 0) {
7046 errno = EINVAL;
7047 return posix_error();
7048 }
7049 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7050 if (buffer == NULL)
7051 return NULL;
7052 if (!_PyVerify_fd(fd)) {
7053 Py_DECREF(buffer);
7054 return posix_error();
7055 }
7056 Py_BEGIN_ALLOW_THREADS
7057 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7058 Py_END_ALLOW_THREADS
7059 if (n < 0) {
7060 Py_DECREF(buffer);
7061 return posix_error();
7062 }
7063 if (n != size)
7064 _PyBytes_Resize(&buffer, n);
7065 return buffer;
7066}
7067#endif
7068
7069PyDoc_STRVAR(posix_write__doc__,
7070"write(fd, string) -> byteswritten\n\n\
7071Write a string to a file descriptor.");
7072
7073static PyObject *
7074posix_write(PyObject *self, PyObject *args)
7075{
7076 Py_buffer pbuf;
7077 int fd;
7078 Py_ssize_t size, len;
7079
7080 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7081 return NULL;
7082 if (!_PyVerify_fd(fd)) {
7083 PyBuffer_Release(&pbuf);
7084 return posix_error();
7085 }
7086 len = pbuf.len;
7087 Py_BEGIN_ALLOW_THREADS
7088#if defined(MS_WIN64) || defined(MS_WINDOWS)
7089 if (len > INT_MAX)
7090 len = INT_MAX;
7091 size = write(fd, pbuf.buf, (int)len);
7092#else
7093 size = write(fd, pbuf.buf, len);
7094#endif
7095 Py_END_ALLOW_THREADS
7096 PyBuffer_Release(&pbuf);
7097 if (size < 0)
7098 return posix_error();
7099 return PyLong_FromSsize_t(size);
7100}
7101
7102#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007103PyDoc_STRVAR(posix_sendfile__doc__,
7104"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7105sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7106 -> byteswritten\n\
7107Copy nbytes bytes from file descriptor in to file descriptor out.");
7108
7109static PyObject *
7110posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7111{
7112 int in, out;
7113 Py_ssize_t ret;
7114 off_t offset;
7115
7116#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7117#ifndef __APPLE__
7118 Py_ssize_t len;
7119#endif
7120 PyObject *headers = NULL, *trailers = NULL;
7121 Py_buffer *hbuf, *tbuf;
7122 off_t sbytes;
7123 struct sf_hdtr sf;
7124 int flags = 0;
7125 sf.headers = NULL;
7126 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007127 static char *keywords[] = {"out", "in",
7128 "offset", "count",
7129 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007130
7131#ifdef __APPLE__
7132 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007133 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007134#else
7135 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007136 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007137#endif
7138 &headers, &trailers, &flags))
7139 return NULL;
7140 if (headers != NULL) {
7141 if (!PySequence_Check(headers)) {
7142 PyErr_SetString(PyExc_TypeError,
7143 "sendfile() headers must be a sequence or None");
7144 return NULL;
7145 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007146 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007147 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007148 if (sf.hdr_cnt > 0 &&
7149 !(i = iov_setup(&(sf.headers), &hbuf,
7150 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007151 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007152#ifdef __APPLE__
7153 sbytes += i;
7154#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007155 }
7156 }
7157 if (trailers != NULL) {
7158 if (!PySequence_Check(trailers)) {
7159 PyErr_SetString(PyExc_TypeError,
7160 "sendfile() trailers must be a sequence or None");
7161 return NULL;
7162 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007163 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007164 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007165 if (sf.trl_cnt > 0 &&
7166 !(i = iov_setup(&(sf.trailers), &tbuf,
7167 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007168 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007169#ifdef __APPLE__
7170 sbytes += i;
7171#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007172 }
7173 }
7174
7175 Py_BEGIN_ALLOW_THREADS
7176#ifdef __APPLE__
7177 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7178#else
7179 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7180#endif
7181 Py_END_ALLOW_THREADS
7182
7183 if (sf.headers != NULL)
7184 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7185 if (sf.trailers != NULL)
7186 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7187
7188 if (ret < 0) {
7189 if ((errno == EAGAIN) || (errno == EBUSY)) {
7190 if (sbytes != 0) {
7191 // some data has been sent
7192 goto done;
7193 }
7194 else {
7195 // no data has been sent; upper application is supposed
7196 // to retry on EAGAIN or EBUSY
7197 return posix_error();
7198 }
7199 }
7200 return posix_error();
7201 }
7202 goto done;
7203
7204done:
7205 #if !defined(HAVE_LARGEFILE_SUPPORT)
7206 return Py_BuildValue("l", sbytes);
7207 #else
7208 return Py_BuildValue("L", sbytes);
7209 #endif
7210
7211#else
7212 Py_ssize_t count;
7213 PyObject *offobj;
7214 static char *keywords[] = {"out", "in",
7215 "offset", "count", NULL};
7216 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7217 keywords, &out, &in, &offobj, &count))
7218 return NULL;
7219#ifdef linux
7220 if (offobj == Py_None) {
7221 Py_BEGIN_ALLOW_THREADS
7222 ret = sendfile(out, in, NULL, count);
7223 Py_END_ALLOW_THREADS
7224 if (ret < 0)
7225 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007226 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007227 }
7228#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007229 if (!_parse_off_t(offobj, &offset))
7230 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007231 Py_BEGIN_ALLOW_THREADS
7232 ret = sendfile(out, in, &offset, count);
7233 Py_END_ALLOW_THREADS
7234 if (ret < 0)
7235 return posix_error();
7236 return Py_BuildValue("n", ret);
7237#endif
7238}
7239#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007241PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007242"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007243Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007244
Barry Warsaw53699e91996-12-10 23:23:01 +00007245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007246posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007247{
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 int fd;
7249 STRUCT_STAT st;
7250 int res;
7251 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7252 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007253#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 /* on OpenVMS we must ensure that all bytes are written to the file */
7255 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007256#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 if (!_PyVerify_fd(fd))
7258 return posix_error();
7259 Py_BEGIN_ALLOW_THREADS
7260 res = FSTAT(fd, &st);
7261 Py_END_ALLOW_THREADS
7262 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007263#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007265#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007267#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 }
Tim Peters5aa91602002-01-30 05:46:57 +00007269
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007271}
7272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007273PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007274"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007275Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007276connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007277
7278static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007279posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007280{
Victor Stinner8c62be82010-05-06 00:08:46 +00007281 int fd;
7282 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7283 return NULL;
7284 if (!_PyVerify_fd(fd))
7285 return PyBool_FromLong(0);
7286 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007287}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007288
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007289#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007290PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007291"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007292Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007293
Barry Warsaw53699e91996-12-10 23:23:01 +00007294static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007295posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007296{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007297#if defined(PYOS_OS2)
7298 HFILE read, write;
7299 APIRET rc;
7300
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007301 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007302 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007303 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007304
7305 return Py_BuildValue("(ii)", read, write);
7306#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007307#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 int fds[2];
7309 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007311 if (res != 0)
7312 return posix_error();
7313 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007314#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 HANDLE read, write;
7316 int read_fd, write_fd;
7317 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007319 if (!ok)
7320 return win32_error("CreatePipe", NULL);
7321 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7322 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7323 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007324#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007325#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007326}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007327#endif /* HAVE_PIPE */
7328
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007329#ifdef HAVE_PIPE2
7330PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007331"pipe2(flags) -> (read_end, write_end)\n\n\
7332Create a pipe with flags set atomically.\n\
7333flags can be constructed by ORing together one or more of these values:\n\
7334O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007335");
7336
7337static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007338posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007339{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007340 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007341 int fds[2];
7342 int res;
7343
Charles-François Natali368f34b2011-06-06 19:49:47 +02007344 flags = PyLong_AsLong(arg);
7345 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007346 return NULL;
7347
7348 res = pipe2(fds, flags);
7349 if (res != 0)
7350 return posix_error();
7351 return Py_BuildValue("(ii)", fds[0], fds[1]);
7352}
7353#endif /* HAVE_PIPE2 */
7354
Ross Lagerwall7807c352011-03-17 20:20:30 +02007355#ifdef HAVE_WRITEV
7356PyDoc_STRVAR(posix_writev__doc__,
7357"writev(fd, buffers) -> byteswritten\n\n\
7358Write the contents of buffers to a file descriptor, where buffers is an\n\
7359arbitrary sequence of buffers.\n\
7360Returns the total bytes written.");
7361
7362static PyObject *
7363posix_writev(PyObject *self, PyObject *args)
7364{
7365 int fd, cnt;
7366 Py_ssize_t res;
7367 PyObject *seq;
7368 struct iovec *iov;
7369 Py_buffer *buf;
7370 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7371 return NULL;
7372 if (!PySequence_Check(seq)) {
7373 PyErr_SetString(PyExc_TypeError,
7374 "writev() arg 2 must be a sequence");
7375 return NULL;
7376 }
7377 cnt = PySequence_Size(seq);
7378
7379 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7380 return NULL;
7381 }
7382
7383 Py_BEGIN_ALLOW_THREADS
7384 res = writev(fd, iov, cnt);
7385 Py_END_ALLOW_THREADS
7386
7387 iov_cleanup(iov, buf, cnt);
7388 return PyLong_FromSsize_t(res);
7389}
7390#endif
7391
7392#ifdef HAVE_PWRITE
7393PyDoc_STRVAR(posix_pwrite__doc__,
7394"pwrite(fd, string, offset) -> byteswritten\n\n\
7395Write string to a file descriptor, fd, from offset, leaving the file\n\
7396offset unchanged.");
7397
7398static PyObject *
7399posix_pwrite(PyObject *self, PyObject *args)
7400{
7401 Py_buffer pbuf;
7402 int fd;
7403 off_t offset;
7404 Py_ssize_t size;
7405
7406 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7407 return NULL;
7408
7409 if (!_PyVerify_fd(fd)) {
7410 PyBuffer_Release(&pbuf);
7411 return posix_error();
7412 }
7413 Py_BEGIN_ALLOW_THREADS
7414 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7415 Py_END_ALLOW_THREADS
7416 PyBuffer_Release(&pbuf);
7417 if (size < 0)
7418 return posix_error();
7419 return PyLong_FromSsize_t(size);
7420}
7421#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007422
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007423#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007424PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007425"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007426Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007427
Barry Warsaw53699e91996-12-10 23:23:01 +00007428static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007429posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007430{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007431 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007432 char *filename;
7433 int mode = 0666;
7434 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007435 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7436 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007437 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007438 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 Py_BEGIN_ALLOW_THREADS
7440 res = mkfifo(filename, mode);
7441 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007442 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007443 if (res < 0)
7444 return posix_error();
7445 Py_INCREF(Py_None);
7446 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007447}
7448#endif
7449
7450
Neal Norwitz11690112002-07-30 01:08:28 +00007451#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007452PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007453"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007454Create a filesystem node (file, device special file or named pipe)\n\
7455named filename. mode specifies both the permissions to use and the\n\
7456type of node to be created, being combined (bitwise OR) with one of\n\
7457S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007458device defines the newly created device special file (probably using\n\
7459os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007460
7461
7462static PyObject *
7463posix_mknod(PyObject *self, PyObject *args)
7464{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007465 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 char *filename;
7467 int mode = 0600;
7468 int device = 0;
7469 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007470 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7471 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007473 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007474 Py_BEGIN_ALLOW_THREADS
7475 res = mknod(filename, mode, device);
7476 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007477 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 if (res < 0)
7479 return posix_error();
7480 Py_INCREF(Py_None);
7481 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007482}
7483#endif
7484
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007485#ifdef HAVE_DEVICE_MACROS
7486PyDoc_STRVAR(posix_major__doc__,
7487"major(device) -> major number\n\
7488Extracts a device major number from a raw device number.");
7489
7490static PyObject *
7491posix_major(PyObject *self, PyObject *args)
7492{
Victor Stinner8c62be82010-05-06 00:08:46 +00007493 int device;
7494 if (!PyArg_ParseTuple(args, "i:major", &device))
7495 return NULL;
7496 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007497}
7498
7499PyDoc_STRVAR(posix_minor__doc__,
7500"minor(device) -> minor number\n\
7501Extracts a device minor number from a raw device number.");
7502
7503static PyObject *
7504posix_minor(PyObject *self, PyObject *args)
7505{
Victor Stinner8c62be82010-05-06 00:08:46 +00007506 int device;
7507 if (!PyArg_ParseTuple(args, "i:minor", &device))
7508 return NULL;
7509 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007510}
7511
7512PyDoc_STRVAR(posix_makedev__doc__,
7513"makedev(major, minor) -> device number\n\
7514Composes a raw device number from the major and minor device numbers.");
7515
7516static PyObject *
7517posix_makedev(PyObject *self, PyObject *args)
7518{
Victor Stinner8c62be82010-05-06 00:08:46 +00007519 int major, minor;
7520 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7521 return NULL;
7522 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007523}
7524#endif /* device macros */
7525
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007526
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007527#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007528PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007529"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007530Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007531
Barry Warsaw53699e91996-12-10 23:23:01 +00007532static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007533posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007534{
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 int fd;
7536 off_t length;
7537 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007538
Ross Lagerwall7807c352011-03-17 20:20:30 +02007539 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007541
Victor Stinner8c62be82010-05-06 00:08:46 +00007542 Py_BEGIN_ALLOW_THREADS
7543 res = ftruncate(fd, length);
7544 Py_END_ALLOW_THREADS
7545 if (res < 0)
7546 return posix_error();
7547 Py_INCREF(Py_None);
7548 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007549}
7550#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007551
Ross Lagerwall7807c352011-03-17 20:20:30 +02007552#ifdef HAVE_TRUNCATE
7553PyDoc_STRVAR(posix_truncate__doc__,
7554"truncate(path, length)\n\n\
7555Truncate the file given by path to length bytes.");
7556
7557static PyObject *
7558posix_truncate(PyObject *self, PyObject *args)
7559{
7560 PyObject *opath;
7561 const char *path;
7562 off_t length;
7563 int res;
7564
7565 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7566 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7567 return NULL;
7568 path = PyBytes_AsString(opath);
7569
7570 Py_BEGIN_ALLOW_THREADS
7571 res = truncate(path, length);
7572 Py_END_ALLOW_THREADS
7573 Py_DECREF(opath);
7574 if (res < 0)
7575 return posix_error();
7576 Py_RETURN_NONE;
7577}
7578#endif
7579
7580#ifdef HAVE_POSIX_FALLOCATE
7581PyDoc_STRVAR(posix_posix_fallocate__doc__,
7582"posix_fallocate(fd, offset, len)\n\n\
7583Ensures that enough disk space is allocated for the file specified by fd\n\
7584starting from offset and continuing for len bytes.");
7585
7586static PyObject *
7587posix_posix_fallocate(PyObject *self, PyObject *args)
7588{
7589 off_t len, offset;
7590 int res, fd;
7591
7592 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7593 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7594 return NULL;
7595
7596 Py_BEGIN_ALLOW_THREADS
7597 res = posix_fallocate(fd, offset, len);
7598 Py_END_ALLOW_THREADS
7599 if (res != 0) {
7600 errno = res;
7601 return posix_error();
7602 }
7603 Py_RETURN_NONE;
7604}
7605#endif
7606
7607#ifdef HAVE_POSIX_FADVISE
7608PyDoc_STRVAR(posix_posix_fadvise__doc__,
7609"posix_fadvise(fd, offset, len, advice)\n\n\
7610Announces an intention to access data in a specific pattern thus allowing\n\
7611the kernel to make optimizations.\n\
7612The advice applies to the region of the file specified by fd starting at\n\
7613offset and continuing for len bytes.\n\
7614advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7615POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7616POSIX_FADV_DONTNEED.");
7617
7618static PyObject *
7619posix_posix_fadvise(PyObject *self, PyObject *args)
7620{
7621 off_t len, offset;
7622 int res, fd, advice;
7623
7624 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7625 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7626 return NULL;
7627
7628 Py_BEGIN_ALLOW_THREADS
7629 res = posix_fadvise(fd, offset, len, advice);
7630 Py_END_ALLOW_THREADS
7631 if (res != 0) {
7632 errno = res;
7633 return posix_error();
7634 }
7635 Py_RETURN_NONE;
7636}
7637#endif
7638
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007639#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007640PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007641"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007642Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007643
Fred Drake762e2061999-08-26 17:23:54 +00007644/* Save putenv() parameters as values here, so we can collect them when they
7645 * get re-set with another call for the same key. */
7646static PyObject *posix_putenv_garbage;
7647
Tim Peters5aa91602002-01-30 05:46:57 +00007648static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007649posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007650{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007651#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007652 wchar_t *s1, *s2;
7653 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007654#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 PyObject *os1, *os2;
7656 char *s1, *s2;
7657 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007658#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007659 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007661
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007662#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 if (!PyArg_ParseTuple(args,
7664 "uu:putenv",
7665 &s1, &s2))
7666 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007667#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007668 if (!PyArg_ParseTuple(args,
7669 "O&O&:putenv",
7670 PyUnicode_FSConverter, &os1,
7671 PyUnicode_FSConverter, &os2))
7672 return NULL;
7673 s1 = PyBytes_AsString(os1);
7674 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007675#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007676
7677#if defined(PYOS_OS2)
7678 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7679 APIRET rc;
7680
Guido van Rossumd48f2521997-12-05 22:19:34 +00007681 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007682 if (rc != NO_ERROR) {
7683 os2_error(rc);
7684 goto error;
7685 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007686
7687 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7688 APIRET rc;
7689
Guido van Rossumd48f2521997-12-05 22:19:34 +00007690 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007691 if (rc != NO_ERROR) {
7692 os2_error(rc);
7693 goto error;
7694 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007695 } else {
7696#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007697 /* XXX This can leak memory -- not easy to fix :-( */
7698 /* len includes space for a trailing \0; the size arg to
7699 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007700#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007701 len = wcslen(s1) + wcslen(s2) + 2;
7702 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007703#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007704 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007705 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007706#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007707 if (newstr == NULL) {
7708 PyErr_NoMemory();
7709 goto error;
7710 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007711#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 newenv = PyUnicode_AsUnicode(newstr);
7713 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7714 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007716 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007718#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 newenv = PyBytes_AS_STRING(newstr);
7720 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7721 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007723 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007724 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007725#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007726
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 /* Install the first arg and newstr in posix_putenv_garbage;
7728 * this will cause previous value to be collected. This has to
7729 * happen after the real putenv() call because the old value
7730 * was still accessible until then. */
7731 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007732#ifdef MS_WINDOWS
7733 PyTuple_GET_ITEM(args, 0),
7734#else
7735 os1,
7736#endif
7737 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007738 /* really not much we can do; just leak */
7739 PyErr_Clear();
7740 }
7741 else {
7742 Py_DECREF(newstr);
7743 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007744
7745#if defined(PYOS_OS2)
7746 }
7747#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007748
Martin v. Löwis011e8422009-05-05 04:43:17 +00007749#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 Py_DECREF(os1);
7751 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007752#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007753 Py_RETURN_NONE;
7754
7755error:
7756#ifndef MS_WINDOWS
7757 Py_DECREF(os1);
7758 Py_DECREF(os2);
7759#endif
7760 Py_XDECREF(newstr);
7761 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007762}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007763#endif /* putenv */
7764
Guido van Rossumc524d952001-10-19 01:31:59 +00007765#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007766PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007767"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007768Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007769
7770static PyObject *
7771posix_unsetenv(PyObject *self, PyObject *args)
7772{
Victor Stinner84ae1182010-05-06 22:05:07 +00007773#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007775
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7777 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007778#else
7779 PyObject *os1;
7780 char *s1;
7781
7782 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7783 PyUnicode_FSConverter, &os1))
7784 return NULL;
7785 s1 = PyBytes_AsString(os1);
7786#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007787
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007789
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 /* Remove the key from posix_putenv_garbage;
7791 * this will cause it to be collected. This has to
7792 * happen after the real unsetenv() call because the
7793 * old value was still accessible until then.
7794 */
7795 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007796#ifdef MS_WINDOWS
7797 PyTuple_GET_ITEM(args, 0)
7798#else
7799 os1
7800#endif
7801 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 /* really not much we can do; just leak */
7803 PyErr_Clear();
7804 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007805
Victor Stinner84ae1182010-05-06 22:05:07 +00007806#ifndef MS_WINDOWS
7807 Py_DECREF(os1);
7808#endif
7809 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007810}
7811#endif /* unsetenv */
7812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007813PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007814"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007815Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007816
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007818posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007819{
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 int code;
7821 char *message;
7822 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7823 return NULL;
7824 message = strerror(code);
7825 if (message == NULL) {
7826 PyErr_SetString(PyExc_ValueError,
7827 "strerror() argument out of range");
7828 return NULL;
7829 }
7830 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007831}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007832
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007833
Guido van Rossumc9641791998-08-04 15:26:23 +00007834#ifdef HAVE_SYS_WAIT_H
7835
Fred Drake106c1a02002-04-23 15:58:02 +00007836#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007837PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007838"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007839Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007840
7841static PyObject *
7842posix_WCOREDUMP(PyObject *self, PyObject *args)
7843{
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 WAIT_TYPE status;
7845 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007846
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7848 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007849
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007851}
7852#endif /* WCOREDUMP */
7853
7854#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007855PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007856"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007857Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007858job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007859
7860static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007861posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007862{
Victor Stinner8c62be82010-05-06 00:08:46 +00007863 WAIT_TYPE status;
7864 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007865
Victor Stinner8c62be82010-05-06 00:08:46 +00007866 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7867 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007868
Victor Stinner8c62be82010-05-06 00:08:46 +00007869 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007870}
7871#endif /* WIFCONTINUED */
7872
Guido van Rossumc9641791998-08-04 15:26:23 +00007873#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007874PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007875"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007876Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007877
7878static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007879posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007880{
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 WAIT_TYPE status;
7882 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007883
Victor Stinner8c62be82010-05-06 00:08:46 +00007884 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7885 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007886
Victor Stinner8c62be82010-05-06 00:08:46 +00007887 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007888}
7889#endif /* WIFSTOPPED */
7890
7891#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007892PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007893"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007894Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007895
7896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007897posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007898{
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 WAIT_TYPE status;
7900 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007901
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7903 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007904
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007906}
7907#endif /* WIFSIGNALED */
7908
7909#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007910PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007911"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007912Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007913system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007914
7915static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007916posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007917{
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 WAIT_TYPE status;
7919 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007920
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7922 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007923
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007925}
7926#endif /* WIFEXITED */
7927
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007928#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007929PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007930"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007931Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007932
7933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007934posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007935{
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 WAIT_TYPE status;
7937 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007938
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7940 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007941
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007943}
7944#endif /* WEXITSTATUS */
7945
7946#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007947PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007948"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007949Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007950value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007951
7952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007953posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007954{
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 WAIT_TYPE status;
7956 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007957
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7959 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007960
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007962}
7963#endif /* WTERMSIG */
7964
7965#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007966PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007967"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007968Return the signal that stopped the process that provided\n\
7969the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007970
7971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007972posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007973{
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 WAIT_TYPE status;
7975 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007976
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7978 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007979
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007981}
7982#endif /* WSTOPSIG */
7983
7984#endif /* HAVE_SYS_WAIT_H */
7985
7986
Thomas Wouters477c8d52006-05-27 19:21:47 +00007987#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007988#ifdef _SCO_DS
7989/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7990 needed definitions in sys/statvfs.h */
7991#define _SVID3
7992#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007993#include <sys/statvfs.h>
7994
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007995static PyObject*
7996_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7998 if (v == NULL)
7999 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008000
8001#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8003 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8004 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8005 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8006 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8007 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8008 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8009 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8010 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8011 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008012#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8014 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8015 PyStructSequence_SET_ITEM(v, 2,
8016 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8017 PyStructSequence_SET_ITEM(v, 3,
8018 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8019 PyStructSequence_SET_ITEM(v, 4,
8020 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8021 PyStructSequence_SET_ITEM(v, 5,
8022 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8023 PyStructSequence_SET_ITEM(v, 6,
8024 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8025 PyStructSequence_SET_ITEM(v, 7,
8026 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8027 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8028 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008029#endif
8030
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008032}
8033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008034PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008035"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008036Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008037
8038static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008039posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008040{
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 int fd, res;
8042 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008043
Victor Stinner8c62be82010-05-06 00:08:46 +00008044 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8045 return NULL;
8046 Py_BEGIN_ALLOW_THREADS
8047 res = fstatvfs(fd, &st);
8048 Py_END_ALLOW_THREADS
8049 if (res != 0)
8050 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008051
Victor Stinner8c62be82010-05-06 00:08:46 +00008052 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008053}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008054#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008055
8056
Thomas Wouters477c8d52006-05-27 19:21:47 +00008057#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008058#include <sys/statvfs.h>
8059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008060PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008061"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008062Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008063
8064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008065posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008066{
Victor Stinner8c62be82010-05-06 00:08:46 +00008067 char *path;
8068 int res;
8069 struct statvfs st;
8070 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
8071 return NULL;
8072 Py_BEGIN_ALLOW_THREADS
8073 res = statvfs(path, &st);
8074 Py_END_ALLOW_THREADS
8075 if (res != 0)
8076 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008077
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008079}
8080#endif /* HAVE_STATVFS */
8081
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008082#ifdef MS_WINDOWS
8083PyDoc_STRVAR(win32__getdiskusage__doc__,
8084"_getdiskusage(path) -> (total, free)\n\n\
8085Return disk usage statistics about the given path as (total, free) tuple.");
8086
8087static PyObject *
8088win32__getdiskusage(PyObject *self, PyObject *args)
8089{
8090 BOOL retval;
8091 ULARGE_INTEGER _, total, free;
8092 LPCTSTR path;
8093
8094 if (! PyArg_ParseTuple(args, "s", &path))
8095 return NULL;
8096
8097 Py_BEGIN_ALLOW_THREADS
8098 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8099 Py_END_ALLOW_THREADS
8100 if (retval == 0)
8101 return PyErr_SetFromWindowsErr(0);
8102
8103 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8104}
8105#endif
8106
8107
Fred Drakec9680921999-12-13 16:37:25 +00008108/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8109 * It maps strings representing configuration variable names to
8110 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008111 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008112 * rarely-used constants. There are three separate tables that use
8113 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008114 *
8115 * This code is always included, even if none of the interfaces that
8116 * need it are included. The #if hackery needed to avoid it would be
8117 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008118 */
8119struct constdef {
8120 char *name;
8121 long value;
8122};
8123
Fred Drake12c6e2d1999-12-14 21:25:03 +00008124static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008125conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008126 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008127{
Christian Heimes217cfd12007-12-02 14:31:20 +00008128 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008129 *valuep = PyLong_AS_LONG(arg);
8130 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008131 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008132 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008133 /* look up the value in the table using a binary search */
8134 size_t lo = 0;
8135 size_t mid;
8136 size_t hi = tablesize;
8137 int cmp;
8138 const char *confname;
8139 if (!PyUnicode_Check(arg)) {
8140 PyErr_SetString(PyExc_TypeError,
8141 "configuration names must be strings or integers");
8142 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008143 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008144 confname = _PyUnicode_AsString(arg);
8145 if (confname == NULL)
8146 return 0;
8147 while (lo < hi) {
8148 mid = (lo + hi) / 2;
8149 cmp = strcmp(confname, table[mid].name);
8150 if (cmp < 0)
8151 hi = mid;
8152 else if (cmp > 0)
8153 lo = mid + 1;
8154 else {
8155 *valuep = table[mid].value;
8156 return 1;
8157 }
8158 }
8159 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8160 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008161 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008162}
8163
8164
8165#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8166static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008167#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008169#endif
8170#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008171 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008172#endif
Fred Drakec9680921999-12-13 16:37:25 +00008173#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
8176#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008178#endif
8179#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008181#endif
8182#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008184#endif
8185#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
8188#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008189 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008190#endif
8191#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008192 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008193#endif
8194#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008195 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008196#endif
8197#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008198 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008199#endif
8200#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008201 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008202#endif
8203#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008204 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008205#endif
8206#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008207 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008208#endif
8209#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008210 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008211#endif
8212#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008213 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008214#endif
8215#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008216 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008217#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008218#ifdef _PC_ACL_ENABLED
8219 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8220#endif
8221#ifdef _PC_MIN_HOLE_SIZE
8222 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8223#endif
8224#ifdef _PC_ALLOC_SIZE_MIN
8225 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8226#endif
8227#ifdef _PC_REC_INCR_XFER_SIZE
8228 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8229#endif
8230#ifdef _PC_REC_MAX_XFER_SIZE
8231 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8232#endif
8233#ifdef _PC_REC_MIN_XFER_SIZE
8234 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8235#endif
8236#ifdef _PC_REC_XFER_ALIGN
8237 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8238#endif
8239#ifdef _PC_SYMLINK_MAX
8240 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8241#endif
8242#ifdef _PC_XATTR_ENABLED
8243 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8244#endif
8245#ifdef _PC_XATTR_EXISTS
8246 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8247#endif
8248#ifdef _PC_TIMESTAMP_RESOLUTION
8249 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8250#endif
Fred Drakec9680921999-12-13 16:37:25 +00008251};
8252
Fred Drakec9680921999-12-13 16:37:25 +00008253static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008254conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008255{
8256 return conv_confname(arg, valuep, posix_constants_pathconf,
8257 sizeof(posix_constants_pathconf)
8258 / sizeof(struct constdef));
8259}
8260#endif
8261
8262#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008263PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008264"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008265Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008266If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008267
8268static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008269posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008270{
8271 PyObject *result = NULL;
8272 int name, fd;
8273
Fred Drake12c6e2d1999-12-14 21:25:03 +00008274 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8275 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008276 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008277
Stefan Krah0e803b32010-11-26 16:16:47 +00008278 errno = 0;
8279 limit = fpathconf(fd, name);
8280 if (limit == -1 && errno != 0)
8281 posix_error();
8282 else
8283 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008284 }
8285 return result;
8286}
8287#endif
8288
8289
8290#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008291PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008292"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008293Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008294If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008295
8296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008297posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008298{
8299 PyObject *result = NULL;
8300 int name;
8301 char *path;
8302
8303 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8304 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008306
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 errno = 0;
8308 limit = pathconf(path, name);
8309 if (limit == -1 && errno != 0) {
8310 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008311 /* could be a path or name problem */
8312 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008313 else
Stefan Krah99439262010-11-26 12:58:05 +00008314 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008315 }
8316 else
8317 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008318 }
8319 return result;
8320}
8321#endif
8322
8323#ifdef HAVE_CONFSTR
8324static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008325#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008327#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008328#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008330#endif
8331#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008333#endif
Fred Draked86ed291999-12-15 15:34:33 +00008334#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008336#endif
8337#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008338 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008339#endif
8340#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008342#endif
8343#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008345#endif
Fred Drakec9680921999-12-13 16:37:25 +00008346#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008347 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008348#endif
8349#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008350 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008351#endif
8352#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008354#endif
8355#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008357#endif
8358#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008359 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008360#endif
8361#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008363#endif
8364#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008366#endif
8367#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008369#endif
Fred Draked86ed291999-12-15 15:34:33 +00008370#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008372#endif
Fred Drakec9680921999-12-13 16:37:25 +00008373#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008374 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008375#endif
Fred Draked86ed291999-12-15 15:34:33 +00008376#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008378#endif
8379#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008381#endif
8382#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008384#endif
8385#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008387#endif
Fred Drakec9680921999-12-13 16:37:25 +00008388#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008389 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008390#endif
8391#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008392 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008393#endif
8394#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008395 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008396#endif
8397#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008399#endif
8400#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008401 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008402#endif
8403#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008405#endif
8406#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008407 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008408#endif
8409#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008410 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008411#endif
8412#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008413 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008414#endif
8415#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008416 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008417#endif
8418#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008419 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008420#endif
8421#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008422 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008423#endif
8424#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008425 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008426#endif
8427#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008428 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008429#endif
8430#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008431 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008432#endif
8433#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008434 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008435#endif
Fred Draked86ed291999-12-15 15:34:33 +00008436#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008437 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008438#endif
8439#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008441#endif
8442#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008443 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008444#endif
8445#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008447#endif
8448#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008450#endif
8451#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008453#endif
8454#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008456#endif
8457#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008458 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008459#endif
8460#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008462#endif
8463#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008465#endif
8466#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008468#endif
8469#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008471#endif
8472#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008474#endif
Fred Drakec9680921999-12-13 16:37:25 +00008475};
8476
8477static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008478conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008479{
8480 return conv_confname(arg, valuep, posix_constants_confstr,
8481 sizeof(posix_constants_confstr)
8482 / sizeof(struct constdef));
8483}
8484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008485PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008486"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008487Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008488
8489static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008490posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008491{
8492 PyObject *result = NULL;
8493 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008494 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008495 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008496
Victor Stinnercb043522010-09-10 23:49:04 +00008497 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8498 return NULL;
8499
8500 errno = 0;
8501 len = confstr(name, buffer, sizeof(buffer));
8502 if (len == 0) {
8503 if (errno) {
8504 posix_error();
8505 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008506 }
8507 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008508 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008509 }
8510 }
Victor Stinnercb043522010-09-10 23:49:04 +00008511
8512 if ((unsigned int)len >= sizeof(buffer)) {
8513 char *buf = PyMem_Malloc(len);
8514 if (buf == NULL)
8515 return PyErr_NoMemory();
8516 confstr(name, buf, len);
8517 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8518 PyMem_Free(buf);
8519 }
8520 else
8521 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008522 return result;
8523}
8524#endif
8525
8526
8527#ifdef HAVE_SYSCONF
8528static struct constdef posix_constants_sysconf[] = {
8529#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008531#endif
8532#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008534#endif
8535#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008537#endif
8538#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008540#endif
8541#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008542 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008543#endif
8544#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008546#endif
8547#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008548 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008549#endif
8550#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008552#endif
8553#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008554 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008555#endif
8556#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008557 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008558#endif
Fred Draked86ed291999-12-15 15:34:33 +00008559#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008560 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008561#endif
8562#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008563 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008564#endif
Fred Drakec9680921999-12-13 16:37:25 +00008565#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008567#endif
Fred Drakec9680921999-12-13 16:37:25 +00008568#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008569 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008570#endif
8571#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008573#endif
8574#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008575 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008576#endif
8577#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008578 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008579#endif
8580#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008581 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008582#endif
Fred Draked86ed291999-12-15 15:34:33 +00008583#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008584 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008585#endif
Fred Drakec9680921999-12-13 16:37:25 +00008586#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008587 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008588#endif
8589#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008590 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008591#endif
8592#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008593 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008594#endif
8595#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008596 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008597#endif
8598#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008600#endif
Fred Draked86ed291999-12-15 15:34:33 +00008601#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008602 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008603#endif
Fred Drakec9680921999-12-13 16:37:25 +00008604#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008606#endif
8607#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008608 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008609#endif
8610#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008611 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008612#endif
8613#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008614 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008615#endif
8616#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008618#endif
8619#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008620 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008621#endif
8622#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008623 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008624#endif
8625#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008627#endif
8628#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008630#endif
8631#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008633#endif
8634#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008636#endif
8637#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008638 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008639#endif
8640#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008641 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008642#endif
8643#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008645#endif
8646#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008648#endif
8649#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008651#endif
8652#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008654#endif
8655#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008657#endif
8658#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008660#endif
8661#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008662 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008663#endif
8664#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008666#endif
8667#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008669#endif
8670#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008672#endif
Fred Draked86ed291999-12-15 15:34:33 +00008673#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008675#endif
Fred Drakec9680921999-12-13 16:37:25 +00008676#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008678#endif
8679#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008680 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008681#endif
8682#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008683 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008684#endif
Fred Draked86ed291999-12-15 15:34:33 +00008685#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008686 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008687#endif
Fred Drakec9680921999-12-13 16:37:25 +00008688#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008690#endif
Fred Draked86ed291999-12-15 15:34:33 +00008691#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008692 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008693#endif
8694#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008696#endif
Fred Drakec9680921999-12-13 16:37:25 +00008697#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008698 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008699#endif
8700#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008702#endif
8703#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008705#endif
8706#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008708#endif
Fred Draked86ed291999-12-15 15:34:33 +00008709#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008711#endif
Fred Drakec9680921999-12-13 16:37:25 +00008712#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008714#endif
8715#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008717#endif
8718#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008720#endif
8721#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008723#endif
8724#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008726#endif
8727#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008729#endif
8730#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008732#endif
Fred Draked86ed291999-12-15 15:34:33 +00008733#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008735#endif
Fred Drakec9680921999-12-13 16:37:25 +00008736#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
8739#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
Fred Draked86ed291999-12-15 15:34:33 +00008742#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008744#endif
Fred Drakec9680921999-12-13 16:37:25 +00008745#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008747#endif
8748#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008750#endif
8751#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008753#endif
8754#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008756#endif
8757#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008759#endif
8760#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008762#endif
8763#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008765#endif
8766#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008768#endif
8769#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008771#endif
Fred Draked86ed291999-12-15 15:34:33 +00008772#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008774#endif
8775#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008777#endif
Fred Drakec9680921999-12-13 16:37:25 +00008778#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008780#endif
8781#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008783#endif
8784#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008786#endif
8787#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
8790#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008792#endif
8793#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008795#endif
8796#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008798#endif
8799#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008801#endif
8802#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008804#endif
8805#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008807#endif
8808#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008810#endif
8811#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008813#endif
8814#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008816#endif
8817#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008819#endif
8820#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008822#endif
8823#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008825#endif
8826#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008828#endif
8829#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008831#endif
8832#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008834#endif
8835#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008837#endif
8838#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008839 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008840#endif
8841#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008842 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008843#endif
8844#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008845 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008846#endif
8847#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008849#endif
8850#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008852#endif
8853#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008855#endif
8856#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008858#endif
8859#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008861#endif
8862#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008863 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008864#endif
8865#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008866 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008867#endif
8868#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008870#endif
8871#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008872 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008873#endif
8874#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008876#endif
8877#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008879#endif
8880#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008882#endif
Fred Draked86ed291999-12-15 15:34:33 +00008883#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008884 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008885#endif
Fred Drakec9680921999-12-13 16:37:25 +00008886#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008888#endif
8889#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008891#endif
8892#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008894#endif
8895#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008897#endif
8898#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008900#endif
8901#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008903#endif
8904#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008906#endif
8907#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008909#endif
8910#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008912#endif
8913#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008914 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008915#endif
8916#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008918#endif
8919#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008920 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008926 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
8931#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008933#endif
8934#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008935 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008936#endif
8937#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008938 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008939#endif
8940#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008941 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008942#endif
8943#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008944 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008945#endif
8946#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008948#endif
8949#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008950 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008951#endif
8952#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008954#endif
8955#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008957#endif
8958#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008959 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008960#endif
8961#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008962 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008963#endif
8964#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008965 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008966#endif
8967#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008968 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008969#endif
8970#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008971 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008972#endif
8973#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008974 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008975#endif
8976#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008977 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008978#endif
8979#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008980 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008981#endif
8982#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008984#endif
8985#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008986 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008987#endif
8988#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008989 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008990#endif
8991#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008992 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008993#endif
8994#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008995 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008996#endif
8997#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008998 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008999#endif
9000#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009001 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009002#endif
9003#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009004 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009005#endif
9006#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009008#endif
9009#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009010 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009011#endif
9012#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009013 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009014#endif
9015#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009016 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009017#endif
9018#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009019 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009020#endif
9021};
9022
9023static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009024conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009025{
9026 return conv_confname(arg, valuep, posix_constants_sysconf,
9027 sizeof(posix_constants_sysconf)
9028 / sizeof(struct constdef));
9029}
9030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009031PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009032"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009033Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009034
9035static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009036posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009037{
9038 PyObject *result = NULL;
9039 int name;
9040
9041 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9042 int value;
9043
9044 errno = 0;
9045 value = sysconf(name);
9046 if (value == -1 && errno != 0)
9047 posix_error();
9048 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009049 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009050 }
9051 return result;
9052}
9053#endif
9054
9055
Fred Drakebec628d1999-12-15 18:31:10 +00009056/* This code is used to ensure that the tables of configuration value names
9057 * are in sorted order as required by conv_confname(), and also to build the
9058 * the exported dictionaries that are used to publish information about the
9059 * names available on the host platform.
9060 *
9061 * Sorting the table at runtime ensures that the table is properly ordered
9062 * when used, even for platforms we're not able to test on. It also makes
9063 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009064 */
Fred Drakebec628d1999-12-15 18:31:10 +00009065
9066static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009067cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009068{
9069 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009070 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009071 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009072 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009073
9074 return strcmp(c1->name, c2->name);
9075}
9076
9077static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009078setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009079 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009080{
Fred Drakebec628d1999-12-15 18:31:10 +00009081 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009082 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009083
9084 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9085 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009086 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009087 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009088
Barry Warsaw3155db32000-04-13 15:20:40 +00009089 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009090 PyObject *o = PyLong_FromLong(table[i].value);
9091 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9092 Py_XDECREF(o);
9093 Py_DECREF(d);
9094 return -1;
9095 }
9096 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009097 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009098 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009099}
9100
Fred Drakebec628d1999-12-15 18:31:10 +00009101/* Return -1 on failure, 0 on success. */
9102static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009103setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009104{
9105#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009106 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009107 sizeof(posix_constants_pathconf)
9108 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009109 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009110 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009111#endif
9112#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009113 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009114 sizeof(posix_constants_confstr)
9115 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009116 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009117 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009118#endif
9119#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009120 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009121 sizeof(posix_constants_sysconf)
9122 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009123 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009124 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009125#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009126 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009127}
Fred Draked86ed291999-12-15 15:34:33 +00009128
9129
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009130PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009131"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009132Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009133in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009134
9135static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009136posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009137{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009138 abort();
9139 /*NOTREACHED*/
9140 Py_FatalError("abort() called from Python code didn't abort!");
9141 return NULL;
9142}
Fred Drakebec628d1999-12-15 18:31:10 +00009143
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009144#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009145PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009146"startfile(filepath [, operation]) - Start a file with its associated\n\
9147application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009148\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009149When \"operation\" is not specified or \"open\", this acts like\n\
9150double-clicking the file in Explorer, or giving the file name as an\n\
9151argument to the DOS \"start\" command: the file is opened with whatever\n\
9152application (if any) its extension is associated.\n\
9153When another \"operation\" is given, it specifies what should be done with\n\
9154the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009155\n\
9156startfile returns as soon as the associated application is launched.\n\
9157There is no option to wait for the application to close, and no way\n\
9158to retrieve the application's exit status.\n\
9159\n\
9160The filepath is relative to the current directory. If you want to use\n\
9161an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009162the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009163
9164static PyObject *
9165win32_startfile(PyObject *self, PyObject *args)
9166{
Victor Stinner8c62be82010-05-06 00:08:46 +00009167 PyObject *ofilepath;
9168 char *filepath;
9169 char *operation = NULL;
9170 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009171
Victor Stinner8c62be82010-05-06 00:08:46 +00009172 PyObject *unipath, *woperation = NULL;
9173 if (!PyArg_ParseTuple(args, "U|s:startfile",
9174 &unipath, &operation)) {
9175 PyErr_Clear();
9176 goto normal;
9177 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009178
Victor Stinner8c62be82010-05-06 00:08:46 +00009179 if (operation) {
9180 woperation = PyUnicode_DecodeASCII(operation,
9181 strlen(operation), NULL);
9182 if (!woperation) {
9183 PyErr_Clear();
9184 operation = NULL;
9185 goto normal;
9186 }
9187 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009188
Victor Stinner8c62be82010-05-06 00:08:46 +00009189 Py_BEGIN_ALLOW_THREADS
9190 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9191 PyUnicode_AS_UNICODE(unipath),
9192 NULL, NULL, SW_SHOWNORMAL);
9193 Py_END_ALLOW_THREADS
9194
9195 Py_XDECREF(woperation);
9196 if (rc <= (HINSTANCE)32) {
9197 PyObject *errval = win32_error_unicode("startfile",
9198 PyUnicode_AS_UNICODE(unipath));
9199 return errval;
9200 }
9201 Py_INCREF(Py_None);
9202 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009203
9204normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009205 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9206 PyUnicode_FSConverter, &ofilepath,
9207 &operation))
9208 return NULL;
9209 filepath = PyBytes_AsString(ofilepath);
9210 Py_BEGIN_ALLOW_THREADS
9211 rc = ShellExecute((HWND)0, operation, filepath,
9212 NULL, NULL, SW_SHOWNORMAL);
9213 Py_END_ALLOW_THREADS
9214 if (rc <= (HINSTANCE)32) {
9215 PyObject *errval = win32_error("startfile", filepath);
9216 Py_DECREF(ofilepath);
9217 return errval;
9218 }
9219 Py_DECREF(ofilepath);
9220 Py_INCREF(Py_None);
9221 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009222}
9223#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009224
Martin v. Löwis438b5342002-12-27 10:16:42 +00009225#ifdef HAVE_GETLOADAVG
9226PyDoc_STRVAR(posix_getloadavg__doc__,
9227"getloadavg() -> (float, float, float)\n\n\
9228Return the number of processes in the system run queue averaged over\n\
9229the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9230was unobtainable");
9231
9232static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009233posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009234{
9235 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009236 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009237 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9238 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009239 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009240 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009241}
9242#endif
9243
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009244#ifdef MS_WINDOWS
9245
9246PyDoc_STRVAR(win32_urandom__doc__,
9247"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009248Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009249
9250typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9251 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9252 DWORD dwFlags );
9253typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9254 BYTE *pbBuffer );
9255
9256static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009257/* This handle is never explicitly released. Instead, the operating
9258 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009259static HCRYPTPROV hCryptProv = 0;
9260
Tim Peters4ad82172004-08-30 17:02:04 +00009261static PyObject*
9262win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009263{
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 int howMany;
9265 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009266
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 /* Read arguments */
9268 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9269 return NULL;
9270 if (howMany < 0)
9271 return PyErr_Format(PyExc_ValueError,
9272 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009273
Victor Stinner8c62be82010-05-06 00:08:46 +00009274 if (hCryptProv == 0) {
9275 HINSTANCE hAdvAPI32 = NULL;
9276 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009277
Victor Stinner8c62be82010-05-06 00:08:46 +00009278 /* Obtain handle to the DLL containing CryptoAPI
9279 This should not fail */
9280 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9281 if(hAdvAPI32 == NULL)
9282 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009283
Victor Stinner8c62be82010-05-06 00:08:46 +00009284 /* Obtain pointers to the CryptoAPI functions
9285 This will fail on some early versions of Win95 */
9286 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9287 hAdvAPI32,
9288 "CryptAcquireContextA");
9289 if (pCryptAcquireContext == NULL)
9290 return PyErr_Format(PyExc_NotImplementedError,
9291 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009292
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9294 hAdvAPI32, "CryptGenRandom");
9295 if (pCryptGenRandom == NULL)
9296 return PyErr_Format(PyExc_NotImplementedError,
9297 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009298
Victor Stinner8c62be82010-05-06 00:08:46 +00009299 /* Acquire context */
9300 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9301 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9302 return win32_error("CryptAcquireContext", NULL);
9303 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009304
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 /* Allocate bytes */
9306 result = PyBytes_FromStringAndSize(NULL, howMany);
9307 if (result != NULL) {
9308 /* Get random data */
9309 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9310 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9311 PyBytes_AS_STRING(result))) {
9312 Py_DECREF(result);
9313 return win32_error("CryptGenRandom", NULL);
9314 }
9315 }
9316 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009317}
9318#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009319
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009320PyDoc_STRVAR(device_encoding__doc__,
9321"device_encoding(fd) -> str\n\n\
9322Return a string describing the encoding of the device\n\
9323if the output is a terminal; else return None.");
9324
9325static PyObject *
9326device_encoding(PyObject *self, PyObject *args)
9327{
Victor Stinner8c62be82010-05-06 00:08:46 +00009328 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009329#if defined(MS_WINDOWS) || defined(MS_WIN64)
9330 UINT cp;
9331#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9333 return NULL;
9334 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9335 Py_INCREF(Py_None);
9336 return Py_None;
9337 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009338#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009339 if (fd == 0)
9340 cp = GetConsoleCP();
9341 else if (fd == 1 || fd == 2)
9342 cp = GetConsoleOutputCP();
9343 else
9344 cp = 0;
9345 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9346 has no console */
9347 if (cp != 0)
9348 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009349#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 {
9351 char *codeset = nl_langinfo(CODESET);
9352 if (codeset != NULL && codeset[0] != 0)
9353 return PyUnicode_FromString(codeset);
9354 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009355#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 Py_INCREF(Py_None);
9357 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009358}
9359
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009360#ifdef __VMS
9361/* Use openssl random routine */
9362#include <openssl/rand.h>
9363PyDoc_STRVAR(vms_urandom__doc__,
9364"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009365Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009366
9367static PyObject*
9368vms_urandom(PyObject *self, PyObject *args)
9369{
Victor Stinner8c62be82010-05-06 00:08:46 +00009370 int howMany;
9371 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009372
Victor Stinner8c62be82010-05-06 00:08:46 +00009373 /* Read arguments */
9374 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9375 return NULL;
9376 if (howMany < 0)
9377 return PyErr_Format(PyExc_ValueError,
9378 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009379
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 /* Allocate bytes */
9381 result = PyBytes_FromStringAndSize(NULL, howMany);
9382 if (result != NULL) {
9383 /* Get random data */
9384 if (RAND_pseudo_bytes((unsigned char*)
9385 PyBytes_AS_STRING(result),
9386 howMany) < 0) {
9387 Py_DECREF(result);
9388 return PyErr_Format(PyExc_ValueError,
9389 "RAND_pseudo_bytes");
9390 }
9391 }
9392 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009393}
9394#endif
9395
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009396#ifdef HAVE_SETRESUID
9397PyDoc_STRVAR(posix_setresuid__doc__,
9398"setresuid(ruid, euid, suid)\n\n\
9399Set the current process's real, effective, and saved user ids.");
9400
9401static PyObject*
9402posix_setresuid (PyObject *self, PyObject *args)
9403{
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 /* We assume uid_t is no larger than a long. */
9405 long ruid, euid, suid;
9406 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9407 return NULL;
9408 if (setresuid(ruid, euid, suid) < 0)
9409 return posix_error();
9410 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009411}
9412#endif
9413
9414#ifdef HAVE_SETRESGID
9415PyDoc_STRVAR(posix_setresgid__doc__,
9416"setresgid(rgid, egid, sgid)\n\n\
9417Set the current process's real, effective, and saved group ids.");
9418
9419static PyObject*
9420posix_setresgid (PyObject *self, PyObject *args)
9421{
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 /* We assume uid_t is no larger than a long. */
9423 long rgid, egid, sgid;
9424 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9425 return NULL;
9426 if (setresgid(rgid, egid, sgid) < 0)
9427 return posix_error();
9428 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009429}
9430#endif
9431
9432#ifdef HAVE_GETRESUID
9433PyDoc_STRVAR(posix_getresuid__doc__,
9434"getresuid() -> (ruid, euid, suid)\n\n\
9435Get tuple of the current process's real, effective, and saved user ids.");
9436
9437static PyObject*
9438posix_getresuid (PyObject *self, PyObject *noargs)
9439{
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 uid_t ruid, euid, suid;
9441 long l_ruid, l_euid, l_suid;
9442 if (getresuid(&ruid, &euid, &suid) < 0)
9443 return posix_error();
9444 /* Force the values into long's as we don't know the size of uid_t. */
9445 l_ruid = ruid;
9446 l_euid = euid;
9447 l_suid = suid;
9448 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009449}
9450#endif
9451
9452#ifdef HAVE_GETRESGID
9453PyDoc_STRVAR(posix_getresgid__doc__,
9454"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009455Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009456
9457static PyObject*
9458posix_getresgid (PyObject *self, PyObject *noargs)
9459{
Victor Stinner8c62be82010-05-06 00:08:46 +00009460 uid_t rgid, egid, sgid;
9461 long l_rgid, l_egid, l_sgid;
9462 if (getresgid(&rgid, &egid, &sgid) < 0)
9463 return posix_error();
9464 /* Force the values into long's as we don't know the size of uid_t. */
9465 l_rgid = rgid;
9466 l_egid = egid;
9467 l_sgid = sgid;
9468 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009469}
9470#endif
9471
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009472/* Posix *at family of functions:
9473 faccessat, fchmodat, fchownat, fstatat, futimesat,
9474 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9475 unlinkat, utimensat, mkfifoat */
9476
9477#ifdef HAVE_FACCESSAT
9478PyDoc_STRVAR(posix_faccessat__doc__,
9479"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9480Like access() but if path is relative, it is taken as relative to dirfd.\n\
9481flags is optional and can be constructed by ORing together zero or more\n\
9482of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9483If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9484is interpreted relative to the current working directory.");
9485
9486static PyObject *
9487posix_faccessat(PyObject *self, PyObject *args)
9488{
9489 PyObject *opath;
9490 char *path;
9491 int mode;
9492 int res;
9493 int dirfd, flags = 0;
9494 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9495 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9496 return NULL;
9497 path = PyBytes_AsString(opath);
9498 Py_BEGIN_ALLOW_THREADS
9499 res = faccessat(dirfd, path, mode, flags);
9500 Py_END_ALLOW_THREADS
9501 Py_DECREF(opath);
9502 return PyBool_FromLong(res == 0);
9503}
9504#endif
9505
9506#ifdef HAVE_FCHMODAT
9507PyDoc_STRVAR(posix_fchmodat__doc__,
9508"fchmodat(dirfd, path, mode, flags=0)\n\n\
9509Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9510flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9511If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9512is interpreted relative to the current working directory.");
9513
9514static PyObject *
9515posix_fchmodat(PyObject *self, PyObject *args)
9516{
9517 int dirfd, mode, res;
9518 int flags = 0;
9519 PyObject *opath;
9520 char *path;
9521
9522 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9523 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9524 return NULL;
9525
9526 path = PyBytes_AsString(opath);
9527
9528 Py_BEGIN_ALLOW_THREADS
9529 res = fchmodat(dirfd, path, mode, flags);
9530 Py_END_ALLOW_THREADS
9531 Py_DECREF(opath);
9532 if (res < 0)
9533 return posix_error();
9534 Py_RETURN_NONE;
9535}
9536#endif /* HAVE_FCHMODAT */
9537
9538#ifdef HAVE_FCHOWNAT
9539PyDoc_STRVAR(posix_fchownat__doc__,
9540"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9541Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9542flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9543If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9544is interpreted relative to the current working directory.");
9545
9546static PyObject *
9547posix_fchownat(PyObject *self, PyObject *args)
9548{
9549 PyObject *opath;
9550 int dirfd, res;
9551 long uid, gid;
9552 int flags = 0;
9553 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009554
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009555 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9556 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9557 return NULL;
9558
9559 path = PyBytes_AsString(opath);
9560
9561 Py_BEGIN_ALLOW_THREADS
9562 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9563 Py_END_ALLOW_THREADS
9564 Py_DECREF(opath);
9565 if (res < 0)
9566 return posix_error();
9567 Py_RETURN_NONE;
9568}
9569#endif /* HAVE_FCHOWNAT */
9570
9571#ifdef HAVE_FSTATAT
9572PyDoc_STRVAR(posix_fstatat__doc__,
9573"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9574Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9575flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9576If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9577is interpreted relative to the current working directory.");
9578
9579static PyObject *
9580posix_fstatat(PyObject *self, PyObject *args)
9581{
9582 PyObject *opath;
9583 char *path;
9584 STRUCT_STAT st;
9585 int dirfd, res, flags = 0;
9586
9587 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9588 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9589 return NULL;
9590 path = PyBytes_AsString(opath);
9591
9592 Py_BEGIN_ALLOW_THREADS
9593 res = fstatat(dirfd, path, &st, flags);
9594 Py_END_ALLOW_THREADS
9595 Py_DECREF(opath);
9596 if (res != 0)
9597 return posix_error();
9598
9599 return _pystat_fromstructstat(&st);
9600}
9601#endif
9602
9603#ifdef HAVE_FUTIMESAT
9604PyDoc_STRVAR(posix_futimesat__doc__,
9605"futimesat(dirfd, path, (atime, mtime))\n\
9606futimesat(dirfd, path, None)\n\n\
9607Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9608If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9609is interpreted relative to the current working directory.");
9610
9611static PyObject *
9612posix_futimesat(PyObject *self, PyObject *args)
9613{
9614 PyObject *opath;
9615 char *path;
9616 int res, dirfd;
9617 PyObject* arg;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009618 time_t atime, mtime;
9619 long ausec, musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009620
9621 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9622 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9623 return NULL;
9624 path = PyBytes_AsString(opath);
9625 if (arg == Py_None) {
9626 /* optional time values not given */
9627 Py_BEGIN_ALLOW_THREADS
9628 res = futimesat(dirfd, path, NULL);
9629 Py_END_ALLOW_THREADS
9630 }
9631 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9632 PyErr_SetString(PyExc_TypeError,
9633 "futimesat() arg 3 must be a tuple (atime, mtime)");
9634 Py_DECREF(opath);
9635 return NULL;
9636 }
9637 else {
9638 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009639 &atime, &ausec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009640 Py_DECREF(opath);
9641 return NULL;
9642 }
9643 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009644 &mtime, &musec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009645 Py_DECREF(opath);
9646 return NULL;
9647 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009648
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009649 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009650 {
9651#ifdef HAVE_UTIMENSAT
9652 struct timespec buf[2];
9653 buf[0].tv_sec = atime;
9654 buf[0].tv_nsec = ausec;
9655 buf[1].tv_sec = mtime;
9656 buf[1].tv_nsec = musec;
9657 res = utimensat(dirfd, path, buf, 0);
9658#else
9659 struct timeval buf[2];
9660 buf[0].tv_sec = atime;
9661 buf[0].tv_usec = ausec;
9662 buf[1].tv_sec = mtime;
9663 buf[1].tv_usec = musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009664 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009665#endif
9666 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009667 Py_END_ALLOW_THREADS
9668 }
9669 Py_DECREF(opath);
9670 if (res < 0) {
9671 return posix_error();
9672 }
9673 Py_RETURN_NONE;
9674}
9675#endif
9676
9677#ifdef HAVE_LINKAT
9678PyDoc_STRVAR(posix_linkat__doc__,
9679"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9680Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9681and if dstpath is relative, it is taken as relative to dstfd.\n\
9682flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9683If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9684srcpath is interpreted relative to the current working directory. This\n\
9685also applies for dstpath.");
9686
9687static PyObject *
9688posix_linkat(PyObject *self, PyObject *args)
9689{
9690 PyObject *osrc, *odst;
9691 char *src, *dst;
9692 int res, srcfd, dstfd;
9693 int flags = 0;
9694
9695 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9696 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9697 return NULL;
9698 src = PyBytes_AsString(osrc);
9699 dst = PyBytes_AsString(odst);
9700 Py_BEGIN_ALLOW_THREADS
9701 res = linkat(srcfd, src, dstfd, dst, flags);
9702 Py_END_ALLOW_THREADS
9703 Py_DECREF(osrc);
9704 Py_DECREF(odst);
9705 if (res < 0)
9706 return posix_error();
9707 Py_RETURN_NONE;
9708}
9709#endif /* HAVE_LINKAT */
9710
9711#ifdef HAVE_MKDIRAT
9712PyDoc_STRVAR(posix_mkdirat__doc__,
9713"mkdirat(dirfd, path, mode=0o777)\n\n\
9714Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9715If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9716is interpreted relative to the current working directory.");
9717
9718static PyObject *
9719posix_mkdirat(PyObject *self, PyObject *args)
9720{
9721 int res, dirfd;
9722 PyObject *opath;
9723 char *path;
9724 int mode = 0777;
9725
9726 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9727 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9728 return NULL;
9729 path = PyBytes_AsString(opath);
9730 Py_BEGIN_ALLOW_THREADS
9731 res = mkdirat(dirfd, path, mode);
9732 Py_END_ALLOW_THREADS
9733 Py_DECREF(opath);
9734 if (res < 0)
9735 return posix_error();
9736 Py_RETURN_NONE;
9737}
9738#endif
9739
9740#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9741PyDoc_STRVAR(posix_mknodat__doc__,
9742"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9743Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9744If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9745is interpreted relative to the current working directory.");
9746
9747static PyObject *
9748posix_mknodat(PyObject *self, PyObject *args)
9749{
9750 PyObject *opath;
9751 char *filename;
9752 int mode = 0600;
9753 int device = 0;
9754 int res, dirfd;
9755 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9756 PyUnicode_FSConverter, &opath, &mode, &device))
9757 return NULL;
9758 filename = PyBytes_AS_STRING(opath);
9759 Py_BEGIN_ALLOW_THREADS
9760 res = mknodat(dirfd, filename, mode, device);
9761 Py_END_ALLOW_THREADS
9762 Py_DECREF(opath);
9763 if (res < 0)
9764 return posix_error();
9765 Py_RETURN_NONE;
9766}
9767#endif
9768
9769#ifdef HAVE_OPENAT
9770PyDoc_STRVAR(posix_openat__doc__,
9771"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9772Like open() but if path is relative, it is taken as relative to dirfd.\n\
9773If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9774is interpreted relative to the current working directory.");
9775
9776static PyObject *
9777posix_openat(PyObject *self, PyObject *args)
9778{
9779 PyObject *ofile;
9780 char *file;
9781 int flag, dirfd, fd;
9782 int mode = 0777;
9783
9784 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9785 &dirfd, PyUnicode_FSConverter, &ofile,
9786 &flag, &mode))
9787 return NULL;
9788 file = PyBytes_AsString(ofile);
9789 Py_BEGIN_ALLOW_THREADS
9790 fd = openat(dirfd, file, flag, mode);
9791 Py_END_ALLOW_THREADS
9792 Py_DECREF(ofile);
9793 if (fd < 0)
9794 return posix_error();
9795 return PyLong_FromLong((long)fd);
9796}
9797#endif
9798
9799#ifdef HAVE_READLINKAT
9800PyDoc_STRVAR(posix_readlinkat__doc__,
9801"readlinkat(dirfd, path) -> path\n\n\
9802Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9803If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9804is interpreted relative to the current working directory.");
9805
9806static PyObject *
9807posix_readlinkat(PyObject *self, PyObject *args)
9808{
9809 PyObject *v, *opath;
9810 char buf[MAXPATHLEN];
9811 char *path;
9812 int n, dirfd;
9813 int arg_is_unicode = 0;
9814
9815 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9816 &dirfd, PyUnicode_FSConverter, &opath))
9817 return NULL;
9818 path = PyBytes_AsString(opath);
9819 v = PySequence_GetItem(args, 1);
9820 if (v == NULL) {
9821 Py_DECREF(opath);
9822 return NULL;
9823 }
9824
9825 if (PyUnicode_Check(v)) {
9826 arg_is_unicode = 1;
9827 }
9828 Py_DECREF(v);
9829
9830 Py_BEGIN_ALLOW_THREADS
9831 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9832 Py_END_ALLOW_THREADS
9833 Py_DECREF(opath);
9834 if (n < 0)
9835 return posix_error();
9836
9837 if (arg_is_unicode)
9838 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9839 else
9840 return PyBytes_FromStringAndSize(buf, n);
9841}
9842#endif /* HAVE_READLINKAT */
9843
9844#ifdef HAVE_RENAMEAT
9845PyDoc_STRVAR(posix_renameat__doc__,
9846"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9847Like rename() but if oldpath is relative, it is taken as relative to\n\
9848olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9849If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9850oldpath is interpreted relative to the current working directory. This\n\
9851also applies for newpath.");
9852
9853static PyObject *
9854posix_renameat(PyObject *self, PyObject *args)
9855{
9856 int res;
9857 PyObject *opathold, *opathnew;
9858 char *opath, *npath;
9859 int oldfd, newfd;
9860
9861 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9862 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9863 return NULL;
9864 opath = PyBytes_AsString(opathold);
9865 npath = PyBytes_AsString(opathnew);
9866 Py_BEGIN_ALLOW_THREADS
9867 res = renameat(oldfd, opath, newfd, npath);
9868 Py_END_ALLOW_THREADS
9869 Py_DECREF(opathold);
9870 Py_DECREF(opathnew);
9871 if (res < 0)
9872 return posix_error();
9873 Py_RETURN_NONE;
9874}
9875#endif
9876
9877#if HAVE_SYMLINKAT
9878PyDoc_STRVAR(posix_symlinkat__doc__,
9879"symlinkat(src, dstfd, dst)\n\n\
9880Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9881If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9882is interpreted relative to the current working directory.");
9883
9884static PyObject *
9885posix_symlinkat(PyObject *self, PyObject *args)
9886{
9887 int res, dstfd;
9888 PyObject *osrc, *odst;
9889 char *src, *dst;
9890
9891 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9892 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9893 return NULL;
9894 src = PyBytes_AsString(osrc);
9895 dst = PyBytes_AsString(odst);
9896 Py_BEGIN_ALLOW_THREADS
9897 res = symlinkat(src, dstfd, dst);
9898 Py_END_ALLOW_THREADS
9899 Py_DECREF(osrc);
9900 Py_DECREF(odst);
9901 if (res < 0)
9902 return posix_error();
9903 Py_RETURN_NONE;
9904}
9905#endif /* HAVE_SYMLINKAT */
9906
9907#ifdef HAVE_UNLINKAT
9908PyDoc_STRVAR(posix_unlinkat__doc__,
9909"unlinkat(dirfd, path, flags=0)\n\n\
9910Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9911flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9912specified, unlinkat() behaves like rmdir().\n\
9913If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9914is interpreted relative to the current working directory.");
9915
9916static PyObject *
9917posix_unlinkat(PyObject *self, PyObject *args)
9918{
9919 int dirfd, res, flags = 0;
9920 PyObject *opath;
9921 char *path;
9922
9923 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9924 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9925 return NULL;
9926 path = PyBytes_AsString(opath);
9927 Py_BEGIN_ALLOW_THREADS
9928 res = unlinkat(dirfd, path, flags);
9929 Py_END_ALLOW_THREADS
9930 Py_DECREF(opath);
9931 if (res < 0)
9932 return posix_error();
9933 Py_RETURN_NONE;
9934}
9935#endif
9936
9937#ifdef HAVE_UTIMENSAT
9938PyDoc_STRVAR(posix_utimensat__doc__,
9939"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9940 (mtime_sec, mtime_nsec), flags)\n\
9941utimensat(dirfd, path, None, None, flags)\n\n\
9942Updates the timestamps of a file with nanosecond precision. If path is\n\
9943relative, it is taken as relative to dirfd.\n\
9944The second form sets atime and mtime to the current time.\n\
9945flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9946If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9947is interpreted relative to the current working directory.\n\
9948If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9949current time.\n\
9950If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9951
9952static PyObject *
9953posix_utimensat(PyObject *self, PyObject *args)
9954{
9955 PyObject *opath;
9956 char *path;
9957 int res, dirfd, flags = 0;
9958 PyObject *atime, *mtime;
9959
9960 struct timespec buf[2];
9961
9962 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9963 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9964 return NULL;
9965 path = PyBytes_AsString(opath);
9966 if (atime == Py_None && mtime == Py_None) {
9967 /* optional time values not given */
9968 Py_BEGIN_ALLOW_THREADS
9969 res = utimensat(dirfd, path, NULL, flags);
9970 Py_END_ALLOW_THREADS
9971 }
9972 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9973 PyErr_SetString(PyExc_TypeError,
9974 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9975 Py_DECREF(opath);
9976 return NULL;
9977 }
9978 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9979 PyErr_SetString(PyExc_TypeError,
9980 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9981 Py_DECREF(opath);
9982 return NULL;
9983 }
9984 else {
9985 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9986 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9987 Py_DECREF(opath);
9988 return NULL;
9989 }
9990 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9991 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9992 Py_DECREF(opath);
9993 return NULL;
9994 }
9995 Py_BEGIN_ALLOW_THREADS
9996 res = utimensat(dirfd, path, buf, flags);
9997 Py_END_ALLOW_THREADS
9998 }
9999 Py_DECREF(opath);
10000 if (res < 0) {
10001 return posix_error();
10002 }
10003 Py_RETURN_NONE;
10004}
10005#endif
10006
10007#ifdef HAVE_MKFIFOAT
10008PyDoc_STRVAR(posix_mkfifoat__doc__,
10009"mkfifoat(dirfd, path, mode=0o666)\n\n\
10010Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10011If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10012is interpreted relative to the current working directory.");
10013
10014static PyObject *
10015posix_mkfifoat(PyObject *self, PyObject *args)
10016{
10017 PyObject *opath;
10018 char *filename;
10019 int mode = 0666;
10020 int res, dirfd;
10021 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10022 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10023 return NULL;
10024 filename = PyBytes_AS_STRING(opath);
10025 Py_BEGIN_ALLOW_THREADS
10026 res = mkfifoat(dirfd, filename, mode);
10027 Py_END_ALLOW_THREADS
10028 Py_DECREF(opath);
10029 if (res < 0)
10030 return posix_error();
10031 Py_RETURN_NONE;
10032}
10033#endif
10034
Benjamin Petersonb77fe172011-09-13 17:20:47 -040010035#ifdef HAVE_SYS_XATTR_H
Benjamin Peterson799bd802011-08-31 22:15:17 -040010036
10037static int
10038try_getxattr(const char *path, const char *name,
10039 ssize_t (*get)(const char *, const char *, void *, size_t),
10040 Py_ssize_t buf_size, PyObject **res)
10041{
10042 PyObject *value;
10043 Py_ssize_t len;
10044
10045 assert(buf_size <= XATTR_SIZE_MAX);
10046 value = PyBytes_FromStringAndSize(NULL, buf_size);
10047 if (!value)
10048 return 0;
10049 Py_BEGIN_ALLOW_THREADS;
10050 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10051 Py_END_ALLOW_THREADS;
10052 if (len < 0) {
10053 Py_DECREF(value);
10054 if (errno == ERANGE) {
10055 value = NULL;
10056 }
10057 else {
10058 posix_error();
10059 return 0;
10060 }
10061 }
10062 else if (len != buf_size) {
10063 /* Can only shrink. */
10064 _PyBytes_Resize(&value, len);
10065 }
10066 *res = value;
10067 return 1;
10068}
10069
10070static PyObject *
10071getxattr_common(const char *path, PyObject *name_obj,
10072 ssize_t (*get)(const char *, const char *, void *, size_t))
10073{
10074 PyObject *value;
10075 const char *name = PyBytes_AS_STRING(name_obj);
10076
10077 /* Try a small value first. */
10078 if (!try_getxattr(path, name, get, 128, &value))
10079 return NULL;
10080 if (value)
10081 return value;
10082 /* Now the maximum possible one. */
10083 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10084 return NULL;
10085 assert(value);
10086 return value;
10087}
10088
10089PyDoc_STRVAR(posix_getxattr__doc__,
10090"getxattr(path, attr) -> value\n\n\
10091Return the value of extended attribute *name* on *path*.");
10092
10093static PyObject *
10094posix_getxattr(PyObject *self, PyObject *args)
10095{
10096 PyObject *path, *res, *name;
10097
10098 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10099 PyUnicode_FSConverter, &name))
10100 return NULL;
10101 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10102 Py_DECREF(path);
10103 Py_DECREF(name);
10104 return res;
10105}
10106
10107PyDoc_STRVAR(posix_lgetxattr__doc__,
10108"lgetxattr(path, attr) -> value\n\n\
10109Like getxattr but don't follow symlinks.");
10110
10111static PyObject *
10112posix_lgetxattr(PyObject *self, PyObject *args)
10113{
10114 PyObject *path, *res, *name;
10115
10116 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10117 PyUnicode_FSConverter, &name))
10118 return NULL;
10119 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10120 Py_DECREF(path);
10121 Py_DECREF(name);
10122 return res;
10123}
10124
10125static ssize_t
10126wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10127{
10128 /* Hack to share code. */
10129 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10130}
10131
10132PyDoc_STRVAR(posix_fgetxattr__doc__,
10133"fgetxattr(fd, attr) -> value\n\n\
10134Like getxattr but operate on a fd instead of a path.");
10135
10136static PyObject *
10137posix_fgetxattr(PyObject *self, PyObject *args)
10138{
10139 PyObject *res, *name;
10140 int fd;
10141
10142 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10143 return NULL;
10144 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10145 Py_DECREF(name);
10146 return res;
10147}
10148
10149PyDoc_STRVAR(posix_setxattr__doc__,
10150"setxattr(path, attr, value, flags=0)\n\n\
10151Set extended attribute *attr* on *path* to *value*.");
10152
10153static PyObject *
10154posix_setxattr(PyObject *self, PyObject *args)
10155{
10156 PyObject *path, *name;
10157 Py_buffer data;
10158 int flags = 0, err;
10159
10160 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10161 &path, PyUnicode_FSConverter, &name, &data, &flags))
10162 return NULL;
10163 Py_BEGIN_ALLOW_THREADS;
10164 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10165 data.buf, data.len, flags);
10166 Py_END_ALLOW_THREADS;
10167 Py_DECREF(path);
10168 Py_DECREF(name);
10169 PyBuffer_Release(&data);
10170 if (err)
10171 return posix_error();
10172 Py_RETURN_NONE;
10173}
10174
10175PyDoc_STRVAR(posix_lsetxattr__doc__,
10176"lsetxattr(path, attr, value, flags=0)\n\n\
10177Like setxattr but don't follow symlinks.");
10178
10179static PyObject *
10180posix_lsetxattr(PyObject *self, PyObject *args)
10181{
10182 PyObject *path, *name;
10183 Py_buffer data;
10184 int flags = 0, err;
10185
10186 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10187 &path, PyUnicode_FSConverter, &name, &data, &flags))
10188 return NULL;
10189 Py_BEGIN_ALLOW_THREADS;
10190 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10191 data.buf, data.len, flags);
10192 Py_END_ALLOW_THREADS;
10193 Py_DECREF(path);
10194 Py_DECREF(name);
10195 PyBuffer_Release(&data);
10196 if (err)
10197 return posix_error();
10198 Py_RETURN_NONE;
10199}
10200
10201PyDoc_STRVAR(posix_fsetxattr__doc__,
10202"fsetxattr(fd, attr, value, flags=0)\n\n\
10203Like setxattr but operates on *fd* instead of a path.");
10204
10205static PyObject *
10206posix_fsetxattr(PyObject *self, PyObject *args)
10207{
10208 Py_buffer data;
10209 const char *name;
10210 int fd, flags = 0, err;
10211
10212 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10213 &name, &data, &flags))
10214 return NULL;
10215 Py_BEGIN_ALLOW_THREADS;
10216 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10217 Py_END_ALLOW_THREADS;
10218 Py_DECREF(name);
10219 PyBuffer_Release(&data);
10220 if (err)
10221 return posix_error();
10222 Py_RETURN_NONE;
10223}
10224
10225PyDoc_STRVAR(posix_removexattr__doc__,
10226"removexattr(path, attr)\n\n\
10227Remove extended attribute *attr* on *path*.");
10228
10229static PyObject *
10230posix_removexattr(PyObject *self, PyObject *args)
10231{
10232 PyObject *path, *name;
10233 int err;
10234
10235 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10236 PyUnicode_FSConverter, &name))
10237 return NULL;
10238 Py_BEGIN_ALLOW_THREADS;
10239 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10240 Py_END_ALLOW_THREADS;
10241 Py_DECREF(path);
10242 Py_DECREF(name);
10243 if (err)
10244 return posix_error();
10245 Py_RETURN_NONE;
10246}
10247
10248PyDoc_STRVAR(posix_lremovexattr__doc__,
10249"lremovexattr(path, attr)\n\n\
10250Like removexattr but don't follow symlinks.");
10251
10252static PyObject *
10253posix_lremovexattr(PyObject *self, PyObject *args)
10254{
10255 PyObject *path, *name;
10256 int err;
10257
10258 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10259 PyUnicode_FSConverter, &name))
10260 return NULL;
10261 Py_BEGIN_ALLOW_THREADS;
10262 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10263 Py_END_ALLOW_THREADS;
10264 Py_DECREF(path);
10265 Py_DECREF(name);
10266 if (err)
10267 return posix_error();
10268 Py_RETURN_NONE;
10269}
10270
10271PyDoc_STRVAR(posix_fremovexattr__doc__,
10272"fremovexattr(fd, attr)\n\n\
10273Like removexattr but operates on a file descriptor.");
10274
10275static PyObject *
10276posix_fremovexattr(PyObject *self, PyObject *args)
10277{
10278 PyObject *name;
10279 int fd, err;
10280
10281 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10282 PyUnicode_FSConverter, &name))
10283 return NULL;
10284 Py_BEGIN_ALLOW_THREADS;
10285 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10286 Py_END_ALLOW_THREADS;
10287 Py_DECREF(name);
10288 if (err)
10289 return posix_error();
10290 Py_RETURN_NONE;
10291}
10292
10293static Py_ssize_t
10294try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10295 Py_ssize_t buf_size, char **buf)
10296{
10297 Py_ssize_t len;
10298
10299 *buf = PyMem_MALLOC(buf_size);
10300 if (!*buf) {
10301 PyErr_NoMemory();
10302 return -1;
10303 }
10304 Py_BEGIN_ALLOW_THREADS;
10305 len = list(path, *buf, buf_size);
10306 Py_END_ALLOW_THREADS;
10307 if (len < 0) {
10308 PyMem_FREE(*buf);
10309 if (errno != ERANGE)
10310 posix_error();
10311 return -1;
10312 }
10313 return len;
10314}
10315
10316static PyObject *
10317listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10318{
10319 PyObject *res, *attr;
10320 Py_ssize_t len, err, start, i;
10321 char *buf;
10322
10323 len = try_listxattr(path, list, 256, &buf);
10324 if (len < 0) {
10325 if (PyErr_Occurred())
10326 return NULL;
10327 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10328 if (len < 0)
10329 return NULL;
10330 }
10331 res = PyList_New(0);
10332 if (!res) {
10333 PyMem_FREE(buf);
10334 return NULL;
10335 }
10336 for (start = i = 0; i < len; i++) {
10337 if (!buf[i]) {
10338 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10339 if (!attr) {
10340 Py_DECREF(res);
10341 PyMem_FREE(buf);
10342 return NULL;
10343 }
10344 err = PyList_Append(res, attr);
10345 Py_DECREF(attr);
10346 if (err) {
10347 Py_DECREF(res);
10348 PyMem_FREE(buf);
10349 return NULL;
10350 }
10351 start = i + 1;
10352 }
10353 }
10354 PyMem_FREE(buf);
10355 return res;
10356}
10357
10358PyDoc_STRVAR(posix_listxattr__doc__,
10359"listxattr(path)\n\n\
10360Return a list of extended attributes on *path*.");
10361
10362static PyObject *
10363posix_listxattr(PyObject *self, PyObject *args)
10364{
10365 PyObject *path, *res;
10366
10367 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10368 return NULL;
10369 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10370 Py_DECREF(path);
10371 return res;
10372}
10373
10374PyDoc_STRVAR(posix_llistxattr__doc__,
10375"llistxattr(path)\n\n\
10376Like listxattr but don't follow symlinks..");
10377
10378static PyObject *
10379posix_llistxattr(PyObject *self, PyObject *args)
10380{
10381 PyObject *path, *res;
10382
10383 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10384 return NULL;
10385 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10386 Py_DECREF(path);
10387 return res;
10388}
10389
10390static ssize_t
10391wrap_flistxattr(const char *path, char *buf, size_t len)
10392{
10393 /* Hack to share code. */
10394 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10395}
10396
10397PyDoc_STRVAR(posix_flistxattr__doc__,
10398"flistxattr(path)\n\n\
10399Like flistxattr but operates on a file descriptor.");
10400
10401static PyObject *
10402posix_flistxattr(PyObject *self, PyObject *args)
10403{
10404 long fd;
10405
10406 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10407 return NULL;
10408 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10409}
10410
Benjamin Petersonb77fe172011-09-13 17:20:47 -040010411#endif /* HAVE_SYS_XATTR_H */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010412
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010413static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010414 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010415#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010416 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010417#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010418 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010419#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010420 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010421#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010422 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010423#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010424 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010425#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010426#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010427 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010428#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010429#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010430 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010431#endif /* HAVE_LCHMOD */
10432#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010433 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010434#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010435#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010436 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010437#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010438#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010439 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010440#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010441#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010442 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010443#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010444#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010445 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010446#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +000010447#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010448 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10449 METH_NOARGS, posix_getcwd__doc__},
10450 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10451 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +000010452#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010453#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010454 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010455#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010456 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010457#ifdef HAVE_FDOPENDIR
10458 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10459#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010460 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10461 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010462#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010463 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010464#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010465#ifdef HAVE_GETPRIORITY
10466 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10467#endif /* HAVE_GETPRIORITY */
10468#ifdef HAVE_SETPRIORITY
10469 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10470#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010471#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010472 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010473#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010474#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010475 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010476#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010477 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10478 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10479 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010480 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010481#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010482 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010483#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010484#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010485 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010486 win_symlink__doc__},
10487#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010488#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010489 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010490#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010492#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010493 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010494#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010495 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10496 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10497 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010498#ifdef HAVE_FUTIMES
10499 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10500#endif
10501#ifdef HAVE_LUTIMES
10502 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10503#endif
10504#ifdef HAVE_FUTIMENS
10505 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10506#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010507#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010508 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010509#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010511#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10513 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010514#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010515#ifdef HAVE_FEXECVE
10516 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10517#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010518#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10520 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010521#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10523 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010524#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010525#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010526#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010528#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010529#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010531#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010532#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010533#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010534 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10535 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010536#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010537#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010538 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010539#endif
10540#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010541 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010542#endif
10543#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010544 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010545#endif
10546#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010547 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010548#endif
10549#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010550 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010551#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010552 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010553#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010554 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10555 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10556#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010557#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010558#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010559 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010560#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010561#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010562 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010563#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010564#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010565 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010566#endif /* HAVE_GETEGID */
10567#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010568 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010569#endif /* HAVE_GETEUID */
10570#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010571 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010572#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010573#ifdef HAVE_GETGROUPLIST
10574 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10575#endif
Fred Drakec9680921999-12-13 16:37:25 +000010576#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010577 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010578#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010579 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010580#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010582#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010583#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010585#endif /* HAVE_GETPPID */
10586#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010588#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010589#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010591#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010592#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010594#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010595#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010597#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010598#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010600#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010601#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010602 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10603 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010604 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010605#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010606#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010607 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010608#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010609#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010610 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010611#endif /* HAVE_SETEUID */
10612#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010613 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010614#endif /* HAVE_SETEGID */
10615#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010616 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010617#endif /* HAVE_SETREUID */
10618#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010619 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010620#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010621#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010622 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010623#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010624#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010625 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010626#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010627#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010628 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010629#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010630#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010631 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010632#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010633#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010634 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010635#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010636#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010637 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010638#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010639#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010640 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010641#endif /* HAVE_WAIT3 */
10642#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010643 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010644#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010645#if defined(HAVE_WAITID) && !defined(__APPLE__)
10646 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10647#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010648#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010649 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010650#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010651#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010653#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010654#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010656#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010657#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010658 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010659#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010660#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010662#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010663#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010665#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10667 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10668 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10669 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10670 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10671 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010672#ifdef HAVE_LOCKF
10673 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10674#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10676 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010677#ifdef HAVE_READV
10678 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10679#endif
10680#ifdef HAVE_PREAD
10681 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10682#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010684#ifdef HAVE_WRITEV
10685 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10686#endif
10687#ifdef HAVE_PWRITE
10688 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10689#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010690#ifdef HAVE_SENDFILE
10691 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10692 posix_sendfile__doc__},
10693#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010694 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10695 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010696#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010698#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010699#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010700 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010701#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010702#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010704#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010705#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010707#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010708#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10710 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10711 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010712#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010713#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010715#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010716#ifdef HAVE_TRUNCATE
10717 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10718#endif
10719#ifdef HAVE_POSIX_FALLOCATE
10720 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10721#endif
10722#ifdef HAVE_POSIX_FADVISE
10723 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10724#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010725#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010726 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010727#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010728#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010729 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010730#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010732#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010733 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010734#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010735#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010736 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010737#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010738#ifdef HAVE_SYNC
10739 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10740#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010741#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010742 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010743#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010744#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010745#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010747#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010748#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010750#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010751#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010753#endif /* WIFSTOPPED */
10754#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010756#endif /* WIFSIGNALED */
10757#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010759#endif /* WIFEXITED */
10760#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010762#endif /* WEXITSTATUS */
10763#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010765#endif /* WTERMSIG */
10766#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010768#endif /* WSTOPSIG */
10769#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010770#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010771 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010772#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010773#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010774 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010775#endif
Fred Drakec9680921999-12-13 16:37:25 +000010776#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010777 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010778#endif
10779#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010780 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010781#endif
10782#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010783 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010784#endif
10785#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010786 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010787#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010789#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010791 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010792 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010793 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010794 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010795#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010796#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010798#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010799 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010801 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010802 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010804 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010805#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010807#endif
10808#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010810#endif
10811#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010813#endif
10814#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010816#endif
10817
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010818/* posix *at family of functions */
10819#ifdef HAVE_FACCESSAT
10820 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10821#endif
10822#ifdef HAVE_FCHMODAT
10823 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10824#endif /* HAVE_FCHMODAT */
10825#ifdef HAVE_FCHOWNAT
10826 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10827#endif /* HAVE_FCHOWNAT */
10828#ifdef HAVE_FSTATAT
10829 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10830#endif
10831#ifdef HAVE_FUTIMESAT
10832 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10833#endif
10834#ifdef HAVE_LINKAT
10835 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10836#endif /* HAVE_LINKAT */
10837#ifdef HAVE_MKDIRAT
10838 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10839#endif
10840#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10841 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10842#endif
10843#ifdef HAVE_OPENAT
10844 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10845#endif
10846#ifdef HAVE_READLINKAT
10847 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10848#endif /* HAVE_READLINKAT */
10849#ifdef HAVE_RENAMEAT
10850 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10851#endif
10852#if HAVE_SYMLINKAT
10853 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10854#endif /* HAVE_SYMLINKAT */
10855#ifdef HAVE_UNLINKAT
10856 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10857#endif
10858#ifdef HAVE_UTIMENSAT
10859 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10860#endif
10861#ifdef HAVE_MKFIFOAT
10862 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10863#endif
Benjamin Petersonb77fe172011-09-13 17:20:47 -040010864#ifdef HAVE_SYS_XATTR_H
Benjamin Peterson799bd802011-08-31 22:15:17 -040010865 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10866 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10867 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10868 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10869 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10870 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10871 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10872 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10873 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10874 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10875 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10876 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10877#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010878 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010879};
10880
10881
Barry Warsaw4a342091996-12-19 23:50:02 +000010882static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010883ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010884{
Victor Stinner8c62be82010-05-06 00:08:46 +000010885 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010886}
10887
Guido van Rossumd48f2521997-12-05 22:19:34 +000010888#if defined(PYOS_OS2)
10889/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010890static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010891{
10892 APIRET rc;
10893 ULONG values[QSV_MAX+1];
10894 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010895 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010896
10897 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010898 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010899 Py_END_ALLOW_THREADS
10900
10901 if (rc != NO_ERROR) {
10902 os2_error(rc);
10903 return -1;
10904 }
10905
Fred Drake4d1e64b2002-04-15 19:40:07 +000010906 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10907 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10908 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10909 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10910 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10911 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10912 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010913
10914 switch (values[QSV_VERSION_MINOR]) {
10915 case 0: ver = "2.00"; break;
10916 case 10: ver = "2.10"; break;
10917 case 11: ver = "2.11"; break;
10918 case 30: ver = "3.00"; break;
10919 case 40: ver = "4.00"; break;
10920 case 50: ver = "5.00"; break;
10921 default:
Tim Peters885d4572001-11-28 20:27:42 +000010922 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010923 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010924 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010925 ver = &tmp[0];
10926 }
10927
10928 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010929 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010930 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010931
10932 /* Add Indicator of Which Drive was Used to Boot the System */
10933 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10934 tmp[1] = ':';
10935 tmp[2] = '\0';
10936
Fred Drake4d1e64b2002-04-15 19:40:07 +000010937 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010938}
10939#endif
10940
Brian Curtin52173d42010-12-02 18:29:18 +000010941#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010942static int
Brian Curtin52173d42010-12-02 18:29:18 +000010943enable_symlink()
10944{
10945 HANDLE tok;
10946 TOKEN_PRIVILEGES tok_priv;
10947 LUID luid;
10948 int meth_idx = 0;
10949
10950 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010951 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010952
10953 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010954 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010955
10956 tok_priv.PrivilegeCount = 1;
10957 tok_priv.Privileges[0].Luid = luid;
10958 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10959
10960 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10961 sizeof(TOKEN_PRIVILEGES),
10962 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010963 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010964
Brian Curtin3b4499c2010-12-28 14:31:47 +000010965 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10966 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010967}
10968#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10969
Barry Warsaw4a342091996-12-19 23:50:02 +000010970static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010971all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010972{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010973#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010975#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010976#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010977 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010978#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010979#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010980 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010981#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010982#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010983 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010984#endif
Fred Drakec9680921999-12-13 16:37:25 +000010985#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010986 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010987#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010988#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010990#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010991#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010992 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010993#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010994#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010995 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010996#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010997#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010999#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011000#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011002#endif
11003#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011004 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011005#endif
11006#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011007 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011008#endif
11009#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011011#endif
11012#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011013 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011014#endif
11015#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011017#endif
11018#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011020#endif
11021#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011023#endif
11024#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011025 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011026#endif
11027#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011028 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011029#endif
11030#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011032#endif
11033#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011034 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011035#endif
11036#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011037 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011038#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011039#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011041#endif
11042#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011044#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011045#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011047#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011048#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011050#endif
11051#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011052 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011053#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011054#ifdef PRIO_PROCESS
11055 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11056#endif
11057#ifdef PRIO_PGRP
11058 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11059#endif
11060#ifdef PRIO_USER
11061 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11062#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011063#ifdef O_CLOEXEC
11064 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11065#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011066/* posix - constants for *at functions */
11067#ifdef AT_SYMLINK_NOFOLLOW
11068 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11069#endif
11070#ifdef AT_EACCESS
11071 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11072#endif
11073#ifdef AT_FDCWD
11074 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11075#endif
11076#ifdef AT_REMOVEDIR
11077 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11078#endif
11079#ifdef AT_SYMLINK_FOLLOW
11080 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11081#endif
11082#ifdef UTIME_NOW
11083 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11084#endif
11085#ifdef UTIME_OMIT
11086 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11087#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011088
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011089
Tim Peters5aa91602002-01-30 05:46:57 +000011090/* MS Windows */
11091#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011092 /* Don't inherit in child processes. */
11093 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011094#endif
11095#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 /* Optimize for short life (keep in memory). */
11097 /* MS forgot to define this one with a non-underscore form too. */
11098 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011099#endif
11100#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011101 /* Automatically delete when last handle is closed. */
11102 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011103#endif
11104#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 /* Optimize for random access. */
11106 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011107#endif
11108#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 /* Optimize for sequential access. */
11110 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011111#endif
11112
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011113/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011114#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 /* Send a SIGIO signal whenever input or output
11116 becomes available on file descriptor */
11117 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011118#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011119#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 /* Direct disk access. */
11121 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011122#endif
11123#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 /* Must be a directory. */
11125 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011126#endif
11127#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011128 /* Do not follow links. */
11129 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011130#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011131#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 /* Do not update the access time. */
11133 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011134#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011135
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011137#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011139#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011140#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011142#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011143#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011145#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011146#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011148#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011149#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011151#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011152#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011154#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011155#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011157#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011158#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011159 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011160#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011161#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011162 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011163#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011164#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011165 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011166#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011167#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011169#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011170#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011172#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011173#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011175#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011176#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011178#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011179#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011181#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011182#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011184#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011185#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011187#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011188
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011189 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011190#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011191 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011192#endif /* ST_RDONLY */
11193#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011194 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011195#endif /* ST_NOSUID */
11196
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011197 /* FreeBSD sendfile() constants */
11198#ifdef SF_NODISKIO
11199 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11200#endif
11201#ifdef SF_MNOWAIT
11202 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11203#endif
11204#ifdef SF_SYNC
11205 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11206#endif
11207
Ross Lagerwall7807c352011-03-17 20:20:30 +020011208 /* constants for posix_fadvise */
11209#ifdef POSIX_FADV_NORMAL
11210 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11211#endif
11212#ifdef POSIX_FADV_SEQUENTIAL
11213 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11214#endif
11215#ifdef POSIX_FADV_RANDOM
11216 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11217#endif
11218#ifdef POSIX_FADV_NOREUSE
11219 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11220#endif
11221#ifdef POSIX_FADV_WILLNEED
11222 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11223#endif
11224#ifdef POSIX_FADV_DONTNEED
11225 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11226#endif
11227
11228 /* constants for waitid */
11229#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11230 if (ins(d, "P_PID", (long)P_PID)) return -1;
11231 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11232 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11233#endif
11234#ifdef WEXITED
11235 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11236#endif
11237#ifdef WNOWAIT
11238 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11239#endif
11240#ifdef WSTOPPED
11241 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11242#endif
11243#ifdef CLD_EXITED
11244 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11245#endif
11246#ifdef CLD_DUMPED
11247 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11248#endif
11249#ifdef CLD_TRAPPED
11250 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11251#endif
11252#ifdef CLD_CONTINUED
11253 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11254#endif
11255
11256 /* constants for lockf */
11257#ifdef F_LOCK
11258 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11259#endif
11260#ifdef F_TLOCK
11261 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11262#endif
11263#ifdef F_ULOCK
11264 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11265#endif
11266#ifdef F_TEST
11267 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11268#endif
11269
11270 /* constants for futimens */
11271#ifdef UTIME_NOW
11272 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11273#endif
11274#ifdef UTIME_OMIT
11275 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11276#endif
11277
Guido van Rossum246bc171999-02-01 23:54:31 +000011278#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011279#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011280 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11281 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11282 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11283 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11284 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11285 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11286 if (ins(d, "P_PM", (long)P_PM)) return -1;
11287 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11288 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11289 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11290 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11291 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11292 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11293 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11294 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11295 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11296 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11297 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11298 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11299 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011300#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011301 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11302 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11303 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11304 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11305 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011306#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011307#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011308
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011309#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011310 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011311 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11312 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11313#ifdef SCHED_SPORADIC
11314 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11315#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011316#ifdef SCHED_BATCH
11317 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11318#endif
11319#ifdef SCHED_IDLE
11320 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11321#endif
11322#ifdef SCHED_RESET_ON_FORK
11323 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11324#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011325#ifdef SCHED_SYS
11326 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11327#endif
11328#ifdef SCHED_IA
11329 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11330#endif
11331#ifdef SCHED_FSS
11332 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11333#endif
11334#ifdef SCHED_FX
11335 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11336#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011337#endif
11338
Benjamin Petersonb77fe172011-09-13 17:20:47 -040011339#ifdef HAVE_SYS_XATTR_H
Benjamin Peterson799bd802011-08-31 22:15:17 -040011340 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11341 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11342 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11343#endif
11344
Guido van Rossumd48f2521997-12-05 22:19:34 +000011345#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011346 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011347#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011349}
11350
11351
Tim Peters5aa91602002-01-30 05:46:57 +000011352#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011353#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011354#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011355
11356#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011357#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011358#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011359
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011360#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011361#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011362#define MODNAME "posix"
11363#endif
11364
Martin v. Löwis1a214512008-06-11 05:26:20 +000011365static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 PyModuleDef_HEAD_INIT,
11367 MODNAME,
11368 posix__doc__,
11369 -1,
11370 posix_methods,
11371 NULL,
11372 NULL,
11373 NULL,
11374 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011375};
11376
11377
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011378PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011379INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011380{
Victor Stinner8c62be82010-05-06 00:08:46 +000011381 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011382
Brian Curtin52173d42010-12-02 18:29:18 +000011383#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011384 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011385#endif
11386
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 m = PyModule_Create(&posixmodule);
11388 if (m == NULL)
11389 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011390
Victor Stinner8c62be82010-05-06 00:08:46 +000011391 /* Initialize environ dictionary */
11392 v = convertenviron();
11393 Py_XINCREF(v);
11394 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11395 return NULL;
11396 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011397
Victor Stinner8c62be82010-05-06 00:08:46 +000011398 if (all_ins(m))
11399 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011400
Victor Stinner8c62be82010-05-06 00:08:46 +000011401 if (setup_confname_tables(m))
11402 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011403
Victor Stinner8c62be82010-05-06 00:08:46 +000011404 Py_INCREF(PyExc_OSError);
11405 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011406
Benjamin Peterson2740af82011-08-02 17:41:34 -050011407#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011408 if (PyType_Ready(&cpu_set_type) < 0)
11409 return NULL;
11410 Py_INCREF(&cpu_set_type);
11411 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011412#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011413
Guido van Rossumb3d39562000-01-31 18:41:26 +000011414#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011415 if (posix_putenv_garbage == NULL)
11416 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011417#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011418
Victor Stinner8c62be82010-05-06 00:08:46 +000011419 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011420#if defined(HAVE_WAITID) && !defined(__APPLE__)
11421 waitid_result_desc.name = MODNAME ".waitid_result";
11422 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11423#endif
11424
Victor Stinner8c62be82010-05-06 00:08:46 +000011425 stat_result_desc.name = MODNAME ".stat_result";
11426 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11427 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11428 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11429 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11430 structseq_new = StatResultType.tp_new;
11431 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011432
Victor Stinner8c62be82010-05-06 00:08:46 +000011433 statvfs_result_desc.name = MODNAME ".statvfs_result";
11434 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011435#ifdef NEED_TICKS_PER_SECOND
11436# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011437 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011438# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011439 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011440# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011441 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011442# endif
11443#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011444
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011445#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011446 sched_param_desc.name = MODNAME ".sched_param";
11447 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11448 SchedParamType.tp_new = sched_param_new;
11449#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011450 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011451#if defined(HAVE_WAITID) && !defined(__APPLE__)
11452 Py_INCREF((PyObject*) &WaitidResultType);
11453 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11454#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011455 Py_INCREF((PyObject*) &StatResultType);
11456 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11457 Py_INCREF((PyObject*) &StatVFSResultType);
11458 PyModule_AddObject(m, "statvfs_result",
11459 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011460
11461#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011462 Py_INCREF(&SchedParamType);
11463 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011464#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011465 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011466
11467#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011468 /*
11469 * Step 2 of weak-linking support on Mac OS X.
11470 *
11471 * The code below removes functions that are not available on the
11472 * currently active platform.
11473 *
11474 * This block allow one to use a python binary that was build on
11475 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11476 * OSX 10.4.
11477 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011478#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011479 if (fstatvfs == NULL) {
11480 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11481 return NULL;
11482 }
11483 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011484#endif /* HAVE_FSTATVFS */
11485
11486#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011487 if (statvfs == NULL) {
11488 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11489 return NULL;
11490 }
11491 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011492#endif /* HAVE_STATVFS */
11493
11494# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011495 if (lchown == NULL) {
11496 if (PyObject_DelAttrString(m, "lchown") == -1) {
11497 return NULL;
11498 }
11499 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011500#endif /* HAVE_LCHOWN */
11501
11502
11503#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011505
Guido van Rossumb6775db1994-08-01 11:34:53 +000011506}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011507
11508#ifdef __cplusplus
11509}
11510#endif