blob: da4bb627ca1fb8af8486ec20276bb1a784db5a1c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020033# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020049#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000050#define INCL_DOS
51#define INCL_DOSERRORS
52#define INCL_DOSPROCESS
53#define INCL_NOPMAPI
54#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000055#if defined(PYCC_GCC)
56#include <ctype.h>
57#include <io.h>
58#include <stdio.h>
59#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000060#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000061#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000062#endif
63
Ross Lagerwall4d076da2011-03-18 06:56:53 +020064#ifdef HAVE_SYS_UIO_H
65#include <sys/uio.h>
66#endif
67
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#endif /* HAVE_SYS_TYPES_H */
71
72#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000073#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000075
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000077#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000078#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000079
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000081#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000083
Guido van Rossumb6775db1994-08-01 11:34:53 +000084#ifdef HAVE_FCNTL_H
85#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000086#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Guido van Rossuma6535fd2001-10-18 19:44:10 +000088#ifdef HAVE_GRP_H
89#include <grp.h>
90#endif
91
Barry Warsaw5676bd12003-01-07 20:57:09 +000092#ifdef HAVE_SYSEXITS_H
93#include <sysexits.h>
94#endif /* HAVE_SYSEXITS_H */
95
Anthony Baxter8a560de2004-10-13 15:30:56 +000096#ifdef HAVE_SYS_LOADAVG_H
97#include <sys/loadavg.h>
98#endif
99
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000100#ifdef HAVE_LANGINFO_H
101#include <langinfo.h>
102#endif
103
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000104#ifdef HAVE_SYS_SENDFILE_H
105#include <sys/sendfile.h>
106#endif
107
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500108#ifdef HAVE_SCHED_H
109#include <sched.h>
110#endif
111
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000112#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
113#ifdef HAVE_SYS_SOCKET_H
114#include <sys/socket.h>
115#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000116#endif
117
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000119/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000120#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000121#include <process.h>
122#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000123#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_GETCWD 1
125#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000126#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#if defined(__OS2__)
128#define HAVE_EXECV 1
129#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000130#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000131#include <process.h>
132#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000133#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#define HAVE_EXECV 1
135#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#define HAVE_OPENDIR 1
137#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000138#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000139#define HAVE_WAIT 1
140#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000141#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000142#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000143#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000144#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000145#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_EXECV 1
147#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#define HAVE_SYSTEM 1
149#define HAVE_CWAIT 1
150#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000151#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000152#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000153#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
154/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156/* Unix functions that the configure script doesn't check for */
157#define HAVE_EXECV 1
158#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000160#define HAVE_FORK1 1
161#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162#define HAVE_GETCWD 1
163#define HAVE_GETEGID 1
164#define HAVE_GETEUID 1
165#define HAVE_GETGID 1
166#define HAVE_GETPPID 1
167#define HAVE_GETUID 1
168#define HAVE_KILL 1
169#define HAVE_OPENDIR 1
170#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000171#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000173#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000174#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif /* _MSC_VER */
176#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000177#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000178#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000179
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000181
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000182#if defined(__sgi)&&_COMPILER_VERSION>=700
183/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
184 (default) */
185extern char *ctermid_r(char *);
186#endif
187
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000188#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000189#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000190extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000192#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000196#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000197#endif
198#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000199extern int chdir(char *);
200extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000201#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int chdir(const char *);
203extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000204#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000205#ifdef __BORLANDC__
206extern int chmod(const char *, int);
207#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000208extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000209#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000210/*#ifdef HAVE_FCHMOD
211extern int fchmod(int, mode_t);
212#endif*/
213/*#ifdef HAVE_LCHMOD
214extern int lchmod(const char *, mode_t);
215#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000216extern int chown(const char *, uid_t, gid_t);
217extern char *getcwd(char *, int);
218extern char *strerror(int);
219extern int link(const char *, const char *);
220extern int rename(const char *, const char *);
221extern int stat(const char *, struct stat *);
222extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000225#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000227extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000230
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233#ifdef HAVE_UTIME_H
234#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000235#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000237#ifdef HAVE_SYS_UTIME_H
238#include <sys/utime.h>
239#define HAVE_UTIME_H /* pretend we do for the rest of this file */
240#endif /* HAVE_SYS_UTIME_H */
241
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#ifdef HAVE_SYS_TIMES_H
243#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245
246#ifdef HAVE_SYS_PARAM_H
247#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000248#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
250#ifdef HAVE_SYS_UTSNAME_H
251#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000254#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000256#define NAMLEN(dirent) strlen((dirent)->d_name)
257#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000258#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000259#include <direct.h>
260#define NAMLEN(dirent) strlen((dirent)->d_name)
261#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000264#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000265#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000267#endif
268#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000270#endif
271#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#endif
274#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#endif
280#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282#endif
283#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000286#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000287#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000288#endif
289#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000290#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000291#endif
292#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000293#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000294#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000295#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000296#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000298#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000299#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000300#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
301#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000302static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000303#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000304#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305
Guido van Rossumd48f2521997-12-05 22:19:34 +0000306#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000308#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309
Tim Petersbc2e10e2002-03-03 23:17:02 +0000310#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000311#if defined(PATH_MAX) && PATH_MAX > 1024
312#define MAXPATHLEN PATH_MAX
313#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000314#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000315#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#endif /* MAXPATHLEN */
317
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000318#ifdef UNION_WAIT
319/* Emulate some macros on systems that have a union instead of macros */
320
321#ifndef WIFEXITED
322#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
323#endif
324
325#ifndef WEXITSTATUS
326#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
327#endif
328
329#ifndef WTERMSIG
330#define WTERMSIG(u_wait) ((u_wait).w_termsig)
331#endif
332
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333#define WAIT_TYPE union wait
334#define WAIT_STATUS_INT(s) (s.w_status)
335
336#else /* !UNION_WAIT */
337#define WAIT_TYPE int
338#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000339#endif /* UNION_WAIT */
340
Greg Wardb48bc172000-03-01 21:51:56 +0000341/* Don't use the "_r" form if we don't need it (also, won't have a
342 prototype for it, at least on Solaris -- maybe others as well?). */
343#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
344#define USE_CTERMID_R
345#endif
346
Fred Drake699f3522000-06-29 21:12:41 +0000347/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000348#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000349#undef FSTAT
350#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000351#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000352# define STAT win32_stat
353# define FSTAT win32_fstat
354# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000355#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000356# define STAT stat
357# define FSTAT fstat
358# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000359#endif
360
Tim Peters11b23062003-04-23 02:39:17 +0000361#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000362#include <sys/mkdev.h>
363#else
364#if defined(MAJOR_IN_SYSMACROS)
365#include <sys/sysmacros.h>
366#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000367#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
368#include <sys/mkdev.h>
369#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000370#endif
Fred Drake699f3522000-06-29 21:12:41 +0000371
Georg Brandla391b112011-02-25 15:23:18 +0000372static int
373_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000374{
375#if !defined(HAVE_LARGEFILE_SUPPORT)
376 *((off_t*)addr) = PyLong_AsLong(arg);
377#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000378 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000379#endif
380 if (PyErr_Occurred())
381 return 0;
382 return 1;
383}
384
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000385#if defined _MSC_VER && _MSC_VER >= 1400
386/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
387 * valid and throw an assertion if it isn't.
388 * Normally, an invalid fd is likely to be a C program error and therefore
389 * an assertion can be useful, but it does contradict the POSIX standard
390 * which for write(2) states:
391 * "Otherwise, -1 shall be returned and errno set to indicate the error."
392 * "[EBADF] The fildes argument is not a valid file descriptor open for
393 * writing."
394 * Furthermore, python allows the user to enter any old integer
395 * as a fd and should merely raise a python exception on error.
396 * The Microsoft CRT doesn't provide an official way to check for the
397 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000398 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000399 * internal structures involved.
400 * The structures below must be updated for each version of visual studio
401 * according to the file internal.h in the CRT source, until MS comes
402 * up with a less hacky way to do this.
403 * (all of this is to avoid globally modifying the CRT behaviour using
404 * _set_invalid_parameter_handler() and _CrtSetReportMode())
405 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000406/* The actual size of the structure is determined at runtime.
407 * Only the first items must be present.
408 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000409typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000410 intptr_t osfhnd;
411 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000412} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000413
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000414extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000415#define IOINFO_L2E 5
416#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
417#define IOINFO_ARRAYS 64
418#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
419#define FOPEN 0x01
420#define _NO_CONSOLE_FILENO (intptr_t)-2
421
422/* This function emulates what the windows CRT does to validate file handles */
423int
424_PyVerify_fd(int fd)
425{
Victor Stinner8c62be82010-05-06 00:08:46 +0000426 const int i1 = fd >> IOINFO_L2E;
427 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000428
Antoine Pitrou22e41552010-08-15 18:07:50 +0000429 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000430
Victor Stinner8c62be82010-05-06 00:08:46 +0000431 /* Determine the actual size of the ioinfo structure,
432 * as used by the CRT loaded in memory
433 */
434 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
435 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
436 }
437 if (sizeof_ioinfo == 0) {
438 /* This should not happen... */
439 goto fail;
440 }
441
442 /* See that it isn't a special CLEAR fileno */
443 if (fd != _NO_CONSOLE_FILENO) {
444 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
445 * we check pointer validity and other info
446 */
447 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
448 /* finally, check that the file is open */
449 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
450 if (info->osfile & FOPEN) {
451 return 1;
452 }
453 }
454 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000455 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000456 errno = EBADF;
457 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000458}
459
460/* the special case of checking dup2. The target fd must be in a sensible range */
461static int
462_PyVerify_fd_dup2(int fd1, int fd2)
463{
Victor Stinner8c62be82010-05-06 00:08:46 +0000464 if (!_PyVerify_fd(fd1))
465 return 0;
466 if (fd2 == _NO_CONSOLE_FILENO)
467 return 0;
468 if ((unsigned)fd2 < _NHANDLE_)
469 return 1;
470 else
471 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000472}
473#else
474/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
475#define _PyVerify_fd_dup2(A, B) (1)
476#endif
477
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000478#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000479/* The following structure was copied from
480 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
481 include doesn't seem to be present in the Windows SDK (at least as included
482 with Visual Studio Express). */
483typedef struct _REPARSE_DATA_BUFFER {
484 ULONG ReparseTag;
485 USHORT ReparseDataLength;
486 USHORT Reserved;
487 union {
488 struct {
489 USHORT SubstituteNameOffset;
490 USHORT SubstituteNameLength;
491 USHORT PrintNameOffset;
492 USHORT PrintNameLength;
493 ULONG Flags;
494 WCHAR PathBuffer[1];
495 } SymbolicLinkReparseBuffer;
496
497 struct {
498 USHORT SubstituteNameOffset;
499 USHORT SubstituteNameLength;
500 USHORT PrintNameOffset;
501 USHORT PrintNameLength;
502 WCHAR PathBuffer[1];
503 } MountPointReparseBuffer;
504
505 struct {
506 UCHAR DataBuffer[1];
507 } GenericReparseBuffer;
508 };
509} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
510
511#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
512 GenericReparseBuffer)
513#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
514
515static int
Brian Curtind25aef52011-06-13 15:16:04 -0500516win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000517{
518 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
519 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
520 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000521
522 if (0 == DeviceIoControl(
523 reparse_point_handle,
524 FSCTL_GET_REPARSE_POINT,
525 NULL, 0, /* in buffer */
526 target_buffer, sizeof(target_buffer),
527 &n_bytes_returned,
528 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500529 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000530
531 if (reparse_tag)
532 *reparse_tag = rdb->ReparseTag;
533
Brian Curtind25aef52011-06-13 15:16:04 -0500534 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000535}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000536#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000537
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000539#ifdef WITH_NEXT_FRAMEWORK
540/* On Darwin/MacOSX a shared library or framework has no access to
541** environ directly, we must obtain it with _NSGetEnviron().
542*/
543#include <crt_externs.h>
544static char **environ;
545#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000547#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548
Barry Warsaw53699e91996-12-10 23:23:01 +0000549static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000550convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551{
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000555#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000557#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000558#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000559 APIRET rc;
560 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
561#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000562
Victor Stinner8c62be82010-05-06 00:08:46 +0000563 d = PyDict_New();
564 if (d == NULL)
565 return NULL;
566#ifdef WITH_NEXT_FRAMEWORK
567 if (environ == NULL)
568 environ = *_NSGetEnviron();
569#endif
570#ifdef MS_WINDOWS
571 /* _wenviron must be initialized in this way if the program is started
572 through main() instead of wmain(). */
573 _wgetenv(L"");
574 if (_wenviron == NULL)
575 return d;
576 /* This part ignores errors */
577 for (e = _wenviron; *e != NULL; e++) {
578 PyObject *k;
579 PyObject *v;
580 wchar_t *p = wcschr(*e, L'=');
581 if (p == NULL)
582 continue;
583 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
584 if (k == NULL) {
585 PyErr_Clear();
586 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000587 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000588 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
589 if (v == NULL) {
590 PyErr_Clear();
591 Py_DECREF(k);
592 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000593 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000594 if (PyDict_GetItem(d, k) == NULL) {
595 if (PyDict_SetItem(d, k, v) != 0)
596 PyErr_Clear();
597 }
598 Py_DECREF(k);
599 Py_DECREF(v);
600 }
601#else
602 if (environ == NULL)
603 return d;
604 /* This part ignores errors */
605 for (e = environ; *e != NULL; e++) {
606 PyObject *k;
607 PyObject *v;
608 char *p = strchr(*e, '=');
609 if (p == NULL)
610 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000611 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000612 if (k == NULL) {
613 PyErr_Clear();
614 continue;
615 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000616 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000617 if (v == NULL) {
618 PyErr_Clear();
619 Py_DECREF(k);
620 continue;
621 }
622 if (PyDict_GetItem(d, k) == NULL) {
623 if (PyDict_SetItem(d, k, v) != 0)
624 PyErr_Clear();
625 }
626 Py_DECREF(k);
627 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000628 }
629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000630#if defined(PYOS_OS2)
631 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
632 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
633 PyObject *v = PyBytes_FromString(buffer);
634 PyDict_SetItemString(d, "BEGINLIBPATH", v);
635 Py_DECREF(v);
636 }
637 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
638 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
639 PyObject *v = PyBytes_FromString(buffer);
640 PyDict_SetItemString(d, "ENDLIBPATH", v);
641 Py_DECREF(v);
642 }
643#endif
644 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645}
646
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647/* Set a POSIX-specific error from errno, and return NULL */
648
Barry Warsawd58d7641998-07-23 16:14:40 +0000649static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000650posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000651{
Victor Stinner8c62be82010-05-06 00:08:46 +0000652 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000653}
Barry Warsawd58d7641998-07-23 16:14:40 +0000654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000655posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000656{
Victor Stinner8c62be82010-05-06 00:08:46 +0000657 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000658}
659
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000660
Mark Hammondef8b6542001-05-13 08:04:26 +0000661static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000662posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000663{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000664 PyObject *name_str, *rc;
665 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
666 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000667 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000668 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
669 name_str);
670 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000671 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000672}
673
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000674#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000675static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000676win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000677{
Victor Stinner8c62be82010-05-06 00:08:46 +0000678 /* XXX We should pass the function name along in the future.
679 (winreg.c also wants to pass the function name.)
680 This would however require an additional param to the
681 Windows error object, which is non-trivial.
682 */
683 errno = GetLastError();
684 if (filename)
685 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
686 else
687 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000688}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000689
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000690static PyObject *
691win32_error_unicode(char* function, Py_UNICODE* filename)
692{
Victor Stinner8c62be82010-05-06 00:08:46 +0000693 /* XXX - see win32_error for comments on 'function' */
694 errno = GetLastError();
695 if (filename)
696 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
697 else
698 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000699}
700
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000702convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703{
Victor Stinner8c62be82010-05-06 00:08:46 +0000704 if (PyUnicode_CheckExact(*param))
705 Py_INCREF(*param);
706 else if (PyUnicode_Check(*param))
707 /* For a Unicode subtype that's not a Unicode object,
708 return a true Unicode object with the same data. */
709 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
710 PyUnicode_GET_SIZE(*param));
711 else
712 *param = PyUnicode_FromEncodedObject(*param,
713 Py_FileSystemDefaultEncoding,
714 "strict");
715 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716}
717
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000718#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719
Guido van Rossumd48f2521997-12-05 22:19:34 +0000720#if defined(PYOS_OS2)
721/**********************************************************************
722 * Helper Function to Trim and Format OS/2 Messages
723 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000724static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000725os2_formatmsg(char *msgbuf, int msglen, char *reason)
726{
727 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
728
729 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
730 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
731
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000732 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000733 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
734 }
735
736 /* Add Optional Reason Text */
737 if (reason) {
738 strcat(msgbuf, " : ");
739 strcat(msgbuf, reason);
740 }
741}
742
743/**********************************************************************
744 * Decode an OS/2 Operating System Error Code
745 *
746 * A convenience function to lookup an OS/2 error code and return a
747 * text message we can use to raise a Python exception.
748 *
749 * Notes:
750 * The messages for errors returned from the OS/2 kernel reside in
751 * the file OSO001.MSG in the \OS2 directory hierarchy.
752 *
753 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000754static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000755os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
756{
757 APIRET rc;
758 ULONG msglen;
759
760 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
761 Py_BEGIN_ALLOW_THREADS
762 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
763 errorcode, "oso001.msg", &msglen);
764 Py_END_ALLOW_THREADS
765
766 if (rc == NO_ERROR)
767 os2_formatmsg(msgbuf, msglen, reason);
768 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000769 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000770 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000771
772 return msgbuf;
773}
774
775/* Set an OS/2-specific error and return NULL. OS/2 kernel
776 errors are not in a global variable e.g. 'errno' nor are
777 they congruent with posix error numbers. */
778
Victor Stinner8c62be82010-05-06 00:08:46 +0000779static PyObject *
780os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000781{
782 char text[1024];
783 PyObject *v;
784
785 os2_strerror(text, sizeof(text), code, "");
786
787 v = Py_BuildValue("(is)", code, text);
788 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000789 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000790 Py_DECREF(v);
791 }
792 return NULL; /* Signal to Python that an Exception is Pending */
793}
794
795#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796
797/* POSIX generic methods */
798
Barry Warsaw53699e91996-12-10 23:23:01 +0000799static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000800posix_fildes(PyObject *fdobj, int (*func)(int))
801{
Victor Stinner8c62be82010-05-06 00:08:46 +0000802 int fd;
803 int res;
804 fd = PyObject_AsFileDescriptor(fdobj);
805 if (fd < 0)
806 return NULL;
807 if (!_PyVerify_fd(fd))
808 return posix_error();
809 Py_BEGIN_ALLOW_THREADS
810 res = (*func)(fd);
811 Py_END_ALLOW_THREADS
812 if (res < 0)
813 return posix_error();
814 Py_INCREF(Py_None);
815 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000816}
Guido van Rossum21142a01999-01-08 21:05:37 +0000817
818static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820{
Victor Stinner8c62be82010-05-06 00:08:46 +0000821 PyObject *opath1 = NULL;
822 char *path1;
823 int res;
824 if (!PyArg_ParseTuple(args, format,
825 PyUnicode_FSConverter, &opath1))
826 return NULL;
827 path1 = PyBytes_AsString(opath1);
828 Py_BEGIN_ALLOW_THREADS
829 res = (*func)(path1);
830 Py_END_ALLOW_THREADS
831 if (res < 0)
832 return posix_error_with_allocated_filename(opath1);
833 Py_DECREF(opath1);
834 Py_INCREF(Py_None);
835 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836}
837
Barry Warsaw53699e91996-12-10 23:23:01 +0000838static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000839posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 char *format,
841 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842{
Victor Stinner8c62be82010-05-06 00:08:46 +0000843 PyObject *opath1 = NULL, *opath2 = NULL;
844 char *path1, *path2;
845 int res;
846 if (!PyArg_ParseTuple(args, format,
847 PyUnicode_FSConverter, &opath1,
848 PyUnicode_FSConverter, &opath2)) {
849 return NULL;
850 }
851 path1 = PyBytes_AsString(opath1);
852 path2 = PyBytes_AsString(opath2);
853 Py_BEGIN_ALLOW_THREADS
854 res = (*func)(path1, path2);
855 Py_END_ALLOW_THREADS
856 Py_DECREF(opath1);
857 Py_DECREF(opath2);
858 if (res != 0)
859 /* XXX how to report both path1 and path2??? */
860 return posix_error();
861 Py_INCREF(Py_None);
862 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863}
864
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000865#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000867win32_1str(PyObject* args, char* func,
868 char* format, BOOL (__stdcall *funcA)(LPCSTR),
869 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870{
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 PyObject *uni;
872 char *ansi;
873 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000874
Victor Stinner8c62be82010-05-06 00:08:46 +0000875 if (!PyArg_ParseTuple(args, wformat, &uni))
876 PyErr_Clear();
877 else {
878 Py_BEGIN_ALLOW_THREADS
879 result = funcW(PyUnicode_AsUnicode(uni));
880 Py_END_ALLOW_THREADS
881 if (!result)
882 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
883 Py_INCREF(Py_None);
884 return Py_None;
885 }
886 if (!PyArg_ParseTuple(args, format, &ansi))
887 return NULL;
888 Py_BEGIN_ALLOW_THREADS
889 result = funcA(ansi);
890 Py_END_ALLOW_THREADS
891 if (!result)
892 return win32_error(func, ansi);
893 Py_INCREF(Py_None);
894 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000895
896}
897
898/* This is a reimplementation of the C library's chdir function,
899 but one that produces Win32 errors instead of DOS error codes.
900 chdir is essentially a wrapper around SetCurrentDirectory; however,
901 it also needs to set "magic" environment variables indicating
902 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000903static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904win32_chdir(LPCSTR path)
905{
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 char new_path[MAX_PATH+1];
907 int result;
908 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909
Victor Stinner8c62be82010-05-06 00:08:46 +0000910 if(!SetCurrentDirectoryA(path))
911 return FALSE;
912 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
913 if (!result)
914 return FALSE;
915 /* In the ANSI API, there should not be any paths longer
916 than MAX_PATH. */
917 assert(result <= MAX_PATH+1);
918 if (strncmp(new_path, "\\\\", 2) == 0 ||
919 strncmp(new_path, "//", 2) == 0)
920 /* UNC path, nothing to do. */
921 return TRUE;
922 env[1] = new_path[0];
923 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924}
925
926/* The Unicode version differs from the ANSI version
927 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000928static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000929win32_wchdir(LPCWSTR path)
930{
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
932 int result;
933 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 if(!SetCurrentDirectoryW(path))
936 return FALSE;
937 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
938 if (!result)
939 return FALSE;
940 if (result > MAX_PATH+1) {
941 new_path = malloc(result * sizeof(wchar_t));
942 if (!new_path) {
943 SetLastError(ERROR_OUTOFMEMORY);
944 return FALSE;
945 }
946 result = GetCurrentDirectoryW(result, new_path);
947 if (!result) {
948 free(new_path);
949 return FALSE;
950 }
951 }
952 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
953 wcsncmp(new_path, L"//", 2) == 0)
954 /* UNC path, nothing to do. */
955 return TRUE;
956 env[1] = new_path[0];
957 result = SetEnvironmentVariableW(env, new_path);
958 if (new_path != _new_path)
959 free(new_path);
960 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961}
962#endif
963
Martin v. Löwis14694662006-02-03 12:54:16 +0000964#ifdef MS_WINDOWS
965/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
966 - time stamps are restricted to second resolution
967 - file modification times suffer from forth-and-back conversions between
968 UTC and local time
969 Therefore, we implement our own stat, based on the Win32 API directly.
970*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000971#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000972
973struct win32_stat{
974 int st_dev;
975 __int64 st_ino;
976 unsigned short st_mode;
977 int st_nlink;
978 int st_uid;
979 int st_gid;
980 int st_rdev;
981 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000982 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000983 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000984 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000985 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000986 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000987 int st_ctime_nsec;
988};
989
990static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
991
992static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000993FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000994{
Victor Stinner8c62be82010-05-06 00:08:46 +0000995 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
996 /* Cannot simply cast and dereference in_ptr,
997 since it might not be aligned properly */
998 __int64 in;
999 memcpy(&in, in_ptr, sizeof(in));
1000 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001001 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001002}
1003
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001005time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006{
Victor Stinner8c62be82010-05-06 00:08:46 +00001007 /* XXX endianness */
1008 __int64 out;
1009 out = time_in + secs_between_epochs;
1010 out = out * 10000000 + nsec_in / 100;
1011 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012}
1013
Martin v. Löwis14694662006-02-03 12:54:16 +00001014/* Below, we *know* that ugo+r is 0444 */
1015#if _S_IREAD != 0400
1016#error Unsupported C library
1017#endif
1018static int
1019attributes_to_mode(DWORD attr)
1020{
Victor Stinner8c62be82010-05-06 00:08:46 +00001021 int m = 0;
1022 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1023 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1024 else
1025 m |= _S_IFREG;
1026 if (attr & FILE_ATTRIBUTE_READONLY)
1027 m |= 0444;
1028 else
1029 m |= 0666;
1030 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001031}
1032
1033static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001034attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001035{
Victor Stinner8c62be82010-05-06 00:08:46 +00001036 memset(result, 0, sizeof(*result));
1037 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1038 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1039 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1040 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1041 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001042 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001043 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001044 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1045 /* first clear the S_IFMT bits */
1046 result->st_mode ^= (result->st_mode & 0170000);
1047 /* now set the bits that make this a symlink */
1048 result->st_mode |= 0120000;
1049 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001050
Victor Stinner8c62be82010-05-06 00:08:46 +00001051 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001052}
1053
Guido van Rossumd8faa362007-04-27 19:54:29 +00001054static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001055attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056{
Victor Stinner8c62be82010-05-06 00:08:46 +00001057 HANDLE hFindFile;
1058 WIN32_FIND_DATAA FileData;
1059 hFindFile = FindFirstFileA(pszFile, &FileData);
1060 if (hFindFile == INVALID_HANDLE_VALUE)
1061 return FALSE;
1062 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001063 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001064 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001065 info->dwFileAttributes = FileData.dwFileAttributes;
1066 info->ftCreationTime = FileData.ftCreationTime;
1067 info->ftLastAccessTime = FileData.ftLastAccessTime;
1068 info->ftLastWriteTime = FileData.ftLastWriteTime;
1069 info->nFileSizeHigh = FileData.nFileSizeHigh;
1070 info->nFileSizeLow = FileData.nFileSizeLow;
1071/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001072 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1073 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001074 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001075}
1076
1077static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001078attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001079{
Victor Stinner8c62be82010-05-06 00:08:46 +00001080 HANDLE hFindFile;
1081 WIN32_FIND_DATAW FileData;
1082 hFindFile = FindFirstFileW(pszFile, &FileData);
1083 if (hFindFile == INVALID_HANDLE_VALUE)
1084 return FALSE;
1085 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001086 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001087 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001088 info->dwFileAttributes = FileData.dwFileAttributes;
1089 info->ftCreationTime = FileData.ftCreationTime;
1090 info->ftLastAccessTime = FileData.ftLastAccessTime;
1091 info->ftLastWriteTime = FileData.ftLastWriteTime;
1092 info->nFileSizeHigh = FileData.nFileSizeHigh;
1093 info->nFileSizeLow = FileData.nFileSizeLow;
1094/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001095 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1096 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001097 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001098}
1099
Brian Curtind25aef52011-06-13 15:16:04 -05001100/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1101static int has_GetFinalPathNameByHandle = 0;
1102static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1103 DWORD);
1104static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1105 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001106static int
Brian Curtind25aef52011-06-13 15:16:04 -05001107check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001108{
Brian Curtind25aef52011-06-13 15:16:04 -05001109 HINSTANCE hKernel32;
1110 /* only recheck */
1111 if (!has_GetFinalPathNameByHandle)
1112 {
1113 hKernel32 = GetModuleHandle("KERNEL32");
1114 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1115 "GetFinalPathNameByHandleA");
1116 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1117 "GetFinalPathNameByHandleW");
1118 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1119 Py_GetFinalPathNameByHandleW;
1120 }
1121 return has_GetFinalPathNameByHandle;
1122}
1123
1124static BOOL
1125get_target_path(HANDLE hdl, wchar_t **target_path)
1126{
1127 int buf_size, result_length;
1128 wchar_t *buf;
1129
1130 /* We have a good handle to the target, use it to determine
1131 the target path name (then we'll call lstat on it). */
1132 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1133 VOLUME_NAME_DOS);
1134 if(!buf_size)
1135 return FALSE;
1136
1137 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001138 if (!buf) {
1139 SetLastError(ERROR_OUTOFMEMORY);
1140 return FALSE;
1141 }
1142
Brian Curtind25aef52011-06-13 15:16:04 -05001143 result_length = Py_GetFinalPathNameByHandleW(hdl,
1144 buf, buf_size, VOLUME_NAME_DOS);
1145
1146 if(!result_length) {
1147 free(buf);
1148 return FALSE;
1149 }
1150
1151 if(!CloseHandle(hdl)) {
1152 free(buf);
1153 return FALSE;
1154 }
1155
1156 buf[result_length] = 0;
1157
1158 *target_path = buf;
1159 return TRUE;
1160}
1161
1162static int
1163win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1164 BOOL traverse);
1165static int
1166win32_xstat_impl(const char *path, struct win32_stat *result,
1167 BOOL traverse)
1168{
Victor Stinner26de69d2011-06-17 15:15:38 +02001169 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001170 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001171 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001172 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001173 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 const char *dot;
1175
Brian Curtind25aef52011-06-13 15:16:04 -05001176 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001177 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1178 traverse reparse point. */
1179 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001180 }
1181
Brian Curtinf5e76d02010-11-24 13:14:05 +00001182 hFile = CreateFileA(
1183 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001184 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185 0, /* share mode */
1186 NULL, /* security attributes */
1187 OPEN_EXISTING,
1188 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001189 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1190 Because of this, calls like GetFinalPathNameByHandle will return
1191 the symlink path agin and not the actual final path. */
1192 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1193 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001194 NULL);
1195
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001196 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001197 /* Either the target doesn't exist, or we don't have access to
1198 get a handle to it. If the former, we need to return an error.
1199 If the latter, we can use attributes_from_dir. */
1200 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001201 return -1;
1202 /* Could not get attributes on open file. Fall back to
1203 reading the directory. */
1204 if (!attributes_from_dir(path, &info, &reparse_tag))
1205 /* Very strange. This should not fail now */
1206 return -1;
1207 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1208 if (traverse) {
1209 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001210 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001211 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001212 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001213 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001214 } else {
1215 if (!GetFileInformationByHandle(hFile, &info)) {
1216 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001217 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001218 }
1219 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001220 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1221 return -1;
1222
1223 /* Close the outer open file handle now that we're about to
1224 reopen it with different flags. */
1225 if (!CloseHandle(hFile))
1226 return -1;
1227
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001228 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001229 /* In order to call GetFinalPathNameByHandle we need to open
1230 the file without the reparse handling flag set. */
1231 hFile2 = CreateFileA(
1232 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1233 NULL, OPEN_EXISTING,
1234 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1235 NULL);
1236 if (hFile2 == INVALID_HANDLE_VALUE)
1237 return -1;
1238
1239 if (!get_target_path(hFile2, &target_path))
1240 return -1;
1241
1242 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001243 free(target_path);
1244 return code;
1245 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001246 } else
1247 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001249 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001250
1251 /* Set S_IEXEC if it is an .exe, .bat, ... */
1252 dot = strrchr(path, '.');
1253 if (dot) {
1254 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1255 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1256 result->st_mode |= 0111;
1257 }
1258 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259}
1260
1261static int
Brian Curtind25aef52011-06-13 15:16:04 -05001262win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1263 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001264{
1265 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001266 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001267 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001268 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001269 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270 const wchar_t *dot;
1271
Brian Curtind25aef52011-06-13 15:16:04 -05001272 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001273 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1274 traverse reparse point. */
1275 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001276 }
1277
Brian Curtinf5e76d02010-11-24 13:14:05 +00001278 hFile = CreateFileW(
1279 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001280 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001281 0, /* share mode */
1282 NULL, /* security attributes */
1283 OPEN_EXISTING,
1284 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001285 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1286 Because of this, calls like GetFinalPathNameByHandle will return
1287 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001288 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001289 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001290 NULL);
1291
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001292 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001293 /* Either the target doesn't exist, or we don't have access to
1294 get a handle to it. If the former, we need to return an error.
1295 If the latter, we can use attributes_from_dir. */
1296 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001297 return -1;
1298 /* Could not get attributes on open file. Fall back to
1299 reading the directory. */
1300 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1301 /* Very strange. This should not fail now */
1302 return -1;
1303 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1304 if (traverse) {
1305 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001306 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001307 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001309 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001310 } else {
1311 if (!GetFileInformationByHandle(hFile, &info)) {
1312 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001313 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001314 }
1315 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001316 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1317 return -1;
1318
1319 /* Close the outer open file handle now that we're about to
1320 reopen it with different flags. */
1321 if (!CloseHandle(hFile))
1322 return -1;
1323
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001324 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001325 /* In order to call GetFinalPathNameByHandle we need to open
1326 the file without the reparse handling flag set. */
1327 hFile2 = CreateFileW(
1328 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1329 NULL, OPEN_EXISTING,
1330 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1331 NULL);
1332 if (hFile2 == INVALID_HANDLE_VALUE)
1333 return -1;
1334
1335 if (!get_target_path(hFile2, &target_path))
1336 return -1;
1337
1338 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001339 free(target_path);
1340 return code;
1341 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001342 } else
1343 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001344 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001345 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001346
1347 /* Set S_IEXEC if it is an .exe, .bat, ... */
1348 dot = wcsrchr(path, '.');
1349 if (dot) {
1350 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1351 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1352 result->st_mode |= 0111;
1353 }
1354 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001355}
1356
1357static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001358win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001359{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001360 /* Protocol violation: we explicitly clear errno, instead of
1361 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001362 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001363 errno = 0;
1364 return code;
1365}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001366
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001367static int
1368win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1369{
1370 /* Protocol violation: we explicitly clear errno, instead of
1371 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001372 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001373 errno = 0;
1374 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001375}
Brian Curtind25aef52011-06-13 15:16:04 -05001376/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001377
1378 In Posix, stat automatically traverses symlinks and returns the stat
1379 structure for the target. In Windows, the equivalent GetFileAttributes by
1380 default does not traverse symlinks and instead returns attributes for
1381 the symlink.
1382
1383 Therefore, win32_lstat will get the attributes traditionally, and
1384 win32_stat will first explicitly resolve the symlink target and then will
1385 call win32_lstat on that result.
1386
Ezio Melotti4969f702011-03-15 05:59:46 +02001387 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001388
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001389static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001390win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001391{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001392 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001393}
1394
Victor Stinner8c62be82010-05-06 00:08:46 +00001395static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001396win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001397{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001398 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001399}
1400
1401static int
1402win32_stat(const char* path, struct win32_stat *result)
1403{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001404 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001405}
1406
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001407static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001408win32_stat_w(const wchar_t* path, struct win32_stat *result)
1409{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001410 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001411}
1412
1413static int
1414win32_fstat(int file_number, struct win32_stat *result)
1415{
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 BY_HANDLE_FILE_INFORMATION info;
1417 HANDLE h;
1418 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001419
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 /* Protocol violation: we explicitly clear errno, instead of
1423 setting it to a POSIX error. Callers should use GetLastError. */
1424 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001425
Victor Stinner8c62be82010-05-06 00:08:46 +00001426 if (h == INVALID_HANDLE_VALUE) {
1427 /* This is really a C library error (invalid file handle).
1428 We set the Win32 error to the closes one matching. */
1429 SetLastError(ERROR_INVALID_HANDLE);
1430 return -1;
1431 }
1432 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001433
Victor Stinner8c62be82010-05-06 00:08:46 +00001434 type = GetFileType(h);
1435 if (type == FILE_TYPE_UNKNOWN) {
1436 DWORD error = GetLastError();
1437 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001438 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001439 }
1440 /* else: valid but unknown file */
1441 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001442
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 if (type != FILE_TYPE_DISK) {
1444 if (type == FILE_TYPE_CHAR)
1445 result->st_mode = _S_IFCHR;
1446 else if (type == FILE_TYPE_PIPE)
1447 result->st_mode = _S_IFIFO;
1448 return 0;
1449 }
1450
1451 if (!GetFileInformationByHandle(h, &info)) {
1452 return -1;
1453 }
1454
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001455 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001456 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001457 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1458 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001459}
1460
1461#endif /* MS_WINDOWS */
1462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001463PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001464"stat_result: Result from stat or lstat.\n\n\
1465This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001466 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001467or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1468\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001469Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1470or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001471\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001473
1474static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001475 {"st_mode", "protection bits"},
1476 {"st_ino", "inode"},
1477 {"st_dev", "device"},
1478 {"st_nlink", "number of hard links"},
1479 {"st_uid", "user ID of owner"},
1480 {"st_gid", "group ID of owner"},
1481 {"st_size", "total size, in bytes"},
1482 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1483 {NULL, "integer time of last access"},
1484 {NULL, "integer time of last modification"},
1485 {NULL, "integer time of last change"},
1486 {"st_atime", "time of last access"},
1487 {"st_mtime", "time of last modification"},
1488 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001489#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001490 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001491#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001492#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001493 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001494#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001495#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001496 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001497#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001498#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001500#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001501#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001503#endif
1504#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001505 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001506#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508};
1509
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001510#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001511#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001513#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001514#endif
1515
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001516#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001517#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1518#else
1519#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1520#endif
1521
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001522#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001523#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1524#else
1525#define ST_RDEV_IDX ST_BLOCKS_IDX
1526#endif
1527
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001528#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1529#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1530#else
1531#define ST_FLAGS_IDX ST_RDEV_IDX
1532#endif
1533
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001534#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001535#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001536#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001537#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001538#endif
1539
1540#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1541#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1542#else
1543#define ST_BIRTHTIME_IDX ST_GEN_IDX
1544#endif
1545
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001546static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001547 "stat_result", /* name */
1548 stat_result__doc__, /* doc */
1549 stat_result_fields,
1550 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001551};
1552
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001554"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1555This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001556 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001557or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001558\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001560
1561static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001562 {"f_bsize", },
1563 {"f_frsize", },
1564 {"f_blocks", },
1565 {"f_bfree", },
1566 {"f_bavail", },
1567 {"f_files", },
1568 {"f_ffree", },
1569 {"f_favail", },
1570 {"f_flag", },
1571 {"f_namemax",},
1572 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001573};
1574
1575static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 "statvfs_result", /* name */
1577 statvfs_result__doc__, /* doc */
1578 statvfs_result_fields,
1579 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001580};
1581
Ross Lagerwall7807c352011-03-17 20:20:30 +02001582#if defined(HAVE_WAITID) && !defined(__APPLE__)
1583PyDoc_STRVAR(waitid_result__doc__,
1584"waitid_result: Result from waitid.\n\n\
1585This object may be accessed either as a tuple of\n\
1586 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1587or via the attributes si_pid, si_uid, and so on.\n\
1588\n\
1589See os.waitid for more information.");
1590
1591static PyStructSequence_Field waitid_result_fields[] = {
1592 {"si_pid", },
1593 {"si_uid", },
1594 {"si_signo", },
1595 {"si_status", },
1596 {"si_code", },
1597 {0}
1598};
1599
1600static PyStructSequence_Desc waitid_result_desc = {
1601 "waitid_result", /* name */
1602 waitid_result__doc__, /* doc */
1603 waitid_result_fields,
1604 5
1605};
1606static PyTypeObject WaitidResultType;
1607#endif
1608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001610static PyTypeObject StatResultType;
1611static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001612#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001613static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001614#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001615static newfunc structseq_new;
1616
1617static PyObject *
1618statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1619{
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 PyStructSequence *result;
1621 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001622
Victor Stinner8c62be82010-05-06 00:08:46 +00001623 result = (PyStructSequence*)structseq_new(type, args, kwds);
1624 if (!result)
1625 return NULL;
1626 /* If we have been initialized from a tuple,
1627 st_?time might be set to None. Initialize it
1628 from the int slots. */
1629 for (i = 7; i <= 9; i++) {
1630 if (result->ob_item[i+3] == Py_None) {
1631 Py_DECREF(Py_None);
1632 Py_INCREF(result->ob_item[i]);
1633 result->ob_item[i+3] = result->ob_item[i];
1634 }
1635 }
1636 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001637}
1638
1639
1640
1641/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001642static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001643
1644PyDoc_STRVAR(stat_float_times__doc__,
1645"stat_float_times([newval]) -> oldval\n\n\
1646Determine whether os.[lf]stat represents time stamps as float objects.\n\
1647If newval is True, future calls to stat() return floats, if it is False,\n\
1648future calls return ints. \n\
1649If newval is omitted, return the current setting.\n");
1650
1651static PyObject*
1652stat_float_times(PyObject* self, PyObject *args)
1653{
Victor Stinner8c62be82010-05-06 00:08:46 +00001654 int newval = -1;
1655 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1656 return NULL;
1657 if (newval == -1)
1658 /* Return old value */
1659 return PyBool_FromLong(_stat_float_times);
1660 _stat_float_times = newval;
1661 Py_INCREF(Py_None);
1662 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001663}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001664
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001665static void
1666fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1667{
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001669#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001671#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001673#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001674 if (!ival)
1675 return;
1676 if (_stat_float_times) {
1677 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1678 } else {
1679 fval = ival;
1680 Py_INCREF(fval);
1681 }
1682 PyStructSequence_SET_ITEM(v, index, ival);
1683 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001684}
1685
Tim Peters5aa91602002-01-30 05:46:57 +00001686/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001687 (used by posix_stat() and posix_fstat()) */
1688static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001689_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001690{
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 unsigned long ansec, mnsec, cnsec;
1692 PyObject *v = PyStructSequence_New(&StatResultType);
1693 if (v == NULL)
1694 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001695
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001697#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 PyStructSequence_SET_ITEM(v, 1,
1699 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001700#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001701 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001702#endif
1703#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001704 PyStructSequence_SET_ITEM(v, 2,
1705 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001706#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001707 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001708#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1710 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1711 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001712#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 PyStructSequence_SET_ITEM(v, 6,
1714 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001716 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001717#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001718
Martin v. Löwis14694662006-02-03 12:54:16 +00001719#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001720 ansec = st->st_atim.tv_nsec;
1721 mnsec = st->st_mtim.tv_nsec;
1722 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001723#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001724 ansec = st->st_atimespec.tv_nsec;
1725 mnsec = st->st_mtimespec.tv_nsec;
1726 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001727#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 ansec = st->st_atime_nsec;
1729 mnsec = st->st_mtime_nsec;
1730 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001731#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001733#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001734 fill_time(v, 7, st->st_atime, ansec);
1735 fill_time(v, 8, st->st_mtime, mnsec);
1736 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001737
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001738#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1740 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001741#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001742#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001743 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1744 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001745#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001746#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001747 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1748 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001749#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001750#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001751 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1752 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001753#endif
1754#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001755 {
1756 PyObject *val;
1757 unsigned long bsec,bnsec;
1758 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001759#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001761#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001763#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 if (_stat_float_times) {
1765 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1766 } else {
1767 val = PyLong_FromLong((long)bsec);
1768 }
1769 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1770 val);
1771 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001772#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001773#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001774 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1775 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001776#endif
Fred Drake699f3522000-06-29 21:12:41 +00001777
Victor Stinner8c62be82010-05-06 00:08:46 +00001778 if (PyErr_Occurred()) {
1779 Py_DECREF(v);
1780 return NULL;
1781 }
Fred Drake699f3522000-06-29 21:12:41 +00001782
Victor Stinner8c62be82010-05-06 00:08:46 +00001783 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001784}
1785
Barry Warsaw53699e91996-12-10 23:23:01 +00001786static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001787posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001789#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001793#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 char *wformat,
1795 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001796{
Victor Stinner8c62be82010-05-06 00:08:46 +00001797 STRUCT_STAT st;
1798 PyObject *opath;
1799 char *path;
1800 int res;
1801 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001802
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001803#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001804 PyUnicodeObject *po;
1805 if (PyArg_ParseTuple(args, wformat, &po)) {
1806 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001807
Victor Stinner8c62be82010-05-06 00:08:46 +00001808 Py_BEGIN_ALLOW_THREADS
1809 /* PyUnicode_AS_UNICODE result OK without
1810 thread lock as it is a simple dereference. */
1811 res = wstatfunc(wpath, &st);
1812 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001813
Victor Stinner8c62be82010-05-06 00:08:46 +00001814 if (res != 0)
1815 return win32_error_unicode("stat", wpath);
1816 return _pystat_fromstructstat(&st);
1817 }
1818 /* Drop the argument parsing error as narrow strings
1819 are also valid. */
1820 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001821#endif
1822
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 if (!PyArg_ParseTuple(args, format,
1824 PyUnicode_FSConverter, &opath))
1825 return NULL;
1826 path = PyBytes_AsString(opath);
1827 Py_BEGIN_ALLOW_THREADS
1828 res = (*statfunc)(path, &st);
1829 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001830
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001832#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001834#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001836#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 }
1838 else
1839 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001840
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 Py_DECREF(opath);
1842 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001843}
1844
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845/* POSIX methods */
1846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001848"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001849Use the real uid/gid to test for access to a path. Note that most\n\
1850operations will use the effective uid/gid, therefore this routine can\n\
1851be used in a suid/sgid environment to test if the invoking user has the\n\
1852specified access to the path. The mode argument can be F_OK to test\n\
1853existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001854
1855static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001856posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001857{
Victor Stinner8c62be82010-05-06 00:08:46 +00001858 PyObject *opath;
1859 char *path;
1860 int mode;
1861
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001862#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001863 DWORD attr;
1864 PyUnicodeObject *po;
1865 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1866 Py_BEGIN_ALLOW_THREADS
1867 /* PyUnicode_AS_UNICODE OK without thread lock as
1868 it is a simple dereference. */
1869 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1870 Py_END_ALLOW_THREADS
1871 goto finish;
1872 }
1873 /* Drop the argument parsing error as narrow strings
1874 are also valid. */
1875 PyErr_Clear();
1876 if (!PyArg_ParseTuple(args, "O&i:access",
1877 PyUnicode_FSConverter, &opath, &mode))
1878 return NULL;
1879 path = PyBytes_AsString(opath);
1880 Py_BEGIN_ALLOW_THREADS
1881 attr = GetFileAttributesA(path);
1882 Py_END_ALLOW_THREADS
1883 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001884finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 if (attr == 0xFFFFFFFF)
1886 /* File does not exist, or cannot read attributes */
1887 return PyBool_FromLong(0);
1888 /* Access is possible if either write access wasn't requested, or
1889 the file isn't read-only, or if it's a directory, as there are
1890 no read-only directories on Windows. */
1891 return PyBool_FromLong(!(mode & 2)
1892 || !(attr & FILE_ATTRIBUTE_READONLY)
1893 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001894#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001895 int res;
1896 if (!PyArg_ParseTuple(args, "O&i:access",
1897 PyUnicode_FSConverter, &opath, &mode))
1898 return NULL;
1899 path = PyBytes_AsString(opath);
1900 Py_BEGIN_ALLOW_THREADS
1901 res = access(path, mode);
1902 Py_END_ALLOW_THREADS
1903 Py_DECREF(opath);
1904 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001905#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001906}
1907
Guido van Rossumd371ff11999-01-25 16:12:23 +00001908#ifndef F_OK
1909#define F_OK 0
1910#endif
1911#ifndef R_OK
1912#define R_OK 4
1913#endif
1914#ifndef W_OK
1915#define W_OK 2
1916#endif
1917#ifndef X_OK
1918#define X_OK 1
1919#endif
1920
1921#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001923"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001925
1926static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001927posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001928{
Victor Stinner8c62be82010-05-06 00:08:46 +00001929 int id;
1930 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001931
Victor Stinner8c62be82010-05-06 00:08:46 +00001932 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1933 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001934
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001935#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001936 /* file descriptor 0 only, the default input device (stdin) */
1937 if (id == 0) {
1938 ret = ttyname();
1939 }
1940 else {
1941 ret = NULL;
1942 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001943#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001945#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 if (ret == NULL)
1947 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001948 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001949}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001950#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001951
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001952#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001954"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001956
1957static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001958posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001959{
Victor Stinner8c62be82010-05-06 00:08:46 +00001960 char *ret;
1961 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001962
Greg Wardb48bc172000-03-01 21:51:56 +00001963#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001965#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001967#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001968 if (ret == NULL)
1969 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001970 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001971}
1972#endif
1973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001975"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001976Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001977
Barry Warsaw53699e91996-12-10 23:23:01 +00001978static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001979posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001980{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001982 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001983#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001984 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001985#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001987#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001988 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001989#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001990}
1991
Fred Drake4d1e64b2002-04-15 19:40:07 +00001992#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001993PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001994"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001995Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001997
1998static PyObject *
1999posix_fchdir(PyObject *self, PyObject *fdobj)
2000{
Victor Stinner8c62be82010-05-06 00:08:46 +00002001 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002002}
2003#endif /* HAVE_FCHDIR */
2004
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002005
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002007"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002008Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002009
Barry Warsaw53699e91996-12-10 23:23:01 +00002010static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002011posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002012{
Victor Stinner8c62be82010-05-06 00:08:46 +00002013 PyObject *opath = NULL;
2014 char *path = NULL;
2015 int i;
2016 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002017#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002018 DWORD attr;
2019 PyUnicodeObject *po;
2020 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2021 Py_BEGIN_ALLOW_THREADS
2022 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2023 if (attr != 0xFFFFFFFF) {
2024 if (i & _S_IWRITE)
2025 attr &= ~FILE_ATTRIBUTE_READONLY;
2026 else
2027 attr |= FILE_ATTRIBUTE_READONLY;
2028 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2029 }
2030 else
2031 res = 0;
2032 Py_END_ALLOW_THREADS
2033 if (!res)
2034 return win32_error_unicode("chmod",
2035 PyUnicode_AS_UNICODE(po));
2036 Py_INCREF(Py_None);
2037 return Py_None;
2038 }
2039 /* Drop the argument parsing error as narrow strings
2040 are also valid. */
2041 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002042
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2044 &opath, &i))
2045 return NULL;
2046 path = PyBytes_AsString(opath);
2047 Py_BEGIN_ALLOW_THREADS
2048 attr = GetFileAttributesA(path);
2049 if (attr != 0xFFFFFFFF) {
2050 if (i & _S_IWRITE)
2051 attr &= ~FILE_ATTRIBUTE_READONLY;
2052 else
2053 attr |= FILE_ATTRIBUTE_READONLY;
2054 res = SetFileAttributesA(path, attr);
2055 }
2056 else
2057 res = 0;
2058 Py_END_ALLOW_THREADS
2059 if (!res) {
2060 win32_error("chmod", path);
2061 Py_DECREF(opath);
2062 return NULL;
2063 }
2064 Py_DECREF(opath);
2065 Py_INCREF(Py_None);
2066 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002067#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002068 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2069 &opath, &i))
2070 return NULL;
2071 path = PyBytes_AsString(opath);
2072 Py_BEGIN_ALLOW_THREADS
2073 res = chmod(path, i);
2074 Py_END_ALLOW_THREADS
2075 if (res < 0)
2076 return posix_error_with_allocated_filename(opath);
2077 Py_DECREF(opath);
2078 Py_INCREF(Py_None);
2079 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002081}
2082
Christian Heimes4e30a842007-11-30 22:12:06 +00002083#ifdef HAVE_FCHMOD
2084PyDoc_STRVAR(posix_fchmod__doc__,
2085"fchmod(fd, mode)\n\n\
2086Change the access permissions of the file given by file\n\
2087descriptor fd.");
2088
2089static PyObject *
2090posix_fchmod(PyObject *self, PyObject *args)
2091{
Victor Stinner8c62be82010-05-06 00:08:46 +00002092 int fd, mode, res;
2093 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2094 return NULL;
2095 Py_BEGIN_ALLOW_THREADS
2096 res = fchmod(fd, mode);
2097 Py_END_ALLOW_THREADS
2098 if (res < 0)
2099 return posix_error();
2100 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002101}
2102#endif /* HAVE_FCHMOD */
2103
2104#ifdef HAVE_LCHMOD
2105PyDoc_STRVAR(posix_lchmod__doc__,
2106"lchmod(path, mode)\n\n\
2107Change the access permissions of a file. If path is a symlink, this\n\
2108affects the link itself rather than the target.");
2109
2110static PyObject *
2111posix_lchmod(PyObject *self, PyObject *args)
2112{
Victor Stinner8c62be82010-05-06 00:08:46 +00002113 PyObject *opath;
2114 char *path;
2115 int i;
2116 int res;
2117 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2118 &opath, &i))
2119 return NULL;
2120 path = PyBytes_AsString(opath);
2121 Py_BEGIN_ALLOW_THREADS
2122 res = lchmod(path, i);
2123 Py_END_ALLOW_THREADS
2124 if (res < 0)
2125 return posix_error_with_allocated_filename(opath);
2126 Py_DECREF(opath);
2127 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002128}
2129#endif /* HAVE_LCHMOD */
2130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002131
Thomas Wouterscf297e42007-02-23 15:07:44 +00002132#ifdef HAVE_CHFLAGS
2133PyDoc_STRVAR(posix_chflags__doc__,
2134"chflags(path, flags)\n\n\
2135Set file flags.");
2136
2137static PyObject *
2138posix_chflags(PyObject *self, PyObject *args)
2139{
Victor Stinner8c62be82010-05-06 00:08:46 +00002140 PyObject *opath;
2141 char *path;
2142 unsigned long flags;
2143 int res;
2144 if (!PyArg_ParseTuple(args, "O&k:chflags",
2145 PyUnicode_FSConverter, &opath, &flags))
2146 return NULL;
2147 path = PyBytes_AsString(opath);
2148 Py_BEGIN_ALLOW_THREADS
2149 res = chflags(path, flags);
2150 Py_END_ALLOW_THREADS
2151 if (res < 0)
2152 return posix_error_with_allocated_filename(opath);
2153 Py_DECREF(opath);
2154 Py_INCREF(Py_None);
2155 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002156}
2157#endif /* HAVE_CHFLAGS */
2158
2159#ifdef HAVE_LCHFLAGS
2160PyDoc_STRVAR(posix_lchflags__doc__,
2161"lchflags(path, flags)\n\n\
2162Set file flags.\n\
2163This function will not follow symbolic links.");
2164
2165static PyObject *
2166posix_lchflags(PyObject *self, PyObject *args)
2167{
Victor Stinner8c62be82010-05-06 00:08:46 +00002168 PyObject *opath;
2169 char *path;
2170 unsigned long flags;
2171 int res;
2172 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2173 PyUnicode_FSConverter, &opath, &flags))
2174 return NULL;
2175 path = PyBytes_AsString(opath);
2176 Py_BEGIN_ALLOW_THREADS
2177 res = lchflags(path, flags);
2178 Py_END_ALLOW_THREADS
2179 if (res < 0)
2180 return posix_error_with_allocated_filename(opath);
2181 Py_DECREF(opath);
2182 Py_INCREF(Py_None);
2183 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002184}
2185#endif /* HAVE_LCHFLAGS */
2186
Martin v. Löwis244edc82001-10-04 22:44:26 +00002187#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002188PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002189"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002190Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002191
2192static PyObject *
2193posix_chroot(PyObject *self, PyObject *args)
2194{
Victor Stinner8c62be82010-05-06 00:08:46 +00002195 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002196}
2197#endif
2198
Guido van Rossum21142a01999-01-08 21:05:37 +00002199#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002201"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002202force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002203
2204static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002205posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002206{
Stefan Krah0e803b32010-11-26 16:16:47 +00002207 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002208}
2209#endif /* HAVE_FSYNC */
2210
Ross Lagerwall7807c352011-03-17 20:20:30 +02002211#ifdef HAVE_SYNC
2212PyDoc_STRVAR(posix_sync__doc__,
2213"sync()\n\n\
2214Force write of everything to disk.");
2215
2216static PyObject *
2217posix_sync(PyObject *self, PyObject *noargs)
2218{
2219 Py_BEGIN_ALLOW_THREADS
2220 sync();
2221 Py_END_ALLOW_THREADS
2222 Py_RETURN_NONE;
2223}
2224#endif
2225
Guido van Rossum21142a01999-01-08 21:05:37 +00002226#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002227
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002228#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002229extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2230#endif
2231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002233"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002234force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002235 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002236
2237static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002238posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002239{
Stefan Krah0e803b32010-11-26 16:16:47 +00002240 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002241}
2242#endif /* HAVE_FDATASYNC */
2243
2244
Fredrik Lundh10723342000-07-10 16:38:09 +00002245#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002246PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002247"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002248Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002249
Barry Warsaw53699e91996-12-10 23:23:01 +00002250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002251posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002252{
Victor Stinner8c62be82010-05-06 00:08:46 +00002253 PyObject *opath;
2254 char *path;
2255 long uid, gid;
2256 int res;
2257 if (!PyArg_ParseTuple(args, "O&ll:chown",
2258 PyUnicode_FSConverter, &opath,
2259 &uid, &gid))
2260 return NULL;
2261 path = PyBytes_AsString(opath);
2262 Py_BEGIN_ALLOW_THREADS
2263 res = chown(path, (uid_t) uid, (gid_t) gid);
2264 Py_END_ALLOW_THREADS
2265 if (res < 0)
2266 return posix_error_with_allocated_filename(opath);
2267 Py_DECREF(opath);
2268 Py_INCREF(Py_None);
2269 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002271#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272
Christian Heimes4e30a842007-11-30 22:12:06 +00002273#ifdef HAVE_FCHOWN
2274PyDoc_STRVAR(posix_fchown__doc__,
2275"fchown(fd, uid, gid)\n\n\
2276Change the owner and group id of the file given by file descriptor\n\
2277fd to the numeric uid and gid.");
2278
2279static PyObject *
2280posix_fchown(PyObject *self, PyObject *args)
2281{
Victor Stinner8c62be82010-05-06 00:08:46 +00002282 int fd;
2283 long uid, gid;
2284 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002285 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002286 return NULL;
2287 Py_BEGIN_ALLOW_THREADS
2288 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2289 Py_END_ALLOW_THREADS
2290 if (res < 0)
2291 return posix_error();
2292 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002293}
2294#endif /* HAVE_FCHOWN */
2295
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002296#ifdef HAVE_LCHOWN
2297PyDoc_STRVAR(posix_lchown__doc__,
2298"lchown(path, uid, gid)\n\n\
2299Change the owner and group id of path to the numeric uid and gid.\n\
2300This function will not follow symbolic links.");
2301
2302static PyObject *
2303posix_lchown(PyObject *self, PyObject *args)
2304{
Victor Stinner8c62be82010-05-06 00:08:46 +00002305 PyObject *opath;
2306 char *path;
2307 long uid, gid;
2308 int res;
2309 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2310 PyUnicode_FSConverter, &opath,
2311 &uid, &gid))
2312 return NULL;
2313 path = PyBytes_AsString(opath);
2314 Py_BEGIN_ALLOW_THREADS
2315 res = lchown(path, (uid_t) uid, (gid_t) gid);
2316 Py_END_ALLOW_THREADS
2317 if (res < 0)
2318 return posix_error_with_allocated_filename(opath);
2319 Py_DECREF(opath);
2320 Py_INCREF(Py_None);
2321 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002322}
2323#endif /* HAVE_LCHOWN */
2324
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002325
Guido van Rossum36bc6801995-06-14 22:54:23 +00002326#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002327static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002328posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002329{
Victor Stinner8c62be82010-05-06 00:08:46 +00002330 char buf[1026];
2331 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002332
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002333#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002334 if (!use_bytes) {
2335 wchar_t wbuf[1026];
2336 wchar_t *wbuf2 = wbuf;
2337 PyObject *resobj;
2338 DWORD len;
2339 Py_BEGIN_ALLOW_THREADS
2340 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2341 /* If the buffer is large enough, len does not include the
2342 terminating \0. If the buffer is too small, len includes
2343 the space needed for the terminator. */
2344 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2345 wbuf2 = malloc(len * sizeof(wchar_t));
2346 if (wbuf2)
2347 len = GetCurrentDirectoryW(len, wbuf2);
2348 }
2349 Py_END_ALLOW_THREADS
2350 if (!wbuf2) {
2351 PyErr_NoMemory();
2352 return NULL;
2353 }
2354 if (!len) {
2355 if (wbuf2 != wbuf) free(wbuf2);
2356 return win32_error("getcwdu", NULL);
2357 }
2358 resobj = PyUnicode_FromWideChar(wbuf2, len);
2359 if (wbuf2 != wbuf) free(wbuf2);
2360 return resobj;
2361 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002362#endif
2363
Victor Stinner8c62be82010-05-06 00:08:46 +00002364 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002365#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002367#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002369#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002370 Py_END_ALLOW_THREADS
2371 if (res == NULL)
2372 return posix_error();
2373 if (use_bytes)
2374 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002375 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002376}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002377
2378PyDoc_STRVAR(posix_getcwd__doc__,
2379"getcwd() -> path\n\n\
2380Return a unicode string representing the current working directory.");
2381
2382static PyObject *
2383posix_getcwd_unicode(PyObject *self)
2384{
2385 return posix_getcwd(0);
2386}
2387
2388PyDoc_STRVAR(posix_getcwdb__doc__,
2389"getcwdb() -> path\n\n\
2390Return a bytes string representing the current working directory.");
2391
2392static PyObject *
2393posix_getcwd_bytes(PyObject *self)
2394{
2395 return posix_getcwd(1);
2396}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002397#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002398
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002399
Guido van Rossumb6775db1994-08-01 11:34:53 +00002400#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002401PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002402"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002403Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002404
Barry Warsaw53699e91996-12-10 23:23:01 +00002405static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002406posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002407{
Victor Stinner8c62be82010-05-06 00:08:46 +00002408 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002410#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002411
Brian Curtin1b9df392010-11-24 20:24:31 +00002412#ifdef MS_WINDOWS
2413PyDoc_STRVAR(win32_link__doc__,
2414"link(src, dst)\n\n\
2415Create a hard link to a file.");
2416
2417static PyObject *
2418win32_link(PyObject *self, PyObject *args)
2419{
2420 PyObject *osrc, *odst;
2421 char *src, *dst;
2422 BOOL rslt;
2423
Brian Curtinfc889c42010-11-28 23:59:46 +00002424 PyUnicodeObject *usrc, *udst;
2425 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2426 Py_BEGIN_ALLOW_THREADS
2427 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2428 PyUnicode_AS_UNICODE(usrc), NULL);
2429 Py_END_ALLOW_THREADS
2430
2431 if (rslt == 0)
2432 return win32_error("link", NULL);
2433
2434 Py_RETURN_NONE;
2435 }
2436
2437 /* Narrow strings also valid. */
2438 PyErr_Clear();
2439
Brian Curtin1b9df392010-11-24 20:24:31 +00002440 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2441 PyUnicode_FSConverter, &odst))
2442 return NULL;
2443
2444 src = PyBytes_AsString(osrc);
2445 dst = PyBytes_AsString(odst);
2446
2447 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002448 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002449 Py_END_ALLOW_THREADS
2450
Stefan Krah30b341f2010-11-27 11:44:18 +00002451 Py_DECREF(osrc);
2452 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002453 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002454 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002455
2456 Py_RETURN_NONE;
2457}
2458#endif /* MS_WINDOWS */
2459
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002462"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002463Return a list containing the names of the entries in the directory.\n\
2464\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002465 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002466\n\
2467The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002469
Barry Warsaw53699e91996-12-10 23:23:01 +00002470static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002471posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002472{
Victor Stinner8c62be82010-05-06 00:08:46 +00002473 /* XXX Should redo this putting the (now four) versions of opendir
2474 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002475#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002476
Victor Stinner8c62be82010-05-06 00:08:46 +00002477 PyObject *d, *v;
2478 HANDLE hFindFile;
2479 BOOL result;
2480 WIN32_FIND_DATA FileData;
2481 PyObject *opath;
2482 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2483 char *bufptr = namebuf;
2484 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002485
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002486 PyObject *po = NULL;
2487 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002488 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002489 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002490
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002491 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002492 po_wchars = L".";
2493 len = 1;
2494 } else {
2495 po_wchars = PyUnicode_AS_UNICODE(po);
2496 len = PyUnicode_GET_SIZE(po);
2497 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002498 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002499 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2500 if (!wnamebuf) {
2501 PyErr_NoMemory();
2502 return NULL;
2503 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002504 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002505 if (len > 0) {
2506 Py_UNICODE wch = wnamebuf[len-1];
2507 if (wch != L'/' && wch != L'\\' && wch != L':')
2508 wnamebuf[len++] = L'\\';
2509 wcscpy(wnamebuf + len, L"*.*");
2510 }
2511 if ((d = PyList_New(0)) == NULL) {
2512 free(wnamebuf);
2513 return NULL;
2514 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002515 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002516 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002517 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002518 if (hFindFile == INVALID_HANDLE_VALUE) {
2519 int error = GetLastError();
2520 if (error == ERROR_FILE_NOT_FOUND) {
2521 free(wnamebuf);
2522 return d;
2523 }
2524 Py_DECREF(d);
2525 win32_error_unicode("FindFirstFileW", wnamebuf);
2526 free(wnamebuf);
2527 return NULL;
2528 }
2529 do {
2530 /* Skip over . and .. */
2531 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2532 wcscmp(wFileData.cFileName, L"..") != 0) {
2533 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2534 if (v == NULL) {
2535 Py_DECREF(d);
2536 d = NULL;
2537 break;
2538 }
2539 if (PyList_Append(d, v) != 0) {
2540 Py_DECREF(v);
2541 Py_DECREF(d);
2542 d = NULL;
2543 break;
2544 }
2545 Py_DECREF(v);
2546 }
2547 Py_BEGIN_ALLOW_THREADS
2548 result = FindNextFileW(hFindFile, &wFileData);
2549 Py_END_ALLOW_THREADS
2550 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2551 it got to the end of the directory. */
2552 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2553 Py_DECREF(d);
2554 win32_error_unicode("FindNextFileW", wnamebuf);
2555 FindClose(hFindFile);
2556 free(wnamebuf);
2557 return NULL;
2558 }
2559 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002560
Victor Stinner8c62be82010-05-06 00:08:46 +00002561 if (FindClose(hFindFile) == FALSE) {
2562 Py_DECREF(d);
2563 win32_error_unicode("FindClose", wnamebuf);
2564 free(wnamebuf);
2565 return NULL;
2566 }
2567 free(wnamebuf);
2568 return d;
2569 }
2570 /* Drop the argument parsing error as narrow strings
2571 are also valid. */
2572 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002573
Victor Stinner8c62be82010-05-06 00:08:46 +00002574 if (!PyArg_ParseTuple(args, "O&:listdir",
2575 PyUnicode_FSConverter, &opath))
2576 return NULL;
2577 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2578 PyErr_SetString(PyExc_ValueError, "path too long");
2579 Py_DECREF(opath);
2580 return NULL;
2581 }
2582 strcpy(namebuf, PyBytes_AsString(opath));
2583 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002584 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002585 if (len > 0) {
2586 char ch = namebuf[len-1];
2587 if (ch != SEP && ch != ALTSEP && ch != ':')
2588 namebuf[len++] = '/';
2589 strcpy(namebuf + len, "*.*");
2590 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002591
Victor Stinner8c62be82010-05-06 00:08:46 +00002592 if ((d = PyList_New(0)) == NULL)
2593 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002594
Antoine Pitroub73caab2010-08-09 23:39:31 +00002595 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002596 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002597 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002598 if (hFindFile == INVALID_HANDLE_VALUE) {
2599 int error = GetLastError();
2600 if (error == ERROR_FILE_NOT_FOUND)
2601 return d;
2602 Py_DECREF(d);
2603 return win32_error("FindFirstFile", namebuf);
2604 }
2605 do {
2606 /* Skip over . and .. */
2607 if (strcmp(FileData.cFileName, ".") != 0 &&
2608 strcmp(FileData.cFileName, "..") != 0) {
2609 v = PyBytes_FromString(FileData.cFileName);
2610 if (v == NULL) {
2611 Py_DECREF(d);
2612 d = NULL;
2613 break;
2614 }
2615 if (PyList_Append(d, v) != 0) {
2616 Py_DECREF(v);
2617 Py_DECREF(d);
2618 d = NULL;
2619 break;
2620 }
2621 Py_DECREF(v);
2622 }
2623 Py_BEGIN_ALLOW_THREADS
2624 result = FindNextFile(hFindFile, &FileData);
2625 Py_END_ALLOW_THREADS
2626 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2627 it got to the end of the directory. */
2628 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2629 Py_DECREF(d);
2630 win32_error("FindNextFile", namebuf);
2631 FindClose(hFindFile);
2632 return NULL;
2633 }
2634 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002635
Victor Stinner8c62be82010-05-06 00:08:46 +00002636 if (FindClose(hFindFile) == FALSE) {
2637 Py_DECREF(d);
2638 return win32_error("FindClose", namebuf);
2639 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002640
Victor Stinner8c62be82010-05-06 00:08:46 +00002641 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002642
Tim Peters0bb44a42000-09-15 07:44:49 +00002643#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002644
2645#ifndef MAX_PATH
2646#define MAX_PATH CCHMAXPATH
2647#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002648 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002649 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002650 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002651 PyObject *d, *v;
2652 char namebuf[MAX_PATH+5];
2653 HDIR hdir = 1;
2654 ULONG srchcnt = 1;
2655 FILEFINDBUF3 ep;
2656 APIRET rc;
2657
Victor Stinner8c62be82010-05-06 00:08:46 +00002658 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002659 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002660 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002661 name = PyBytes_AsString(oname);
2662 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002663 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002664 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002665 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002666 return NULL;
2667 }
2668 strcpy(namebuf, name);
2669 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002670 if (*pt == ALTSEP)
2671 *pt = SEP;
2672 if (namebuf[len-1] != SEP)
2673 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002674 strcpy(namebuf + len, "*.*");
2675
Neal Norwitz6c913782007-10-14 03:23:09 +00002676 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002677 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002678 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002679 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002680
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002681 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2682 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002683 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002684 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2685 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2686 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002687
2688 if (rc != NO_ERROR) {
2689 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002690 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002691 }
2692
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002693 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002694 do {
2695 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002696 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002697 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002698
2699 strcpy(namebuf, ep.achName);
2700
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002701 /* Leave Case of Name Alone -- In Native Form */
2702 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002703
Christian Heimes72b710a2008-05-26 13:28:38 +00002704 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002705 if (v == NULL) {
2706 Py_DECREF(d);
2707 d = NULL;
2708 break;
2709 }
2710 if (PyList_Append(d, v) != 0) {
2711 Py_DECREF(v);
2712 Py_DECREF(d);
2713 d = NULL;
2714 break;
2715 }
2716 Py_DECREF(v);
2717 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2718 }
2719
Victor Stinnerdcb24032010-04-22 12:08:36 +00002720 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002721 return d;
2722#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002723 PyObject *oname;
2724 char *name;
2725 PyObject *d, *v;
2726 DIR *dirp;
2727 struct dirent *ep;
2728 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002729
Victor Stinner8c62be82010-05-06 00:08:46 +00002730 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002731 /* v is never read, so it does not need to be initialized yet. */
2732 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002733 arg_is_unicode = 0;
2734 PyErr_Clear();
2735 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002736 oname = NULL;
2737 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002738 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002739 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002740 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002741 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002742 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002743 Py_BEGIN_ALLOW_THREADS
2744 dirp = opendir(name);
2745 Py_END_ALLOW_THREADS
2746 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002747 return posix_error_with_allocated_filename(oname);
2748 }
2749 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002750 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002752 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002753 Py_DECREF(oname);
2754 return NULL;
2755 }
2756 for (;;) {
2757 errno = 0;
2758 Py_BEGIN_ALLOW_THREADS
2759 ep = readdir(dirp);
2760 Py_END_ALLOW_THREADS
2761 if (ep == NULL) {
2762 if (errno == 0) {
2763 break;
2764 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002765 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002766 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002767 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002768 Py_DECREF(d);
2769 return posix_error_with_allocated_filename(oname);
2770 }
2771 }
2772 if (ep->d_name[0] == '.' &&
2773 (NAMLEN(ep) == 1 ||
2774 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2775 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002776 if (arg_is_unicode)
2777 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2778 else
2779 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002781 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 break;
2783 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002784 if (PyList_Append(d, v) != 0) {
2785 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002786 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002787 break;
2788 }
2789 Py_DECREF(v);
2790 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002791 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002792 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002793 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002795
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002797
Tim Peters0bb44a42000-09-15 07:44:49 +00002798#endif /* which OS */
2799} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002800
Antoine Pitrou8250e232011-02-25 23:41:16 +00002801#ifdef HAVE_FDOPENDIR
2802PyDoc_STRVAR(posix_fdlistdir__doc__,
2803"fdlistdir(fd) -> list_of_strings\n\n\
2804Like listdir(), but uses a file descriptor instead.\n\
2805After succesful execution of this function, fd will be closed.");
2806
2807static PyObject *
2808posix_fdlistdir(PyObject *self, PyObject *args)
2809{
2810 PyObject *d, *v;
2811 DIR *dirp;
2812 struct dirent *ep;
2813 int fd;
2814
2815 errno = 0;
2816 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2817 return NULL;
2818 Py_BEGIN_ALLOW_THREADS
2819 dirp = fdopendir(fd);
2820 Py_END_ALLOW_THREADS
2821 if (dirp == NULL) {
2822 close(fd);
2823 return posix_error();
2824 }
2825 if ((d = PyList_New(0)) == NULL) {
2826 Py_BEGIN_ALLOW_THREADS
2827 closedir(dirp);
2828 Py_END_ALLOW_THREADS
2829 return NULL;
2830 }
2831 for (;;) {
2832 errno = 0;
2833 Py_BEGIN_ALLOW_THREADS
2834 ep = readdir(dirp);
2835 Py_END_ALLOW_THREADS
2836 if (ep == NULL) {
2837 if (errno == 0) {
2838 break;
2839 } else {
2840 Py_BEGIN_ALLOW_THREADS
2841 closedir(dirp);
2842 Py_END_ALLOW_THREADS
2843 Py_DECREF(d);
2844 return posix_error();
2845 }
2846 }
2847 if (ep->d_name[0] == '.' &&
2848 (NAMLEN(ep) == 1 ||
2849 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2850 continue;
2851 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2852 if (v == NULL) {
2853 Py_CLEAR(d);
2854 break;
2855 }
2856 if (PyList_Append(d, v) != 0) {
2857 Py_DECREF(v);
2858 Py_CLEAR(d);
2859 break;
2860 }
2861 Py_DECREF(v);
2862 }
2863 Py_BEGIN_ALLOW_THREADS
2864 closedir(dirp);
2865 Py_END_ALLOW_THREADS
2866
2867 return d;
2868}
2869#endif
2870
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002871#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002872/* A helper function for abspath on win32 */
2873static PyObject *
2874posix__getfullpathname(PyObject *self, PyObject *args)
2875{
Victor Stinner8c62be82010-05-06 00:08:46 +00002876 PyObject *opath;
2877 char *path;
2878 char outbuf[MAX_PATH*2];
2879 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002880#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002881 PyUnicodeObject *po;
2882 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2883 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2884 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2885 Py_UNICODE *wtemp;
2886 DWORD result;
2887 PyObject *v;
2888 result = GetFullPathNameW(wpath,
2889 sizeof(woutbuf)/sizeof(woutbuf[0]),
2890 woutbuf, &wtemp);
2891 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2892 woutbufp = malloc(result * sizeof(Py_UNICODE));
2893 if (!woutbufp)
2894 return PyErr_NoMemory();
2895 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2896 }
2897 if (result)
2898 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2899 else
2900 v = win32_error_unicode("GetFullPathNameW", wpath);
2901 if (woutbufp != woutbuf)
2902 free(woutbufp);
2903 return v;
2904 }
2905 /* Drop the argument parsing error as narrow strings
2906 are also valid. */
2907 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002908
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002909#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002910 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2911 PyUnicode_FSConverter, &opath))
2912 return NULL;
2913 path = PyBytes_AsString(opath);
2914 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2915 outbuf, &temp)) {
2916 win32_error("GetFullPathName", path);
2917 Py_DECREF(opath);
2918 return NULL;
2919 }
2920 Py_DECREF(opath);
2921 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2922 return PyUnicode_Decode(outbuf, strlen(outbuf),
2923 Py_FileSystemDefaultEncoding, NULL);
2924 }
2925 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002926} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002927
Brian Curtind25aef52011-06-13 15:16:04 -05002928
Brian Curtinf5e76d02010-11-24 13:14:05 +00002929
Brian Curtind40e6f72010-07-08 21:39:08 +00002930/* A helper function for samepath on windows */
2931static PyObject *
2932posix__getfinalpathname(PyObject *self, PyObject *args)
2933{
2934 HANDLE hFile;
2935 int buf_size;
2936 wchar_t *target_path;
2937 int result_length;
2938 PyObject *result;
2939 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002940
Brian Curtin94622b02010-09-24 00:03:39 +00002941 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002942 return NULL;
2943 }
2944
2945 if(!check_GetFinalPathNameByHandle()) {
2946 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2947 NotImplementedError. */
2948 return PyErr_Format(PyExc_NotImplementedError,
2949 "GetFinalPathNameByHandle not available on this platform");
2950 }
2951
2952 hFile = CreateFileW(
2953 path,
2954 0, /* desired access */
2955 0, /* share mode */
2956 NULL, /* security attributes */
2957 OPEN_EXISTING,
2958 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2959 FILE_FLAG_BACKUP_SEMANTICS,
2960 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002961
Brian Curtind40e6f72010-07-08 21:39:08 +00002962 if(hFile == INVALID_HANDLE_VALUE) {
2963 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002964 return PyErr_Format(PyExc_RuntimeError,
2965 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002966 }
2967
2968 /* We have a good handle to the target, use it to determine the
2969 target path name. */
2970 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2971
2972 if(!buf_size)
2973 return win32_error_unicode("GetFinalPathNameByHandle", path);
2974
2975 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2976 if(!target_path)
2977 return PyErr_NoMemory();
2978
2979 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2980 buf_size, VOLUME_NAME_DOS);
2981 if(!result_length)
2982 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2983
2984 if(!CloseHandle(hFile))
2985 return win32_error_unicode("GetFinalPathNameByHandle", path);
2986
2987 target_path[result_length] = 0;
2988 result = PyUnicode_FromUnicode(target_path, result_length);
2989 free(target_path);
2990 return result;
2991
2992} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002993
2994static PyObject *
2995posix__getfileinformation(PyObject *self, PyObject *args)
2996{
2997 HANDLE hFile;
2998 BY_HANDLE_FILE_INFORMATION info;
2999 int fd;
3000
3001 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3002 return NULL;
3003
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003004 if (!_PyVerify_fd(fd))
3005 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003006
3007 hFile = (HANDLE)_get_osfhandle(fd);
3008 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003009 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003010
3011 if (!GetFileInformationByHandle(hFile, &info))
3012 return win32_error("_getfileinformation", NULL);
3013
3014 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3015 info.nFileIndexHigh,
3016 info.nFileIndexLow);
3017}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003018
Brian Curtin95d028f2011-06-09 09:10:38 -05003019PyDoc_STRVAR(posix__isdir__doc__,
3020"Return true if the pathname refers to an existing directory.");
3021
Brian Curtin9c669cc2011-06-08 18:17:18 -05003022static PyObject *
3023posix__isdir(PyObject *self, PyObject *args)
3024{
3025 PyObject *opath;
3026 char *path;
3027 PyUnicodeObject *po;
3028 DWORD attributes;
3029
3030 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3031 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3032
3033 attributes = GetFileAttributesW(wpath);
3034 if (attributes == INVALID_FILE_ATTRIBUTES)
3035 Py_RETURN_FALSE;
3036 goto check;
3037 }
3038 /* Drop the argument parsing error as narrow strings
3039 are also valid. */
3040 PyErr_Clear();
3041
3042 if (!PyArg_ParseTuple(args, "O&:_isdir",
3043 PyUnicode_FSConverter, &opath))
3044 return NULL;
3045
3046 path = PyBytes_AsString(opath);
3047 attributes = GetFileAttributesA(path);
3048 if (attributes == INVALID_FILE_ATTRIBUTES)
3049 Py_RETURN_FALSE;
3050
3051check:
3052 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3053 Py_RETURN_TRUE;
3054 else
3055 Py_RETURN_FALSE;
3056}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003057#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003059PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003060"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003061Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003062
Barry Warsaw53699e91996-12-10 23:23:01 +00003063static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003064posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003065{
Victor Stinner8c62be82010-05-06 00:08:46 +00003066 int res;
3067 PyObject *opath;
3068 char *path;
3069 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003070
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003071#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003072 PyUnicodeObject *po;
3073 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3074 Py_BEGIN_ALLOW_THREADS
3075 /* PyUnicode_AS_UNICODE OK without thread lock as
3076 it is a simple dereference. */
3077 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3078 Py_END_ALLOW_THREADS
3079 if (!res)
3080 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3081 Py_INCREF(Py_None);
3082 return Py_None;
3083 }
3084 /* Drop the argument parsing error as narrow strings
3085 are also valid. */
3086 PyErr_Clear();
3087 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3088 PyUnicode_FSConverter, &opath, &mode))
3089 return NULL;
3090 path = PyBytes_AsString(opath);
3091 Py_BEGIN_ALLOW_THREADS
3092 /* PyUnicode_AS_UNICODE OK without thread lock as
3093 it is a simple dereference. */
3094 res = CreateDirectoryA(path, NULL);
3095 Py_END_ALLOW_THREADS
3096 if (!res) {
3097 win32_error("mkdir", path);
3098 Py_DECREF(opath);
3099 return NULL;
3100 }
3101 Py_DECREF(opath);
3102 Py_INCREF(Py_None);
3103 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003104#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003105
Victor Stinner8c62be82010-05-06 00:08:46 +00003106 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3107 PyUnicode_FSConverter, &opath, &mode))
3108 return NULL;
3109 path = PyBytes_AsString(opath);
3110 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003111#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003112 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003113#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003115#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003116 Py_END_ALLOW_THREADS
3117 if (res < 0)
3118 return posix_error_with_allocated_filename(opath);
3119 Py_DECREF(opath);
3120 Py_INCREF(Py_None);
3121 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003122#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003123}
3124
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003125
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003126/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3127#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003128#include <sys/resource.h>
3129#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003130
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003131
3132#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003133PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003134"nice(inc) -> new_priority\n\n\
3135Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003136
Barry Warsaw53699e91996-12-10 23:23:01 +00003137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003138posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003139{
Victor Stinner8c62be82010-05-06 00:08:46 +00003140 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003141
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3143 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003144
Victor Stinner8c62be82010-05-06 00:08:46 +00003145 /* There are two flavours of 'nice': one that returns the new
3146 priority (as required by almost all standards out there) and the
3147 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3148 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003149
Victor Stinner8c62be82010-05-06 00:08:46 +00003150 If we are of the nice family that returns the new priority, we
3151 need to clear errno before the call, and check if errno is filled
3152 before calling posix_error() on a returnvalue of -1, because the
3153 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003154
Victor Stinner8c62be82010-05-06 00:08:46 +00003155 errno = 0;
3156 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003157#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003158 if (value == 0)
3159 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003160#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003161 if (value == -1 && errno != 0)
3162 /* either nice() or getpriority() returned an error */
3163 return posix_error();
3164 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003165}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003166#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003167
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003168
3169#ifdef HAVE_GETPRIORITY
3170PyDoc_STRVAR(posix_getpriority__doc__,
3171"getpriority(which, who) -> current_priority\n\n\
3172Get program scheduling priority.");
3173
3174static PyObject *
3175posix_getpriority(PyObject *self, PyObject *args)
3176{
3177 int which, who, retval;
3178
3179 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3180 return NULL;
3181 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003182 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003183 if (errno != 0)
3184 return posix_error();
3185 return PyLong_FromLong((long)retval);
3186}
3187#endif /* HAVE_GETPRIORITY */
3188
3189
3190#ifdef HAVE_SETPRIORITY
3191PyDoc_STRVAR(posix_setpriority__doc__,
3192"setpriority(which, who, prio) -> None\n\n\
3193Set program scheduling priority.");
3194
3195static PyObject *
3196posix_setpriority(PyObject *self, PyObject *args)
3197{
3198 int which, who, prio, retval;
3199
3200 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3201 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003202 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003203 if (retval == -1)
3204 return posix_error();
3205 Py_RETURN_NONE;
3206}
3207#endif /* HAVE_SETPRIORITY */
3208
3209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003211"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003212Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213
Barry Warsaw53699e91996-12-10 23:23:01 +00003214static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003215posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003216{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003217#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003218 PyObject *o1, *o2;
3219 char *p1, *p2;
3220 BOOL result;
3221 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3222 goto error;
3223 if (!convert_to_unicode(&o1))
3224 goto error;
3225 if (!convert_to_unicode(&o2)) {
3226 Py_DECREF(o1);
3227 goto error;
3228 }
3229 Py_BEGIN_ALLOW_THREADS
3230 result = MoveFileW(PyUnicode_AsUnicode(o1),
3231 PyUnicode_AsUnicode(o2));
3232 Py_END_ALLOW_THREADS
3233 Py_DECREF(o1);
3234 Py_DECREF(o2);
3235 if (!result)
3236 return win32_error("rename", NULL);
3237 Py_INCREF(Py_None);
3238 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003239error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003240 PyErr_Clear();
3241 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3242 return NULL;
3243 Py_BEGIN_ALLOW_THREADS
3244 result = MoveFileA(p1, p2);
3245 Py_END_ALLOW_THREADS
3246 if (!result)
3247 return win32_error("rename", NULL);
3248 Py_INCREF(Py_None);
3249 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003250#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003251 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003252#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003253}
3254
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003256PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003257"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003258Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003259
Barry Warsaw53699e91996-12-10 23:23:01 +00003260static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003261posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003262{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003263#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003266 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003267#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003268}
3269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003271PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003272"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003273Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003274
Barry Warsaw53699e91996-12-10 23:23:01 +00003275static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003276posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003277{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003278#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003279 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003280#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003281 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003282#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003283}
3284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003285
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003286#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003287PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003288"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003289Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003290
Barry Warsaw53699e91996-12-10 23:23:01 +00003291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003292posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003293{
Victor Stinner8c62be82010-05-06 00:08:46 +00003294 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003295#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 wchar_t *command;
3297 if (!PyArg_ParseTuple(args, "u:system", &command))
3298 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003299
Victor Stinner8c62be82010-05-06 00:08:46 +00003300 Py_BEGIN_ALLOW_THREADS
3301 sts = _wsystem(command);
3302 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003303#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003304 PyObject *command_obj;
3305 char *command;
3306 if (!PyArg_ParseTuple(args, "O&:system",
3307 PyUnicode_FSConverter, &command_obj))
3308 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003309
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 command = PyBytes_AsString(command_obj);
3311 Py_BEGIN_ALLOW_THREADS
3312 sts = system(command);
3313 Py_END_ALLOW_THREADS
3314 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003315#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003316 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003317}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003319
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003322"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003323Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003324
Barry Warsaw53699e91996-12-10 23:23:01 +00003325static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003326posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003327{
Victor Stinner8c62be82010-05-06 00:08:46 +00003328 int i;
3329 if (!PyArg_ParseTuple(args, "i:umask", &i))
3330 return NULL;
3331 i = (int)umask(i);
3332 if (i < 0)
3333 return posix_error();
3334 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003335}
3336
Brian Curtind40e6f72010-07-08 21:39:08 +00003337#ifdef MS_WINDOWS
3338
3339/* override the default DeleteFileW behavior so that directory
3340symlinks can be removed with this function, the same as with
3341Unix symlinks */
3342BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3343{
3344 WIN32_FILE_ATTRIBUTE_DATA info;
3345 WIN32_FIND_DATAW find_data;
3346 HANDLE find_data_handle;
3347 int is_directory = 0;
3348 int is_link = 0;
3349
3350 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3351 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003352
Brian Curtind40e6f72010-07-08 21:39:08 +00003353 /* Get WIN32_FIND_DATA structure for the path to determine if
3354 it is a symlink */
3355 if(is_directory &&
3356 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3357 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3358
3359 if(find_data_handle != INVALID_HANDLE_VALUE) {
3360 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3361 FindClose(find_data_handle);
3362 }
3363 }
3364 }
3365
3366 if (is_directory && is_link)
3367 return RemoveDirectoryW(lpFileName);
3368
3369 return DeleteFileW(lpFileName);
3370}
3371#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003374"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003378"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003379Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003380
Barry Warsaw53699e91996-12-10 23:23:01 +00003381static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003382posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003383{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003384#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003385 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3386 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003389#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003390}
3391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003392
Guido van Rossumb6775db1994-08-01 11:34:53 +00003393#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003394PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003395"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003396Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003397
Barry Warsaw53699e91996-12-10 23:23:01 +00003398static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003399posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003400{
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 struct utsname u;
3402 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003403
Victor Stinner8c62be82010-05-06 00:08:46 +00003404 Py_BEGIN_ALLOW_THREADS
3405 res = uname(&u);
3406 Py_END_ALLOW_THREADS
3407 if (res < 0)
3408 return posix_error();
3409 return Py_BuildValue("(sssss)",
3410 u.sysname,
3411 u.nodename,
3412 u.release,
3413 u.version,
3414 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003415}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003416#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003417
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003418static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003419extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003420{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003421 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003422 if (PyFloat_Check(t)) {
3423 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003424 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003425 if (!intobj)
3426 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003427#if SIZEOF_TIME_T > SIZEOF_LONG
3428 intval = PyLong_AsUnsignedLongLongMask(intobj);
3429#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003431#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003432 Py_DECREF(intobj);
3433 if (intval == -1 && PyErr_Occurred())
3434 return -1;
3435 *sec = intval;
3436 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3437 if (*usec < 0)
3438 /* If rounding gave us a negative number,
3439 truncate. */
3440 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003441 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003442 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003443#if SIZEOF_TIME_T > SIZEOF_LONG
3444 intval = PyLong_AsUnsignedLongLongMask(t);
3445#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003447#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003448 if (intval == -1 && PyErr_Occurred())
3449 return -1;
3450 *sec = intval;
3451 *usec = 0;
3452 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003453}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003455PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003456"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003457utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003458Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003459second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003460
Barry Warsaw53699e91996-12-10 23:23:01 +00003461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003462posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003463{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003464#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003465 PyObject *arg;
3466 PyUnicodeObject *obwpath;
3467 wchar_t *wpath = NULL;
3468 PyObject *oapath;
3469 char *apath;
3470 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003471 time_t atimesec, mtimesec;
3472 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 FILETIME atime, mtime;
3474 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003475
Victor Stinner8c62be82010-05-06 00:08:46 +00003476 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3477 wpath = PyUnicode_AS_UNICODE(obwpath);
3478 Py_BEGIN_ALLOW_THREADS
3479 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3480 NULL, OPEN_EXISTING,
3481 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3482 Py_END_ALLOW_THREADS
3483 if (hFile == INVALID_HANDLE_VALUE)
3484 return win32_error_unicode("utime", wpath);
3485 } else
3486 /* Drop the argument parsing error as narrow strings
3487 are also valid. */
3488 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003489
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 if (!wpath) {
3491 if (!PyArg_ParseTuple(args, "O&O:utime",
3492 PyUnicode_FSConverter, &oapath, &arg))
3493 return NULL;
3494 apath = PyBytes_AsString(oapath);
3495 Py_BEGIN_ALLOW_THREADS
3496 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3497 NULL, OPEN_EXISTING,
3498 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3499 Py_END_ALLOW_THREADS
3500 if (hFile == INVALID_HANDLE_VALUE) {
3501 win32_error("utime", apath);
3502 Py_DECREF(oapath);
3503 return NULL;
3504 }
3505 Py_DECREF(oapath);
3506 }
3507
3508 if (arg == Py_None) {
3509 SYSTEMTIME now;
3510 GetSystemTime(&now);
3511 if (!SystemTimeToFileTime(&now, &mtime) ||
3512 !SystemTimeToFileTime(&now, &atime)) {
3513 win32_error("utime", NULL);
3514 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003515 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 }
3517 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3518 PyErr_SetString(PyExc_TypeError,
3519 "utime() arg 2 must be a tuple (atime, mtime)");
3520 goto done;
3521 }
3522 else {
3523 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3524 &atimesec, &ausec) == -1)
3525 goto done;
3526 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3527 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3528 &mtimesec, &musec) == -1)
3529 goto done;
3530 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3531 }
3532 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3533 /* Avoid putting the file name into the error here,
3534 as that may confuse the user into believing that
3535 something is wrong with the file, when it also
3536 could be the time stamp that gives a problem. */
3537 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003538 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003539 }
3540 Py_INCREF(Py_None);
3541 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003542done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 CloseHandle(hFile);
3544 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003545#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003546
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 PyObject *opath;
3548 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003549 time_t atime, mtime;
3550 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 int res;
3552 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003553
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003554#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003556#define ATIME buf[0].tv_sec
3557#define MTIME buf[1].tv_sec
3558#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003559/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003560 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003561#define ATIME buf.actime
3562#define MTIME buf.modtime
3563#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003564#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003566#define ATIME buf[0]
3567#define MTIME buf[1]
3568#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003569#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003570
Mark Hammond817c9292003-12-03 01:22:38 +00003571
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 if (!PyArg_ParseTuple(args, "O&O:utime",
3573 PyUnicode_FSConverter, &opath, &arg))
3574 return NULL;
3575 path = PyBytes_AsString(opath);
3576 if (arg == Py_None) {
3577 /* optional time values not given */
3578 Py_BEGIN_ALLOW_THREADS
3579 res = utime(path, NULL);
3580 Py_END_ALLOW_THREADS
3581 }
3582 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3583 PyErr_SetString(PyExc_TypeError,
3584 "utime() arg 2 must be a tuple (atime, mtime)");
3585 Py_DECREF(opath);
3586 return NULL;
3587 }
3588 else {
3589 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3590 &atime, &ausec) == -1) {
3591 Py_DECREF(opath);
3592 return NULL;
3593 }
3594 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3595 &mtime, &musec) == -1) {
3596 Py_DECREF(opath);
3597 return NULL;
3598 }
3599 ATIME = atime;
3600 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003601#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 buf[0].tv_usec = ausec;
3603 buf[1].tv_usec = musec;
3604 Py_BEGIN_ALLOW_THREADS
3605 res = utimes(path, buf);
3606 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003607#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003608 Py_BEGIN_ALLOW_THREADS
3609 res = utime(path, UTIME_ARG);
3610 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003611#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003612 }
3613 if (res < 0) {
3614 return posix_error_with_allocated_filename(opath);
3615 }
3616 Py_DECREF(opath);
3617 Py_INCREF(Py_None);
3618 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003619#undef UTIME_ARG
3620#undef ATIME
3621#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003622#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003623}
3624
Ross Lagerwall7807c352011-03-17 20:20:30 +02003625#ifdef HAVE_FUTIMES
3626PyDoc_STRVAR(posix_futimes__doc__,
3627"futimes(fd, (atime, mtime))\n\
3628futimes(fd, None)\n\n\
3629Set the access and modified time of the file specified by the file\n\
3630descriptor fd to the given values. If the second form is used, set the\n\
3631access and modified times to the current time.");
3632
3633static PyObject *
3634posix_futimes(PyObject *self, PyObject *args)
3635{
3636 int res, fd;
3637 PyObject* arg;
3638 struct timeval buf[2];
3639 long ausec, musec;
3640
3641 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3642 return NULL;
3643
3644 if (arg == Py_None) {
3645 /* optional time values not given */
3646 Py_BEGIN_ALLOW_THREADS
3647 res = futimes(fd, NULL);
3648 Py_END_ALLOW_THREADS
3649 }
3650 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3651 PyErr_SetString(PyExc_TypeError,
3652 "futimes() arg 2 must be a tuple (atime, mtime)");
3653 return NULL;
3654 }
3655 else {
3656 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3657 &(buf[0].tv_sec), &ausec) == -1) {
3658 return NULL;
3659 }
3660 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3661 &(buf[1].tv_sec), &musec) == -1) {
3662 return NULL;
3663 }
3664 buf[0].tv_usec = ausec;
3665 buf[1].tv_usec = musec;
3666 Py_BEGIN_ALLOW_THREADS
3667 res = futimes(fd, buf);
3668 Py_END_ALLOW_THREADS
3669 }
3670 if (res < 0)
3671 return posix_error();
3672 Py_RETURN_NONE;
3673}
3674#endif
3675
3676#ifdef HAVE_LUTIMES
3677PyDoc_STRVAR(posix_lutimes__doc__,
3678"lutimes(path, (atime, mtime))\n\
3679lutimes(path, None)\n\n\
3680Like utime(), but if path is a symbolic link, it is not dereferenced.");
3681
3682static PyObject *
3683posix_lutimes(PyObject *self, PyObject *args)
3684{
3685 PyObject *opath, *arg;
3686 const char *path;
3687 int res;
3688 struct timeval buf[2];
3689 long ausec, musec;
3690
3691 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3692 PyUnicode_FSConverter, &opath, &arg))
3693 return NULL;
3694 path = PyBytes_AsString(opath);
3695 if (arg == Py_None) {
3696 /* optional time values not given */
3697 Py_BEGIN_ALLOW_THREADS
3698 res = lutimes(path, NULL);
3699 Py_END_ALLOW_THREADS
3700 }
3701 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3702 PyErr_SetString(PyExc_TypeError,
3703 "lutimes() arg 2 must be a tuple (atime, mtime)");
3704 Py_DECREF(opath);
3705 return NULL;
3706 }
3707 else {
3708 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3709 &(buf[0].tv_sec), &ausec) == -1) {
3710 Py_DECREF(opath);
3711 return NULL;
3712 }
3713 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3714 &(buf[1].tv_sec), &musec) == -1) {
3715 Py_DECREF(opath);
3716 return NULL;
3717 }
3718 buf[0].tv_usec = ausec;
3719 buf[1].tv_usec = musec;
3720 Py_BEGIN_ALLOW_THREADS
3721 res = lutimes(path, buf);
3722 Py_END_ALLOW_THREADS
3723 }
3724 Py_DECREF(opath);
3725 if (res < 0)
3726 return posix_error();
3727 Py_RETURN_NONE;
3728}
3729#endif
3730
3731#ifdef HAVE_FUTIMENS
3732PyDoc_STRVAR(posix_futimens__doc__,
3733"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3734futimens(fd, None, None)\n\n\
3735Updates the timestamps of a file specified by the file descriptor fd, with\n\
3736nanosecond precision.\n\
3737The second form sets atime and mtime to the current time.\n\
3738If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3739current time.\n\
3740If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3741
3742static PyObject *
3743posix_futimens(PyObject *self, PyObject *args)
3744{
3745 int res, fd;
3746 PyObject *atime, *mtime;
3747 struct timespec buf[2];
3748
3749 if (!PyArg_ParseTuple(args, "iOO:futimens",
3750 &fd, &atime, &mtime))
3751 return NULL;
3752 if (atime == Py_None && mtime == Py_None) {
3753 /* optional time values not given */
3754 Py_BEGIN_ALLOW_THREADS
3755 res = futimens(fd, NULL);
3756 Py_END_ALLOW_THREADS
3757 }
3758 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3759 PyErr_SetString(PyExc_TypeError,
3760 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3761 return NULL;
3762 }
3763 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3764 PyErr_SetString(PyExc_TypeError,
3765 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3766 return NULL;
3767 }
3768 else {
3769 if (!PyArg_ParseTuple(atime, "ll:futimens",
3770 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3771 return NULL;
3772 }
3773 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3774 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3775 return NULL;
3776 }
3777 Py_BEGIN_ALLOW_THREADS
3778 res = futimens(fd, buf);
3779 Py_END_ALLOW_THREADS
3780 }
3781 if (res < 0)
3782 return posix_error();
3783 Py_RETURN_NONE;
3784}
3785#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003786
Guido van Rossum3b066191991-06-04 19:40:25 +00003787/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003790"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003792
Barry Warsaw53699e91996-12-10 23:23:01 +00003793static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003794posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003795{
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 int sts;
3797 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3798 return NULL;
3799 _exit(sts);
3800 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003801}
3802
Martin v. Löwis114619e2002-10-07 06:44:21 +00003803#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3804static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003805free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003806{
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 Py_ssize_t i;
3808 for (i = 0; i < count; i++)
3809 PyMem_Free(array[i]);
3810 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003811}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003812
Antoine Pitrou69f71142009-05-24 21:25:49 +00003813static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003814int fsconvert_strdup(PyObject *o, char**out)
3815{
Victor Stinner8c62be82010-05-06 00:08:46 +00003816 PyObject *bytes;
3817 Py_ssize_t size;
3818 if (!PyUnicode_FSConverter(o, &bytes))
3819 return 0;
3820 size = PyBytes_GET_SIZE(bytes);
3821 *out = PyMem_Malloc(size+1);
3822 if (!*out)
3823 return 0;
3824 memcpy(*out, PyBytes_AsString(bytes), size+1);
3825 Py_DECREF(bytes);
3826 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003827}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003828#endif
3829
Ross Lagerwall7807c352011-03-17 20:20:30 +02003830#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003831static char**
3832parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3833{
Victor Stinner8c62be82010-05-06 00:08:46 +00003834 char **envlist;
3835 Py_ssize_t i, pos, envc;
3836 PyObject *keys=NULL, *vals=NULL;
3837 PyObject *key, *val, *key2, *val2;
3838 char *p, *k, *v;
3839 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003840
Victor Stinner8c62be82010-05-06 00:08:46 +00003841 i = PyMapping_Size(env);
3842 if (i < 0)
3843 return NULL;
3844 envlist = PyMem_NEW(char *, i + 1);
3845 if (envlist == NULL) {
3846 PyErr_NoMemory();
3847 return NULL;
3848 }
3849 envc = 0;
3850 keys = PyMapping_Keys(env);
3851 vals = PyMapping_Values(env);
3852 if (!keys || !vals)
3853 goto error;
3854 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3855 PyErr_Format(PyExc_TypeError,
3856 "env.keys() or env.values() is not a list");
3857 goto error;
3858 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003859
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 for (pos = 0; pos < i; pos++) {
3861 key = PyList_GetItem(keys, pos);
3862 val = PyList_GetItem(vals, pos);
3863 if (!key || !val)
3864 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003865
Victor Stinner8c62be82010-05-06 00:08:46 +00003866 if (PyUnicode_FSConverter(key, &key2) == 0)
3867 goto error;
3868 if (PyUnicode_FSConverter(val, &val2) == 0) {
3869 Py_DECREF(key2);
3870 goto error;
3871 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003872
3873#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003874 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3875 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003876#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003877 k = PyBytes_AsString(key2);
3878 v = PyBytes_AsString(val2);
3879 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003880
Victor Stinner8c62be82010-05-06 00:08:46 +00003881 p = PyMem_NEW(char, len);
3882 if (p == NULL) {
3883 PyErr_NoMemory();
3884 Py_DECREF(key2);
3885 Py_DECREF(val2);
3886 goto error;
3887 }
3888 PyOS_snprintf(p, len, "%s=%s", k, v);
3889 envlist[envc++] = p;
3890 Py_DECREF(key2);
3891 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003892#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003894#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003895 }
3896 Py_DECREF(vals);
3897 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003898
Victor Stinner8c62be82010-05-06 00:08:46 +00003899 envlist[envc] = 0;
3900 *envc_ptr = envc;
3901 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003902
3903error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003904 Py_XDECREF(keys);
3905 Py_XDECREF(vals);
3906 while (--envc >= 0)
3907 PyMem_DEL(envlist[envc]);
3908 PyMem_DEL(envlist);
3909 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003910}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003911
Ross Lagerwall7807c352011-03-17 20:20:30 +02003912static char**
3913parse_arglist(PyObject* argv, Py_ssize_t *argc)
3914{
3915 int i;
3916 char **argvlist = PyMem_NEW(char *, *argc+1);
3917 if (argvlist == NULL) {
3918 PyErr_NoMemory();
3919 return NULL;
3920 }
3921 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003922 PyObject* item = PySequence_ITEM(argv, i);
3923 if (item == NULL)
3924 goto fail;
3925 if (!fsconvert_strdup(item, &argvlist[i])) {
3926 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003927 goto fail;
3928 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003929 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003930 }
3931 argvlist[*argc] = NULL;
3932 return argvlist;
3933fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003934 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003935 free_string_array(argvlist, *argc);
3936 return NULL;
3937}
3938#endif
3939
3940#ifdef HAVE_EXECV
3941PyDoc_STRVAR(posix_execv__doc__,
3942"execv(path, args)\n\n\
3943Execute an executable path with arguments, replacing current process.\n\
3944\n\
3945 path: path of executable file\n\
3946 args: tuple or list of strings");
3947
3948static PyObject *
3949posix_execv(PyObject *self, PyObject *args)
3950{
3951 PyObject *opath;
3952 char *path;
3953 PyObject *argv;
3954 char **argvlist;
3955 Py_ssize_t argc;
3956
3957 /* execv has two arguments: (path, argv), where
3958 argv is a list or tuple of strings. */
3959
3960 if (!PyArg_ParseTuple(args, "O&O:execv",
3961 PyUnicode_FSConverter,
3962 &opath, &argv))
3963 return NULL;
3964 path = PyBytes_AsString(opath);
3965 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3966 PyErr_SetString(PyExc_TypeError,
3967 "execv() arg 2 must be a tuple or list");
3968 Py_DECREF(opath);
3969 return NULL;
3970 }
3971 argc = PySequence_Size(argv);
3972 if (argc < 1) {
3973 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3974 Py_DECREF(opath);
3975 return NULL;
3976 }
3977
3978 argvlist = parse_arglist(argv, &argc);
3979 if (argvlist == NULL) {
3980 Py_DECREF(opath);
3981 return NULL;
3982 }
3983
3984 execv(path, argvlist);
3985
3986 /* If we get here it's definitely an error */
3987
3988 free_string_array(argvlist, argc);
3989 Py_DECREF(opath);
3990 return posix_error();
3991}
3992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003993PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003994"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003995Execute a path with arguments and environment, replacing current process.\n\
3996\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003997 path: path of executable file\n\
3998 args: tuple or list of arguments\n\
3999 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004000
Barry Warsaw53699e91996-12-10 23:23:01 +00004001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004002posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004003{
Victor Stinner8c62be82010-05-06 00:08:46 +00004004 PyObject *opath;
4005 char *path;
4006 PyObject *argv, *env;
4007 char **argvlist;
4008 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004009 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004010
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 /* execve has three arguments: (path, argv, env), where
4012 argv is a list or tuple of strings and env is a dictionary
4013 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004014
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 if (!PyArg_ParseTuple(args, "O&OO:execve",
4016 PyUnicode_FSConverter,
4017 &opath, &argv, &env))
4018 return NULL;
4019 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004020 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004021 PyErr_SetString(PyExc_TypeError,
4022 "execve() arg 2 must be a tuple or list");
4023 goto fail_0;
4024 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004025 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004026 if (!PyMapping_Check(env)) {
4027 PyErr_SetString(PyExc_TypeError,
4028 "execve() arg 3 must be a mapping object");
4029 goto fail_0;
4030 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004031
Ross Lagerwall7807c352011-03-17 20:20:30 +02004032 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004033 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004034 goto fail_0;
4035 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004036
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 envlist = parse_envlist(env, &envc);
4038 if (envlist == NULL)
4039 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004040
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004042
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004044
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004046
Victor Stinner8c62be82010-05-06 00:08:46 +00004047 while (--envc >= 0)
4048 PyMem_DEL(envlist[envc]);
4049 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004050 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004051 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004052 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004053 Py_DECREF(opath);
4054 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004055}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004056#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004057
Ross Lagerwall7807c352011-03-17 20:20:30 +02004058#ifdef HAVE_FEXECVE
4059PyDoc_STRVAR(posix_fexecve__doc__,
4060"fexecve(fd, args, env)\n\n\
4061Execute the program specified by a file descriptor with arguments and\n\
4062environment, replacing the current process.\n\
4063\n\
4064 fd: file descriptor of executable\n\
4065 args: tuple or list of arguments\n\
4066 env: dictionary of strings mapping to strings");
4067
4068static PyObject *
4069posix_fexecve(PyObject *self, PyObject *args)
4070{
4071 int fd;
4072 PyObject *argv, *env;
4073 char **argvlist;
4074 char **envlist;
4075 Py_ssize_t argc, envc;
4076
4077 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4078 &fd, &argv, &env))
4079 return NULL;
4080 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4081 PyErr_SetString(PyExc_TypeError,
4082 "fexecve() arg 2 must be a tuple or list");
4083 return NULL;
4084 }
4085 argc = PySequence_Size(argv);
4086 if (!PyMapping_Check(env)) {
4087 PyErr_SetString(PyExc_TypeError,
4088 "fexecve() arg 3 must be a mapping object");
4089 return NULL;
4090 }
4091
4092 argvlist = parse_arglist(argv, &argc);
4093 if (argvlist == NULL)
4094 return NULL;
4095
4096 envlist = parse_envlist(env, &envc);
4097 if (envlist == NULL)
4098 goto fail;
4099
4100 fexecve(fd, argvlist, envlist);
4101
4102 /* If we get here it's definitely an error */
4103
4104 (void) posix_error();
4105
4106 while (--envc >= 0)
4107 PyMem_DEL(envlist[envc]);
4108 PyMem_DEL(envlist);
4109 fail:
4110 free_string_array(argvlist, argc);
4111 return NULL;
4112}
4113#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004114
Guido van Rossuma1065681999-01-25 23:20:23 +00004115#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004116PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004117"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004118Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004119\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004120 mode: mode of process creation\n\
4121 path: path of executable file\n\
4122 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004123
4124static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004125posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004126{
Victor Stinner8c62be82010-05-06 00:08:46 +00004127 PyObject *opath;
4128 char *path;
4129 PyObject *argv;
4130 char **argvlist;
4131 int mode, i;
4132 Py_ssize_t argc;
4133 Py_intptr_t spawnval;
4134 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004135
Victor Stinner8c62be82010-05-06 00:08:46 +00004136 /* spawnv has three arguments: (mode, path, argv), where
4137 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004138
Victor Stinner8c62be82010-05-06 00:08:46 +00004139 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4140 PyUnicode_FSConverter,
4141 &opath, &argv))
4142 return NULL;
4143 path = PyBytes_AsString(opath);
4144 if (PyList_Check(argv)) {
4145 argc = PyList_Size(argv);
4146 getitem = PyList_GetItem;
4147 }
4148 else if (PyTuple_Check(argv)) {
4149 argc = PyTuple_Size(argv);
4150 getitem = PyTuple_GetItem;
4151 }
4152 else {
4153 PyErr_SetString(PyExc_TypeError,
4154 "spawnv() arg 2 must be a tuple or list");
4155 Py_DECREF(opath);
4156 return NULL;
4157 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004158
Victor Stinner8c62be82010-05-06 00:08:46 +00004159 argvlist = PyMem_NEW(char *, argc+1);
4160 if (argvlist == NULL) {
4161 Py_DECREF(opath);
4162 return PyErr_NoMemory();
4163 }
4164 for (i = 0; i < argc; i++) {
4165 if (!fsconvert_strdup((*getitem)(argv, i),
4166 &argvlist[i])) {
4167 free_string_array(argvlist, i);
4168 PyErr_SetString(
4169 PyExc_TypeError,
4170 "spawnv() arg 2 must contain only strings");
4171 Py_DECREF(opath);
4172 return NULL;
4173 }
4174 }
4175 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004176
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004177#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004178 Py_BEGIN_ALLOW_THREADS
4179 spawnval = spawnv(mode, path, argvlist);
4180 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004181#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 if (mode == _OLD_P_OVERLAY)
4183 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004184
Victor Stinner8c62be82010-05-06 00:08:46 +00004185 Py_BEGIN_ALLOW_THREADS
4186 spawnval = _spawnv(mode, path, argvlist);
4187 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004188#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004189
Victor Stinner8c62be82010-05-06 00:08:46 +00004190 free_string_array(argvlist, argc);
4191 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004192
Victor Stinner8c62be82010-05-06 00:08:46 +00004193 if (spawnval == -1)
4194 return posix_error();
4195 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004196#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004198#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004199 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004200#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004201}
4202
4203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004204PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004205"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004206Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004207\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004208 mode: mode of process creation\n\
4209 path: path of executable file\n\
4210 args: tuple or list of arguments\n\
4211 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004212
4213static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004214posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004215{
Victor Stinner8c62be82010-05-06 00:08:46 +00004216 PyObject *opath;
4217 char *path;
4218 PyObject *argv, *env;
4219 char **argvlist;
4220 char **envlist;
4221 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004222 int mode;
4223 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004224 Py_intptr_t spawnval;
4225 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4226 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004227
Victor Stinner8c62be82010-05-06 00:08:46 +00004228 /* spawnve has four arguments: (mode, path, argv, env), where
4229 argv is a list or tuple of strings and env is a dictionary
4230 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004231
Victor Stinner8c62be82010-05-06 00:08:46 +00004232 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4233 PyUnicode_FSConverter,
4234 &opath, &argv, &env))
4235 return NULL;
4236 path = PyBytes_AsString(opath);
4237 if (PyList_Check(argv)) {
4238 argc = PyList_Size(argv);
4239 getitem = PyList_GetItem;
4240 }
4241 else if (PyTuple_Check(argv)) {
4242 argc = PyTuple_Size(argv);
4243 getitem = PyTuple_GetItem;
4244 }
4245 else {
4246 PyErr_SetString(PyExc_TypeError,
4247 "spawnve() arg 2 must be a tuple or list");
4248 goto fail_0;
4249 }
4250 if (!PyMapping_Check(env)) {
4251 PyErr_SetString(PyExc_TypeError,
4252 "spawnve() arg 3 must be a mapping object");
4253 goto fail_0;
4254 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004255
Victor Stinner8c62be82010-05-06 00:08:46 +00004256 argvlist = PyMem_NEW(char *, argc+1);
4257 if (argvlist == NULL) {
4258 PyErr_NoMemory();
4259 goto fail_0;
4260 }
4261 for (i = 0; i < argc; i++) {
4262 if (!fsconvert_strdup((*getitem)(argv, i),
4263 &argvlist[i]))
4264 {
4265 lastarg = i;
4266 goto fail_1;
4267 }
4268 }
4269 lastarg = argc;
4270 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004271
Victor Stinner8c62be82010-05-06 00:08:46 +00004272 envlist = parse_envlist(env, &envc);
4273 if (envlist == NULL)
4274 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004275
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004276#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004277 Py_BEGIN_ALLOW_THREADS
4278 spawnval = spawnve(mode, path, argvlist, envlist);
4279 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004280#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004281 if (mode == _OLD_P_OVERLAY)
4282 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004283
Victor Stinner8c62be82010-05-06 00:08:46 +00004284 Py_BEGIN_ALLOW_THREADS
4285 spawnval = _spawnve(mode, path, argvlist, envlist);
4286 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004287#endif
Tim Peters25059d32001-12-07 20:35:43 +00004288
Victor Stinner8c62be82010-05-06 00:08:46 +00004289 if (spawnval == -1)
4290 (void) posix_error();
4291 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004292#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004294#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004295 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004296#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004297
Victor Stinner8c62be82010-05-06 00:08:46 +00004298 while (--envc >= 0)
4299 PyMem_DEL(envlist[envc]);
4300 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004301 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004303 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 Py_DECREF(opath);
4305 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004306}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004307
4308/* OS/2 supports spawnvp & spawnvpe natively */
4309#if defined(PYOS_OS2)
4310PyDoc_STRVAR(posix_spawnvp__doc__,
4311"spawnvp(mode, file, args)\n\n\
4312Execute the program 'file' in a new process, using the environment\n\
4313search path to find the file.\n\
4314\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004315 mode: mode of process creation\n\
4316 file: executable file name\n\
4317 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004318
4319static PyObject *
4320posix_spawnvp(PyObject *self, PyObject *args)
4321{
Victor Stinner8c62be82010-05-06 00:08:46 +00004322 PyObject *opath;
4323 char *path;
4324 PyObject *argv;
4325 char **argvlist;
4326 int mode, i, argc;
4327 Py_intptr_t spawnval;
4328 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004329
Victor Stinner8c62be82010-05-06 00:08:46 +00004330 /* spawnvp has three arguments: (mode, path, argv), where
4331 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004332
Victor Stinner8c62be82010-05-06 00:08:46 +00004333 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4334 PyUnicode_FSConverter,
4335 &opath, &argv))
4336 return NULL;
4337 path = PyBytes_AsString(opath);
4338 if (PyList_Check(argv)) {
4339 argc = PyList_Size(argv);
4340 getitem = PyList_GetItem;
4341 }
4342 else if (PyTuple_Check(argv)) {
4343 argc = PyTuple_Size(argv);
4344 getitem = PyTuple_GetItem;
4345 }
4346 else {
4347 PyErr_SetString(PyExc_TypeError,
4348 "spawnvp() arg 2 must be a tuple or list");
4349 Py_DECREF(opath);
4350 return NULL;
4351 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004352
Victor Stinner8c62be82010-05-06 00:08:46 +00004353 argvlist = PyMem_NEW(char *, argc+1);
4354 if (argvlist == NULL) {
4355 Py_DECREF(opath);
4356 return PyErr_NoMemory();
4357 }
4358 for (i = 0; i < argc; i++) {
4359 if (!fsconvert_strdup((*getitem)(argv, i),
4360 &argvlist[i])) {
4361 free_string_array(argvlist, i);
4362 PyErr_SetString(
4363 PyExc_TypeError,
4364 "spawnvp() arg 2 must contain only strings");
4365 Py_DECREF(opath);
4366 return NULL;
4367 }
4368 }
4369 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004370
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004376#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004378
Victor Stinner8c62be82010-05-06 00:08:46 +00004379 free_string_array(argvlist, argc);
4380 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004381
Victor Stinner8c62be82010-05-06 00:08:46 +00004382 if (spawnval == -1)
4383 return posix_error();
4384 else
4385 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004386}
4387
4388
4389PyDoc_STRVAR(posix_spawnvpe__doc__,
4390"spawnvpe(mode, file, args, env)\n\n\
4391Execute the program 'file' in a new process, using the environment\n\
4392search path to find the file.\n\
4393\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004394 mode: mode of process creation\n\
4395 file: executable file name\n\
4396 args: tuple or list of arguments\n\
4397 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004398
4399static PyObject *
4400posix_spawnvpe(PyObject *self, PyObject *args)
4401{
Victor Stinner8c62be82010-05-06 00:08:46 +00004402 PyObject *opath
4403 char *path;
4404 PyObject *argv, *env;
4405 char **argvlist;
4406 char **envlist;
4407 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004408 int mode;
4409 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004410 Py_intptr_t spawnval;
4411 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4412 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004413
Victor Stinner8c62be82010-05-06 00:08:46 +00004414 /* spawnvpe has four arguments: (mode, path, argv, env), where
4415 argv is a list or tuple of strings and env is a dictionary
4416 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004417
Victor Stinner8c62be82010-05-06 00:08:46 +00004418 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4419 PyUnicode_FSConverter,
4420 &opath, &argv, &env))
4421 return NULL;
4422 path = PyBytes_AsString(opath);
4423 if (PyList_Check(argv)) {
4424 argc = PyList_Size(argv);
4425 getitem = PyList_GetItem;
4426 }
4427 else if (PyTuple_Check(argv)) {
4428 argc = PyTuple_Size(argv);
4429 getitem = PyTuple_GetItem;
4430 }
4431 else {
4432 PyErr_SetString(PyExc_TypeError,
4433 "spawnvpe() arg 2 must be a tuple or list");
4434 goto fail_0;
4435 }
4436 if (!PyMapping_Check(env)) {
4437 PyErr_SetString(PyExc_TypeError,
4438 "spawnvpe() arg 3 must be a mapping object");
4439 goto fail_0;
4440 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004441
Victor Stinner8c62be82010-05-06 00:08:46 +00004442 argvlist = PyMem_NEW(char *, argc+1);
4443 if (argvlist == NULL) {
4444 PyErr_NoMemory();
4445 goto fail_0;
4446 }
4447 for (i = 0; i < argc; i++) {
4448 if (!fsconvert_strdup((*getitem)(argv, i),
4449 &argvlist[i]))
4450 {
4451 lastarg = i;
4452 goto fail_1;
4453 }
4454 }
4455 lastarg = argc;
4456 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004457
Victor Stinner8c62be82010-05-06 00:08:46 +00004458 envlist = parse_envlist(env, &envc);
4459 if (envlist == NULL)
4460 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004461
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004463#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004465#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004469
Victor Stinner8c62be82010-05-06 00:08:46 +00004470 if (spawnval == -1)
4471 (void) posix_error();
4472 else
4473 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004474
Victor Stinner8c62be82010-05-06 00:08:46 +00004475 while (--envc >= 0)
4476 PyMem_DEL(envlist[envc]);
4477 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004478 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004480 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 Py_DECREF(opath);
4482 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004483}
4484#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004485#endif /* HAVE_SPAWNV */
4486
4487
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004488#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004489PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004490"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004491Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4492\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004493Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004494
4495static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004496posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004497{
Victor Stinner8c62be82010-05-06 00:08:46 +00004498 pid_t pid;
4499 int result = 0;
4500 _PyImport_AcquireLock();
4501 pid = fork1();
4502 if (pid == 0) {
4503 /* child: this clobbers and resets the import lock. */
4504 PyOS_AfterFork();
4505 } else {
4506 /* parent: release the import lock. */
4507 result = _PyImport_ReleaseLock();
4508 }
4509 if (pid == -1)
4510 return posix_error();
4511 if (result < 0) {
4512 /* Don't clobber the OSError if the fork failed. */
4513 PyErr_SetString(PyExc_RuntimeError,
4514 "not holding the import lock");
4515 return NULL;
4516 }
4517 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004518}
4519#endif
4520
4521
Guido van Rossumad0ee831995-03-01 10:34:45 +00004522#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004523PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004524"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004526Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004527
Barry Warsaw53699e91996-12-10 23:23:01 +00004528static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004529posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004530{
Victor Stinner8c62be82010-05-06 00:08:46 +00004531 pid_t pid;
4532 int result = 0;
4533 _PyImport_AcquireLock();
4534 pid = fork();
4535 if (pid == 0) {
4536 /* child: this clobbers and resets the import lock. */
4537 PyOS_AfterFork();
4538 } else {
4539 /* parent: release the import lock. */
4540 result = _PyImport_ReleaseLock();
4541 }
4542 if (pid == -1)
4543 return posix_error();
4544 if (result < 0) {
4545 /* Don't clobber the OSError if the fork failed. */
4546 PyErr_SetString(PyExc_RuntimeError,
4547 "not holding the import lock");
4548 return NULL;
4549 }
4550 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004551}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004552#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004553
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004554#ifdef HAVE_SCHED_H
4555
4556PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4557"sched_get_priority_max(policy)\n\n\
4558Get the maximum scheduling priority for *policy*.");
4559
4560static PyObject *
4561posix_sched_get_priority_max(PyObject *self, PyObject *args)
4562{
4563 int policy, max;
4564
4565 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4566 return NULL;
4567 max = sched_get_priority_max(policy);
4568 if (max < 0)
4569 return posix_error();
4570 return PyLong_FromLong(max);
4571}
4572
4573PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4574"sched_get_priority_min(policy)\n\n\
4575Get the minimum scheduling priority for *policy*.");
4576
4577static PyObject *
4578posix_sched_get_priority_min(PyObject *self, PyObject *args)
4579{
4580 int policy, min;
4581
4582 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4583 return NULL;
4584 min = sched_get_priority_min(policy);
4585 if (min < 0)
4586 return posix_error();
4587 return PyLong_FromLong(min);
4588}
4589
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004590#ifdef HAVE_SCHED_SETSCHEDULER
4591
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004592PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4593"sched_getscheduler(pid)\n\n\
4594Get the scheduling policy for the process with a PID of *pid*.\n\
4595Passing a PID of 0 returns the scheduling policy for the calling process.");
4596
4597static PyObject *
4598posix_sched_getscheduler(PyObject *self, PyObject *args)
4599{
4600 pid_t pid;
4601 int policy;
4602
4603 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4604 return NULL;
4605 policy = sched_getscheduler(pid);
4606 if (policy < 0)
4607 return posix_error();
4608 return PyLong_FromLong(policy);
4609}
4610
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004611#endif
4612
4613#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4614
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004615static PyObject *
4616sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4617{
4618 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004619 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004620
4621 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4622 return NULL;
4623 res = PyStructSequence_New(type);
4624 if (!res)
4625 return NULL;
4626 Py_INCREF(priority);
4627 PyStructSequence_SET_ITEM(res, 0, priority);
4628 return res;
4629}
4630
4631PyDoc_STRVAR(sched_param__doc__,
4632"sched_param(sched_priority): A scheduling parameter.\n\n\
4633Current has only one field: sched_priority");
4634
4635static PyStructSequence_Field sched_param_fields[] = {
4636 {"sched_priority", "the scheduling priority"},
4637 {0}
4638};
4639
4640static PyStructSequence_Desc sched_param_desc = {
4641 "sched_param", /* name */
4642 sched_param__doc__, /* doc */
4643 sched_param_fields,
4644 1
4645};
4646
4647static int
4648convert_sched_param(PyObject *param, struct sched_param *res)
4649{
4650 long priority;
4651
4652 if (Py_TYPE(param) != &SchedParamType) {
4653 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4654 return 0;
4655 }
4656 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4657 if (priority == -1 && PyErr_Occurred())
4658 return 0;
4659 if (priority > INT_MAX || priority < INT_MIN) {
4660 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4661 return 0;
4662 }
4663 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4664 return 1;
4665}
4666
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004667#endif
4668
4669#ifdef HAVE_SCHED_SETSCHEDULER
4670
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004671PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4672"sched_setscheduler(pid, policy, param)\n\n\
4673Set the scheduling policy, *policy*, for *pid*.\n\
4674If *pid* is 0, the calling process is changed.\n\
4675*param* is an instance of sched_param.");
4676
4677static PyObject *
4678posix_sched_setscheduler(PyObject *self, PyObject *args)
4679{
4680 pid_t pid;
4681 int policy;
4682 struct sched_param param;
4683
4684 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4685 &pid, &policy, &convert_sched_param, &param))
4686 return NULL;
4687 if (sched_setscheduler(pid, policy, &param))
4688 return posix_error();
4689 Py_RETURN_NONE;
4690}
4691
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004692#endif
4693
4694#ifdef HAVE_SCHED_SETPARAM
4695
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004696PyDoc_STRVAR(posix_sched_getparam__doc__,
4697"sched_getparam(pid) -> sched_param\n\n\
4698Returns scheduling parameters for the process with *pid* as an instance of the\n\
4699sched_param class. A PID of 0 means the calling process.");
4700
4701static PyObject *
4702posix_sched_getparam(PyObject *self, PyObject *args)
4703{
4704 pid_t pid;
4705 struct sched_param param;
4706 PyObject *res, *priority;
4707
4708 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4709 return NULL;
4710 if (sched_getparam(pid, &param))
4711 return posix_error();
4712 res = PyStructSequence_New(&SchedParamType);
4713 if (!res)
4714 return NULL;
4715 priority = PyLong_FromLong(param.sched_priority);
4716 if (!priority) {
4717 Py_DECREF(res);
4718 return NULL;
4719 }
4720 PyStructSequence_SET_ITEM(res, 0, priority);
4721 return res;
4722}
4723
4724PyDoc_STRVAR(posix_sched_setparam__doc__,
4725"sched_setparam(pid, param)\n\n\
4726Set scheduling parameters for a process with PID *pid*.\n\
4727A PID of 0 means the calling process.");
4728
4729static PyObject *
4730posix_sched_setparam(PyObject *self, PyObject *args)
4731{
4732 pid_t pid;
4733 struct sched_param param;
4734
4735 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4736 &pid, &convert_sched_param, &param))
4737 return NULL;
4738 if (sched_setparam(pid, &param))
4739 return posix_error();
4740 Py_RETURN_NONE;
4741}
4742
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004743#endif
4744
4745#ifdef HAVE_SCHED_RR_GET_INTERVAL
4746
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004747PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4748"sched_rr_get_interval(pid) -> float\n\n\
4749Return the round-robin quantum for the process with PID *pid* in seconds.");
4750
4751static PyObject *
4752posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4753{
4754 pid_t pid;
4755 struct timespec interval;
4756
4757 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4758 return NULL;
4759 if (sched_rr_get_interval(pid, &interval))
4760 return posix_error();
4761 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4762}
4763
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004764#endif
4765
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004766PyDoc_STRVAR(posix_sched_yield__doc__,
4767"sched_yield()\n\n\
4768Voluntarily relinquish the CPU.");
4769
4770static PyObject *
4771posix_sched_yield(PyObject *self, PyObject *noargs)
4772{
4773 if (sched_yield())
4774 return posix_error();
4775 Py_RETURN_NONE;
4776}
4777
Benjamin Peterson2740af82011-08-02 17:41:34 -05004778#ifdef HAVE_SCHED_SETAFFINITY
4779
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004780typedef struct {
4781 PyObject_HEAD;
4782 Py_ssize_t size;
4783 int ncpus;
4784 cpu_set_t *set;
4785} Py_cpu_set;
4786
4787static PyTypeObject cpu_set_type;
4788
4789static void
4790cpu_set_dealloc(Py_cpu_set *set)
4791{
4792 assert(set->set);
4793 CPU_FREE(set->set);
4794 Py_TYPE(set)->tp_free(set);
4795}
4796
4797static Py_cpu_set *
4798make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4799{
4800 Py_cpu_set *set;
4801
4802 if (size < 0) {
4803 PyErr_SetString(PyExc_ValueError, "negative size");
4804 return NULL;
4805 }
4806 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4807 if (!set)
4808 return NULL;
4809 set->ncpus = size;
4810 set->size = CPU_ALLOC_SIZE(size);
4811 set->set = CPU_ALLOC(size);
4812 if (!set->set) {
4813 type->tp_free(set);
4814 PyErr_NoMemory();
4815 return NULL;
4816 }
4817 CPU_ZERO_S(set->size, set->set);
4818 return set;
4819}
4820
4821static PyObject *
4822cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4823{
4824 int size;
4825
4826 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4827 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4828 return NULL;
4829 return (PyObject *)make_new_cpu_set(type, size);
4830}
4831
4832static PyObject *
4833cpu_set_repr(Py_cpu_set *set)
4834{
4835 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4836}
4837
4838static Py_ssize_t
4839cpu_set_len(Py_cpu_set *set)
4840{
4841 return set->ncpus;
4842}
4843
4844static int
4845_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4846{
4847 int cpu;
4848 if (!PyArg_ParseTuple(args, requester, &cpu))
4849 return -1;
4850 if (cpu < 0) {
4851 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4852 return -1;
4853 }
4854 if (cpu >= set->ncpus) {
4855 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4856 return -1;
4857 }
4858 return cpu;
4859}
4860
4861PyDoc_STRVAR(cpu_set_set_doc,
4862"cpu_set.set(i)\n\n\
4863Add CPU *i* to the set.");
4864
4865static PyObject *
4866cpu_set_set(Py_cpu_set *set, PyObject *args)
4867{
4868 int cpu = _get_cpu(set, "i|set", args);
4869 if (cpu == -1)
4870 return NULL;
4871 CPU_SET_S(cpu, set->size, set->set);
4872 Py_RETURN_NONE;
4873}
4874
4875PyDoc_STRVAR(cpu_set_count_doc,
4876"cpu_set.count() -> int\n\n\
4877Return the number of CPUs active in the set.");
4878
4879static PyObject *
4880cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4881{
4882 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4883}
4884
4885PyDoc_STRVAR(cpu_set_clear_doc,
4886"cpu_set.clear(i)\n\n\
4887Remove CPU *i* from the set.");
4888
4889static PyObject *
4890cpu_set_clear(Py_cpu_set *set, PyObject *args)
4891{
4892 int cpu = _get_cpu(set, "i|clear", args);
4893 if (cpu == -1)
4894 return NULL;
4895 CPU_CLR_S(cpu, set->size, set->set);
4896 Py_RETURN_NONE;
4897}
4898
4899PyDoc_STRVAR(cpu_set_isset_doc,
4900"cpu_set.isset(i) -> bool\n\n\
4901Test if CPU *i* is in the set.");
4902
4903static PyObject *
4904cpu_set_isset(Py_cpu_set *set, PyObject *args)
4905{
4906 int cpu = _get_cpu(set, "i|isset", args);
4907 if (cpu == -1)
4908 return NULL;
4909 if (CPU_ISSET_S(cpu, set->size, set->set))
4910 Py_RETURN_TRUE;
4911 Py_RETURN_FALSE;
4912}
4913
4914PyDoc_STRVAR(cpu_set_zero_doc,
4915"cpu_set.zero()\n\n\
4916Clear the cpu_set.");
4917
4918static PyObject *
4919cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4920{
4921 CPU_ZERO_S(set->size, set->set);
4922 Py_RETURN_NONE;
4923}
4924
4925static PyObject *
4926cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4927{
4928 int eq;
4929
4930 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) {
4931 Py_INCREF(Py_NotImplemented);
4932 return Py_NotImplemented;
4933 }
4934 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4935 if ((op == Py_EQ) ? eq : !eq)
4936 Py_RETURN_TRUE;
4937 else
4938 Py_RETURN_FALSE;
4939}
4940
4941#define CPU_SET_BINOP(name, op) \
4942 static PyObject * \
4943 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
4944 if (res) { \
4945 Py_INCREF(res); \
4946 } \
4947 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05004948 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004949 if (!res) \
4950 return NULL; \
4951 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05004952 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004953 Py_DECREF(res); \
4954 Py_INCREF(Py_NotImplemented); \
4955 return Py_NotImplemented; \
4956 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05004957 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004958 op(res->size, res->set, left->set, right->set); \
4959 return (PyObject *)res; \
4960 } \
4961 static PyObject * \
4962 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
4963 return do_cpu_set_##name(left, right, NULL); \
4964 } \
4965 static PyObject * \
4966 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
4967 return do_cpu_set_##name(left, right, left); \
4968 } \
4969
4970CPU_SET_BINOP(and, CPU_AND_S)
4971CPU_SET_BINOP(or, CPU_OR_S)
4972CPU_SET_BINOP(xor, CPU_XOR_S)
4973#undef CPU_SET_BINOP
4974
4975PyDoc_STRVAR(cpu_set_doc,
4976"cpu_set(size)\n\n\
4977Create an empty mask of CPUs.");
4978
4979static PyNumberMethods cpu_set_as_number = {
4980 0, /*nb_add*/
4981 0, /*nb_subtract*/
4982 0, /*nb_multiply*/
4983 0, /*nb_remainder*/
4984 0, /*nb_divmod*/
4985 0, /*nb_power*/
4986 0, /*nb_negative*/
4987 0, /*nb_positive*/
4988 0, /*nb_absolute*/
4989 0, /*nb_bool*/
4990 0, /*nb_invert*/
4991 0, /*nb_lshift*/
4992 0, /*nb_rshift*/
4993 (binaryfunc)cpu_set_and, /*nb_and*/
4994 (binaryfunc)cpu_set_xor, /*nb_xor*/
4995 (binaryfunc)cpu_set_or, /*nb_or*/
4996 0, /*nb_int*/
4997 0, /*nb_reserved*/
4998 0, /*nb_float*/
4999 0, /*nb_inplace_add*/
5000 0, /*nb_inplace_subtract*/
5001 0, /*nb_inplace_multiply*/
5002 0, /*nb_inplace_remainder*/
5003 0, /*nb_inplace_power*/
5004 0, /*nb_inplace_lshift*/
5005 0, /*nb_inplace_rshift*/
5006 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5007 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5008 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5009};
5010
5011static PySequenceMethods cpu_set_as_sequence = {
5012 (lenfunc)cpu_set_len, /* sq_length */
5013};
5014
5015static PyMethodDef cpu_set_methods[] = {
5016 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5017 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5018 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5019 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5020 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5021 {NULL, NULL} /* sentinel */
5022};
5023
5024static PyTypeObject cpu_set_type = {
5025 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5026 "posix.cpu_set", /* tp_name */
5027 sizeof(Py_cpu_set), /* tp_basicsize */
5028 0, /* tp_itemsize */
5029 /* methods */
5030 (destructor)cpu_set_dealloc, /* tp_dealloc */
5031 0, /* tp_print */
5032 0, /* tp_getattr */
5033 0, /* tp_setattr */
5034 0, /* tp_reserved */
5035 (reprfunc)cpu_set_repr, /* tp_repr */
5036 &cpu_set_as_number, /* tp_as_number */
5037 &cpu_set_as_sequence, /* tp_as_sequence */
5038 0, /* tp_as_mapping */
5039 PyObject_HashNotImplemented, /* tp_hash */
5040 0, /* tp_call */
5041 0, /* tp_str */
5042 PyObject_GenericGetAttr, /* tp_getattro */
5043 0, /* tp_setattro */
5044 0, /* tp_as_buffer */
5045 Py_TPFLAGS_DEFAULT, /* tp_flags */
5046 cpu_set_doc, /* tp_doc */
5047 0, /* tp_traverse */
5048 0, /* tp_clear */
5049 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5050 0, /* tp_weaklistoffset */
5051 0, /* tp_iter */
5052 0, /* tp_iternext */
5053 cpu_set_methods, /* tp_methods */
5054 0, /* tp_members */
5055 0, /* tp_getset */
5056 0, /* tp_base */
5057 0, /* tp_dict */
5058 0, /* tp_descr_get */
5059 0, /* tp_descr_set */
5060 0, /* tp_dictoffset */
5061 0, /* tp_init */
5062 PyType_GenericAlloc, /* tp_alloc */
5063 cpu_set_new, /* tp_new */
5064 PyObject_Del, /* tp_free */
5065};
5066
5067PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5068"sched_setaffinity(pid, cpu_set)\n\n\
5069Set the affinity of the process with PID *pid* to *cpu_set*.");
5070
5071static PyObject *
5072posix_sched_setaffinity(PyObject *self, PyObject *args)
5073{
5074 pid_t pid;
5075 Py_cpu_set *cpu_set;
5076
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005077 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:schbed_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005078 &pid, &cpu_set_type, &cpu_set))
5079 return NULL;
5080 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5081 return posix_error();
5082 Py_RETURN_NONE;
5083}
5084
5085PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5086"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5087Return the affinity of the process with PID *pid*.\n\
5088The returned cpu_set will be of size *ncpus*.");
5089
5090static PyObject *
5091posix_sched_getaffinity(PyObject *self, PyObject *args)
5092{
5093 pid_t pid;
5094 int ncpus;
5095 Py_cpu_set *res;
5096
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005097 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005098 &pid, &ncpus))
5099 return NULL;
5100 res = make_new_cpu_set(&cpu_set_type, ncpus);
5101 if (!res)
5102 return NULL;
5103 if (sched_getaffinity(pid, res->size, res->set)) {
5104 Py_DECREF(res);
5105 return posix_error();
5106 }
5107 return (PyObject *)res;
5108}
5109
Benjamin Peterson2740af82011-08-02 17:41:34 -05005110#endif /* HAVE_SCHED_SETAFFINITY */
5111
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005112#endif /* HAVE_SCHED_H */
5113
Neal Norwitzb59798b2003-03-21 01:43:31 +00005114/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005115/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5116#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005117#define DEV_PTY_FILE "/dev/ptc"
5118#define HAVE_DEV_PTMX
5119#else
5120#define DEV_PTY_FILE "/dev/ptmx"
5121#endif
5122
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005123#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005124#ifdef HAVE_PTY_H
5125#include <pty.h>
5126#else
5127#ifdef HAVE_LIBUTIL_H
5128#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005129#else
5130#ifdef HAVE_UTIL_H
5131#include <util.h>
5132#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005133#endif /* HAVE_LIBUTIL_H */
5134#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005135#ifdef HAVE_STROPTS_H
5136#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005137#endif
5138#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005139
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005140#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005141PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005142"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005143Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005144
5145static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005146posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005147{
Victor Stinner8c62be82010-05-06 00:08:46 +00005148 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005149#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005150 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005151#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005152#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005153 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005154#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005155 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005156#endif
5157#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005158
Thomas Wouters70c21a12000-07-14 14:28:33 +00005159#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005160 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5161 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005162#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005163 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5164 if (slave_name == NULL)
5165 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005166
Victor Stinner8c62be82010-05-06 00:08:46 +00005167 slave_fd = open(slave_name, O_RDWR);
5168 if (slave_fd < 0)
5169 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005170#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005171 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5172 if (master_fd < 0)
5173 return posix_error();
5174 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5175 /* change permission of slave */
5176 if (grantpt(master_fd) < 0) {
5177 PyOS_setsig(SIGCHLD, sig_saved);
5178 return posix_error();
5179 }
5180 /* unlock slave */
5181 if (unlockpt(master_fd) < 0) {
5182 PyOS_setsig(SIGCHLD, sig_saved);
5183 return posix_error();
5184 }
5185 PyOS_setsig(SIGCHLD, sig_saved);
5186 slave_name = ptsname(master_fd); /* get name of slave */
5187 if (slave_name == NULL)
5188 return posix_error();
5189 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5190 if (slave_fd < 0)
5191 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005192#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5194 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005195#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005196 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005197#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005198#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005199#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005200
Victor Stinner8c62be82010-05-06 00:08:46 +00005201 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005202
Fred Drake8cef4cf2000-06-28 16:40:38 +00005203}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005204#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005205
5206#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005207PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005208"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005209Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5210Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005211To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005212
5213static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005214posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005215{
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 int master_fd = -1, result = 0;
5217 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005218
Victor Stinner8c62be82010-05-06 00:08:46 +00005219 _PyImport_AcquireLock();
5220 pid = forkpty(&master_fd, NULL, NULL, NULL);
5221 if (pid == 0) {
5222 /* child: this clobbers and resets the import lock. */
5223 PyOS_AfterFork();
5224 } else {
5225 /* parent: release the import lock. */
5226 result = _PyImport_ReleaseLock();
5227 }
5228 if (pid == -1)
5229 return posix_error();
5230 if (result < 0) {
5231 /* Don't clobber the OSError if the fork failed. */
5232 PyErr_SetString(PyExc_RuntimeError,
5233 "not holding the import lock");
5234 return NULL;
5235 }
5236 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005237}
5238#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005239
Ross Lagerwall7807c352011-03-17 20:20:30 +02005240
Guido van Rossumad0ee831995-03-01 10:34:45 +00005241#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005242PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005243"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005244Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005245
Barry Warsaw53699e91996-12-10 23:23:01 +00005246static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005247posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005248{
Victor Stinner8c62be82010-05-06 00:08:46 +00005249 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005250}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005251#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005253
Guido van Rossumad0ee831995-03-01 10:34:45 +00005254#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005255PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005256"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005257Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005258
Barry Warsaw53699e91996-12-10 23:23:01 +00005259static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005260posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005261{
Victor Stinner8c62be82010-05-06 00:08:46 +00005262 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005263}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005264#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005266
Guido van Rossumad0ee831995-03-01 10:34:45 +00005267#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005268PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005269"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005271
Barry Warsaw53699e91996-12-10 23:23:01 +00005272static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005273posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005274{
Victor Stinner8c62be82010-05-06 00:08:46 +00005275 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005276}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005277#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005281"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005282Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005283
Barry Warsaw53699e91996-12-10 23:23:01 +00005284static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005285posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005286{
Victor Stinner8c62be82010-05-06 00:08:46 +00005287 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005288}
5289
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005290#ifdef HAVE_GETGROUPLIST
5291PyDoc_STRVAR(posix_getgrouplist__doc__,
5292"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5293Returns a list of groups to which a user belongs.\n\n\
5294 user: username to lookup\n\
5295 group: base group id of the user");
5296
5297static PyObject *
5298posix_getgrouplist(PyObject *self, PyObject *args)
5299{
5300#ifdef NGROUPS_MAX
5301#define MAX_GROUPS NGROUPS_MAX
5302#else
5303 /* defined to be 16 on Solaris7, so this should be a small number */
5304#define MAX_GROUPS 64
5305#endif
5306
5307 const char *user;
5308 int i, ngroups;
5309 PyObject *list;
5310#ifdef __APPLE__
5311 int *groups, basegid;
5312#else
5313 gid_t *groups, basegid;
5314#endif
5315 ngroups = MAX_GROUPS;
5316
5317 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5318 return NULL;
5319
5320#ifdef __APPLE__
5321 groups = PyMem_Malloc(ngroups * sizeof(int));
5322#else
5323 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5324#endif
5325 if (groups == NULL)
5326 return PyErr_NoMemory();
5327
5328 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5329 PyMem_Del(groups);
5330 return posix_error();
5331 }
5332
5333 list = PyList_New(ngroups);
5334 if (list == NULL) {
5335 PyMem_Del(groups);
5336 return NULL;
5337 }
5338
5339 for (i = 0; i < ngroups; i++) {
5340 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5341 if (o == NULL) {
5342 Py_DECREF(list);
5343 PyMem_Del(groups);
5344 return NULL;
5345 }
5346 PyList_SET_ITEM(list, i, o);
5347 }
5348
5349 PyMem_Del(groups);
5350
5351 return list;
5352}
5353#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005354
Fred Drakec9680921999-12-13 16:37:25 +00005355#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005356PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005357"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005359
5360static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005361posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005362{
5363 PyObject *result = NULL;
5364
Fred Drakec9680921999-12-13 16:37:25 +00005365#ifdef NGROUPS_MAX
5366#define MAX_GROUPS NGROUPS_MAX
5367#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005368 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005369#define MAX_GROUPS 64
5370#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005371 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005372
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005373 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005374 * This is a helper variable to store the intermediate result when
5375 * that happens.
5376 *
5377 * To keep the code readable the OSX behaviour is unconditional,
5378 * according to the POSIX spec this should be safe on all unix-y
5379 * systems.
5380 */
5381 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005382 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005383
Victor Stinner8c62be82010-05-06 00:08:46 +00005384 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005385 if (n < 0) {
5386 if (errno == EINVAL) {
5387 n = getgroups(0, NULL);
5388 if (n == -1) {
5389 return posix_error();
5390 }
5391 if (n == 0) {
5392 /* Avoid malloc(0) */
5393 alt_grouplist = grouplist;
5394 } else {
5395 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5396 if (alt_grouplist == NULL) {
5397 errno = EINVAL;
5398 return posix_error();
5399 }
5400 n = getgroups(n, alt_grouplist);
5401 if (n == -1) {
5402 PyMem_Free(alt_grouplist);
5403 return posix_error();
5404 }
5405 }
5406 } else {
5407 return posix_error();
5408 }
5409 }
5410 result = PyList_New(n);
5411 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005412 int i;
5413 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005414 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005415 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005416 Py_DECREF(result);
5417 result = NULL;
5418 break;
Fred Drakec9680921999-12-13 16:37:25 +00005419 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005420 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005421 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005422 }
5423
5424 if (alt_grouplist != grouplist) {
5425 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005426 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005427
Fred Drakec9680921999-12-13 16:37:25 +00005428 return result;
5429}
5430#endif
5431
Antoine Pitroub7572f02009-12-02 20:46:48 +00005432#ifdef HAVE_INITGROUPS
5433PyDoc_STRVAR(posix_initgroups__doc__,
5434"initgroups(username, gid) -> None\n\n\
5435Call the system initgroups() to initialize the group access list with all of\n\
5436the groups of which the specified username is a member, plus the specified\n\
5437group id.");
5438
5439static PyObject *
5440posix_initgroups(PyObject *self, PyObject *args)
5441{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005442 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005443 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005444 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005445 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005446
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005447 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5448 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005450 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005451
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005452 res = initgroups(username, (gid_t) gid);
5453 Py_DECREF(oname);
5454 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005455 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005456
Victor Stinner8c62be82010-05-06 00:08:46 +00005457 Py_INCREF(Py_None);
5458 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005459}
5460#endif
5461
Martin v. Löwis606edc12002-06-13 21:09:11 +00005462#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005463PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005464"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005465Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005466
5467static PyObject *
5468posix_getpgid(PyObject *self, PyObject *args)
5469{
Victor Stinner8c62be82010-05-06 00:08:46 +00005470 pid_t pid, pgid;
5471 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5472 return NULL;
5473 pgid = getpgid(pid);
5474 if (pgid < 0)
5475 return posix_error();
5476 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005477}
5478#endif /* HAVE_GETPGID */
5479
5480
Guido van Rossumb6775db1994-08-01 11:34:53 +00005481#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005483"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005485
Barry Warsaw53699e91996-12-10 23:23:01 +00005486static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005487posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005488{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005489#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005490 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005491#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005492 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005493#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005494}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005495#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005496
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005497
Guido van Rossumb6775db1994-08-01 11:34:53 +00005498#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005499PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005500"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005501Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005502
Barry Warsaw53699e91996-12-10 23:23:01 +00005503static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005504posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005505{
Guido van Rossum64933891994-10-20 21:56:42 +00005506#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005507 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005508#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005509 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005510#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 return posix_error();
5512 Py_INCREF(Py_None);
5513 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005514}
5515
Guido van Rossumb6775db1994-08-01 11:34:53 +00005516#endif /* HAVE_SETPGRP */
5517
Guido van Rossumad0ee831995-03-01 10:34:45 +00005518#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005519
5520#ifdef MS_WINDOWS
5521#include <tlhelp32.h>
5522
5523static PyObject*
5524win32_getppid()
5525{
5526 HANDLE snapshot;
5527 pid_t mypid;
5528 PyObject* result = NULL;
5529 BOOL have_record;
5530 PROCESSENTRY32 pe;
5531
5532 mypid = getpid(); /* This function never fails */
5533
5534 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5535 if (snapshot == INVALID_HANDLE_VALUE)
5536 return PyErr_SetFromWindowsErr(GetLastError());
5537
5538 pe.dwSize = sizeof(pe);
5539 have_record = Process32First(snapshot, &pe);
5540 while (have_record) {
5541 if (mypid == (pid_t)pe.th32ProcessID) {
5542 /* We could cache the ulong value in a static variable. */
5543 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5544 break;
5545 }
5546
5547 have_record = Process32Next(snapshot, &pe);
5548 }
5549
5550 /* If our loop exits and our pid was not found (result will be NULL)
5551 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5552 * error anyway, so let's raise it. */
5553 if (!result)
5554 result = PyErr_SetFromWindowsErr(GetLastError());
5555
5556 CloseHandle(snapshot);
5557
5558 return result;
5559}
5560#endif /*MS_WINDOWS*/
5561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005562PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005563"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005564Return the parent's process id. If the parent process has already exited,\n\
5565Windows machines will still return its id; others systems will return the id\n\
5566of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Barry Warsaw53699e91996-12-10 23:23:01 +00005568static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005569posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005570{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005571#ifdef MS_WINDOWS
5572 return win32_getppid();
5573#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005574 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005575#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005576}
5577#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005578
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005579
Fred Drake12c6e2d1999-12-14 21:25:03 +00005580#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005582"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005583Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005584
5585static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005586posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005587{
Victor Stinner8c62be82010-05-06 00:08:46 +00005588 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005589#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005590 wchar_t user_name[UNLEN + 1];
5591 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5592
5593 if (GetUserNameW(user_name, &num_chars)) {
5594 /* num_chars is the number of unicode chars plus null terminator */
5595 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005596 }
5597 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005598 result = PyErr_SetFromWindowsErr(GetLastError());
5599#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005600 char *name;
5601 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005602
Victor Stinner8c62be82010-05-06 00:08:46 +00005603 errno = 0;
5604 name = getlogin();
5605 if (name == NULL) {
5606 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005607 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005608 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005609 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005610 }
5611 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005612 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005613 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005614#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005615 return result;
5616}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005617#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005618
Guido van Rossumad0ee831995-03-01 10:34:45 +00005619#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005621"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005622Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005623
Barry Warsaw53699e91996-12-10 23:23:01 +00005624static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005625posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005626{
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005628}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005629#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005631
Guido van Rossumad0ee831995-03-01 10:34:45 +00005632#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005633PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005634"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005635Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005636
Barry Warsaw53699e91996-12-10 23:23:01 +00005637static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005638posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005639{
Victor Stinner8c62be82010-05-06 00:08:46 +00005640 pid_t pid;
5641 int sig;
5642 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5643 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005644#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005645 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5646 APIRET rc;
5647 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005648 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005649
5650 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5651 APIRET rc;
5652 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005653 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005654
5655 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005656 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005657#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 if (kill(pid, sig) == -1)
5659 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005660#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005661 Py_INCREF(Py_None);
5662 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005663}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005664#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005665
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005666#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005667PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005668"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005669Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005670
5671static PyObject *
5672posix_killpg(PyObject *self, PyObject *args)
5673{
Victor Stinner8c62be82010-05-06 00:08:46 +00005674 int sig;
5675 pid_t pgid;
5676 /* XXX some man pages make the `pgid` parameter an int, others
5677 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5678 take the same type. Moreover, pid_t is always at least as wide as
5679 int (else compilation of this module fails), which is safe. */
5680 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5681 return NULL;
5682 if (killpg(pgid, sig) == -1)
5683 return posix_error();
5684 Py_INCREF(Py_None);
5685 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005686}
5687#endif
5688
Brian Curtineb24d742010-04-12 17:16:38 +00005689#ifdef MS_WINDOWS
5690PyDoc_STRVAR(win32_kill__doc__,
5691"kill(pid, sig)\n\n\
5692Kill a process with a signal.");
5693
5694static PyObject *
5695win32_kill(PyObject *self, PyObject *args)
5696{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005697 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005698 DWORD pid, sig, err;
5699 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005700
Victor Stinner8c62be82010-05-06 00:08:46 +00005701 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5702 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005703
Victor Stinner8c62be82010-05-06 00:08:46 +00005704 /* Console processes which share a common console can be sent CTRL+C or
5705 CTRL+BREAK events, provided they handle said events. */
5706 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5707 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5708 err = GetLastError();
5709 PyErr_SetFromWindowsErr(err);
5710 }
5711 else
5712 Py_RETURN_NONE;
5713 }
Brian Curtineb24d742010-04-12 17:16:38 +00005714
Victor Stinner8c62be82010-05-06 00:08:46 +00005715 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5716 attempt to open and terminate the process. */
5717 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5718 if (handle == NULL) {
5719 err = GetLastError();
5720 return PyErr_SetFromWindowsErr(err);
5721 }
Brian Curtineb24d742010-04-12 17:16:38 +00005722
Victor Stinner8c62be82010-05-06 00:08:46 +00005723 if (TerminateProcess(handle, sig) == 0) {
5724 err = GetLastError();
5725 result = PyErr_SetFromWindowsErr(err);
5726 } else {
5727 Py_INCREF(Py_None);
5728 result = Py_None;
5729 }
Brian Curtineb24d742010-04-12 17:16:38 +00005730
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 CloseHandle(handle);
5732 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005733}
5734#endif /* MS_WINDOWS */
5735
Guido van Rossumc0125471996-06-28 18:55:32 +00005736#ifdef HAVE_PLOCK
5737
5738#ifdef HAVE_SYS_LOCK_H
5739#include <sys/lock.h>
5740#endif
5741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005742PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005743"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005744Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005745
Barry Warsaw53699e91996-12-10 23:23:01 +00005746static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005747posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005748{
Victor Stinner8c62be82010-05-06 00:08:46 +00005749 int op;
5750 if (!PyArg_ParseTuple(args, "i:plock", &op))
5751 return NULL;
5752 if (plock(op) == -1)
5753 return posix_error();
5754 Py_INCREF(Py_None);
5755 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005756}
5757#endif
5758
Guido van Rossumb6775db1994-08-01 11:34:53 +00005759#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005760PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005761"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005762Set the current process's user id.");
5763
Barry Warsaw53699e91996-12-10 23:23:01 +00005764static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005765posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005766{
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 long uid_arg;
5768 uid_t uid;
5769 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5770 return NULL;
5771 uid = uid_arg;
5772 if (uid != uid_arg) {
5773 PyErr_SetString(PyExc_OverflowError, "user id too big");
5774 return NULL;
5775 }
5776 if (setuid(uid) < 0)
5777 return posix_error();
5778 Py_INCREF(Py_None);
5779 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005780}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005781#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005783
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005784#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005785PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005786"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005787Set the current process's effective user id.");
5788
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005789static PyObject *
5790posix_seteuid (PyObject *self, PyObject *args)
5791{
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 long euid_arg;
5793 uid_t euid;
5794 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5795 return NULL;
5796 euid = euid_arg;
5797 if (euid != euid_arg) {
5798 PyErr_SetString(PyExc_OverflowError, "user id too big");
5799 return NULL;
5800 }
5801 if (seteuid(euid) < 0) {
5802 return posix_error();
5803 } else {
5804 Py_INCREF(Py_None);
5805 return Py_None;
5806 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005807}
5808#endif /* HAVE_SETEUID */
5809
5810#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005811PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005812"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005813Set the current process's effective group id.");
5814
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005815static PyObject *
5816posix_setegid (PyObject *self, PyObject *args)
5817{
Victor Stinner8c62be82010-05-06 00:08:46 +00005818 long egid_arg;
5819 gid_t egid;
5820 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5821 return NULL;
5822 egid = egid_arg;
5823 if (egid != egid_arg) {
5824 PyErr_SetString(PyExc_OverflowError, "group id too big");
5825 return NULL;
5826 }
5827 if (setegid(egid) < 0) {
5828 return posix_error();
5829 } else {
5830 Py_INCREF(Py_None);
5831 return Py_None;
5832 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005833}
5834#endif /* HAVE_SETEGID */
5835
5836#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005838"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005839Set the current process's real and effective user ids.");
5840
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005841static PyObject *
5842posix_setreuid (PyObject *self, PyObject *args)
5843{
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 long ruid_arg, euid_arg;
5845 uid_t ruid, euid;
5846 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5847 return NULL;
5848 if (ruid_arg == -1)
5849 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5850 else
5851 ruid = ruid_arg; /* otherwise, assign from our long */
5852 if (euid_arg == -1)
5853 euid = (uid_t)-1;
5854 else
5855 euid = euid_arg;
5856 if ((euid_arg != -1 && euid != euid_arg) ||
5857 (ruid_arg != -1 && ruid != ruid_arg)) {
5858 PyErr_SetString(PyExc_OverflowError, "user id too big");
5859 return NULL;
5860 }
5861 if (setreuid(ruid, euid) < 0) {
5862 return posix_error();
5863 } else {
5864 Py_INCREF(Py_None);
5865 return Py_None;
5866 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005867}
5868#endif /* HAVE_SETREUID */
5869
5870#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005871PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005872"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005873Set the current process's real and effective group ids.");
5874
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005875static PyObject *
5876posix_setregid (PyObject *self, PyObject *args)
5877{
Victor Stinner8c62be82010-05-06 00:08:46 +00005878 long rgid_arg, egid_arg;
5879 gid_t rgid, egid;
5880 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5881 return NULL;
5882 if (rgid_arg == -1)
5883 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5884 else
5885 rgid = rgid_arg; /* otherwise, assign from our long */
5886 if (egid_arg == -1)
5887 egid = (gid_t)-1;
5888 else
5889 egid = egid_arg;
5890 if ((egid_arg != -1 && egid != egid_arg) ||
5891 (rgid_arg != -1 && rgid != rgid_arg)) {
5892 PyErr_SetString(PyExc_OverflowError, "group id too big");
5893 return NULL;
5894 }
5895 if (setregid(rgid, egid) < 0) {
5896 return posix_error();
5897 } else {
5898 Py_INCREF(Py_None);
5899 return Py_None;
5900 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005901}
5902#endif /* HAVE_SETREGID */
5903
Guido van Rossumb6775db1994-08-01 11:34:53 +00005904#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005906"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005907Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005908
Barry Warsaw53699e91996-12-10 23:23:01 +00005909static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005910posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005911{
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 long gid_arg;
5913 gid_t gid;
5914 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5915 return NULL;
5916 gid = gid_arg;
5917 if (gid != gid_arg) {
5918 PyErr_SetString(PyExc_OverflowError, "group id too big");
5919 return NULL;
5920 }
5921 if (setgid(gid) < 0)
5922 return posix_error();
5923 Py_INCREF(Py_None);
5924 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005925}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005926#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005927
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005928#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005929PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005930"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005931Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005932
5933static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005934posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005935{
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 int i, len;
5937 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005938
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 if (!PySequence_Check(groups)) {
5940 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5941 return NULL;
5942 }
5943 len = PySequence_Size(groups);
5944 if (len > MAX_GROUPS) {
5945 PyErr_SetString(PyExc_ValueError, "too many groups");
5946 return NULL;
5947 }
5948 for(i = 0; i < len; i++) {
5949 PyObject *elem;
5950 elem = PySequence_GetItem(groups, i);
5951 if (!elem)
5952 return NULL;
5953 if (!PyLong_Check(elem)) {
5954 PyErr_SetString(PyExc_TypeError,
5955 "groups must be integers");
5956 Py_DECREF(elem);
5957 return NULL;
5958 } else {
5959 unsigned long x = PyLong_AsUnsignedLong(elem);
5960 if (PyErr_Occurred()) {
5961 PyErr_SetString(PyExc_TypeError,
5962 "group id too big");
5963 Py_DECREF(elem);
5964 return NULL;
5965 }
5966 grouplist[i] = x;
5967 /* read back the value to see if it fitted in gid_t */
5968 if (grouplist[i] != x) {
5969 PyErr_SetString(PyExc_TypeError,
5970 "group id too big");
5971 Py_DECREF(elem);
5972 return NULL;
5973 }
5974 }
5975 Py_DECREF(elem);
5976 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005977
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 if (setgroups(len, grouplist) < 0)
5979 return posix_error();
5980 Py_INCREF(Py_None);
5981 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005982}
5983#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005984
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005985#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5986static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005987wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005988{
Victor Stinner8c62be82010-05-06 00:08:46 +00005989 PyObject *result;
5990 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005991
Victor Stinner8c62be82010-05-06 00:08:46 +00005992 if (pid == -1)
5993 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005994
Victor Stinner8c62be82010-05-06 00:08:46 +00005995 if (struct_rusage == NULL) {
5996 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5997 if (m == NULL)
5998 return NULL;
5999 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6000 Py_DECREF(m);
6001 if (struct_rusage == NULL)
6002 return NULL;
6003 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006004
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6006 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6007 if (!result)
6008 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006009
6010#ifndef doubletime
6011#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6012#endif
6013
Victor Stinner8c62be82010-05-06 00:08:46 +00006014 PyStructSequence_SET_ITEM(result, 0,
6015 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6016 PyStructSequence_SET_ITEM(result, 1,
6017 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006018#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6020 SET_INT(result, 2, ru->ru_maxrss);
6021 SET_INT(result, 3, ru->ru_ixrss);
6022 SET_INT(result, 4, ru->ru_idrss);
6023 SET_INT(result, 5, ru->ru_isrss);
6024 SET_INT(result, 6, ru->ru_minflt);
6025 SET_INT(result, 7, ru->ru_majflt);
6026 SET_INT(result, 8, ru->ru_nswap);
6027 SET_INT(result, 9, ru->ru_inblock);
6028 SET_INT(result, 10, ru->ru_oublock);
6029 SET_INT(result, 11, ru->ru_msgsnd);
6030 SET_INT(result, 12, ru->ru_msgrcv);
6031 SET_INT(result, 13, ru->ru_nsignals);
6032 SET_INT(result, 14, ru->ru_nvcsw);
6033 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006034#undef SET_INT
6035
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 if (PyErr_Occurred()) {
6037 Py_DECREF(result);
6038 return NULL;
6039 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006040
Victor Stinner8c62be82010-05-06 00:08:46 +00006041 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006042}
6043#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6044
6045#ifdef HAVE_WAIT3
6046PyDoc_STRVAR(posix_wait3__doc__,
6047"wait3(options) -> (pid, status, rusage)\n\n\
6048Wait for completion of a child process.");
6049
6050static PyObject *
6051posix_wait3(PyObject *self, PyObject *args)
6052{
Victor Stinner8c62be82010-05-06 00:08:46 +00006053 pid_t pid;
6054 int options;
6055 struct rusage ru;
6056 WAIT_TYPE status;
6057 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006058
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6060 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006061
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 Py_BEGIN_ALLOW_THREADS
6063 pid = wait3(&status, options, &ru);
6064 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006065
Victor Stinner8c62be82010-05-06 00:08:46 +00006066 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006067}
6068#endif /* HAVE_WAIT3 */
6069
6070#ifdef HAVE_WAIT4
6071PyDoc_STRVAR(posix_wait4__doc__,
6072"wait4(pid, options) -> (pid, status, rusage)\n\n\
6073Wait for completion of a given child process.");
6074
6075static PyObject *
6076posix_wait4(PyObject *self, PyObject *args)
6077{
Victor Stinner8c62be82010-05-06 00:08:46 +00006078 pid_t pid;
6079 int options;
6080 struct rusage ru;
6081 WAIT_TYPE status;
6082 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006083
Victor Stinner8c62be82010-05-06 00:08:46 +00006084 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6085 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006086
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 Py_BEGIN_ALLOW_THREADS
6088 pid = wait4(pid, &status, options, &ru);
6089 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006090
Victor Stinner8c62be82010-05-06 00:08:46 +00006091 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006092}
6093#endif /* HAVE_WAIT4 */
6094
Ross Lagerwall7807c352011-03-17 20:20:30 +02006095#if defined(HAVE_WAITID) && !defined(__APPLE__)
6096PyDoc_STRVAR(posix_waitid__doc__,
6097"waitid(idtype, id, options) -> waitid_result\n\n\
6098Wait for the completion of one or more child processes.\n\n\
6099idtype can be P_PID, P_PGID or P_ALL.\n\
6100id specifies the pid to wait on.\n\
6101options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6102or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6103Returns either waitid_result or None if WNOHANG is specified and there are\n\
6104no children in a waitable state.");
6105
6106static PyObject *
6107posix_waitid(PyObject *self, PyObject *args)
6108{
6109 PyObject *result;
6110 idtype_t idtype;
6111 id_t id;
6112 int options, res;
6113 siginfo_t si;
6114 si.si_pid = 0;
6115 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6116 return NULL;
6117 Py_BEGIN_ALLOW_THREADS
6118 res = waitid(idtype, id, &si, options);
6119 Py_END_ALLOW_THREADS
6120 if (res == -1)
6121 return posix_error();
6122
6123 if (si.si_pid == 0)
6124 Py_RETURN_NONE;
6125
6126 result = PyStructSequence_New(&WaitidResultType);
6127 if (!result)
6128 return NULL;
6129
6130 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6131 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6132 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6133 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6134 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6135 if (PyErr_Occurred()) {
6136 Py_DECREF(result);
6137 return NULL;
6138 }
6139
6140 return result;
6141}
6142#endif
6143
Guido van Rossumb6775db1994-08-01 11:34:53 +00006144#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006145PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006146"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006147Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006148
Barry Warsaw53699e91996-12-10 23:23:01 +00006149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006150posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006151{
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 pid_t pid;
6153 int options;
6154 WAIT_TYPE status;
6155 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006156
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6158 return NULL;
6159 Py_BEGIN_ALLOW_THREADS
6160 pid = waitpid(pid, &status, options);
6161 Py_END_ALLOW_THREADS
6162 if (pid == -1)
6163 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006164
Victor Stinner8c62be82010-05-06 00:08:46 +00006165 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006166}
6167
Tim Petersab034fa2002-02-01 11:27:43 +00006168#elif defined(HAVE_CWAIT)
6169
6170/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006172"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006173"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006174
6175static PyObject *
6176posix_waitpid(PyObject *self, PyObject *args)
6177{
Victor Stinner8c62be82010-05-06 00:08:46 +00006178 Py_intptr_t pid;
6179 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006180
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6182 return NULL;
6183 Py_BEGIN_ALLOW_THREADS
6184 pid = _cwait(&status, pid, options);
6185 Py_END_ALLOW_THREADS
6186 if (pid == -1)
6187 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006188
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 /* shift the status left a byte so this is more like the POSIX waitpid */
6190 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006191}
6192#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006193
Guido van Rossumad0ee831995-03-01 10:34:45 +00006194#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006196"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006197Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006198
Barry Warsaw53699e91996-12-10 23:23:01 +00006199static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006200posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006201{
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 pid_t pid;
6203 WAIT_TYPE status;
6204 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006205
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 Py_BEGIN_ALLOW_THREADS
6207 pid = wait(&status);
6208 Py_END_ALLOW_THREADS
6209 if (pid == -1)
6210 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006211
Victor Stinner8c62be82010-05-06 00:08:46 +00006212 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006213}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006214#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006215
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006217PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006218"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006219Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006220
Barry Warsaw53699e91996-12-10 23:23:01 +00006221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006222posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006223{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006224#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006226#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006227#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006228 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006229 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006230#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006231 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006232#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006233#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006234}
6235
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006236
Guido van Rossumb6775db1994-08-01 11:34:53 +00006237#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006239"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006240Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006241
Barry Warsaw53699e91996-12-10 23:23:01 +00006242static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006243posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006244{
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 PyObject* v;
6246 char buf[MAXPATHLEN];
6247 PyObject *opath;
6248 char *path;
6249 int n;
6250 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006251
Victor Stinner8c62be82010-05-06 00:08:46 +00006252 if (!PyArg_ParseTuple(args, "O&:readlink",
6253 PyUnicode_FSConverter, &opath))
6254 return NULL;
6255 path = PyBytes_AsString(opath);
6256 v = PySequence_GetItem(args, 0);
6257 if (v == NULL) {
6258 Py_DECREF(opath);
6259 return NULL;
6260 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006261
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 if (PyUnicode_Check(v)) {
6263 arg_is_unicode = 1;
6264 }
6265 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006266
Victor Stinner8c62be82010-05-06 00:08:46 +00006267 Py_BEGIN_ALLOW_THREADS
6268 n = readlink(path, buf, (int) sizeof buf);
6269 Py_END_ALLOW_THREADS
6270 if (n < 0)
6271 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006272
Victor Stinner8c62be82010-05-06 00:08:46 +00006273 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006274 if (arg_is_unicode)
6275 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6276 else
6277 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006278}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006279#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006280
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006281
Brian Curtin52173d42010-12-02 18:29:18 +00006282#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006283PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006284"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006285Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006286
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006287static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006288posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006289{
Victor Stinner8c62be82010-05-06 00:08:46 +00006290 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006291}
6292#endif /* HAVE_SYMLINK */
6293
Brian Curtind40e6f72010-07-08 21:39:08 +00006294#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6295
6296PyDoc_STRVAR(win_readlink__doc__,
6297"readlink(path) -> path\n\n\
6298Return a string representing the path to which the symbolic link points.");
6299
Brian Curtind40e6f72010-07-08 21:39:08 +00006300/* Windows readlink implementation */
6301static PyObject *
6302win_readlink(PyObject *self, PyObject *args)
6303{
6304 wchar_t *path;
6305 DWORD n_bytes_returned;
6306 DWORD io_result;
6307 PyObject *result;
6308 HANDLE reparse_point_handle;
6309
6310 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6311 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6312 wchar_t *print_name;
6313
6314 if (!PyArg_ParseTuple(args,
6315 "u:readlink",
6316 &path))
6317 return NULL;
6318
6319 /* First get a handle to the reparse point */
6320 Py_BEGIN_ALLOW_THREADS
6321 reparse_point_handle = CreateFileW(
6322 path,
6323 0,
6324 0,
6325 0,
6326 OPEN_EXISTING,
6327 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6328 0);
6329 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006330
Brian Curtind40e6f72010-07-08 21:39:08 +00006331 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6332 {
6333 return win32_error_unicode("readlink", path);
6334 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006335
Brian Curtind40e6f72010-07-08 21:39:08 +00006336 Py_BEGIN_ALLOW_THREADS
6337 /* New call DeviceIoControl to read the reparse point */
6338 io_result = DeviceIoControl(
6339 reparse_point_handle,
6340 FSCTL_GET_REPARSE_POINT,
6341 0, 0, /* in buffer */
6342 target_buffer, sizeof(target_buffer),
6343 &n_bytes_returned,
6344 0 /* we're not using OVERLAPPED_IO */
6345 );
6346 CloseHandle(reparse_point_handle);
6347 Py_END_ALLOW_THREADS
6348
6349 if (io_result==0)
6350 {
6351 return win32_error_unicode("readlink", path);
6352 }
6353
6354 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6355 {
6356 PyErr_SetString(PyExc_ValueError,
6357 "not a symbolic link");
6358 return NULL;
6359 }
Brian Curtin74e45612010-07-09 15:58:59 +00006360 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6361 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6362
6363 result = PyUnicode_FromWideChar(print_name,
6364 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006365 return result;
6366}
6367
6368#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6369
Brian Curtin52173d42010-12-02 18:29:18 +00006370#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006371
6372/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6373static int has_CreateSymbolicLinkW = 0;
6374static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6375static int
6376check_CreateSymbolicLinkW()
6377{
6378 HINSTANCE hKernel32;
6379 /* only recheck */
6380 if (has_CreateSymbolicLinkW)
6381 return has_CreateSymbolicLinkW;
6382 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006383 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6384 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006385 if (Py_CreateSymbolicLinkW)
6386 has_CreateSymbolicLinkW = 1;
6387 return has_CreateSymbolicLinkW;
6388}
6389
6390PyDoc_STRVAR(win_symlink__doc__,
6391"symlink(src, dst, target_is_directory=False)\n\n\
6392Create a symbolic link pointing to src named dst.\n\
6393target_is_directory is required if the target is to be interpreted as\n\
6394a directory.\n\
6395This function requires Windows 6.0 or greater, and raises a\n\
6396NotImplementedError otherwise.");
6397
6398static PyObject *
6399win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6400{
6401 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6402 PyObject *src, *dest;
6403 int target_is_directory = 0;
6404 DWORD res;
6405 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006406
Brian Curtind40e6f72010-07-08 21:39:08 +00006407 if (!check_CreateSymbolicLinkW())
6408 {
6409 /* raise NotImplementedError */
6410 return PyErr_Format(PyExc_NotImplementedError,
6411 "CreateSymbolicLinkW not found");
6412 }
6413 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6414 kwlist, &src, &dest, &target_is_directory))
6415 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006416
6417 if (win32_can_symlink == 0)
6418 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6419
Brian Curtind40e6f72010-07-08 21:39:08 +00006420 if (!convert_to_unicode(&src)) { return NULL; }
6421 if (!convert_to_unicode(&dest)) {
6422 Py_DECREF(src);
6423 return NULL;
6424 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006425
Brian Curtind40e6f72010-07-08 21:39:08 +00006426 /* if src is a directory, ensure target_is_directory==1 */
6427 if(
6428 GetFileAttributesExW(
6429 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6430 ))
6431 {
6432 target_is_directory = target_is_directory ||
6433 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6434 }
6435
6436 Py_BEGIN_ALLOW_THREADS
6437 res = Py_CreateSymbolicLinkW(
6438 PyUnicode_AsUnicode(dest),
6439 PyUnicode_AsUnicode(src),
6440 target_is_directory);
6441 Py_END_ALLOW_THREADS
6442 Py_DECREF(src);
6443 Py_DECREF(dest);
6444 if (!res)
6445 {
6446 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6447 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006448
Brian Curtind40e6f72010-07-08 21:39:08 +00006449 Py_INCREF(Py_None);
6450 return Py_None;
6451}
Brian Curtin52173d42010-12-02 18:29:18 +00006452#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006453
6454#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006455#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6456static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006457system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006458{
6459 ULONG value = 0;
6460
6461 Py_BEGIN_ALLOW_THREADS
6462 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6463 Py_END_ALLOW_THREADS
6464
6465 return value;
6466}
6467
6468static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006469posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006470{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006471 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 return Py_BuildValue("ddddd",
6473 (double)0 /* t.tms_utime / HZ */,
6474 (double)0 /* t.tms_stime / HZ */,
6475 (double)0 /* t.tms_cutime / HZ */,
6476 (double)0 /* t.tms_cstime / HZ */,
6477 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006478}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006479#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006480#define NEED_TICKS_PER_SECOND
6481static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006482static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006483posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006484{
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 struct tms t;
6486 clock_t c;
6487 errno = 0;
6488 c = times(&t);
6489 if (c == (clock_t) -1)
6490 return posix_error();
6491 return Py_BuildValue("ddddd",
6492 (double)t.tms_utime / ticks_per_second,
6493 (double)t.tms_stime / ticks_per_second,
6494 (double)t.tms_cutime / ticks_per_second,
6495 (double)t.tms_cstime / ticks_per_second,
6496 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006497}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006498#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006499#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006500
6501
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006502#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006503#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006505posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006506{
Victor Stinner8c62be82010-05-06 00:08:46 +00006507 FILETIME create, exit, kernel, user;
6508 HANDLE hProc;
6509 hProc = GetCurrentProcess();
6510 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6511 /* The fields of a FILETIME structure are the hi and lo part
6512 of a 64-bit value expressed in 100 nanosecond units.
6513 1e7 is one second in such units; 1e-7 the inverse.
6514 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6515 */
6516 return Py_BuildValue(
6517 "ddddd",
6518 (double)(user.dwHighDateTime*429.4967296 +
6519 user.dwLowDateTime*1e-7),
6520 (double)(kernel.dwHighDateTime*429.4967296 +
6521 kernel.dwLowDateTime*1e-7),
6522 (double)0,
6523 (double)0,
6524 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006525}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006526#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006527
6528#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006529PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006530"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006531Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006532#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006533
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006534
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006535#ifdef HAVE_GETSID
6536PyDoc_STRVAR(posix_getsid__doc__,
6537"getsid(pid) -> sid\n\n\
6538Call the system call getsid().");
6539
6540static PyObject *
6541posix_getsid(PyObject *self, PyObject *args)
6542{
Victor Stinner8c62be82010-05-06 00:08:46 +00006543 pid_t pid;
6544 int sid;
6545 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6546 return NULL;
6547 sid = getsid(pid);
6548 if (sid < 0)
6549 return posix_error();
6550 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006551}
6552#endif /* HAVE_GETSID */
6553
6554
Guido van Rossumb6775db1994-08-01 11:34:53 +00006555#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006556PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006557"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006558Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006559
Barry Warsaw53699e91996-12-10 23:23:01 +00006560static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006561posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006562{
Victor Stinner8c62be82010-05-06 00:08:46 +00006563 if (setsid() < 0)
6564 return posix_error();
6565 Py_INCREF(Py_None);
6566 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006567}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006568#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006569
Guido van Rossumb6775db1994-08-01 11:34:53 +00006570#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006571PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006572"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006573Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006574
Barry Warsaw53699e91996-12-10 23:23:01 +00006575static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006576posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006577{
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 pid_t pid;
6579 int pgrp;
6580 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6581 return NULL;
6582 if (setpgid(pid, pgrp) < 0)
6583 return posix_error();
6584 Py_INCREF(Py_None);
6585 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006586}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006587#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006588
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006589
Guido van Rossumb6775db1994-08-01 11:34:53 +00006590#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006591PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006592"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006593Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006594
Barry Warsaw53699e91996-12-10 23:23:01 +00006595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006596posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006597{
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 int fd;
6599 pid_t pgid;
6600 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6601 return NULL;
6602 pgid = tcgetpgrp(fd);
6603 if (pgid < 0)
6604 return posix_error();
6605 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006606}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006607#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006608
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006609
Guido van Rossumb6775db1994-08-01 11:34:53 +00006610#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006611PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006612"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006613Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006614
Barry Warsaw53699e91996-12-10 23:23:01 +00006615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006616posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006617{
Victor Stinner8c62be82010-05-06 00:08:46 +00006618 int fd;
6619 pid_t pgid;
6620 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6621 return NULL;
6622 if (tcsetpgrp(fd, pgid) < 0)
6623 return posix_error();
6624 Py_INCREF(Py_None);
6625 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006626}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006627#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006628
Guido van Rossum687dd131993-05-17 08:34:16 +00006629/* Functions acting on file descriptors */
6630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006631PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006632"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006633Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006634
Barry Warsaw53699e91996-12-10 23:23:01 +00006635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006636posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006637{
Victor Stinner8c62be82010-05-06 00:08:46 +00006638 PyObject *ofile;
6639 char *file;
6640 int flag;
6641 int mode = 0777;
6642 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006643
6644#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006646 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006647 Py_BEGIN_ALLOW_THREADS
6648 /* PyUnicode_AS_UNICODE OK without thread
6649 lock as it is a simple dereference. */
6650 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6651 Py_END_ALLOW_THREADS
6652 if (fd < 0)
6653 return posix_error();
6654 return PyLong_FromLong((long)fd);
6655 }
6656 /* Drop the argument parsing error as narrow strings
6657 are also valid. */
6658 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006659#endif
6660
Victor Stinner26de69d2011-06-17 15:15:38 +02006661 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006662 PyUnicode_FSConverter, &ofile,
6663 &flag, &mode))
6664 return NULL;
6665 file = PyBytes_AsString(ofile);
6666 Py_BEGIN_ALLOW_THREADS
6667 fd = open(file, flag, mode);
6668 Py_END_ALLOW_THREADS
6669 if (fd < 0)
6670 return posix_error_with_allocated_filename(ofile);
6671 Py_DECREF(ofile);
6672 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006673}
6674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006676PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006677"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006678Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006679
Barry Warsaw53699e91996-12-10 23:23:01 +00006680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006681posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006682{
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 int fd, res;
6684 if (!PyArg_ParseTuple(args, "i:close", &fd))
6685 return NULL;
6686 if (!_PyVerify_fd(fd))
6687 return posix_error();
6688 Py_BEGIN_ALLOW_THREADS
6689 res = close(fd);
6690 Py_END_ALLOW_THREADS
6691 if (res < 0)
6692 return posix_error();
6693 Py_INCREF(Py_None);
6694 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006695}
6696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006697
Victor Stinner8c62be82010-05-06 00:08:46 +00006698PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006699"closerange(fd_low, fd_high)\n\n\
6700Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6701
6702static PyObject *
6703posix_closerange(PyObject *self, PyObject *args)
6704{
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 int fd_from, fd_to, i;
6706 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6707 return NULL;
6708 Py_BEGIN_ALLOW_THREADS
6709 for (i = fd_from; i < fd_to; i++)
6710 if (_PyVerify_fd(i))
6711 close(i);
6712 Py_END_ALLOW_THREADS
6713 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006714}
6715
6716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006717PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006718"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006719Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006720
Barry Warsaw53699e91996-12-10 23:23:01 +00006721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006722posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006723{
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 int fd;
6725 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6726 return NULL;
6727 if (!_PyVerify_fd(fd))
6728 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 if (fd < 0)
6731 return posix_error();
6732 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006733}
6734
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006735
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006736PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006737"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006738Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006739
Barry Warsaw53699e91996-12-10 23:23:01 +00006740static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006741posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006742{
Victor Stinner8c62be82010-05-06 00:08:46 +00006743 int fd, fd2, res;
6744 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6745 return NULL;
6746 if (!_PyVerify_fd_dup2(fd, fd2))
6747 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006749 if (res < 0)
6750 return posix_error();
6751 Py_INCREF(Py_None);
6752 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006753}
6754
Ross Lagerwall7807c352011-03-17 20:20:30 +02006755#ifdef HAVE_LOCKF
6756PyDoc_STRVAR(posix_lockf__doc__,
6757"lockf(fd, cmd, len)\n\n\
6758Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6759fd is an open file descriptor.\n\
6760cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6761F_TEST.\n\
6762len specifies the section of the file to lock.");
6763
6764static PyObject *
6765posix_lockf(PyObject *self, PyObject *args)
6766{
6767 int fd, cmd, res;
6768 off_t len;
6769 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6770 &fd, &cmd, _parse_off_t, &len))
6771 return NULL;
6772
6773 Py_BEGIN_ALLOW_THREADS
6774 res = lockf(fd, cmd, len);
6775 Py_END_ALLOW_THREADS
6776
6777 if (res < 0)
6778 return posix_error();
6779
6780 Py_RETURN_NONE;
6781}
6782#endif
6783
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006785PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006786"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006787Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006788
Barry Warsaw53699e91996-12-10 23:23:01 +00006789static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006790posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006791{
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006793#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006795#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006797#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006798 PyObject *posobj;
6799 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006801#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6803 switch (how) {
6804 case 0: how = SEEK_SET; break;
6805 case 1: how = SEEK_CUR; break;
6806 case 2: how = SEEK_END; break;
6807 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006808#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006809
Ross Lagerwall8e749672011-03-17 21:54:07 +02006810#if !defined(HAVE_LARGEFILE_SUPPORT)
6811 pos = PyLong_AsLong(posobj);
6812#else
6813 pos = PyLong_AsLongLong(posobj);
6814#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006815 if (PyErr_Occurred())
6816 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006817
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 if (!_PyVerify_fd(fd))
6819 return posix_error();
6820 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006821#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006823#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006825#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 Py_END_ALLOW_THREADS
6827 if (res < 0)
6828 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006829
6830#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006832#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006833 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006834#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006835}
6836
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006838PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006839"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006840Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006841
Barry Warsaw53699e91996-12-10 23:23:01 +00006842static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006843posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006844{
Victor Stinner8c62be82010-05-06 00:08:46 +00006845 int fd, size;
6846 Py_ssize_t n;
6847 PyObject *buffer;
6848 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6849 return NULL;
6850 if (size < 0) {
6851 errno = EINVAL;
6852 return posix_error();
6853 }
6854 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6855 if (buffer == NULL)
6856 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006857 if (!_PyVerify_fd(fd)) {
6858 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006860 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 Py_BEGIN_ALLOW_THREADS
6862 n = read(fd, PyBytes_AS_STRING(buffer), size);
6863 Py_END_ALLOW_THREADS
6864 if (n < 0) {
6865 Py_DECREF(buffer);
6866 return posix_error();
6867 }
6868 if (n != size)
6869 _PyBytes_Resize(&buffer, n);
6870 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006871}
6872
Ross Lagerwall7807c352011-03-17 20:20:30 +02006873#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6874 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006875static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006876iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6877{
6878 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006879 Py_ssize_t blen, total = 0;
6880
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006881 *iov = PyMem_New(struct iovec, cnt);
6882 if (*iov == NULL) {
6883 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006884 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006885 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006886
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006887 *buf = PyMem_New(Py_buffer, cnt);
6888 if (*buf == NULL) {
6889 PyMem_Del(*iov);
6890 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006891 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006892 }
6893
6894 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006895 PyObject *item = PySequence_GetItem(seq, i);
6896 if (item == NULL)
6897 goto fail;
6898 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6899 Py_DECREF(item);
6900 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006901 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006902 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006903 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006904 blen = (*buf)[i].len;
6905 (*iov)[i].iov_len = blen;
6906 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006907 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006908 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006909
6910fail:
6911 PyMem_Del(*iov);
6912 for (j = 0; j < i; j++) {
6913 PyBuffer_Release(&(*buf)[j]);
6914 }
6915 PyMem_Del(*buf);
6916 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006917}
6918
6919static void
6920iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6921{
6922 int i;
6923 PyMem_Del(iov);
6924 for (i = 0; i < cnt; i++) {
6925 PyBuffer_Release(&buf[i]);
6926 }
6927 PyMem_Del(buf);
6928}
6929#endif
6930
Ross Lagerwall7807c352011-03-17 20:20:30 +02006931#ifdef HAVE_READV
6932PyDoc_STRVAR(posix_readv__doc__,
6933"readv(fd, buffers) -> bytesread\n\n\
6934Read from a file descriptor into a number of writable buffers. buffers\n\
6935is an arbitrary sequence of writable buffers.\n\
6936Returns the total number of bytes read.");
6937
6938static PyObject *
6939posix_readv(PyObject *self, PyObject *args)
6940{
6941 int fd, cnt;
6942 Py_ssize_t n;
6943 PyObject *seq;
6944 struct iovec *iov;
6945 Py_buffer *buf;
6946
6947 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6948 return NULL;
6949 if (!PySequence_Check(seq)) {
6950 PyErr_SetString(PyExc_TypeError,
6951 "readv() arg 2 must be a sequence");
6952 return NULL;
6953 }
6954 cnt = PySequence_Size(seq);
6955
6956 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6957 return NULL;
6958
6959 Py_BEGIN_ALLOW_THREADS
6960 n = readv(fd, iov, cnt);
6961 Py_END_ALLOW_THREADS
6962
6963 iov_cleanup(iov, buf, cnt);
6964 return PyLong_FromSsize_t(n);
6965}
6966#endif
6967
6968#ifdef HAVE_PREAD
6969PyDoc_STRVAR(posix_pread__doc__,
6970"pread(fd, buffersize, offset) -> string\n\n\
6971Read from a file descriptor, fd, at a position of offset. It will read up\n\
6972to buffersize number of bytes. The file offset remains unchanged.");
6973
6974static PyObject *
6975posix_pread(PyObject *self, PyObject *args)
6976{
6977 int fd, size;
6978 off_t offset;
6979 Py_ssize_t n;
6980 PyObject *buffer;
6981 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6982 return NULL;
6983
6984 if (size < 0) {
6985 errno = EINVAL;
6986 return posix_error();
6987 }
6988 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6989 if (buffer == NULL)
6990 return NULL;
6991 if (!_PyVerify_fd(fd)) {
6992 Py_DECREF(buffer);
6993 return posix_error();
6994 }
6995 Py_BEGIN_ALLOW_THREADS
6996 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6997 Py_END_ALLOW_THREADS
6998 if (n < 0) {
6999 Py_DECREF(buffer);
7000 return posix_error();
7001 }
7002 if (n != size)
7003 _PyBytes_Resize(&buffer, n);
7004 return buffer;
7005}
7006#endif
7007
7008PyDoc_STRVAR(posix_write__doc__,
7009"write(fd, string) -> byteswritten\n\n\
7010Write a string to a file descriptor.");
7011
7012static PyObject *
7013posix_write(PyObject *self, PyObject *args)
7014{
7015 Py_buffer pbuf;
7016 int fd;
7017 Py_ssize_t size, len;
7018
7019 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7020 return NULL;
7021 if (!_PyVerify_fd(fd)) {
7022 PyBuffer_Release(&pbuf);
7023 return posix_error();
7024 }
7025 len = pbuf.len;
7026 Py_BEGIN_ALLOW_THREADS
7027#if defined(MS_WIN64) || defined(MS_WINDOWS)
7028 if (len > INT_MAX)
7029 len = INT_MAX;
7030 size = write(fd, pbuf.buf, (int)len);
7031#else
7032 size = write(fd, pbuf.buf, len);
7033#endif
7034 Py_END_ALLOW_THREADS
7035 PyBuffer_Release(&pbuf);
7036 if (size < 0)
7037 return posix_error();
7038 return PyLong_FromSsize_t(size);
7039}
7040
7041#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007042PyDoc_STRVAR(posix_sendfile__doc__,
7043"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7044sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7045 -> byteswritten\n\
7046Copy nbytes bytes from file descriptor in to file descriptor out.");
7047
7048static PyObject *
7049posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7050{
7051 int in, out;
7052 Py_ssize_t ret;
7053 off_t offset;
7054
7055#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7056#ifndef __APPLE__
7057 Py_ssize_t len;
7058#endif
7059 PyObject *headers = NULL, *trailers = NULL;
7060 Py_buffer *hbuf, *tbuf;
7061 off_t sbytes;
7062 struct sf_hdtr sf;
7063 int flags = 0;
7064 sf.headers = NULL;
7065 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007066 static char *keywords[] = {"out", "in",
7067 "offset", "count",
7068 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007069
7070#ifdef __APPLE__
7071 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007072 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007073#else
7074 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007075 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007076#endif
7077 &headers, &trailers, &flags))
7078 return NULL;
7079 if (headers != NULL) {
7080 if (!PySequence_Check(headers)) {
7081 PyErr_SetString(PyExc_TypeError,
7082 "sendfile() headers must be a sequence or None");
7083 return NULL;
7084 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007085 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007086 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007087 if (sf.hdr_cnt > 0 &&
7088 !(i = iov_setup(&(sf.headers), &hbuf,
7089 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007090 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007091#ifdef __APPLE__
7092 sbytes += i;
7093#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007094 }
7095 }
7096 if (trailers != NULL) {
7097 if (!PySequence_Check(trailers)) {
7098 PyErr_SetString(PyExc_TypeError,
7099 "sendfile() trailers must be a sequence or None");
7100 return NULL;
7101 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007102 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007103 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007104 if (sf.trl_cnt > 0 &&
7105 !(i = iov_setup(&(sf.trailers), &tbuf,
7106 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007107 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007108#ifdef __APPLE__
7109 sbytes += i;
7110#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007111 }
7112 }
7113
7114 Py_BEGIN_ALLOW_THREADS
7115#ifdef __APPLE__
7116 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7117#else
7118 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7119#endif
7120 Py_END_ALLOW_THREADS
7121
7122 if (sf.headers != NULL)
7123 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7124 if (sf.trailers != NULL)
7125 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7126
7127 if (ret < 0) {
7128 if ((errno == EAGAIN) || (errno == EBUSY)) {
7129 if (sbytes != 0) {
7130 // some data has been sent
7131 goto done;
7132 }
7133 else {
7134 // no data has been sent; upper application is supposed
7135 // to retry on EAGAIN or EBUSY
7136 return posix_error();
7137 }
7138 }
7139 return posix_error();
7140 }
7141 goto done;
7142
7143done:
7144 #if !defined(HAVE_LARGEFILE_SUPPORT)
7145 return Py_BuildValue("l", sbytes);
7146 #else
7147 return Py_BuildValue("L", sbytes);
7148 #endif
7149
7150#else
7151 Py_ssize_t count;
7152 PyObject *offobj;
7153 static char *keywords[] = {"out", "in",
7154 "offset", "count", NULL};
7155 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7156 keywords, &out, &in, &offobj, &count))
7157 return NULL;
7158#ifdef linux
7159 if (offobj == Py_None) {
7160 Py_BEGIN_ALLOW_THREADS
7161 ret = sendfile(out, in, NULL, count);
7162 Py_END_ALLOW_THREADS
7163 if (ret < 0)
7164 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007165 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007166 }
7167#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007168 if (!_parse_off_t(offobj, &offset))
7169 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007170 Py_BEGIN_ALLOW_THREADS
7171 ret = sendfile(out, in, &offset, count);
7172 Py_END_ALLOW_THREADS
7173 if (ret < 0)
7174 return posix_error();
7175 return Py_BuildValue("n", ret);
7176#endif
7177}
7178#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007179
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007180PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007181"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007182Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007183
Barry Warsaw53699e91996-12-10 23:23:01 +00007184static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007185posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007186{
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 int fd;
7188 STRUCT_STAT st;
7189 int res;
7190 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7191 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007192#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 /* on OpenVMS we must ensure that all bytes are written to the file */
7194 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007195#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 if (!_PyVerify_fd(fd))
7197 return posix_error();
7198 Py_BEGIN_ALLOW_THREADS
7199 res = FSTAT(fd, &st);
7200 Py_END_ALLOW_THREADS
7201 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007202#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007204#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007206#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 }
Tim Peters5aa91602002-01-30 05:46:57 +00007208
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007210}
7211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007212PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007213"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007214Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007215connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007216
7217static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007218posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007219{
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 int fd;
7221 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7222 return NULL;
7223 if (!_PyVerify_fd(fd))
7224 return PyBool_FromLong(0);
7225 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007226}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007227
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007228#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007229PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007230"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007231Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007232
Barry Warsaw53699e91996-12-10 23:23:01 +00007233static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007234posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007235{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007236#if defined(PYOS_OS2)
7237 HFILE read, write;
7238 APIRET rc;
7239
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007240 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007241 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007242 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007243
7244 return Py_BuildValue("(ii)", read, write);
7245#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007246#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 int fds[2];
7248 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007250 if (res != 0)
7251 return posix_error();
7252 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007253#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 HANDLE read, write;
7255 int read_fd, write_fd;
7256 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 if (!ok)
7259 return win32_error("CreatePipe", NULL);
7260 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7261 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7262 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007263#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007264#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007265}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007266#endif /* HAVE_PIPE */
7267
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007268#ifdef HAVE_PIPE2
7269PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007270"pipe2(flags) -> (read_end, write_end)\n\n\
7271Create a pipe with flags set atomically.\n\
7272flags can be constructed by ORing together one or more of these values:\n\
7273O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007274");
7275
7276static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007277posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007278{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007279 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007280 int fds[2];
7281 int res;
7282
Charles-François Natali368f34b2011-06-06 19:49:47 +02007283 flags = PyLong_AsLong(arg);
7284 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007285 return NULL;
7286
7287 res = pipe2(fds, flags);
7288 if (res != 0)
7289 return posix_error();
7290 return Py_BuildValue("(ii)", fds[0], fds[1]);
7291}
7292#endif /* HAVE_PIPE2 */
7293
Ross Lagerwall7807c352011-03-17 20:20:30 +02007294#ifdef HAVE_WRITEV
7295PyDoc_STRVAR(posix_writev__doc__,
7296"writev(fd, buffers) -> byteswritten\n\n\
7297Write the contents of buffers to a file descriptor, where buffers is an\n\
7298arbitrary sequence of buffers.\n\
7299Returns the total bytes written.");
7300
7301static PyObject *
7302posix_writev(PyObject *self, PyObject *args)
7303{
7304 int fd, cnt;
7305 Py_ssize_t res;
7306 PyObject *seq;
7307 struct iovec *iov;
7308 Py_buffer *buf;
7309 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7310 return NULL;
7311 if (!PySequence_Check(seq)) {
7312 PyErr_SetString(PyExc_TypeError,
7313 "writev() arg 2 must be a sequence");
7314 return NULL;
7315 }
7316 cnt = PySequence_Size(seq);
7317
7318 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7319 return NULL;
7320 }
7321
7322 Py_BEGIN_ALLOW_THREADS
7323 res = writev(fd, iov, cnt);
7324 Py_END_ALLOW_THREADS
7325
7326 iov_cleanup(iov, buf, cnt);
7327 return PyLong_FromSsize_t(res);
7328}
7329#endif
7330
7331#ifdef HAVE_PWRITE
7332PyDoc_STRVAR(posix_pwrite__doc__,
7333"pwrite(fd, string, offset) -> byteswritten\n\n\
7334Write string to a file descriptor, fd, from offset, leaving the file\n\
7335offset unchanged.");
7336
7337static PyObject *
7338posix_pwrite(PyObject *self, PyObject *args)
7339{
7340 Py_buffer pbuf;
7341 int fd;
7342 off_t offset;
7343 Py_ssize_t size;
7344
7345 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7346 return NULL;
7347
7348 if (!_PyVerify_fd(fd)) {
7349 PyBuffer_Release(&pbuf);
7350 return posix_error();
7351 }
7352 Py_BEGIN_ALLOW_THREADS
7353 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7354 Py_END_ALLOW_THREADS
7355 PyBuffer_Release(&pbuf);
7356 if (size < 0)
7357 return posix_error();
7358 return PyLong_FromSsize_t(size);
7359}
7360#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007361
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007362#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007363PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007364"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007365Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007366
Barry Warsaw53699e91996-12-10 23:23:01 +00007367static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007368posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007369{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007370 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 char *filename;
7372 int mode = 0666;
7373 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007374 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7375 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007377 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 Py_BEGIN_ALLOW_THREADS
7379 res = mkfifo(filename, mode);
7380 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007381 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 if (res < 0)
7383 return posix_error();
7384 Py_INCREF(Py_None);
7385 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007386}
7387#endif
7388
7389
Neal Norwitz11690112002-07-30 01:08:28 +00007390#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007391PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007392"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007393Create a filesystem node (file, device special file or named pipe)\n\
7394named filename. mode specifies both the permissions to use and the\n\
7395type of node to be created, being combined (bitwise OR) with one of\n\
7396S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007397device defines the newly created device special file (probably using\n\
7398os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007399
7400
7401static PyObject *
7402posix_mknod(PyObject *self, PyObject *args)
7403{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007404 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 char *filename;
7406 int mode = 0600;
7407 int device = 0;
7408 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007409 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7410 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007412 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 Py_BEGIN_ALLOW_THREADS
7414 res = mknod(filename, mode, device);
7415 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007416 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007417 if (res < 0)
7418 return posix_error();
7419 Py_INCREF(Py_None);
7420 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007421}
7422#endif
7423
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007424#ifdef HAVE_DEVICE_MACROS
7425PyDoc_STRVAR(posix_major__doc__,
7426"major(device) -> major number\n\
7427Extracts a device major number from a raw device number.");
7428
7429static PyObject *
7430posix_major(PyObject *self, PyObject *args)
7431{
Victor Stinner8c62be82010-05-06 00:08:46 +00007432 int device;
7433 if (!PyArg_ParseTuple(args, "i:major", &device))
7434 return NULL;
7435 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007436}
7437
7438PyDoc_STRVAR(posix_minor__doc__,
7439"minor(device) -> minor number\n\
7440Extracts a device minor number from a raw device number.");
7441
7442static PyObject *
7443posix_minor(PyObject *self, PyObject *args)
7444{
Victor Stinner8c62be82010-05-06 00:08:46 +00007445 int device;
7446 if (!PyArg_ParseTuple(args, "i:minor", &device))
7447 return NULL;
7448 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007449}
7450
7451PyDoc_STRVAR(posix_makedev__doc__,
7452"makedev(major, minor) -> device number\n\
7453Composes a raw device number from the major and minor device numbers.");
7454
7455static PyObject *
7456posix_makedev(PyObject *self, PyObject *args)
7457{
Victor Stinner8c62be82010-05-06 00:08:46 +00007458 int major, minor;
7459 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7460 return NULL;
7461 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007462}
7463#endif /* device macros */
7464
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007465
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007466#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007467PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007468"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007469Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007470
Barry Warsaw53699e91996-12-10 23:23:01 +00007471static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007472posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007473{
Victor Stinner8c62be82010-05-06 00:08:46 +00007474 int fd;
7475 off_t length;
7476 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007477
Ross Lagerwall7807c352011-03-17 20:20:30 +02007478 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007480
Victor Stinner8c62be82010-05-06 00:08:46 +00007481 Py_BEGIN_ALLOW_THREADS
7482 res = ftruncate(fd, length);
7483 Py_END_ALLOW_THREADS
7484 if (res < 0)
7485 return posix_error();
7486 Py_INCREF(Py_None);
7487 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007488}
7489#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007490
Ross Lagerwall7807c352011-03-17 20:20:30 +02007491#ifdef HAVE_TRUNCATE
7492PyDoc_STRVAR(posix_truncate__doc__,
7493"truncate(path, length)\n\n\
7494Truncate the file given by path to length bytes.");
7495
7496static PyObject *
7497posix_truncate(PyObject *self, PyObject *args)
7498{
7499 PyObject *opath;
7500 const char *path;
7501 off_t length;
7502 int res;
7503
7504 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7505 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7506 return NULL;
7507 path = PyBytes_AsString(opath);
7508
7509 Py_BEGIN_ALLOW_THREADS
7510 res = truncate(path, length);
7511 Py_END_ALLOW_THREADS
7512 Py_DECREF(opath);
7513 if (res < 0)
7514 return posix_error();
7515 Py_RETURN_NONE;
7516}
7517#endif
7518
7519#ifdef HAVE_POSIX_FALLOCATE
7520PyDoc_STRVAR(posix_posix_fallocate__doc__,
7521"posix_fallocate(fd, offset, len)\n\n\
7522Ensures that enough disk space is allocated for the file specified by fd\n\
7523starting from offset and continuing for len bytes.");
7524
7525static PyObject *
7526posix_posix_fallocate(PyObject *self, PyObject *args)
7527{
7528 off_t len, offset;
7529 int res, fd;
7530
7531 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7532 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7533 return NULL;
7534
7535 Py_BEGIN_ALLOW_THREADS
7536 res = posix_fallocate(fd, offset, len);
7537 Py_END_ALLOW_THREADS
7538 if (res != 0) {
7539 errno = res;
7540 return posix_error();
7541 }
7542 Py_RETURN_NONE;
7543}
7544#endif
7545
7546#ifdef HAVE_POSIX_FADVISE
7547PyDoc_STRVAR(posix_posix_fadvise__doc__,
7548"posix_fadvise(fd, offset, len, advice)\n\n\
7549Announces an intention to access data in a specific pattern thus allowing\n\
7550the kernel to make optimizations.\n\
7551The advice applies to the region of the file specified by fd starting at\n\
7552offset and continuing for len bytes.\n\
7553advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7554POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7555POSIX_FADV_DONTNEED.");
7556
7557static PyObject *
7558posix_posix_fadvise(PyObject *self, PyObject *args)
7559{
7560 off_t len, offset;
7561 int res, fd, advice;
7562
7563 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7564 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7565 return NULL;
7566
7567 Py_BEGIN_ALLOW_THREADS
7568 res = posix_fadvise(fd, offset, len, advice);
7569 Py_END_ALLOW_THREADS
7570 if (res != 0) {
7571 errno = res;
7572 return posix_error();
7573 }
7574 Py_RETURN_NONE;
7575}
7576#endif
7577
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007578#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007579PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007580"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007581Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007582
Fred Drake762e2061999-08-26 17:23:54 +00007583/* Save putenv() parameters as values here, so we can collect them when they
7584 * get re-set with another call for the same key. */
7585static PyObject *posix_putenv_garbage;
7586
Tim Peters5aa91602002-01-30 05:46:57 +00007587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007588posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007589{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007590#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007591 wchar_t *s1, *s2;
7592 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007593#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007594 PyObject *os1, *os2;
7595 char *s1, *s2;
7596 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007597#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007598 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007600
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007601#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 if (!PyArg_ParseTuple(args,
7603 "uu:putenv",
7604 &s1, &s2))
7605 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007606#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 if (!PyArg_ParseTuple(args,
7608 "O&O&:putenv",
7609 PyUnicode_FSConverter, &os1,
7610 PyUnicode_FSConverter, &os2))
7611 return NULL;
7612 s1 = PyBytes_AsString(os1);
7613 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007614#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007615
7616#if defined(PYOS_OS2)
7617 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7618 APIRET rc;
7619
Guido van Rossumd48f2521997-12-05 22:19:34 +00007620 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007621 if (rc != NO_ERROR) {
7622 os2_error(rc);
7623 goto error;
7624 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007625
7626 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7627 APIRET rc;
7628
Guido van Rossumd48f2521997-12-05 22:19:34 +00007629 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007630 if (rc != NO_ERROR) {
7631 os2_error(rc);
7632 goto error;
7633 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007634 } else {
7635#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 /* XXX This can leak memory -- not easy to fix :-( */
7637 /* len includes space for a trailing \0; the size arg to
7638 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007639#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 len = wcslen(s1) + wcslen(s2) + 2;
7641 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007642#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007643 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007645#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007646 if (newstr == NULL) {
7647 PyErr_NoMemory();
7648 goto error;
7649 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007650#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 newenv = PyUnicode_AsUnicode(newstr);
7652 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7653 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007654 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007655 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007656 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007657#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007658 newenv = PyBytes_AS_STRING(newstr);
7659 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7660 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007662 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007664#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007665
Victor Stinner8c62be82010-05-06 00:08:46 +00007666 /* Install the first arg and newstr in posix_putenv_garbage;
7667 * this will cause previous value to be collected. This has to
7668 * happen after the real putenv() call because the old value
7669 * was still accessible until then. */
7670 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007671#ifdef MS_WINDOWS
7672 PyTuple_GET_ITEM(args, 0),
7673#else
7674 os1,
7675#endif
7676 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007677 /* really not much we can do; just leak */
7678 PyErr_Clear();
7679 }
7680 else {
7681 Py_DECREF(newstr);
7682 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007683
7684#if defined(PYOS_OS2)
7685 }
7686#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007687
Martin v. Löwis011e8422009-05-05 04:43:17 +00007688#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 Py_DECREF(os1);
7690 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007691#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007692 Py_RETURN_NONE;
7693
7694error:
7695#ifndef MS_WINDOWS
7696 Py_DECREF(os1);
7697 Py_DECREF(os2);
7698#endif
7699 Py_XDECREF(newstr);
7700 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007701}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007702#endif /* putenv */
7703
Guido van Rossumc524d952001-10-19 01:31:59 +00007704#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007705PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007706"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007707Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007708
7709static PyObject *
7710posix_unsetenv(PyObject *self, PyObject *args)
7711{
Victor Stinner84ae1182010-05-06 22:05:07 +00007712#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007714
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7716 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007717#else
7718 PyObject *os1;
7719 char *s1;
7720
7721 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7722 PyUnicode_FSConverter, &os1))
7723 return NULL;
7724 s1 = PyBytes_AsString(os1);
7725#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007726
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007728
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 /* Remove the key from posix_putenv_garbage;
7730 * this will cause it to be collected. This has to
7731 * happen after the real unsetenv() call because the
7732 * old value was still accessible until then.
7733 */
7734 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007735#ifdef MS_WINDOWS
7736 PyTuple_GET_ITEM(args, 0)
7737#else
7738 os1
7739#endif
7740 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007741 /* really not much we can do; just leak */
7742 PyErr_Clear();
7743 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007744
Victor Stinner84ae1182010-05-06 22:05:07 +00007745#ifndef MS_WINDOWS
7746 Py_DECREF(os1);
7747#endif
7748 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007749}
7750#endif /* unsetenv */
7751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007752PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007753"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007754Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007755
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007756static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007757posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007758{
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 int code;
7760 char *message;
7761 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7762 return NULL;
7763 message = strerror(code);
7764 if (message == NULL) {
7765 PyErr_SetString(PyExc_ValueError,
7766 "strerror() argument out of range");
7767 return NULL;
7768 }
7769 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007770}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007771
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007772
Guido van Rossumc9641791998-08-04 15:26:23 +00007773#ifdef HAVE_SYS_WAIT_H
7774
Fred Drake106c1a02002-04-23 15:58:02 +00007775#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007776PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007777"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007778Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007779
7780static PyObject *
7781posix_WCOREDUMP(PyObject *self, PyObject *args)
7782{
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 WAIT_TYPE status;
7784 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007785
Victor Stinner8c62be82010-05-06 00:08:46 +00007786 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7787 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007788
Victor Stinner8c62be82010-05-06 00:08:46 +00007789 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007790}
7791#endif /* WCOREDUMP */
7792
7793#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007794PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007795"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007796Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007797job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007798
7799static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007800posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007801{
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 WAIT_TYPE status;
7803 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007804
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7806 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007807
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007809}
7810#endif /* WIFCONTINUED */
7811
Guido van Rossumc9641791998-08-04 15:26:23 +00007812#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007813PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007814"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007815Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007816
7817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007818posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007819{
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 WAIT_TYPE status;
7821 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007822
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7824 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007825
Victor Stinner8c62be82010-05-06 00:08:46 +00007826 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007827}
7828#endif /* WIFSTOPPED */
7829
7830#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007831PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007832"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007833Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007834
7835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007836posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007837{
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 WAIT_TYPE status;
7839 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007840
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7842 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007843
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007845}
7846#endif /* WIFSIGNALED */
7847
7848#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007849PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007850"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007851Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007852system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007853
7854static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007855posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007856{
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 WAIT_TYPE status;
7858 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007859
Victor Stinner8c62be82010-05-06 00:08:46 +00007860 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7861 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007862
Victor Stinner8c62be82010-05-06 00:08:46 +00007863 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007864}
7865#endif /* WIFEXITED */
7866
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007867#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007868PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007869"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007870Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007871
7872static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007873posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007874{
Victor Stinner8c62be82010-05-06 00:08:46 +00007875 WAIT_TYPE status;
7876 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007877
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7879 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007880
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007882}
7883#endif /* WEXITSTATUS */
7884
7885#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007886PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007887"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007888Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007889value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007890
7891static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007892posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007893{
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 WAIT_TYPE status;
7895 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007896
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7898 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007899
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007901}
7902#endif /* WTERMSIG */
7903
7904#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007905PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007906"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007907Return the signal that stopped the process that provided\n\
7908the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007909
7910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007911posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007912{
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 WAIT_TYPE status;
7914 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007915
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7917 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007918
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007920}
7921#endif /* WSTOPSIG */
7922
7923#endif /* HAVE_SYS_WAIT_H */
7924
7925
Thomas Wouters477c8d52006-05-27 19:21:47 +00007926#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007927#ifdef _SCO_DS
7928/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7929 needed definitions in sys/statvfs.h */
7930#define _SVID3
7931#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007932#include <sys/statvfs.h>
7933
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007934static PyObject*
7935_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7937 if (v == NULL)
7938 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007939
7940#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7942 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7943 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7944 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7945 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7946 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7947 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7948 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7949 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7950 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007951#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7953 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7954 PyStructSequence_SET_ITEM(v, 2,
7955 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7956 PyStructSequence_SET_ITEM(v, 3,
7957 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7958 PyStructSequence_SET_ITEM(v, 4,
7959 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7960 PyStructSequence_SET_ITEM(v, 5,
7961 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7962 PyStructSequence_SET_ITEM(v, 6,
7963 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7964 PyStructSequence_SET_ITEM(v, 7,
7965 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7966 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7967 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007968#endif
7969
Victor Stinner8c62be82010-05-06 00:08:46 +00007970 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007971}
7972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007973PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007974"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007975Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007976
7977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007978posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007979{
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 int fd, res;
7981 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007982
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7984 return NULL;
7985 Py_BEGIN_ALLOW_THREADS
7986 res = fstatvfs(fd, &st);
7987 Py_END_ALLOW_THREADS
7988 if (res != 0)
7989 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007990
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007992}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007993#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007994
7995
Thomas Wouters477c8d52006-05-27 19:21:47 +00007996#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007997#include <sys/statvfs.h>
7998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007999PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008000"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008001Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008002
8003static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008004posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008005{
Victor Stinner8c62be82010-05-06 00:08:46 +00008006 char *path;
8007 int res;
8008 struct statvfs st;
8009 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
8010 return NULL;
8011 Py_BEGIN_ALLOW_THREADS
8012 res = statvfs(path, &st);
8013 Py_END_ALLOW_THREADS
8014 if (res != 0)
8015 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008016
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008018}
8019#endif /* HAVE_STATVFS */
8020
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008021#ifdef MS_WINDOWS
8022PyDoc_STRVAR(win32__getdiskusage__doc__,
8023"_getdiskusage(path) -> (total, free)\n\n\
8024Return disk usage statistics about the given path as (total, free) tuple.");
8025
8026static PyObject *
8027win32__getdiskusage(PyObject *self, PyObject *args)
8028{
8029 BOOL retval;
8030 ULARGE_INTEGER _, total, free;
8031 LPCTSTR path;
8032
8033 if (! PyArg_ParseTuple(args, "s", &path))
8034 return NULL;
8035
8036 Py_BEGIN_ALLOW_THREADS
8037 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8038 Py_END_ALLOW_THREADS
8039 if (retval == 0)
8040 return PyErr_SetFromWindowsErr(0);
8041
8042 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8043}
8044#endif
8045
8046
Fred Drakec9680921999-12-13 16:37:25 +00008047/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8048 * It maps strings representing configuration variable names to
8049 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008050 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008051 * rarely-used constants. There are three separate tables that use
8052 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008053 *
8054 * This code is always included, even if none of the interfaces that
8055 * need it are included. The #if hackery needed to avoid it would be
8056 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008057 */
8058struct constdef {
8059 char *name;
8060 long value;
8061};
8062
Fred Drake12c6e2d1999-12-14 21:25:03 +00008063static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008064conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008065 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008066{
Christian Heimes217cfd12007-12-02 14:31:20 +00008067 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008068 *valuep = PyLong_AS_LONG(arg);
8069 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008070 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008071 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008072 /* look up the value in the table using a binary search */
8073 size_t lo = 0;
8074 size_t mid;
8075 size_t hi = tablesize;
8076 int cmp;
8077 const char *confname;
8078 if (!PyUnicode_Check(arg)) {
8079 PyErr_SetString(PyExc_TypeError,
8080 "configuration names must be strings or integers");
8081 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008082 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008083 confname = _PyUnicode_AsString(arg);
8084 if (confname == NULL)
8085 return 0;
8086 while (lo < hi) {
8087 mid = (lo + hi) / 2;
8088 cmp = strcmp(confname, table[mid].name);
8089 if (cmp < 0)
8090 hi = mid;
8091 else if (cmp > 0)
8092 lo = mid + 1;
8093 else {
8094 *valuep = table[mid].value;
8095 return 1;
8096 }
8097 }
8098 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8099 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008101}
8102
8103
8104#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8105static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008106#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008108#endif
8109#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008111#endif
Fred Drakec9680921999-12-13 16:37:25 +00008112#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008113 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008114#endif
8115#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008116 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008117#endif
8118#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008119 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008120#endif
8121#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008122 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008123#endif
8124#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008125 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008126#endif
8127#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008128 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008129#endif
8130#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008131 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008132#endif
8133#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008134 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008135#endif
8136#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008137 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008138#endif
8139#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008140 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008141#endif
8142#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008143 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008144#endif
8145#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008146 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008147#endif
8148#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008149 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008150#endif
8151#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008152 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008153#endif
8154#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008155 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008156#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008157#ifdef _PC_ACL_ENABLED
8158 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8159#endif
8160#ifdef _PC_MIN_HOLE_SIZE
8161 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8162#endif
8163#ifdef _PC_ALLOC_SIZE_MIN
8164 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8165#endif
8166#ifdef _PC_REC_INCR_XFER_SIZE
8167 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8168#endif
8169#ifdef _PC_REC_MAX_XFER_SIZE
8170 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8171#endif
8172#ifdef _PC_REC_MIN_XFER_SIZE
8173 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8174#endif
8175#ifdef _PC_REC_XFER_ALIGN
8176 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8177#endif
8178#ifdef _PC_SYMLINK_MAX
8179 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8180#endif
8181#ifdef _PC_XATTR_ENABLED
8182 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8183#endif
8184#ifdef _PC_XATTR_EXISTS
8185 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8186#endif
8187#ifdef _PC_TIMESTAMP_RESOLUTION
8188 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8189#endif
Fred Drakec9680921999-12-13 16:37:25 +00008190};
8191
Fred Drakec9680921999-12-13 16:37:25 +00008192static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008193conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008194{
8195 return conv_confname(arg, valuep, posix_constants_pathconf,
8196 sizeof(posix_constants_pathconf)
8197 / sizeof(struct constdef));
8198}
8199#endif
8200
8201#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008202PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008203"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008204Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008205If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008206
8207static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008208posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008209{
8210 PyObject *result = NULL;
8211 int name, fd;
8212
Fred Drake12c6e2d1999-12-14 21:25:03 +00008213 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8214 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008215 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008216
Stefan Krah0e803b32010-11-26 16:16:47 +00008217 errno = 0;
8218 limit = fpathconf(fd, name);
8219 if (limit == -1 && errno != 0)
8220 posix_error();
8221 else
8222 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008223 }
8224 return result;
8225}
8226#endif
8227
8228
8229#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008230PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008231"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008232Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008233If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008234
8235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008236posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008237{
8238 PyObject *result = NULL;
8239 int name;
8240 char *path;
8241
8242 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8243 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008245
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 errno = 0;
8247 limit = pathconf(path, name);
8248 if (limit == -1 && errno != 0) {
8249 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008250 /* could be a path or name problem */
8251 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008252 else
Stefan Krah99439262010-11-26 12:58:05 +00008253 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 }
8255 else
8256 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008257 }
8258 return result;
8259}
8260#endif
8261
8262#ifdef HAVE_CONFSTR
8263static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008264#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008266#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008267#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008269#endif
8270#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008272#endif
Fred Draked86ed291999-12-15 15:34:33 +00008273#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008275#endif
8276#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008278#endif
8279#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008281#endif
8282#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008284#endif
Fred Drakec9680921999-12-13 16:37:25 +00008285#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008287#endif
8288#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008290#endif
8291#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008293#endif
8294#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008296#endif
8297#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008299#endif
8300#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008302#endif
8303#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008305#endif
8306#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008308#endif
Fred Draked86ed291999-12-15 15:34:33 +00008309#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008311#endif
Fred Drakec9680921999-12-13 16:37:25 +00008312#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008314#endif
Fred Draked86ed291999-12-15 15:34:33 +00008315#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008317#endif
8318#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008320#endif
8321#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008323#endif
8324#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008326#endif
Fred Drakec9680921999-12-13 16:37:25 +00008327#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008329#endif
8330#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008332#endif
8333#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008335#endif
8336#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008338#endif
8339#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008341#endif
8342#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008344#endif
8345#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008347#endif
8348#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008350#endif
8351#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008353#endif
8354#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008355 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008356#endif
8357#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008358 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008359#endif
8360#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008361 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008362#endif
8363#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008364 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008365#endif
8366#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008368#endif
8369#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008371#endif
8372#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008373 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008374#endif
Fred Draked86ed291999-12-15 15:34:33 +00008375#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008376 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008377#endif
8378#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008380#endif
8381#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008382 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008383#endif
8384#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008386#endif
8387#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008389#endif
8390#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008391 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008392#endif
8393#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008395#endif
8396#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008397 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008398#endif
8399#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008400 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008401#endif
8402#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008403 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008404#endif
8405#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008406 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008407#endif
8408#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008409 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008410#endif
8411#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008413#endif
Fred Drakec9680921999-12-13 16:37:25 +00008414};
8415
8416static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008417conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008418{
8419 return conv_confname(arg, valuep, posix_constants_confstr,
8420 sizeof(posix_constants_confstr)
8421 / sizeof(struct constdef));
8422}
8423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008424PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008425"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008426Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008427
8428static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008429posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008430{
8431 PyObject *result = NULL;
8432 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008433 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008434 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008435
Victor Stinnercb043522010-09-10 23:49:04 +00008436 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8437 return NULL;
8438
8439 errno = 0;
8440 len = confstr(name, buffer, sizeof(buffer));
8441 if (len == 0) {
8442 if (errno) {
8443 posix_error();
8444 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008445 }
8446 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008447 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008448 }
8449 }
Victor Stinnercb043522010-09-10 23:49:04 +00008450
8451 if ((unsigned int)len >= sizeof(buffer)) {
8452 char *buf = PyMem_Malloc(len);
8453 if (buf == NULL)
8454 return PyErr_NoMemory();
8455 confstr(name, buf, len);
8456 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8457 PyMem_Free(buf);
8458 }
8459 else
8460 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008461 return result;
8462}
8463#endif
8464
8465
8466#ifdef HAVE_SYSCONF
8467static struct constdef posix_constants_sysconf[] = {
8468#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008470#endif
8471#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008473#endif
8474#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008476#endif
8477#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008479#endif
8480#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008482#endif
8483#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008485#endif
8486#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008487 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008488#endif
8489#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008490 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008491#endif
8492#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008493 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008494#endif
8495#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008496 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008497#endif
Fred Draked86ed291999-12-15 15:34:33 +00008498#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008499 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008500#endif
8501#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008502 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008503#endif
Fred Drakec9680921999-12-13 16:37:25 +00008504#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008505 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008506#endif
Fred Drakec9680921999-12-13 16:37:25 +00008507#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008508 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008509#endif
8510#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008511 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008512#endif
8513#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008514 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008515#endif
8516#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008518#endif
8519#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008521#endif
Fred Draked86ed291999-12-15 15:34:33 +00008522#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008523 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008524#endif
Fred Drakec9680921999-12-13 16:37:25 +00008525#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008526 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008527#endif
8528#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008529 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008530#endif
8531#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008532 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008533#endif
8534#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008535 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008536#endif
8537#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008538 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008539#endif
Fred Draked86ed291999-12-15 15:34:33 +00008540#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008541 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008542#endif
Fred Drakec9680921999-12-13 16:37:25 +00008543#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008544 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008545#endif
8546#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008547 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008548#endif
8549#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008550 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008551#endif
8552#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008553 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008554#endif
8555#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008556 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008557#endif
8558#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008559 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008560#endif
8561#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008562 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008563#endif
8564#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008565 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008566#endif
8567#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008568 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008569#endif
8570#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008571 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008572#endif
8573#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008574 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008575#endif
8576#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008577 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008578#endif
8579#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008580 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008581#endif
8582#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008583 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008584#endif
8585#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008586 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008587#endif
8588#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008589 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008590#endif
8591#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008593#endif
8594#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008595 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008596#endif
8597#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008598 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008599#endif
8600#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008601 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008602#endif
8603#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008604 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008605#endif
8606#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008607 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008608#endif
8609#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008610 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008611#endif
Fred Draked86ed291999-12-15 15:34:33 +00008612#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008613 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008614#endif
Fred Drakec9680921999-12-13 16:37:25 +00008615#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008616 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008617#endif
8618#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008619 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008620#endif
8621#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008622 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008623#endif
Fred Draked86ed291999-12-15 15:34:33 +00008624#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008625 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008626#endif
Fred Drakec9680921999-12-13 16:37:25 +00008627#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008628 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008629#endif
Fred Draked86ed291999-12-15 15:34:33 +00008630#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008632#endif
8633#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008634 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008635#endif
Fred Drakec9680921999-12-13 16:37:25 +00008636#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008637 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008638#endif
8639#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008640 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008641#endif
8642#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008643 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008644#endif
8645#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008646 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008647#endif
Fred Draked86ed291999-12-15 15:34:33 +00008648#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008649 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008650#endif
Fred Drakec9680921999-12-13 16:37:25 +00008651#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008652 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008653#endif
8654#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008655 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008656#endif
8657#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008658 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008659#endif
8660#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008661 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008662#endif
8663#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008664 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008665#endif
8666#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008667 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008668#endif
8669#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008670 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008671#endif
Fred Draked86ed291999-12-15 15:34:33 +00008672#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008673 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008674#endif
Fred Drakec9680921999-12-13 16:37:25 +00008675#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008676 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008677#endif
8678#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008679 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008680#endif
Fred Draked86ed291999-12-15 15:34:33 +00008681#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008682 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008683#endif
Fred Drakec9680921999-12-13 16:37:25 +00008684#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008685 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008686#endif
8687#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008688 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008689#endif
8690#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008691 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008692#endif
8693#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008694 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008695#endif
8696#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008697 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008698#endif
8699#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008701#endif
8702#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008703 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008704#endif
8705#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008707#endif
8708#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008709 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008710#endif
Fred Draked86ed291999-12-15 15:34:33 +00008711#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008712 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008713#endif
8714#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008715 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008716#endif
Fred Drakec9680921999-12-13 16:37:25 +00008717#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008718 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008719#endif
8720#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008721 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008722#endif
8723#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008724 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008725#endif
8726#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008727 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008728#endif
8729#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008730 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008731#endif
8732#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008733 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008734#endif
8735#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008736 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008737#endif
8738#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008739 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008740#endif
8741#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008742 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008743#endif
8744#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008745 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008746#endif
8747#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008748 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008749#endif
8750#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008751 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008752#endif
8753#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008754 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008755#endif
8756#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008757 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008758#endif
8759#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008760 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008761#endif
8762#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008763 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008764#endif
8765#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008766 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008767#endif
8768#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008769 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008770#endif
8771#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008772 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008773#endif
8774#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008775 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008776#endif
8777#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008778 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008779#endif
8780#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008781 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008782#endif
8783#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008784 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008785#endif
8786#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008787 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008788#endif
8789#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008790 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008791#endif
8792#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008793 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008794#endif
8795#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008796 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008797#endif
8798#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008799 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008800#endif
8801#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008802 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008803#endif
8804#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008805 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008806#endif
8807#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008808 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008809#endif
8810#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008811 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008812#endif
8813#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008814 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008815#endif
8816#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008817 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008818#endif
8819#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008820 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008821#endif
Fred Draked86ed291999-12-15 15:34:33 +00008822#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008823 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008824#endif
Fred Drakec9680921999-12-13 16:37:25 +00008825#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008826 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008827#endif
8828#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008829 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008830#endif
8831#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008832 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008833#endif
8834#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008835 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008836#endif
8837#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008838 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008839#endif
8840#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008841 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008842#endif
8843#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008844 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008845#endif
8846#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008847 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008848#endif
8849#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008850 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008851#endif
8852#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008853 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008854#endif
8855#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008856 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008857#endif
8858#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008859 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008860#endif
8861#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008862 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008863#endif
8864#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008865 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008866#endif
8867#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008868 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008869#endif
8870#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008871 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008872#endif
8873#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008874 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008875#endif
8876#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008877 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008878#endif
8879#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008880 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008881#endif
8882#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008883 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008884#endif
8885#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008886 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008887#endif
8888#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008889 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008890#endif
8891#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008892 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008893#endif
8894#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008895 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008896#endif
8897#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008898 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008899#endif
8900#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008901 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008902#endif
8903#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008904 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008905#endif
8906#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008907 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008908#endif
8909#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008910 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008911#endif
8912#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008913 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008914#endif
8915#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008916 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008917#endif
8918#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008919 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008920#endif
8921#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008922 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008923#endif
8924#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008925 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008926#endif
8927#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008928 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008929#endif
8930#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008931 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008932#endif
8933#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008934 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008935#endif
8936#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008937 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008938#endif
8939#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008940 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008941#endif
8942#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008943 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008944#endif
8945#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008946 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008947#endif
8948#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008949 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008950#endif
8951#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008952 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008953#endif
8954#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008955 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008956#endif
8957#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008958 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008959#endif
8960};
8961
8962static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008963conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008964{
8965 return conv_confname(arg, valuep, posix_constants_sysconf,
8966 sizeof(posix_constants_sysconf)
8967 / sizeof(struct constdef));
8968}
8969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008970PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008971"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008972Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008973
8974static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008975posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008976{
8977 PyObject *result = NULL;
8978 int name;
8979
8980 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8981 int value;
8982
8983 errno = 0;
8984 value = sysconf(name);
8985 if (value == -1 && errno != 0)
8986 posix_error();
8987 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008988 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008989 }
8990 return result;
8991}
8992#endif
8993
8994
Fred Drakebec628d1999-12-15 18:31:10 +00008995/* This code is used to ensure that the tables of configuration value names
8996 * are in sorted order as required by conv_confname(), and also to build the
8997 * the exported dictionaries that are used to publish information about the
8998 * names available on the host platform.
8999 *
9000 * Sorting the table at runtime ensures that the table is properly ordered
9001 * when used, even for platforms we're not able to test on. It also makes
9002 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009003 */
Fred Drakebec628d1999-12-15 18:31:10 +00009004
9005static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009006cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009007{
9008 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009009 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009010 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009011 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009012
9013 return strcmp(c1->name, c2->name);
9014}
9015
9016static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009017setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009018 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009019{
Fred Drakebec628d1999-12-15 18:31:10 +00009020 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009021 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009022
9023 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9024 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009025 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009026 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009027
Barry Warsaw3155db32000-04-13 15:20:40 +00009028 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009029 PyObject *o = PyLong_FromLong(table[i].value);
9030 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9031 Py_XDECREF(o);
9032 Py_DECREF(d);
9033 return -1;
9034 }
9035 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009036 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009037 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009038}
9039
Fred Drakebec628d1999-12-15 18:31:10 +00009040/* Return -1 on failure, 0 on success. */
9041static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009042setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009043{
9044#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009045 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009046 sizeof(posix_constants_pathconf)
9047 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009048 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009049 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009050#endif
9051#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009052 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009053 sizeof(posix_constants_confstr)
9054 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009055 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009056 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009057#endif
9058#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009059 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009060 sizeof(posix_constants_sysconf)
9061 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009062 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009063 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009064#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009065 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009066}
Fred Draked86ed291999-12-15 15:34:33 +00009067
9068
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009069PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009070"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009071Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009072in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009073
9074static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009075posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009076{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009077 abort();
9078 /*NOTREACHED*/
9079 Py_FatalError("abort() called from Python code didn't abort!");
9080 return NULL;
9081}
Fred Drakebec628d1999-12-15 18:31:10 +00009082
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009083#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009084PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009085"startfile(filepath [, operation]) - Start a file with its associated\n\
9086application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009087\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009088When \"operation\" is not specified or \"open\", this acts like\n\
9089double-clicking the file in Explorer, or giving the file name as an\n\
9090argument to the DOS \"start\" command: the file is opened with whatever\n\
9091application (if any) its extension is associated.\n\
9092When another \"operation\" is given, it specifies what should be done with\n\
9093the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009094\n\
9095startfile returns as soon as the associated application is launched.\n\
9096There is no option to wait for the application to close, and no way\n\
9097to retrieve the application's exit status.\n\
9098\n\
9099The filepath is relative to the current directory. If you want to use\n\
9100an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009101the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009102
9103static PyObject *
9104win32_startfile(PyObject *self, PyObject *args)
9105{
Victor Stinner8c62be82010-05-06 00:08:46 +00009106 PyObject *ofilepath;
9107 char *filepath;
9108 char *operation = NULL;
9109 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009110
Victor Stinner8c62be82010-05-06 00:08:46 +00009111 PyObject *unipath, *woperation = NULL;
9112 if (!PyArg_ParseTuple(args, "U|s:startfile",
9113 &unipath, &operation)) {
9114 PyErr_Clear();
9115 goto normal;
9116 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009117
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 if (operation) {
9119 woperation = PyUnicode_DecodeASCII(operation,
9120 strlen(operation), NULL);
9121 if (!woperation) {
9122 PyErr_Clear();
9123 operation = NULL;
9124 goto normal;
9125 }
9126 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009127
Victor Stinner8c62be82010-05-06 00:08:46 +00009128 Py_BEGIN_ALLOW_THREADS
9129 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9130 PyUnicode_AS_UNICODE(unipath),
9131 NULL, NULL, SW_SHOWNORMAL);
9132 Py_END_ALLOW_THREADS
9133
9134 Py_XDECREF(woperation);
9135 if (rc <= (HINSTANCE)32) {
9136 PyObject *errval = win32_error_unicode("startfile",
9137 PyUnicode_AS_UNICODE(unipath));
9138 return errval;
9139 }
9140 Py_INCREF(Py_None);
9141 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009142
9143normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009144 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9145 PyUnicode_FSConverter, &ofilepath,
9146 &operation))
9147 return NULL;
9148 filepath = PyBytes_AsString(ofilepath);
9149 Py_BEGIN_ALLOW_THREADS
9150 rc = ShellExecute((HWND)0, operation, filepath,
9151 NULL, NULL, SW_SHOWNORMAL);
9152 Py_END_ALLOW_THREADS
9153 if (rc <= (HINSTANCE)32) {
9154 PyObject *errval = win32_error("startfile", filepath);
9155 Py_DECREF(ofilepath);
9156 return errval;
9157 }
9158 Py_DECREF(ofilepath);
9159 Py_INCREF(Py_None);
9160 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009161}
9162#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009163
Martin v. Löwis438b5342002-12-27 10:16:42 +00009164#ifdef HAVE_GETLOADAVG
9165PyDoc_STRVAR(posix_getloadavg__doc__,
9166"getloadavg() -> (float, float, float)\n\n\
9167Return the number of processes in the system run queue averaged over\n\
9168the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9169was unobtainable");
9170
9171static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009172posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009173{
9174 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009175 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009176 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9177 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009178 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009179 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009180}
9181#endif
9182
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009183#ifdef MS_WINDOWS
9184
9185PyDoc_STRVAR(win32_urandom__doc__,
9186"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009187Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009188
9189typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9190 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9191 DWORD dwFlags );
9192typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9193 BYTE *pbBuffer );
9194
9195static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009196/* This handle is never explicitly released. Instead, the operating
9197 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009198static HCRYPTPROV hCryptProv = 0;
9199
Tim Peters4ad82172004-08-30 17:02:04 +00009200static PyObject*
9201win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009202{
Victor Stinner8c62be82010-05-06 00:08:46 +00009203 int howMany;
9204 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009205
Victor Stinner8c62be82010-05-06 00:08:46 +00009206 /* Read arguments */
9207 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9208 return NULL;
9209 if (howMany < 0)
9210 return PyErr_Format(PyExc_ValueError,
9211 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009212
Victor Stinner8c62be82010-05-06 00:08:46 +00009213 if (hCryptProv == 0) {
9214 HINSTANCE hAdvAPI32 = NULL;
9215 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009216
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 /* Obtain handle to the DLL containing CryptoAPI
9218 This should not fail */
9219 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9220 if(hAdvAPI32 == NULL)
9221 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009222
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 /* Obtain pointers to the CryptoAPI functions
9224 This will fail on some early versions of Win95 */
9225 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9226 hAdvAPI32,
9227 "CryptAcquireContextA");
9228 if (pCryptAcquireContext == NULL)
9229 return PyErr_Format(PyExc_NotImplementedError,
9230 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009231
Victor Stinner8c62be82010-05-06 00:08:46 +00009232 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9233 hAdvAPI32, "CryptGenRandom");
9234 if (pCryptGenRandom == NULL)
9235 return PyErr_Format(PyExc_NotImplementedError,
9236 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009237
Victor Stinner8c62be82010-05-06 00:08:46 +00009238 /* Acquire context */
9239 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9240 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9241 return win32_error("CryptAcquireContext", NULL);
9242 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009243
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 /* Allocate bytes */
9245 result = PyBytes_FromStringAndSize(NULL, howMany);
9246 if (result != NULL) {
9247 /* Get random data */
9248 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9249 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9250 PyBytes_AS_STRING(result))) {
9251 Py_DECREF(result);
9252 return win32_error("CryptGenRandom", NULL);
9253 }
9254 }
9255 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009256}
9257#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009258
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009259PyDoc_STRVAR(device_encoding__doc__,
9260"device_encoding(fd) -> str\n\n\
9261Return a string describing the encoding of the device\n\
9262if the output is a terminal; else return None.");
9263
9264static PyObject *
9265device_encoding(PyObject *self, PyObject *args)
9266{
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009268#if defined(MS_WINDOWS) || defined(MS_WIN64)
9269 UINT cp;
9270#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9272 return NULL;
9273 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9274 Py_INCREF(Py_None);
9275 return Py_None;
9276 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009277#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009278 if (fd == 0)
9279 cp = GetConsoleCP();
9280 else if (fd == 1 || fd == 2)
9281 cp = GetConsoleOutputCP();
9282 else
9283 cp = 0;
9284 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9285 has no console */
9286 if (cp != 0)
9287 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009288#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009289 {
9290 char *codeset = nl_langinfo(CODESET);
9291 if (codeset != NULL && codeset[0] != 0)
9292 return PyUnicode_FromString(codeset);
9293 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009294#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009295 Py_INCREF(Py_None);
9296 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009297}
9298
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009299#ifdef __VMS
9300/* Use openssl random routine */
9301#include <openssl/rand.h>
9302PyDoc_STRVAR(vms_urandom__doc__,
9303"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009304Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009305
9306static PyObject*
9307vms_urandom(PyObject *self, PyObject *args)
9308{
Victor Stinner8c62be82010-05-06 00:08:46 +00009309 int howMany;
9310 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009311
Victor Stinner8c62be82010-05-06 00:08:46 +00009312 /* Read arguments */
9313 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9314 return NULL;
9315 if (howMany < 0)
9316 return PyErr_Format(PyExc_ValueError,
9317 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009318
Victor Stinner8c62be82010-05-06 00:08:46 +00009319 /* Allocate bytes */
9320 result = PyBytes_FromStringAndSize(NULL, howMany);
9321 if (result != NULL) {
9322 /* Get random data */
9323 if (RAND_pseudo_bytes((unsigned char*)
9324 PyBytes_AS_STRING(result),
9325 howMany) < 0) {
9326 Py_DECREF(result);
9327 return PyErr_Format(PyExc_ValueError,
9328 "RAND_pseudo_bytes");
9329 }
9330 }
9331 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009332}
9333#endif
9334
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009335#ifdef HAVE_SETRESUID
9336PyDoc_STRVAR(posix_setresuid__doc__,
9337"setresuid(ruid, euid, suid)\n\n\
9338Set the current process's real, effective, and saved user ids.");
9339
9340static PyObject*
9341posix_setresuid (PyObject *self, PyObject *args)
9342{
Victor Stinner8c62be82010-05-06 00:08:46 +00009343 /* We assume uid_t is no larger than a long. */
9344 long ruid, euid, suid;
9345 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9346 return NULL;
9347 if (setresuid(ruid, euid, suid) < 0)
9348 return posix_error();
9349 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009350}
9351#endif
9352
9353#ifdef HAVE_SETRESGID
9354PyDoc_STRVAR(posix_setresgid__doc__,
9355"setresgid(rgid, egid, sgid)\n\n\
9356Set the current process's real, effective, and saved group ids.");
9357
9358static PyObject*
9359posix_setresgid (PyObject *self, PyObject *args)
9360{
Victor Stinner8c62be82010-05-06 00:08:46 +00009361 /* We assume uid_t is no larger than a long. */
9362 long rgid, egid, sgid;
9363 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9364 return NULL;
9365 if (setresgid(rgid, egid, sgid) < 0)
9366 return posix_error();
9367 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009368}
9369#endif
9370
9371#ifdef HAVE_GETRESUID
9372PyDoc_STRVAR(posix_getresuid__doc__,
9373"getresuid() -> (ruid, euid, suid)\n\n\
9374Get tuple of the current process's real, effective, and saved user ids.");
9375
9376static PyObject*
9377posix_getresuid (PyObject *self, PyObject *noargs)
9378{
Victor Stinner8c62be82010-05-06 00:08:46 +00009379 uid_t ruid, euid, suid;
9380 long l_ruid, l_euid, l_suid;
9381 if (getresuid(&ruid, &euid, &suid) < 0)
9382 return posix_error();
9383 /* Force the values into long's as we don't know the size of uid_t. */
9384 l_ruid = ruid;
9385 l_euid = euid;
9386 l_suid = suid;
9387 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009388}
9389#endif
9390
9391#ifdef HAVE_GETRESGID
9392PyDoc_STRVAR(posix_getresgid__doc__,
9393"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009394Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009395
9396static PyObject*
9397posix_getresgid (PyObject *self, PyObject *noargs)
9398{
Victor Stinner8c62be82010-05-06 00:08:46 +00009399 uid_t rgid, egid, sgid;
9400 long l_rgid, l_egid, l_sgid;
9401 if (getresgid(&rgid, &egid, &sgid) < 0)
9402 return posix_error();
9403 /* Force the values into long's as we don't know the size of uid_t. */
9404 l_rgid = rgid;
9405 l_egid = egid;
9406 l_sgid = sgid;
9407 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009408}
9409#endif
9410
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009411/* Posix *at family of functions:
9412 faccessat, fchmodat, fchownat, fstatat, futimesat,
9413 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9414 unlinkat, utimensat, mkfifoat */
9415
9416#ifdef HAVE_FACCESSAT
9417PyDoc_STRVAR(posix_faccessat__doc__,
9418"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9419Like access() but if path is relative, it is taken as relative to dirfd.\n\
9420flags is optional and can be constructed by ORing together zero or more\n\
9421of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9422If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9423is interpreted relative to the current working directory.");
9424
9425static PyObject *
9426posix_faccessat(PyObject *self, PyObject *args)
9427{
9428 PyObject *opath;
9429 char *path;
9430 int mode;
9431 int res;
9432 int dirfd, flags = 0;
9433 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9434 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9435 return NULL;
9436 path = PyBytes_AsString(opath);
9437 Py_BEGIN_ALLOW_THREADS
9438 res = faccessat(dirfd, path, mode, flags);
9439 Py_END_ALLOW_THREADS
9440 Py_DECREF(opath);
9441 return PyBool_FromLong(res == 0);
9442}
9443#endif
9444
9445#ifdef HAVE_FCHMODAT
9446PyDoc_STRVAR(posix_fchmodat__doc__,
9447"fchmodat(dirfd, path, mode, flags=0)\n\n\
9448Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9449flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9450If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9451is interpreted relative to the current working directory.");
9452
9453static PyObject *
9454posix_fchmodat(PyObject *self, PyObject *args)
9455{
9456 int dirfd, mode, res;
9457 int flags = 0;
9458 PyObject *opath;
9459 char *path;
9460
9461 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9462 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9463 return NULL;
9464
9465 path = PyBytes_AsString(opath);
9466
9467 Py_BEGIN_ALLOW_THREADS
9468 res = fchmodat(dirfd, path, mode, flags);
9469 Py_END_ALLOW_THREADS
9470 Py_DECREF(opath);
9471 if (res < 0)
9472 return posix_error();
9473 Py_RETURN_NONE;
9474}
9475#endif /* HAVE_FCHMODAT */
9476
9477#ifdef HAVE_FCHOWNAT
9478PyDoc_STRVAR(posix_fchownat__doc__,
9479"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9480Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9481flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9482If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9483is interpreted relative to the current working directory.");
9484
9485static PyObject *
9486posix_fchownat(PyObject *self, PyObject *args)
9487{
9488 PyObject *opath;
9489 int dirfd, res;
9490 long uid, gid;
9491 int flags = 0;
9492 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009493
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009494 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9495 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9496 return NULL;
9497
9498 path = PyBytes_AsString(opath);
9499
9500 Py_BEGIN_ALLOW_THREADS
9501 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9502 Py_END_ALLOW_THREADS
9503 Py_DECREF(opath);
9504 if (res < 0)
9505 return posix_error();
9506 Py_RETURN_NONE;
9507}
9508#endif /* HAVE_FCHOWNAT */
9509
9510#ifdef HAVE_FSTATAT
9511PyDoc_STRVAR(posix_fstatat__doc__,
9512"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9513Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9514flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9515If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9516is interpreted relative to the current working directory.");
9517
9518static PyObject *
9519posix_fstatat(PyObject *self, PyObject *args)
9520{
9521 PyObject *opath;
9522 char *path;
9523 STRUCT_STAT st;
9524 int dirfd, res, flags = 0;
9525
9526 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9527 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9528 return NULL;
9529 path = PyBytes_AsString(opath);
9530
9531 Py_BEGIN_ALLOW_THREADS
9532 res = fstatat(dirfd, path, &st, flags);
9533 Py_END_ALLOW_THREADS
9534 Py_DECREF(opath);
9535 if (res != 0)
9536 return posix_error();
9537
9538 return _pystat_fromstructstat(&st);
9539}
9540#endif
9541
9542#ifdef HAVE_FUTIMESAT
9543PyDoc_STRVAR(posix_futimesat__doc__,
9544"futimesat(dirfd, path, (atime, mtime))\n\
9545futimesat(dirfd, path, None)\n\n\
9546Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9547If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9548is interpreted relative to the current working directory.");
9549
9550static PyObject *
9551posix_futimesat(PyObject *self, PyObject *args)
9552{
9553 PyObject *opath;
9554 char *path;
9555 int res, dirfd;
9556 PyObject* arg;
9557
9558 struct timeval buf[2];
9559
9560 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9561 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9562 return NULL;
9563 path = PyBytes_AsString(opath);
9564 if (arg == Py_None) {
9565 /* optional time values not given */
9566 Py_BEGIN_ALLOW_THREADS
9567 res = futimesat(dirfd, path, NULL);
9568 Py_END_ALLOW_THREADS
9569 }
9570 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9571 PyErr_SetString(PyExc_TypeError,
9572 "futimesat() arg 3 must be a tuple (atime, mtime)");
9573 Py_DECREF(opath);
9574 return NULL;
9575 }
9576 else {
9577 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009578 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009579 Py_DECREF(opath);
9580 return NULL;
9581 }
9582 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009583 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009584 Py_DECREF(opath);
9585 return NULL;
9586 }
9587 Py_BEGIN_ALLOW_THREADS
9588 res = futimesat(dirfd, path, buf);
9589 Py_END_ALLOW_THREADS
9590 }
9591 Py_DECREF(opath);
9592 if (res < 0) {
9593 return posix_error();
9594 }
9595 Py_RETURN_NONE;
9596}
9597#endif
9598
9599#ifdef HAVE_LINKAT
9600PyDoc_STRVAR(posix_linkat__doc__,
9601"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9602Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9603and if dstpath is relative, it is taken as relative to dstfd.\n\
9604flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9605If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9606srcpath is interpreted relative to the current working directory. This\n\
9607also applies for dstpath.");
9608
9609static PyObject *
9610posix_linkat(PyObject *self, PyObject *args)
9611{
9612 PyObject *osrc, *odst;
9613 char *src, *dst;
9614 int res, srcfd, dstfd;
9615 int flags = 0;
9616
9617 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9618 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9619 return NULL;
9620 src = PyBytes_AsString(osrc);
9621 dst = PyBytes_AsString(odst);
9622 Py_BEGIN_ALLOW_THREADS
9623 res = linkat(srcfd, src, dstfd, dst, flags);
9624 Py_END_ALLOW_THREADS
9625 Py_DECREF(osrc);
9626 Py_DECREF(odst);
9627 if (res < 0)
9628 return posix_error();
9629 Py_RETURN_NONE;
9630}
9631#endif /* HAVE_LINKAT */
9632
9633#ifdef HAVE_MKDIRAT
9634PyDoc_STRVAR(posix_mkdirat__doc__,
9635"mkdirat(dirfd, path, mode=0o777)\n\n\
9636Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9637If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9638is interpreted relative to the current working directory.");
9639
9640static PyObject *
9641posix_mkdirat(PyObject *self, PyObject *args)
9642{
9643 int res, dirfd;
9644 PyObject *opath;
9645 char *path;
9646 int mode = 0777;
9647
9648 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9649 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9650 return NULL;
9651 path = PyBytes_AsString(opath);
9652 Py_BEGIN_ALLOW_THREADS
9653 res = mkdirat(dirfd, path, mode);
9654 Py_END_ALLOW_THREADS
9655 Py_DECREF(opath);
9656 if (res < 0)
9657 return posix_error();
9658 Py_RETURN_NONE;
9659}
9660#endif
9661
9662#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9663PyDoc_STRVAR(posix_mknodat__doc__,
9664"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9665Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9666If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9667is interpreted relative to the current working directory.");
9668
9669static PyObject *
9670posix_mknodat(PyObject *self, PyObject *args)
9671{
9672 PyObject *opath;
9673 char *filename;
9674 int mode = 0600;
9675 int device = 0;
9676 int res, dirfd;
9677 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9678 PyUnicode_FSConverter, &opath, &mode, &device))
9679 return NULL;
9680 filename = PyBytes_AS_STRING(opath);
9681 Py_BEGIN_ALLOW_THREADS
9682 res = mknodat(dirfd, filename, mode, device);
9683 Py_END_ALLOW_THREADS
9684 Py_DECREF(opath);
9685 if (res < 0)
9686 return posix_error();
9687 Py_RETURN_NONE;
9688}
9689#endif
9690
9691#ifdef HAVE_OPENAT
9692PyDoc_STRVAR(posix_openat__doc__,
9693"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9694Like open() but if path is relative, it is taken as relative to dirfd.\n\
9695If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9696is interpreted relative to the current working directory.");
9697
9698static PyObject *
9699posix_openat(PyObject *self, PyObject *args)
9700{
9701 PyObject *ofile;
9702 char *file;
9703 int flag, dirfd, fd;
9704 int mode = 0777;
9705
9706 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9707 &dirfd, PyUnicode_FSConverter, &ofile,
9708 &flag, &mode))
9709 return NULL;
9710 file = PyBytes_AsString(ofile);
9711 Py_BEGIN_ALLOW_THREADS
9712 fd = openat(dirfd, file, flag, mode);
9713 Py_END_ALLOW_THREADS
9714 Py_DECREF(ofile);
9715 if (fd < 0)
9716 return posix_error();
9717 return PyLong_FromLong((long)fd);
9718}
9719#endif
9720
9721#ifdef HAVE_READLINKAT
9722PyDoc_STRVAR(posix_readlinkat__doc__,
9723"readlinkat(dirfd, path) -> path\n\n\
9724Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9725If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9726is interpreted relative to the current working directory.");
9727
9728static PyObject *
9729posix_readlinkat(PyObject *self, PyObject *args)
9730{
9731 PyObject *v, *opath;
9732 char buf[MAXPATHLEN];
9733 char *path;
9734 int n, dirfd;
9735 int arg_is_unicode = 0;
9736
9737 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9738 &dirfd, PyUnicode_FSConverter, &opath))
9739 return NULL;
9740 path = PyBytes_AsString(opath);
9741 v = PySequence_GetItem(args, 1);
9742 if (v == NULL) {
9743 Py_DECREF(opath);
9744 return NULL;
9745 }
9746
9747 if (PyUnicode_Check(v)) {
9748 arg_is_unicode = 1;
9749 }
9750 Py_DECREF(v);
9751
9752 Py_BEGIN_ALLOW_THREADS
9753 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9754 Py_END_ALLOW_THREADS
9755 Py_DECREF(opath);
9756 if (n < 0)
9757 return posix_error();
9758
9759 if (arg_is_unicode)
9760 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9761 else
9762 return PyBytes_FromStringAndSize(buf, n);
9763}
9764#endif /* HAVE_READLINKAT */
9765
9766#ifdef HAVE_RENAMEAT
9767PyDoc_STRVAR(posix_renameat__doc__,
9768"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9769Like rename() but if oldpath is relative, it is taken as relative to\n\
9770olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9771If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9772oldpath is interpreted relative to the current working directory. This\n\
9773also applies for newpath.");
9774
9775static PyObject *
9776posix_renameat(PyObject *self, PyObject *args)
9777{
9778 int res;
9779 PyObject *opathold, *opathnew;
9780 char *opath, *npath;
9781 int oldfd, newfd;
9782
9783 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9784 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9785 return NULL;
9786 opath = PyBytes_AsString(opathold);
9787 npath = PyBytes_AsString(opathnew);
9788 Py_BEGIN_ALLOW_THREADS
9789 res = renameat(oldfd, opath, newfd, npath);
9790 Py_END_ALLOW_THREADS
9791 Py_DECREF(opathold);
9792 Py_DECREF(opathnew);
9793 if (res < 0)
9794 return posix_error();
9795 Py_RETURN_NONE;
9796}
9797#endif
9798
9799#if HAVE_SYMLINKAT
9800PyDoc_STRVAR(posix_symlinkat__doc__,
9801"symlinkat(src, dstfd, dst)\n\n\
9802Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9803If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9804is interpreted relative to the current working directory.");
9805
9806static PyObject *
9807posix_symlinkat(PyObject *self, PyObject *args)
9808{
9809 int res, dstfd;
9810 PyObject *osrc, *odst;
9811 char *src, *dst;
9812
9813 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9814 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9815 return NULL;
9816 src = PyBytes_AsString(osrc);
9817 dst = PyBytes_AsString(odst);
9818 Py_BEGIN_ALLOW_THREADS
9819 res = symlinkat(src, dstfd, dst);
9820 Py_END_ALLOW_THREADS
9821 Py_DECREF(osrc);
9822 Py_DECREF(odst);
9823 if (res < 0)
9824 return posix_error();
9825 Py_RETURN_NONE;
9826}
9827#endif /* HAVE_SYMLINKAT */
9828
9829#ifdef HAVE_UNLINKAT
9830PyDoc_STRVAR(posix_unlinkat__doc__,
9831"unlinkat(dirfd, path, flags=0)\n\n\
9832Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9833flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9834specified, unlinkat() behaves like rmdir().\n\
9835If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9836is interpreted relative to the current working directory.");
9837
9838static PyObject *
9839posix_unlinkat(PyObject *self, PyObject *args)
9840{
9841 int dirfd, res, flags = 0;
9842 PyObject *opath;
9843 char *path;
9844
9845 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9846 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9847 return NULL;
9848 path = PyBytes_AsString(opath);
9849 Py_BEGIN_ALLOW_THREADS
9850 res = unlinkat(dirfd, path, flags);
9851 Py_END_ALLOW_THREADS
9852 Py_DECREF(opath);
9853 if (res < 0)
9854 return posix_error();
9855 Py_RETURN_NONE;
9856}
9857#endif
9858
9859#ifdef HAVE_UTIMENSAT
9860PyDoc_STRVAR(posix_utimensat__doc__,
9861"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9862 (mtime_sec, mtime_nsec), flags)\n\
9863utimensat(dirfd, path, None, None, flags)\n\n\
9864Updates the timestamps of a file with nanosecond precision. If path is\n\
9865relative, it is taken as relative to dirfd.\n\
9866The second form sets atime and mtime to the current time.\n\
9867flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9868If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9869is interpreted relative to the current working directory.\n\
9870If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9871current time.\n\
9872If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9873
9874static PyObject *
9875posix_utimensat(PyObject *self, PyObject *args)
9876{
9877 PyObject *opath;
9878 char *path;
9879 int res, dirfd, flags = 0;
9880 PyObject *atime, *mtime;
9881
9882 struct timespec buf[2];
9883
9884 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9885 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9886 return NULL;
9887 path = PyBytes_AsString(opath);
9888 if (atime == Py_None && mtime == Py_None) {
9889 /* optional time values not given */
9890 Py_BEGIN_ALLOW_THREADS
9891 res = utimensat(dirfd, path, NULL, flags);
9892 Py_END_ALLOW_THREADS
9893 }
9894 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9895 PyErr_SetString(PyExc_TypeError,
9896 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9897 Py_DECREF(opath);
9898 return NULL;
9899 }
9900 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9901 PyErr_SetString(PyExc_TypeError,
9902 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9903 Py_DECREF(opath);
9904 return NULL;
9905 }
9906 else {
9907 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9908 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9909 Py_DECREF(opath);
9910 return NULL;
9911 }
9912 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9913 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9914 Py_DECREF(opath);
9915 return NULL;
9916 }
9917 Py_BEGIN_ALLOW_THREADS
9918 res = utimensat(dirfd, path, buf, flags);
9919 Py_END_ALLOW_THREADS
9920 }
9921 Py_DECREF(opath);
9922 if (res < 0) {
9923 return posix_error();
9924 }
9925 Py_RETURN_NONE;
9926}
9927#endif
9928
9929#ifdef HAVE_MKFIFOAT
9930PyDoc_STRVAR(posix_mkfifoat__doc__,
9931"mkfifoat(dirfd, path, mode=0o666)\n\n\
9932Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9933If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9934is interpreted relative to the current working directory.");
9935
9936static PyObject *
9937posix_mkfifoat(PyObject *self, PyObject *args)
9938{
9939 PyObject *opath;
9940 char *filename;
9941 int mode = 0666;
9942 int res, dirfd;
9943 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9944 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9945 return NULL;
9946 filename = PyBytes_AS_STRING(opath);
9947 Py_BEGIN_ALLOW_THREADS
9948 res = mkfifoat(dirfd, filename, mode);
9949 Py_END_ALLOW_THREADS
9950 Py_DECREF(opath);
9951 if (res < 0)
9952 return posix_error();
9953 Py_RETURN_NONE;
9954}
9955#endif
9956
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009957static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009959#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009960 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009961#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009962 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009963#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009965#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009966 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009967#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009968 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009969#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009970#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009971 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009972#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009973#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009974 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009975#endif /* HAVE_LCHMOD */
9976#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009977 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009978#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009979#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009980 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009981#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009982#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009983 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009984#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009985#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009986 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009987#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009988#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009989 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009990#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009991#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009992 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9993 METH_NOARGS, posix_getcwd__doc__},
9994 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9995 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009996#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009997#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009998 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009999#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010001#ifdef HAVE_FDOPENDIR
10002 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10003#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010004 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10005 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010006#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010007 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010008#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010009#ifdef HAVE_GETPRIORITY
10010 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10011#endif /* HAVE_GETPRIORITY */
10012#ifdef HAVE_SETPRIORITY
10013 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10014#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010015#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010016 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010017#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010018#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010019 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010020#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010021 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10022 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10023 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010025#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010026 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010027#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010028#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010029 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010030 win_symlink__doc__},
10031#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010032#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010034#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010035 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010036#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010037 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010038#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010039 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10040 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10041 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010042#ifdef HAVE_FUTIMES
10043 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10044#endif
10045#ifdef HAVE_LUTIMES
10046 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10047#endif
10048#ifdef HAVE_FUTIMENS
10049 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10050#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010051#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010052 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010053#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010054 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010055#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010056 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10057 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010058#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010059#ifdef HAVE_FEXECVE
10060 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10061#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010062#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10064 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010065#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10067 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010068#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010069#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010070#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010071 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010072#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010073#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010074 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010075#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010076#ifdef HAVE_SCHED_H
10077 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10078 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010079#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010080 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010081#endif
10082#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010083 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010084#endif
10085#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010086 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010087#endif
10088#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010089 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010090#endif
10091#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010092 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010093#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010094 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010095#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010096 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10097 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10098#endif
Benjamin Peterson2740af82011-08-02 17:41:34 -050010099#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010100#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010101 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010102#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010103#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010104 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010105#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010106#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010107 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010108#endif /* HAVE_GETEGID */
10109#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010110 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010111#endif /* HAVE_GETEUID */
10112#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010113 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010114#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010115#ifdef HAVE_GETGROUPLIST
10116 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10117#endif
Fred Drakec9680921999-12-13 16:37:25 +000010118#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010119 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010120#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010121 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010122#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010124#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010125#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010127#endif /* HAVE_GETPPID */
10128#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010130#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010131#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010133#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010134#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010135 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010136#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010137#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010139#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010140#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010141 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010142#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010143#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010144 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10145 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010146 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010147#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010148#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010149 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010150#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010151#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010152 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010153#endif /* HAVE_SETEUID */
10154#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010155 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010156#endif /* HAVE_SETEGID */
10157#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010158 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010159#endif /* HAVE_SETREUID */
10160#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010161 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010162#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010163#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010164 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010165#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010166#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010167 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010168#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010169#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010170 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010171#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010172#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010173 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010174#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010175#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010176 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010177#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010178#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010179 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010180#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010181#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010182 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010183#endif /* HAVE_WAIT3 */
10184#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010185 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010186#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010187#if defined(HAVE_WAITID) && !defined(__APPLE__)
10188 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10189#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010190#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010191 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010192#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010193#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010194 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010195#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010196#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010197 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010198#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010199#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010200 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010201#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010202#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010203 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010204#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010205#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010206 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010207#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010208 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10209 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10210 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10211 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10212 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10213 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010214#ifdef HAVE_LOCKF
10215 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10216#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010217 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10218 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010219#ifdef HAVE_READV
10220 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10221#endif
10222#ifdef HAVE_PREAD
10223 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10224#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010225 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010226#ifdef HAVE_WRITEV
10227 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10228#endif
10229#ifdef HAVE_PWRITE
10230 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10231#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010232#ifdef HAVE_SENDFILE
10233 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10234 posix_sendfile__doc__},
10235#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010236 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10237 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010238#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010239 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010240#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010241#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010242 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010243#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010244#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010245 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010246#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010247#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010248 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010249#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010250#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010251 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10252 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10253 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010254#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010255#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010256 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010257#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010258#ifdef HAVE_TRUNCATE
10259 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10260#endif
10261#ifdef HAVE_POSIX_FALLOCATE
10262 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10263#endif
10264#ifdef HAVE_POSIX_FADVISE
10265 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10266#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010267#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010269#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010270#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010272#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010273 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010274#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010275 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010276#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010277#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010278 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010279#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010280#ifdef HAVE_SYNC
10281 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10282#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010283#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010284 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010285#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010286#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010287#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010288 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010289#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010290#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010291 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010292#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010293#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010294 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010295#endif /* WIFSTOPPED */
10296#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010297 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010298#endif /* WIFSIGNALED */
10299#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010300 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010301#endif /* WIFEXITED */
10302#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010303 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010304#endif /* WEXITSTATUS */
10305#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010306 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010307#endif /* WTERMSIG */
10308#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010309 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010310#endif /* WSTOPSIG */
10311#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010312#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010313 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010314#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010315#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010316 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010317#endif
Fred Drakec9680921999-12-13 16:37:25 +000010318#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010319 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010320#endif
10321#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010322 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010323#endif
10324#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010325 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010326#endif
10327#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010328 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010329#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010330 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010331#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010332 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010333 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010334 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010335 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010336 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010337#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010338#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010339 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010340#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010341 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010342 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010343 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010344 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010345 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010346 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010347#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010348 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010349#endif
10350#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010351 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010352#endif
10353#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010354 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010355#endif
10356#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010357 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010358#endif
10359
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010360/* posix *at family of functions */
10361#ifdef HAVE_FACCESSAT
10362 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10363#endif
10364#ifdef HAVE_FCHMODAT
10365 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10366#endif /* HAVE_FCHMODAT */
10367#ifdef HAVE_FCHOWNAT
10368 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10369#endif /* HAVE_FCHOWNAT */
10370#ifdef HAVE_FSTATAT
10371 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10372#endif
10373#ifdef HAVE_FUTIMESAT
10374 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10375#endif
10376#ifdef HAVE_LINKAT
10377 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10378#endif /* HAVE_LINKAT */
10379#ifdef HAVE_MKDIRAT
10380 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10381#endif
10382#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10383 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10384#endif
10385#ifdef HAVE_OPENAT
10386 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10387#endif
10388#ifdef HAVE_READLINKAT
10389 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10390#endif /* HAVE_READLINKAT */
10391#ifdef HAVE_RENAMEAT
10392 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10393#endif
10394#if HAVE_SYMLINKAT
10395 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10396#endif /* HAVE_SYMLINKAT */
10397#ifdef HAVE_UNLINKAT
10398 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10399#endif
10400#ifdef HAVE_UTIMENSAT
10401 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10402#endif
10403#ifdef HAVE_MKFIFOAT
10404 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10405#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010406 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010407};
10408
10409
Barry Warsaw4a342091996-12-19 23:50:02 +000010410static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010411ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010412{
Victor Stinner8c62be82010-05-06 00:08:46 +000010413 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010414}
10415
Guido van Rossumd48f2521997-12-05 22:19:34 +000010416#if defined(PYOS_OS2)
10417/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010418static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010419{
10420 APIRET rc;
10421 ULONG values[QSV_MAX+1];
10422 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010423 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010424
10425 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010426 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010427 Py_END_ALLOW_THREADS
10428
10429 if (rc != NO_ERROR) {
10430 os2_error(rc);
10431 return -1;
10432 }
10433
Fred Drake4d1e64b2002-04-15 19:40:07 +000010434 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10435 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10436 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10437 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10438 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10439 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10440 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010441
10442 switch (values[QSV_VERSION_MINOR]) {
10443 case 0: ver = "2.00"; break;
10444 case 10: ver = "2.10"; break;
10445 case 11: ver = "2.11"; break;
10446 case 30: ver = "3.00"; break;
10447 case 40: ver = "4.00"; break;
10448 case 50: ver = "5.00"; break;
10449 default:
Tim Peters885d4572001-11-28 20:27:42 +000010450 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010451 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010452 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010453 ver = &tmp[0];
10454 }
10455
10456 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010457 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010458 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010459
10460 /* Add Indicator of Which Drive was Used to Boot the System */
10461 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10462 tmp[1] = ':';
10463 tmp[2] = '\0';
10464
Fred Drake4d1e64b2002-04-15 19:40:07 +000010465 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010466}
10467#endif
10468
Brian Curtin52173d42010-12-02 18:29:18 +000010469#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010470static int
Brian Curtin52173d42010-12-02 18:29:18 +000010471enable_symlink()
10472{
10473 HANDLE tok;
10474 TOKEN_PRIVILEGES tok_priv;
10475 LUID luid;
10476 int meth_idx = 0;
10477
10478 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010479 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010480
10481 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010482 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010483
10484 tok_priv.PrivilegeCount = 1;
10485 tok_priv.Privileges[0].Luid = luid;
10486 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10487
10488 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10489 sizeof(TOKEN_PRIVILEGES),
10490 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010491 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010492
Brian Curtin3b4499c2010-12-28 14:31:47 +000010493 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10494 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010495}
10496#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10497
Barry Warsaw4a342091996-12-19 23:50:02 +000010498static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010499all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010500{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010501#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010502 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010503#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010504#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010505 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010506#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010507#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010508 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010509#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010510#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010511 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010512#endif
Fred Drakec9680921999-12-13 16:37:25 +000010513#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010514 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010515#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010516#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010517 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010518#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010519#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010520 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010521#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010522#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010523 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010524#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010525#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010526 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010527#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010528#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010529 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010530#endif
10531#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010532 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010533#endif
10534#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000010535 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010536#endif
10537#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000010538 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010539#endif
10540#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010541 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010542#endif
10543#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000010544 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010545#endif
10546#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010547 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010548#endif
10549#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010550 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010551#endif
10552#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010553 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010554#endif
10555#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010556 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010557#endif
10558#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010559 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010560#endif
10561#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000010562 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010563#endif
10564#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010565 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010566#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010567#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010568 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010569#endif
10570#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000010571 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010572#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010573#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010574 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010575#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010576#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010577 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010578#endif
10579#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010580 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010581#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010582#ifdef PRIO_PROCESS
10583 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
10584#endif
10585#ifdef PRIO_PGRP
10586 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
10587#endif
10588#ifdef PRIO_USER
10589 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
10590#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010591#ifdef O_CLOEXEC
10592 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10593#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010594/* posix - constants for *at functions */
10595#ifdef AT_SYMLINK_NOFOLLOW
10596 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10597#endif
10598#ifdef AT_EACCESS
10599 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10600#endif
10601#ifdef AT_FDCWD
10602 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
10603#endif
10604#ifdef AT_REMOVEDIR
10605 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
10606#endif
10607#ifdef AT_SYMLINK_FOLLOW
10608 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
10609#endif
10610#ifdef UTIME_NOW
10611 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10612#endif
10613#ifdef UTIME_OMIT
10614 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10615#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010616
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010617
Tim Peters5aa91602002-01-30 05:46:57 +000010618/* MS Windows */
10619#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 /* Don't inherit in child processes. */
10621 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010622#endif
10623#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010624 /* Optimize for short life (keep in memory). */
10625 /* MS forgot to define this one with a non-underscore form too. */
10626 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010627#endif
10628#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 /* Automatically delete when last handle is closed. */
10630 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010631#endif
10632#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010633 /* Optimize for random access. */
10634 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010635#endif
10636#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010637 /* Optimize for sequential access. */
10638 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010639#endif
10640
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010641/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010642#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010643 /* Send a SIGIO signal whenever input or output
10644 becomes available on file descriptor */
10645 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010646#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010647#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010648 /* Direct disk access. */
10649 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010650#endif
10651#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 /* Must be a directory. */
10653 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010654#endif
10655#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 /* Do not follow links. */
10657 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010658#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010659#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010660 /* Do not update the access time. */
10661 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010662#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000010663
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010665#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010667#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010668#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010669 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010670#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010671#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010673#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010674#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010676#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010677#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010679#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010680#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010682#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010683#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010685#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010686#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010687 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010688#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010689#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010690 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010691#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010692#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010694#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010695#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010696 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010697#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010698#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010699 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010700#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010701#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010702 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010703#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010704#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010705 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010706#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010707#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010708 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010709#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010710#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010711 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010712#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010713#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010715#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010716
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010717 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010718#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010719 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010720#endif /* ST_RDONLY */
10721#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010722 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010723#endif /* ST_NOSUID */
10724
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010725 /* FreeBSD sendfile() constants */
10726#ifdef SF_NODISKIO
10727 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10728#endif
10729#ifdef SF_MNOWAIT
10730 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10731#endif
10732#ifdef SF_SYNC
10733 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10734#endif
10735
Ross Lagerwall7807c352011-03-17 20:20:30 +020010736 /* constants for posix_fadvise */
10737#ifdef POSIX_FADV_NORMAL
10738 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10739#endif
10740#ifdef POSIX_FADV_SEQUENTIAL
10741 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10742#endif
10743#ifdef POSIX_FADV_RANDOM
10744 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10745#endif
10746#ifdef POSIX_FADV_NOREUSE
10747 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10748#endif
10749#ifdef POSIX_FADV_WILLNEED
10750 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10751#endif
10752#ifdef POSIX_FADV_DONTNEED
10753 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10754#endif
10755
10756 /* constants for waitid */
10757#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10758 if (ins(d, "P_PID", (long)P_PID)) return -1;
10759 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10760 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10761#endif
10762#ifdef WEXITED
10763 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10764#endif
10765#ifdef WNOWAIT
10766 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10767#endif
10768#ifdef WSTOPPED
10769 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10770#endif
10771#ifdef CLD_EXITED
10772 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10773#endif
10774#ifdef CLD_DUMPED
10775 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10776#endif
10777#ifdef CLD_TRAPPED
10778 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10779#endif
10780#ifdef CLD_CONTINUED
10781 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10782#endif
10783
10784 /* constants for lockf */
10785#ifdef F_LOCK
10786 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10787#endif
10788#ifdef F_TLOCK
10789 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10790#endif
10791#ifdef F_ULOCK
10792 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10793#endif
10794#ifdef F_TEST
10795 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10796#endif
10797
10798 /* constants for futimens */
10799#ifdef UTIME_NOW
10800 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10801#endif
10802#ifdef UTIME_OMIT
10803 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10804#endif
10805
Guido van Rossum246bc171999-02-01 23:54:31 +000010806#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010807#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010808 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10809 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10810 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10811 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10812 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10813 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10814 if (ins(d, "P_PM", (long)P_PM)) return -1;
10815 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10816 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10817 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10818 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10819 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10820 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10821 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10822 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10823 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10824 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10825 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10826 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10827 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010828#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010829 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10830 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10831 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10832 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10833 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010834#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010835#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010836
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010837#ifdef HAVE_SCHED_H
10838 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
10839 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
10840#ifdef SCHED_SPORADIC
10841 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
10842#endif
10843 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
10844#ifdef SCHED_BATCH
10845 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
10846#endif
10847#ifdef SCHED_IDLE
10848 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
10849#endif
10850#ifdef SCHED_RESET_ON_FORK
10851 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
10852#endif
10853#endif
10854
Guido van Rossumd48f2521997-12-05 22:19:34 +000010855#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010856 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010857#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010858 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010859}
10860
10861
Tim Peters5aa91602002-01-30 05:46:57 +000010862#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010863#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010864#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010865
10866#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010867#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010868#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010869
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010870#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010871#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010872#define MODNAME "posix"
10873#endif
10874
Martin v. Löwis1a214512008-06-11 05:26:20 +000010875static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010876 PyModuleDef_HEAD_INIT,
10877 MODNAME,
10878 posix__doc__,
10879 -1,
10880 posix_methods,
10881 NULL,
10882 NULL,
10883 NULL,
10884 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010885};
10886
10887
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010888PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010889INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010890{
Victor Stinner8c62be82010-05-06 00:08:46 +000010891 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010892
Brian Curtin52173d42010-12-02 18:29:18 +000010893#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010894 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010895#endif
10896
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 m = PyModule_Create(&posixmodule);
10898 if (m == NULL)
10899 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010900
Victor Stinner8c62be82010-05-06 00:08:46 +000010901 /* Initialize environ dictionary */
10902 v = convertenviron();
10903 Py_XINCREF(v);
10904 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10905 return NULL;
10906 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010907
Victor Stinner8c62be82010-05-06 00:08:46 +000010908 if (all_ins(m))
10909 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010910
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 if (setup_confname_tables(m))
10912 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010913
Victor Stinner8c62be82010-05-06 00:08:46 +000010914 Py_INCREF(PyExc_OSError);
10915 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010916
Benjamin Peterson2740af82011-08-02 17:41:34 -050010917#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010918 if (PyType_Ready(&cpu_set_type) < 0)
10919 return NULL;
10920 Py_INCREF(&cpu_set_type);
10921 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050010922#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010923
Guido van Rossumb3d39562000-01-31 18:41:26 +000010924#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 if (posix_putenv_garbage == NULL)
10926 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010927#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010928
Victor Stinner8c62be82010-05-06 00:08:46 +000010929 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010930#if defined(HAVE_WAITID) && !defined(__APPLE__)
10931 waitid_result_desc.name = MODNAME ".waitid_result";
10932 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10933#endif
10934
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 stat_result_desc.name = MODNAME ".stat_result";
10936 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10937 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10938 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10939 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10940 structseq_new = StatResultType.tp_new;
10941 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010942
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 statvfs_result_desc.name = MODNAME ".statvfs_result";
10944 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010945#ifdef NEED_TICKS_PER_SECOND
10946# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010948# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010950# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010951 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010952# endif
10953#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010954
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050010955#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010956 sched_param_desc.name = MODNAME ".sched_param";
10957 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
10958 SchedParamType.tp_new = sched_param_new;
10959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010960 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010961#if defined(HAVE_WAITID) && !defined(__APPLE__)
10962 Py_INCREF((PyObject*) &WaitidResultType);
10963 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10964#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010965 Py_INCREF((PyObject*) &StatResultType);
10966 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10967 Py_INCREF((PyObject*) &StatVFSResultType);
10968 PyModule_AddObject(m, "statvfs_result",
10969 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050010970
10971#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010972 Py_INCREF(&SchedParamType);
10973 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050010974#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010975 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010976
10977#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010978 /*
10979 * Step 2 of weak-linking support on Mac OS X.
10980 *
10981 * The code below removes functions that are not available on the
10982 * currently active platform.
10983 *
10984 * This block allow one to use a python binary that was build on
10985 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10986 * OSX 10.4.
10987 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010988#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 if (fstatvfs == NULL) {
10990 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10991 return NULL;
10992 }
10993 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010994#endif /* HAVE_FSTATVFS */
10995
10996#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 if (statvfs == NULL) {
10998 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10999 return NULL;
11000 }
11001 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011002#endif /* HAVE_STATVFS */
11003
11004# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011005 if (lchown == NULL) {
11006 if (PyObject_DelAttrString(m, "lchown") == -1) {
11007 return NULL;
11008 }
11009 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011010#endif /* HAVE_LCHOWN */
11011
11012
11013#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011014 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011015
Guido van Rossumb6775db1994-08-01 11:34:53 +000011016}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011017
11018#ifdef __cplusplus
11019}
11020#endif