blob: 602c4222dd1e1670e84d2da6c228bb1a2e39dbfc [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 Peterson9428d532011-09-14 11:45:52 -0400110#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
111#define USE_XATTRS
112#endif
113
114#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400115#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400116#endif
117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
119#ifdef HAVE_SYS_SOCKET_H
120#include <sys/socket.h>
121#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#endif
123
Victor Stinner8b905bd2011-10-25 13:34:04 +0200124#ifdef HAVE_DLFCN_H
125#include <dlfcn.h>
126#endif
127
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000129/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000130#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000131#include <process.h>
132#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000133#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#define HAVE_GETCWD 1
135#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000136#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#if defined(__OS2__)
138#define HAVE_EXECV 1
139#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#include <process.h>
142#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000143#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144#define HAVE_EXECV 1
145#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_OPENDIR 1
147#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#define HAVE_WAIT 1
150#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000152#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000153#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000154#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_EXECV 1
157#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000158#define HAVE_SYSTEM 1
159#define HAVE_CWAIT 1
160#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000161#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000162#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000163#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
164/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166/* Unix functions that the configure script doesn't check for */
167#define HAVE_EXECV 1
168#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000169#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000170#define HAVE_FORK1 1
171#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_GETCWD 1
173#define HAVE_GETEGID 1
174#define HAVE_GETEUID 1
175#define HAVE_GETGID 1
176#define HAVE_GETPPID 1
177#define HAVE_GETUID 1
178#define HAVE_KILL 1
179#define HAVE_OPENDIR 1
180#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000183#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000184#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000185#endif /* _MSC_VER */
186#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000187#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000189
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000191
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000192#if defined(__sgi)&&_COMPILER_VERSION>=700
193/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
194 (default) */
195extern char *ctermid_r(char *);
196#endif
197
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000198#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000201#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000202#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000205extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000206#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000207#endif
208#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int chdir(char *);
210extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int chdir(const char *);
213extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000215#ifdef __BORLANDC__
216extern int chmod(const char *, int);
217#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000219#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000220/*#ifdef HAVE_FCHMOD
221extern int fchmod(int, mode_t);
222#endif*/
223/*#ifdef HAVE_LCHMOD
224extern int lchmod(const char *, mode_t);
225#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000226extern int chown(const char *, uid_t, gid_t);
227extern char *getcwd(char *, int);
228extern char *strerror(int);
229extern int link(const char *, const char *);
230extern int rename(const char *, const char *);
231extern int stat(const char *, struct stat *);
232extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000234extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000235#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000237extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000240
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000241#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#ifdef HAVE_UTIME_H
244#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000245#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000247#ifdef HAVE_SYS_UTIME_H
248#include <sys/utime.h>
249#define HAVE_UTIME_H /* pretend we do for the rest of this file */
250#endif /* HAVE_SYS_UTIME_H */
251
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#ifdef HAVE_SYS_TIMES_H
253#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
256#ifdef HAVE_SYS_PARAM_H
257#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259
260#ifdef HAVE_SYS_UTSNAME_H
261#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000262#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000264#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000266#define NAMLEN(dirent) strlen((dirent)->d_name)
267#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000268#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#include <direct.h>
270#define NAMLEN(dirent) strlen((dirent)->d_name)
271#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000274#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000277#endif
278#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#endif
281#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#endif
284#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000286#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289#endif
290#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292#endif
293#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000296#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000297#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000298#endif
299#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000300#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000301#endif
302#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000303#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000304#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000305#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000306#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000308#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000309#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000310#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
311#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000312static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000313#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000314#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000315
Guido van Rossumd48f2521997-12-05 22:19:34 +0000316#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000317#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000318#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321#if defined(PATH_MAX) && PATH_MAX > 1024
322#define MAXPATHLEN PATH_MAX
323#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000326#endif /* MAXPATHLEN */
327
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000328#ifdef UNION_WAIT
329/* Emulate some macros on systems that have a union instead of macros */
330
331#ifndef WIFEXITED
332#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
333#endif
334
335#ifndef WEXITSTATUS
336#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
337#endif
338
339#ifndef WTERMSIG
340#define WTERMSIG(u_wait) ((u_wait).w_termsig)
341#endif
342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343#define WAIT_TYPE union wait
344#define WAIT_STATUS_INT(s) (s.w_status)
345
346#else /* !UNION_WAIT */
347#define WAIT_TYPE int
348#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000349#endif /* UNION_WAIT */
350
Greg Wardb48bc172000-03-01 21:51:56 +0000351/* Don't use the "_r" form if we don't need it (also, won't have a
352 prototype for it, at least on Solaris -- maybe others as well?). */
353#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
354#define USE_CTERMID_R
355#endif
356
Fred Drake699f3522000-06-29 21:12:41 +0000357/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000358#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000359#undef FSTAT
360#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000361#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000362# define STAT win32_stat
363# define FSTAT win32_fstat
364# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000365#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT stat
367# define FSTAT fstat
368# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000369#endif
370
Tim Peters11b23062003-04-23 02:39:17 +0000371#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000372#include <sys/mkdev.h>
373#else
374#if defined(MAJOR_IN_SYSMACROS)
375#include <sys/sysmacros.h>
376#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000377#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
378#include <sys/mkdev.h>
379#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000380#endif
Fred Drake699f3522000-06-29 21:12:41 +0000381
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200382/* A helper used by a number of POSIX-only functions */
383#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000384static int
385_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000386{
387#if !defined(HAVE_LARGEFILE_SUPPORT)
388 *((off_t*)addr) = PyLong_AsLong(arg);
389#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000390 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000391#endif
392 if (PyErr_Occurred())
393 return 0;
394 return 1;
395}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200396#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000397
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000398#if defined _MSC_VER && _MSC_VER >= 1400
399/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
400 * valid and throw an assertion if it isn't.
401 * Normally, an invalid fd is likely to be a C program error and therefore
402 * an assertion can be useful, but it does contradict the POSIX standard
403 * which for write(2) states:
404 * "Otherwise, -1 shall be returned and errno set to indicate the error."
405 * "[EBADF] The fildes argument is not a valid file descriptor open for
406 * writing."
407 * Furthermore, python allows the user to enter any old integer
408 * as a fd and should merely raise a python exception on error.
409 * The Microsoft CRT doesn't provide an official way to check for the
410 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000411 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000412 * internal structures involved.
413 * The structures below must be updated for each version of visual studio
414 * according to the file internal.h in the CRT source, until MS comes
415 * up with a less hacky way to do this.
416 * (all of this is to avoid globally modifying the CRT behaviour using
417 * _set_invalid_parameter_handler() and _CrtSetReportMode())
418 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419/* The actual size of the structure is determined at runtime.
420 * Only the first items must be present.
421 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000423 intptr_t osfhnd;
424 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000425} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000426
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000427extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000428#define IOINFO_L2E 5
429#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
430#define IOINFO_ARRAYS 64
431#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
432#define FOPEN 0x01
433#define _NO_CONSOLE_FILENO (intptr_t)-2
434
435/* This function emulates what the windows CRT does to validate file handles */
436int
437_PyVerify_fd(int fd)
438{
Victor Stinner8c62be82010-05-06 00:08:46 +0000439 const int i1 = fd >> IOINFO_L2E;
440 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000441
Antoine Pitrou22e41552010-08-15 18:07:50 +0000442 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000443
Victor Stinner8c62be82010-05-06 00:08:46 +0000444 /* Determine the actual size of the ioinfo structure,
445 * as used by the CRT loaded in memory
446 */
447 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
448 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
449 }
450 if (sizeof_ioinfo == 0) {
451 /* This should not happen... */
452 goto fail;
453 }
454
455 /* See that it isn't a special CLEAR fileno */
456 if (fd != _NO_CONSOLE_FILENO) {
457 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
458 * we check pointer validity and other info
459 */
460 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
461 /* finally, check that the file is open */
462 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
463 if (info->osfile & FOPEN) {
464 return 1;
465 }
466 }
467 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000468 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000469 errno = EBADF;
470 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000471}
472
473/* the special case of checking dup2. The target fd must be in a sensible range */
474static int
475_PyVerify_fd_dup2(int fd1, int fd2)
476{
Victor Stinner8c62be82010-05-06 00:08:46 +0000477 if (!_PyVerify_fd(fd1))
478 return 0;
479 if (fd2 == _NO_CONSOLE_FILENO)
480 return 0;
481 if ((unsigned)fd2 < _NHANDLE_)
482 return 1;
483 else
484 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000485}
486#else
487/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
488#define _PyVerify_fd_dup2(A, B) (1)
489#endif
490
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000491#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000492/* The following structure was copied from
493 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
494 include doesn't seem to be present in the Windows SDK (at least as included
495 with Visual Studio Express). */
496typedef struct _REPARSE_DATA_BUFFER {
497 ULONG ReparseTag;
498 USHORT ReparseDataLength;
499 USHORT Reserved;
500 union {
501 struct {
502 USHORT SubstituteNameOffset;
503 USHORT SubstituteNameLength;
504 USHORT PrintNameOffset;
505 USHORT PrintNameLength;
506 ULONG Flags;
507 WCHAR PathBuffer[1];
508 } SymbolicLinkReparseBuffer;
509
510 struct {
511 USHORT SubstituteNameOffset;
512 USHORT SubstituteNameLength;
513 USHORT PrintNameOffset;
514 USHORT PrintNameLength;
515 WCHAR PathBuffer[1];
516 } MountPointReparseBuffer;
517
518 struct {
519 UCHAR DataBuffer[1];
520 } GenericReparseBuffer;
521 };
522} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
523
524#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
525 GenericReparseBuffer)
526#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
527
528static int
Brian Curtind25aef52011-06-13 15:16:04 -0500529win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000530{
531 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
532 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
533 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000534
535 if (0 == DeviceIoControl(
536 reparse_point_handle,
537 FSCTL_GET_REPARSE_POINT,
538 NULL, 0, /* in buffer */
539 target_buffer, sizeof(target_buffer),
540 &n_bytes_returned,
541 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500542 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000543
544 if (reparse_tag)
545 *reparse_tag = rdb->ReparseTag;
546
Brian Curtind25aef52011-06-13 15:16:04 -0500547 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000548}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100549
550static int
551win32_warn_bytes_api()
552{
553 return PyErr_WarnEx(PyExc_DeprecationWarning,
554 "The Windows bytes API has been deprecated, "
555 "use Unicode filenames instead",
556 1);
557}
558
559static PyObject*
560win32_decode_filename(PyObject *obj)
561{
562 PyObject *unicode;
563 if (PyUnicode_Check(obj)) {
564 if (PyUnicode_READY(obj))
565 return NULL;
566 Py_INCREF(obj);
567 return obj;
568 }
569 if (!PyUnicode_FSDecoder(obj, &unicode))
570 return NULL;
571 if (win32_warn_bytes_api()) {
572 Py_DECREF(unicode);
573 return NULL;
574 }
575 return unicode;
576}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000577#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000578
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000580#ifdef WITH_NEXT_FRAMEWORK
581/* On Darwin/MacOSX a shared library or framework has no access to
582** environ directly, we must obtain it with _NSGetEnviron().
583*/
584#include <crt_externs.h>
585static char **environ;
586#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000588#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589
Barry Warsaw53699e91996-12-10 23:23:01 +0000590static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000591convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592{
Victor Stinner8c62be82010-05-06 00:08:46 +0000593 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000594#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000595 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000596#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000597 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000598#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000599#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000600 APIRET rc;
601 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
602#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000603
Victor Stinner8c62be82010-05-06 00:08:46 +0000604 d = PyDict_New();
605 if (d == NULL)
606 return NULL;
607#ifdef WITH_NEXT_FRAMEWORK
608 if (environ == NULL)
609 environ = *_NSGetEnviron();
610#endif
611#ifdef MS_WINDOWS
612 /* _wenviron must be initialized in this way if the program is started
613 through main() instead of wmain(). */
614 _wgetenv(L"");
615 if (_wenviron == NULL)
616 return d;
617 /* This part ignores errors */
618 for (e = _wenviron; *e != NULL; e++) {
619 PyObject *k;
620 PyObject *v;
621 wchar_t *p = wcschr(*e, L'=');
622 if (p == NULL)
623 continue;
624 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
625 if (k == NULL) {
626 PyErr_Clear();
627 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000628 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000629 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
630 if (v == NULL) {
631 PyErr_Clear();
632 Py_DECREF(k);
633 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000634 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000635 if (PyDict_GetItem(d, k) == NULL) {
636 if (PyDict_SetItem(d, k, v) != 0)
637 PyErr_Clear();
638 }
639 Py_DECREF(k);
640 Py_DECREF(v);
641 }
642#else
643 if (environ == NULL)
644 return d;
645 /* This part ignores errors */
646 for (e = environ; *e != NULL; e++) {
647 PyObject *k;
648 PyObject *v;
649 char *p = strchr(*e, '=');
650 if (p == NULL)
651 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000652 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 if (k == NULL) {
654 PyErr_Clear();
655 continue;
656 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000657 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000658 if (v == NULL) {
659 PyErr_Clear();
660 Py_DECREF(k);
661 continue;
662 }
663 if (PyDict_GetItem(d, k) == NULL) {
664 if (PyDict_SetItem(d, k, v) != 0)
665 PyErr_Clear();
666 }
667 Py_DECREF(k);
668 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000669 }
670#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000671#if defined(PYOS_OS2)
672 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
673 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
674 PyObject *v = PyBytes_FromString(buffer);
675 PyDict_SetItemString(d, "BEGINLIBPATH", v);
676 Py_DECREF(v);
677 }
678 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
679 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
680 PyObject *v = PyBytes_FromString(buffer);
681 PyDict_SetItemString(d, "ENDLIBPATH", v);
682 Py_DECREF(v);
683 }
684#endif
685 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686}
687
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688/* Set a POSIX-specific error from errno, and return NULL */
689
Barry Warsawd58d7641998-07-23 16:14:40 +0000690static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000691posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000692{
Victor Stinner8c62be82010-05-06 00:08:46 +0000693 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694}
Barry Warsawd58d7641998-07-23 16:14:40 +0000695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000696posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000697{
Victor Stinner8c62be82010-05-06 00:08:46 +0000698 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000699}
700
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000701
Mark Hammondef8b6542001-05-13 08:04:26 +0000702static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000703posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000704{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000705 PyObject *name_str, *rc;
706 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
707 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000708 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000709 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
710 name_str);
711 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000712 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000713}
714
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000715#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000716static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000717win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 /* XXX We should pass the function name along in the future.
720 (winreg.c also wants to pass the function name.)
721 This would however require an additional param to the
722 Windows error object, which is non-trivial.
723 */
724 errno = GetLastError();
725 if (filename)
726 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
727 else
728 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000729}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000730
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000731static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +0200732win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000733{
Victor Stinner8c62be82010-05-06 00:08:46 +0000734 /* XXX - see win32_error for comments on 'function' */
735 errno = GetLastError();
736 if (filename)
737 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
738 else
739 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000740}
741
Victor Stinnereb5657a2011-09-30 01:44:27 +0200742static PyObject *
743win32_error_object(char* function, PyObject* filename)
744{
745 /* XXX - see win32_error for comments on 'function' */
746 errno = GetLastError();
747 if (filename)
748 return PyErr_SetExcFromWindowsErrWithFilenameObject(
749 PyExc_WindowsError,
750 errno,
751 filename);
752 else
753 return PyErr_SetFromWindowsErr(errno);
754}
755
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000756#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757
Guido van Rossumd48f2521997-12-05 22:19:34 +0000758#if defined(PYOS_OS2)
759/**********************************************************************
760 * Helper Function to Trim and Format OS/2 Messages
761 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000762static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000763os2_formatmsg(char *msgbuf, int msglen, char *reason)
764{
765 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
766
767 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
768 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
769
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000770 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000771 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
772 }
773
774 /* Add Optional Reason Text */
775 if (reason) {
776 strcat(msgbuf, " : ");
777 strcat(msgbuf, reason);
778 }
779}
780
781/**********************************************************************
782 * Decode an OS/2 Operating System Error Code
783 *
784 * A convenience function to lookup an OS/2 error code and return a
785 * text message we can use to raise a Python exception.
786 *
787 * Notes:
788 * The messages for errors returned from the OS/2 kernel reside in
789 * the file OSO001.MSG in the \OS2 directory hierarchy.
790 *
791 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000792static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000793os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
794{
795 APIRET rc;
796 ULONG msglen;
797
798 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
799 Py_BEGIN_ALLOW_THREADS
800 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
801 errorcode, "oso001.msg", &msglen);
802 Py_END_ALLOW_THREADS
803
804 if (rc == NO_ERROR)
805 os2_formatmsg(msgbuf, msglen, reason);
806 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000807 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000808 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000809
810 return msgbuf;
811}
812
813/* Set an OS/2-specific error and return NULL. OS/2 kernel
814 errors are not in a global variable e.g. 'errno' nor are
815 they congruent with posix error numbers. */
816
Victor Stinner8c62be82010-05-06 00:08:46 +0000817static PyObject *
818os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000819{
820 char text[1024];
821 PyObject *v;
822
823 os2_strerror(text, sizeof(text), code, "");
824
825 v = Py_BuildValue("(is)", code, text);
826 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000827 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000828 Py_DECREF(v);
829 }
830 return NULL; /* Signal to Python that an Exception is Pending */
831}
832
833#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834
835/* POSIX generic methods */
836
Barry Warsaw53699e91996-12-10 23:23:01 +0000837static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000838posix_fildes(PyObject *fdobj, int (*func)(int))
839{
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 int fd;
841 int res;
842 fd = PyObject_AsFileDescriptor(fdobj);
843 if (fd < 0)
844 return NULL;
845 if (!_PyVerify_fd(fd))
846 return posix_error();
847 Py_BEGIN_ALLOW_THREADS
848 res = (*func)(fd);
849 Py_END_ALLOW_THREADS
850 if (res < 0)
851 return posix_error();
852 Py_INCREF(Py_None);
853 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000854}
Guido van Rossum21142a01999-01-08 21:05:37 +0000855
856static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000857posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858{
Victor Stinner8c62be82010-05-06 00:08:46 +0000859 PyObject *opath1 = NULL;
860 char *path1;
861 int res;
862 if (!PyArg_ParseTuple(args, format,
863 PyUnicode_FSConverter, &opath1))
864 return NULL;
865 path1 = PyBytes_AsString(opath1);
866 Py_BEGIN_ALLOW_THREADS
867 res = (*func)(path1);
868 Py_END_ALLOW_THREADS
869 if (res < 0)
870 return posix_error_with_allocated_filename(opath1);
871 Py_DECREF(opath1);
872 Py_INCREF(Py_None);
873 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000874}
875
Barry Warsaw53699e91996-12-10 23:23:01 +0000876static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000877posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000878 char *format,
879 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000880{
Victor Stinner8c62be82010-05-06 00:08:46 +0000881 PyObject *opath1 = NULL, *opath2 = NULL;
882 char *path1, *path2;
883 int res;
884 if (!PyArg_ParseTuple(args, format,
885 PyUnicode_FSConverter, &opath1,
886 PyUnicode_FSConverter, &opath2)) {
887 return NULL;
888 }
889 path1 = PyBytes_AsString(opath1);
890 path2 = PyBytes_AsString(opath2);
891 Py_BEGIN_ALLOW_THREADS
892 res = (*func)(path1, path2);
893 Py_END_ALLOW_THREADS
894 Py_DECREF(opath1);
895 Py_DECREF(opath2);
896 if (res != 0)
897 /* XXX how to report both path1 and path2??? */
898 return posix_error();
899 Py_INCREF(Py_None);
900 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000901}
902
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000903#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000905win32_1str(PyObject* args, char* func,
906 char* format, BOOL (__stdcall *funcA)(LPCSTR),
907 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908{
Victor Stinner8c62be82010-05-06 00:08:46 +0000909 PyObject *uni;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100910 const char *ansi;
Victor Stinner8c62be82010-05-06 00:08:46 +0000911 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000912
Victor Stinnereb5657a2011-09-30 01:44:27 +0200913 if (PyArg_ParseTuple(args, wformat, &uni))
914 {
915 wchar_t *wstr = PyUnicode_AsUnicode(uni);
916 if (wstr == NULL)
917 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000918 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +0200919 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +0000920 Py_END_ALLOW_THREADS
921 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +0200922 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +0000923 Py_INCREF(Py_None);
924 return Py_None;
925 }
Victor Stinnereb5657a2011-09-30 01:44:27 +0200926 PyErr_Clear();
927
Victor Stinner8c62be82010-05-06 00:08:46 +0000928 if (!PyArg_ParseTuple(args, format, &ansi))
929 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100930 if (win32_warn_bytes_api())
931 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 Py_BEGIN_ALLOW_THREADS
933 result = funcA(ansi);
934 Py_END_ALLOW_THREADS
935 if (!result)
936 return win32_error(func, ansi);
937 Py_INCREF(Py_None);
938 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939
940}
941
942/* This is a reimplementation of the C library's chdir function,
943 but one that produces Win32 errors instead of DOS error codes.
944 chdir is essentially a wrapper around SetCurrentDirectory; however,
945 it also needs to set "magic" environment variables indicating
946 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000947static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948win32_chdir(LPCSTR path)
949{
Victor Stinner8c62be82010-05-06 00:08:46 +0000950 char new_path[MAX_PATH+1];
951 int result;
952 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953
Victor Stinner8c62be82010-05-06 00:08:46 +0000954 if(!SetCurrentDirectoryA(path))
955 return FALSE;
956 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
957 if (!result)
958 return FALSE;
959 /* In the ANSI API, there should not be any paths longer
960 than MAX_PATH. */
961 assert(result <= MAX_PATH+1);
962 if (strncmp(new_path, "\\\\", 2) == 0 ||
963 strncmp(new_path, "//", 2) == 0)
964 /* UNC path, nothing to do. */
965 return TRUE;
966 env[1] = new_path[0];
967 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968}
969
970/* The Unicode version differs from the ANSI version
971 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000972static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973win32_wchdir(LPCWSTR path)
974{
Victor Stinner8c62be82010-05-06 00:08:46 +0000975 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
976 int result;
977 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978
Victor Stinner8c62be82010-05-06 00:08:46 +0000979 if(!SetCurrentDirectoryW(path))
980 return FALSE;
981 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
982 if (!result)
983 return FALSE;
984 if (result > MAX_PATH+1) {
985 new_path = malloc(result * sizeof(wchar_t));
986 if (!new_path) {
987 SetLastError(ERROR_OUTOFMEMORY);
988 return FALSE;
989 }
990 result = GetCurrentDirectoryW(result, new_path);
991 if (!result) {
992 free(new_path);
993 return FALSE;
994 }
995 }
996 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
997 wcsncmp(new_path, L"//", 2) == 0)
998 /* UNC path, nothing to do. */
999 return TRUE;
1000 env[1] = new_path[0];
1001 result = SetEnvironmentVariableW(env, new_path);
1002 if (new_path != _new_path)
1003 free(new_path);
1004 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005}
1006#endif
1007
Martin v. Löwis14694662006-02-03 12:54:16 +00001008#ifdef MS_WINDOWS
1009/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1010 - time stamps are restricted to second resolution
1011 - file modification times suffer from forth-and-back conversions between
1012 UTC and local time
1013 Therefore, we implement our own stat, based on the Win32 API directly.
1014*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001015#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001016
1017struct win32_stat{
1018 int st_dev;
1019 __int64 st_ino;
1020 unsigned short st_mode;
1021 int st_nlink;
1022 int st_uid;
1023 int st_gid;
1024 int st_rdev;
1025 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001026 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001027 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001028 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001029 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001030 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001031 int st_ctime_nsec;
1032};
1033
1034static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1035
1036static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001037FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001038{
Victor Stinner8c62be82010-05-06 00:08:46 +00001039 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1040 /* Cannot simply cast and dereference in_ptr,
1041 since it might not be aligned properly */
1042 __int64 in;
1043 memcpy(&in, in_ptr, sizeof(in));
1044 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001045 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001046}
1047
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001049time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050{
Victor Stinner8c62be82010-05-06 00:08:46 +00001051 /* XXX endianness */
1052 __int64 out;
1053 out = time_in + secs_between_epochs;
1054 out = out * 10000000 + nsec_in / 100;
1055 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056}
1057
Martin v. Löwis14694662006-02-03 12:54:16 +00001058/* Below, we *know* that ugo+r is 0444 */
1059#if _S_IREAD != 0400
1060#error Unsupported C library
1061#endif
1062static int
1063attributes_to_mode(DWORD attr)
1064{
Victor Stinner8c62be82010-05-06 00:08:46 +00001065 int m = 0;
1066 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1067 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1068 else
1069 m |= _S_IFREG;
1070 if (attr & FILE_ATTRIBUTE_READONLY)
1071 m |= 0444;
1072 else
1073 m |= 0666;
1074 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001075}
1076
1077static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001078attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001079{
Victor Stinner8c62be82010-05-06 00:08:46 +00001080 memset(result, 0, sizeof(*result));
1081 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1082 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1083 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1084 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1085 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001086 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001087 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001088 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1089 /* first clear the S_IFMT bits */
1090 result->st_mode ^= (result->st_mode & 0170000);
1091 /* now set the bits that make this a symlink */
1092 result->st_mode |= 0120000;
1093 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001094
Victor Stinner8c62be82010-05-06 00:08:46 +00001095 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001096}
1097
Guido van Rossumd8faa362007-04-27 19:54:29 +00001098static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001099attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001100{
Victor Stinner8c62be82010-05-06 00:08:46 +00001101 HANDLE hFindFile;
1102 WIN32_FIND_DATAA FileData;
1103 hFindFile = FindFirstFileA(pszFile, &FileData);
1104 if (hFindFile == INVALID_HANDLE_VALUE)
1105 return FALSE;
1106 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001107 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001108 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001109 info->dwFileAttributes = FileData.dwFileAttributes;
1110 info->ftCreationTime = FileData.ftCreationTime;
1111 info->ftLastAccessTime = FileData.ftLastAccessTime;
1112 info->ftLastWriteTime = FileData.ftLastWriteTime;
1113 info->nFileSizeHigh = FileData.nFileSizeHigh;
1114 info->nFileSizeLow = FileData.nFileSizeLow;
1115/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001116 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1117 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001118 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119}
1120
1121static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001122attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001123{
Victor Stinner8c62be82010-05-06 00:08:46 +00001124 HANDLE hFindFile;
1125 WIN32_FIND_DATAW FileData;
1126 hFindFile = FindFirstFileW(pszFile, &FileData);
1127 if (hFindFile == INVALID_HANDLE_VALUE)
1128 return FALSE;
1129 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001130 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001132 info->dwFileAttributes = FileData.dwFileAttributes;
1133 info->ftCreationTime = FileData.ftCreationTime;
1134 info->ftLastAccessTime = FileData.ftLastAccessTime;
1135 info->ftLastWriteTime = FileData.ftLastWriteTime;
1136 info->nFileSizeHigh = FileData.nFileSizeHigh;
1137 info->nFileSizeLow = FileData.nFileSizeLow;
1138/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001139 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1140 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001141 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001142}
1143
Brian Curtind25aef52011-06-13 15:16:04 -05001144/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1145static int has_GetFinalPathNameByHandle = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001146static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1147 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001148static int
Brian Curtind25aef52011-06-13 15:16:04 -05001149check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001150{
Brian Curtind25aef52011-06-13 15:16:04 -05001151 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001152 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1153 DWORD);
1154
Brian Curtind25aef52011-06-13 15:16:04 -05001155 /* only recheck */
1156 if (!has_GetFinalPathNameByHandle)
1157 {
1158 hKernel32 = GetModuleHandle("KERNEL32");
1159 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1160 "GetFinalPathNameByHandleA");
1161 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1162 "GetFinalPathNameByHandleW");
1163 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1164 Py_GetFinalPathNameByHandleW;
1165 }
1166 return has_GetFinalPathNameByHandle;
1167}
1168
1169static BOOL
1170get_target_path(HANDLE hdl, wchar_t **target_path)
1171{
1172 int buf_size, result_length;
1173 wchar_t *buf;
1174
1175 /* We have a good handle to the target, use it to determine
1176 the target path name (then we'll call lstat on it). */
1177 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1178 VOLUME_NAME_DOS);
1179 if(!buf_size)
1180 return FALSE;
1181
1182 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001183 if (!buf) {
1184 SetLastError(ERROR_OUTOFMEMORY);
1185 return FALSE;
1186 }
1187
Brian Curtind25aef52011-06-13 15:16:04 -05001188 result_length = Py_GetFinalPathNameByHandleW(hdl,
1189 buf, buf_size, VOLUME_NAME_DOS);
1190
1191 if(!result_length) {
1192 free(buf);
1193 return FALSE;
1194 }
1195
1196 if(!CloseHandle(hdl)) {
1197 free(buf);
1198 return FALSE;
1199 }
1200
1201 buf[result_length] = 0;
1202
1203 *target_path = buf;
1204 return TRUE;
1205}
1206
1207static int
1208win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1209 BOOL traverse);
1210static int
1211win32_xstat_impl(const char *path, struct win32_stat *result,
1212 BOOL traverse)
1213{
Victor Stinner26de69d2011-06-17 15:15:38 +02001214 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001215 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001217 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001218 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 const char *dot;
1220
Brian Curtind25aef52011-06-13 15:16:04 -05001221 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001222 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1223 traverse reparse point. */
1224 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001225 }
1226
Brian Curtinf5e76d02010-11-24 13:14:05 +00001227 hFile = CreateFileA(
1228 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001229 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001230 0, /* share mode */
1231 NULL, /* security attributes */
1232 OPEN_EXISTING,
1233 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001234 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1235 Because of this, calls like GetFinalPathNameByHandle will return
1236 the symlink path agin and not the actual final path. */
1237 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1238 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239 NULL);
1240
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001241 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001242 /* Either the target doesn't exist, or we don't have access to
1243 get a handle to it. If the former, we need to return an error.
1244 If the latter, we can use attributes_from_dir. */
1245 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001246 return -1;
1247 /* Could not get attributes on open file. Fall back to
1248 reading the directory. */
1249 if (!attributes_from_dir(path, &info, &reparse_tag))
1250 /* Very strange. This should not fail now */
1251 return -1;
1252 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1253 if (traverse) {
1254 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001255 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001256 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001257 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001258 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001259 } else {
1260 if (!GetFileInformationByHandle(hFile, &info)) {
1261 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001262 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001263 }
1264 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001265 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1266 return -1;
1267
1268 /* Close the outer open file handle now that we're about to
1269 reopen it with different flags. */
1270 if (!CloseHandle(hFile))
1271 return -1;
1272
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001273 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001274 /* In order to call GetFinalPathNameByHandle we need to open
1275 the file without the reparse handling flag set. */
1276 hFile2 = CreateFileA(
1277 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1278 NULL, OPEN_EXISTING,
1279 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1280 NULL);
1281 if (hFile2 == INVALID_HANDLE_VALUE)
1282 return -1;
1283
1284 if (!get_target_path(hFile2, &target_path))
1285 return -1;
1286
1287 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001288 free(target_path);
1289 return code;
1290 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001291 } else
1292 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001293 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001294 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001295
1296 /* Set S_IEXEC if it is an .exe, .bat, ... */
1297 dot = strrchr(path, '.');
1298 if (dot) {
1299 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1300 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1301 result->st_mode |= 0111;
1302 }
1303 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001304}
1305
1306static int
Brian Curtind25aef52011-06-13 15:16:04 -05001307win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1308 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001309{
1310 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001311 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001312 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001313 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001314 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001315 const wchar_t *dot;
1316
Brian Curtind25aef52011-06-13 15:16:04 -05001317 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001318 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1319 traverse reparse point. */
1320 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001321 }
1322
Brian Curtinf5e76d02010-11-24 13:14:05 +00001323 hFile = CreateFileW(
1324 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001325 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001326 0, /* share mode */
1327 NULL, /* security attributes */
1328 OPEN_EXISTING,
1329 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001330 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1331 Because of this, calls like GetFinalPathNameByHandle will return
1332 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001333 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001334 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001335 NULL);
1336
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001337 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001338 /* Either the target doesn't exist, or we don't have access to
1339 get a handle to it. If the former, we need to return an error.
1340 If the latter, we can use attributes_from_dir. */
1341 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001342 return -1;
1343 /* Could not get attributes on open file. Fall back to
1344 reading the directory. */
1345 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1346 /* Very strange. This should not fail now */
1347 return -1;
1348 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1349 if (traverse) {
1350 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001351 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001352 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001353 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001354 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001355 } else {
1356 if (!GetFileInformationByHandle(hFile, &info)) {
1357 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001358 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001359 }
1360 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001361 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1362 return -1;
1363
1364 /* Close the outer open file handle now that we're about to
1365 reopen it with different flags. */
1366 if (!CloseHandle(hFile))
1367 return -1;
1368
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001370 /* In order to call GetFinalPathNameByHandle we need to open
1371 the file without the reparse handling flag set. */
1372 hFile2 = CreateFileW(
1373 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1374 NULL, OPEN_EXISTING,
1375 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1376 NULL);
1377 if (hFile2 == INVALID_HANDLE_VALUE)
1378 return -1;
1379
1380 if (!get_target_path(hFile2, &target_path))
1381 return -1;
1382
1383 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001384 free(target_path);
1385 return code;
1386 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001387 } else
1388 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001389 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001390 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001391
1392 /* Set S_IEXEC if it is an .exe, .bat, ... */
1393 dot = wcsrchr(path, '.');
1394 if (dot) {
1395 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1396 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1397 result->st_mode |= 0111;
1398 }
1399 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001400}
1401
1402static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001403win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001405 /* Protocol violation: we explicitly clear errno, instead of
1406 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001407 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001408 errno = 0;
1409 return code;
1410}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001411
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001412static int
1413win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1414{
1415 /* Protocol violation: we explicitly clear errno, instead of
1416 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001417 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001418 errno = 0;
1419 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001420}
Brian Curtind25aef52011-06-13 15:16:04 -05001421/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001422
1423 In Posix, stat automatically traverses symlinks and returns the stat
1424 structure for the target. In Windows, the equivalent GetFileAttributes by
1425 default does not traverse symlinks and instead returns attributes for
1426 the symlink.
1427
1428 Therefore, win32_lstat will get the attributes traditionally, and
1429 win32_stat will first explicitly resolve the symlink target and then will
1430 call win32_lstat on that result.
1431
Ezio Melotti4969f702011-03-15 05:59:46 +02001432 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001433
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001434static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001435win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001436{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001437 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001438}
1439
Victor Stinner8c62be82010-05-06 00:08:46 +00001440static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001441win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001442{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001443 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001444}
1445
1446static int
1447win32_stat(const char* path, struct win32_stat *result)
1448{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001449 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001450}
1451
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001452static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001453win32_stat_w(const wchar_t* path, struct win32_stat *result)
1454{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001455 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001456}
1457
1458static int
1459win32_fstat(int file_number, struct win32_stat *result)
1460{
Victor Stinner8c62be82010-05-06 00:08:46 +00001461 BY_HANDLE_FILE_INFORMATION info;
1462 HANDLE h;
1463 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001464
Victor Stinner8c62be82010-05-06 00:08:46 +00001465 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001466
Victor Stinner8c62be82010-05-06 00:08:46 +00001467 /* Protocol violation: we explicitly clear errno, instead of
1468 setting it to a POSIX error. Callers should use GetLastError. */
1469 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001470
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 if (h == INVALID_HANDLE_VALUE) {
1472 /* This is really a C library error (invalid file handle).
1473 We set the Win32 error to the closes one matching. */
1474 SetLastError(ERROR_INVALID_HANDLE);
1475 return -1;
1476 }
1477 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001478
Victor Stinner8c62be82010-05-06 00:08:46 +00001479 type = GetFileType(h);
1480 if (type == FILE_TYPE_UNKNOWN) {
1481 DWORD error = GetLastError();
1482 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001483 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001484 }
1485 /* else: valid but unknown file */
1486 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001487
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 if (type != FILE_TYPE_DISK) {
1489 if (type == FILE_TYPE_CHAR)
1490 result->st_mode = _S_IFCHR;
1491 else if (type == FILE_TYPE_PIPE)
1492 result->st_mode = _S_IFIFO;
1493 return 0;
1494 }
1495
1496 if (!GetFileInformationByHandle(h, &info)) {
1497 return -1;
1498 }
1499
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001500 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1503 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001504}
1505
1506#endif /* MS_WINDOWS */
1507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001508PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001509"stat_result: Result from stat or lstat.\n\n\
1510This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001511 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1513\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001514Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1515or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001516\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001518
1519static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001520 {"st_mode", "protection bits"},
1521 {"st_ino", "inode"},
1522 {"st_dev", "device"},
1523 {"st_nlink", "number of hard links"},
1524 {"st_uid", "user ID of owner"},
1525 {"st_gid", "group ID of owner"},
1526 {"st_size", "total size, in bytes"},
1527 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1528 {NULL, "integer time of last access"},
1529 {NULL, "integer time of last modification"},
1530 {NULL, "integer time of last change"},
1531 {"st_atime", "time of last access"},
1532 {"st_mtime", "time of last modification"},
1533 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001534#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001536#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001537#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001538 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001539#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001540#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001542#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001543#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001544 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001545#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001546#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001547 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001548#endif
1549#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001550 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001551#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001552 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001553};
1554
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001555#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001556#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001557#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001558#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001559#endif
1560
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001561#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001562#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1563#else
1564#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1565#endif
1566
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001567#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001568#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1569#else
1570#define ST_RDEV_IDX ST_BLOCKS_IDX
1571#endif
1572
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001573#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1574#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1575#else
1576#define ST_FLAGS_IDX ST_RDEV_IDX
1577#endif
1578
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001579#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001580#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001581#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001582#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001583#endif
1584
1585#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1586#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1587#else
1588#define ST_BIRTHTIME_IDX ST_GEN_IDX
1589#endif
1590
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001591static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001592 "stat_result", /* name */
1593 stat_result__doc__, /* doc */
1594 stat_result_fields,
1595 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001596};
1597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001599"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1600This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001601 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001602or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001603\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001605
1606static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 {"f_bsize", },
1608 {"f_frsize", },
1609 {"f_blocks", },
1610 {"f_bfree", },
1611 {"f_bavail", },
1612 {"f_files", },
1613 {"f_ffree", },
1614 {"f_favail", },
1615 {"f_flag", },
1616 {"f_namemax",},
1617 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001618};
1619
1620static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 "statvfs_result", /* name */
1622 statvfs_result__doc__, /* doc */
1623 statvfs_result_fields,
1624 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001625};
1626
Ross Lagerwall7807c352011-03-17 20:20:30 +02001627#if defined(HAVE_WAITID) && !defined(__APPLE__)
1628PyDoc_STRVAR(waitid_result__doc__,
1629"waitid_result: Result from waitid.\n\n\
1630This object may be accessed either as a tuple of\n\
1631 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1632or via the attributes si_pid, si_uid, and so on.\n\
1633\n\
1634See os.waitid for more information.");
1635
1636static PyStructSequence_Field waitid_result_fields[] = {
1637 {"si_pid", },
1638 {"si_uid", },
1639 {"si_signo", },
1640 {"si_status", },
1641 {"si_code", },
1642 {0}
1643};
1644
1645static PyStructSequence_Desc waitid_result_desc = {
1646 "waitid_result", /* name */
1647 waitid_result__doc__, /* doc */
1648 waitid_result_fields,
1649 5
1650};
1651static PyTypeObject WaitidResultType;
1652#endif
1653
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001655static PyTypeObject StatResultType;
1656static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001657#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001658static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001659#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001660static newfunc structseq_new;
1661
1662static PyObject *
1663statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1664{
Victor Stinner8c62be82010-05-06 00:08:46 +00001665 PyStructSequence *result;
1666 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001667
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 result = (PyStructSequence*)structseq_new(type, args, kwds);
1669 if (!result)
1670 return NULL;
1671 /* If we have been initialized from a tuple,
1672 st_?time might be set to None. Initialize it
1673 from the int slots. */
1674 for (i = 7; i <= 9; i++) {
1675 if (result->ob_item[i+3] == Py_None) {
1676 Py_DECREF(Py_None);
1677 Py_INCREF(result->ob_item[i]);
1678 result->ob_item[i+3] = result->ob_item[i];
1679 }
1680 }
1681 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001682}
1683
1684
1685
1686/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001687static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001688
1689PyDoc_STRVAR(stat_float_times__doc__,
1690"stat_float_times([newval]) -> oldval\n\n\
1691Determine whether os.[lf]stat represents time stamps as float objects.\n\
1692If newval is True, future calls to stat() return floats, if it is False,\n\
1693future calls return ints. \n\
1694If newval is omitted, return the current setting.\n");
1695
1696static PyObject*
1697stat_float_times(PyObject* self, PyObject *args)
1698{
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 int newval = -1;
1700 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1701 return NULL;
1702 if (newval == -1)
1703 /* Return old value */
1704 return PyBool_FromLong(_stat_float_times);
1705 _stat_float_times = newval;
1706 Py_INCREF(Py_None);
1707 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001708}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001709
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001710static void
1711fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1712{
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001714#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001716#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001717 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001718#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001719 if (!ival)
1720 return;
1721 if (_stat_float_times) {
1722 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1723 } else {
1724 fval = ival;
1725 Py_INCREF(fval);
1726 }
1727 PyStructSequence_SET_ITEM(v, index, ival);
1728 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001729}
1730
Tim Peters5aa91602002-01-30 05:46:57 +00001731/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001732 (used by posix_stat() and posix_fstat()) */
1733static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001734_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001735{
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 unsigned long ansec, mnsec, cnsec;
1737 PyObject *v = PyStructSequence_New(&StatResultType);
1738 if (v == NULL)
1739 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001740
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001742#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001743 PyStructSequence_SET_ITEM(v, 1,
1744 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001745#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001746 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001747#endif
1748#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, 2,
1750 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001751#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001753#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001754 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1755 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1756 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001757#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 PyStructSequence_SET_ITEM(v, 6,
1759 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001760#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001761 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001762#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001763
Martin v. Löwis14694662006-02-03 12:54:16 +00001764#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001765 ansec = st->st_atim.tv_nsec;
1766 mnsec = st->st_mtim.tv_nsec;
1767 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001768#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 ansec = st->st_atimespec.tv_nsec;
1770 mnsec = st->st_mtimespec.tv_nsec;
1771 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001772#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001773 ansec = st->st_atime_nsec;
1774 mnsec = st->st_mtime_nsec;
1775 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001776#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001778#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 fill_time(v, 7, st->st_atime, ansec);
1780 fill_time(v, 8, st->st_mtime, mnsec);
1781 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001782
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001783#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1785 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001786#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001787#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1789 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001790#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001791#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1793 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001794#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001795#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1797 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001798#endif
1799#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001800 {
1801 PyObject *val;
1802 unsigned long bsec,bnsec;
1803 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001804#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001805 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001806#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001807 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001808#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 if (_stat_float_times) {
1810 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1811 } else {
1812 val = PyLong_FromLong((long)bsec);
1813 }
1814 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1815 val);
1816 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001817#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001818#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001819 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1820 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001821#endif
Fred Drake699f3522000-06-29 21:12:41 +00001822
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 if (PyErr_Occurred()) {
1824 Py_DECREF(v);
1825 return NULL;
1826 }
Fred Drake699f3522000-06-29 21:12:41 +00001827
Victor Stinner8c62be82010-05-06 00:08:46 +00001828 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001829}
1830
Barry Warsaw53699e91996-12-10 23:23:01 +00001831static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001832posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001834#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001836#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001838#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 char *wformat,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001840 int (*wstatfunc)(const wchar_t *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841{
Victor Stinner8c62be82010-05-06 00:08:46 +00001842 STRUCT_STAT st;
1843 PyObject *opath;
1844 char *path;
1845 int res;
1846 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001847
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001848#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001849 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 if (PyArg_ParseTuple(args, wformat, &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001851 wchar_t *wpath = PyUnicode_AsUnicode(po);
1852 if (wpath == NULL)
1853 return NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00001854
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 res = wstatfunc(wpath, &st);
1857 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001858
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 if (res != 0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001860 return win32_error_object("stat", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 return _pystat_fromstructstat(&st);
1862 }
1863 /* Drop the argument parsing error as narrow strings
1864 are also valid. */
1865 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001866#endif
1867
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 if (!PyArg_ParseTuple(args, format,
1869 PyUnicode_FSConverter, &opath))
1870 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001871#ifdef MS_WINDOWS
1872 if (win32_warn_bytes_api()) {
1873 Py_DECREF(opath);
1874 return NULL;
1875 }
1876#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 path = PyBytes_AsString(opath);
1878 Py_BEGIN_ALLOW_THREADS
1879 res = (*statfunc)(path, &st);
1880 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001881
Victor Stinner8c62be82010-05-06 00:08:46 +00001882 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001883#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001884 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001885#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001886 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001887#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001888 }
1889 else
1890 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001891
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 Py_DECREF(opath);
1893 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001894}
1895
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001896/* POSIX methods */
1897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001899"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001900Use the real uid/gid to test for access to a path. Note that most\n\
1901operations will use the effective uid/gid, therefore this routine can\n\
1902be used in a suid/sgid environment to test if the invoking user has the\n\
1903specified access to the path. The mode argument can be F_OK to test\n\
1904existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001905
1906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001907posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001908{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001909 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00001910 int mode;
1911
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001912#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001913 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02001914 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001915 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001916 wchar_t* wpath = PyUnicode_AsUnicode(po);
1917 if (wpath == NULL)
1918 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001919 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001920 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00001921 Py_END_ALLOW_THREADS
1922 goto finish;
1923 }
1924 /* Drop the argument parsing error as narrow strings
1925 are also valid. */
1926 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001927 if (!PyArg_ParseTuple(args, "yi:access", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00001928 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001929 if (win32_warn_bytes_api())
1930 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 Py_BEGIN_ALLOW_THREADS
1932 attr = GetFileAttributesA(path);
1933 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001934finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 if (attr == 0xFFFFFFFF)
1936 /* File does not exist, or cannot read attributes */
1937 return PyBool_FromLong(0);
1938 /* Access is possible if either write access wasn't requested, or
1939 the file isn't read-only, or if it's a directory, as there are
1940 no read-only directories on Windows. */
1941 return PyBool_FromLong(!(mode & 2)
1942 || !(attr & FILE_ATTRIBUTE_READONLY)
1943 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001944#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001945 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 int res;
1947 if (!PyArg_ParseTuple(args, "O&i:access",
1948 PyUnicode_FSConverter, &opath, &mode))
1949 return NULL;
1950 path = PyBytes_AsString(opath);
1951 Py_BEGIN_ALLOW_THREADS
1952 res = access(path, mode);
1953 Py_END_ALLOW_THREADS
1954 Py_DECREF(opath);
1955 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001956#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001957}
1958
Guido van Rossumd371ff11999-01-25 16:12:23 +00001959#ifndef F_OK
1960#define F_OK 0
1961#endif
1962#ifndef R_OK
1963#define R_OK 4
1964#endif
1965#ifndef W_OK
1966#define W_OK 2
1967#endif
1968#ifndef X_OK
1969#define X_OK 1
1970#endif
1971
1972#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001974"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001975Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001976
1977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001978posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001979{
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 int id;
1981 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001982
Victor Stinner8c62be82010-05-06 00:08:46 +00001983 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1984 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001985
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001986#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001987 /* file descriptor 0 only, the default input device (stdin) */
1988 if (id == 0) {
1989 ret = ttyname();
1990 }
1991 else {
1992 ret = NULL;
1993 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001994#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001996#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001997 if (ret == NULL)
1998 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001999 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002000}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002001#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002002
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002003#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002005"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002007
2008static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002009posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002010{
Victor Stinner8c62be82010-05-06 00:08:46 +00002011 char *ret;
2012 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002013
Greg Wardb48bc172000-03-01 21:51:56 +00002014#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002016#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002017 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002018#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002019 if (ret == NULL)
2020 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002021 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002022}
2023#endif
2024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002026"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002028
Barry Warsaw53699e91996-12-10 23:23:01 +00002029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002030posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002031{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002032#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002036#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002037 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002038#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002039 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002040#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002041}
2042
Fred Drake4d1e64b2002-04-15 19:40:07 +00002043#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002045"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002046Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002048
2049static PyObject *
2050posix_fchdir(PyObject *self, PyObject *fdobj)
2051{
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002053}
2054#endif /* HAVE_FCHDIR */
2055
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002058"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060
Barry Warsaw53699e91996-12-10 23:23:01 +00002061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002062posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002063{
Victor Stinner8c62be82010-05-06 00:08:46 +00002064 PyObject *opath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002065 const char *path = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 int i;
2067 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002068#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002069 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002070 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00002071 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002072 wchar_t *wpath = PyUnicode_AsUnicode(po);
2073 if (wpath == NULL)
2074 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002075 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002076 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002077 if (attr != 0xFFFFFFFF) {
2078 if (i & _S_IWRITE)
2079 attr &= ~FILE_ATTRIBUTE_READONLY;
2080 else
2081 attr |= FILE_ATTRIBUTE_READONLY;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002082 res = SetFileAttributesW(wpath, attr);
Victor Stinner8c62be82010-05-06 00:08:46 +00002083 }
2084 else
2085 res = 0;
2086 Py_END_ALLOW_THREADS
2087 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002088 return win32_error_object("chmod", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 Py_INCREF(Py_None);
2090 return Py_None;
2091 }
2092 /* Drop the argument parsing error as narrow strings
2093 are also valid. */
2094 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002095
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002096 if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002098 if (win32_warn_bytes_api())
2099 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002100 Py_BEGIN_ALLOW_THREADS
2101 attr = GetFileAttributesA(path);
2102 if (attr != 0xFFFFFFFF) {
2103 if (i & _S_IWRITE)
2104 attr &= ~FILE_ATTRIBUTE_READONLY;
2105 else
2106 attr |= FILE_ATTRIBUTE_READONLY;
2107 res = SetFileAttributesA(path, attr);
2108 }
2109 else
2110 res = 0;
2111 Py_END_ALLOW_THREADS
2112 if (!res) {
2113 win32_error("chmod", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002114 return NULL;
2115 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002116 Py_INCREF(Py_None);
2117 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002118#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002119 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2120 &opath, &i))
2121 return NULL;
2122 path = PyBytes_AsString(opath);
2123 Py_BEGIN_ALLOW_THREADS
2124 res = chmod(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_INCREF(Py_None);
2130 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002131#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002132}
2133
Christian Heimes4e30a842007-11-30 22:12:06 +00002134#ifdef HAVE_FCHMOD
2135PyDoc_STRVAR(posix_fchmod__doc__,
2136"fchmod(fd, mode)\n\n\
2137Change the access permissions of the file given by file\n\
2138descriptor fd.");
2139
2140static PyObject *
2141posix_fchmod(PyObject *self, PyObject *args)
2142{
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 int fd, mode, res;
2144 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2145 return NULL;
2146 Py_BEGIN_ALLOW_THREADS
2147 res = fchmod(fd, mode);
2148 Py_END_ALLOW_THREADS
2149 if (res < 0)
2150 return posix_error();
2151 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002152}
2153#endif /* HAVE_FCHMOD */
2154
2155#ifdef HAVE_LCHMOD
2156PyDoc_STRVAR(posix_lchmod__doc__,
2157"lchmod(path, mode)\n\n\
2158Change the access permissions of a file. If path is a symlink, this\n\
2159affects the link itself rather than the target.");
2160
2161static PyObject *
2162posix_lchmod(PyObject *self, PyObject *args)
2163{
Victor Stinner8c62be82010-05-06 00:08:46 +00002164 PyObject *opath;
2165 char *path;
2166 int i;
2167 int res;
2168 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2169 &opath, &i))
2170 return NULL;
2171 path = PyBytes_AsString(opath);
2172 Py_BEGIN_ALLOW_THREADS
2173 res = lchmod(path, i);
2174 Py_END_ALLOW_THREADS
2175 if (res < 0)
2176 return posix_error_with_allocated_filename(opath);
2177 Py_DECREF(opath);
2178 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002179}
2180#endif /* HAVE_LCHMOD */
2181
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002182
Thomas Wouterscf297e42007-02-23 15:07:44 +00002183#ifdef HAVE_CHFLAGS
2184PyDoc_STRVAR(posix_chflags__doc__,
2185"chflags(path, flags)\n\n\
2186Set file flags.");
2187
2188static PyObject *
2189posix_chflags(PyObject *self, PyObject *args)
2190{
Victor Stinner8c62be82010-05-06 00:08:46 +00002191 PyObject *opath;
2192 char *path;
2193 unsigned long flags;
2194 int res;
2195 if (!PyArg_ParseTuple(args, "O&k:chflags",
2196 PyUnicode_FSConverter, &opath, &flags))
2197 return NULL;
2198 path = PyBytes_AsString(opath);
2199 Py_BEGIN_ALLOW_THREADS
2200 res = chflags(path, flags);
2201 Py_END_ALLOW_THREADS
2202 if (res < 0)
2203 return posix_error_with_allocated_filename(opath);
2204 Py_DECREF(opath);
2205 Py_INCREF(Py_None);
2206 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002207}
2208#endif /* HAVE_CHFLAGS */
2209
2210#ifdef HAVE_LCHFLAGS
2211PyDoc_STRVAR(posix_lchflags__doc__,
2212"lchflags(path, flags)\n\n\
2213Set file flags.\n\
2214This function will not follow symbolic links.");
2215
2216static PyObject *
2217posix_lchflags(PyObject *self, PyObject *args)
2218{
Victor Stinner8c62be82010-05-06 00:08:46 +00002219 PyObject *opath;
2220 char *path;
2221 unsigned long flags;
2222 int res;
2223 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2224 PyUnicode_FSConverter, &opath, &flags))
2225 return NULL;
2226 path = PyBytes_AsString(opath);
2227 Py_BEGIN_ALLOW_THREADS
2228 res = lchflags(path, flags);
2229 Py_END_ALLOW_THREADS
2230 if (res < 0)
2231 return posix_error_with_allocated_filename(opath);
2232 Py_DECREF(opath);
2233 Py_INCREF(Py_None);
2234 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002235}
2236#endif /* HAVE_LCHFLAGS */
2237
Martin v. Löwis244edc82001-10-04 22:44:26 +00002238#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002239PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002240"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002241Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002242
2243static PyObject *
2244posix_chroot(PyObject *self, PyObject *args)
2245{
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002247}
2248#endif
2249
Guido van Rossum21142a01999-01-08 21:05:37 +00002250#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002252"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002254
2255static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002256posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002257{
Stefan Krah0e803b32010-11-26 16:16:47 +00002258 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002259}
2260#endif /* HAVE_FSYNC */
2261
Ross Lagerwall7807c352011-03-17 20:20:30 +02002262#ifdef HAVE_SYNC
2263PyDoc_STRVAR(posix_sync__doc__,
2264"sync()\n\n\
2265Force write of everything to disk.");
2266
2267static PyObject *
2268posix_sync(PyObject *self, PyObject *noargs)
2269{
2270 Py_BEGIN_ALLOW_THREADS
2271 sync();
2272 Py_END_ALLOW_THREADS
2273 Py_RETURN_NONE;
2274}
2275#endif
2276
Guido van Rossum21142a01999-01-08 21:05:37 +00002277#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002278
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002279#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002280extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2281#endif
2282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002283PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002284"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002285force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002286 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002287
2288static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002289posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002290{
Stefan Krah0e803b32010-11-26 16:16:47 +00002291 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002292}
2293#endif /* HAVE_FDATASYNC */
2294
2295
Fredrik Lundh10723342000-07-10 16:38:09 +00002296#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002297PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002298"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002299Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002300
Barry Warsaw53699e91996-12-10 23:23:01 +00002301static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002302posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002303{
Victor Stinner8c62be82010-05-06 00:08:46 +00002304 PyObject *opath;
2305 char *path;
2306 long uid, gid;
2307 int res;
2308 if (!PyArg_ParseTuple(args, "O&ll:chown",
2309 PyUnicode_FSConverter, &opath,
2310 &uid, &gid))
2311 return NULL;
2312 path = PyBytes_AsString(opath);
2313 Py_BEGIN_ALLOW_THREADS
2314 res = chown(path, (uid_t) uid, (gid_t) gid);
2315 Py_END_ALLOW_THREADS
2316 if (res < 0)
2317 return posix_error_with_allocated_filename(opath);
2318 Py_DECREF(opath);
2319 Py_INCREF(Py_None);
2320 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002321}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002322#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002323
Christian Heimes4e30a842007-11-30 22:12:06 +00002324#ifdef HAVE_FCHOWN
2325PyDoc_STRVAR(posix_fchown__doc__,
2326"fchown(fd, uid, gid)\n\n\
2327Change the owner and group id of the file given by file descriptor\n\
2328fd to the numeric uid and gid.");
2329
2330static PyObject *
2331posix_fchown(PyObject *self, PyObject *args)
2332{
Victor Stinner8c62be82010-05-06 00:08:46 +00002333 int fd;
2334 long uid, gid;
2335 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002336 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002337 return NULL;
2338 Py_BEGIN_ALLOW_THREADS
2339 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2340 Py_END_ALLOW_THREADS
2341 if (res < 0)
2342 return posix_error();
2343 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002344}
2345#endif /* HAVE_FCHOWN */
2346
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002347#ifdef HAVE_LCHOWN
2348PyDoc_STRVAR(posix_lchown__doc__,
2349"lchown(path, uid, gid)\n\n\
2350Change the owner and group id of path to the numeric uid and gid.\n\
2351This function will not follow symbolic links.");
2352
2353static PyObject *
2354posix_lchown(PyObject *self, PyObject *args)
2355{
Victor Stinner8c62be82010-05-06 00:08:46 +00002356 PyObject *opath;
2357 char *path;
2358 long uid, gid;
2359 int res;
2360 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2361 PyUnicode_FSConverter, &opath,
2362 &uid, &gid))
2363 return NULL;
2364 path = PyBytes_AsString(opath);
2365 Py_BEGIN_ALLOW_THREADS
2366 res = lchown(path, (uid_t) uid, (gid_t) gid);
2367 Py_END_ALLOW_THREADS
2368 if (res < 0)
2369 return posix_error_with_allocated_filename(opath);
2370 Py_DECREF(opath);
2371 Py_INCREF(Py_None);
2372 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002373}
2374#endif /* HAVE_LCHOWN */
2375
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002376
Guido van Rossum36bc6801995-06-14 22:54:23 +00002377#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002378static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002379posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002380{
Victor Stinner8c62be82010-05-06 00:08:46 +00002381 char buf[1026];
2382 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002383
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002384#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002385 if (!use_bytes) {
2386 wchar_t wbuf[1026];
2387 wchar_t *wbuf2 = wbuf;
2388 PyObject *resobj;
2389 DWORD len;
2390 Py_BEGIN_ALLOW_THREADS
2391 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2392 /* If the buffer is large enough, len does not include the
2393 terminating \0. If the buffer is too small, len includes
2394 the space needed for the terminator. */
2395 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2396 wbuf2 = malloc(len * sizeof(wchar_t));
2397 if (wbuf2)
2398 len = GetCurrentDirectoryW(len, wbuf2);
2399 }
2400 Py_END_ALLOW_THREADS
2401 if (!wbuf2) {
2402 PyErr_NoMemory();
2403 return NULL;
2404 }
2405 if (!len) {
2406 if (wbuf2 != wbuf) free(wbuf2);
2407 return win32_error("getcwdu", NULL);
2408 }
2409 resobj = PyUnicode_FromWideChar(wbuf2, len);
2410 if (wbuf2 != wbuf) free(wbuf2);
2411 return resobj;
2412 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01002413
2414 if (win32_warn_bytes_api())
2415 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002416#endif
2417
Victor Stinner8c62be82010-05-06 00:08:46 +00002418 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002419#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002420 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002421#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002422 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002423#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 Py_END_ALLOW_THREADS
2425 if (res == NULL)
2426 return posix_error();
2427 if (use_bytes)
2428 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002429 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002430}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002431
2432PyDoc_STRVAR(posix_getcwd__doc__,
2433"getcwd() -> path\n\n\
2434Return a unicode string representing the current working directory.");
2435
2436static PyObject *
2437posix_getcwd_unicode(PyObject *self)
2438{
2439 return posix_getcwd(0);
2440}
2441
2442PyDoc_STRVAR(posix_getcwdb__doc__,
2443"getcwdb() -> path\n\n\
2444Return a bytes string representing the current working directory.");
2445
2446static PyObject *
2447posix_getcwd_bytes(PyObject *self)
2448{
2449 return posix_getcwd(1);
2450}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002451#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002453
Guido van Rossumb6775db1994-08-01 11:34:53 +00002454#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002455PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002456"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002457Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002458
Barry Warsaw53699e91996-12-10 23:23:01 +00002459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002460posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002461{
Victor Stinner8c62be82010-05-06 00:08:46 +00002462 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002463}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002464#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002465
Brian Curtin1b9df392010-11-24 20:24:31 +00002466#ifdef MS_WINDOWS
2467PyDoc_STRVAR(win32_link__doc__,
2468"link(src, dst)\n\n\
2469Create a hard link to a file.");
2470
2471static PyObject *
2472win32_link(PyObject *self, PyObject *args)
2473{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002474 PyObject *src, *dst;
2475 BOOL ok;
Brian Curtin1b9df392010-11-24 20:24:31 +00002476
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002477 if (PyArg_ParseTuple(args, "UU:link", &src, &dst))
Victor Stinnereb5657a2011-09-30 01:44:27 +02002478 {
2479 wchar_t *wsrc, *wdst;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002480
2481 wsrc = PyUnicode_AsUnicode(src);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002482 if (wsrc == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002483 goto error;
2484 wdst = PyUnicode_AsUnicode(dst);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002485 if (wdst == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002486 goto error;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002487
Brian Curtinfc889c42010-11-28 23:59:46 +00002488 Py_BEGIN_ALLOW_THREADS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002489 ok = CreateHardLinkW(wdst, wsrc, NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002490 Py_END_ALLOW_THREADS
2491
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002492 if (!ok)
Brian Curtinfc889c42010-11-28 23:59:46 +00002493 return win32_error("link", NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002494 Py_RETURN_NONE;
2495 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002496 else {
2497 PyErr_Clear();
2498 if (!PyArg_ParseTuple(args, "O&O&:link",
2499 PyUnicode_FSConverter, &src,
2500 PyUnicode_FSConverter, &dst))
2501 return NULL;
Brian Curtinfc889c42010-11-28 23:59:46 +00002502
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002503 if (win32_warn_bytes_api())
2504 goto error;
Brian Curtinfc889c42010-11-28 23:59:46 +00002505
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002506 Py_BEGIN_ALLOW_THREADS
2507 ok = CreateHardLinkA(PyBytes_AS_STRING(dst),
2508 PyBytes_AS_STRING(src),
2509 NULL);
2510 Py_END_ALLOW_THREADS
2511
2512 Py_XDECREF(src);
2513 Py_XDECREF(dst);
2514
2515 if (!ok)
2516 return win32_error("link", NULL);
2517 Py_RETURN_NONE;
2518
2519 error:
2520 Py_XDECREF(src);
2521 Py_XDECREF(dst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002522 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002523 }
Brian Curtin1b9df392010-11-24 20:24:31 +00002524}
2525#endif /* MS_WINDOWS */
2526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002528PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002529"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002530Return a list containing the names of the entries in the directory.\n\
2531\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002532 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533\n\
2534The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002536
Barry Warsaw53699e91996-12-10 23:23:01 +00002537static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002538posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002539{
Victor Stinner8c62be82010-05-06 00:08:46 +00002540 /* XXX Should redo this putting the (now four) versions of opendir
2541 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002542#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002543
Victor Stinner8c62be82010-05-06 00:08:46 +00002544 PyObject *d, *v;
2545 HANDLE hFindFile;
2546 BOOL result;
2547 WIN32_FIND_DATA FileData;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002548 const char *path;
2549 Py_ssize_t pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002550 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2551 char *bufptr = namebuf;
2552 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002553
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002554 PyObject *po = NULL;
2555 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002556 WIN32_FIND_DATAW wFileData;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002557 wchar_t *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002558
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002559 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002560 po_wchars = L".";
2561 len = 1;
2562 } else {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02002563 po_wchars = PyUnicode_AsUnicodeAndSize(po, &len);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002564 if (po_wchars == NULL)
2565 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002566 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002567 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002568 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2569 if (!wnamebuf) {
2570 PyErr_NoMemory();
2571 return NULL;
2572 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002573 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002574 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002575 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00002576 if (wch != L'/' && wch != L'\\' && wch != L':')
2577 wnamebuf[len++] = L'\\';
2578 wcscpy(wnamebuf + len, L"*.*");
2579 }
2580 if ((d = PyList_New(0)) == NULL) {
2581 free(wnamebuf);
2582 return NULL;
2583 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002584 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002585 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002586 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002587 if (hFindFile == INVALID_HANDLE_VALUE) {
2588 int error = GetLastError();
2589 if (error == ERROR_FILE_NOT_FOUND) {
2590 free(wnamebuf);
2591 return d;
2592 }
2593 Py_DECREF(d);
2594 win32_error_unicode("FindFirstFileW", wnamebuf);
2595 free(wnamebuf);
2596 return NULL;
2597 }
2598 do {
2599 /* Skip over . and .. */
2600 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2601 wcscmp(wFileData.cFileName, L"..") != 0) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002602 v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 if (v == NULL) {
2604 Py_DECREF(d);
2605 d = NULL;
2606 break;
2607 }
2608 if (PyList_Append(d, v) != 0) {
2609 Py_DECREF(v);
2610 Py_DECREF(d);
2611 d = NULL;
2612 break;
2613 }
2614 Py_DECREF(v);
2615 }
2616 Py_BEGIN_ALLOW_THREADS
2617 result = FindNextFileW(hFindFile, &wFileData);
2618 Py_END_ALLOW_THREADS
2619 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2620 it got to the end of the directory. */
2621 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2622 Py_DECREF(d);
2623 win32_error_unicode("FindNextFileW", wnamebuf);
2624 FindClose(hFindFile);
2625 free(wnamebuf);
2626 return NULL;
2627 }
2628 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002629
Victor Stinner8c62be82010-05-06 00:08:46 +00002630 if (FindClose(hFindFile) == FALSE) {
2631 Py_DECREF(d);
2632 win32_error_unicode("FindClose", wnamebuf);
2633 free(wnamebuf);
2634 return NULL;
2635 }
2636 free(wnamebuf);
2637 return d;
2638 }
2639 /* Drop the argument parsing error as narrow strings
2640 are also valid. */
2641 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002643 if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen))
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002645 if (win32_warn_bytes_api())
2646 return NULL;
2647 if (pathlen+1 > MAX_PATH) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002648 PyErr_SetString(PyExc_ValueError, "path too long");
Victor Stinner8c62be82010-05-06 00:08:46 +00002649 return NULL;
2650 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002651 strcpy(namebuf, path);
2652 len = pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002653 if (len > 0) {
2654 char ch = namebuf[len-1];
2655 if (ch != SEP && ch != ALTSEP && ch != ':')
2656 namebuf[len++] = '/';
2657 strcpy(namebuf + len, "*.*");
2658 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002659
Victor Stinner8c62be82010-05-06 00:08:46 +00002660 if ((d = PyList_New(0)) == NULL)
2661 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002662
Antoine Pitroub73caab2010-08-09 23:39:31 +00002663 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002665 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002666 if (hFindFile == INVALID_HANDLE_VALUE) {
2667 int error = GetLastError();
2668 if (error == ERROR_FILE_NOT_FOUND)
2669 return d;
2670 Py_DECREF(d);
2671 return win32_error("FindFirstFile", namebuf);
2672 }
2673 do {
2674 /* Skip over . and .. */
2675 if (strcmp(FileData.cFileName, ".") != 0 &&
2676 strcmp(FileData.cFileName, "..") != 0) {
2677 v = PyBytes_FromString(FileData.cFileName);
2678 if (v == NULL) {
2679 Py_DECREF(d);
2680 d = NULL;
2681 break;
2682 }
2683 if (PyList_Append(d, v) != 0) {
2684 Py_DECREF(v);
2685 Py_DECREF(d);
2686 d = NULL;
2687 break;
2688 }
2689 Py_DECREF(v);
2690 }
2691 Py_BEGIN_ALLOW_THREADS
2692 result = FindNextFile(hFindFile, &FileData);
2693 Py_END_ALLOW_THREADS
2694 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2695 it got to the end of the directory. */
2696 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2697 Py_DECREF(d);
2698 win32_error("FindNextFile", namebuf);
2699 FindClose(hFindFile);
2700 return NULL;
2701 }
2702 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002703
Victor Stinner8c62be82010-05-06 00:08:46 +00002704 if (FindClose(hFindFile) == FALSE) {
2705 Py_DECREF(d);
2706 return win32_error("FindClose", namebuf);
2707 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002708
Victor Stinner8c62be82010-05-06 00:08:46 +00002709 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002710
Tim Peters0bb44a42000-09-15 07:44:49 +00002711#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002712
2713#ifndef MAX_PATH
2714#define MAX_PATH CCHMAXPATH
2715#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002716 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002717 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002718 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002719 PyObject *d, *v;
2720 char namebuf[MAX_PATH+5];
2721 HDIR hdir = 1;
2722 ULONG srchcnt = 1;
2723 FILEFINDBUF3 ep;
2724 APIRET rc;
2725
Victor Stinner8c62be82010-05-06 00:08:46 +00002726 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002727 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002728 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002729 name = PyBytes_AsString(oname);
2730 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002731 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002732 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002733 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002734 return NULL;
2735 }
2736 strcpy(namebuf, name);
2737 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002738 if (*pt == ALTSEP)
2739 *pt = SEP;
2740 if (namebuf[len-1] != SEP)
2741 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002742 strcpy(namebuf + len, "*.*");
2743
Neal Norwitz6c913782007-10-14 03:23:09 +00002744 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002745 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002746 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002747 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002748
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002749 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2750 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002751 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002752 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2753 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2754 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002755
2756 if (rc != NO_ERROR) {
2757 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002758 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002759 }
2760
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002761 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002762 do {
2763 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002764 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002765 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002766
2767 strcpy(namebuf, ep.achName);
2768
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002769 /* Leave Case of Name Alone -- In Native Form */
2770 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002771
Christian Heimes72b710a2008-05-26 13:28:38 +00002772 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002773 if (v == NULL) {
2774 Py_DECREF(d);
2775 d = NULL;
2776 break;
2777 }
2778 if (PyList_Append(d, v) != 0) {
2779 Py_DECREF(v);
2780 Py_DECREF(d);
2781 d = NULL;
2782 break;
2783 }
2784 Py_DECREF(v);
2785 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2786 }
2787
Victor Stinnerdcb24032010-04-22 12:08:36 +00002788 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002789 return d;
2790#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002791 PyObject *oname;
2792 char *name;
2793 PyObject *d, *v;
2794 DIR *dirp;
2795 struct dirent *ep;
2796 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002797
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002799 /* v is never read, so it does not need to be initialized yet. */
2800 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002801 arg_is_unicode = 0;
2802 PyErr_Clear();
2803 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002804 oname = NULL;
2805 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002807 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002808 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002809 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002810 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002811 Py_BEGIN_ALLOW_THREADS
2812 dirp = opendir(name);
2813 Py_END_ALLOW_THREADS
2814 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002815 return posix_error_with_allocated_filename(oname);
2816 }
2817 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002818 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002819 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002820 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002821 Py_DECREF(oname);
2822 return NULL;
2823 }
2824 for (;;) {
2825 errno = 0;
2826 Py_BEGIN_ALLOW_THREADS
2827 ep = readdir(dirp);
2828 Py_END_ALLOW_THREADS
2829 if (ep == NULL) {
2830 if (errno == 0) {
2831 break;
2832 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002833 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002835 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 Py_DECREF(d);
2837 return posix_error_with_allocated_filename(oname);
2838 }
2839 }
2840 if (ep->d_name[0] == '.' &&
2841 (NAMLEN(ep) == 1 ||
2842 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2843 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002844 if (arg_is_unicode)
2845 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2846 else
2847 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002848 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002849 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002850 break;
2851 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002852 if (PyList_Append(d, v) != 0) {
2853 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002854 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002855 break;
2856 }
2857 Py_DECREF(v);
2858 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002859 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002861 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002862 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002863
Victor Stinner8c62be82010-05-06 00:08:46 +00002864 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002865
Tim Peters0bb44a42000-09-15 07:44:49 +00002866#endif /* which OS */
2867} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002868
Antoine Pitrou8250e232011-02-25 23:41:16 +00002869#ifdef HAVE_FDOPENDIR
2870PyDoc_STRVAR(posix_fdlistdir__doc__,
2871"fdlistdir(fd) -> list_of_strings\n\n\
2872Like listdir(), but uses a file descriptor instead.\n\
2873After succesful execution of this function, fd will be closed.");
2874
2875static PyObject *
2876posix_fdlistdir(PyObject *self, PyObject *args)
2877{
2878 PyObject *d, *v;
2879 DIR *dirp;
2880 struct dirent *ep;
2881 int fd;
2882
2883 errno = 0;
2884 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2885 return NULL;
2886 Py_BEGIN_ALLOW_THREADS
2887 dirp = fdopendir(fd);
2888 Py_END_ALLOW_THREADS
2889 if (dirp == NULL) {
2890 close(fd);
2891 return posix_error();
2892 }
Charles-François Natali38f425e2012-01-08 19:07:18 +01002893 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00002894 if ((d = PyList_New(0)) == NULL) {
2895 Py_BEGIN_ALLOW_THREADS
2896 closedir(dirp);
2897 Py_END_ALLOW_THREADS
2898 return NULL;
2899 }
2900 for (;;) {
2901 errno = 0;
2902 Py_BEGIN_ALLOW_THREADS
2903 ep = readdir(dirp);
2904 Py_END_ALLOW_THREADS
2905 if (ep == NULL) {
2906 if (errno == 0) {
2907 break;
2908 } else {
2909 Py_BEGIN_ALLOW_THREADS
2910 closedir(dirp);
2911 Py_END_ALLOW_THREADS
2912 Py_DECREF(d);
2913 return posix_error();
2914 }
2915 }
2916 if (ep->d_name[0] == '.' &&
2917 (NAMLEN(ep) == 1 ||
2918 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2919 continue;
2920 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2921 if (v == NULL) {
2922 Py_CLEAR(d);
2923 break;
2924 }
2925 if (PyList_Append(d, v) != 0) {
2926 Py_DECREF(v);
2927 Py_CLEAR(d);
2928 break;
2929 }
2930 Py_DECREF(v);
2931 }
2932 Py_BEGIN_ALLOW_THREADS
2933 closedir(dirp);
2934 Py_END_ALLOW_THREADS
2935
2936 return d;
2937}
2938#endif
2939
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002940#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002941/* A helper function for abspath on win32 */
2942static PyObject *
2943posix__getfullpathname(PyObject *self, PyObject *args)
2944{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002945 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002946 char outbuf[MAX_PATH*2];
2947 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002948 PyObject *po;
2949
2950 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
2951 {
2952 wchar_t *wpath;
2953 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2954 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00002955 DWORD result;
2956 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002957
2958 wpath = PyUnicode_AsUnicode(po);
2959 if (wpath == NULL)
2960 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002961 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02002962 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002963 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02002964 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002965 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00002966 if (!woutbufp)
2967 return PyErr_NoMemory();
2968 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2969 }
2970 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002971 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00002972 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02002973 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002974 if (woutbufp != woutbuf)
2975 free(woutbufp);
2976 return v;
2977 }
2978 /* Drop the argument parsing error as narrow strings
2979 are also valid. */
2980 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02002981
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002982 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
2983 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00002984 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002985 if (win32_warn_bytes_api())
2986 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02002987 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002988 outbuf, &temp)) {
2989 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002990 return NULL;
2991 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002992 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2993 return PyUnicode_Decode(outbuf, strlen(outbuf),
2994 Py_FileSystemDefaultEncoding, NULL);
2995 }
2996 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002997} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002998
Brian Curtind25aef52011-06-13 15:16:04 -05002999
Brian Curtinf5e76d02010-11-24 13:14:05 +00003000
Brian Curtind40e6f72010-07-08 21:39:08 +00003001/* A helper function for samepath on windows */
3002static PyObject *
3003posix__getfinalpathname(PyObject *self, PyObject *args)
3004{
3005 HANDLE hFile;
3006 int buf_size;
3007 wchar_t *target_path;
3008 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003009 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003010 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003011
Victor Stinnereb5657a2011-09-30 01:44:27 +02003012 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003013 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003014 path = PyUnicode_AsUnicode(po);
3015 if (path == NULL)
3016 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003017
3018 if(!check_GetFinalPathNameByHandle()) {
3019 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3020 NotImplementedError. */
3021 return PyErr_Format(PyExc_NotImplementedError,
3022 "GetFinalPathNameByHandle not available on this platform");
3023 }
3024
3025 hFile = CreateFileW(
3026 path,
3027 0, /* desired access */
3028 0, /* share mode */
3029 NULL, /* security attributes */
3030 OPEN_EXISTING,
3031 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3032 FILE_FLAG_BACKUP_SEMANTICS,
3033 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003034
Victor Stinnereb5657a2011-09-30 01:44:27 +02003035 if(hFile == INVALID_HANDLE_VALUE)
3036 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003037
3038 /* We have a good handle to the target, use it to determine the
3039 target path name. */
3040 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3041
3042 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003043 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003044
3045 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3046 if(!target_path)
3047 return PyErr_NoMemory();
3048
3049 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3050 buf_size, VOLUME_NAME_DOS);
3051 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003052 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003053
3054 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003055 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003056
3057 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003058 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003059 free(target_path);
3060 return result;
3061
3062} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003063
3064static PyObject *
3065posix__getfileinformation(PyObject *self, PyObject *args)
3066{
3067 HANDLE hFile;
3068 BY_HANDLE_FILE_INFORMATION info;
3069 int fd;
3070
3071 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3072 return NULL;
3073
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003074 if (!_PyVerify_fd(fd))
3075 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003076
3077 hFile = (HANDLE)_get_osfhandle(fd);
3078 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003079 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003080
3081 if (!GetFileInformationByHandle(hFile, &info))
3082 return win32_error("_getfileinformation", NULL);
3083
3084 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3085 info.nFileIndexHigh,
3086 info.nFileIndexLow);
3087}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003088
Brian Curtin95d028f2011-06-09 09:10:38 -05003089PyDoc_STRVAR(posix__isdir__doc__,
3090"Return true if the pathname refers to an existing directory.");
3091
Brian Curtin9c669cc2011-06-08 18:17:18 -05003092static PyObject *
3093posix__isdir(PyObject *self, PyObject *args)
3094{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003095 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003096 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003097 DWORD attributes;
3098
3099 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003100 wchar_t *wpath = PyUnicode_AsUnicode(po);
3101 if (wpath == NULL)
3102 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003103
3104 attributes = GetFileAttributesW(wpath);
3105 if (attributes == INVALID_FILE_ATTRIBUTES)
3106 Py_RETURN_FALSE;
3107 goto check;
3108 }
3109 /* Drop the argument parsing error as narrow strings
3110 are also valid. */
3111 PyErr_Clear();
3112
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003113 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003114 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003115 if (win32_warn_bytes_api())
3116 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003117 attributes = GetFileAttributesA(path);
3118 if (attributes == INVALID_FILE_ATTRIBUTES)
3119 Py_RETURN_FALSE;
3120
3121check:
3122 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3123 Py_RETURN_TRUE;
3124 else
3125 Py_RETURN_FALSE;
3126}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003127#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003129PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003130"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003131Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003132
Barry Warsaw53699e91996-12-10 23:23:01 +00003133static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003134posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003135{
Victor Stinner8c62be82010-05-06 00:08:46 +00003136 int res;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003137 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003139
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003140#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003141 PyObject *po;
3142 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode))
3143 {
3144 wchar_t *wpath = PyUnicode_AsUnicode(po);
3145 if (wpath == NULL)
3146 return NULL;
3147
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003149 res = CreateDirectoryW(wpath, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003150 Py_END_ALLOW_THREADS
3151 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003152 return win32_error_object("mkdir", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 Py_INCREF(Py_None);
3154 return Py_None;
3155 }
3156 /* Drop the argument parsing error as narrow strings
3157 are also valid. */
3158 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003159 if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003161 if (win32_warn_bytes_api())
3162 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003164 res = CreateDirectoryA(path, NULL);
3165 Py_END_ALLOW_THREADS
3166 if (!res) {
3167 win32_error("mkdir", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003168 return NULL;
3169 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003170 Py_INCREF(Py_None);
3171 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003172#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003173 PyObject *opath;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003174
Victor Stinner8c62be82010-05-06 00:08:46 +00003175 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3176 PyUnicode_FSConverter, &opath, &mode))
3177 return NULL;
3178 path = PyBytes_AsString(opath);
3179 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003180#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003181 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003182#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003183 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003184#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003185 Py_END_ALLOW_THREADS
3186 if (res < 0)
3187 return posix_error_with_allocated_filename(opath);
3188 Py_DECREF(opath);
3189 Py_INCREF(Py_None);
3190 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003191#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003192}
3193
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003194
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003195/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3196#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003197#include <sys/resource.h>
3198#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003199
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003200
3201#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003202PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003203"nice(inc) -> new_priority\n\n\
3204Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003205
Barry Warsaw53699e91996-12-10 23:23:01 +00003206static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003207posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003208{
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003210
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3212 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003213
Victor Stinner8c62be82010-05-06 00:08:46 +00003214 /* There are two flavours of 'nice': one that returns the new
3215 priority (as required by almost all standards out there) and the
3216 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3217 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003218
Victor Stinner8c62be82010-05-06 00:08:46 +00003219 If we are of the nice family that returns the new priority, we
3220 need to clear errno before the call, and check if errno is filled
3221 before calling posix_error() on a returnvalue of -1, because the
3222 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003223
Victor Stinner8c62be82010-05-06 00:08:46 +00003224 errno = 0;
3225 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003226#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003227 if (value == 0)
3228 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003229#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 if (value == -1 && errno != 0)
3231 /* either nice() or getpriority() returned an error */
3232 return posix_error();
3233 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003234}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003235#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003236
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003237
3238#ifdef HAVE_GETPRIORITY
3239PyDoc_STRVAR(posix_getpriority__doc__,
3240"getpriority(which, who) -> current_priority\n\n\
3241Get program scheduling priority.");
3242
3243static PyObject *
3244posix_getpriority(PyObject *self, PyObject *args)
3245{
3246 int which, who, retval;
3247
3248 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3249 return NULL;
3250 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003251 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003252 if (errno != 0)
3253 return posix_error();
3254 return PyLong_FromLong((long)retval);
3255}
3256#endif /* HAVE_GETPRIORITY */
3257
3258
3259#ifdef HAVE_SETPRIORITY
3260PyDoc_STRVAR(posix_setpriority__doc__,
3261"setpriority(which, who, prio) -> None\n\n\
3262Set program scheduling priority.");
3263
3264static PyObject *
3265posix_setpriority(PyObject *self, PyObject *args)
3266{
3267 int which, who, prio, retval;
3268
3269 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3270 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003271 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003272 if (retval == -1)
3273 return posix_error();
3274 Py_RETURN_NONE;
3275}
3276#endif /* HAVE_SETPRIORITY */
3277
3278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003279PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003280"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003281Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003282
Barry Warsaw53699e91996-12-10 23:23:01 +00003283static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003284posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003285{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003286#ifdef MS_WINDOWS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003287 PyObject *src, *dst;
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 BOOL result;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003289 if (PyArg_ParseTuple(args, "UU:rename", &src, &dst))
3290 {
3291 wchar_t *wsrc, *wdst;
3292
3293 wsrc = PyUnicode_AsUnicode(src);
3294 if (wsrc == NULL)
3295 return NULL;
3296 wdst = PyUnicode_AsUnicode(dst);
3297 if (wdst == NULL)
3298 return NULL;
3299 Py_BEGIN_ALLOW_THREADS
3300 result = MoveFileW(wsrc, wdst);
3301 Py_END_ALLOW_THREADS
3302 if (!result)
3303 return win32_error("rename", NULL);
3304 Py_INCREF(Py_None);
3305 return Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003307 else {
3308 PyErr_Clear();
3309 if (!PyArg_ParseTuple(args, "O&O&:rename",
3310 PyUnicode_FSConverter, &src,
3311 PyUnicode_FSConverter, &dst))
3312 return NULL;
3313
3314 if (win32_warn_bytes_api())
3315 goto error;
3316
3317 Py_BEGIN_ALLOW_THREADS
3318 result = MoveFileA(PyBytes_AS_STRING(src),
3319 PyBytes_AS_STRING(dst));
3320 Py_END_ALLOW_THREADS
3321
3322 Py_XDECREF(src);
3323 Py_XDECREF(dst);
3324
3325 if (!result)
3326 return win32_error("rename", NULL);
3327 Py_INCREF(Py_None);
3328 return Py_None;
3329
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003330error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003331 Py_XDECREF(src);
3332 Py_XDECREF(dst);
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003334 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003335#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003336 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003337#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003338}
3339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003341PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003342"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003343Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003344
Barry Warsaw53699e91996-12-10 23:23:01 +00003345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003346posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003347{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003348#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003349 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003350#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003351 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003352#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003353}
3354
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003356PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003357"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003358Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003359
Barry Warsaw53699e91996-12-10 23:23:01 +00003360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003361posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003362{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003363#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003364 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003365#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003367#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003368}
3369
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003370
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003371#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003372PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003373"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003374Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003375
Barry Warsaw53699e91996-12-10 23:23:01 +00003376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003377posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003378{
Victor Stinner8c62be82010-05-06 00:08:46 +00003379 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003380#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003381 wchar_t *command;
3382 if (!PyArg_ParseTuple(args, "u:system", &command))
3383 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003384
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 Py_BEGIN_ALLOW_THREADS
3386 sts = _wsystem(command);
3387 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003388#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003389 PyObject *command_obj;
3390 char *command;
3391 if (!PyArg_ParseTuple(args, "O&:system",
3392 PyUnicode_FSConverter, &command_obj))
3393 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003394
Victor Stinner8c62be82010-05-06 00:08:46 +00003395 command = PyBytes_AsString(command_obj);
3396 Py_BEGIN_ALLOW_THREADS
3397 sts = system(command);
3398 Py_END_ALLOW_THREADS
3399 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003400#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003402}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003403#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003404
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003406PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003407"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003408Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003409
Barry Warsaw53699e91996-12-10 23:23:01 +00003410static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003411posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003412{
Victor Stinner8c62be82010-05-06 00:08:46 +00003413 int i;
3414 if (!PyArg_ParseTuple(args, "i:umask", &i))
3415 return NULL;
3416 i = (int)umask(i);
3417 if (i < 0)
3418 return posix_error();
3419 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003420}
3421
Brian Curtind40e6f72010-07-08 21:39:08 +00003422#ifdef MS_WINDOWS
3423
3424/* override the default DeleteFileW behavior so that directory
3425symlinks can be removed with this function, the same as with
3426Unix symlinks */
3427BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3428{
3429 WIN32_FILE_ATTRIBUTE_DATA info;
3430 WIN32_FIND_DATAW find_data;
3431 HANDLE find_data_handle;
3432 int is_directory = 0;
3433 int is_link = 0;
3434
3435 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3436 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003437
Brian Curtind40e6f72010-07-08 21:39:08 +00003438 /* Get WIN32_FIND_DATA structure for the path to determine if
3439 it is a symlink */
3440 if(is_directory &&
3441 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3442 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3443
3444 if(find_data_handle != INVALID_HANDLE_VALUE) {
3445 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3446 FindClose(find_data_handle);
3447 }
3448 }
3449 }
3450
3451 if (is_directory && is_link)
3452 return RemoveDirectoryW(lpFileName);
3453
3454 return DeleteFileW(lpFileName);
3455}
3456#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003458PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003459"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003460Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003462PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003463"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003464Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003465
Barry Warsaw53699e91996-12-10 23:23:01 +00003466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003467posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003468{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003469#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003470 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3471 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003472#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003474#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003475}
3476
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003477
Guido van Rossumb6775db1994-08-01 11:34:53 +00003478#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003479PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003480"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003481Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003482
Barry Warsaw53699e91996-12-10 23:23:01 +00003483static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003484posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003485{
Victor Stinner8c62be82010-05-06 00:08:46 +00003486 struct utsname u;
3487 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003488
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 Py_BEGIN_ALLOW_THREADS
3490 res = uname(&u);
3491 Py_END_ALLOW_THREADS
3492 if (res < 0)
3493 return posix_error();
3494 return Py_BuildValue("(sssss)",
3495 u.sysname,
3496 u.nodename,
3497 u.release,
3498 u.version,
3499 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003500}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003501#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003502
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003503
3504/*
3505 * Classic POSIX utime functions supported microseconds (1m/sec).
3506 * Newer POSIX functions support nanoseconds (1 billion per sec).
3507 * posixmodule now uses the new functions where possible.
3508 * This improves accuracy in many situations, for example shutil.copy2().
3509 *
3510 * The implementation isn't currently sophisticated enough to handle
3511 * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false.
3512 * Specifically, posix_futimes() would break.
3513 *
3514 * Supporting such a platform wouldn't be impossible; you'd need two
3515 * extract_time() functions, or make its precision a parameter.
3516 * Since such a platform seems unlikely we haven't bothered.
3517 */
3518#if defined(HAVE_UTIMENSAT)
3519#define EXTRACT_TIME_PRECISION (1e9)
3520#if !defined(HAVE_FUTIMENS)
3521#error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment.
3522#endif
3523#else
3524#define EXTRACT_TIME_PRECISION (1e6)
3525#endif
3526
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003527static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003528extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003529{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003530 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003531 if (PyFloat_Check(t)) {
3532 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003533 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003534 if (!intobj)
3535 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003536#if SIZEOF_TIME_T > SIZEOF_LONG
3537 intval = PyLong_AsUnsignedLongLongMask(intobj);
3538#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003539 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003540#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 Py_DECREF(intobj);
3542 if (intval == -1 && PyErr_Occurred())
3543 return -1;
3544 *sec = intval;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003545
3546 *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION);
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 if (*usec < 0)
3548 /* If rounding gave us a negative number,
3549 truncate. */
3550 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003551 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003552 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003553#if SIZEOF_TIME_T > SIZEOF_LONG
3554 intval = PyLong_AsUnsignedLongLongMask(t);
3555#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003556 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003557#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 if (intval == -1 && PyErr_Occurred())
3559 return -1;
3560 *sec = intval;
3561 *usec = 0;
3562 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003563}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003565PyDoc_STRVAR(posix_utime__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003566"utime(path[, (atime, mtime)])\n\
3567Set the access and modified time of the file to the given values.\n\
3568If no second argument is used, set the access and modified times to\n\
3569the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003570
Barry Warsaw53699e91996-12-10 23:23:01 +00003571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003572posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003573{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003574#ifdef MS_WINDOWS
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003575 PyObject *arg = Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003576 PyObject *obwpath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003577 wchar_t *wpath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003578 const char *apath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003579 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003580 time_t atimesec, mtimesec;
3581 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 FILETIME atime, mtime;
3583 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003584
Brian Curtin52fbea12011-11-06 13:41:17 -06003585 if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003586 wpath = PyUnicode_AsUnicode(obwpath);
3587 if (wpath == NULL)
3588 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003589 Py_BEGIN_ALLOW_THREADS
3590 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3591 NULL, OPEN_EXISTING,
3592 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3593 Py_END_ALLOW_THREADS
3594 if (hFile == INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003595 return win32_error_object("utime", obwpath);
3596 }
3597 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 /* Drop the argument parsing error as narrow strings
3599 are also valid. */
3600 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003601
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003602 if (!PyArg_ParseTuple(args, "y|O:utime", &apath, &arg))
3603 return NULL;
3604 if (win32_warn_bytes_api())
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003606
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 Py_BEGIN_ALLOW_THREADS
3608 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3609 NULL, OPEN_EXISTING,
3610 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3611 Py_END_ALLOW_THREADS
3612 if (hFile == INVALID_HANDLE_VALUE) {
3613 win32_error("utime", apath);
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 return NULL;
3615 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003616 }
3617
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003618 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 SYSTEMTIME now;
3620 GetSystemTime(&now);
3621 if (!SystemTimeToFileTime(&now, &mtime) ||
3622 !SystemTimeToFileTime(&now, &atime)) {
3623 win32_error("utime", NULL);
3624 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003625 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003626 }
3627 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3628 PyErr_SetString(PyExc_TypeError,
3629 "utime() arg 2 must be a tuple (atime, mtime)");
3630 goto done;
3631 }
3632 else {
3633 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3634 &atimesec, &ausec) == -1)
3635 goto done;
3636 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3637 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3638 &mtimesec, &musec) == -1)
3639 goto done;
3640 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3641 }
3642 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3643 /* Avoid putting the file name into the error here,
3644 as that may confuse the user into believing that
3645 something is wrong with the file, when it also
3646 could be the time stamp that gives a problem. */
3647 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003648 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003649 }
3650 Py_INCREF(Py_None);
3651 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003652done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 CloseHandle(hFile);
3654 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003655#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003656
Victor Stinner8c62be82010-05-06 00:08:46 +00003657 PyObject *opath;
3658 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003659 time_t atime, mtime;
3660 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003661 int res;
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003662 PyObject* arg = Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003663
Brian Curtin52fbea12011-11-06 13:41:17 -06003664 if (!PyArg_ParseTuple(args, "O&|O:utime",
Victor Stinner8c62be82010-05-06 00:08:46 +00003665 PyUnicode_FSConverter, &opath, &arg))
3666 return NULL;
3667 path = PyBytes_AsString(opath);
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003668 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003669 /* optional time values not given */
3670 Py_BEGIN_ALLOW_THREADS
3671 res = utime(path, NULL);
3672 Py_END_ALLOW_THREADS
3673 }
3674 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3675 PyErr_SetString(PyExc_TypeError,
3676 "utime() arg 2 must be a tuple (atime, mtime)");
3677 Py_DECREF(opath);
3678 return NULL;
3679 }
3680 else {
3681 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3682 &atime, &ausec) == -1) {
3683 Py_DECREF(opath);
3684 return NULL;
3685 }
3686 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3687 &mtime, &musec) == -1) {
3688 Py_DECREF(opath);
3689 return NULL;
3690 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003691
3692 Py_BEGIN_ALLOW_THREADS
3693 {
3694#ifdef HAVE_UTIMENSAT
3695 struct timespec buf[2];
3696 buf[0].tv_sec = atime;
3697 buf[0].tv_nsec = ausec;
3698 buf[1].tv_sec = mtime;
3699 buf[1].tv_nsec = musec;
3700 res = utimensat(AT_FDCWD, path, buf, 0);
3701#elif defined(HAVE_UTIMES)
3702 struct timeval buf[2];
3703 buf[0].tv_sec = atime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 buf[0].tv_usec = ausec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003705 buf[1].tv_sec = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003706 buf[1].tv_usec = musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003708#elif defined(HAVE_UTIME_H)
3709 /* XXX should define struct utimbuf instead, above */
3710 struct utimbuf buf;
3711 buf.actime = atime;
3712 buf.modtime = mtime;
3713 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003714#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003715 time_t buf[2];
3716 buf[0] = atime;
3717 buf[1] = mtime;
3718 res = utime(path, buf);
3719#endif
3720 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003721 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 }
3723 if (res < 0) {
3724 return posix_error_with_allocated_filename(opath);
3725 }
3726 Py_DECREF(opath);
3727 Py_INCREF(Py_None);
3728 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003729#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003730#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003731}
3732
Ross Lagerwall7807c352011-03-17 20:20:30 +02003733#ifdef HAVE_FUTIMES
3734PyDoc_STRVAR(posix_futimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003735"futimes(fd[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003736Set the access and modified time of the file specified by the file\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003737descriptor fd to the given values. If no second argument is used, set the\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003738access and modified times to the current time.");
3739
3740static PyObject *
3741posix_futimes(PyObject *self, PyObject *args)
3742{
3743 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003744 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003745 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003746 long ausec, musec;
3747
Brian Curtinc1b65d12011-11-07 14:18:54 -06003748 if (!PyArg_ParseTuple(args, "i|O:futimes", &fd, &arg))
Ross Lagerwall7807c352011-03-17 20:20:30 +02003749 return NULL;
3750
3751 if (arg == Py_None) {
3752 /* optional time values not given */
3753 Py_BEGIN_ALLOW_THREADS
3754 res = futimes(fd, NULL);
3755 Py_END_ALLOW_THREADS
3756 }
3757 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3758 PyErr_SetString(PyExc_TypeError,
3759 "futimes() arg 2 must be a tuple (atime, mtime)");
3760 return NULL;
3761 }
3762 else {
3763 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003764 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003765 return NULL;
3766 }
3767 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003768 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003769 return NULL;
3770 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003771 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003772 {
3773#ifdef HAVE_FUTIMENS
3774 struct timespec buf[2];
3775 buf[0].tv_sec = atime;
3776 buf[0].tv_nsec = ausec;
3777 buf[1].tv_sec = mtime;
3778 buf[1].tv_nsec = musec;
3779 res = futimens(fd, buf);
3780#else
3781 struct timeval buf[2];
3782 buf[0].tv_sec = atime;
3783 buf[0].tv_usec = ausec;
3784 buf[1].tv_sec = mtime;
3785 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003786 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003787#endif
3788 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003789 Py_END_ALLOW_THREADS
3790 }
3791 if (res < 0)
3792 return posix_error();
3793 Py_RETURN_NONE;
3794}
3795#endif
3796
3797#ifdef HAVE_LUTIMES
3798PyDoc_STRVAR(posix_lutimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003799"lutimes(path[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003800Like utime(), but if path is a symbolic link, it is not dereferenced.");
3801
3802static PyObject *
3803posix_lutimes(PyObject *self, PyObject *args)
3804{
Brian Curtinc1b65d12011-11-07 14:18:54 -06003805 PyObject *opath;
3806 PyObject *arg = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003807 const char *path;
3808 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003809 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003810 long ausec, musec;
3811
Brian Curtinc1b65d12011-11-07 14:18:54 -06003812 if (!PyArg_ParseTuple(args, "O&|O:lutimes",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003813 PyUnicode_FSConverter, &opath, &arg))
3814 return NULL;
3815 path = PyBytes_AsString(opath);
3816 if (arg == Py_None) {
3817 /* optional time values not given */
3818 Py_BEGIN_ALLOW_THREADS
3819 res = lutimes(path, NULL);
3820 Py_END_ALLOW_THREADS
3821 }
3822 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3823 PyErr_SetString(PyExc_TypeError,
3824 "lutimes() arg 2 must be a tuple (atime, mtime)");
3825 Py_DECREF(opath);
3826 return NULL;
3827 }
3828 else {
3829 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003830 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003831 Py_DECREF(opath);
3832 return NULL;
3833 }
3834 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003835 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003836 Py_DECREF(opath);
3837 return NULL;
3838 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003839 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003840 {
3841#ifdef HAVE_UTIMENSAT
3842 struct timespec buf[2];
3843 buf[0].tv_sec = atime;
3844 buf[0].tv_nsec = ausec;
3845 buf[1].tv_sec = mtime;
3846 buf[1].tv_nsec = musec;
3847 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3848#else
3849 struct timeval buf[2];
3850 buf[0].tv_sec = atime;
3851 buf[0].tv_usec = ausec;
3852 buf[1].tv_sec = mtime;
3853 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003854 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003855#endif
3856 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003857 Py_END_ALLOW_THREADS
3858 }
3859 Py_DECREF(opath);
3860 if (res < 0)
3861 return posix_error();
3862 Py_RETURN_NONE;
3863}
3864#endif
3865
3866#ifdef HAVE_FUTIMENS
3867PyDoc_STRVAR(posix_futimens__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003868"futimens(fd[, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003869Updates the timestamps of a file specified by the file descriptor fd, with\n\
3870nanosecond precision.\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003871If no second argument is given, set atime and mtime to the current time.\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003872If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3873current time.\n\
3874If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3875
3876static PyObject *
3877posix_futimens(PyObject *self, PyObject *args)
3878{
3879 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003880 PyObject *atime = Py_None;
3881 PyObject *mtime = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003882 struct timespec buf[2];
3883
Brian Curtinc1b65d12011-11-07 14:18:54 -06003884 if (!PyArg_ParseTuple(args, "i|OO:futimens",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003885 &fd, &atime, &mtime))
3886 return NULL;
3887 if (atime == Py_None && mtime == Py_None) {
3888 /* optional time values not given */
3889 Py_BEGIN_ALLOW_THREADS
3890 res = futimens(fd, NULL);
3891 Py_END_ALLOW_THREADS
3892 }
3893 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3894 PyErr_SetString(PyExc_TypeError,
3895 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3896 return NULL;
3897 }
3898 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3899 PyErr_SetString(PyExc_TypeError,
3900 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3901 return NULL;
3902 }
3903 else {
3904 if (!PyArg_ParseTuple(atime, "ll:futimens",
3905 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3906 return NULL;
3907 }
3908 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3909 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3910 return NULL;
3911 }
3912 Py_BEGIN_ALLOW_THREADS
3913 res = futimens(fd, buf);
3914 Py_END_ALLOW_THREADS
3915 }
3916 if (res < 0)
3917 return posix_error();
3918 Py_RETURN_NONE;
3919}
3920#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003921
Guido van Rossum3b066191991-06-04 19:40:25 +00003922/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003924PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003925"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003926Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003927
Barry Warsaw53699e91996-12-10 23:23:01 +00003928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003929posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003930{
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 int sts;
3932 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3933 return NULL;
3934 _exit(sts);
3935 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003936}
3937
Martin v. Löwis114619e2002-10-07 06:44:21 +00003938#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3939static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003940free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003941{
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 Py_ssize_t i;
3943 for (i = 0; i < count; i++)
3944 PyMem_Free(array[i]);
3945 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003946}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003947
Antoine Pitrou69f71142009-05-24 21:25:49 +00003948static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003949int fsconvert_strdup(PyObject *o, char**out)
3950{
Victor Stinner8c62be82010-05-06 00:08:46 +00003951 PyObject *bytes;
3952 Py_ssize_t size;
3953 if (!PyUnicode_FSConverter(o, &bytes))
3954 return 0;
3955 size = PyBytes_GET_SIZE(bytes);
3956 *out = PyMem_Malloc(size+1);
3957 if (!*out)
3958 return 0;
3959 memcpy(*out, PyBytes_AsString(bytes), size+1);
3960 Py_DECREF(bytes);
3961 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003962}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003963#endif
3964
Ross Lagerwall7807c352011-03-17 20:20:30 +02003965#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003966static char**
3967parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3968{
Victor Stinner8c62be82010-05-06 00:08:46 +00003969 char **envlist;
3970 Py_ssize_t i, pos, envc;
3971 PyObject *keys=NULL, *vals=NULL;
3972 PyObject *key, *val, *key2, *val2;
3973 char *p, *k, *v;
3974 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003975
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 i = PyMapping_Size(env);
3977 if (i < 0)
3978 return NULL;
3979 envlist = PyMem_NEW(char *, i + 1);
3980 if (envlist == NULL) {
3981 PyErr_NoMemory();
3982 return NULL;
3983 }
3984 envc = 0;
3985 keys = PyMapping_Keys(env);
3986 vals = PyMapping_Values(env);
3987 if (!keys || !vals)
3988 goto error;
3989 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3990 PyErr_Format(PyExc_TypeError,
3991 "env.keys() or env.values() is not a list");
3992 goto error;
3993 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003994
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 for (pos = 0; pos < i; pos++) {
3996 key = PyList_GetItem(keys, pos);
3997 val = PyList_GetItem(vals, pos);
3998 if (!key || !val)
3999 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004000
Victor Stinner8c62be82010-05-06 00:08:46 +00004001 if (PyUnicode_FSConverter(key, &key2) == 0)
4002 goto error;
4003 if (PyUnicode_FSConverter(val, &val2) == 0) {
4004 Py_DECREF(key2);
4005 goto error;
4006 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004007
4008#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004009 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
4010 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00004011#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 k = PyBytes_AsString(key2);
4013 v = PyBytes_AsString(val2);
4014 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004015
Victor Stinner8c62be82010-05-06 00:08:46 +00004016 p = PyMem_NEW(char, len);
4017 if (p == NULL) {
4018 PyErr_NoMemory();
4019 Py_DECREF(key2);
4020 Py_DECREF(val2);
4021 goto error;
4022 }
4023 PyOS_snprintf(p, len, "%s=%s", k, v);
4024 envlist[envc++] = p;
4025 Py_DECREF(key2);
4026 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004027#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004028 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004029#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004030 }
4031 Py_DECREF(vals);
4032 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004033
Victor Stinner8c62be82010-05-06 00:08:46 +00004034 envlist[envc] = 0;
4035 *envc_ptr = envc;
4036 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004037
4038error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 Py_XDECREF(keys);
4040 Py_XDECREF(vals);
4041 while (--envc >= 0)
4042 PyMem_DEL(envlist[envc]);
4043 PyMem_DEL(envlist);
4044 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004045}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004046
Ross Lagerwall7807c352011-03-17 20:20:30 +02004047static char**
4048parse_arglist(PyObject* argv, Py_ssize_t *argc)
4049{
4050 int i;
4051 char **argvlist = PyMem_NEW(char *, *argc+1);
4052 if (argvlist == NULL) {
4053 PyErr_NoMemory();
4054 return NULL;
4055 }
4056 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004057 PyObject* item = PySequence_ITEM(argv, i);
4058 if (item == NULL)
4059 goto fail;
4060 if (!fsconvert_strdup(item, &argvlist[i])) {
4061 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004062 goto fail;
4063 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004064 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004065 }
4066 argvlist[*argc] = NULL;
4067 return argvlist;
4068fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004069 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004070 free_string_array(argvlist, *argc);
4071 return NULL;
4072}
4073#endif
4074
4075#ifdef HAVE_EXECV
4076PyDoc_STRVAR(posix_execv__doc__,
4077"execv(path, args)\n\n\
4078Execute an executable path with arguments, replacing current process.\n\
4079\n\
4080 path: path of executable file\n\
4081 args: tuple or list of strings");
4082
4083static PyObject *
4084posix_execv(PyObject *self, PyObject *args)
4085{
4086 PyObject *opath;
4087 char *path;
4088 PyObject *argv;
4089 char **argvlist;
4090 Py_ssize_t argc;
4091
4092 /* execv has two arguments: (path, argv), where
4093 argv is a list or tuple of strings. */
4094
4095 if (!PyArg_ParseTuple(args, "O&O:execv",
4096 PyUnicode_FSConverter,
4097 &opath, &argv))
4098 return NULL;
4099 path = PyBytes_AsString(opath);
4100 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4101 PyErr_SetString(PyExc_TypeError,
4102 "execv() arg 2 must be a tuple or list");
4103 Py_DECREF(opath);
4104 return NULL;
4105 }
4106 argc = PySequence_Size(argv);
4107 if (argc < 1) {
4108 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4109 Py_DECREF(opath);
4110 return NULL;
4111 }
4112
4113 argvlist = parse_arglist(argv, &argc);
4114 if (argvlist == NULL) {
4115 Py_DECREF(opath);
4116 return NULL;
4117 }
4118
4119 execv(path, argvlist);
4120
4121 /* If we get here it's definitely an error */
4122
4123 free_string_array(argvlist, argc);
4124 Py_DECREF(opath);
4125 return posix_error();
4126}
4127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004128PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004129"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004130Execute a path with arguments and environment, replacing current process.\n\
4131\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004132 path: path of executable file\n\
4133 args: tuple or list of arguments\n\
4134 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004135
Barry Warsaw53699e91996-12-10 23:23:01 +00004136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004137posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004138{
Victor Stinner8c62be82010-05-06 00:08:46 +00004139 PyObject *opath;
4140 char *path;
4141 PyObject *argv, *env;
4142 char **argvlist;
4143 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004144 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004145
Victor Stinner8c62be82010-05-06 00:08:46 +00004146 /* execve has three arguments: (path, argv, env), where
4147 argv is a list or tuple of strings and env is a dictionary
4148 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004149
Victor Stinner8c62be82010-05-06 00:08:46 +00004150 if (!PyArg_ParseTuple(args, "O&OO:execve",
4151 PyUnicode_FSConverter,
4152 &opath, &argv, &env))
4153 return NULL;
4154 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004155 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004156 PyErr_SetString(PyExc_TypeError,
4157 "execve() arg 2 must be a tuple or list");
4158 goto fail_0;
4159 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004160 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 if (!PyMapping_Check(env)) {
4162 PyErr_SetString(PyExc_TypeError,
4163 "execve() arg 3 must be a mapping object");
4164 goto fail_0;
4165 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004166
Ross Lagerwall7807c352011-03-17 20:20:30 +02004167 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004168 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004169 goto fail_0;
4170 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004171
Victor Stinner8c62be82010-05-06 00:08:46 +00004172 envlist = parse_envlist(env, &envc);
4173 if (envlist == NULL)
4174 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004175
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004177
Victor Stinner8c62be82010-05-06 00:08:46 +00004178 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004179
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004181
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 while (--envc >= 0)
4183 PyMem_DEL(envlist[envc]);
4184 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004185 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004186 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004187 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 Py_DECREF(opath);
4189 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004190}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004191#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004192
Ross Lagerwall7807c352011-03-17 20:20:30 +02004193#ifdef HAVE_FEXECVE
4194PyDoc_STRVAR(posix_fexecve__doc__,
4195"fexecve(fd, args, env)\n\n\
4196Execute the program specified by a file descriptor with arguments and\n\
4197environment, replacing the current process.\n\
4198\n\
4199 fd: file descriptor of executable\n\
4200 args: tuple or list of arguments\n\
4201 env: dictionary of strings mapping to strings");
4202
4203static PyObject *
4204posix_fexecve(PyObject *self, PyObject *args)
4205{
4206 int fd;
4207 PyObject *argv, *env;
4208 char **argvlist;
4209 char **envlist;
4210 Py_ssize_t argc, envc;
4211
4212 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4213 &fd, &argv, &env))
4214 return NULL;
4215 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4216 PyErr_SetString(PyExc_TypeError,
4217 "fexecve() arg 2 must be a tuple or list");
4218 return NULL;
4219 }
4220 argc = PySequence_Size(argv);
4221 if (!PyMapping_Check(env)) {
4222 PyErr_SetString(PyExc_TypeError,
4223 "fexecve() arg 3 must be a mapping object");
4224 return NULL;
4225 }
4226
4227 argvlist = parse_arglist(argv, &argc);
4228 if (argvlist == NULL)
4229 return NULL;
4230
4231 envlist = parse_envlist(env, &envc);
4232 if (envlist == NULL)
4233 goto fail;
4234
4235 fexecve(fd, argvlist, envlist);
4236
4237 /* If we get here it's definitely an error */
4238
4239 (void) posix_error();
4240
4241 while (--envc >= 0)
4242 PyMem_DEL(envlist[envc]);
4243 PyMem_DEL(envlist);
4244 fail:
4245 free_string_array(argvlist, argc);
4246 return NULL;
4247}
4248#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004249
Guido van Rossuma1065681999-01-25 23:20:23 +00004250#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004251PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004252"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004253Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004254\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004255 mode: mode of process creation\n\
4256 path: path of executable file\n\
4257 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004258
4259static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004260posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004261{
Victor Stinner8c62be82010-05-06 00:08:46 +00004262 PyObject *opath;
4263 char *path;
4264 PyObject *argv;
4265 char **argvlist;
4266 int mode, i;
4267 Py_ssize_t argc;
4268 Py_intptr_t spawnval;
4269 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004270
Victor Stinner8c62be82010-05-06 00:08:46 +00004271 /* spawnv has three arguments: (mode, path, argv), where
4272 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004273
Victor Stinner8c62be82010-05-06 00:08:46 +00004274 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4275 PyUnicode_FSConverter,
4276 &opath, &argv))
4277 return NULL;
4278 path = PyBytes_AsString(opath);
4279 if (PyList_Check(argv)) {
4280 argc = PyList_Size(argv);
4281 getitem = PyList_GetItem;
4282 }
4283 else if (PyTuple_Check(argv)) {
4284 argc = PyTuple_Size(argv);
4285 getitem = PyTuple_GetItem;
4286 }
4287 else {
4288 PyErr_SetString(PyExc_TypeError,
4289 "spawnv() arg 2 must be a tuple or list");
4290 Py_DECREF(opath);
4291 return NULL;
4292 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004293
Victor Stinner8c62be82010-05-06 00:08:46 +00004294 argvlist = PyMem_NEW(char *, argc+1);
4295 if (argvlist == NULL) {
4296 Py_DECREF(opath);
4297 return PyErr_NoMemory();
4298 }
4299 for (i = 0; i < argc; i++) {
4300 if (!fsconvert_strdup((*getitem)(argv, i),
4301 &argvlist[i])) {
4302 free_string_array(argvlist, i);
4303 PyErr_SetString(
4304 PyExc_TypeError,
4305 "spawnv() arg 2 must contain only strings");
4306 Py_DECREF(opath);
4307 return NULL;
4308 }
4309 }
4310 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004311
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004312#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 Py_BEGIN_ALLOW_THREADS
4314 spawnval = spawnv(mode, path, argvlist);
4315 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004316#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004317 if (mode == _OLD_P_OVERLAY)
4318 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004319
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 Py_BEGIN_ALLOW_THREADS
4321 spawnval = _spawnv(mode, path, argvlist);
4322 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004323#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004324
Victor Stinner8c62be82010-05-06 00:08:46 +00004325 free_string_array(argvlist, argc);
4326 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004327
Victor Stinner8c62be82010-05-06 00:08:46 +00004328 if (spawnval == -1)
4329 return posix_error();
4330 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004331#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004332 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004333#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004335#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004336}
4337
4338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004339PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004340"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004341Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004342\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004343 mode: mode of process creation\n\
4344 path: path of executable file\n\
4345 args: tuple or list of arguments\n\
4346 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004347
4348static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004349posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004350{
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 PyObject *opath;
4352 char *path;
4353 PyObject *argv, *env;
4354 char **argvlist;
4355 char **envlist;
4356 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004357 int mode;
4358 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004359 Py_intptr_t spawnval;
4360 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4361 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004362
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 /* spawnve has four arguments: (mode, path, argv, env), where
4364 argv is a list or tuple of strings and env is a dictionary
4365 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004366
Victor Stinner8c62be82010-05-06 00:08:46 +00004367 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4368 PyUnicode_FSConverter,
4369 &opath, &argv, &env))
4370 return NULL;
4371 path = PyBytes_AsString(opath);
4372 if (PyList_Check(argv)) {
4373 argc = PyList_Size(argv);
4374 getitem = PyList_GetItem;
4375 }
4376 else if (PyTuple_Check(argv)) {
4377 argc = PyTuple_Size(argv);
4378 getitem = PyTuple_GetItem;
4379 }
4380 else {
4381 PyErr_SetString(PyExc_TypeError,
4382 "spawnve() arg 2 must be a tuple or list");
4383 goto fail_0;
4384 }
4385 if (!PyMapping_Check(env)) {
4386 PyErr_SetString(PyExc_TypeError,
4387 "spawnve() arg 3 must be a mapping object");
4388 goto fail_0;
4389 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004390
Victor Stinner8c62be82010-05-06 00:08:46 +00004391 argvlist = PyMem_NEW(char *, argc+1);
4392 if (argvlist == NULL) {
4393 PyErr_NoMemory();
4394 goto fail_0;
4395 }
4396 for (i = 0; i < argc; i++) {
4397 if (!fsconvert_strdup((*getitem)(argv, i),
4398 &argvlist[i]))
4399 {
4400 lastarg = i;
4401 goto fail_1;
4402 }
4403 }
4404 lastarg = argc;
4405 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004406
Victor Stinner8c62be82010-05-06 00:08:46 +00004407 envlist = parse_envlist(env, &envc);
4408 if (envlist == NULL)
4409 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004410
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004411#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 Py_BEGIN_ALLOW_THREADS
4413 spawnval = spawnve(mode, path, argvlist, envlist);
4414 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004415#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004416 if (mode == _OLD_P_OVERLAY)
4417 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004418
Victor Stinner8c62be82010-05-06 00:08:46 +00004419 Py_BEGIN_ALLOW_THREADS
4420 spawnval = _spawnve(mode, path, argvlist, envlist);
4421 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004422#endif
Tim Peters25059d32001-12-07 20:35:43 +00004423
Victor Stinner8c62be82010-05-06 00:08:46 +00004424 if (spawnval == -1)
4425 (void) posix_error();
4426 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004427#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004428 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004429#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004430 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004431#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004432
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 while (--envc >= 0)
4434 PyMem_DEL(envlist[envc]);
4435 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004436 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004437 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004438 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004439 Py_DECREF(opath);
4440 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004441}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004442
4443/* OS/2 supports spawnvp & spawnvpe natively */
4444#if defined(PYOS_OS2)
4445PyDoc_STRVAR(posix_spawnvp__doc__,
4446"spawnvp(mode, file, args)\n\n\
4447Execute the program 'file' in a new process, using the environment\n\
4448search path to find the file.\n\
4449\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004450 mode: mode of process creation\n\
4451 file: executable file name\n\
4452 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004453
4454static PyObject *
4455posix_spawnvp(PyObject *self, PyObject *args)
4456{
Victor Stinner8c62be82010-05-06 00:08:46 +00004457 PyObject *opath;
4458 char *path;
4459 PyObject *argv;
4460 char **argvlist;
4461 int mode, i, argc;
4462 Py_intptr_t spawnval;
4463 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004464
Victor Stinner8c62be82010-05-06 00:08:46 +00004465 /* spawnvp has three arguments: (mode, path, argv), where
4466 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4469 PyUnicode_FSConverter,
4470 &opath, &argv))
4471 return NULL;
4472 path = PyBytes_AsString(opath);
4473 if (PyList_Check(argv)) {
4474 argc = PyList_Size(argv);
4475 getitem = PyList_GetItem;
4476 }
4477 else if (PyTuple_Check(argv)) {
4478 argc = PyTuple_Size(argv);
4479 getitem = PyTuple_GetItem;
4480 }
4481 else {
4482 PyErr_SetString(PyExc_TypeError,
4483 "spawnvp() arg 2 must be a tuple or list");
4484 Py_DECREF(opath);
4485 return NULL;
4486 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004487
Victor Stinner8c62be82010-05-06 00:08:46 +00004488 argvlist = PyMem_NEW(char *, argc+1);
4489 if (argvlist == NULL) {
4490 Py_DECREF(opath);
4491 return PyErr_NoMemory();
4492 }
4493 for (i = 0; i < argc; i++) {
4494 if (!fsconvert_strdup((*getitem)(argv, i),
4495 &argvlist[i])) {
4496 free_string_array(argvlist, i);
4497 PyErr_SetString(
4498 PyExc_TypeError,
4499 "spawnvp() arg 2 must contain only strings");
4500 Py_DECREF(opath);
4501 return NULL;
4502 }
4503 }
4504 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004505
Victor Stinner8c62be82010-05-06 00:08:46 +00004506 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004507#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004508 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004509#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004510 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004511#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004512 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004513
Victor Stinner8c62be82010-05-06 00:08:46 +00004514 free_string_array(argvlist, argc);
4515 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004516
Victor Stinner8c62be82010-05-06 00:08:46 +00004517 if (spawnval == -1)
4518 return posix_error();
4519 else
4520 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004521}
4522
4523
4524PyDoc_STRVAR(posix_spawnvpe__doc__,
4525"spawnvpe(mode, file, args, env)\n\n\
4526Execute the program 'file' in a new process, using the environment\n\
4527search path to find the file.\n\
4528\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004529 mode: mode of process creation\n\
4530 file: executable file name\n\
4531 args: tuple or list of arguments\n\
4532 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004533
4534static PyObject *
4535posix_spawnvpe(PyObject *self, PyObject *args)
4536{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004537 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 char *path;
4539 PyObject *argv, *env;
4540 char **argvlist;
4541 char **envlist;
4542 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004543 int mode;
4544 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004545 Py_intptr_t spawnval;
4546 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4547 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004548
Victor Stinner8c62be82010-05-06 00:08:46 +00004549 /* spawnvpe has four arguments: (mode, path, argv, env), where
4550 argv is a list or tuple of strings and env is a dictionary
4551 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004552
Victor Stinner8c62be82010-05-06 00:08:46 +00004553 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4554 PyUnicode_FSConverter,
4555 &opath, &argv, &env))
4556 return NULL;
4557 path = PyBytes_AsString(opath);
4558 if (PyList_Check(argv)) {
4559 argc = PyList_Size(argv);
4560 getitem = PyList_GetItem;
4561 }
4562 else if (PyTuple_Check(argv)) {
4563 argc = PyTuple_Size(argv);
4564 getitem = PyTuple_GetItem;
4565 }
4566 else {
4567 PyErr_SetString(PyExc_TypeError,
4568 "spawnvpe() arg 2 must be a tuple or list");
4569 goto fail_0;
4570 }
4571 if (!PyMapping_Check(env)) {
4572 PyErr_SetString(PyExc_TypeError,
4573 "spawnvpe() arg 3 must be a mapping object");
4574 goto fail_0;
4575 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004576
Victor Stinner8c62be82010-05-06 00:08:46 +00004577 argvlist = PyMem_NEW(char *, argc+1);
4578 if (argvlist == NULL) {
4579 PyErr_NoMemory();
4580 goto fail_0;
4581 }
4582 for (i = 0; i < argc; i++) {
4583 if (!fsconvert_strdup((*getitem)(argv, i),
4584 &argvlist[i]))
4585 {
4586 lastarg = i;
4587 goto fail_1;
4588 }
4589 }
4590 lastarg = argc;
4591 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004592
Victor Stinner8c62be82010-05-06 00:08:46 +00004593 envlist = parse_envlist(env, &envc);
4594 if (envlist == NULL)
4595 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004596
Victor Stinner8c62be82010-05-06 00:08:46 +00004597 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004598#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004599 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004600#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004601 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004602#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004603 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004604
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 if (spawnval == -1)
4606 (void) posix_error();
4607 else
4608 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004609
Victor Stinner8c62be82010-05-06 00:08:46 +00004610 while (--envc >= 0)
4611 PyMem_DEL(envlist[envc]);
4612 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004613 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004614 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004615 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004616 Py_DECREF(opath);
4617 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004618}
4619#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004620#endif /* HAVE_SPAWNV */
4621
4622
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004623#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004624PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004625"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004626Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4627\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004628Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004629
4630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004631posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004632{
Victor Stinner8c62be82010-05-06 00:08:46 +00004633 pid_t pid;
4634 int result = 0;
4635 _PyImport_AcquireLock();
4636 pid = fork1();
4637 if (pid == 0) {
4638 /* child: this clobbers and resets the import lock. */
4639 PyOS_AfterFork();
4640 } else {
4641 /* parent: release the import lock. */
4642 result = _PyImport_ReleaseLock();
4643 }
4644 if (pid == -1)
4645 return posix_error();
4646 if (result < 0) {
4647 /* Don't clobber the OSError if the fork failed. */
4648 PyErr_SetString(PyExc_RuntimeError,
4649 "not holding the import lock");
4650 return NULL;
4651 }
4652 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004653}
4654#endif
4655
4656
Guido van Rossumad0ee831995-03-01 10:34:45 +00004657#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004658PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004659"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004660Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004661Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004662
Barry Warsaw53699e91996-12-10 23:23:01 +00004663static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004664posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004665{
Victor Stinner8c62be82010-05-06 00:08:46 +00004666 pid_t pid;
4667 int result = 0;
4668 _PyImport_AcquireLock();
4669 pid = fork();
4670 if (pid == 0) {
4671 /* child: this clobbers and resets the import lock. */
4672 PyOS_AfterFork();
4673 } else {
4674 /* parent: release the import lock. */
4675 result = _PyImport_ReleaseLock();
4676 }
4677 if (pid == -1)
4678 return posix_error();
4679 if (result < 0) {
4680 /* Don't clobber the OSError if the fork failed. */
4681 PyErr_SetString(PyExc_RuntimeError,
4682 "not holding the import lock");
4683 return NULL;
4684 }
4685 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004686}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004687#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004688
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004689#ifdef HAVE_SCHED_H
4690
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004691#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4692
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004693PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4694"sched_get_priority_max(policy)\n\n\
4695Get the maximum scheduling priority for *policy*.");
4696
4697static PyObject *
4698posix_sched_get_priority_max(PyObject *self, PyObject *args)
4699{
4700 int policy, max;
4701
4702 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4703 return NULL;
4704 max = sched_get_priority_max(policy);
4705 if (max < 0)
4706 return posix_error();
4707 return PyLong_FromLong(max);
4708}
4709
4710PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4711"sched_get_priority_min(policy)\n\n\
4712Get the minimum scheduling priority for *policy*.");
4713
4714static PyObject *
4715posix_sched_get_priority_min(PyObject *self, PyObject *args)
4716{
4717 int policy, min;
4718
4719 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4720 return NULL;
4721 min = sched_get_priority_min(policy);
4722 if (min < 0)
4723 return posix_error();
4724 return PyLong_FromLong(min);
4725}
4726
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004727#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4728
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004729#ifdef HAVE_SCHED_SETSCHEDULER
4730
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004731PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4732"sched_getscheduler(pid)\n\n\
4733Get the scheduling policy for the process with a PID of *pid*.\n\
4734Passing a PID of 0 returns the scheduling policy for the calling process.");
4735
4736static PyObject *
4737posix_sched_getscheduler(PyObject *self, PyObject *args)
4738{
4739 pid_t pid;
4740 int policy;
4741
4742 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4743 return NULL;
4744 policy = sched_getscheduler(pid);
4745 if (policy < 0)
4746 return posix_error();
4747 return PyLong_FromLong(policy);
4748}
4749
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004750#endif
4751
4752#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4753
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004754static PyObject *
4755sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4756{
4757 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004758 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004759
4760 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4761 return NULL;
4762 res = PyStructSequence_New(type);
4763 if (!res)
4764 return NULL;
4765 Py_INCREF(priority);
4766 PyStructSequence_SET_ITEM(res, 0, priority);
4767 return res;
4768}
4769
4770PyDoc_STRVAR(sched_param__doc__,
4771"sched_param(sched_priority): A scheduling parameter.\n\n\
4772Current has only one field: sched_priority");
4773
4774static PyStructSequence_Field sched_param_fields[] = {
4775 {"sched_priority", "the scheduling priority"},
4776 {0}
4777};
4778
4779static PyStructSequence_Desc sched_param_desc = {
4780 "sched_param", /* name */
4781 sched_param__doc__, /* doc */
4782 sched_param_fields,
4783 1
4784};
4785
4786static int
4787convert_sched_param(PyObject *param, struct sched_param *res)
4788{
4789 long priority;
4790
4791 if (Py_TYPE(param) != &SchedParamType) {
4792 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4793 return 0;
4794 }
4795 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4796 if (priority == -1 && PyErr_Occurred())
4797 return 0;
4798 if (priority > INT_MAX || priority < INT_MIN) {
4799 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4800 return 0;
4801 }
4802 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4803 return 1;
4804}
4805
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004806#endif
4807
4808#ifdef HAVE_SCHED_SETSCHEDULER
4809
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004810PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4811"sched_setscheduler(pid, policy, param)\n\n\
4812Set the scheduling policy, *policy*, for *pid*.\n\
4813If *pid* is 0, the calling process is changed.\n\
4814*param* is an instance of sched_param.");
4815
4816static PyObject *
4817posix_sched_setscheduler(PyObject *self, PyObject *args)
4818{
4819 pid_t pid;
4820 int policy;
4821 struct sched_param param;
4822
4823 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4824 &pid, &policy, &convert_sched_param, &param))
4825 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004826
4827 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004828 ** sched_setscheduler() returns 0 in Linux, but the previous
4829 ** scheduling policy under Solaris/Illumos, and others.
4830 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004831 */
4832 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004833 return posix_error();
4834 Py_RETURN_NONE;
4835}
4836
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004837#endif
4838
4839#ifdef HAVE_SCHED_SETPARAM
4840
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004841PyDoc_STRVAR(posix_sched_getparam__doc__,
4842"sched_getparam(pid) -> sched_param\n\n\
4843Returns scheduling parameters for the process with *pid* as an instance of the\n\
4844sched_param class. A PID of 0 means the calling process.");
4845
4846static PyObject *
4847posix_sched_getparam(PyObject *self, PyObject *args)
4848{
4849 pid_t pid;
4850 struct sched_param param;
4851 PyObject *res, *priority;
4852
4853 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4854 return NULL;
4855 if (sched_getparam(pid, &param))
4856 return posix_error();
4857 res = PyStructSequence_New(&SchedParamType);
4858 if (!res)
4859 return NULL;
4860 priority = PyLong_FromLong(param.sched_priority);
4861 if (!priority) {
4862 Py_DECREF(res);
4863 return NULL;
4864 }
4865 PyStructSequence_SET_ITEM(res, 0, priority);
4866 return res;
4867}
4868
4869PyDoc_STRVAR(posix_sched_setparam__doc__,
4870"sched_setparam(pid, param)\n\n\
4871Set scheduling parameters for a process with PID *pid*.\n\
4872A PID of 0 means the calling process.");
4873
4874static PyObject *
4875posix_sched_setparam(PyObject *self, PyObject *args)
4876{
4877 pid_t pid;
4878 struct sched_param param;
4879
4880 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4881 &pid, &convert_sched_param, &param))
4882 return NULL;
4883 if (sched_setparam(pid, &param))
4884 return posix_error();
4885 Py_RETURN_NONE;
4886}
4887
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004888#endif
4889
4890#ifdef HAVE_SCHED_RR_GET_INTERVAL
4891
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004892PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4893"sched_rr_get_interval(pid) -> float\n\n\
4894Return the round-robin quantum for the process with PID *pid* in seconds.");
4895
4896static PyObject *
4897posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4898{
4899 pid_t pid;
4900 struct timespec interval;
4901
4902 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4903 return NULL;
4904 if (sched_rr_get_interval(pid, &interval))
4905 return posix_error();
4906 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4907}
4908
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004909#endif
4910
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004911PyDoc_STRVAR(posix_sched_yield__doc__,
4912"sched_yield()\n\n\
4913Voluntarily relinquish the CPU.");
4914
4915static PyObject *
4916posix_sched_yield(PyObject *self, PyObject *noargs)
4917{
4918 if (sched_yield())
4919 return posix_error();
4920 Py_RETURN_NONE;
4921}
4922
Benjamin Peterson2740af82011-08-02 17:41:34 -05004923#ifdef HAVE_SCHED_SETAFFINITY
4924
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004925typedef struct {
4926 PyObject_HEAD;
4927 Py_ssize_t size;
4928 int ncpus;
4929 cpu_set_t *set;
4930} Py_cpu_set;
4931
4932static PyTypeObject cpu_set_type;
4933
4934static void
4935cpu_set_dealloc(Py_cpu_set *set)
4936{
4937 assert(set->set);
4938 CPU_FREE(set->set);
4939 Py_TYPE(set)->tp_free(set);
4940}
4941
4942static Py_cpu_set *
4943make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4944{
4945 Py_cpu_set *set;
4946
4947 if (size < 0) {
4948 PyErr_SetString(PyExc_ValueError, "negative size");
4949 return NULL;
4950 }
4951 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4952 if (!set)
4953 return NULL;
4954 set->ncpus = size;
4955 set->size = CPU_ALLOC_SIZE(size);
4956 set->set = CPU_ALLOC(size);
4957 if (!set->set) {
4958 type->tp_free(set);
4959 PyErr_NoMemory();
4960 return NULL;
4961 }
4962 CPU_ZERO_S(set->size, set->set);
4963 return set;
4964}
4965
4966static PyObject *
4967cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4968{
4969 int size;
4970
4971 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4972 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4973 return NULL;
4974 return (PyObject *)make_new_cpu_set(type, size);
4975}
4976
4977static PyObject *
4978cpu_set_repr(Py_cpu_set *set)
4979{
4980 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
Victor Stinner63941882011-09-29 00:42:28 +02004981}
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004982
4983static Py_ssize_t
4984cpu_set_len(Py_cpu_set *set)
4985{
4986 return set->ncpus;
4987}
4988
4989static int
4990_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4991{
4992 int cpu;
4993 if (!PyArg_ParseTuple(args, requester, &cpu))
4994 return -1;
4995 if (cpu < 0) {
4996 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4997 return -1;
4998 }
4999 if (cpu >= set->ncpus) {
5000 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
5001 return -1;
5002 }
5003 return cpu;
5004}
5005
5006PyDoc_STRVAR(cpu_set_set_doc,
5007"cpu_set.set(i)\n\n\
5008Add CPU *i* to the set.");
5009
5010static PyObject *
5011cpu_set_set(Py_cpu_set *set, PyObject *args)
5012{
5013 int cpu = _get_cpu(set, "i|set", args);
5014 if (cpu == -1)
5015 return NULL;
5016 CPU_SET_S(cpu, set->size, set->set);
5017 Py_RETURN_NONE;
5018}
5019
5020PyDoc_STRVAR(cpu_set_count_doc,
5021"cpu_set.count() -> int\n\n\
5022Return the number of CPUs active in the set.");
5023
5024static PyObject *
5025cpu_set_count(Py_cpu_set *set, PyObject *noargs)
5026{
5027 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
5028}
5029
5030PyDoc_STRVAR(cpu_set_clear_doc,
5031"cpu_set.clear(i)\n\n\
5032Remove CPU *i* from the set.");
5033
5034static PyObject *
5035cpu_set_clear(Py_cpu_set *set, PyObject *args)
5036{
5037 int cpu = _get_cpu(set, "i|clear", args);
5038 if (cpu == -1)
5039 return NULL;
5040 CPU_CLR_S(cpu, set->size, set->set);
5041 Py_RETURN_NONE;
5042}
5043
5044PyDoc_STRVAR(cpu_set_isset_doc,
5045"cpu_set.isset(i) -> bool\n\n\
5046Test if CPU *i* is in the set.");
5047
5048static PyObject *
5049cpu_set_isset(Py_cpu_set *set, PyObject *args)
5050{
5051 int cpu = _get_cpu(set, "i|isset", args);
5052 if (cpu == -1)
5053 return NULL;
5054 if (CPU_ISSET_S(cpu, set->size, set->set))
5055 Py_RETURN_TRUE;
5056 Py_RETURN_FALSE;
5057}
5058
5059PyDoc_STRVAR(cpu_set_zero_doc,
5060"cpu_set.zero()\n\n\
5061Clear the cpu_set.");
5062
5063static PyObject *
5064cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
5065{
5066 CPU_ZERO_S(set->size, set->set);
5067 Py_RETURN_NONE;
5068}
5069
5070static PyObject *
5071cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
5072{
5073 int eq;
5074
Brian Curtindfc80e32011-08-10 20:28:54 -05005075 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5076 Py_RETURN_NOTIMPLEMENTED;
5077
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005078 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5079 if ((op == Py_EQ) ? eq : !eq)
5080 Py_RETURN_TRUE;
5081 else
5082 Py_RETURN_FALSE;
5083}
5084
5085#define CPU_SET_BINOP(name, op) \
5086 static PyObject * \
5087 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5088 if (res) { \
5089 Py_INCREF(res); \
5090 } \
5091 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005092 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005093 if (!res) \
5094 return NULL; \
5095 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005096 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005097 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005098 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005099 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005100 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005101 op(res->size, res->set, left->set, right->set); \
5102 return (PyObject *)res; \
5103 } \
5104 static PyObject * \
5105 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5106 return do_cpu_set_##name(left, right, NULL); \
5107 } \
5108 static PyObject * \
5109 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5110 return do_cpu_set_##name(left, right, left); \
5111 } \
5112
5113CPU_SET_BINOP(and, CPU_AND_S)
5114CPU_SET_BINOP(or, CPU_OR_S)
5115CPU_SET_BINOP(xor, CPU_XOR_S)
5116#undef CPU_SET_BINOP
5117
5118PyDoc_STRVAR(cpu_set_doc,
5119"cpu_set(size)\n\n\
5120Create an empty mask of CPUs.");
5121
5122static PyNumberMethods cpu_set_as_number = {
5123 0, /*nb_add*/
5124 0, /*nb_subtract*/
5125 0, /*nb_multiply*/
5126 0, /*nb_remainder*/
5127 0, /*nb_divmod*/
5128 0, /*nb_power*/
5129 0, /*nb_negative*/
5130 0, /*nb_positive*/
5131 0, /*nb_absolute*/
5132 0, /*nb_bool*/
5133 0, /*nb_invert*/
5134 0, /*nb_lshift*/
5135 0, /*nb_rshift*/
5136 (binaryfunc)cpu_set_and, /*nb_and*/
5137 (binaryfunc)cpu_set_xor, /*nb_xor*/
5138 (binaryfunc)cpu_set_or, /*nb_or*/
5139 0, /*nb_int*/
5140 0, /*nb_reserved*/
5141 0, /*nb_float*/
5142 0, /*nb_inplace_add*/
5143 0, /*nb_inplace_subtract*/
5144 0, /*nb_inplace_multiply*/
5145 0, /*nb_inplace_remainder*/
5146 0, /*nb_inplace_power*/
5147 0, /*nb_inplace_lshift*/
5148 0, /*nb_inplace_rshift*/
5149 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5150 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5151 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5152};
5153
5154static PySequenceMethods cpu_set_as_sequence = {
5155 (lenfunc)cpu_set_len, /* sq_length */
5156};
5157
5158static PyMethodDef cpu_set_methods[] = {
5159 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5160 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5161 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5162 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5163 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5164 {NULL, NULL} /* sentinel */
5165};
5166
5167static PyTypeObject cpu_set_type = {
5168 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5169 "posix.cpu_set", /* tp_name */
5170 sizeof(Py_cpu_set), /* tp_basicsize */
5171 0, /* tp_itemsize */
5172 /* methods */
5173 (destructor)cpu_set_dealloc, /* tp_dealloc */
5174 0, /* tp_print */
5175 0, /* tp_getattr */
5176 0, /* tp_setattr */
5177 0, /* tp_reserved */
5178 (reprfunc)cpu_set_repr, /* tp_repr */
5179 &cpu_set_as_number, /* tp_as_number */
5180 &cpu_set_as_sequence, /* tp_as_sequence */
5181 0, /* tp_as_mapping */
5182 PyObject_HashNotImplemented, /* tp_hash */
5183 0, /* tp_call */
5184 0, /* tp_str */
5185 PyObject_GenericGetAttr, /* tp_getattro */
5186 0, /* tp_setattro */
5187 0, /* tp_as_buffer */
5188 Py_TPFLAGS_DEFAULT, /* tp_flags */
5189 cpu_set_doc, /* tp_doc */
5190 0, /* tp_traverse */
5191 0, /* tp_clear */
5192 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5193 0, /* tp_weaklistoffset */
5194 0, /* tp_iter */
5195 0, /* tp_iternext */
5196 cpu_set_methods, /* tp_methods */
5197 0, /* tp_members */
5198 0, /* tp_getset */
5199 0, /* tp_base */
5200 0, /* tp_dict */
5201 0, /* tp_descr_get */
5202 0, /* tp_descr_set */
5203 0, /* tp_dictoffset */
5204 0, /* tp_init */
5205 PyType_GenericAlloc, /* tp_alloc */
5206 cpu_set_new, /* tp_new */
5207 PyObject_Del, /* tp_free */
5208};
5209
5210PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5211"sched_setaffinity(pid, cpu_set)\n\n\
5212Set the affinity of the process with PID *pid* to *cpu_set*.");
5213
5214static PyObject *
5215posix_sched_setaffinity(PyObject *self, PyObject *args)
5216{
5217 pid_t pid;
5218 Py_cpu_set *cpu_set;
5219
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005220 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005221 &pid, &cpu_set_type, &cpu_set))
5222 return NULL;
5223 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5224 return posix_error();
5225 Py_RETURN_NONE;
5226}
5227
5228PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5229"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5230Return the affinity of the process with PID *pid*.\n\
5231The returned cpu_set will be of size *ncpus*.");
5232
5233static PyObject *
5234posix_sched_getaffinity(PyObject *self, PyObject *args)
5235{
5236 pid_t pid;
5237 int ncpus;
5238 Py_cpu_set *res;
5239
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005240 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005241 &pid, &ncpus))
5242 return NULL;
5243 res = make_new_cpu_set(&cpu_set_type, ncpus);
5244 if (!res)
5245 return NULL;
5246 if (sched_getaffinity(pid, res->size, res->set)) {
5247 Py_DECREF(res);
5248 return posix_error();
5249 }
5250 return (PyObject *)res;
5251}
5252
Benjamin Peterson2740af82011-08-02 17:41:34 -05005253#endif /* HAVE_SCHED_SETAFFINITY */
5254
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005255#endif /* HAVE_SCHED_H */
5256
Neal Norwitzb59798b2003-03-21 01:43:31 +00005257/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005258/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5259#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005260#define DEV_PTY_FILE "/dev/ptc"
5261#define HAVE_DEV_PTMX
5262#else
5263#define DEV_PTY_FILE "/dev/ptmx"
5264#endif
5265
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005266#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005267#ifdef HAVE_PTY_H
5268#include <pty.h>
5269#else
5270#ifdef HAVE_LIBUTIL_H
5271#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005272#else
5273#ifdef HAVE_UTIL_H
5274#include <util.h>
5275#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005276#endif /* HAVE_LIBUTIL_H */
5277#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005278#ifdef HAVE_STROPTS_H
5279#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005280#endif
5281#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005282
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005283#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005284PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005285"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005286Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005287
5288static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005289posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005290{
Victor Stinner8c62be82010-05-06 00:08:46 +00005291 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005292#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005293 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005294#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005295#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005296 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005297#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005298 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005299#endif
5300#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005301
Thomas Wouters70c21a12000-07-14 14:28:33 +00005302#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005303 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5304 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005305#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005306 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5307 if (slave_name == NULL)
5308 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005309
Victor Stinner8c62be82010-05-06 00:08:46 +00005310 slave_fd = open(slave_name, O_RDWR);
5311 if (slave_fd < 0)
5312 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005313#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005314 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5315 if (master_fd < 0)
5316 return posix_error();
5317 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5318 /* change permission of slave */
5319 if (grantpt(master_fd) < 0) {
5320 PyOS_setsig(SIGCHLD, sig_saved);
5321 return posix_error();
5322 }
5323 /* unlock slave */
5324 if (unlockpt(master_fd) < 0) {
5325 PyOS_setsig(SIGCHLD, sig_saved);
5326 return posix_error();
5327 }
5328 PyOS_setsig(SIGCHLD, sig_saved);
5329 slave_name = ptsname(master_fd); /* get name of slave */
5330 if (slave_name == NULL)
5331 return posix_error();
5332 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5333 if (slave_fd < 0)
5334 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005335#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005336 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5337 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005338#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005339 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005340#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005341#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005342#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005343
Victor Stinner8c62be82010-05-06 00:08:46 +00005344 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005345
Fred Drake8cef4cf2000-06-28 16:40:38 +00005346}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005347#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005348
5349#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005350PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005351"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005352Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5353Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005355
5356static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005357posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005358{
Victor Stinner8c62be82010-05-06 00:08:46 +00005359 int master_fd = -1, result = 0;
5360 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005361
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 _PyImport_AcquireLock();
5363 pid = forkpty(&master_fd, NULL, NULL, NULL);
5364 if (pid == 0) {
5365 /* child: this clobbers and resets the import lock. */
5366 PyOS_AfterFork();
5367 } else {
5368 /* parent: release the import lock. */
5369 result = _PyImport_ReleaseLock();
5370 }
5371 if (pid == -1)
5372 return posix_error();
5373 if (result < 0) {
5374 /* Don't clobber the OSError if the fork failed. */
5375 PyErr_SetString(PyExc_RuntimeError,
5376 "not holding the import lock");
5377 return NULL;
5378 }
5379 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005380}
5381#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005382
Ross Lagerwall7807c352011-03-17 20:20:30 +02005383
Guido van Rossumad0ee831995-03-01 10:34:45 +00005384#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005385PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005386"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005387Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005388
Barry Warsaw53699e91996-12-10 23:23:01 +00005389static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005390posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005391{
Victor Stinner8c62be82010-05-06 00:08:46 +00005392 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005393}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005394#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005395
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005396
Guido van Rossumad0ee831995-03-01 10:34:45 +00005397#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005399"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005400Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005401
Barry Warsaw53699e91996-12-10 23:23:01 +00005402static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005403posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005404{
Victor Stinner8c62be82010-05-06 00:08:46 +00005405 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005406}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005407#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005409
Guido van Rossumad0ee831995-03-01 10:34:45 +00005410#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005412"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005414
Barry Warsaw53699e91996-12-10 23:23:01 +00005415static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005416posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005417{
Victor Stinner8c62be82010-05-06 00:08:46 +00005418 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005419}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005420#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005421
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005423PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005424"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005425Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005426
Barry Warsaw53699e91996-12-10 23:23:01 +00005427static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005428posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005429{
Victor Stinner8c62be82010-05-06 00:08:46 +00005430 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005431}
5432
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005433#ifdef HAVE_GETGROUPLIST
5434PyDoc_STRVAR(posix_getgrouplist__doc__,
5435"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5436Returns a list of groups to which a user belongs.\n\n\
5437 user: username to lookup\n\
5438 group: base group id of the user");
5439
5440static PyObject *
5441posix_getgrouplist(PyObject *self, PyObject *args)
5442{
5443#ifdef NGROUPS_MAX
5444#define MAX_GROUPS NGROUPS_MAX
5445#else
5446 /* defined to be 16 on Solaris7, so this should be a small number */
5447#define MAX_GROUPS 64
5448#endif
5449
5450 const char *user;
5451 int i, ngroups;
5452 PyObject *list;
5453#ifdef __APPLE__
5454 int *groups, basegid;
5455#else
5456 gid_t *groups, basegid;
5457#endif
5458 ngroups = MAX_GROUPS;
5459
5460 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5461 return NULL;
5462
5463#ifdef __APPLE__
5464 groups = PyMem_Malloc(ngroups * sizeof(int));
5465#else
5466 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5467#endif
5468 if (groups == NULL)
5469 return PyErr_NoMemory();
5470
5471 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5472 PyMem_Del(groups);
5473 return posix_error();
5474 }
5475
5476 list = PyList_New(ngroups);
5477 if (list == NULL) {
5478 PyMem_Del(groups);
5479 return NULL;
5480 }
5481
5482 for (i = 0; i < ngroups; i++) {
5483 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5484 if (o == NULL) {
5485 Py_DECREF(list);
5486 PyMem_Del(groups);
5487 return NULL;
5488 }
5489 PyList_SET_ITEM(list, i, o);
5490 }
5491
5492 PyMem_Del(groups);
5493
5494 return list;
5495}
5496#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005497
Fred Drakec9680921999-12-13 16:37:25 +00005498#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005499PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005500"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005502
5503static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005504posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005505{
5506 PyObject *result = NULL;
5507
Fred Drakec9680921999-12-13 16:37:25 +00005508#ifdef NGROUPS_MAX
5509#define MAX_GROUPS NGROUPS_MAX
5510#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005512#define MAX_GROUPS 64
5513#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005514 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005515
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005516 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005517 * This is a helper variable to store the intermediate result when
5518 * that happens.
5519 *
5520 * To keep the code readable the OSX behaviour is unconditional,
5521 * according to the POSIX spec this should be safe on all unix-y
5522 * systems.
5523 */
5524 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005525 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005526
Victor Stinner8c62be82010-05-06 00:08:46 +00005527 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005528 if (n < 0) {
5529 if (errno == EINVAL) {
5530 n = getgroups(0, NULL);
5531 if (n == -1) {
5532 return posix_error();
5533 }
5534 if (n == 0) {
5535 /* Avoid malloc(0) */
5536 alt_grouplist = grouplist;
5537 } else {
5538 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5539 if (alt_grouplist == NULL) {
5540 errno = EINVAL;
5541 return posix_error();
5542 }
5543 n = getgroups(n, alt_grouplist);
5544 if (n == -1) {
5545 PyMem_Free(alt_grouplist);
5546 return posix_error();
5547 }
5548 }
5549 } else {
5550 return posix_error();
5551 }
5552 }
5553 result = PyList_New(n);
5554 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005555 int i;
5556 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005557 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005558 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005559 Py_DECREF(result);
5560 result = NULL;
5561 break;
Fred Drakec9680921999-12-13 16:37:25 +00005562 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005563 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005564 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005565 }
5566
5567 if (alt_grouplist != grouplist) {
5568 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005569 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005570
Fred Drakec9680921999-12-13 16:37:25 +00005571 return result;
5572}
5573#endif
5574
Antoine Pitroub7572f02009-12-02 20:46:48 +00005575#ifdef HAVE_INITGROUPS
5576PyDoc_STRVAR(posix_initgroups__doc__,
5577"initgroups(username, gid) -> None\n\n\
5578Call the system initgroups() to initialize the group access list with all of\n\
5579the groups of which the specified username is a member, plus the specified\n\
5580group id.");
5581
5582static PyObject *
5583posix_initgroups(PyObject *self, PyObject *args)
5584{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005585 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005586 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005587 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005588 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005589
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005590 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5591 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005592 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005593 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005594
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005595 res = initgroups(username, (gid_t) gid);
5596 Py_DECREF(oname);
5597 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005598 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005599
Victor Stinner8c62be82010-05-06 00:08:46 +00005600 Py_INCREF(Py_None);
5601 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005602}
5603#endif
5604
Martin v. Löwis606edc12002-06-13 21:09:11 +00005605#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005606PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005607"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005608Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005609
5610static PyObject *
5611posix_getpgid(PyObject *self, PyObject *args)
5612{
Victor Stinner8c62be82010-05-06 00:08:46 +00005613 pid_t pid, pgid;
5614 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5615 return NULL;
5616 pgid = getpgid(pid);
5617 if (pgid < 0)
5618 return posix_error();
5619 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005620}
5621#endif /* HAVE_GETPGID */
5622
5623
Guido van Rossumb6775db1994-08-01 11:34:53 +00005624#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005625PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005626"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005627Return the current process group id.");
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_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005631{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005632#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005633 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005634#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005636#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005637}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005638#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005640
Guido van Rossumb6775db1994-08-01 11:34:53 +00005641#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005643"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005644Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005645
Barry Warsaw53699e91996-12-10 23:23:01 +00005646static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005647posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005648{
Guido van Rossum64933891994-10-20 21:56:42 +00005649#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005650 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005651#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005653#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005654 return posix_error();
5655 Py_INCREF(Py_None);
5656 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005657}
5658
Guido van Rossumb6775db1994-08-01 11:34:53 +00005659#endif /* HAVE_SETPGRP */
5660
Guido van Rossumad0ee831995-03-01 10:34:45 +00005661#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005662
5663#ifdef MS_WINDOWS
5664#include <tlhelp32.h>
5665
5666static PyObject*
5667win32_getppid()
5668{
5669 HANDLE snapshot;
5670 pid_t mypid;
5671 PyObject* result = NULL;
5672 BOOL have_record;
5673 PROCESSENTRY32 pe;
5674
5675 mypid = getpid(); /* This function never fails */
5676
5677 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5678 if (snapshot == INVALID_HANDLE_VALUE)
5679 return PyErr_SetFromWindowsErr(GetLastError());
5680
5681 pe.dwSize = sizeof(pe);
5682 have_record = Process32First(snapshot, &pe);
5683 while (have_record) {
5684 if (mypid == (pid_t)pe.th32ProcessID) {
5685 /* We could cache the ulong value in a static variable. */
5686 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5687 break;
5688 }
5689
5690 have_record = Process32Next(snapshot, &pe);
5691 }
5692
5693 /* If our loop exits and our pid was not found (result will be NULL)
5694 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5695 * error anyway, so let's raise it. */
5696 if (!result)
5697 result = PyErr_SetFromWindowsErr(GetLastError());
5698
5699 CloseHandle(snapshot);
5700
5701 return result;
5702}
5703#endif /*MS_WINDOWS*/
5704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005706"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005707Return the parent's process id. If the parent process has already exited,\n\
5708Windows machines will still return its id; others systems will return the id\n\
5709of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005710
Barry Warsaw53699e91996-12-10 23:23:01 +00005711static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005712posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005713{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005714#ifdef MS_WINDOWS
5715 return win32_getppid();
5716#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005717 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005718#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005719}
5720#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005721
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005722
Fred Drake12c6e2d1999-12-14 21:25:03 +00005723#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005724PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005725"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005726Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005727
5728static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005729posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005730{
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005732#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005733 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02005734 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005735
5736 if (GetUserNameW(user_name, &num_chars)) {
5737 /* num_chars is the number of unicode chars plus null terminator */
5738 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005739 }
5740 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005741 result = PyErr_SetFromWindowsErr(GetLastError());
5742#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005743 char *name;
5744 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005745
Victor Stinner8c62be82010-05-06 00:08:46 +00005746 errno = 0;
5747 name = getlogin();
5748 if (name == NULL) {
5749 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005750 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005751 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005752 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 }
5754 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005755 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005757#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005758 return result;
5759}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005760#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005761
Guido van Rossumad0ee831995-03-01 10:34:45 +00005762#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005763PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005764"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005765Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005766
Barry Warsaw53699e91996-12-10 23:23:01 +00005767static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005768posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005769{
Victor Stinner8c62be82010-05-06 00:08:46 +00005770 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005771}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005772#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005774
Guido van Rossumad0ee831995-03-01 10:34:45 +00005775#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005777"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005778Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005779
Barry Warsaw53699e91996-12-10 23:23:01 +00005780static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005781posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005782{
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 pid_t pid;
5784 int sig;
5785 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5786 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005787#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005788 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5789 APIRET rc;
5790 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005791 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005792
5793 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5794 APIRET rc;
5795 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005796 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005797
5798 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005799 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005800#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005801 if (kill(pid, sig) == -1)
5802 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005803#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005804 Py_INCREF(Py_None);
5805 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005806}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005807#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005808
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005809#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005810PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005811"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005813
5814static PyObject *
5815posix_killpg(PyObject *self, PyObject *args)
5816{
Victor Stinner8c62be82010-05-06 00:08:46 +00005817 int sig;
5818 pid_t pgid;
5819 /* XXX some man pages make the `pgid` parameter an int, others
5820 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5821 take the same type. Moreover, pid_t is always at least as wide as
5822 int (else compilation of this module fails), which is safe. */
5823 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5824 return NULL;
5825 if (killpg(pgid, sig) == -1)
5826 return posix_error();
5827 Py_INCREF(Py_None);
5828 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005829}
5830#endif
5831
Brian Curtineb24d742010-04-12 17:16:38 +00005832#ifdef MS_WINDOWS
5833PyDoc_STRVAR(win32_kill__doc__,
5834"kill(pid, sig)\n\n\
5835Kill a process with a signal.");
5836
5837static PyObject *
5838win32_kill(PyObject *self, PyObject *args)
5839{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005840 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005841 DWORD pid, sig, err;
5842 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005843
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5845 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005846
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 /* Console processes which share a common console can be sent CTRL+C or
5848 CTRL+BREAK events, provided they handle said events. */
5849 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5850 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5851 err = GetLastError();
5852 PyErr_SetFromWindowsErr(err);
5853 }
5854 else
5855 Py_RETURN_NONE;
5856 }
Brian Curtineb24d742010-04-12 17:16:38 +00005857
Victor Stinner8c62be82010-05-06 00:08:46 +00005858 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5859 attempt to open and terminate the process. */
5860 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5861 if (handle == NULL) {
5862 err = GetLastError();
5863 return PyErr_SetFromWindowsErr(err);
5864 }
Brian Curtineb24d742010-04-12 17:16:38 +00005865
Victor Stinner8c62be82010-05-06 00:08:46 +00005866 if (TerminateProcess(handle, sig) == 0) {
5867 err = GetLastError();
5868 result = PyErr_SetFromWindowsErr(err);
5869 } else {
5870 Py_INCREF(Py_None);
5871 result = Py_None;
5872 }
Brian Curtineb24d742010-04-12 17:16:38 +00005873
Victor Stinner8c62be82010-05-06 00:08:46 +00005874 CloseHandle(handle);
5875 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005876}
5877#endif /* MS_WINDOWS */
5878
Guido van Rossumc0125471996-06-28 18:55:32 +00005879#ifdef HAVE_PLOCK
5880
5881#ifdef HAVE_SYS_LOCK_H
5882#include <sys/lock.h>
5883#endif
5884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005885PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005886"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005887Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005888
Barry Warsaw53699e91996-12-10 23:23:01 +00005889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005890posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005891{
Victor Stinner8c62be82010-05-06 00:08:46 +00005892 int op;
5893 if (!PyArg_ParseTuple(args, "i:plock", &op))
5894 return NULL;
5895 if (plock(op) == -1)
5896 return posix_error();
5897 Py_INCREF(Py_None);
5898 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005899}
5900#endif
5901
Guido van Rossumb6775db1994-08-01 11:34:53 +00005902#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005904"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905Set the current process's user id.");
5906
Barry Warsaw53699e91996-12-10 23:23:01 +00005907static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005908posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005909{
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 long uid_arg;
5911 uid_t uid;
5912 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5913 return NULL;
5914 uid = uid_arg;
5915 if (uid != uid_arg) {
5916 PyErr_SetString(PyExc_OverflowError, "user id too big");
5917 return NULL;
5918 }
5919 if (setuid(uid) < 0)
5920 return posix_error();
5921 Py_INCREF(Py_None);
5922 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005923}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005924#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005925
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005926
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005927#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005928PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005929"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005930Set the current process's effective user id.");
5931
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005932static PyObject *
5933posix_seteuid (PyObject *self, PyObject *args)
5934{
Victor Stinner8c62be82010-05-06 00:08:46 +00005935 long euid_arg;
5936 uid_t euid;
5937 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5938 return NULL;
5939 euid = euid_arg;
5940 if (euid != euid_arg) {
5941 PyErr_SetString(PyExc_OverflowError, "user id too big");
5942 return NULL;
5943 }
5944 if (seteuid(euid) < 0) {
5945 return posix_error();
5946 } else {
5947 Py_INCREF(Py_None);
5948 return Py_None;
5949 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005950}
5951#endif /* HAVE_SETEUID */
5952
5953#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005954PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005955"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005956Set the current process's effective group id.");
5957
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005958static PyObject *
5959posix_setegid (PyObject *self, PyObject *args)
5960{
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 long egid_arg;
5962 gid_t egid;
5963 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5964 return NULL;
5965 egid = egid_arg;
5966 if (egid != egid_arg) {
5967 PyErr_SetString(PyExc_OverflowError, "group id too big");
5968 return NULL;
5969 }
5970 if (setegid(egid) < 0) {
5971 return posix_error();
5972 } else {
5973 Py_INCREF(Py_None);
5974 return Py_None;
5975 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005976}
5977#endif /* HAVE_SETEGID */
5978
5979#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005980PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005981"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005982Set the current process's real and effective user ids.");
5983
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005984static PyObject *
5985posix_setreuid (PyObject *self, PyObject *args)
5986{
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 long ruid_arg, euid_arg;
5988 uid_t ruid, euid;
5989 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5990 return NULL;
5991 if (ruid_arg == -1)
5992 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5993 else
5994 ruid = ruid_arg; /* otherwise, assign from our long */
5995 if (euid_arg == -1)
5996 euid = (uid_t)-1;
5997 else
5998 euid = euid_arg;
5999 if ((euid_arg != -1 && euid != euid_arg) ||
6000 (ruid_arg != -1 && ruid != ruid_arg)) {
6001 PyErr_SetString(PyExc_OverflowError, "user id too big");
6002 return NULL;
6003 }
6004 if (setreuid(ruid, euid) < 0) {
6005 return posix_error();
6006 } else {
6007 Py_INCREF(Py_None);
6008 return Py_None;
6009 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006010}
6011#endif /* HAVE_SETREUID */
6012
6013#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006014PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006015"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006016Set the current process's real and effective group ids.");
6017
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006018static PyObject *
6019posix_setregid (PyObject *self, PyObject *args)
6020{
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 long rgid_arg, egid_arg;
6022 gid_t rgid, egid;
6023 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
6024 return NULL;
6025 if (rgid_arg == -1)
6026 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6027 else
6028 rgid = rgid_arg; /* otherwise, assign from our long */
6029 if (egid_arg == -1)
6030 egid = (gid_t)-1;
6031 else
6032 egid = egid_arg;
6033 if ((egid_arg != -1 && egid != egid_arg) ||
6034 (rgid_arg != -1 && rgid != rgid_arg)) {
6035 PyErr_SetString(PyExc_OverflowError, "group id too big");
6036 return NULL;
6037 }
6038 if (setregid(rgid, egid) < 0) {
6039 return posix_error();
6040 } else {
6041 Py_INCREF(Py_None);
6042 return Py_None;
6043 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006044}
6045#endif /* HAVE_SETREGID */
6046
Guido van Rossumb6775db1994-08-01 11:34:53 +00006047#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006048PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006049"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006051
Barry Warsaw53699e91996-12-10 23:23:01 +00006052static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006053posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006054{
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 long gid_arg;
6056 gid_t gid;
6057 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6058 return NULL;
6059 gid = gid_arg;
6060 if (gid != gid_arg) {
6061 PyErr_SetString(PyExc_OverflowError, "group id too big");
6062 return NULL;
6063 }
6064 if (setgid(gid) < 0)
6065 return posix_error();
6066 Py_INCREF(Py_None);
6067 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006068}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006069#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006070
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006071#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006072PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006073"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006074Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006075
6076static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006077posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006078{
Victor Stinner8c62be82010-05-06 00:08:46 +00006079 int i, len;
6080 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006081
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 if (!PySequence_Check(groups)) {
6083 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6084 return NULL;
6085 }
6086 len = PySequence_Size(groups);
6087 if (len > MAX_GROUPS) {
6088 PyErr_SetString(PyExc_ValueError, "too many groups");
6089 return NULL;
6090 }
6091 for(i = 0; i < len; i++) {
6092 PyObject *elem;
6093 elem = PySequence_GetItem(groups, i);
6094 if (!elem)
6095 return NULL;
6096 if (!PyLong_Check(elem)) {
6097 PyErr_SetString(PyExc_TypeError,
6098 "groups must be integers");
6099 Py_DECREF(elem);
6100 return NULL;
6101 } else {
6102 unsigned long x = PyLong_AsUnsignedLong(elem);
6103 if (PyErr_Occurred()) {
6104 PyErr_SetString(PyExc_TypeError,
6105 "group id too big");
6106 Py_DECREF(elem);
6107 return NULL;
6108 }
6109 grouplist[i] = x;
6110 /* read back the value to see if it fitted in gid_t */
6111 if (grouplist[i] != x) {
6112 PyErr_SetString(PyExc_TypeError,
6113 "group id too big");
6114 Py_DECREF(elem);
6115 return NULL;
6116 }
6117 }
6118 Py_DECREF(elem);
6119 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006120
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 if (setgroups(len, grouplist) < 0)
6122 return posix_error();
6123 Py_INCREF(Py_None);
6124 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006125}
6126#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006127
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006128#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6129static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00006130wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006131{
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 PyObject *result;
6133 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006134 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006135
Victor Stinner8c62be82010-05-06 00:08:46 +00006136 if (pid == -1)
6137 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006138
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 if (struct_rusage == NULL) {
6140 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6141 if (m == NULL)
6142 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006143 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 Py_DECREF(m);
6145 if (struct_rusage == NULL)
6146 return NULL;
6147 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006148
Victor Stinner8c62be82010-05-06 00:08:46 +00006149 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6150 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6151 if (!result)
6152 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006153
6154#ifndef doubletime
6155#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6156#endif
6157
Victor Stinner8c62be82010-05-06 00:08:46 +00006158 PyStructSequence_SET_ITEM(result, 0,
6159 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6160 PyStructSequence_SET_ITEM(result, 1,
6161 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006162#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6164 SET_INT(result, 2, ru->ru_maxrss);
6165 SET_INT(result, 3, ru->ru_ixrss);
6166 SET_INT(result, 4, ru->ru_idrss);
6167 SET_INT(result, 5, ru->ru_isrss);
6168 SET_INT(result, 6, ru->ru_minflt);
6169 SET_INT(result, 7, ru->ru_majflt);
6170 SET_INT(result, 8, ru->ru_nswap);
6171 SET_INT(result, 9, ru->ru_inblock);
6172 SET_INT(result, 10, ru->ru_oublock);
6173 SET_INT(result, 11, ru->ru_msgsnd);
6174 SET_INT(result, 12, ru->ru_msgrcv);
6175 SET_INT(result, 13, ru->ru_nsignals);
6176 SET_INT(result, 14, ru->ru_nvcsw);
6177 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006178#undef SET_INT
6179
Victor Stinner8c62be82010-05-06 00:08:46 +00006180 if (PyErr_Occurred()) {
6181 Py_DECREF(result);
6182 return NULL;
6183 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006186}
6187#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6188
6189#ifdef HAVE_WAIT3
6190PyDoc_STRVAR(posix_wait3__doc__,
6191"wait3(options) -> (pid, status, rusage)\n\n\
6192Wait for completion of a child process.");
6193
6194static PyObject *
6195posix_wait3(PyObject *self, PyObject *args)
6196{
Victor Stinner8c62be82010-05-06 00:08:46 +00006197 pid_t pid;
6198 int options;
6199 struct rusage ru;
6200 WAIT_TYPE status;
6201 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006202
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6204 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006205
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 Py_BEGIN_ALLOW_THREADS
6207 pid = wait3(&status, options, &ru);
6208 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006209
Victor Stinner8c62be82010-05-06 00:08:46 +00006210 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006211}
6212#endif /* HAVE_WAIT3 */
6213
6214#ifdef HAVE_WAIT4
6215PyDoc_STRVAR(posix_wait4__doc__,
6216"wait4(pid, options) -> (pid, status, rusage)\n\n\
6217Wait for completion of a given child process.");
6218
6219static PyObject *
6220posix_wait4(PyObject *self, PyObject *args)
6221{
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 pid_t pid;
6223 int options;
6224 struct rusage ru;
6225 WAIT_TYPE status;
6226 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006227
Victor Stinner8c62be82010-05-06 00:08:46 +00006228 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6229 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006230
Victor Stinner8c62be82010-05-06 00:08:46 +00006231 Py_BEGIN_ALLOW_THREADS
6232 pid = wait4(pid, &status, options, &ru);
6233 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006234
Victor Stinner8c62be82010-05-06 00:08:46 +00006235 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006236}
6237#endif /* HAVE_WAIT4 */
6238
Ross Lagerwall7807c352011-03-17 20:20:30 +02006239#if defined(HAVE_WAITID) && !defined(__APPLE__)
6240PyDoc_STRVAR(posix_waitid__doc__,
6241"waitid(idtype, id, options) -> waitid_result\n\n\
6242Wait for the completion of one or more child processes.\n\n\
6243idtype can be P_PID, P_PGID or P_ALL.\n\
6244id specifies the pid to wait on.\n\
6245options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6246or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6247Returns either waitid_result or None if WNOHANG is specified and there are\n\
6248no children in a waitable state.");
6249
6250static PyObject *
6251posix_waitid(PyObject *self, PyObject *args)
6252{
6253 PyObject *result;
6254 idtype_t idtype;
6255 id_t id;
6256 int options, res;
6257 siginfo_t si;
6258 si.si_pid = 0;
6259 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6260 return NULL;
6261 Py_BEGIN_ALLOW_THREADS
6262 res = waitid(idtype, id, &si, options);
6263 Py_END_ALLOW_THREADS
6264 if (res == -1)
6265 return posix_error();
6266
6267 if (si.si_pid == 0)
6268 Py_RETURN_NONE;
6269
6270 result = PyStructSequence_New(&WaitidResultType);
6271 if (!result)
6272 return NULL;
6273
6274 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6275 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6276 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6277 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6278 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6279 if (PyErr_Occurred()) {
6280 Py_DECREF(result);
6281 return NULL;
6282 }
6283
6284 return result;
6285}
6286#endif
6287
Guido van Rossumb6775db1994-08-01 11:34:53 +00006288#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006289PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006290"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006291Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006292
Barry Warsaw53699e91996-12-10 23:23:01 +00006293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006294posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006295{
Victor Stinner8c62be82010-05-06 00:08:46 +00006296 pid_t pid;
6297 int options;
6298 WAIT_TYPE status;
6299 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006300
Victor Stinner8c62be82010-05-06 00:08:46 +00006301 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6302 return NULL;
6303 Py_BEGIN_ALLOW_THREADS
6304 pid = waitpid(pid, &status, options);
6305 Py_END_ALLOW_THREADS
6306 if (pid == -1)
6307 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006308
Victor Stinner8c62be82010-05-06 00:08:46 +00006309 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006310}
6311
Tim Petersab034fa2002-02-01 11:27:43 +00006312#elif defined(HAVE_CWAIT)
6313
6314/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006315PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006316"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006317"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006318
6319static PyObject *
6320posix_waitpid(PyObject *self, PyObject *args)
6321{
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 Py_intptr_t pid;
6323 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006324
Victor Stinner8c62be82010-05-06 00:08:46 +00006325 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6326 return NULL;
6327 Py_BEGIN_ALLOW_THREADS
6328 pid = _cwait(&status, pid, options);
6329 Py_END_ALLOW_THREADS
6330 if (pid == -1)
6331 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006332
Victor Stinner8c62be82010-05-06 00:08:46 +00006333 /* shift the status left a byte so this is more like the POSIX waitpid */
6334 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006335}
6336#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006337
Guido van Rossumad0ee831995-03-01 10:34:45 +00006338#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006339PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006340"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006341Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006342
Barry Warsaw53699e91996-12-10 23:23:01 +00006343static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006344posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006345{
Victor Stinner8c62be82010-05-06 00:08:46 +00006346 pid_t pid;
6347 WAIT_TYPE status;
6348 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006349
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 Py_BEGIN_ALLOW_THREADS
6351 pid = wait(&status);
6352 Py_END_ALLOW_THREADS
6353 if (pid == -1)
6354 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006355
Victor Stinner8c62be82010-05-06 00:08:46 +00006356 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006357}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006358#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006359
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006361PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006362"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006363Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006364
Barry Warsaw53699e91996-12-10 23:23:01 +00006365static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006366posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006367{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006368#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006369 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006370#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006371#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006372 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006373 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006374#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006375 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006376#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006377#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006378}
6379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006380
Guido van Rossumb6775db1994-08-01 11:34:53 +00006381#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006382PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006383"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006384Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006385
Barry Warsaw53699e91996-12-10 23:23:01 +00006386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006387posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006388{
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 PyObject* v;
6390 char buf[MAXPATHLEN];
6391 PyObject *opath;
6392 char *path;
6393 int n;
6394 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006395
Victor Stinner8c62be82010-05-06 00:08:46 +00006396 if (!PyArg_ParseTuple(args, "O&:readlink",
6397 PyUnicode_FSConverter, &opath))
6398 return NULL;
6399 path = PyBytes_AsString(opath);
6400 v = PySequence_GetItem(args, 0);
6401 if (v == NULL) {
6402 Py_DECREF(opath);
6403 return NULL;
6404 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006405
Victor Stinner8c62be82010-05-06 00:08:46 +00006406 if (PyUnicode_Check(v)) {
6407 arg_is_unicode = 1;
6408 }
6409 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006410
Victor Stinner8c62be82010-05-06 00:08:46 +00006411 Py_BEGIN_ALLOW_THREADS
6412 n = readlink(path, buf, (int) sizeof buf);
6413 Py_END_ALLOW_THREADS
6414 if (n < 0)
6415 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006416
Victor Stinner8c62be82010-05-06 00:08:46 +00006417 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006418 if (arg_is_unicode)
6419 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6420 else
6421 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006422}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006423#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006424
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006425
Brian Curtin52173d42010-12-02 18:29:18 +00006426#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006427PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006428"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006429Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006430
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006431static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006432posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006433{
Victor Stinner8c62be82010-05-06 00:08:46 +00006434 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006435}
6436#endif /* HAVE_SYMLINK */
6437
Brian Curtind40e6f72010-07-08 21:39:08 +00006438#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6439
6440PyDoc_STRVAR(win_readlink__doc__,
6441"readlink(path) -> path\n\n\
6442Return a string representing the path to which the symbolic link points.");
6443
Brian Curtind40e6f72010-07-08 21:39:08 +00006444/* Windows readlink implementation */
6445static PyObject *
6446win_readlink(PyObject *self, PyObject *args)
6447{
6448 wchar_t *path;
6449 DWORD n_bytes_returned;
6450 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006451 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00006452 HANDLE reparse_point_handle;
6453
6454 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6455 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6456 wchar_t *print_name;
6457
6458 if (!PyArg_ParseTuple(args,
Victor Stinnereb5657a2011-09-30 01:44:27 +02006459 "U:readlink",
6460 &po))
6461 return NULL;
6462 path = PyUnicode_AsUnicode(po);
6463 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006464 return NULL;
6465
6466 /* First get a handle to the reparse point */
6467 Py_BEGIN_ALLOW_THREADS
6468 reparse_point_handle = CreateFileW(
6469 path,
6470 0,
6471 0,
6472 0,
6473 OPEN_EXISTING,
6474 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6475 0);
6476 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006477
Brian Curtind40e6f72010-07-08 21:39:08 +00006478 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006479 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006480
Brian Curtind40e6f72010-07-08 21:39:08 +00006481 Py_BEGIN_ALLOW_THREADS
6482 /* New call DeviceIoControl to read the reparse point */
6483 io_result = DeviceIoControl(
6484 reparse_point_handle,
6485 FSCTL_GET_REPARSE_POINT,
6486 0, 0, /* in buffer */
6487 target_buffer, sizeof(target_buffer),
6488 &n_bytes_returned,
6489 0 /* we're not using OVERLAPPED_IO */
6490 );
6491 CloseHandle(reparse_point_handle);
6492 Py_END_ALLOW_THREADS
6493
6494 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006495 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006496
6497 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6498 {
6499 PyErr_SetString(PyExc_ValueError,
6500 "not a symbolic link");
6501 return NULL;
6502 }
Brian Curtin74e45612010-07-09 15:58:59 +00006503 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6504 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6505
6506 result = PyUnicode_FromWideChar(print_name,
6507 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006508 return result;
6509}
6510
6511#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6512
Brian Curtin52173d42010-12-02 18:29:18 +00006513#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006514
6515/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6516static int has_CreateSymbolicLinkW = 0;
6517static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6518static int
6519check_CreateSymbolicLinkW()
6520{
6521 HINSTANCE hKernel32;
6522 /* only recheck */
6523 if (has_CreateSymbolicLinkW)
6524 return has_CreateSymbolicLinkW;
6525 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006526 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6527 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006528 if (Py_CreateSymbolicLinkW)
6529 has_CreateSymbolicLinkW = 1;
6530 return has_CreateSymbolicLinkW;
6531}
6532
6533PyDoc_STRVAR(win_symlink__doc__,
6534"symlink(src, dst, target_is_directory=False)\n\n\
6535Create a symbolic link pointing to src named dst.\n\
6536target_is_directory is required if the target is to be interpreted as\n\
6537a directory.\n\
6538This function requires Windows 6.0 or greater, and raises a\n\
6539NotImplementedError otherwise.");
6540
6541static PyObject *
6542win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6543{
6544 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006545 PyObject *osrc, *odest;
6546 PyObject *usrc = NULL, *udest = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006547 wchar_t *wsrc, *wdest;
Brian Curtind40e6f72010-07-08 21:39:08 +00006548 int target_is_directory = 0;
6549 DWORD res;
6550 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006551
Brian Curtind40e6f72010-07-08 21:39:08 +00006552 if (!check_CreateSymbolicLinkW())
6553 {
6554 /* raise NotImplementedError */
6555 return PyErr_Format(PyExc_NotImplementedError,
6556 "CreateSymbolicLinkW not found");
6557 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006558 if (!PyArg_ParseTupleAndKeywords(
6559 args, kwargs, "OO|i:symlink", kwlist,
6560 &osrc, &odest, &target_is_directory))
Brian Curtind40e6f72010-07-08 21:39:08 +00006561 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006562
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006563 usrc = win32_decode_filename(osrc);
6564 if (!usrc)
6565 return NULL;
6566 udest = win32_decode_filename(odest);
6567 if (!udest)
6568 goto error;
6569
Brian Curtin3b4499c2010-12-28 14:31:47 +00006570 if (win32_can_symlink == 0)
6571 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6572
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006573 wsrc = PyUnicode_AsUnicode(usrc);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006574 if (wsrc == NULL)
6575 goto error;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006576 wdest = PyUnicode_AsUnicode(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006577 if (wsrc == NULL)
6578 goto error;
6579
Brian Curtind40e6f72010-07-08 21:39:08 +00006580 /* if src is a directory, ensure target_is_directory==1 */
6581 if(
6582 GetFileAttributesExW(
Victor Stinnereb5657a2011-09-30 01:44:27 +02006583 wsrc, GetFileExInfoStandard, &src_info
Brian Curtind40e6f72010-07-08 21:39:08 +00006584 ))
6585 {
6586 target_is_directory = target_is_directory ||
6587 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6588 }
6589
6590 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006591 res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
Brian Curtind40e6f72010-07-08 21:39:08 +00006592 Py_END_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006593
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006594 Py_DECREF(usrc);
6595 Py_DECREF(udest);
Brian Curtind40e6f72010-07-08 21:39:08 +00006596 if (!res)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006597 return win32_error_object("symlink", osrc);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006598
Brian Curtind40e6f72010-07-08 21:39:08 +00006599 Py_INCREF(Py_None);
6600 return Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006601
6602error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006603 Py_XDECREF(usrc);
6604 Py_XDECREF(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006605 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006606}
Brian Curtin52173d42010-12-02 18:29:18 +00006607#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006608
6609#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006610#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6611static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006612system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006613{
6614 ULONG value = 0;
6615
6616 Py_BEGIN_ALLOW_THREADS
6617 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6618 Py_END_ALLOW_THREADS
6619
6620 return value;
6621}
6622
6623static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006624posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006625{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006626 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006627 return Py_BuildValue("ddddd",
6628 (double)0 /* t.tms_utime / HZ */,
6629 (double)0 /* t.tms_stime / HZ */,
6630 (double)0 /* t.tms_cutime / HZ */,
6631 (double)0 /* t.tms_cstime / HZ */,
6632 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006633}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006634#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006635#define NEED_TICKS_PER_SECOND
6636static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006637static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006638posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006639{
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 struct tms t;
6641 clock_t c;
6642 errno = 0;
6643 c = times(&t);
6644 if (c == (clock_t) -1)
6645 return posix_error();
6646 return Py_BuildValue("ddddd",
6647 (double)t.tms_utime / ticks_per_second,
6648 (double)t.tms_stime / ticks_per_second,
6649 (double)t.tms_cutime / ticks_per_second,
6650 (double)t.tms_cstime / ticks_per_second,
6651 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006652}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006653#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006654#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006655
6656
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006657#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006658#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006660posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006661{
Victor Stinner8c62be82010-05-06 00:08:46 +00006662 FILETIME create, exit, kernel, user;
6663 HANDLE hProc;
6664 hProc = GetCurrentProcess();
6665 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6666 /* The fields of a FILETIME structure are the hi and lo part
6667 of a 64-bit value expressed in 100 nanosecond units.
6668 1e7 is one second in such units; 1e-7 the inverse.
6669 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6670 */
6671 return Py_BuildValue(
6672 "ddddd",
6673 (double)(user.dwHighDateTime*429.4967296 +
6674 user.dwLowDateTime*1e-7),
6675 (double)(kernel.dwHighDateTime*429.4967296 +
6676 kernel.dwLowDateTime*1e-7),
6677 (double)0,
6678 (double)0,
6679 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006680}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006681#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006682
6683#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006684PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006685"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006686Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006687#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006688
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006689
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006690#ifdef HAVE_GETSID
6691PyDoc_STRVAR(posix_getsid__doc__,
6692"getsid(pid) -> sid\n\n\
6693Call the system call getsid().");
6694
6695static PyObject *
6696posix_getsid(PyObject *self, PyObject *args)
6697{
Victor Stinner8c62be82010-05-06 00:08:46 +00006698 pid_t pid;
6699 int sid;
6700 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6701 return NULL;
6702 sid = getsid(pid);
6703 if (sid < 0)
6704 return posix_error();
6705 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006706}
6707#endif /* HAVE_GETSID */
6708
6709
Guido van Rossumb6775db1994-08-01 11:34:53 +00006710#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006711PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006712"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006713Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006714
Barry Warsaw53699e91996-12-10 23:23:01 +00006715static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006716posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006717{
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 if (setsid() < 0)
6719 return posix_error();
6720 Py_INCREF(Py_None);
6721 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006722}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006723#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006724
Guido van Rossumb6775db1994-08-01 11:34:53 +00006725#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006726PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006727"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006728Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006729
Barry Warsaw53699e91996-12-10 23:23:01 +00006730static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006731posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006732{
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 pid_t pid;
6734 int pgrp;
6735 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6736 return NULL;
6737 if (setpgid(pid, pgrp) < 0)
6738 return posix_error();
6739 Py_INCREF(Py_None);
6740 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006741}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006742#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006743
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006744
Guido van Rossumb6775db1994-08-01 11:34:53 +00006745#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006746PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006747"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006748Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006749
Barry Warsaw53699e91996-12-10 23:23:01 +00006750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006751posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006752{
Victor Stinner8c62be82010-05-06 00:08:46 +00006753 int fd;
6754 pid_t pgid;
6755 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6756 return NULL;
6757 pgid = tcgetpgrp(fd);
6758 if (pgid < 0)
6759 return posix_error();
6760 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006761}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006762#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006763
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006764
Guido van Rossumb6775db1994-08-01 11:34:53 +00006765#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006766PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006767"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006768Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006769
Barry Warsaw53699e91996-12-10 23:23:01 +00006770static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006771posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006772{
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 int fd;
6774 pid_t pgid;
6775 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6776 return NULL;
6777 if (tcsetpgrp(fd, pgid) < 0)
6778 return posix_error();
6779 Py_INCREF(Py_None);
6780 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006781}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006782#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006783
Guido van Rossum687dd131993-05-17 08:34:16 +00006784/* Functions acting on file descriptors */
6785
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006786PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006787"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006788Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006789
Barry Warsaw53699e91996-12-10 23:23:01 +00006790static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006791posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006792{
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 PyObject *ofile;
6794 char *file;
6795 int flag;
6796 int mode = 0777;
6797 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006798
6799#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006800 PyObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006801 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02006802 wchar_t *wpath = PyUnicode_AsUnicode(po);
6803 if (wpath == NULL)
6804 return NULL;
6805
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006807 fd = _wopen(wpath, flag, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 Py_END_ALLOW_THREADS
6809 if (fd < 0)
6810 return posix_error();
6811 return PyLong_FromLong((long)fd);
6812 }
6813 /* Drop the argument parsing error as narrow strings
6814 are also valid. */
6815 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006816#endif
6817
Victor Stinner26de69d2011-06-17 15:15:38 +02006818 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 PyUnicode_FSConverter, &ofile,
6820 &flag, &mode))
6821 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006822#ifdef MS_WINDOWS
6823 if (win32_warn_bytes_api()) {
6824 Py_DECREF(ofile);
6825 return NULL;
6826 }
6827#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 file = PyBytes_AsString(ofile);
6829 Py_BEGIN_ALLOW_THREADS
6830 fd = open(file, flag, mode);
6831 Py_END_ALLOW_THREADS
6832 if (fd < 0)
6833 return posix_error_with_allocated_filename(ofile);
6834 Py_DECREF(ofile);
6835 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006836}
6837
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006839PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006840"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006841Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006842
Barry Warsaw53699e91996-12-10 23:23:01 +00006843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006844posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006845{
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 int fd, res;
6847 if (!PyArg_ParseTuple(args, "i:close", &fd))
6848 return NULL;
6849 if (!_PyVerify_fd(fd))
6850 return posix_error();
6851 Py_BEGIN_ALLOW_THREADS
6852 res = close(fd);
6853 Py_END_ALLOW_THREADS
6854 if (res < 0)
6855 return posix_error();
6856 Py_INCREF(Py_None);
6857 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006858}
6859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006860
Victor Stinner8c62be82010-05-06 00:08:46 +00006861PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006862"closerange(fd_low, fd_high)\n\n\
6863Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6864
6865static PyObject *
6866posix_closerange(PyObject *self, PyObject *args)
6867{
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 int fd_from, fd_to, i;
6869 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6870 return NULL;
6871 Py_BEGIN_ALLOW_THREADS
6872 for (i = fd_from; i < fd_to; i++)
6873 if (_PyVerify_fd(i))
6874 close(i);
6875 Py_END_ALLOW_THREADS
6876 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006877}
6878
6879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006880PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006881"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006882Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006883
Barry Warsaw53699e91996-12-10 23:23:01 +00006884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006885posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006886{
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 int fd;
6888 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6889 return NULL;
6890 if (!_PyVerify_fd(fd))
6891 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006893 if (fd < 0)
6894 return posix_error();
6895 return PyLong_FromLong((long)fd);
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_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006900"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006901Duplicate 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_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006905{
Victor Stinner8c62be82010-05-06 00:08:46 +00006906 int fd, fd2, res;
6907 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6908 return NULL;
6909 if (!_PyVerify_fd_dup2(fd, fd2))
6910 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 if (res < 0)
6913 return posix_error();
6914 Py_INCREF(Py_None);
6915 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006916}
6917
Ross Lagerwall7807c352011-03-17 20:20:30 +02006918#ifdef HAVE_LOCKF
6919PyDoc_STRVAR(posix_lockf__doc__,
6920"lockf(fd, cmd, len)\n\n\
6921Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6922fd is an open file descriptor.\n\
6923cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6924F_TEST.\n\
6925len specifies the section of the file to lock.");
6926
6927static PyObject *
6928posix_lockf(PyObject *self, PyObject *args)
6929{
6930 int fd, cmd, res;
6931 off_t len;
6932 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6933 &fd, &cmd, _parse_off_t, &len))
6934 return NULL;
6935
6936 Py_BEGIN_ALLOW_THREADS
6937 res = lockf(fd, cmd, len);
6938 Py_END_ALLOW_THREADS
6939
6940 if (res < 0)
6941 return posix_error();
6942
6943 Py_RETURN_NONE;
6944}
6945#endif
6946
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006948PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006949"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01006950Set the current position of a file descriptor.\n\
6951Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006952
Barry Warsaw53699e91996-12-10 23:23:01 +00006953static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006954posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006955{
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006957#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006959#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006961#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006962 PyObject *posobj;
6963 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006965#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6967 switch (how) {
6968 case 0: how = SEEK_SET; break;
6969 case 1: how = SEEK_CUR; break;
6970 case 2: how = SEEK_END; break;
6971 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006972#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006973
Ross Lagerwall8e749672011-03-17 21:54:07 +02006974#if !defined(HAVE_LARGEFILE_SUPPORT)
6975 pos = PyLong_AsLong(posobj);
6976#else
6977 pos = PyLong_AsLongLong(posobj);
6978#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 if (PyErr_Occurred())
6980 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006981
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 if (!_PyVerify_fd(fd))
6983 return posix_error();
6984 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006985#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006987#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006989#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 Py_END_ALLOW_THREADS
6991 if (res < 0)
6992 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006993
6994#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006996#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006998#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006999}
7000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007002PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007003"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007004Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007005
Barry Warsaw53699e91996-12-10 23:23:01 +00007006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007007posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007008{
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 int fd, size;
7010 Py_ssize_t n;
7011 PyObject *buffer;
7012 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7013 return NULL;
7014 if (size < 0) {
7015 errno = EINVAL;
7016 return posix_error();
7017 }
7018 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7019 if (buffer == NULL)
7020 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007021 if (!_PyVerify_fd(fd)) {
7022 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007024 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 Py_BEGIN_ALLOW_THREADS
7026 n = read(fd, PyBytes_AS_STRING(buffer), size);
7027 Py_END_ALLOW_THREADS
7028 if (n < 0) {
7029 Py_DECREF(buffer);
7030 return posix_error();
7031 }
7032 if (n != size)
7033 _PyBytes_Resize(&buffer, n);
7034 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007035}
7036
Ross Lagerwall7807c352011-03-17 20:20:30 +02007037#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7038 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007039static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007040iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7041{
7042 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007043 Py_ssize_t blen, total = 0;
7044
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007045 *iov = PyMem_New(struct iovec, cnt);
7046 if (*iov == NULL) {
7047 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007048 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007049 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007050
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007051 *buf = PyMem_New(Py_buffer, cnt);
7052 if (*buf == NULL) {
7053 PyMem_Del(*iov);
7054 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007055 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007056 }
7057
7058 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007059 PyObject *item = PySequence_GetItem(seq, i);
7060 if (item == NULL)
7061 goto fail;
7062 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7063 Py_DECREF(item);
7064 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007065 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007066 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007067 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007068 blen = (*buf)[i].len;
7069 (*iov)[i].iov_len = blen;
7070 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007071 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007072 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007073
7074fail:
7075 PyMem_Del(*iov);
7076 for (j = 0; j < i; j++) {
7077 PyBuffer_Release(&(*buf)[j]);
7078 }
7079 PyMem_Del(*buf);
7080 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007081}
7082
7083static void
7084iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7085{
7086 int i;
7087 PyMem_Del(iov);
7088 for (i = 0; i < cnt; i++) {
7089 PyBuffer_Release(&buf[i]);
7090 }
7091 PyMem_Del(buf);
7092}
7093#endif
7094
Ross Lagerwall7807c352011-03-17 20:20:30 +02007095#ifdef HAVE_READV
7096PyDoc_STRVAR(posix_readv__doc__,
7097"readv(fd, buffers) -> bytesread\n\n\
7098Read from a file descriptor into a number of writable buffers. buffers\n\
7099is an arbitrary sequence of writable buffers.\n\
7100Returns the total number of bytes read.");
7101
7102static PyObject *
7103posix_readv(PyObject *self, PyObject *args)
7104{
7105 int fd, cnt;
7106 Py_ssize_t n;
7107 PyObject *seq;
7108 struct iovec *iov;
7109 Py_buffer *buf;
7110
7111 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7112 return NULL;
7113 if (!PySequence_Check(seq)) {
7114 PyErr_SetString(PyExc_TypeError,
7115 "readv() arg 2 must be a sequence");
7116 return NULL;
7117 }
7118 cnt = PySequence_Size(seq);
7119
7120 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7121 return NULL;
7122
7123 Py_BEGIN_ALLOW_THREADS
7124 n = readv(fd, iov, cnt);
7125 Py_END_ALLOW_THREADS
7126
7127 iov_cleanup(iov, buf, cnt);
7128 return PyLong_FromSsize_t(n);
7129}
7130#endif
7131
7132#ifdef HAVE_PREAD
7133PyDoc_STRVAR(posix_pread__doc__,
7134"pread(fd, buffersize, offset) -> string\n\n\
7135Read from a file descriptor, fd, at a position of offset. It will read up\n\
7136to buffersize number of bytes. The file offset remains unchanged.");
7137
7138static PyObject *
7139posix_pread(PyObject *self, PyObject *args)
7140{
7141 int fd, size;
7142 off_t offset;
7143 Py_ssize_t n;
7144 PyObject *buffer;
7145 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7146 return NULL;
7147
7148 if (size < 0) {
7149 errno = EINVAL;
7150 return posix_error();
7151 }
7152 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7153 if (buffer == NULL)
7154 return NULL;
7155 if (!_PyVerify_fd(fd)) {
7156 Py_DECREF(buffer);
7157 return posix_error();
7158 }
7159 Py_BEGIN_ALLOW_THREADS
7160 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7161 Py_END_ALLOW_THREADS
7162 if (n < 0) {
7163 Py_DECREF(buffer);
7164 return posix_error();
7165 }
7166 if (n != size)
7167 _PyBytes_Resize(&buffer, n);
7168 return buffer;
7169}
7170#endif
7171
7172PyDoc_STRVAR(posix_write__doc__,
7173"write(fd, string) -> byteswritten\n\n\
7174Write a string to a file descriptor.");
7175
7176static PyObject *
7177posix_write(PyObject *self, PyObject *args)
7178{
7179 Py_buffer pbuf;
7180 int fd;
7181 Py_ssize_t size, len;
7182
7183 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7184 return NULL;
7185 if (!_PyVerify_fd(fd)) {
7186 PyBuffer_Release(&pbuf);
7187 return posix_error();
7188 }
7189 len = pbuf.len;
7190 Py_BEGIN_ALLOW_THREADS
7191#if defined(MS_WIN64) || defined(MS_WINDOWS)
7192 if (len > INT_MAX)
7193 len = INT_MAX;
7194 size = write(fd, pbuf.buf, (int)len);
7195#else
7196 size = write(fd, pbuf.buf, len);
7197#endif
7198 Py_END_ALLOW_THREADS
7199 PyBuffer_Release(&pbuf);
7200 if (size < 0)
7201 return posix_error();
7202 return PyLong_FromSsize_t(size);
7203}
7204
7205#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007206PyDoc_STRVAR(posix_sendfile__doc__,
7207"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7208sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7209 -> byteswritten\n\
7210Copy nbytes bytes from file descriptor in to file descriptor out.");
7211
7212static PyObject *
7213posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7214{
7215 int in, out;
7216 Py_ssize_t ret;
7217 off_t offset;
7218
7219#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7220#ifndef __APPLE__
7221 Py_ssize_t len;
7222#endif
7223 PyObject *headers = NULL, *trailers = NULL;
7224 Py_buffer *hbuf, *tbuf;
7225 off_t sbytes;
7226 struct sf_hdtr sf;
7227 int flags = 0;
7228 sf.headers = NULL;
7229 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007230 static char *keywords[] = {"out", "in",
7231 "offset", "count",
7232 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007233
7234#ifdef __APPLE__
7235 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007236 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007237#else
7238 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007239 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007240#endif
7241 &headers, &trailers, &flags))
7242 return NULL;
7243 if (headers != NULL) {
7244 if (!PySequence_Check(headers)) {
7245 PyErr_SetString(PyExc_TypeError,
7246 "sendfile() headers must be a sequence or None");
7247 return NULL;
7248 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007249 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007250 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007251 if (sf.hdr_cnt > 0 &&
7252 !(i = iov_setup(&(sf.headers), &hbuf,
7253 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007254 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007255#ifdef __APPLE__
7256 sbytes += i;
7257#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007258 }
7259 }
7260 if (trailers != NULL) {
7261 if (!PySequence_Check(trailers)) {
7262 PyErr_SetString(PyExc_TypeError,
7263 "sendfile() trailers must be a sequence or None");
7264 return NULL;
7265 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007266 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007267 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007268 if (sf.trl_cnt > 0 &&
7269 !(i = iov_setup(&(sf.trailers), &tbuf,
7270 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007271 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007272#ifdef __APPLE__
7273 sbytes += i;
7274#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007275 }
7276 }
7277
7278 Py_BEGIN_ALLOW_THREADS
7279#ifdef __APPLE__
7280 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7281#else
7282 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7283#endif
7284 Py_END_ALLOW_THREADS
7285
7286 if (sf.headers != NULL)
7287 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7288 if (sf.trailers != NULL)
7289 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7290
7291 if (ret < 0) {
7292 if ((errno == EAGAIN) || (errno == EBUSY)) {
7293 if (sbytes != 0) {
7294 // some data has been sent
7295 goto done;
7296 }
7297 else {
7298 // no data has been sent; upper application is supposed
7299 // to retry on EAGAIN or EBUSY
7300 return posix_error();
7301 }
7302 }
7303 return posix_error();
7304 }
7305 goto done;
7306
7307done:
7308 #if !defined(HAVE_LARGEFILE_SUPPORT)
7309 return Py_BuildValue("l", sbytes);
7310 #else
7311 return Py_BuildValue("L", sbytes);
7312 #endif
7313
7314#else
7315 Py_ssize_t count;
7316 PyObject *offobj;
7317 static char *keywords[] = {"out", "in",
7318 "offset", "count", NULL};
7319 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7320 keywords, &out, &in, &offobj, &count))
7321 return NULL;
7322#ifdef linux
7323 if (offobj == Py_None) {
7324 Py_BEGIN_ALLOW_THREADS
7325 ret = sendfile(out, in, NULL, count);
7326 Py_END_ALLOW_THREADS
7327 if (ret < 0)
7328 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007329 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007330 }
7331#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007332 if (!_parse_off_t(offobj, &offset))
7333 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007334 Py_BEGIN_ALLOW_THREADS
7335 ret = sendfile(out, in, &offset, count);
7336 Py_END_ALLOW_THREADS
7337 if (ret < 0)
7338 return posix_error();
7339 return Py_BuildValue("n", ret);
7340#endif
7341}
7342#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007343
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007344PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007345"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007346Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007347
Barry Warsaw53699e91996-12-10 23:23:01 +00007348static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007349posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007350{
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 int fd;
7352 STRUCT_STAT st;
7353 int res;
7354 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7355 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007356#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 /* on OpenVMS we must ensure that all bytes are written to the file */
7358 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007359#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007360 if (!_PyVerify_fd(fd))
7361 return posix_error();
7362 Py_BEGIN_ALLOW_THREADS
7363 res = FSTAT(fd, &st);
7364 Py_END_ALLOW_THREADS
7365 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007366#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007368#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007370#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 }
Tim Peters5aa91602002-01-30 05:46:57 +00007372
Victor Stinner8c62be82010-05-06 00:08:46 +00007373 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007374}
7375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007376PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007377"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007378Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007379connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007380
7381static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007382posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007383{
Victor Stinner8c62be82010-05-06 00:08:46 +00007384 int fd;
7385 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7386 return NULL;
7387 if (!_PyVerify_fd(fd))
7388 return PyBool_FromLong(0);
7389 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007390}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007391
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007392#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007393PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007394"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007395Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007396
Barry Warsaw53699e91996-12-10 23:23:01 +00007397static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007398posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007399{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007400#if defined(PYOS_OS2)
7401 HFILE read, write;
7402 APIRET rc;
7403
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007404 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007405 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007406 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007407
7408 return Py_BuildValue("(ii)", read, write);
7409#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007410#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 int fds[2];
7412 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 if (res != 0)
7415 return posix_error();
7416 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007417#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007418 HANDLE read, write;
7419 int read_fd, write_fd;
7420 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007421 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007422 if (!ok)
7423 return win32_error("CreatePipe", NULL);
7424 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7425 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7426 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007427#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007428#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007429}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007430#endif /* HAVE_PIPE */
7431
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007432#ifdef HAVE_PIPE2
7433PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007434"pipe2(flags) -> (read_end, write_end)\n\n\
7435Create a pipe with flags set atomically.\n\
7436flags can be constructed by ORing together one or more of these values:\n\
7437O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007438");
7439
7440static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007441posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007442{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007443 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007444 int fds[2];
7445 int res;
7446
Charles-François Natali368f34b2011-06-06 19:49:47 +02007447 flags = PyLong_AsLong(arg);
7448 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007449 return NULL;
7450
7451 res = pipe2(fds, flags);
7452 if (res != 0)
7453 return posix_error();
7454 return Py_BuildValue("(ii)", fds[0], fds[1]);
7455}
7456#endif /* HAVE_PIPE2 */
7457
Ross Lagerwall7807c352011-03-17 20:20:30 +02007458#ifdef HAVE_WRITEV
7459PyDoc_STRVAR(posix_writev__doc__,
7460"writev(fd, buffers) -> byteswritten\n\n\
7461Write the contents of buffers to a file descriptor, where buffers is an\n\
7462arbitrary sequence of buffers.\n\
7463Returns the total bytes written.");
7464
7465static PyObject *
7466posix_writev(PyObject *self, PyObject *args)
7467{
7468 int fd, cnt;
7469 Py_ssize_t res;
7470 PyObject *seq;
7471 struct iovec *iov;
7472 Py_buffer *buf;
7473 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7474 return NULL;
7475 if (!PySequence_Check(seq)) {
7476 PyErr_SetString(PyExc_TypeError,
7477 "writev() arg 2 must be a sequence");
7478 return NULL;
7479 }
7480 cnt = PySequence_Size(seq);
7481
7482 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7483 return NULL;
7484 }
7485
7486 Py_BEGIN_ALLOW_THREADS
7487 res = writev(fd, iov, cnt);
7488 Py_END_ALLOW_THREADS
7489
7490 iov_cleanup(iov, buf, cnt);
7491 return PyLong_FromSsize_t(res);
7492}
7493#endif
7494
7495#ifdef HAVE_PWRITE
7496PyDoc_STRVAR(posix_pwrite__doc__,
7497"pwrite(fd, string, offset) -> byteswritten\n\n\
7498Write string to a file descriptor, fd, from offset, leaving the file\n\
7499offset unchanged.");
7500
7501static PyObject *
7502posix_pwrite(PyObject *self, PyObject *args)
7503{
7504 Py_buffer pbuf;
7505 int fd;
7506 off_t offset;
7507 Py_ssize_t size;
7508
7509 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7510 return NULL;
7511
7512 if (!_PyVerify_fd(fd)) {
7513 PyBuffer_Release(&pbuf);
7514 return posix_error();
7515 }
7516 Py_BEGIN_ALLOW_THREADS
7517 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7518 Py_END_ALLOW_THREADS
7519 PyBuffer_Release(&pbuf);
7520 if (size < 0)
7521 return posix_error();
7522 return PyLong_FromSsize_t(size);
7523}
7524#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007525
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007526#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007527PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007528"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007529Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007530
Barry Warsaw53699e91996-12-10 23:23:01 +00007531static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007532posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007533{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007534 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 char *filename;
7536 int mode = 0666;
7537 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007538 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7539 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007541 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007542 Py_BEGIN_ALLOW_THREADS
7543 res = mkfifo(filename, mode);
7544 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007545 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007546 if (res < 0)
7547 return posix_error();
7548 Py_INCREF(Py_None);
7549 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007550}
7551#endif
7552
7553
Neal Norwitz11690112002-07-30 01:08:28 +00007554#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007555PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007556"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007557Create a filesystem node (file, device special file or named pipe)\n\
7558named filename. mode specifies both the permissions to use and the\n\
7559type of node to be created, being combined (bitwise OR) with one of\n\
7560S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007561device defines the newly created device special file (probably using\n\
7562os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007563
7564
7565static PyObject *
7566posix_mknod(PyObject *self, PyObject *args)
7567{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007568 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 char *filename;
7570 int mode = 0600;
7571 int device = 0;
7572 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007573 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7574 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007576 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 Py_BEGIN_ALLOW_THREADS
7578 res = mknod(filename, mode, device);
7579 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007580 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007581 if (res < 0)
7582 return posix_error();
7583 Py_INCREF(Py_None);
7584 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007585}
7586#endif
7587
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007588#ifdef HAVE_DEVICE_MACROS
7589PyDoc_STRVAR(posix_major__doc__,
7590"major(device) -> major number\n\
7591Extracts a device major number from a raw device number.");
7592
7593static PyObject *
7594posix_major(PyObject *self, PyObject *args)
7595{
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 int device;
7597 if (!PyArg_ParseTuple(args, "i:major", &device))
7598 return NULL;
7599 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007600}
7601
7602PyDoc_STRVAR(posix_minor__doc__,
7603"minor(device) -> minor number\n\
7604Extracts a device minor number from a raw device number.");
7605
7606static PyObject *
7607posix_minor(PyObject *self, PyObject *args)
7608{
Victor Stinner8c62be82010-05-06 00:08:46 +00007609 int device;
7610 if (!PyArg_ParseTuple(args, "i:minor", &device))
7611 return NULL;
7612 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007613}
7614
7615PyDoc_STRVAR(posix_makedev__doc__,
7616"makedev(major, minor) -> device number\n\
7617Composes a raw device number from the major and minor device numbers.");
7618
7619static PyObject *
7620posix_makedev(PyObject *self, PyObject *args)
7621{
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 int major, minor;
7623 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7624 return NULL;
7625 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007626}
7627#endif /* device macros */
7628
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007629
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007630#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007631PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007632"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007633Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007634
Barry Warsaw53699e91996-12-10 23:23:01 +00007635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007636posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007637{
Victor Stinner8c62be82010-05-06 00:08:46 +00007638 int fd;
7639 off_t length;
7640 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007641
Ross Lagerwall7807c352011-03-17 20:20:30 +02007642 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007644
Victor Stinner8c62be82010-05-06 00:08:46 +00007645 Py_BEGIN_ALLOW_THREADS
7646 res = ftruncate(fd, length);
7647 Py_END_ALLOW_THREADS
7648 if (res < 0)
7649 return posix_error();
7650 Py_INCREF(Py_None);
7651 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007652}
7653#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007654
Ross Lagerwall7807c352011-03-17 20:20:30 +02007655#ifdef HAVE_TRUNCATE
7656PyDoc_STRVAR(posix_truncate__doc__,
7657"truncate(path, length)\n\n\
7658Truncate the file given by path to length bytes.");
7659
7660static PyObject *
7661posix_truncate(PyObject *self, PyObject *args)
7662{
7663 PyObject *opath;
7664 const char *path;
7665 off_t length;
7666 int res;
7667
7668 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7669 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7670 return NULL;
7671 path = PyBytes_AsString(opath);
7672
7673 Py_BEGIN_ALLOW_THREADS
7674 res = truncate(path, length);
7675 Py_END_ALLOW_THREADS
7676 Py_DECREF(opath);
7677 if (res < 0)
7678 return posix_error();
7679 Py_RETURN_NONE;
7680}
7681#endif
7682
7683#ifdef HAVE_POSIX_FALLOCATE
7684PyDoc_STRVAR(posix_posix_fallocate__doc__,
7685"posix_fallocate(fd, offset, len)\n\n\
7686Ensures that enough disk space is allocated for the file specified by fd\n\
7687starting from offset and continuing for len bytes.");
7688
7689static PyObject *
7690posix_posix_fallocate(PyObject *self, PyObject *args)
7691{
7692 off_t len, offset;
7693 int res, fd;
7694
7695 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7696 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7697 return NULL;
7698
7699 Py_BEGIN_ALLOW_THREADS
7700 res = posix_fallocate(fd, offset, len);
7701 Py_END_ALLOW_THREADS
7702 if (res != 0) {
7703 errno = res;
7704 return posix_error();
7705 }
7706 Py_RETURN_NONE;
7707}
7708#endif
7709
7710#ifdef HAVE_POSIX_FADVISE
7711PyDoc_STRVAR(posix_posix_fadvise__doc__,
7712"posix_fadvise(fd, offset, len, advice)\n\n\
7713Announces an intention to access data in a specific pattern thus allowing\n\
7714the kernel to make optimizations.\n\
7715The advice applies to the region of the file specified by fd starting at\n\
7716offset and continuing for len bytes.\n\
7717advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7718POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7719POSIX_FADV_DONTNEED.");
7720
7721static PyObject *
7722posix_posix_fadvise(PyObject *self, PyObject *args)
7723{
7724 off_t len, offset;
7725 int res, fd, advice;
7726
7727 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7728 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7729 return NULL;
7730
7731 Py_BEGIN_ALLOW_THREADS
7732 res = posix_fadvise(fd, offset, len, advice);
7733 Py_END_ALLOW_THREADS
7734 if (res != 0) {
7735 errno = res;
7736 return posix_error();
7737 }
7738 Py_RETURN_NONE;
7739}
7740#endif
7741
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007742#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007743PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007744"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007745Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007746
Fred Drake762e2061999-08-26 17:23:54 +00007747/* Save putenv() parameters as values here, so we can collect them when they
7748 * get re-set with another call for the same key. */
7749static PyObject *posix_putenv_garbage;
7750
Tim Peters5aa91602002-01-30 05:46:57 +00007751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007752posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007753{
Victor Stinner84ae1182010-05-06 22:05:07 +00007754 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007755#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01007756 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007757 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01007758
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007760 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01007761 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00007762 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007763
Victor Stinner65170952011-11-22 22:16:17 +01007764 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00007765 if (newstr == NULL) {
7766 PyErr_NoMemory();
7767 goto error;
7768 }
Victor Stinner65170952011-11-22 22:16:17 +01007769 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
7770 PyErr_Format(PyExc_ValueError,
7771 "the environment variable is longer than %u characters",
7772 _MAX_ENV);
7773 goto error;
7774 }
7775
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02007777 if (newenv == NULL)
7778 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007781 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007783#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007784 PyObject *os1, *os2;
7785 char *s1, *s2;
7786 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007787
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007788 if (!PyArg_ParseTuple(args,
7789 "O&O&:putenv",
7790 PyUnicode_FSConverter, &os1,
7791 PyUnicode_FSConverter, &os2))
7792 return NULL;
7793 s1 = PyBytes_AsString(os1);
7794 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00007795
Victor Stinner65170952011-11-22 22:16:17 +01007796 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007797 if (newstr == NULL) {
7798 PyErr_NoMemory();
7799 goto error;
7800 }
7801
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007805 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007807#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007808
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 /* Install the first arg and newstr in posix_putenv_garbage;
7810 * this will cause previous value to be collected. This has to
7811 * happen after the real putenv() call because the old value
7812 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01007813 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 /* really not much we can do; just leak */
7815 PyErr_Clear();
7816 }
7817 else {
7818 Py_DECREF(newstr);
7819 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007820
Martin v. Löwis011e8422009-05-05 04:43:17 +00007821#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 Py_DECREF(os1);
7823 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007824#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007825 Py_RETURN_NONE;
7826
7827error:
7828#ifndef MS_WINDOWS
7829 Py_DECREF(os1);
7830 Py_DECREF(os2);
7831#endif
7832 Py_XDECREF(newstr);
7833 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00007834}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007835#endif /* putenv */
7836
Guido van Rossumc524d952001-10-19 01:31:59 +00007837#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007838PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007839"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007840Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007841
7842static PyObject *
7843posix_unsetenv(PyObject *self, PyObject *args)
7844{
Victor Stinner65170952011-11-22 22:16:17 +01007845 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01007846#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01007847 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01007848#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007849
7850 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00007851
Victor Stinner65170952011-11-22 22:16:17 +01007852 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00007853 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007854
Victor Stinner984890f2011-11-24 13:53:38 +01007855#ifdef HAVE_BROKEN_UNSETENV
7856 unsetenv(PyBytes_AS_STRING(name));
7857#else
Victor Stinner65170952011-11-22 22:16:17 +01007858 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007859 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06007860 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01007861 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007862 }
Victor Stinner984890f2011-11-24 13:53:38 +01007863#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007864
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 /* Remove the key from posix_putenv_garbage;
7866 * this will cause it to be collected. This has to
7867 * happen after the real unsetenv() call because the
7868 * old value was still accessible until then.
7869 */
Victor Stinner65170952011-11-22 22:16:17 +01007870 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 /* really not much we can do; just leak */
7872 PyErr_Clear();
7873 }
Victor Stinner65170952011-11-22 22:16:17 +01007874 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00007875 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007876}
7877#endif /* unsetenv */
7878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007879PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007880"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007881Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007882
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007884posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007885{
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 int code;
7887 char *message;
7888 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7889 return NULL;
7890 message = strerror(code);
7891 if (message == NULL) {
7892 PyErr_SetString(PyExc_ValueError,
7893 "strerror() argument out of range");
7894 return NULL;
7895 }
Victor Stinner1b579672011-12-17 05:47:23 +01007896 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007897}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007898
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007899
Guido van Rossumc9641791998-08-04 15:26:23 +00007900#ifdef HAVE_SYS_WAIT_H
7901
Fred Drake106c1a02002-04-23 15:58:02 +00007902#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007903PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007904"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007905Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007906
7907static PyObject *
7908posix_WCOREDUMP(PyObject *self, PyObject *args)
7909{
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 WAIT_TYPE status;
7911 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007912
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7914 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007915
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007917}
7918#endif /* WCOREDUMP */
7919
7920#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007921PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007922"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007923Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007924job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007925
7926static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007927posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007928{
Victor Stinner8c62be82010-05-06 00:08:46 +00007929 WAIT_TYPE status;
7930 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007931
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7933 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007934
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007936}
7937#endif /* WIFCONTINUED */
7938
Guido van Rossumc9641791998-08-04 15:26:23 +00007939#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007940PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007941"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007942Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007943
7944static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007945posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007946{
Victor Stinner8c62be82010-05-06 00:08:46 +00007947 WAIT_TYPE status;
7948 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007949
Victor Stinner8c62be82010-05-06 00:08:46 +00007950 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7951 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007952
Victor Stinner8c62be82010-05-06 00:08:46 +00007953 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007954}
7955#endif /* WIFSTOPPED */
7956
7957#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007958PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007959"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007960Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007961
7962static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007963posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007964{
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 WAIT_TYPE status;
7966 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007967
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7969 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007970
Victor Stinner8c62be82010-05-06 00:08:46 +00007971 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007972}
7973#endif /* WIFSIGNALED */
7974
7975#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007976PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007977"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007978Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007979system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007980
7981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007982posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007983{
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 WAIT_TYPE status;
7985 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007986
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7988 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007989
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007991}
7992#endif /* WIFEXITED */
7993
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007994#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007995PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007996"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007997Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007998
7999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008000posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008001{
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 WAIT_TYPE status;
8003 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008004
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8006 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008007
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008009}
8010#endif /* WEXITSTATUS */
8011
8012#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008013PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008014"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008015Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008016value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008017
8018static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008019posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008020{
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 WAIT_TYPE status;
8022 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008023
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8025 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008026
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008028}
8029#endif /* WTERMSIG */
8030
8031#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008032PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008033"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008034Return the signal that stopped the process that provided\n\
8035the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008036
8037static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008038posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008039{
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 WAIT_TYPE status;
8041 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008042
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8044 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008045
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008047}
8048#endif /* WSTOPSIG */
8049
8050#endif /* HAVE_SYS_WAIT_H */
8051
8052
Thomas Wouters477c8d52006-05-27 19:21:47 +00008053#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008054#ifdef _SCO_DS
8055/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8056 needed definitions in sys/statvfs.h */
8057#define _SVID3
8058#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008059#include <sys/statvfs.h>
8060
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008061static PyObject*
8062_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8064 if (v == NULL)
8065 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008066
8067#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8069 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8070 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8071 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8072 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8073 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8074 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8075 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8076 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8077 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008078#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8080 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8081 PyStructSequence_SET_ITEM(v, 2,
8082 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8083 PyStructSequence_SET_ITEM(v, 3,
8084 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8085 PyStructSequence_SET_ITEM(v, 4,
8086 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8087 PyStructSequence_SET_ITEM(v, 5,
8088 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8089 PyStructSequence_SET_ITEM(v, 6,
8090 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8091 PyStructSequence_SET_ITEM(v, 7,
8092 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8093 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8094 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008095#endif
8096
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008098}
8099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008100PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008101"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008102Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008103
8104static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008105posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008106{
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 int fd, res;
8108 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008109
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8111 return NULL;
8112 Py_BEGIN_ALLOW_THREADS
8113 res = fstatvfs(fd, &st);
8114 Py_END_ALLOW_THREADS
8115 if (res != 0)
8116 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008117
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008119}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008120#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008121
8122
Thomas Wouters477c8d52006-05-27 19:21:47 +00008123#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008124#include <sys/statvfs.h>
8125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008126PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008127"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008128Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008129
8130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008131posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008132{
Victor Stinner6fa67772011-09-20 04:04:33 +02008133 PyObject *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008134 int res;
8135 struct statvfs st;
Victor Stinner6fa67772011-09-20 04:04:33 +02008136 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00008137 return NULL;
8138 Py_BEGIN_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008139 res = statvfs(PyBytes_AS_STRING(path), &st);
Victor Stinner8c62be82010-05-06 00:08:46 +00008140 Py_END_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008141 if (res != 0) {
8142 posix_error_with_filename(PyBytes_AS_STRING(path));
8143 Py_DECREF(path);
8144 return NULL;
8145 }
8146 Py_DECREF(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008147
Victor Stinner8c62be82010-05-06 00:08:46 +00008148 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008149}
8150#endif /* HAVE_STATVFS */
8151
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008152#ifdef MS_WINDOWS
8153PyDoc_STRVAR(win32__getdiskusage__doc__,
8154"_getdiskusage(path) -> (total, free)\n\n\
8155Return disk usage statistics about the given path as (total, free) tuple.");
8156
8157static PyObject *
8158win32__getdiskusage(PyObject *self, PyObject *args)
8159{
8160 BOOL retval;
8161 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008162 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008163
Victor Stinner6139c1b2011-11-09 22:14:14 +01008164 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008165 return NULL;
8166
8167 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008168 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008169 Py_END_ALLOW_THREADS
8170 if (retval == 0)
8171 return PyErr_SetFromWindowsErr(0);
8172
8173 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8174}
8175#endif
8176
8177
Fred Drakec9680921999-12-13 16:37:25 +00008178/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8179 * It maps strings representing configuration variable names to
8180 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008181 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008182 * rarely-used constants. There are three separate tables that use
8183 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008184 *
8185 * This code is always included, even if none of the interfaces that
8186 * need it are included. The #if hackery needed to avoid it would be
8187 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008188 */
8189struct constdef {
8190 char *name;
8191 long value;
8192};
8193
Fred Drake12c6e2d1999-12-14 21:25:03 +00008194static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008195conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008196 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008197{
Christian Heimes217cfd12007-12-02 14:31:20 +00008198 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008199 *valuep = PyLong_AS_LONG(arg);
8200 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008201 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008202 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008203 /* look up the value in the table using a binary search */
8204 size_t lo = 0;
8205 size_t mid;
8206 size_t hi = tablesize;
8207 int cmp;
8208 const char *confname;
8209 if (!PyUnicode_Check(arg)) {
8210 PyErr_SetString(PyExc_TypeError,
8211 "configuration names must be strings or integers");
8212 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008213 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008214 confname = _PyUnicode_AsString(arg);
8215 if (confname == NULL)
8216 return 0;
8217 while (lo < hi) {
8218 mid = (lo + hi) / 2;
8219 cmp = strcmp(confname, table[mid].name);
8220 if (cmp < 0)
8221 hi = mid;
8222 else if (cmp > 0)
8223 lo = mid + 1;
8224 else {
8225 *valuep = table[mid].value;
8226 return 1;
8227 }
8228 }
8229 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8230 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008231 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008232}
8233
8234
8235#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8236static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008237#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008239#endif
8240#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008242#endif
Fred Drakec9680921999-12-13 16:37:25 +00008243#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008245#endif
8246#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008248#endif
8249#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008251#endif
8252#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008254#endif
8255#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008257#endif
8258#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008260#endif
8261#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008263#endif
8264#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008266#endif
8267#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008269#endif
8270#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008272#endif
8273#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008275#endif
8276#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008278#endif
8279#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008281#endif
8282#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008284#endif
8285#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008287#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008288#ifdef _PC_ACL_ENABLED
8289 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8290#endif
8291#ifdef _PC_MIN_HOLE_SIZE
8292 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8293#endif
8294#ifdef _PC_ALLOC_SIZE_MIN
8295 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8296#endif
8297#ifdef _PC_REC_INCR_XFER_SIZE
8298 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8299#endif
8300#ifdef _PC_REC_MAX_XFER_SIZE
8301 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8302#endif
8303#ifdef _PC_REC_MIN_XFER_SIZE
8304 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8305#endif
8306#ifdef _PC_REC_XFER_ALIGN
8307 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8308#endif
8309#ifdef _PC_SYMLINK_MAX
8310 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8311#endif
8312#ifdef _PC_XATTR_ENABLED
8313 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8314#endif
8315#ifdef _PC_XATTR_EXISTS
8316 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8317#endif
8318#ifdef _PC_TIMESTAMP_RESOLUTION
8319 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8320#endif
Fred Drakec9680921999-12-13 16:37:25 +00008321};
8322
Fred Drakec9680921999-12-13 16:37:25 +00008323static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008324conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008325{
8326 return conv_confname(arg, valuep, posix_constants_pathconf,
8327 sizeof(posix_constants_pathconf)
8328 / sizeof(struct constdef));
8329}
8330#endif
8331
8332#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008333PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008334"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008335Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008336If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008337
8338static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008339posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008340{
8341 PyObject *result = NULL;
8342 int name, fd;
8343
Fred Drake12c6e2d1999-12-14 21:25:03 +00008344 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8345 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008346 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008347
Stefan Krah0e803b32010-11-26 16:16:47 +00008348 errno = 0;
8349 limit = fpathconf(fd, name);
8350 if (limit == -1 && errno != 0)
8351 posix_error();
8352 else
8353 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008354 }
8355 return result;
8356}
8357#endif
8358
8359
8360#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008361PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008362"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008363Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008364If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008365
8366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008367posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008368{
8369 PyObject *result = NULL;
8370 int name;
8371 char *path;
8372
8373 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8374 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008376
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 errno = 0;
8378 limit = pathconf(path, name);
8379 if (limit == -1 && errno != 0) {
8380 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008381 /* could be a path or name problem */
8382 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008383 else
Stefan Krah99439262010-11-26 12:58:05 +00008384 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 }
8386 else
8387 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008388 }
8389 return result;
8390}
8391#endif
8392
8393#ifdef HAVE_CONFSTR
8394static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008395#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008397#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008398#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008399 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008400#endif
8401#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008403#endif
Fred Draked86ed291999-12-15 15:34:33 +00008404#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008405 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008406#endif
8407#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008409#endif
8410#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008412#endif
8413#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008415#endif
Fred Drakec9680921999-12-13 16:37:25 +00008416#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008418#endif
8419#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008420 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008421#endif
8422#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008423 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008424#endif
8425#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008426 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008427#endif
8428#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008430#endif
8431#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008432 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008433#endif
8434#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008435 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008436#endif
8437#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008439#endif
Fred Draked86ed291999-12-15 15:34:33 +00008440#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008442#endif
Fred Drakec9680921999-12-13 16:37:25 +00008443#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008445#endif
Fred Draked86ed291999-12-15 15:34:33 +00008446#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008448#endif
8449#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008451#endif
8452#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008453 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008454#endif
8455#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008457#endif
Fred Drakec9680921999-12-13 16:37:25 +00008458#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008460#endif
8461#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008462 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008463#endif
8464#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008466#endif
8467#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008468 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008469#endif
8470#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008472#endif
8473#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008475#endif
8476#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008478#endif
8479#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008481#endif
8482#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008483 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008484#endif
8485#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008486 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008487#endif
8488#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008490#endif
8491#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008493#endif
8494#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008495 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008496#endif
8497#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008498 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008499#endif
8500#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008502#endif
8503#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008504 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008505#endif
Fred Draked86ed291999-12-15 15:34:33 +00008506#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008508#endif
8509#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008510 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008511#endif
8512#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008513 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008514#endif
8515#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008516 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008517#endif
8518#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008520#endif
8521#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008522 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008523#endif
8524#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008526#endif
8527#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008528 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008529#endif
8530#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008531 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008532#endif
8533#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008534 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008535#endif
8536#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008537 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008538#endif
8539#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008541#endif
8542#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008544#endif
Fred Drakec9680921999-12-13 16:37:25 +00008545};
8546
8547static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008548conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008549{
8550 return conv_confname(arg, valuep, posix_constants_confstr,
8551 sizeof(posix_constants_confstr)
8552 / sizeof(struct constdef));
8553}
8554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008555PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008556"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008557Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008558
8559static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008560posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008561{
8562 PyObject *result = NULL;
8563 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008564 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008565 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008566
Victor Stinnercb043522010-09-10 23:49:04 +00008567 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8568 return NULL;
8569
8570 errno = 0;
8571 len = confstr(name, buffer, sizeof(buffer));
8572 if (len == 0) {
8573 if (errno) {
8574 posix_error();
8575 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008576 }
8577 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008578 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008579 }
8580 }
Victor Stinnercb043522010-09-10 23:49:04 +00008581
8582 if ((unsigned int)len >= sizeof(buffer)) {
8583 char *buf = PyMem_Malloc(len);
8584 if (buf == NULL)
8585 return PyErr_NoMemory();
8586 confstr(name, buf, len);
8587 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8588 PyMem_Free(buf);
8589 }
8590 else
8591 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008592 return result;
8593}
8594#endif
8595
8596
8597#ifdef HAVE_SYSCONF
8598static struct constdef posix_constants_sysconf[] = {
8599#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008600 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008601#endif
8602#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008604#endif
8605#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008606 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008607#endif
8608#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008609 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008610#endif
8611#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008612 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008613#endif
8614#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008615 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008616#endif
8617#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008618 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008619#endif
8620#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008621 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008622#endif
8623#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008625#endif
8626#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008628#endif
Fred Draked86ed291999-12-15 15:34:33 +00008629#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008631#endif
8632#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008634#endif
Fred Drakec9680921999-12-13 16:37:25 +00008635#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008637#endif
Fred Drakec9680921999-12-13 16:37:25 +00008638#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008640#endif
8641#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008643#endif
8644#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008646#endif
8647#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008649#endif
8650#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008652#endif
Fred Draked86ed291999-12-15 15:34:33 +00008653#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008655#endif
Fred Drakec9680921999-12-13 16:37:25 +00008656#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008658#endif
8659#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008661#endif
8662#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008663 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008664#endif
8665#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008666 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008667#endif
8668#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008670#endif
Fred Draked86ed291999-12-15 15:34:33 +00008671#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008672 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008673#endif
Fred Drakec9680921999-12-13 16:37:25 +00008674#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008676#endif
8677#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008679#endif
8680#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008681 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008682#endif
8683#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008684 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008685#endif
8686#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008687 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008688#endif
8689#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008690 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008691#endif
8692#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008693 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008694#endif
8695#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008697#endif
8698#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008699 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008700#endif
8701#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008703#endif
8704#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008705 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008706#endif
8707#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008709#endif
8710#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008712#endif
8713#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008715#endif
8716#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008717 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008718#endif
8719#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008721#endif
8722#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008723 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008724#endif
8725#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008726 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008727#endif
8728#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008729 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008730#endif
8731#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008732 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008733#endif
8734#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008735 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008736#endif
8737#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008738 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008739#endif
8740#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008741 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008742#endif
Fred Draked86ed291999-12-15 15:34:33 +00008743#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008744 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008745#endif
Fred Drakec9680921999-12-13 16:37:25 +00008746#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008747 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008748#endif
8749#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008750 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008751#endif
8752#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008754#endif
Fred Draked86ed291999-12-15 15:34:33 +00008755#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008757#endif
Fred Drakec9680921999-12-13 16:37:25 +00008758#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008760#endif
Fred Draked86ed291999-12-15 15:34:33 +00008761#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008763#endif
8764#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008766#endif
Fred Drakec9680921999-12-13 16:37:25 +00008767#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008769#endif
8770#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008772#endif
8773#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008775#endif
8776#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008778#endif
Fred Draked86ed291999-12-15 15:34:33 +00008779#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008781#endif
Fred Drakec9680921999-12-13 16:37:25 +00008782#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008783 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008784#endif
8785#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008786 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008787#endif
8788#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008789 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008790#endif
8791#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008793#endif
8794#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008796#endif
8797#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008799#endif
8800#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008802#endif
Fred Draked86ed291999-12-15 15:34:33 +00008803#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008805#endif
Fred Drakec9680921999-12-13 16:37:25 +00008806#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
8809#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008811#endif
Fred Draked86ed291999-12-15 15:34:33 +00008812#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008814#endif
Fred Drakec9680921999-12-13 16:37:25 +00008815#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
8818#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
Fred Draked86ed291999-12-15 15:34:33 +00008842#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008844#endif
8845#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008847#endif
Fred Drakec9680921999-12-13 16:37:25 +00008848#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
8851#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008853#endif
8854#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008856#endif
8857#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008859#endif
8860#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008862#endif
8863#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008865#endif
8866#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008868#endif
8869#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008871#endif
8872#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008874#endif
8875#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008877#endif
8878#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008880#endif
8881#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008883#endif
8884#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008886#endif
8887#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008889#endif
8890#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008892#endif
8893#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008895#endif
8896#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008898#endif
8899#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008901#endif
8902#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008904#endif
8905#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008907#endif
8908#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008910#endif
8911#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008913#endif
8914#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008916#endif
8917#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
8920#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008922#endif
8923#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
8929#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008931#endif
8932#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008933 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008934#endif
8935#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008937#endif
8938#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008940#endif
8941#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008943#endif
8944#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008945 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008946#endif
8947#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008949#endif
8950#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008952#endif
Fred Draked86ed291999-12-15 15:34:33 +00008953#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008955#endif
Fred Drakec9680921999-12-13 16:37:25 +00008956#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008957 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008958#endif
8959#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008960 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008961#endif
8962#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008963 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008964#endif
8965#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008966 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008967#endif
8968#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008969 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008970#endif
8971#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008972 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008973#endif
8974#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008975 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008976#endif
8977#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008978 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008979#endif
8980#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008981 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008982#endif
8983#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008984 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008985#endif
8986#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008987 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008988#endif
8989#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008990 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008991#endif
8992#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008993 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008994#endif
8995#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008996 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008997#endif
8998#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008999 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009000#endif
9001#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009002 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009003#endif
9004#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009005 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009006#endif
9007#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009008 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009009#endif
9010#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009011 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009012#endif
9013#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009014 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009015#endif
9016#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009017 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009018#endif
9019#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009020 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009021#endif
9022#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009023 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009024#endif
9025#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009026 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009027#endif
9028#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009029 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009030#endif
9031#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009032 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009033#endif
9034#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009035 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009036#endif
9037#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009038 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009039#endif
9040#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009041 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009042#endif
9043#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009044 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009045#endif
9046#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009047 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009048#endif
9049#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009050 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009051#endif
9052#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009053 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009054#endif
9055#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009056 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009057#endif
9058#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009059 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009060#endif
9061#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009062 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009063#endif
9064#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009065 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009066#endif
9067#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009068 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009069#endif
9070#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009071 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009072#endif
9073#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009074 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009075#endif
9076#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009077 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009078#endif
9079#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009080 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009081#endif
9082#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009083 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009084#endif
9085#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009086 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009087#endif
9088#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009089 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009090#endif
9091};
9092
9093static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009094conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009095{
9096 return conv_confname(arg, valuep, posix_constants_sysconf,
9097 sizeof(posix_constants_sysconf)
9098 / sizeof(struct constdef));
9099}
9100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009101PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009102"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009103Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009104
9105static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009106posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009107{
9108 PyObject *result = NULL;
9109 int name;
9110
9111 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9112 int value;
9113
9114 errno = 0;
9115 value = sysconf(name);
9116 if (value == -1 && errno != 0)
9117 posix_error();
9118 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009119 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009120 }
9121 return result;
9122}
9123#endif
9124
9125
Fred Drakebec628d1999-12-15 18:31:10 +00009126/* This code is used to ensure that the tables of configuration value names
9127 * are in sorted order as required by conv_confname(), and also to build the
9128 * the exported dictionaries that are used to publish information about the
9129 * names available on the host platform.
9130 *
9131 * Sorting the table at runtime ensures that the table is properly ordered
9132 * when used, even for platforms we're not able to test on. It also makes
9133 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009134 */
Fred Drakebec628d1999-12-15 18:31:10 +00009135
9136static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009137cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009138{
9139 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009140 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009141 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009142 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009143
9144 return strcmp(c1->name, c2->name);
9145}
9146
9147static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009148setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009149 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009150{
Fred Drakebec628d1999-12-15 18:31:10 +00009151 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009152 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009153
9154 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9155 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009156 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009157 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009158
Barry Warsaw3155db32000-04-13 15:20:40 +00009159 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009160 PyObject *o = PyLong_FromLong(table[i].value);
9161 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9162 Py_XDECREF(o);
9163 Py_DECREF(d);
9164 return -1;
9165 }
9166 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009167 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009168 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009169}
9170
Fred Drakebec628d1999-12-15 18:31:10 +00009171/* Return -1 on failure, 0 on success. */
9172static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009173setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009174{
9175#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009176 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009177 sizeof(posix_constants_pathconf)
9178 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009179 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009180 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009181#endif
9182#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009183 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009184 sizeof(posix_constants_confstr)
9185 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009186 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009187 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009188#endif
9189#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009190 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009191 sizeof(posix_constants_sysconf)
9192 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009193 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009194 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009195#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009196 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009197}
Fred Draked86ed291999-12-15 15:34:33 +00009198
9199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009200PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009201"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009202Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009203in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009204
9205static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009206posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009207{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009208 abort();
9209 /*NOTREACHED*/
9210 Py_FatalError("abort() called from Python code didn't abort!");
9211 return NULL;
9212}
Fred Drakebec628d1999-12-15 18:31:10 +00009213
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009214#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009215PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009216"startfile(filepath [, operation]) - Start a file with its associated\n\
9217application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009218\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009219When \"operation\" is not specified or \"open\", this acts like\n\
9220double-clicking the file in Explorer, or giving the file name as an\n\
9221argument to the DOS \"start\" command: the file is opened with whatever\n\
9222application (if any) its extension is associated.\n\
9223When another \"operation\" is given, it specifies what should be done with\n\
9224the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009225\n\
9226startfile returns as soon as the associated application is launched.\n\
9227There is no option to wait for the application to close, and no way\n\
9228to retrieve the application's exit status.\n\
9229\n\
9230The filepath is relative to the current directory. If you want to use\n\
9231an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009232the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009233
9234static PyObject *
9235win32_startfile(PyObject *self, PyObject *args)
9236{
Victor Stinner8c62be82010-05-06 00:08:46 +00009237 PyObject *ofilepath;
9238 char *filepath;
9239 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009240 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009241 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009242
Victor Stinnereb5657a2011-09-30 01:44:27 +02009243 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 if (!PyArg_ParseTuple(args, "U|s:startfile",
9245 &unipath, &operation)) {
9246 PyErr_Clear();
9247 goto normal;
9248 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009249
Victor Stinner8c62be82010-05-06 00:08:46 +00009250 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009251 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009252 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009253 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009254 PyErr_Clear();
9255 operation = NULL;
9256 goto normal;
9257 }
9258 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009259
Victor Stinnereb5657a2011-09-30 01:44:27 +02009260 wpath = PyUnicode_AsUnicode(unipath);
9261 if (wpath == NULL)
9262 goto normal;
9263 if (uoperation) {
9264 woperation = PyUnicode_AsUnicode(uoperation);
9265 if (woperation == NULL)
9266 goto normal;
9267 }
9268 else
9269 woperation = NULL;
9270
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009272 rc = ShellExecuteW((HWND)0, woperation, wpath,
9273 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009274 Py_END_ALLOW_THREADS
9275
Victor Stinnereb5657a2011-09-30 01:44:27 +02009276 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009277 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009278 win32_error_object("startfile", unipath);
9279 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009280 }
9281 Py_INCREF(Py_None);
9282 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009283
9284normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9286 PyUnicode_FSConverter, &ofilepath,
9287 &operation))
9288 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009289 if (win32_warn_bytes_api()) {
9290 Py_DECREF(ofilepath);
9291 return NULL;
9292 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 filepath = PyBytes_AsString(ofilepath);
9294 Py_BEGIN_ALLOW_THREADS
9295 rc = ShellExecute((HWND)0, operation, filepath,
9296 NULL, NULL, SW_SHOWNORMAL);
9297 Py_END_ALLOW_THREADS
9298 if (rc <= (HINSTANCE)32) {
9299 PyObject *errval = win32_error("startfile", filepath);
9300 Py_DECREF(ofilepath);
9301 return errval;
9302 }
9303 Py_DECREF(ofilepath);
9304 Py_INCREF(Py_None);
9305 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009306}
9307#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009308
Martin v. Löwis438b5342002-12-27 10:16:42 +00009309#ifdef HAVE_GETLOADAVG
9310PyDoc_STRVAR(posix_getloadavg__doc__,
9311"getloadavg() -> (float, float, float)\n\n\
9312Return the number of processes in the system run queue averaged over\n\
9313the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9314was unobtainable");
9315
9316static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009317posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009318{
9319 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009320 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009321 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9322 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009323 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009324 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009325}
9326#endif
9327
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009328#ifdef MS_WINDOWS
9329
9330PyDoc_STRVAR(win32_urandom__doc__,
9331"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009332Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009333
9334typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9335 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9336 DWORD dwFlags );
9337typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9338 BYTE *pbBuffer );
9339
9340static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009341/* This handle is never explicitly released. Instead, the operating
9342 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009343static HCRYPTPROV hCryptProv = 0;
9344
Tim Peters4ad82172004-08-30 17:02:04 +00009345static PyObject*
9346win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009347{
Victor Stinner8c62be82010-05-06 00:08:46 +00009348 int howMany;
9349 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009350
Victor Stinner8c62be82010-05-06 00:08:46 +00009351 /* Read arguments */
9352 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9353 return NULL;
9354 if (howMany < 0)
9355 return PyErr_Format(PyExc_ValueError,
9356 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009357
Victor Stinner8c62be82010-05-06 00:08:46 +00009358 if (hCryptProv == 0) {
9359 HINSTANCE hAdvAPI32 = NULL;
9360 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009361
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 /* Obtain handle to the DLL containing CryptoAPI
9363 This should not fail */
9364 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9365 if(hAdvAPI32 == NULL)
9366 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009367
Victor Stinner8c62be82010-05-06 00:08:46 +00009368 /* Obtain pointers to the CryptoAPI functions
9369 This will fail on some early versions of Win95 */
9370 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9371 hAdvAPI32,
9372 "CryptAcquireContextA");
9373 if (pCryptAcquireContext == NULL)
9374 return PyErr_Format(PyExc_NotImplementedError,
9375 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009376
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9378 hAdvAPI32, "CryptGenRandom");
9379 if (pCryptGenRandom == NULL)
9380 return PyErr_Format(PyExc_NotImplementedError,
9381 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009382
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 /* Acquire context */
9384 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9385 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9386 return win32_error("CryptAcquireContext", NULL);
9387 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009388
Victor Stinner8c62be82010-05-06 00:08:46 +00009389 /* Allocate bytes */
9390 result = PyBytes_FromStringAndSize(NULL, howMany);
9391 if (result != NULL) {
9392 /* Get random data */
9393 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9394 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9395 PyBytes_AS_STRING(result))) {
9396 Py_DECREF(result);
9397 return win32_error("CryptGenRandom", NULL);
9398 }
9399 }
9400 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009401}
9402#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009403
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009404PyDoc_STRVAR(device_encoding__doc__,
9405"device_encoding(fd) -> str\n\n\
9406Return a string describing the encoding of the device\n\
9407if the output is a terminal; else return None.");
9408
9409static PyObject *
9410device_encoding(PyObject *self, PyObject *args)
9411{
Victor Stinner8c62be82010-05-06 00:08:46 +00009412 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009413#if defined(MS_WINDOWS) || defined(MS_WIN64)
9414 UINT cp;
9415#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009416 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9417 return NULL;
9418 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9419 Py_INCREF(Py_None);
9420 return Py_None;
9421 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009422#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009423 if (fd == 0)
9424 cp = GetConsoleCP();
9425 else if (fd == 1 || fd == 2)
9426 cp = GetConsoleOutputCP();
9427 else
9428 cp = 0;
9429 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9430 has no console */
9431 if (cp != 0)
9432 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009433#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009434 {
9435 char *codeset = nl_langinfo(CODESET);
9436 if (codeset != NULL && codeset[0] != 0)
9437 return PyUnicode_FromString(codeset);
9438 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009439#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 Py_INCREF(Py_None);
9441 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009442}
9443
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009444#ifdef __VMS
9445/* Use openssl random routine */
9446#include <openssl/rand.h>
9447PyDoc_STRVAR(vms_urandom__doc__,
9448"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009449Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009450
9451static PyObject*
9452vms_urandom(PyObject *self, PyObject *args)
9453{
Victor Stinner8c62be82010-05-06 00:08:46 +00009454 int howMany;
9455 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009456
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 /* Read arguments */
9458 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9459 return NULL;
9460 if (howMany < 0)
9461 return PyErr_Format(PyExc_ValueError,
9462 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009463
Victor Stinner8c62be82010-05-06 00:08:46 +00009464 /* Allocate bytes */
9465 result = PyBytes_FromStringAndSize(NULL, howMany);
9466 if (result != NULL) {
9467 /* Get random data */
9468 if (RAND_pseudo_bytes((unsigned char*)
9469 PyBytes_AS_STRING(result),
9470 howMany) < 0) {
9471 Py_DECREF(result);
9472 return PyErr_Format(PyExc_ValueError,
9473 "RAND_pseudo_bytes");
9474 }
9475 }
9476 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009477}
9478#endif
9479
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009480#ifdef HAVE_SETRESUID
9481PyDoc_STRVAR(posix_setresuid__doc__,
9482"setresuid(ruid, euid, suid)\n\n\
9483Set the current process's real, effective, and saved user ids.");
9484
9485static PyObject*
9486posix_setresuid (PyObject *self, PyObject *args)
9487{
Victor Stinner8c62be82010-05-06 00:08:46 +00009488 /* We assume uid_t is no larger than a long. */
9489 long ruid, euid, suid;
9490 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9491 return NULL;
9492 if (setresuid(ruid, euid, suid) < 0)
9493 return posix_error();
9494 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009495}
9496#endif
9497
9498#ifdef HAVE_SETRESGID
9499PyDoc_STRVAR(posix_setresgid__doc__,
9500"setresgid(rgid, egid, sgid)\n\n\
9501Set the current process's real, effective, and saved group ids.");
9502
9503static PyObject*
9504posix_setresgid (PyObject *self, PyObject *args)
9505{
Victor Stinner8c62be82010-05-06 00:08:46 +00009506 /* We assume uid_t is no larger than a long. */
9507 long rgid, egid, sgid;
9508 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9509 return NULL;
9510 if (setresgid(rgid, egid, sgid) < 0)
9511 return posix_error();
9512 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009513}
9514#endif
9515
9516#ifdef HAVE_GETRESUID
9517PyDoc_STRVAR(posix_getresuid__doc__,
9518"getresuid() -> (ruid, euid, suid)\n\n\
9519Get tuple of the current process's real, effective, and saved user ids.");
9520
9521static PyObject*
9522posix_getresuid (PyObject *self, PyObject *noargs)
9523{
Victor Stinner8c62be82010-05-06 00:08:46 +00009524 uid_t ruid, euid, suid;
9525 long l_ruid, l_euid, l_suid;
9526 if (getresuid(&ruid, &euid, &suid) < 0)
9527 return posix_error();
9528 /* Force the values into long's as we don't know the size of uid_t. */
9529 l_ruid = ruid;
9530 l_euid = euid;
9531 l_suid = suid;
9532 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009533}
9534#endif
9535
9536#ifdef HAVE_GETRESGID
9537PyDoc_STRVAR(posix_getresgid__doc__,
9538"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009539Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009540
9541static PyObject*
9542posix_getresgid (PyObject *self, PyObject *noargs)
9543{
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 uid_t rgid, egid, sgid;
9545 long l_rgid, l_egid, l_sgid;
9546 if (getresgid(&rgid, &egid, &sgid) < 0)
9547 return posix_error();
9548 /* Force the values into long's as we don't know the size of uid_t. */
9549 l_rgid = rgid;
9550 l_egid = egid;
9551 l_sgid = sgid;
9552 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009553}
9554#endif
9555
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009556/* Posix *at family of functions:
9557 faccessat, fchmodat, fchownat, fstatat, futimesat,
9558 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9559 unlinkat, utimensat, mkfifoat */
9560
9561#ifdef HAVE_FACCESSAT
9562PyDoc_STRVAR(posix_faccessat__doc__,
9563"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9564Like access() but if path is relative, it is taken as relative to dirfd.\n\
9565flags is optional and can be constructed by ORing together zero or more\n\
9566of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9567If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9568is interpreted relative to the current working directory.");
9569
9570static PyObject *
9571posix_faccessat(PyObject *self, PyObject *args)
9572{
9573 PyObject *opath;
9574 char *path;
9575 int mode;
9576 int res;
9577 int dirfd, flags = 0;
9578 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9579 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9580 return NULL;
9581 path = PyBytes_AsString(opath);
9582 Py_BEGIN_ALLOW_THREADS
9583 res = faccessat(dirfd, path, mode, flags);
9584 Py_END_ALLOW_THREADS
9585 Py_DECREF(opath);
9586 return PyBool_FromLong(res == 0);
9587}
9588#endif
9589
9590#ifdef HAVE_FCHMODAT
9591PyDoc_STRVAR(posix_fchmodat__doc__,
9592"fchmodat(dirfd, path, mode, flags=0)\n\n\
9593Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9594flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9595If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9596is interpreted relative to the current working directory.");
9597
9598static PyObject *
9599posix_fchmodat(PyObject *self, PyObject *args)
9600{
9601 int dirfd, mode, res;
9602 int flags = 0;
9603 PyObject *opath;
9604 char *path;
9605
9606 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9607 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9608 return NULL;
9609
9610 path = PyBytes_AsString(opath);
9611
9612 Py_BEGIN_ALLOW_THREADS
9613 res = fchmodat(dirfd, path, mode, flags);
9614 Py_END_ALLOW_THREADS
9615 Py_DECREF(opath);
9616 if (res < 0)
9617 return posix_error();
9618 Py_RETURN_NONE;
9619}
9620#endif /* HAVE_FCHMODAT */
9621
9622#ifdef HAVE_FCHOWNAT
9623PyDoc_STRVAR(posix_fchownat__doc__,
9624"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9625Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9626flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9627If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9628is interpreted relative to the current working directory.");
9629
9630static PyObject *
9631posix_fchownat(PyObject *self, PyObject *args)
9632{
9633 PyObject *opath;
9634 int dirfd, res;
9635 long uid, gid;
9636 int flags = 0;
9637 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009638
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009639 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9640 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9641 return NULL;
9642
9643 path = PyBytes_AsString(opath);
9644
9645 Py_BEGIN_ALLOW_THREADS
9646 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9647 Py_END_ALLOW_THREADS
9648 Py_DECREF(opath);
9649 if (res < 0)
9650 return posix_error();
9651 Py_RETURN_NONE;
9652}
9653#endif /* HAVE_FCHOWNAT */
9654
9655#ifdef HAVE_FSTATAT
9656PyDoc_STRVAR(posix_fstatat__doc__,
9657"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9658Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9659flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9660If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9661is interpreted relative to the current working directory.");
9662
9663static PyObject *
9664posix_fstatat(PyObject *self, PyObject *args)
9665{
9666 PyObject *opath;
9667 char *path;
9668 STRUCT_STAT st;
9669 int dirfd, res, flags = 0;
9670
9671 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9672 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9673 return NULL;
9674 path = PyBytes_AsString(opath);
9675
9676 Py_BEGIN_ALLOW_THREADS
9677 res = fstatat(dirfd, path, &st, flags);
9678 Py_END_ALLOW_THREADS
9679 Py_DECREF(opath);
9680 if (res != 0)
9681 return posix_error();
9682
9683 return _pystat_fromstructstat(&st);
9684}
9685#endif
9686
9687#ifdef HAVE_FUTIMESAT
9688PyDoc_STRVAR(posix_futimesat__doc__,
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009689"futimesat(dirfd, path[, (atime, mtime)])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009690Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9691If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9692is interpreted relative to the current working directory.");
9693
9694static PyObject *
9695posix_futimesat(PyObject *self, PyObject *args)
9696{
9697 PyObject *opath;
9698 char *path;
9699 int res, dirfd;
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009700 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009701 time_t atime, mtime;
9702 long ausec, musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009703
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009704 if (!PyArg_ParseTuple(args, "iO&|O:futimesat",
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009705 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9706 return NULL;
9707 path = PyBytes_AsString(opath);
9708 if (arg == Py_None) {
9709 /* optional time values not given */
9710 Py_BEGIN_ALLOW_THREADS
9711 res = futimesat(dirfd, path, NULL);
9712 Py_END_ALLOW_THREADS
9713 }
9714 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9715 PyErr_SetString(PyExc_TypeError,
9716 "futimesat() arg 3 must be a tuple (atime, mtime)");
9717 Py_DECREF(opath);
9718 return NULL;
9719 }
9720 else {
9721 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009722 &atime, &ausec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009723 Py_DECREF(opath);
9724 return NULL;
9725 }
9726 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009727 &mtime, &musec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009728 Py_DECREF(opath);
9729 return NULL;
9730 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009731
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009732 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009733 {
9734#ifdef HAVE_UTIMENSAT
9735 struct timespec buf[2];
9736 buf[0].tv_sec = atime;
9737 buf[0].tv_nsec = ausec;
9738 buf[1].tv_sec = mtime;
9739 buf[1].tv_nsec = musec;
9740 res = utimensat(dirfd, path, buf, 0);
9741#else
9742 struct timeval buf[2];
9743 buf[0].tv_sec = atime;
9744 buf[0].tv_usec = ausec;
9745 buf[1].tv_sec = mtime;
9746 buf[1].tv_usec = musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009747 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009748#endif
9749 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009750 Py_END_ALLOW_THREADS
9751 }
9752 Py_DECREF(opath);
9753 if (res < 0) {
9754 return posix_error();
9755 }
9756 Py_RETURN_NONE;
9757}
9758#endif
9759
9760#ifdef HAVE_LINKAT
9761PyDoc_STRVAR(posix_linkat__doc__,
9762"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9763Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9764and if dstpath is relative, it is taken as relative to dstfd.\n\
9765flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9766If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9767srcpath is interpreted relative to the current working directory. This\n\
9768also applies for dstpath.");
9769
9770static PyObject *
9771posix_linkat(PyObject *self, PyObject *args)
9772{
9773 PyObject *osrc, *odst;
9774 char *src, *dst;
9775 int res, srcfd, dstfd;
9776 int flags = 0;
9777
9778 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9779 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9780 return NULL;
9781 src = PyBytes_AsString(osrc);
9782 dst = PyBytes_AsString(odst);
9783 Py_BEGIN_ALLOW_THREADS
9784 res = linkat(srcfd, src, dstfd, dst, flags);
9785 Py_END_ALLOW_THREADS
9786 Py_DECREF(osrc);
9787 Py_DECREF(odst);
9788 if (res < 0)
9789 return posix_error();
9790 Py_RETURN_NONE;
9791}
9792#endif /* HAVE_LINKAT */
9793
9794#ifdef HAVE_MKDIRAT
9795PyDoc_STRVAR(posix_mkdirat__doc__,
9796"mkdirat(dirfd, path, mode=0o777)\n\n\
9797Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9798If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9799is interpreted relative to the current working directory.");
9800
9801static PyObject *
9802posix_mkdirat(PyObject *self, PyObject *args)
9803{
9804 int res, dirfd;
9805 PyObject *opath;
9806 char *path;
9807 int mode = 0777;
9808
9809 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9810 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9811 return NULL;
9812 path = PyBytes_AsString(opath);
9813 Py_BEGIN_ALLOW_THREADS
9814 res = mkdirat(dirfd, path, mode);
9815 Py_END_ALLOW_THREADS
9816 Py_DECREF(opath);
9817 if (res < 0)
9818 return posix_error();
9819 Py_RETURN_NONE;
9820}
9821#endif
9822
9823#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9824PyDoc_STRVAR(posix_mknodat__doc__,
9825"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9826Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9827If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9828is interpreted relative to the current working directory.");
9829
9830static PyObject *
9831posix_mknodat(PyObject *self, PyObject *args)
9832{
9833 PyObject *opath;
9834 char *filename;
9835 int mode = 0600;
9836 int device = 0;
9837 int res, dirfd;
9838 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9839 PyUnicode_FSConverter, &opath, &mode, &device))
9840 return NULL;
9841 filename = PyBytes_AS_STRING(opath);
9842 Py_BEGIN_ALLOW_THREADS
9843 res = mknodat(dirfd, filename, mode, device);
9844 Py_END_ALLOW_THREADS
9845 Py_DECREF(opath);
9846 if (res < 0)
9847 return posix_error();
9848 Py_RETURN_NONE;
9849}
9850#endif
9851
9852#ifdef HAVE_OPENAT
9853PyDoc_STRVAR(posix_openat__doc__,
9854"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9855Like open() but if path is relative, it is taken as relative to dirfd.\n\
9856If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9857is interpreted relative to the current working directory.");
9858
9859static PyObject *
9860posix_openat(PyObject *self, PyObject *args)
9861{
9862 PyObject *ofile;
9863 char *file;
9864 int flag, dirfd, fd;
9865 int mode = 0777;
9866
9867 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9868 &dirfd, PyUnicode_FSConverter, &ofile,
9869 &flag, &mode))
9870 return NULL;
9871 file = PyBytes_AsString(ofile);
9872 Py_BEGIN_ALLOW_THREADS
9873 fd = openat(dirfd, file, flag, mode);
9874 Py_END_ALLOW_THREADS
9875 Py_DECREF(ofile);
9876 if (fd < 0)
9877 return posix_error();
9878 return PyLong_FromLong((long)fd);
9879}
9880#endif
9881
9882#ifdef HAVE_READLINKAT
9883PyDoc_STRVAR(posix_readlinkat__doc__,
9884"readlinkat(dirfd, path) -> path\n\n\
9885Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9886If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9887is interpreted relative to the current working directory.");
9888
9889static PyObject *
9890posix_readlinkat(PyObject *self, PyObject *args)
9891{
9892 PyObject *v, *opath;
9893 char buf[MAXPATHLEN];
9894 char *path;
9895 int n, dirfd;
9896 int arg_is_unicode = 0;
9897
9898 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9899 &dirfd, PyUnicode_FSConverter, &opath))
9900 return NULL;
9901 path = PyBytes_AsString(opath);
9902 v = PySequence_GetItem(args, 1);
9903 if (v == NULL) {
9904 Py_DECREF(opath);
9905 return NULL;
9906 }
9907
9908 if (PyUnicode_Check(v)) {
9909 arg_is_unicode = 1;
9910 }
9911 Py_DECREF(v);
9912
9913 Py_BEGIN_ALLOW_THREADS
9914 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9915 Py_END_ALLOW_THREADS
9916 Py_DECREF(opath);
9917 if (n < 0)
9918 return posix_error();
9919
9920 if (arg_is_unicode)
9921 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9922 else
9923 return PyBytes_FromStringAndSize(buf, n);
9924}
9925#endif /* HAVE_READLINKAT */
9926
9927#ifdef HAVE_RENAMEAT
9928PyDoc_STRVAR(posix_renameat__doc__,
9929"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9930Like rename() but if oldpath is relative, it is taken as relative to\n\
9931olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9932If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9933oldpath is interpreted relative to the current working directory. This\n\
9934also applies for newpath.");
9935
9936static PyObject *
9937posix_renameat(PyObject *self, PyObject *args)
9938{
9939 int res;
9940 PyObject *opathold, *opathnew;
9941 char *opath, *npath;
9942 int oldfd, newfd;
9943
9944 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9945 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9946 return NULL;
9947 opath = PyBytes_AsString(opathold);
9948 npath = PyBytes_AsString(opathnew);
9949 Py_BEGIN_ALLOW_THREADS
9950 res = renameat(oldfd, opath, newfd, npath);
9951 Py_END_ALLOW_THREADS
9952 Py_DECREF(opathold);
9953 Py_DECREF(opathnew);
9954 if (res < 0)
9955 return posix_error();
9956 Py_RETURN_NONE;
9957}
9958#endif
9959
9960#if HAVE_SYMLINKAT
9961PyDoc_STRVAR(posix_symlinkat__doc__,
9962"symlinkat(src, dstfd, dst)\n\n\
9963Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9964If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9965is interpreted relative to the current working directory.");
9966
9967static PyObject *
9968posix_symlinkat(PyObject *self, PyObject *args)
9969{
9970 int res, dstfd;
9971 PyObject *osrc, *odst;
9972 char *src, *dst;
9973
9974 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9975 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9976 return NULL;
9977 src = PyBytes_AsString(osrc);
9978 dst = PyBytes_AsString(odst);
9979 Py_BEGIN_ALLOW_THREADS
9980 res = symlinkat(src, dstfd, dst);
9981 Py_END_ALLOW_THREADS
9982 Py_DECREF(osrc);
9983 Py_DECREF(odst);
9984 if (res < 0)
9985 return posix_error();
9986 Py_RETURN_NONE;
9987}
9988#endif /* HAVE_SYMLINKAT */
9989
9990#ifdef HAVE_UNLINKAT
9991PyDoc_STRVAR(posix_unlinkat__doc__,
9992"unlinkat(dirfd, path, flags=0)\n\n\
9993Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9994flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9995specified, unlinkat() behaves like rmdir().\n\
9996If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9997is interpreted relative to the current working directory.");
9998
9999static PyObject *
10000posix_unlinkat(PyObject *self, PyObject *args)
10001{
10002 int dirfd, res, flags = 0;
10003 PyObject *opath;
10004 char *path;
10005
10006 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
10007 &dirfd, PyUnicode_FSConverter, &opath, &flags))
10008 return NULL;
10009 path = PyBytes_AsString(opath);
10010 Py_BEGIN_ALLOW_THREADS
10011 res = unlinkat(dirfd, path, flags);
10012 Py_END_ALLOW_THREADS
10013 Py_DECREF(opath);
10014 if (res < 0)
10015 return posix_error();
10016 Py_RETURN_NONE;
10017}
10018#endif
10019
10020#ifdef HAVE_UTIMENSAT
10021PyDoc_STRVAR(posix_utimensat__doc__,
Brian Curtin569b4942011-11-07 16:09:20 -060010022"utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\
10023 mtime=(mtime_sec, mtime_nsec), flags=0])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010024utimensat(dirfd, path, None, None, flags)\n\n\
10025Updates the timestamps of a file with nanosecond precision. If path is\n\
10026relative, it is taken as relative to dirfd.\n\
Brian Curtin569b4942011-11-07 16:09:20 -060010027If atime and mtime are both None, which is the default, set atime and\n\
10028mtime to the current time.\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010029flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
10030If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10031is interpreted relative to the current working directory.\n\
10032If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
10033current time.\n\
10034If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
10035
10036static PyObject *
Brian Curtin569b4942011-11-07 16:09:20 -060010037posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010038{
10039 PyObject *opath;
10040 char *path;
10041 int res, dirfd, flags = 0;
Brian Curtin569b4942011-11-07 16:09:20 -060010042 PyObject *atime = Py_None;
10043 PyObject *mtime = Py_None;
10044
10045 static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL};
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010046
10047 struct timespec buf[2];
10048
Brian Curtin569b4942011-11-07 16:09:20 -060010049 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist,
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010050 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
10051 return NULL;
10052 path = PyBytes_AsString(opath);
10053 if (atime == Py_None && mtime == Py_None) {
10054 /* optional time values not given */
10055 Py_BEGIN_ALLOW_THREADS
10056 res = utimensat(dirfd, path, NULL, flags);
10057 Py_END_ALLOW_THREADS
10058 }
10059 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
10060 PyErr_SetString(PyExc_TypeError,
10061 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
10062 Py_DECREF(opath);
10063 return NULL;
10064 }
10065 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
10066 PyErr_SetString(PyExc_TypeError,
10067 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
10068 Py_DECREF(opath);
10069 return NULL;
10070 }
10071 else {
10072 if (!PyArg_ParseTuple(atime, "ll:utimensat",
10073 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
10074 Py_DECREF(opath);
10075 return NULL;
10076 }
10077 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
10078 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
10079 Py_DECREF(opath);
10080 return NULL;
10081 }
10082 Py_BEGIN_ALLOW_THREADS
10083 res = utimensat(dirfd, path, buf, flags);
10084 Py_END_ALLOW_THREADS
10085 }
10086 Py_DECREF(opath);
10087 if (res < 0) {
10088 return posix_error();
10089 }
10090 Py_RETURN_NONE;
10091}
10092#endif
10093
10094#ifdef HAVE_MKFIFOAT
10095PyDoc_STRVAR(posix_mkfifoat__doc__,
10096"mkfifoat(dirfd, path, mode=0o666)\n\n\
10097Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10098If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10099is interpreted relative to the current working directory.");
10100
10101static PyObject *
10102posix_mkfifoat(PyObject *self, PyObject *args)
10103{
10104 PyObject *opath;
10105 char *filename;
10106 int mode = 0666;
10107 int res, dirfd;
10108 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10109 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10110 return NULL;
10111 filename = PyBytes_AS_STRING(opath);
10112 Py_BEGIN_ALLOW_THREADS
10113 res = mkfifoat(dirfd, filename, mode);
10114 Py_END_ALLOW_THREADS
10115 Py_DECREF(opath);
10116 if (res < 0)
10117 return posix_error();
10118 Py_RETURN_NONE;
10119}
10120#endif
10121
Benjamin Peterson9428d532011-09-14 11:45:52 -040010122#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010123
10124static int
10125try_getxattr(const char *path, const char *name,
10126 ssize_t (*get)(const char *, const char *, void *, size_t),
10127 Py_ssize_t buf_size, PyObject **res)
10128{
10129 PyObject *value;
10130 Py_ssize_t len;
10131
10132 assert(buf_size <= XATTR_SIZE_MAX);
10133 value = PyBytes_FromStringAndSize(NULL, buf_size);
10134 if (!value)
10135 return 0;
10136 Py_BEGIN_ALLOW_THREADS;
10137 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10138 Py_END_ALLOW_THREADS;
10139 if (len < 0) {
10140 Py_DECREF(value);
10141 if (errno == ERANGE) {
10142 value = NULL;
10143 }
10144 else {
10145 posix_error();
10146 return 0;
10147 }
10148 }
10149 else if (len != buf_size) {
10150 /* Can only shrink. */
10151 _PyBytes_Resize(&value, len);
10152 }
10153 *res = value;
10154 return 1;
10155}
10156
10157static PyObject *
10158getxattr_common(const char *path, PyObject *name_obj,
10159 ssize_t (*get)(const char *, const char *, void *, size_t))
10160{
10161 PyObject *value;
10162 const char *name = PyBytes_AS_STRING(name_obj);
10163
10164 /* Try a small value first. */
10165 if (!try_getxattr(path, name, get, 128, &value))
10166 return NULL;
10167 if (value)
10168 return value;
10169 /* Now the maximum possible one. */
10170 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10171 return NULL;
10172 assert(value);
10173 return value;
10174}
10175
10176PyDoc_STRVAR(posix_getxattr__doc__,
10177"getxattr(path, attr) -> value\n\n\
10178Return the value of extended attribute *name* on *path*.");
10179
10180static PyObject *
10181posix_getxattr(PyObject *self, PyObject *args)
10182{
10183 PyObject *path, *res, *name;
10184
10185 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10186 PyUnicode_FSConverter, &name))
10187 return NULL;
10188 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10189 Py_DECREF(path);
10190 Py_DECREF(name);
10191 return res;
10192}
10193
10194PyDoc_STRVAR(posix_lgetxattr__doc__,
10195"lgetxattr(path, attr) -> value\n\n\
10196Like getxattr but don't follow symlinks.");
10197
10198static PyObject *
10199posix_lgetxattr(PyObject *self, PyObject *args)
10200{
10201 PyObject *path, *res, *name;
10202
10203 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10204 PyUnicode_FSConverter, &name))
10205 return NULL;
10206 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10207 Py_DECREF(path);
10208 Py_DECREF(name);
10209 return res;
10210}
10211
10212static ssize_t
10213wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10214{
10215 /* Hack to share code. */
10216 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10217}
10218
10219PyDoc_STRVAR(posix_fgetxattr__doc__,
10220"fgetxattr(fd, attr) -> value\n\n\
10221Like getxattr but operate on a fd instead of a path.");
10222
10223static PyObject *
10224posix_fgetxattr(PyObject *self, PyObject *args)
10225{
10226 PyObject *res, *name;
10227 int fd;
10228
10229 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10230 return NULL;
10231 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10232 Py_DECREF(name);
10233 return res;
10234}
10235
10236PyDoc_STRVAR(posix_setxattr__doc__,
10237"setxattr(path, attr, value, flags=0)\n\n\
10238Set extended attribute *attr* on *path* to *value*.");
10239
10240static PyObject *
10241posix_setxattr(PyObject *self, PyObject *args)
10242{
10243 PyObject *path, *name;
10244 Py_buffer data;
10245 int flags = 0, err;
10246
10247 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10248 &path, PyUnicode_FSConverter, &name, &data, &flags))
10249 return NULL;
10250 Py_BEGIN_ALLOW_THREADS;
10251 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10252 data.buf, data.len, flags);
10253 Py_END_ALLOW_THREADS;
10254 Py_DECREF(path);
10255 Py_DECREF(name);
10256 PyBuffer_Release(&data);
10257 if (err)
10258 return posix_error();
10259 Py_RETURN_NONE;
10260}
10261
10262PyDoc_STRVAR(posix_lsetxattr__doc__,
10263"lsetxattr(path, attr, value, flags=0)\n\n\
10264Like setxattr but don't follow symlinks.");
10265
10266static PyObject *
10267posix_lsetxattr(PyObject *self, PyObject *args)
10268{
10269 PyObject *path, *name;
10270 Py_buffer data;
10271 int flags = 0, err;
10272
10273 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10274 &path, PyUnicode_FSConverter, &name, &data, &flags))
10275 return NULL;
10276 Py_BEGIN_ALLOW_THREADS;
10277 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10278 data.buf, data.len, flags);
10279 Py_END_ALLOW_THREADS;
10280 Py_DECREF(path);
10281 Py_DECREF(name);
10282 PyBuffer_Release(&data);
10283 if (err)
10284 return posix_error();
10285 Py_RETURN_NONE;
10286}
10287
10288PyDoc_STRVAR(posix_fsetxattr__doc__,
10289"fsetxattr(fd, attr, value, flags=0)\n\n\
10290Like setxattr but operates on *fd* instead of a path.");
10291
10292static PyObject *
10293posix_fsetxattr(PyObject *self, PyObject *args)
10294{
10295 Py_buffer data;
10296 const char *name;
10297 int fd, flags = 0, err;
10298
10299 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10300 &name, &data, &flags))
10301 return NULL;
10302 Py_BEGIN_ALLOW_THREADS;
10303 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10304 Py_END_ALLOW_THREADS;
10305 Py_DECREF(name);
10306 PyBuffer_Release(&data);
10307 if (err)
10308 return posix_error();
10309 Py_RETURN_NONE;
10310}
10311
10312PyDoc_STRVAR(posix_removexattr__doc__,
10313"removexattr(path, attr)\n\n\
10314Remove extended attribute *attr* on *path*.");
10315
10316static PyObject *
10317posix_removexattr(PyObject *self, PyObject *args)
10318{
10319 PyObject *path, *name;
10320 int err;
10321
10322 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10323 PyUnicode_FSConverter, &name))
10324 return NULL;
10325 Py_BEGIN_ALLOW_THREADS;
10326 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10327 Py_END_ALLOW_THREADS;
10328 Py_DECREF(path);
10329 Py_DECREF(name);
10330 if (err)
10331 return posix_error();
10332 Py_RETURN_NONE;
10333}
10334
10335PyDoc_STRVAR(posix_lremovexattr__doc__,
10336"lremovexattr(path, attr)\n\n\
10337Like removexattr but don't follow symlinks.");
10338
10339static PyObject *
10340posix_lremovexattr(PyObject *self, PyObject *args)
10341{
10342 PyObject *path, *name;
10343 int err;
10344
10345 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10346 PyUnicode_FSConverter, &name))
10347 return NULL;
10348 Py_BEGIN_ALLOW_THREADS;
10349 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10350 Py_END_ALLOW_THREADS;
10351 Py_DECREF(path);
10352 Py_DECREF(name);
10353 if (err)
10354 return posix_error();
10355 Py_RETURN_NONE;
10356}
10357
10358PyDoc_STRVAR(posix_fremovexattr__doc__,
10359"fremovexattr(fd, attr)\n\n\
10360Like removexattr but operates on a file descriptor.");
10361
10362static PyObject *
10363posix_fremovexattr(PyObject *self, PyObject *args)
10364{
10365 PyObject *name;
10366 int fd, err;
10367
10368 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10369 PyUnicode_FSConverter, &name))
10370 return NULL;
10371 Py_BEGIN_ALLOW_THREADS;
10372 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10373 Py_END_ALLOW_THREADS;
10374 Py_DECREF(name);
10375 if (err)
10376 return posix_error();
10377 Py_RETURN_NONE;
10378}
10379
10380static Py_ssize_t
10381try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10382 Py_ssize_t buf_size, char **buf)
10383{
10384 Py_ssize_t len;
10385
10386 *buf = PyMem_MALLOC(buf_size);
10387 if (!*buf) {
10388 PyErr_NoMemory();
10389 return -1;
10390 }
10391 Py_BEGIN_ALLOW_THREADS;
10392 len = list(path, *buf, buf_size);
10393 Py_END_ALLOW_THREADS;
10394 if (len < 0) {
10395 PyMem_FREE(*buf);
10396 if (errno != ERANGE)
10397 posix_error();
10398 return -1;
10399 }
10400 return len;
10401}
10402
10403static PyObject *
10404listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10405{
10406 PyObject *res, *attr;
10407 Py_ssize_t len, err, start, i;
10408 char *buf;
10409
10410 len = try_listxattr(path, list, 256, &buf);
10411 if (len < 0) {
10412 if (PyErr_Occurred())
10413 return NULL;
10414 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10415 if (len < 0)
10416 return NULL;
10417 }
10418 res = PyList_New(0);
10419 if (!res) {
10420 PyMem_FREE(buf);
10421 return NULL;
10422 }
10423 for (start = i = 0; i < len; i++) {
10424 if (!buf[i]) {
10425 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10426 if (!attr) {
10427 Py_DECREF(res);
10428 PyMem_FREE(buf);
10429 return NULL;
10430 }
10431 err = PyList_Append(res, attr);
10432 Py_DECREF(attr);
10433 if (err) {
10434 Py_DECREF(res);
10435 PyMem_FREE(buf);
10436 return NULL;
10437 }
10438 start = i + 1;
10439 }
10440 }
10441 PyMem_FREE(buf);
10442 return res;
10443}
10444
10445PyDoc_STRVAR(posix_listxattr__doc__,
10446"listxattr(path)\n\n\
10447Return a list of extended attributes on *path*.");
10448
10449static PyObject *
10450posix_listxattr(PyObject *self, PyObject *args)
10451{
10452 PyObject *path, *res;
10453
10454 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10455 return NULL;
10456 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10457 Py_DECREF(path);
10458 return res;
10459}
10460
10461PyDoc_STRVAR(posix_llistxattr__doc__,
10462"llistxattr(path)\n\n\
10463Like listxattr but don't follow symlinks..");
10464
10465static PyObject *
10466posix_llistxattr(PyObject *self, PyObject *args)
10467{
10468 PyObject *path, *res;
10469
10470 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10471 return NULL;
10472 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10473 Py_DECREF(path);
10474 return res;
10475}
10476
10477static ssize_t
10478wrap_flistxattr(const char *path, char *buf, size_t len)
10479{
10480 /* Hack to share code. */
10481 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10482}
10483
10484PyDoc_STRVAR(posix_flistxattr__doc__,
10485"flistxattr(path)\n\n\
10486Like flistxattr but operates on a file descriptor.");
10487
10488static PyObject *
10489posix_flistxattr(PyObject *self, PyObject *args)
10490{
10491 long fd;
10492
10493 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10494 return NULL;
10495 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10496}
10497
Benjamin Peterson9428d532011-09-14 11:45:52 -040010498#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010499
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010500static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010501 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010502#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010504#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010505 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010506#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010507 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010508#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010510#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010511 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010512#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010513#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010514 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010515#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010516#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010517 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010518#endif /* HAVE_LCHMOD */
10519#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010520 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010521#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010522#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010523 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010524#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010525#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010526 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010527#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010528#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010529 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010530#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010531#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010532 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010533#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010534#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010535 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10536 METH_NOARGS, posix_getcwd__doc__},
10537 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10538 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010539#endif
10540#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010541 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010542#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010543 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010544#ifdef HAVE_FDOPENDIR
10545 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10546#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010547 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10548 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010549#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010550 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010551#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010552#ifdef HAVE_GETPRIORITY
10553 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10554#endif /* HAVE_GETPRIORITY */
10555#ifdef HAVE_SETPRIORITY
10556 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10557#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010558#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010559 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010560#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010561#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010562 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010563#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010564 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10565 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10566 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010567 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010568#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010570#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010571#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010572 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010573 win_symlink__doc__},
10574#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010575#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010576 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010577#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010579#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010580 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010581#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10583 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10584 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010585#ifdef HAVE_FUTIMES
10586 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10587#endif
10588#ifdef HAVE_LUTIMES
10589 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10590#endif
10591#ifdef HAVE_FUTIMENS
10592 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10593#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010594#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010595 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010596#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010597 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010598#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10600 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010601#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010602#ifdef HAVE_FEXECVE
10603 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10604#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010605#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010606 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10607 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010608#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10610 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010611#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010612#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010613#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010615#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010616#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010618#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010619#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010620#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010621 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10622 {"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 +020010623#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010624#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010625 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010626#endif
10627#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010628 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010629#endif
10630#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010631 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010632#endif
10633#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010634 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010635#endif
10636#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010637 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010638#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010639 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010640#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010641 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10642 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10643#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010644#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010645#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010646 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010647#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010648#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010649 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010650#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010651#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010653#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010654#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010656#endif /* HAVE_GETEUID */
10657#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010658 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010659#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010660#ifdef HAVE_GETGROUPLIST
10661 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10662#endif
Fred Drakec9680921999-12-13 16:37:25 +000010663#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010665#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010667#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010669#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010670#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010672#endif /* HAVE_GETPPID */
10673#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010675#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010676#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010678#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010679#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010681#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010682#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010684#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010685#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010687#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010688#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10690 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010691 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010692#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010693#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010694 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010695#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010696#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010698#endif /* HAVE_SETEUID */
10699#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010701#endif /* HAVE_SETEGID */
10702#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010704#endif /* HAVE_SETREUID */
10705#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010707#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010708#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010710#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010711#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010713#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010714#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010716#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010717#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010719#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010720#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010721 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010722#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010723#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010724 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010725#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010726#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010727 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010728#endif /* HAVE_WAIT3 */
10729#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010730 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010731#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010732#if defined(HAVE_WAITID) && !defined(__APPLE__)
10733 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10734#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010735#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010736 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010737#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010738#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010739 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010740#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010741#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010742 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010743#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010744#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010745 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010746#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010747#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010748 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010749#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010750#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010751 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010752#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010753 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10754 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10755 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10756 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10757 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10758 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010759#ifdef HAVE_LOCKF
10760 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10761#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010762 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10763 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010764#ifdef HAVE_READV
10765 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10766#endif
10767#ifdef HAVE_PREAD
10768 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10769#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010771#ifdef HAVE_WRITEV
10772 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10773#endif
10774#ifdef HAVE_PWRITE
10775 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10776#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010777#ifdef HAVE_SENDFILE
10778 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10779 posix_sendfile__doc__},
10780#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010781 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10782 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010783#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010784 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010785#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010786#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010787 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010788#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010789#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010791#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010792#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010793 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010794#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010795#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10797 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10798 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010799#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010800#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010801 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010802#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010803#ifdef HAVE_TRUNCATE
10804 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10805#endif
10806#ifdef HAVE_POSIX_FALLOCATE
10807 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10808#endif
10809#ifdef HAVE_POSIX_FADVISE
10810 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10811#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010812#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010814#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010815#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010816 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010817#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010819#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010820 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010821#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010822#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010823 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010824#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010825#ifdef HAVE_SYNC
10826 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10827#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010828#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010829 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010830#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010831#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010832#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010834#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010835#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010837#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010838#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010840#endif /* WIFSTOPPED */
10841#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010843#endif /* WIFSIGNALED */
10844#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010846#endif /* WIFEXITED */
10847#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010849#endif /* WEXITSTATUS */
10850#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010852#endif /* WTERMSIG */
10853#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010855#endif /* WSTOPSIG */
10856#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010857#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010858 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010859#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010860#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010861 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010862#endif
Fred Drakec9680921999-12-13 16:37:25 +000010863#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010864 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010865#endif
10866#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010867 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010868#endif
10869#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010871#endif
10872#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010873 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010874#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010875 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010876#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010877 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010878 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010879 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010880 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010881 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010882#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010883#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010885#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010886 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010888 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010889 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010891 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010892#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010894#endif
10895#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010897#endif
10898#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010900#endif
10901#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010903#endif
10904
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010905/* posix *at family of functions */
10906#ifdef HAVE_FACCESSAT
10907 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10908#endif
10909#ifdef HAVE_FCHMODAT
10910 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10911#endif /* HAVE_FCHMODAT */
10912#ifdef HAVE_FCHOWNAT
10913 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10914#endif /* HAVE_FCHOWNAT */
10915#ifdef HAVE_FSTATAT
10916 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10917#endif
10918#ifdef HAVE_FUTIMESAT
10919 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10920#endif
10921#ifdef HAVE_LINKAT
10922 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10923#endif /* HAVE_LINKAT */
10924#ifdef HAVE_MKDIRAT
10925 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10926#endif
10927#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10928 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10929#endif
10930#ifdef HAVE_OPENAT
10931 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10932#endif
10933#ifdef HAVE_READLINKAT
10934 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10935#endif /* HAVE_READLINKAT */
10936#ifdef HAVE_RENAMEAT
10937 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10938#endif
10939#if HAVE_SYMLINKAT
10940 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10941#endif /* HAVE_SYMLINKAT */
10942#ifdef HAVE_UNLINKAT
10943 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10944#endif
10945#ifdef HAVE_UTIMENSAT
Jesus Cead03a4912011-11-08 17:28:04 +010010946 {"utimensat", (PyCFunction)posix_utimensat,
10947 METH_VARARGS | METH_KEYWORDS,
Brian Curtin569b4942011-11-07 16:09:20 -060010948 posix_utimensat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010949#endif
10950#ifdef HAVE_MKFIFOAT
10951 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10952#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040010953#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010954 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10955 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10956 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10957 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10958 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10959 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10960 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10961 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10962 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10963 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10964 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10965 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10966#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010968};
10969
10970
Barry Warsaw4a342091996-12-19 23:50:02 +000010971static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010972ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010973{
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010975}
10976
Guido van Rossumd48f2521997-12-05 22:19:34 +000010977#if defined(PYOS_OS2)
10978/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010979static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010980{
10981 APIRET rc;
10982 ULONG values[QSV_MAX+1];
10983 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010984 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010985
10986 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010987 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010988 Py_END_ALLOW_THREADS
10989
10990 if (rc != NO_ERROR) {
10991 os2_error(rc);
10992 return -1;
10993 }
10994
Fred Drake4d1e64b2002-04-15 19:40:07 +000010995 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10996 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10997 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10998 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10999 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
11000 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
11001 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011002
11003 switch (values[QSV_VERSION_MINOR]) {
11004 case 0: ver = "2.00"; break;
11005 case 10: ver = "2.10"; break;
11006 case 11: ver = "2.11"; break;
11007 case 30: ver = "3.00"; break;
11008 case 40: ver = "4.00"; break;
11009 case 50: ver = "5.00"; break;
11010 default:
Tim Peters885d4572001-11-28 20:27:42 +000011011 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011013 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011014 ver = &tmp[0];
11015 }
11016
11017 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011018 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011019 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011020
11021 /* Add Indicator of Which Drive was Used to Boot the System */
11022 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11023 tmp[1] = ':';
11024 tmp[2] = '\0';
11025
Fred Drake4d1e64b2002-04-15 19:40:07 +000011026 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011027}
11028#endif
11029
Brian Curtin52173d42010-12-02 18:29:18 +000011030#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011031static int
Brian Curtin52173d42010-12-02 18:29:18 +000011032enable_symlink()
11033{
11034 HANDLE tok;
11035 TOKEN_PRIVILEGES tok_priv;
11036 LUID luid;
11037 int meth_idx = 0;
11038
11039 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011040 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011041
11042 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011043 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011044
11045 tok_priv.PrivilegeCount = 1;
11046 tok_priv.Privileges[0].Luid = luid;
11047 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11048
11049 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11050 sizeof(TOKEN_PRIVILEGES),
11051 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011052 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011053
Brian Curtin3b4499c2010-12-28 14:31:47 +000011054 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11055 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011056}
11057#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11058
Barry Warsaw4a342091996-12-19 23:50:02 +000011059static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011060all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011061{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011062#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011064#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011065#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011067#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011068#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011070#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011071#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011073#endif
Fred Drakec9680921999-12-13 16:37:25 +000011074#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011076#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011077#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011079#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011080#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011082#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011083#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011085#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011086#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011088#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011089#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011091#endif
11092#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011094#endif
11095#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011097#endif
11098#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011100#endif
11101#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011103#endif
11104#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011106#endif
11107#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011109#endif
11110#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011112#endif
11113#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011115#endif
11116#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011118#endif
11119#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011121#endif
11122#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011124#endif
11125#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011127#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011128#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011130#endif
11131#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011133#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011134#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011136#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011137#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011139#endif
11140#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011142#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011143#ifdef PRIO_PROCESS
11144 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11145#endif
11146#ifdef PRIO_PGRP
11147 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11148#endif
11149#ifdef PRIO_USER
11150 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11151#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011152#ifdef O_CLOEXEC
11153 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11154#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011155/* posix - constants for *at functions */
11156#ifdef AT_SYMLINK_NOFOLLOW
11157 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11158#endif
11159#ifdef AT_EACCESS
11160 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11161#endif
11162#ifdef AT_FDCWD
11163 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11164#endif
11165#ifdef AT_REMOVEDIR
11166 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11167#endif
11168#ifdef AT_SYMLINK_FOLLOW
11169 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11170#endif
11171#ifdef UTIME_NOW
11172 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11173#endif
11174#ifdef UTIME_OMIT
11175 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11176#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011177
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011178
Tim Peters5aa91602002-01-30 05:46:57 +000011179/* MS Windows */
11180#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011181 /* Don't inherit in child processes. */
11182 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011183#endif
11184#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011185 /* Optimize for short life (keep in memory). */
11186 /* MS forgot to define this one with a non-underscore form too. */
11187 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011188#endif
11189#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011190 /* Automatically delete when last handle is closed. */
11191 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011192#endif
11193#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011194 /* Optimize for random access. */
11195 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011196#endif
11197#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 /* Optimize for sequential access. */
11199 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011200#endif
11201
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011202/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011203#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 /* Send a SIGIO signal whenever input or output
11205 becomes available on file descriptor */
11206 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011207#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011208#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011209 /* Direct disk access. */
11210 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011211#endif
11212#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 /* Must be a directory. */
11214 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011215#endif
11216#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 /* Do not follow links. */
11218 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011219#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011220#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011221 /* Do not update the access time. */
11222 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011223#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011224
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011226#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011227 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011228#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011229#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011230 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011231#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011232#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011233 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011234#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011235#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011236 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011237#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011238#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011239 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011240#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011241#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011242 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011243#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011244#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011245 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011246#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011247#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011248 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011249#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011250#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011251 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011252#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011253#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011254 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011255#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011256#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011257 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011258#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011259#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011260 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011261#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011262#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011263 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011264#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011265#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011266 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011267#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011268#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011269 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011270#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011271#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011272 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011273#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011274#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011275 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011276#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011277
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011278 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011279#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011280 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011281#endif /* ST_RDONLY */
11282#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011283 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011284#endif /* ST_NOSUID */
11285
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011286 /* FreeBSD sendfile() constants */
11287#ifdef SF_NODISKIO
11288 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11289#endif
11290#ifdef SF_MNOWAIT
11291 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11292#endif
11293#ifdef SF_SYNC
11294 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11295#endif
11296
Ross Lagerwall7807c352011-03-17 20:20:30 +020011297 /* constants for posix_fadvise */
11298#ifdef POSIX_FADV_NORMAL
11299 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11300#endif
11301#ifdef POSIX_FADV_SEQUENTIAL
11302 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11303#endif
11304#ifdef POSIX_FADV_RANDOM
11305 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11306#endif
11307#ifdef POSIX_FADV_NOREUSE
11308 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11309#endif
11310#ifdef POSIX_FADV_WILLNEED
11311 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11312#endif
11313#ifdef POSIX_FADV_DONTNEED
11314 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11315#endif
11316
11317 /* constants for waitid */
11318#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11319 if (ins(d, "P_PID", (long)P_PID)) return -1;
11320 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11321 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11322#endif
11323#ifdef WEXITED
11324 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11325#endif
11326#ifdef WNOWAIT
11327 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11328#endif
11329#ifdef WSTOPPED
11330 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11331#endif
11332#ifdef CLD_EXITED
11333 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11334#endif
11335#ifdef CLD_DUMPED
11336 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11337#endif
11338#ifdef CLD_TRAPPED
11339 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11340#endif
11341#ifdef CLD_CONTINUED
11342 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11343#endif
11344
11345 /* constants for lockf */
11346#ifdef F_LOCK
11347 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11348#endif
11349#ifdef F_TLOCK
11350 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11351#endif
11352#ifdef F_ULOCK
11353 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11354#endif
11355#ifdef F_TEST
11356 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11357#endif
11358
11359 /* constants for futimens */
11360#ifdef UTIME_NOW
11361 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11362#endif
11363#ifdef UTIME_OMIT
11364 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11365#endif
11366
Guido van Rossum246bc171999-02-01 23:54:31 +000011367#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011368#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11370 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11371 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11372 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11373 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11374 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11375 if (ins(d, "P_PM", (long)P_PM)) return -1;
11376 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11377 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11378 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11379 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11380 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11381 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11382 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11383 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11384 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11385 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11386 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11387 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11388 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011389#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11391 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11392 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11393 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11394 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011395#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011396#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011397
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011398#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011399 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011400 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11401 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11402#ifdef SCHED_SPORADIC
11403 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11404#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011405#ifdef SCHED_BATCH
11406 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11407#endif
11408#ifdef SCHED_IDLE
11409 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11410#endif
11411#ifdef SCHED_RESET_ON_FORK
11412 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11413#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011414#ifdef SCHED_SYS
11415 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11416#endif
11417#ifdef SCHED_IA
11418 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11419#endif
11420#ifdef SCHED_FSS
11421 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11422#endif
11423#ifdef SCHED_FX
11424 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11425#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011426#endif
11427
Benjamin Peterson9428d532011-09-14 11:45:52 -040011428#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011429 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11430 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11431 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11432#endif
11433
Victor Stinner8b905bd2011-10-25 13:34:04 +020011434#ifdef RTLD_LAZY
11435 if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1;
11436#endif
11437#ifdef RTLD_NOW
11438 if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1;
11439#endif
11440#ifdef RTLD_GLOBAL
11441 if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1;
11442#endif
11443#ifdef RTLD_LOCAL
11444 if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1;
11445#endif
11446#ifdef RTLD_NODELETE
11447 if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1;
11448#endif
11449#ifdef RTLD_NOLOAD
11450 if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1;
11451#endif
11452#ifdef RTLD_DEEPBIND
11453 if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1;
11454#endif
11455
Guido van Rossumd48f2521997-12-05 22:19:34 +000011456#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011457 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011458#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011459 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011460}
11461
11462
Tim Peters5aa91602002-01-30 05:46:57 +000011463#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011464#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011465#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011466
11467#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011468#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011469#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011470
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011471#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011472#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011473#define MODNAME "posix"
11474#endif
11475
Martin v. Löwis1a214512008-06-11 05:26:20 +000011476static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011477 PyModuleDef_HEAD_INIT,
11478 MODNAME,
11479 posix__doc__,
11480 -1,
11481 posix_methods,
11482 NULL,
11483 NULL,
11484 NULL,
11485 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011486};
11487
11488
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011489PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011490INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011491{
Victor Stinner8c62be82010-05-06 00:08:46 +000011492 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011493
Brian Curtin52173d42010-12-02 18:29:18 +000011494#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011495 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011496#endif
11497
Victor Stinner8c62be82010-05-06 00:08:46 +000011498 m = PyModule_Create(&posixmodule);
11499 if (m == NULL)
11500 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011501
Victor Stinner8c62be82010-05-06 00:08:46 +000011502 /* Initialize environ dictionary */
11503 v = convertenviron();
11504 Py_XINCREF(v);
11505 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11506 return NULL;
11507 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011508
Victor Stinner8c62be82010-05-06 00:08:46 +000011509 if (all_ins(m))
11510 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011511
Victor Stinner8c62be82010-05-06 00:08:46 +000011512 if (setup_confname_tables(m))
11513 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011514
Victor Stinner8c62be82010-05-06 00:08:46 +000011515 Py_INCREF(PyExc_OSError);
11516 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011517
Benjamin Peterson2740af82011-08-02 17:41:34 -050011518#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011519 if (PyType_Ready(&cpu_set_type) < 0)
11520 return NULL;
11521 Py_INCREF(&cpu_set_type);
11522 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011523#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011524
Guido van Rossumb3d39562000-01-31 18:41:26 +000011525#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011526 if (posix_putenv_garbage == NULL)
11527 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011528#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011529
Victor Stinner8c62be82010-05-06 00:08:46 +000011530 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011531#if defined(HAVE_WAITID) && !defined(__APPLE__)
11532 waitid_result_desc.name = MODNAME ".waitid_result";
11533 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11534#endif
11535
Victor Stinner8c62be82010-05-06 00:08:46 +000011536 stat_result_desc.name = MODNAME ".stat_result";
11537 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11538 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11539 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11540 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11541 structseq_new = StatResultType.tp_new;
11542 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011543
Victor Stinner8c62be82010-05-06 00:08:46 +000011544 statvfs_result_desc.name = MODNAME ".statvfs_result";
11545 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011546#ifdef NEED_TICKS_PER_SECOND
11547# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011548 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011549# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011550 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011551# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011553# endif
11554#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011555
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011556#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011557 sched_param_desc.name = MODNAME ".sched_param";
11558 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11559 SchedParamType.tp_new = sched_param_new;
11560#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011561 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011562#if defined(HAVE_WAITID) && !defined(__APPLE__)
11563 Py_INCREF((PyObject*) &WaitidResultType);
11564 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11565#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011566 Py_INCREF((PyObject*) &StatResultType);
11567 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11568 Py_INCREF((PyObject*) &StatVFSResultType);
11569 PyModule_AddObject(m, "statvfs_result",
11570 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011571
11572#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011573 Py_INCREF(&SchedParamType);
11574 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011575#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011576 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011577
11578#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011579 /*
11580 * Step 2 of weak-linking support on Mac OS X.
11581 *
11582 * The code below removes functions that are not available on the
11583 * currently active platform.
11584 *
11585 * This block allow one to use a python binary that was build on
11586 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11587 * OSX 10.4.
11588 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011589#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011590 if (fstatvfs == NULL) {
11591 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11592 return NULL;
11593 }
11594 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011595#endif /* HAVE_FSTATVFS */
11596
11597#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011598 if (statvfs == NULL) {
11599 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11600 return NULL;
11601 }
11602 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011603#endif /* HAVE_STATVFS */
11604
11605# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011606 if (lchown == NULL) {
11607 if (PyObject_DelAttrString(m, "lchown") == -1) {
11608 return NULL;
11609 }
11610 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011611#endif /* HAVE_LCHOWN */
11612
11613
11614#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011615 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011616
Guido van Rossumb6775db1994-08-01 11:34:53 +000011617}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011618
11619#ifdef __cplusplus
11620}
11621#endif